📄 wycview.cpp
字号:
m_map.SetExtent(r); // zoom to the state
m_map.Refresh(); // force redraw
m_map.FlashShape(shape, 3);
//
double Perimeter;
Perimeter=pPoly->GetPerimeter();
double Area;
Area=pPoly->GetArea();
CString strPerimeter;
CString strArea;
strPerimeter.Format("%f",Perimeter);
strArea.Format("%f",Area);
MessageBox("面积:"+strArea+"周长:"+strPerimeter);
}
break;
case ID_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_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_ADDRECTANGLE:
{
// 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);
}
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_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_QUERY_POINT:
{
//***1获得查询范围
CMoPoint mapPt(m_map.ToMapPoint((float)X, (float)Y));
CMoRectangle select_Rect;
if(!select_Rect.CreateDispatch("MapObjects2.Rectangle"))
return;
double x = mapPt.GetX();
double y = mapPt.GetY();
CMoRectangle rect_Extent(m_map.GetExtent());
double X_Scale;
double Y_Scale;
X_Scale = rect_Extent.GetHeight()/m_map.GetFullExtent().GetHeight();
Y_Scale = rect_Extent.GetWidth()/m_map.GetFullExtent().GetWidth();
double m_Scale = X_Scale;
if(X_Scale>Y_Scale)
m_Scale = Y_Scale;
//设置扑捉范围
select_Rect.SetBottom(y-2*m_Scale);
select_Rect.SetTop(y+2*m_Scale);
select_Rect.SetLeft(x-2*m_Scale);
select_Rect.SetRight(x+2*m_Scale);
//***2由查询范围选取目标
CMoLayers layers(m_map.GetLayers());
CMoMapLayer SelectedMapLayer(layers.Item(COleVariant(TEXT(m_layer))));
CMoRecordset m_SelectedRecSet;
m_SelectedRecSet = SelectedMapLayer.SearchShape(select_Rect,6,"");
//***3得到选取目标的属性
CMoTableDesc tableDesc(m_SelectedRecSet.GetTableDesc());
CMoFields ShapeFields(m_SelectedRecSet.GetFields());
CMoField ShapeField(ShapeFields.Item(COleVariant(TEXT("Shape"))));
CString resultStr;
for(int i=0;i<tableDesc.GetFieldCount();i++)
{
CString name = tableDesc.GetFieldName(i);
ShapeField = ShapeFields.Item(COleVariant(name));
CString value = ShapeField.GetValueAsString();
resultStr = resultStr+name+":"+value+"\n";
}
MessageBox(resultStr);
}
break;
}//end switch
}
void CWycView::OnMapTool(UINT nID)
{
m_curTool = nID;
}
void CWycView::OnUpdateMapTool(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
pCmdUI->SetCheck(pCmdUI->m_nID == m_curTool);
}
void CWycView::OnMapFullExtent()
{
CMoRectangle r(m_map.GetFullExtent());
m_map.SetExtent(r);
}
void CWycView::OnGetlengthLine()
{
m_curTool = ID_GETLENGTH_LINE;
}
void CWycView::OnGetdistanceP1top2()
{
m_curTool = ID_GETDISTANCE_P1TOP2;
if(! m_startPoint.CreateDispatch("MapObjects2.Point"))
return;
if(! m_endPoint.CreateDispatch("MapObjects2.Point"))
return;
m_mouseDownNum = 0;
}
void CWycView::OnDblClickMap1()
{
if(m_curTool == ID_GETDISTANCE_P1TOP2)
{
double distance;
CString Pointdistance,PD;
distance = m_startPoint.DistanceTo(m_endPoint);
PD.Format("%f",distance);
Pointdistance = "所查两点间的距离为:"+PD;
MessageBox(Pointdistance);
m_mouseDownNum = 0;
m_startPoint.ReleaseDispatch();
m_endPoint.ReleaseDispatch();
}
}
void CWycView::OnBeforeLayerDrawMap1(short index, long hDC)
{
//
// Calculate whether or not counties should be shown
//
CMoRectangle extent(m_map.GetExtent());
CMoRectangle fullExtent(m_map.GetFullExtent());
BOOL showDetail = (extent.GetWidth() < (fullExtent.GetWidth() / 4.0));
//
// Set layer visiblility
//
CMoLayers layers(m_map.GetLayers());
CMoMapLayer layer(layers.Item(COleVariant(index)));
switch (index)
{
// counties
case 1: layer.SetVisible(showDetail); break;
// states
case 2: layer.SetVisible(!showDetail); break;
}
}
/*void CWycView::OnAfterLayerDrawMap1(short index, BOOL canceled, long hDC)
{
if (index != 1)
return; // Only draw after counties
if (LPDISPATCH(m_selection) == 0)
return; // No selection to draw
//
// Draw the selected counties in a highlight color
//
CMoSymbol sym;
sym.CreateDispatch(TEXT("MapObjects2.Symbol"));
sym.SetColor(moMagenta);
m_selection.MoveFirst();
CMoFields fields(m_selection.GetFields());
CMoField shapeField(fields.Item(COleVariant(TEXT("Shape"))));
CMoPolygon shape;
while (!m_selection.GetEof())
{
shape.AttachDispatch(shapeField.GetValue().pdispVal);
m_map.DrawShape(shape, sym);
m_selection.MoveNext();
}
}*/
void CWycView::OnSearchStates()
{
m_curTool = ID_SEARCH_STATES;
}
void CWycView::OnUpdateSearchStates(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
pCmdUI->SetCheck(pCmdUI->m_nID == m_curTool);
}
void CWycView::OnAfterTrackingLayerDrawMap1(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);
// TODO: Add your control notification handler code here
if (LPDISPATCH(m_selection) == 0)
return; // No selection to draw
//
// Draw the selected counties in a highlight color
//
sym.SetColor(moMagenta);
sym.SetSymbolType(moFillSymbol);
sym.SetStyle(moLightGrayFill);//experiment with different fill styles
m_selection.MoveFirst();
CMoFields fields(m_selection.GetFields());
CMoField shapeField(fields.Item(COleVariant(TEXT("Shape"))));
CMoPolygon shape;
while (!m_selection.GetEof())
{
shape.AttachDispatch(shapeField.GetValue().pdispVal);
m_map.DrawShape(shape, sym);
m_selection.MoveNext();
}
}
void CWycView::OnAddpoint()
{
// TODO: Add your command handler code here
m_curTool = ID_ADDPOINT;
m_map.SetMousePointer(moArrow);
}
void CWycView::OnAddrectangle()
{
// TODO: Add your command handler code here
m_curTool = ID_ADDRECTANGLE;
m_map.SetMousePointer(moArrow);
}
void CWycView::OnAddline()
{
// TODO: Add your command handler code here
m_curTool = ID_ADDLINE;
m_map.SetMousePointer(moArrow);
}
void CWycView::ReleasePoints()
{
for (int i = 0; i < m_points.GetSize(); i++)
{
m_points[i]->ReleaseDispatch();
delete m_points[i];
}
m_points.RemoveAll();
}
void CWycView::OnFileSave()
{
// TODO: Add your command handler code here
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_ADDPOINT: SavePoints(conn, tableDesc); break;
case ID_ADDLINE: SaveLines(conn, tableDesc); break;
case ID_MAP_ADDPOLY: SavePolygons(conn, tableDesc); break;
case ID_ADDELLIPSE: SaveEllipses(conn, tableDesc); break;
case ID_ADDRECTANGLE: SaveRectangles(conn,tableDesc); break;
}
//Invalidate();
}
}
void CWycView::SavePoints(CMoDataConnection &conn, CMoTableDesc &tableDesc)
{
// Create a GeoDataset and associate it with a new layer
// The VARIANT is needed now for the HasZ and HasM parameters for the AddGeoDataset
// function which has changed since MapObjects 1.2
VARIANT va;
VariantInit(&va);
va.vt = VT_BOOL;
va.boolVal = false;
CMoGeoDataset geoDataset(conn.AddGeoDataset(GetFileTitle(m_path), moPoint, tableDesc, va, va));
CMoMapLayer layer;
if (!layer.CreateDispatch(TEXT("MapObjects2.MapLayer")))
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -