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

📄 dlgmaplayers.cpp

📁 一个英国人写的GIS查看/编辑工具。支持标准的shapefile地图文件格式和coverage地图文件格式。同时可以编辑相应的dbf文件。
💻 CPP
字号:
//////////////////////////////////////////////////////
//
// NRDB Pro - Spatial database and mapping application
//
// Copyright (c) 1989-2004 Richard D. Alexander
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
//
// NRDB Pro is part of the Natural Resources Database Project 
// 
// Homepage: http://www.nrdb.co.uk/
// Users' Forum: http://nrdb.mypalawan.info/
// 

#include "stdafx.h"
#include <math.h>

#include "nrdb.h"
#include "DlgMapLayers.h"
#include "dlgaddlayer.h"
#include "sheetquery.h"
#include "importmaplines.h"
#include "dlgprogress.h"
#include "viewmap.h"
#include "sheetmapprop.h"
#include "mainfrm.h"
#include "shapefile.h"
#include "comboboxsymbol.h"
#include "dlgaddoverlay.h"
#include "dlgdigitisetype.h"
#include "shapefile.h"
#include "dlgftypeattr.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CDlgMapLayers dialog


CDlgMapLayers::CDlgMapLayers(CMapLayerArray* paMapObject, CDocMap* pDoc, 
                             CWnd* pParent /*=NULL*/)
	: CDialog(CDlgMapLayers::IDD, pParent)
{

   m_paMapObject = paMapObject;
   m_pDocMap = pDoc;

	//{{AFX_DATA_INIT(CDlgMapLayers)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
}

CDlgMapLayers::~CDlgMapLayers()
{
}

void CDlgMapLayers::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDlgMapLayers)
	DDX_Control(pDX, IDC_AUTOUPDATE, m_ckAutoUpdate);
	DDX_Control(pDX, IDC_MAPLAYER, m_lbMapLayer);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CDlgMapLayers, CDialog)
	//{{AFX_MSG_MAP(CDlgMapLayers)
	ON_BN_CLICKED(IDC_ADD, OnAdd)
	ON_BN_CLICKED(IDC_DELETE, OnDelete)
	ON_NOTIFY(UDN_DELTAPOS, IDC_UPDOWN, OnDeltaposUpdown)
	ON_BN_CLICKED(IDC_PROPERTIES, OnProperties)	
	ON_BN_CLICKED(IDC_QUERY, OnQuery)
	ON_WM_CLOSE()
	ON_BN_CLICKED(IDC_LEGEND, OnLegend)
	ON_LBN_DBLCLK(IDC_MAPLAYER, OnDblclkMaplayer)
	ON_BN_CLICKED(IDC_OVERLAYS, OnOverlays)
	ON_BN_CLICKED(IDC_NEW, OnNew)
	ON_LBN_SELCHANGE(IDC_MAPLAYER, OnSelchangeMaplayer)
	ON_BN_CLICKED(IDC_AUTOUPDATE, OnAutoupdate)
	//}}AFX_MSG_MAP
  END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDlgMapLayers message handlers

BOOL CDlgMapLayers::OnInitDialog() 
{
	CDialog::OnInitDialog();

   // Initialise tick list box as single selection

   m_lbMapLayer.Init(FALSE);

   BDSetActiveWnd(GetSafeHwnd());

   // First list of layers

   InitLayers(m_paMapObject->GetDefault(), TRUE);

   // Option to switch off auto updating of map (must happen before previous reports)

   m_ckAutoUpdate.SetCheck(m_pDocMap->GetViewMap()->IsAutoUpdate());


   // Previous reports

   if (BDGetPreviousReport() == CNRDB::maplayer)
   {
      OnAdd();
      OnOK();
   };
   if (BDGetPreviousReport() == CNRDB::mapquery) 
   {
      OnQuery();
      OnOK();
   };

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

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

void CDlgMapLayers::InitLayers(int iSel, BOOL bInit)
{
   m_lbMapLayer.ResetContent();

   for (int i = 0; i < m_paMapObject->GetSize(); i++)
   {
      int index = m_lbMapLayer.AddString(m_paMapObject->GetAt(i)->GetName());


      m_lbMapLayer.SetCheck(index, m_paMapObject->GetAt(i)->IsVisible());

      if (iSel == i) m_lbMapLayer.SetCurSel(iSel);
   }

   // Disable properties button if no layers

   GetDlgItem(IDC_PROPERTIES)->EnableWindow(m_lbMapLayer.GetCount() > 0);
   GetDlgItem(IDC_DELETE)->EnableWindow(m_lbMapLayer.GetCount() > 0);   
  
   // Redraw map

   if (!bInit)
   {
      m_pDocMap->UpdateAllViews(NULL);   
   };
}

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

void CDlgMapLayers::OnAdd() 
{   
   CDlgAddLayer dlg;  
   CString s;   
   
   if (dlg.DoModal() == IDOK)
   {        
      CDlgProgress dlgProgress(FALSE, this);
      
      CMapLayer* pMapLayer = new CMapLayer(dlg.IsMapLines());

      // Load data or create an empty layer if no features were selected

      if (pMapLayer->Initialise(dlg.GetFType(), dlg.GetFeatures(), 
                            dlg.GetAttr(), dlg.GetColour()) ||
                            dlg.GetFeatures().GetSize() == 0)
      {      
        // Update the list of layers
         
	      m_paMapObject->InsertAt(0, pMapLayer);
         InitLayers(0);
      
         // Set previous report

         BDSetPreviousReport(CNRDB::maplayer);
      } else
      {
         delete pMapLayer;
         
      }
   }           
}


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

void CDlgMapLayers::OnDelete() 
{
   int i = m_lbMapLayer.GetCurSel();
   if (i != LB_ERR)
   {
      CMapLayer* pMapLayer = (CMapLayer*)m_paMapObject->GetAt(i);
      ASSERT(pMapLayer != NULL);

      delete pMapLayer;
      m_paMapObject->RemoveAt(i);

   m_pDocMap->GetViewMap()->m_aSearchObj.RemoveAll();
   }	

   // Update the list of layers

   InitLayers(max(0,i-1));
}

///////////////////////////////////////////////////////////////////////////////
//
// Re-order the list of layers
//

void CDlgMapLayers::OnDeltaposUpdown(NMHDR* pNMHDR, LRESULT* pResult) 
{
	NM_UPDOWN* pNMUpDown = (NM_UPDOWN*)pNMHDR;
   
	int i = m_lbMapLayer.GetCurSel();
   int iSwap = -1;

   // Determine which items to swap

   if (pNMUpDown->iDelta == -1 && i > 0)
   {
      iSwap = i-1;
      
   }    
   else if (pNMUpDown->iDelta == 1 && i != LB_ERR && i+1 < m_lbMapLayer.GetCount())
   {
      iSwap = i+1;
   };

   // Swap the items
   if (iSwap != -1)
   {
      CMapLayer* pMapLayer = (CMapLayer*)m_paMapObject->GetAt(iSwap);
      m_paMapObject->SetAt(iSwap, m_paMapObject->GetAt(i));
      m_paMapObject->SetAt(i, pMapLayer);

      InitLayers(iSwap);
   };
	
	*pResult = 0;
}


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

void CDlgMapLayers::OnProperties() 
{
   int index = m_lbMapLayer.GetCurSel();
   if (index != LB_ERR)
   {
      CMapLayer* pMapLayer = (CMapLayer*)m_paMapObject->GetAt(index);      

      CSheetMapProp dlg(m_paMapObject, pMapLayer, BDString(IDS_MAPPROP));      
      if (dlg.DoModal())
      {
         InitLayers(index);             
      }
   };	
}

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

void CDlgMapLayers::OnDblclkMaplayer() 
{
   OnProperties();	
}
  
/////////////////////////////////////////////////////////////////////////////

void CDlgMapLayers::OnQuery() 
{
   CQuery* pQueryL = NULL;

   int index = m_lbMapLayer.GetCurSel();
   if (index != LB_ERR)
   {
      CMapLayer* pMapLayer = (CMapLayer*)m_paMapObject->GetAt(index);   
      pQueryL = pMapLayer->GetQuery();
   };

   CQuery* pQuery = NULL;
   if (pQueryL != NULL) pQuery = new CQuery(*pQueryL);
   else pQuery = new CQuery;

   CSheetQuery dlg(pQuery, CSheetQuery::Map, BDString(IDS_MAP)); 
   int nRet = dlg.DoModal();

   if (nRet == IDOK)
   {
      CDlgProgress dlgProgress(FALSE, this);

      BeginWaitCursor();
      
      CMapLayer* pMapLayer = new CMapLayer;
      if (pMapLayer->Initialise(pQuery))
      {
         m_paMapObject->InsertAt(0, pMapLayer);
         InitLayers(0);      

         BDSetPreviousReport(CNRDB::mapquery);
      } else
      {
         delete pMapLayer;         

         if (AfxMessageBox(BDString(IDS_QUERYERROR), MB_YESNO) == IDYES)
         {            
            OnQuery();                           
         };         
      }
      EndWaitCursor();
   } 
   else
   {
      delete pQuery;
   }   
}

/////////////////////////////////////////////////////////////////////////////
//
// Create an empty layer to contain a legend and open the map properties
//

void CDlgMapLayers::OnLegend() 
{
   // Create an empty layer

   CMapLayer* pMapLayer = new CMapLayer(FALSE);
   
   // Set the symbol to none

   pMapLayer->GetMapProp().m_nSymbol = CComboBoxSymbol::none;

   // Add to list   

   CSheetMapProp dlg(m_paMapObject, pMapLayer, BDString(IDS_MAPPROP));      
   if (dlg.DoModal() == IDOK)
   {
       m_paMapObject->InsertAt(0, pMapLayer);
      InitLayers(0);
   } else
   {
      delete pMapLayer;
   }
}

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

void CDlgMapLayers::OnClose() 
{  
   OnOK();
}

/////////////////////////////////////////////////////////////////////////////
//
// Store active layer
//

void CDlgMapLayers::OnOK() 
{
   // Set visibility state of layers
   for (int i = 0; i < m_lbMapLayer.GetCount(); i++)
   {
      m_paMapObject->GetAt(i)->SetVisible(m_lbMapLayer.IsCheck(i));
   }

   int index = m_lbMapLayer.GetCurSel();
   if (index != LB_ERR)
   {
      m_paMapObject->SetDefault(index);            

      // Update key

   }

   // Set preference for automatic updating

   m_pDocMap->GetViewMap()->SetAutoUpdate(m_ckAutoUpdate.GetCheck());
	
   // Update all views as visibility may have changed

   m_pDocMap->UpdateAllViews(NULL);   
    

	CDialog::OnOK();
}

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

void CDlgMapLayers::OnOverlays() 
{
    CMapLayer* pMapLayer = new CMapLayer;

	CDlgAddOverlay dlg(pMapLayer);
	if (dlg.DoModal() == IDOK)
	{    
       m_paMapObject->InsertAt(0, pMapLayer);

	   InitLayers(0);	         
	} else
	{
		delete pMapLayer;
	}
}

///////////////////////////////////////////////////////////////////////////////
//
// Ask the user if they wish to digitize points, polylines or polygons.
// Create a new feature type, then start digitizing.
//

void CDlgMapLayers::OnNew() 
{
   int nType = 0;

   // Determine which type to add

   CDlgDigitiseType	dlg;
   if (dlg.DoModal() == IDOK)
   {
      nType = dlg.GetType();

      // Create a new feature type

      int nDataType = 0;
      if (nType == SHPPoint) nDataType = BDCOORD;
      else if (nType == SHPPolygon || nType == SHPPolyLine) nDataType = BDMAPLINES;

      CDlgFTypeAttr dlgF(0,0,nDataType);
      if (dlgF.DoModal() == IDOK)
      {
         // Create a query for this feature type

         CFeatureType ftype;
         CAttrArray aAttr;

         ftype.m_lId = dlgF.GetId();
         BDFeatureType(BDHandle(), &ftype, BDSELECT);
         BDFTypeAttrInit(BDHandle(), ftype.m_lId, &aAttr);
         
         CArrayAttrSel aAttrSel;
         CQueryAttrSel attrsel;

         // Add the feature name

         attrsel.m_lAttr = BDFEATURE;
         attrsel.m_lFType = ftype.m_lId;
         attrsel.m_lDataType = BDFEATURE;
         aAttrSel.Add(attrsel);

         // Add the coordinate or polyline attribute
         for (int i = 0; i < aAttr.GetSize(); i++)
         {
            if (aAttr[i]->m_lDataType == nDataType)
            {
               attrsel.m_lAttr = aAttr[i]->m_lAttrId;
               attrsel.m_lDataType = nDataType;
               aAttrSel.Add(attrsel);
               break;
            }
         }         
         
         CArray <long, long> alFeatures; // No features yet

         CQuery* pQuery = new CQuery(dlgF.GetId(), aAttrSel, alFeatures);         

         // Add this ftype as a new map layer

         CMapLayer* pMapLayer = new CMapLayer;

         pMapLayer->Initialise(pQuery); // Will return error as no features yet
         m_paMapObject->InsertAt(0, pMapLayer);
         InitLayers(0);               

         // Set the mode to editing
  
         int nAddType = 0;
         if (nType == SHPPoint) nAddType = CViewMap::points;
         if (nType == SHPPolyLine) nAddType = CViewMap::polyline;
         if (nType == SHPPolygon) nAddType = CViewMap::polygon;

         m_pDocMap->GetViewMap()->SetMode(CViewMap::addnew | nAddType);

         OnOK();
      }     
   }
}

///////////////////////////////////////////////////////////////////////////////
//
// If the visibility has changed then redraw the map layers
//

void CDlgMapLayers::OnSelchangeMaplayer() 
{
   for (int i = 0; i < m_paMapObject->GetSize(); i++)
   {
      if (m_paMapObject->GetAt(i)->IsVisible() != m_lbMapLayer.IsCheck(i))
	  {
         m_paMapObject->GetAt(i)->SetVisible(m_lbMapLayer.IsCheck(i)); 
		 m_pDocMap->UpdateAllViews(NULL);   
		 break;
	  }
   };
}

///////////////////////////////////////////////////////////////////////////////
//
// Update when checked so takes effect before dialog is closed
//

void CDlgMapLayers::OnAutoupdate() 
{
   m_pDocMap->GetViewMap()->SetAutoUpdate(m_ckAutoUpdate.GetCheck());	
}

⌨️ 快捷键说明

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