⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 maplayer.cpp

📁 一个英国人写的GIS查看/编辑工具。支持标准的shapefile地图文件格式和coverage地图文件格式。同时可以编辑相应的dbf文件。
💻 CPP
📖 第 1 页 / 共 4 页
字号:
   for (int i = 0; i < m_aAttr.GetSize(); i++)
   {
      for (int j = 0; j < aAttr.GetSize(); j++)
      {
         if (m_aAttr[i].m_lAttr == aAttr.GetAt(j)->GetAttrId() &&
             aAttr[j]->GetDataType() == BDNUMBER)      
         {
             dValue = *aAttr[j]->GetDouble();             
         }
      };
   };

   // Determine the map objects to display

   for (i = 0; i < m_aAttr.GetSize() && bOK; i++)
   {
      for (int j = 0; j < aAttr.GetSize() && bOK; j++)
      {
         if (m_aAttr[i].m_lAttr == aAttr.GetAt(j)->GetAttrId())      
         {
            CAttribute* pAttr = aAttr[j];
            if (pAttr->GetFTypeId() == m_aAttr[i].m_lFType)
            {
               // Set the text associated with the object

               pMapLayerObj->SetText(sAttrName);

               // Set the date associated with the object

               pMapLayerObj->SetDate(aAttr.m_lDate);

               if (pAttr->GetDataType() == BDMAPLINES)
               {                        

                  CLongLines* pMapLines = new CLongLines(*pAttr->GetLongBinary());

                  // Check if polygon                  
                  

                  if (!IsPolygon(pMapLines) && !m_bOnLoad)
                  {
                     m_prop.m_nPattern = BS_NULL;
                     m_prop.m_crLine = -1;
                     m_prop.m_bPolygon = FALSE;

                  }
              
                  if (pMapLines != NULL)
                  {
                     pMapLayerObj->SetMapObject(pMapLines);                        
                     pMapLayerObj->SetDataType(BDMAPLINES);

                     if (dValue != AFX_RFX_DOUBLE_PSEUDO_NULL)
                     {
                        pMapLayerObj->SetValue(dValue);
                     }
                     bFound = TRUE;
                  } else
                  {
                     bOK = FALSE;
                     AfxMessageBox(BDString(IDS_OUTOFMEMORY));
                  }
                  break;
               }

               // Add coordinates to the map

               else if (pAttr->GetDataType() == BDCOORD)
               {	     
                  // Set polygon to true for symbols so that custom
                  // fills can be used

                  if (!m_bOnLoad) m_prop.m_bPolygon = TRUE;

                  CCoord* pCoord = new CCoord(*pAttr->GetCoord());
                  if (pCoord != NULL)
                  {
                     if (!pCoord->IsNull())
                     {
                        pMapLayerObj->SetMapObject(pCoord);                 
                        if (dValue != AFX_RFX_DOUBLE_PSEUDO_NULL)
                        {
                           pMapLayerObj->SetValue(dValue);
                        }
                        bFound = TRUE;
                        pMapLayerObj->SetDataType(BDCOORD);                     
                     };
                  } else
                  {
                     bOK = FALSE;
                     AfxMessageBox(BDString(IDS_OUTOFMEMORY));
                  }
                  break;
               }  
               
               // Image

               else if (pAttr->GetDataType() == BDIMAGE)
               {
                  if (!m_bOnLoad) m_prop.m_bPolygon = FALSE;
                  
                  CImageFile* pImage = new CImageFile;

                  m_prop.m_nSymbol = CComboBoxSymbol::none;
                  
                  if (pImage != NULL)
                  {
                     pImage->Initialise(*pAttr->GetLongBinary());                 
                     
                     pMapLayerObj->SetMapObject(pImage);                 
                     if (dValue != AFX_RFX_DOUBLE_PSEUDO_NULL)
                     {
                        pMapLayerObj->SetValue(dValue);
                     }
                     bFound = TRUE;
                     pMapLayerObj->SetDataType(BDIMAGE);                                                               
                  } else
                  {
                     bOK = FALSE;
                     AfxMessageBox(BDString(IDS_OUTOFMEMORY));
                  }
                  break;
               }

 
            };

         };
      };     
   };   

   // If not found then error (e.g. if produced using a query statistic)

   if (!bFound) bOK = FALSE;
   
   return bOK;
}

/////////////////////////////////////////////////////////////////////////////
//
// Check if maplines are a polygon
//

BOOL CMapLayer::IsPolygon(CLongLines* pMapLines)
{   
   int j = 0;
   for (int i = 0; i < pMapLines->GetSize(); i++)
   {      
      CLongCoord& coord = pMapLines->GetAt(i);
      if (coord.IsNull() && i > 0)
      {
         if (pMapLines->GetAt(j).x != pMapLines->GetAt(i-1).x ||
             pMapLines->GetAt(j).y != pMapLines->GetAt(i-1).y) return FALSE;
         else j = i+1;         
      }
   }
   return TRUE;
}

/////////////////////////////////////////////////////////////////////////////
//
// Determines whether feature type name and parent feature type name are 
// required, also determines the fastest way of querying the data - query
// each datum required or retrieve all data and search
//

BOOL CMapLayer::LoadData(BOOL bOnLoad)
{
   // Create query

   m_bOnLoad = bOnLoad;

   // Load data from overlay file

   if (m_sFile != "")
   {
      if (m_sFile.Right(4).CompareNoCase(".jpg") == 0 || 
          m_sFile.Right(4).CompareNoCase(".png") == 0)
      {
         // Indicate to load georeferences, only run utility to edit if not
         // loading from .nrm file

         int nFlag = CImageFile::GeoRef;
         if (bOnLoad) nFlag |= CImageFile::OnLoad;
                 
         CImageFile* pImage = new CImageFile;
         pImage->Open(m_sFile, nFlag);

         CMapLayerObj* pMapLayerObj = new CMapLayerObj;
         
         pMapLayerObj->SetMapObject(pImage); 
         pMapLayerObj->SetDataType(BDIMAGE);
         

         Add(pMapLayerObj);


      } else
      {
         CDlgAddOverlay dlg(this, m_sFile, m_sColumn);
	      return dlg.DoModal() == IDOK;
      };
   }

   // Create query from selections (probably no longer needed!)

   else
   {
	   if (m_pQuery == NULL)
	   {        
		  m_pQuery = new CQuery(m_lFType, m_aAttr, m_alFeatures);
	   }
	   // Queries

	   if (m_pQuery != NULL)
	   {
		  return Initialise(m_pQuery);
	   } 
   };

   m_bOnLoad = FALSE;

   return FALSE;   
};


///////////////////////////////////////////////////////////////////////////////
//
// Update an existing map object
//

void CMapLayerArray::Update(CMapLayer* pMapLayer, CMapLayerObj* pMapLayerObj)
{        
   CAttrArray aAttr;
   CString sFeature, sParentFeature;

   pMapLayerObj->FreeMem();

   //BDGetApp()->SetProgressBar(NULL);
   
   aAttr.m_lFeature = pMapLayerObj->GetFeature();
   aAttr.m_lFType = pMapLayer->GetFType();
   
   // Retrieve feature

   CFeature feature;
   feature.m_lId = pMapLayerObj->GetFeature();
   feature.m_lFeatureTypeId = pMapLayer->GetFType();
   BOOL bFound = BDFeature(BDHandle(), &feature, BDSELECT);
   BDEnd(BDHandle());
   
   // Requery the data for the one feature
   
	CMapLayer maplayer;	   

   // Handle queries

	if (pMapLayer->GetQuery() != NULL)
	{
		// Create a copy of the original query with only the updated feature

      CQuery* pQuery = new CQuery; // Delete in CMapLayer destructor
		*pQuery = *pMapLayer->GetQuery();
		CQueryElement* pElement = pQuery;
		while (pElement != NULL)
		{
		  if (pElement->GetDataType() == BDFEATURE)
		  {
			 CQueryLink* pLink = (CQueryLink*)pElement;
			 CQueryFeature qf = feature;
			 qf.SetSelected();
			 pLink->m_aFeatures.RemoveAll();			 				 
			 pLink->m_aFeatures.Add(qf);
			 break;
		  }
		  pElement = pElement->GetNextQuery();
		}
		maplayer.Initialise(pQuery);       
	} 
	// Add data to existing layer

	for (int i = 0; i < maplayer.GetSize(); i++)
	{	  
	  pMapLayer->Add(maplayer.GetAt(i));   
	}

   // Prevents deletion in destructor

	maplayer.CArray <CMapLayerObj*,  CMapLayerObj*>::RemoveAll(); 

   // Need to add the feature to the existing query so that it is saved
   // with the layer

   CQueryElement* pElement = pMapLayer->GetQuery();
	while (pElement != NULL)
   {
	   if (pElement->GetDataType() == BDFEATURE)
      {
          CQueryLink* pLink = (CQueryLink*)pElement;
			 CQueryFeature qf = feature;
			 qf.SetSelected();			 
			 pLink->m_aFeatures.Add(qf);
			 break;
      };	
      pElement = pElement->GetNextQuery();      
   };
   
   // Tidy up, delete the map layer object
      
   //pMapLayer = pMapLayerE;

   for (i = 0; i < pMapLayer->GetSize(); i++)
   {
      if (pMapLayerObj == pMapLayer->GetAt(i))
      {
         pMapLayer->RemoveAt(i);
         delete pMapLayerObj;
         break;
      }
   }      
}


///////////////////////////////////////////////////////////////////////////////

void CMapLayerArray::Write(LPCSTR sFile)
{
   CString s;

   FILE* pFile = fopen(sFile, "w");
   if (pFile != NULL)
   {
	  fprintf(pFile,"DataSource=%s\n",BDGetDataSource());
    
     // Write map extent

     CViewMap* pViewMap = BDGetDocMap()->GetViewMap();
     fprintf(pFile,"Zoom=%lf\n", pViewMap->m_dZoom);
     fprintf(pFile,"OffX=%lf\n", pViewMap->m_dOffX);
     fprintf(pFile,"OffY=%lf\n", pViewMap->m_dOffY);

     fprintf(pFile,"title=\"%s\"\n",m_sTitle);
     BDWrite(pFile, &m_logfontT, "logfontT");
     BDWrite(pFile, &m_logfontL, "logfontL");
     fprintf(pFile,"legend1Font=%i\n", m_bLegend1Font);

     // Map Grid

     fprintf(pFile, "gridtype=%d\n", m_mapgrid.m_nType);
     BDWrite(pFile, &m_mapgrid.m_logfont, "logfont");
     s = m_mapgrid.m_style.AsString();
     fprintf(pFile, "mapstyle=%s\n", (LPCSTR)s);
     fprintf(pFile,"latitude=%d,%d,%d\n", m_mapgrid.m_nDegLat, m_mapgrid.m_nMinLat, m_mapgrid.m_nSecLat);
     fprintf(pFile,"longitude=%d,%d,%d\n", m_mapgrid.m_nDegLng, m_mapgrid.m_nMinLng, m_mapgrid.m_nSecLng);
          
     // Write layers

      for (int i = 0; i < GetSize(); i++)
      {
         CMapLayer* pMapLayer = (CMapLayer*)GetAt(i);
         pMapLayer->Write(pFile);         
      }
      fclose(pFile);
   }   
}

///////////////////////////////////////////////////////////////////////////////
// 
// Reads data from file into structure.  
//

BOOL CMapLayerArray::Read(LPCSTR sFile)
{
   BOOL bOK = TRUE;
   int nRet, index;
   CString s;
   CString sDataSource;

   RemoveAll();

   AfxGetApp()->BeginWaitCursor();

   FILE* pFile = fopen(sFile, "r");

   TRY
   {      
      if (pFile != NULL)
      {
		 // Read the data source name

         sDataSource = BDNextStr(pFile);

         if (sDataSource != BDGetDataSource())
         {
            m_sError = BDString(IDS_FILEDSN) + ": " + sDataSource;            
            AfxThrowFileException(0,0,"");
         }


     // Skip layout (NRDB Pro 1 layout format)

     CRect rect;
     BDNext(pFile, rect, "rectMap");        
     BDNext(pFile, rect, "rectTitle");           
     BDNext(pFile, rect, "rectLegend");              
     BDNext(pFile, rect, "rectComments");              
     BDNext(pFile, rect, "rectNorthArrow");              
     BDNext(pFile, rect, "rectScaleBar");                   
     CDWordArray adw;
     BDNext(pFile, adw, "lineStyleMap");     
     BDNext(pFile, adw, "lineStyleLegend");     

     // Read map zoom

     CViewMap* pViewMap = BDGetDocMap()->GetViewMap();
     pViewMap->m_dZoom = BDNextDouble(pFile, "Zoom", 1);     
     pViewMap->m_dOffX = BDNextDouble(pFile, "OffX", (pViewMap->m_dMinX + pViewMap->m_dMaxX)/2);          
     pViewMap->m_dOffY = BDNextDouble(pFile, "OffY", (pViewMap->m_dMinY + pViewMap->m_dMaxY)/2);          
    
     m_sTitle = BDNextStr(pFile,"title");
     BDNext(pFile, &m_logfontT, "logfontT");
     BDNext(pFile, &m_logfontL, "logfontL");
     m_bLegend1Font = BDNextInt(pFile, "legend1Font");

     // Read Map Grid

     m_mapgrid.m_nType = BDNextInt(pFile, "gridtype", -1);
     if (m_mapgrid.m_nType != -1)
     {
        BDNext(pFile, &m_mapgrid.m_logfont, "logfont");
        BDNextStr(pFile, FALSE); 
        BDNext(pFile, m_mapgrid.m_style);
        m_mapgrid.m_nDegLat = BDNextInt(pFile, TRUE);
        m_mapgrid.m_nMinLat = BDNextInt(pFile, FALSE);
        m_mapgrid.m_nSecLat = BDNextInt(pFile, FALSE);
        m_mapgrid.m_nDegLng = BDNextInt(pFile, TRUE);
        m_mapgrid.m_nMinLng = BDNextInt(pFile, FALSE);
        m_mapgrid.m_nSecLng = BDNextInt(pFile, FALSE);
     } else
     {
        m_mapgrid = CMapGrid();
     }
     
         // For each layer read is values from the file 
         // and add it to the list
      
         do 
         {
            // Add item to list first (so it is deleted if an exception occurs)

            CMapLayer* pMapLayer = new CMapLayer;
            if (pMapLayer != NULL)
            {
               index = Add(pMapLayer);

               nRet = pMapLayer->Read(this, pFile);			

               // Load data for the layer
               if (nRet == 1)
               {                  
                  pMapLayer->LoadData(TRUE);                
               }
               else if (nRet == -1)                 
               {          
                  RemoveAt(index);
                  delete pMapLayer;
               } else
               {
                  bOK = FALSE;
               }
            } else
            {
               bOK = FALSE;
            }
         }
         while (nRet == 1 && bOK);        
      }      
   } 
   CATCH (CFileException, pEx)
   {
      bOK = FALSE;
   }
   END_CATCH

   // Delete all values on error

   if (!bOK) RemoveAll();

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -