📄 wycview.cpp
字号:
// wycView.cpp : implementation of the CWycView class
//
#include "stdafx.h"
#include "wyc.h"
#include "morectangle.h"
#include "MoTableDesc.h"
#include "MapHelper.h"
#include "MapObjects2.h"
#include "map.h"
#include "MoPoint.h"
#include "wycDoc.h"
#include "wycView.h"
#include "LAYER.h"
#include "DELETELAYER.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CWycView
IMPLEMENT_DYNCREATE(CWycView, CFormView)
BEGIN_MESSAGE_MAP(CWycView, CFormView)
//{{AFX_MSG_MAP(CWycView)
ON_WM_SIZE()
ON_COMMAND_RANGE(ID_MAP_ZOOMIN, ID_GETPOINTCOOR, OnMapTool)
ON_UPDATE_COMMAND_UI_RANGE(ID_MAP_ZOOMIN, ID_GETPOINTCOOR, OnUpdateMapTool)
ON_COMMAND(ID_FULLEXTEND, OnMapFullExtent)
ON_COMMAND(ID_GETLENGTH_LINE, OnGetlengthLine)
ON_COMMAND(ID_GETDISTANCE_P1TOP2, OnGetdistanceP1top2)
ON_UPDATE_COMMAND_UI(ID_SEARCH_STATES, OnUpdateSearchStates)
ON_COMMAND(ID_ADDPOINT, OnAddpoint)
ON_COMMAND(ID_ADDRECTANGLE, OnAddrectangle)
ON_COMMAND(ID_ADDLINE, OnAddline)
ON_COMMAND(ID_FILE_SAVE, OnFileSave)
ON_COMMAND(ID_ADDELLIPSE, OnAddellipse)
ON_COMMAND(ID_MAP_ADDPOLY, OnMapAddpoly)
ON_COMMAND(ID_FILE_OPEN, OnFileOpen)
ON_COMMAND(ID_FILE_NEW, OnFileNew)
ON_COMMAND(ID_SELECT, OnSelect)
ON_COMMAND(ID_SEARCH_STATES, OnSearchStates)
ON_COMMAND(ID_DELETELAYER, OnDeletelayer)
ON_COMMAND(ID_RESETLAYER, OnResetlayer)
ON_COMMAND(ID_QUERY_POINT, OnQueryPoint)
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CFormView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CFormView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CFormView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CWycView construction/destruction
CWycView::CWycView()
: CFormView(CWycView::IDD)
{
//{{AFX_DATA_INIT(CWycView)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// TODO: add construction code here
}
CWycView::~CWycView()
{
}
void CWycView::DoDataExchange(CDataExchange* pDX)
{
CFormView::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CWycView)
DDX_Control(pDX, IDC_MAP1, m_map);
//}}AFX_DATA_MAP
}
BOOL CWycView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CFormView::PreCreateWindow(cs);
}
void CWycView::OnInitialUpdate()
{
CFormView::OnInitialUpdate();
GetParentFrame()->RecalcLayout();
ResizeParentToFit();
///设置地图背景色------
COLORREF m_BackColor = RGB(150,180,0);
m_map.SetBackColor(m_BackColor);
VERIFY(SetTimer(1, 1000, 0)); // 1 second timer
//
// Add data to the map.
//
AddLayer(m_map, TEXT("..\\data\\usa\\states.shp"), moLimeGreen);
AddLayer(m_map, TEXT("..\\data\\usa\\counties.shp"), moPaleYellow);
AddLayer(m_map, TEXT("..\\data\\usa\\ushigh.shp"), moRed);
//
// Set up county renderer
//
CMoLayers layers(m_map.GetLayers());
CMoMapLayer counties(layers.Item(COleVariant(TEXT("Counties"))));
CMoClassBreaksRenderer cbRenderer; // Create from scratch
cbRenderer.CreateDispatch(TEXT("MapObjects2.ClassBreaksRenderer"));
cbRenderer.SetField(TEXT("MOBILEHOME"));
// calculate breaks away from the mean in both directions,
// but only add those breaks that are within the range of values
CMoRecordset recs(counties.GetRecords());
CMoStatistics stats(recs.CalculateStatistics(TEXT("MOBILEHOME")));
double breakVal = stats.GetMean() - (stats.GetStdDev() * 3.0);
double min = stats.GetMin();
double max = stats.GetMax();
for (int i = 0; i < 7; i++)
{
if (min <= breakVal && breakVal <= max)
{
int breakCount = cbRenderer.GetBreakCount();
cbRenderer.SetBreakCount(breakCount + 1);
cbRenderer.SetBreak(breakCount, breakVal);
}
breakVal = breakVal + stats.GetStdDev();
}
cbRenderer.RampColors(moPaleYellow, moOrange);
counties.SetRenderer(cbRenderer);
//
// Set up states renderer
//
CMoMapLayer states(layers.Item(COleVariant(TEXT("States"))));
CMoDotDensityRenderer ddRenderer; // Create from scratch
ddRenderer.CreateDispatch(TEXT("MapObjects2.DotDensityRenderer"));
ddRenderer.SetField(TEXT("NO_FARMS87"));
recs = states.GetRecords();
stats = recs.CalculateStatistics(TEXT("NO_FARMS87"));
ddRenderer.SetDotValue(stats.GetMax() / 100.0);
states.SetRenderer(ddRenderer);
//
//
// Set the tracking layer symbol
//
CMoTrackingLayer tLayer(m_map.GetTrackingLayer());
tLayer.SetSymbolCount(1);
CMoFont fnt;
fnt.InitializeFont(); // similar to CreateDispatch
// now define the font face
fnt.SetName(TEXT("Wingdings"));
CMoSymbol sym(tLayer.GetSymbol(0));
sym.SetColor(moBlack);
sym.SetStyle(moTrueTypeMarker);
sym.SetFont(fnt.GetFontDispatch());
sym.SetSize(18);
sym.SetCharacterIndex(81);
fnt.ReleaseFont();
}
/////////////////////////////////////////////////////////////////////////////
// CWycView printing
BOOL CWycView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CWycView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CWycView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
void CWycView::OnPrint(CDC* pDC, CPrintInfo* /*pInfo*/)
{
// TODO: add customized printing code here
}
/////////////////////////////////////////////////////////////////////////////
// CWycView diagnostics
#ifdef _DEBUG
void CWycView::AssertValid() const
{
CFormView::AssertValid();
}
void CWycView::Dump(CDumpContext& dc) const
{
CFormView::Dump(dc);
}
CWycDoc* CWycView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CWycDoc)));
return (CWycDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CWycView message handlers
void CWycView::OnSize(UINT nType, int cx, int cy)
{
CFormView::OnSize(nType, cx, cy);
if (m_map.m_hWnd)
m_map.SetWindowPos(0, 0, 0, cx, cy, SWP_NOZORDER);
}
BEGIN_EVENTSINK_MAP(CWycView, CFormView)
//{{AFX_EVENTSINK_MAP(CWycView)
ON_EVENT(CWycView, IDC_MAP1, -605 /* MouseDown */, OnMouseDownMap1, VTS_I2 VTS_I2 VTS_I4 VTS_I4)
ON_EVENT(CWycView, IDC_MAP1, -601 /* DblClick */, OnDblClickMap1, VTS_NONE)
ON_EVENT(CWycView, IDC_MAP1, 2 /* BeforeLayerDraw */, OnBeforeLayerDrawMap1, VTS_I2 VTS_I4)
ON_EVENT(CWycView, IDC_MAP1, 4 /* AfterTrackingLayerDraw */, OnAfterTrackingLayerDrawMap1, VTS_I4)
//}}AFX_EVENTSINK_MAP
END_EVENTSINK_MAP()
void CWycView::OnMouseDownMap1(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_ZOOMIN:
{
m_map.SetMousePointer(51);
CMoRectangle r(m_map.TrackRectangle());
if (LPDISPATCH(r))
m_map.SetExtent(r);
}break;
case ID_MAP_ZOOMOUT:
{
m_map.SetMousePointer(52);
CMoRectangle r(m_map.GetExtent());
r.ScaleRectangle(1.5);
m_map.SetExtent(r);
}break;
case ID_MAP_PAN:
{
m_map.SetMousePointer(53);
m_map.Pan();
}break;
case ID_SELECT:
m_map.SetMousePointer(57);
break;
case ID_MAP_QUERY:
{
//
// search for a highway within a N pixel tolerance
//
const double tolerance = m_map.ToMapDistance((float)10);
CMoLayers layers(m_map.GetLayers());
CMoMapLayer highways(layers.Item(COleVariant(TEXT("USHigh"))));
CMoPoint mapPt(m_map.ToMapPoint((float)X, (float)Y));
CMoRecordset selectedHighways(highways.SearchByDistance(mapPt, tolerance, TEXT("")));
if (selectedHighways.GetEof())
::MessageBeep(MB_ICONQUESTION); // no highways found
else
{
// Find counties that selected highways intersect
CMoMapLayer counties(layers.Item(COleVariant(TEXT("Counties"))));
m_selection = counties.SearchShape(selectedHighways,
moEdgeTouchOrAreaIntersect, TEXT(""));
// Redraw map
//m_map.Refresh();
VARIANT va;
VariantInit(&va);
va.vt = VT_NULL;
CMoTrackingLayer m_trackLayer(m_map.GetTrackingLayer());
m_trackLayer.Refresh(true,va);
}
}
break;
case ID_GETPOINTCOOR:
{
CMoPoint m_CurrentPoint;
//if(!m_CurrentPoint.CreateDispatch("MapObjects2.Point"))
// return;
m_CurrentPoint = m_map.ToMapPoint(float(X),float(Y));
CString m_CoordinateStr,m_XCoordinateStr,m_YCoordinateStr;
m_XCoordinateStr.Format("%f",m_CurrentPoint.GetX());
m_YCoordinateStr.Format("%f",m_CurrentPoint.GetY());
m_CoordinateStr = "X:"+m_XCoordinateStr+",Y:"+m_YCoordinateStr;
MessageBox(m_CoordinateStr);
m_CurrentPoint.ReleaseDispatch();
}break;
case ID_GETDISTANCE_P1TOP2:
{
if(m_mouseDownNum==0)
m_startPoint = m_map.ToMapPoint(float(X),float(Y));
else
m_endPoint = m_map.ToMapPoint(float(X),float(Y));
m_mouseDownNum ++;
}break;
case ID_GETLENGTH_LINE:
{
CMoPoint mapPt(m_map.ToMapPoint((float)X, (float)Y));
//***1实现线目标的扑捉
//求解当前放大系数
CMoRectangle rect_Extent(m_map.GetExtent());
double X_Scale;
double Y_Scale;
double x = mapPt.GetX();
double y = mapPt.GetY();
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;
//设置扑捉范围
CMoRectangle select_Rect;
if(!select_Rect.CreateDispatch("MapObjects2.Rectangle"))
return;
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);
//扑捉并得到目标
CMoLayers layers(m_map.GetLayers());
CMoMapLayer SelectedMapLayer(layers.Item(COleVariant(TEXT("USHigh"))));
CMoRecordset m_SelectedRecSet;
m_SelectedRecSet = SelectedMapLayer.SearchShape(select_Rect,6,"");
m_SelectedRecSet.MoveFirst();
CMoFields ShapeFields(m_SelectedRecSet.GetFields());
CMoField ShapeField(ShapeFields.Item(COleVariant(TEXT("Shape"))));
CMoLine * pLine = new CMoLine();
pLine->AttachDispatch(ShapeField.GetValue().pdispVal);
//得到并显示目标
CMoPolygon shape(ShapeField.GetValue().pdispVal);
CMoRectangle r(shape.GetExtent());
r.ScaleRectangle(2);
m_map.SetExtent(r); // zoom to the state
m_map.Refresh(); // force redraw
m_map.FlashShape(shape, 3);
//**得到线目标的长度
double Length;
Length = pLine->GetLength();
CString StrLength;
StrLength.Format("%f",Length);
MessageBox("所查询线段目标的长度为:"+StrLength);
mapPt.ReleaseDispatch();
select_Rect.ReleaseDispatch();
delete pLine;
/* 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;
case ID_SEARCH_STATES:
{
CMoPoint mapPt(m_map.ToMapPoint((float)X, (float)Y));
//求解当前放大比例系数
CMoRectangle rect_Extent(m_map.GetExtent());
double x = mapPt.GetX();
double y = mapPt.GetY();
double X_Scale;
double Y_Scale;
X_Scale=rect_Extent.GetHeight()/m_map.GetFullExtent().GetHeight();
Y_Scale=rect_Extent.GetHeight()/m_map.GetFullExtent().GetHeight();
double m_Scale=X_Scale;
if(X_Scale>Y_Scale)
m_Scale=Y_Scale;
//得到捕捉范围
CMoRectangle select_Rect;
if(!select_Rect.CreateDispatch("MapObjects2.Rectangle"))
return;
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);
//
CMoLayers layers(m_map.GetLayers());
CMoMapLayer selectedMaplayer(layers.Item(COleVariant(TEXT("Counties"))));
CMoRecordset m_SelectedRecSet;
m_SelectedRecSet=selectedMaplayer.SearchShape(select_Rect,6,"");
m_SelectedRecSet.MoveFirst();
CMoFields shapeFields(m_SelectedRecSet.GetFields());
CMoField shapeField(shapeFields.Item(COleVariant(TEXT("Shape"))));
CMoPolygon *pPoly=new CMoPolygon();
pPoly->AttachDispatch(shapeField.GetValue().pdispVal);
//闪烁显示目标
CMoPolygon shape(shapeField.GetValue().pdispVal);
CMoRectangle r(shape.GetExtent());
r.ScaleRectangle(2);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -