📄 view.cpp
字号:
// will be writing out the ellipse as a polygon as shape files don't know about Ellipses
// Use Buffer(0,Extent) to create a polygon.
//
// need to set up a Variant for the Extent parameter
CMoRectangle extent(m_map.GetFullExtent());
VariantInit(&va);
va.vt = VT_DISPATCH;
va.pdispVal = extent.m_lpDispatch;
for (int i = 0; i < m_ellipses.GetSize(); i++)
{
recs.AddNew();
CMoEllipse ellipse(*m_ellipses[i]);
CMoPolygon poly(ellipse.Buffer(0.0,va));
// Shape = poly
SetValue(fields,TEXT("Shape"),LPDISPATCH(poly.m_lpDispatch));
// "Name" = featureName
featureName.Format("%s%d", "ellipse", i);
SetValue(fields, TEXT("Name"), featureName);
// "Area" = poly.GetArea()
SetValue(fields, TEXT("Area"), poly.GetArea());
// "Perimeter" = poly.GetPerimeter()
SetValue(fields, TEXT("Perimeter"), poly.GetPerimeter());
recs.Update();
}
// Add new layer to map
CMoLayers layers(m_map.GetLayers());
layers.Add(layer);
ReleaseEllipses();
}
void CAddShapeView::OnMapSaveShapes()
{
CString filter(TEXT("ESRI Shapefiles (*.shp)|*.shp|"));
CFileDialog dlg(FALSE, TEXT(".shp"), 0, OFN_ALLOWMULTISELECT | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY, filter);
if (dlg.DoModal() == IDOK)
{
m_path = dlg.GetPathName();
// Open a new connection
CMoDataConnection conn;
if (!conn.CreateDispatch(TEXT("MapObjects2.DataConnection")))
return;
conn.SetDatabase(GetFileDirectory(m_path));
if (!conn.Connect())
return;
// Define the geodataset
CMoTableDesc tableDesc;
if (!tableDesc.CreateDispatch(TEXT("MapObjects2.TableDesc")))
return;
// set the field names, types, and lengths
tableDesc.SetFieldCount(3);
tableDesc.SetFieldName(0, TEXT("Name"));
tableDesc.SetFieldType(0, moString);
tableDesc.SetFieldLength(0, 16);
tableDesc.SetFieldName(1, TEXT("Area"));
tableDesc.SetFieldType(1, moDouble);
tableDesc.SetFieldPrecision(1, 15);
tableDesc.SetFieldScale(1, 3); // decimal places
tableDesc.SetFieldName(2, TEXT("Perimeter"));
tableDesc.SetFieldType(2, moDouble);
tableDesc.SetFieldPrecision(2, 15);
tableDesc.SetFieldScale(2, 3); // decimal places
switch (m_curTool)
{
case ID_MAP_ADDPOINT: SavePoints(conn, tableDesc); break;
case ID_MAP_ADDLINE: SaveLines(conn, tableDesc); break;
case ID_MAP_ADDPOLY: SavePolygons(conn, tableDesc); break;
case ID_ADDELLIPSE: SaveEllipses(conn, tableDesc); break;
case ID_ADDRECT: SaveRectangles(conn,tableDesc); break;
}
//Invalidate();
}
}
BEGIN_EVENTSINK_MAP(CAddShapeView, CFormView)
//{{AFX_EVENTSINK_MAP(CAddShapeView)
ON_EVENT(CAddShapeView, IDC_MAP1, -605 /* MouseDown */, OnMapMouseDown, VTS_I2 VTS_I2 VTS_I4 VTS_I4)
ON_EVENT(CAddShapeView, IDC_MAP1, 4 /* AfterTrackingLayerDraw */, OnMapAfterTrackingLayerDraw, VTS_I4)
//}}AFX_EVENTSINK_MAP
END_EVENTSINK_MAP()
void CAddShapeView::OnMapMouseDown(short Button, short Shift, long x, long y)
{
// Set a variant to use when refreshing the tracking layer
VARIANT va;
VariantInit(&va);
va.vt = VT_I4;
// Get the tracking layer
CMoTrackingLayer tLayer(m_map.GetTrackingLayer());
switch (m_curTool)
{
case ID_MAP_ADDPOINT:
{
// Capture a new point
CMoPoint* point = new CMoPoint(m_map.ToMapPoint((float)x, (float)y));
if (LPDISPATCH(point))
m_points.Add(point);
// create a temp rectangle with which to refresh the screen
// around the point
CMoRectangle r = CMoRectangle();
r.CreateDispatch(TEXT("MapObjects2.Rectangle"));
r.SetBottom(point->GetY());
r.SetLeft(point->GetX());
r.SetTop(point->GetY()+1.0);
r.SetRight(point->GetX()+1.0);
r.ScaleRectangle(50.0);
va.vt = VT_DISPATCH;
va.pdispVal = r.m_lpDispatch;
tLayer.Refresh(true, va);
}
break;
case ID_MAP_ADDLINE:
{
// Track a new line
CMoLine* line = new CMoLine(m_map.TrackLine());
if (LPDISPATCH(line))
m_lines.Add(line);
CMoRectangle r = CMoRectangle(line->GetExtent());
va.vt = VT_DISPATCH;
va.pdispVal = r.m_lpDispatch;
tLayer.Refresh(true, va);
}
break;
case ID_ADDELLIPSE:
{
// Create a new ellipse object
CMoEllipse* ellipse = new CMoEllipse();
ellipse->CreateDispatch(TEXT("MapObjects2.Ellipse"));
// Track a rectangle and to set the ellipse width/height
CMoRectangle rect(m_map.TrackRectangle());
ellipse->SetBottom(rect.GetBottom());
ellipse->SetLeft(rect.GetLeft());
ellipse->SetRight(rect.GetRight());
ellipse->SetTop(rect.GetTop());
if (LPDISPATCH(ellipse))
m_ellipses.Add(ellipse);
va.vt = VT_DISPATCH;
va.pdispVal = rect.m_lpDispatch;
tLayer.Refresh(true, va);
}
break;
case ID_ADDRECT:
{
// Track a new line
CMoRectangle* rect = new CMoRectangle(m_map.TrackRectangle());
if (LPDISPATCH(rect))
m_rectangles.Add(rect);
va.vt = VT_DISPATCH;
va.pdispVal = rect->m_lpDispatch;
tLayer.Refresh(true, va);
}
break;
case ID_MAP_ADDPOLY:
{
// Track a new polygon
CMoPolygon* poly = new CMoPolygon(m_map.TrackPolygon());
if (LPDISPATCH(poly))
m_polys.Add(poly);
CMoRectangle rect = CMoRectangle(poly->GetExtent());
va.vt = VT_DISPATCH;
va.pdispVal = rect.m_lpDispatch;
tLayer.Refresh(true, va);
}
break;
case ID_MAP_PAN:
{
m_map.Pan();
}
break;
case ID_MAP_ZOOMIN:
{
// track a rectangle and zoom into it
CMoRectangle pRect(m_map.TrackRectangle());
m_map.SetExtent(pRect);
}
break;
case ID_MAP_ZOOMOUT:
{
// zoom out around the current point
CMoPoint pt(m_map.ToMapPoint((float)x,(float)y));
m_map.CenterAt(pt.GetX(),pt.GetY());
CMoRectangle r(m_map.GetExtent());
r.ScaleRectangle(1.5);
m_map.SetExtent(r);
}
break;
}
}
void CAddShapeView::OnMapAfterTrackingLayerDraw(long hDC)
{
CMoSymbol sym;
if (!sym.CreateDispatch("MapObjects2.Symbol"))
return;
sym.SetColor(moPurple);
//
// Draw lines
//
sym.SetSymbolType(moLineSymbol);
sym.SetStyle(moSolidLine);
sym.SetSize(2);
for (int i = 0; i < m_lines.GetSize(); i++)
m_map.DrawShape(*m_lines[i], sym);
//
// Draw polygons
//
sym.SetSymbolType(moFillSymbol);
sym.SetStyle(moGrayFill);
for (i = 0; i < m_polys.GetSize(); i++)
m_map.DrawShape(*m_polys[i], sym);
sym.SetSymbolType(moFillSymbol);
sym.SetStyle(moGrayFill);
for (i = 0; i < m_rectangles.GetSize(); i++)
m_map.DrawShape(*m_rectangles[i], sym);
sym.SetSymbolType(moFillSymbol);
sym.SetStyle(moGrayFill);
for (i = 0; i < m_ellipses.GetSize(); i++)
m_map.DrawShape(*m_ellipses[i], sym);
//
// Draw points
//
sym.SetColor(moRed);
sym.SetSymbolType(moPointSymbol);
sym.SetStyle(moCircleMarker);
for (i = 0; i < m_points.GetSize(); i++)
m_map.DrawShape(*m_points[i], sym);
}
void CAddShapeView::OnMapAddPoint()
{
m_curTool = ID_MAP_ADDPOINT;
m_map.SetMousePointer(moArrow);
}
void CAddShapeView::OnMapAddLine()
{
m_curTool = ID_MAP_ADDLINE;
m_map.SetMousePointer(moArrow);
}
void CAddShapeView::OnMapAddPoly()
{
m_curTool = ID_MAP_ADDPOLY;
m_map.SetMousePointer(moArrow);
}
void CAddShapeView::OnAddellipse()
{
m_curTool = ID_ADDELLIPSE;
m_map.SetMousePointer(moArrow);
}
void CAddShapeView::OnAddrect()
{
m_curTool = ID_ADDRECT;
m_map.SetMousePointer(moArrow);
}
void CAddShapeView::OnFullextent()
{
CMoRectangle pRect = m_map.GetFullExtent();
m_map.SetExtent(pRect);
m_map.Invalidate(false);
}
void CAddShapeView::OnUpdateMapTool(CCmdUI* pCmdUI)
{
pCmdUI->SetCheck(pCmdUI->m_nID == m_curTool);
}
void CAddShapeView::OnZoomIn()
{
m_curTool = ID_MAP_ZOOMIN;
m_map.SetMousePointer(moZoomIn);
}
void CAddShapeView::OnZoomout()
{
m_curTool = ID_MAP_ZOOMOUT;
m_map.SetMousePointer(moZoomOut);
}
void CAddShapeView::OnPan()
{
m_curTool = ID_MAP_PAN;
m_map.SetMousePointer(moPan);
}
void CAddShapeView::OnFileOpen()
{
CString filter(TEXT("ESRI Shapefiles (*.shp)|*.shp|"));
CFileDialog dlg(TRUE, TEXT(".shp"), 0, OFN_ALLOWMULTISELECT | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY, filter);
if (dlg.DoModal() == IDOK)
{
m_path = dlg.GetPathName();
// call the map helper function AddLayer
// should add functionality to allow user to select their own colour
// an renderer
//CDataDir dataDir;
AddLayer(m_map, m_path, moWhite, NULL);
// if there are already map layers then do not reset the map extent
CMoLayers layers = m_map.GetLayers();
if (layers.GetCount() < 2) // the raster image plus this new layer
{
CMoRectangle extent(m_map.GetFullExtent());
extent.ScaleRectangle(.1);
m_map.SetExtent(extent);
}
m_map.Refresh();
}
}
void CAddShapeView::OnFileNew()
{
//Simply clear the current collection of shapes
this->ReleaseLines();
this->ReleasePoints();
this->ReleasePolygons();
this->ReleaseRectangles();
this->ReleaseEllipses();
// remove all layers
CMoLayers layers(m_map.GetLayers());
layers.Clear();
// and add the raster image again
CDataDir dataDir;
AddImageLayer(m_map, dataDir.GetPath() + "\\washington\\wash.bmp");
CMoRectangle extent(m_map.GetFullExtent());
extent.ScaleRectangle(.1);
m_map.SetExtent(extent);
m_curTool = ID_MAP_ADDPOLY;
m_path = "untitled.shp";
m_map.Invalidate(FALSE);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -