MoLectureView.cpp
资源名称:MoLecture.rar [点击查看]
上传用户:qinfarui
上传日期:2022-08-10
资源大小:362k
文件大小:23k
源码类别:
GIS编程
开发平台:
Visual C++
- // MoLectureView.cpp : implementation of the CMoLectureView class
- //
- #include "stdafx.h"
- #include "MoLecture.h"
- #include "MoLectureDoc.h"
- #include "MoLectureView.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CMoLectureView
- IMPLEMENT_DYNCREATE(CMoLectureView, CFormView)
- BEGIN_MESSAGE_MAP(CMoLectureView, CFormView)
- //{{AFX_MSG_MAP(CMoLectureView)
- ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
- ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
- ON_BN_CLICKED(IDC_BUTTON4, OnButton4)
- ON_BN_CLICKED(IDC_BUTTON5, OnButton5)
- ON_BN_CLICKED(IDC_BUTTON6, OnButton6)
- ON_BN_CLICKED(IDC_BUTTON7, OnButton7)
- ON_BN_CLICKED(IDC_BUTTON8, OnButton8)
- ON_BN_CLICKED(IDC_BUTTON9, OnButton9)
- ON_BN_CLICKED(IDC_BUTTON10, OnButton10)
- ON_BN_CLICKED(IDC_BUTTON11, OnButton11)
- ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
- ON_BN_CLICKED(IDC_BUTTON12, OnButton12)
- ON_BN_CLICKED(IDC_BUTTON13, OnButton13)
- ON_BN_CLICKED(IDC_BUTTON14, OnButton14)
- ON_BN_CLICKED(IDC_BUTTON15, OnButton15)
- ON_BN_CLICKED(IDC_BUTTON16, OnButton16)
- ON_BN_CLICKED(IDC_BUTTON17, OnButton17)
- ON_BN_CLICKED(IDC_BUTTON18, OnButton18)
- ON_BN_CLICKED(IDC_BUTTON19, OnButton19)
- ON_BN_CLICKED(IDC_BUTTON20, OnButton20)
- ON_BN_CLICKED(IDC_BUTTON21, OnButton21)
- ON_BN_CLICKED(IDC_BUTTON22, OnButton22)
- ON_BN_CLICKED(IDC_BUTTON23, OnButton23)
- ON_BN_CLICKED(IDC_BUTTON24, OnButton24)
- ON_BN_CLICKED(IDC_BUTTON25, OnButton25)
- //}}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()
- /////////////////////////////////////////////////////////////////////////////
- // CMoLectureView construction/destruction
- CMoLectureView::CMoLectureView()
- : CFormView(CMoLectureView::IDD)
- {
- //{{AFX_DATA_INIT(CMoLectureView)
- m_X = 0.0;
- m_Y = 0.0;
- m_strQuery = _T("");
- //}}AFX_DATA_INIT
- // TODO: add construction code here
- m_ToolNum = -1;
- }
- CMoLectureView::~CMoLectureView()
- {
- }
- void CMoLectureView::DoDataExchange(CDataExchange* pDX)
- {
- CFormView::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CMoLectureView)
- DDX_Control(pDX, IDC_LIST1, m_ListAttribute);
- DDX_Control(pDX, IDC_MAP1, m_Map);
- DDX_Text(pDX, IDC_EDIT1, m_X);
- DDX_Text(pDX, IDC_EDIT2, m_Y);
- DDX_Text(pDX, IDC_EDIT4, m_strQuery);
- //}}AFX_DATA_MAP
- }
- BOOL CMoLectureView::PreCreateWindow(CREATESTRUCT& cs)
- {
- // TODO: Modify the Window class or styles here by modifying
- // the CREATESTRUCT cs
- return CFormView::PreCreateWindow(cs);
- }
- //初始化鹰眼无模态对话框
- void CMoLectureView::OnInitialUpdate()
- {
- CFormView::OnInitialUpdate();
- GetParentFrame()->RecalcLayout();
- ResizeParentToFit();
- pEyeDlg = new CEyeDlg;
- pEyeDlg->Create(IDD_EYEDLG,NULL);
- pEyeDlg->ShowWindow(SW_HIDE);
- }
- /////////////////////////////////////////////////////////////////////////////
- // CMoLectureView printing
- BOOL CMoLectureView::OnPreparePrinting(CPrintInfo* pInfo)
- {
- // default preparation
- return DoPreparePrinting(pInfo);
- }
- void CMoLectureView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
- {
- // TODO: add extra initialization before printing
- }
- void CMoLectureView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
- {
- // TODO: add cleanup after printing
- }
- void CMoLectureView::OnPrint(CDC* pDC, CPrintInfo* /*pInfo*/)
- {
- // TODO: add customized printing code here
- }
- /////////////////////////////////////////////////////////////////////////////
- // CMoLectureView diagnostics
- #ifdef _DEBUG
- void CMoLectureView::AssertValid() const
- {
- CFormView::AssertValid();
- }
- void CMoLectureView::Dump(CDumpContext& dc) const
- {
- CFormView::Dump(dc);
- }
- /*CMoLectureDoc* CMoLectureView::GetDocument() // non-debug version is inline
- {
- ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMoLectureDoc)));
- return (CMoLectureDoc*)m_pDocument;
- }*/
- #endif //_DEBUG
- /////////////////////////////////////////////////////////////////////////////
- // CMoLectureView message handlers
- //添加图层
- void CMoLectureView::OnButton1()
- {
- // TODO: Add your control notification handler code here
- //选择图层文件
- CFileDialog dlg(true, ".shp","*.shp",OFN_HIDEREADONLY,"Shape文件(*.shp)|*.shp",NULL);
- CString str;
- if (dlg.DoModal()==IDOK)
- {
- str=dlg.GetPathName();
- }
- else
- return;
- //封装在MapHelper.h中的全局函数
- AddLayer(m_Map,str);
- //在主视图添加数据的同时也给鹰眼视图添加数据
- AddLayer(pEyeDlg->m_EyeMap,str);
- }
- //添加栅格影像图层
- void CMoLectureView::OnButton2()
- {
- // TODO: Add your control notification handler code here
- //选择图层文件
- CFileDialog dlg(true, ".bmp","*.bmp",OFN_HIDEREADONLY,"栅格文件(*.bmp)|*.bmp",NULL);
- CString str;
- if (dlg.DoModal()==IDOK)
- {
- str=dlg.GetPathName();
- }
- else
- return;
- //封装在MapHelper.h中的全局函数
- AddImageLayer(m_Map, str);
- }
- //获得层数
- void CMoLectureView::OnButton3()
- {
- // TODO: Add your control notification handler code here
- CMoLayers m_Layers=m_Map.GetLayers(); //获得图层集合
- int nLayerCount = m_Layers.GetCount();
- }
- //遍历图层
- void CMoLectureView::OnButton4()
- {
- // TODO: Add your control notification handler code here
- CMoLayers m_Layers=m_Map.GetLayers(); //获得图层集合
- int nLayerCount = m_Layers.GetCount();
- /*VARIANT index;
- VariantInit(&index);
- index.vt=VT_I2;
- for(i=0;i<nLayerCount;i++)
- {
- CMoMapLayer layer;
- index.iVal = i;
- layer = m_Layers.Item(index);
- AfxMessageBox(layer.GetName());
- }*/
- ///////
- long nCount = 0;
- COleVariant index;
- for(nCount=0;nCount<nLayerCount;nCount++)
- {
- CMoMapLayer layer;
- index = COleVariant(nCount,VT_I4);
- layer = m_Layers.Item(index);
- AfxMessageBox(layer.GetName());
- }
- }
- //遍历图层记录
- void CMoLectureView::OnButton5()
- {
- // TODO: Add your control notification handler code here
- CMoLayers m_Layers=m_Map.GetLayers(); //获得图层集合
- int nLayerCount = m_Layers.GetCount();
- VARIANT index;
- VariantInit(&index);
- index.vt=VT_I2;
- int i;
- for(i=0;i<nLayerCount;i++)
- {
- CMoMapLayer layer;
- index.iVal = i;
- layer = m_Layers.Item(index);
- CMoRecordset rs = layer.GetRecords();
- int j=0;
- rs.MoveFirst();
- for(j=0;j<rs.GetCount();j++)
- {
- CMoFields fs = rs.GetFields();
- rs.MoveNext();
- }
- }
- }
- //设置放大状态
- void CMoLectureView::OnButton6()
- {
- // TODO: Add your control notification handler code here
- m_Map.SetMousePointer(moZoomIn);
- m_ToolNum = 1;
- // ZoomIn(m_Map);
- }
- //设置缩小状态
- void CMoLectureView::OnButton7()
- {
- // TODO: Add your control notification handler code here
- m_ToolNum = 2;
- m_Map.SetMousePointer(moZoomOut);
- //ZoomOut(m_Map);
- }
- //设置漫游状态
- void CMoLectureView::OnButton8()
- {
- // TODO: Add your control notification handler code here
- m_ToolNum = 3;
- m_Map.SetMousePointer(moPan);
- // Pan(m_Map);
- }
- //全图操作
- void CMoLectureView::OnButton9()
- {
- // TODO: Add your control notification handler code here
- CMoRectangle f = m_Map.GetFullExtent();
- m_Map.SetExtent(f);
- }
- BEGIN_EVENTSINK_MAP(CMoLectureView, CFormView)
- //{{AFX_EVENTSINK_MAP(CMoLectureView)
- ON_EVENT(CMoLectureView, IDC_MAP1, -605 /* MouseDown */, OnMouseDownMap1, VTS_I2 VTS_I2 VTS_I4 VTS_I4)
- ON_EVENT(CMoLectureView, IDC_MAP1, -606 /* MouseMove */, OnMouseMoveMap1, VTS_I2 VTS_I2 VTS_I4 VTS_I4)
- ON_EVENT(CMoLectureView, IDC_MAP1, 4 /* AfterTrackingLayerDraw */, OnAfterTrackingLayerDrawMap1, VTS_I4)
- //}}AFX_EVENTSINK_MAP
- END_EVENTSINK_MAP()
- void CMoLectureView::OnMouseDownMap1(short Button, short Shift, long X, long Y)
- {
- // TODO: Add your control notification handler code here
- //为点线面增加临时图层
- CMoTrackingLayer tLayer(m_Map.GetTrackingLayer());
- if(m_ToolNum == 1)
- {
- //地图放大
- //ZoomIn(m_Map);
- //地图依据比例尺放大
- //CMoRectangle f = m_Map.GetExtent();
- //f.ScaleRectangle(.5);
- //m_Map.SetExtent(f);
- //地图拉框放大
- CMoRectangle r = m_Map.TrackRectangle();
- m_Map.SetExtent(r);
- pEyeDlg->m_EyeMap.Refresh();
- }
- else if(m_ToolNum == 2)
- {
- //地图依据比例尺缩小
- CMoRectangle f = m_Map.GetExtent();
- f.ScaleRectangle(1.5);
- m_Map.SetExtent(f);
- pEyeDlg->m_EyeMap.Refresh();
- }
- else if(m_ToolNum == 3)
- {
- //地图漫游
- Pan(m_Map);
- pEyeDlg->m_EyeMap.Refresh();
- }
- else if(m_ToolNum == 4)
- {
- //量距操作
- CMoLine line = m_Map.TrackLine();
- CString str;
- str.Format("The Distance of Line is %.3f",line.GetLength());
- AfxMessageBox(str);
- }
- else if(m_ToolNum == 5)
- {
- //量面积操作
- CMoPolygon polygon = m_Map.TrackPolygon();
- CString str;
- str.Format("The area is %.3f",polygon.GetArea());
- AfxMessageBox(str);
- }
- else if(m_ToolNum == 6)
- {
- //添加点
- 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);
- VARIANT va;
- VariantInit(&va);
- va.vt = VT_DISPATCH;
- va.pdispVal = r.m_lpDispatch;
- tLayer.Refresh(true, va);
- }
- else if(m_ToolNum == 7)
- {
- //添加线
- CMoLine* line = new CMoLine(m_Map.TrackLine());
- if (LPDISPATCH(line))
- m_lines.Add(line);
- CMoRectangle r = CMoRectangle(line->GetExtent());
- VARIANT va;
- VariantInit(&va);
- va.vt = VT_DISPATCH;
- va.pdispVal = r.m_lpDispatch;
- tLayer.Refresh(true, va);
- }
- else if(m_ToolNum == 8)
- {
- //添加面
- CMoPolygon* poly = new CMoPolygon(m_Map.TrackPolygon());
- if (LPDISPATCH(poly))
- m_polys.Add(poly);
- CMoRectangle rect = CMoRectangle(poly->GetExtent());
- VARIANT va;
- VariantInit(&va);
- va.vt = VT_DISPATCH;
- va.pdispVal = rect.m_lpDispatch;
- tLayer.Refresh(true, va);
- }
- else if(m_ToolNum == 9)
- {
- //点查询
- CMoLayers layers(m_Map.GetLayers());//得到所有图层
- VARIANT vItem;//万能变量
- VariantInit(&vItem);//万能变量初始化
- vItem.vt = VT_I4;//将类型设置为整型
- vItem.lVal = 0;//将万能变量初始值设为0
- CMoMapLayer states(layers.Item(vItem));//将当前层设为第vItem层
- const double tolerance = m_Map.ToMapDistance((float)10);
- //将容忍度设为10
- CMoPoint mapPt(m_Map.ToMapPoint((float)X, (float)Y));
- CMoRecordset recs(states.SearchByDistance(mapPt, tolerance, TEXT("")));
- //寻找与该点近周的一个矩形框
- if (recs.GetEof())
- ::MessageBeep(MB_ICONQUESTION);//如果没有成功
- else
- {
- CMoFields fields(recs.GetFields());//获取矩形框
- CMoField shapeField(fields.Item(COleVariant(TEXT("Shape"))));
- CMoField areaField(fields.Item(COleVariant(TEXT("Area"))));
- double dArea = areaField.GetValue().dblVal;
- //读出Shape里的属性
- 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, 5);//重画5次选中区域
- }
- }
- /* else if(m_ToolNum == 10)
- {
- //点查询
- CMoLayers layers(m_Map.GetLayers());//得到所有图层
- VARIANT vItem;//万能变量
- VariantInit(&vItem);//万能变量初始化
- vItem.vt = VT_I4;//将类型设置为整型
- vItem.lVal = 0;//将万能变量初始值设为0
- CMoMapLayer states(layers.Item(vItem));//将当前层设为第vItem层
- CMoPoint mapPt(m_Map.ToMapPoint((float)X, (float)Y));
- CMoRecordset recs;
- //查询在点所在的多边形
- recs = states.SearchShape(mapPt, moPointInPolygon, TEXT(""));
- if (recs.GetEof())
- ::MessageBeep(MB_ICONQUESTION);//如果没有成功
- else
- {
- CMoFields fields(recs.GetFields());//获取矩形框
- CMoField shapeField(fields.Item(COleVariant(TEXT("Shape"))));
- CMoField areaField(fields.Item(COleVariant(TEXT("Area"))));
- double dArea = areaField.GetValue().dblVal;
- //读出Shape里的属性
- 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, 5);//重画5次选中区域
- }
- }*/
- else if(m_ToolNum == 10)
- {
- //线查询
- CMoLayers layers(m_Map.GetLayers());//得到所有图层
- VARIANT vItem;//万能变量
- VariantInit(&vItem);//万能变量初始化
- vItem.vt = VT_I4;//将类型设置为整型
- vItem.lVal = 0;//将万能变量初始值设为0
- CMoMapLayer states(layers.Item(vItem));//将当前层设为第vItem层
- CMoLine* line = new CMoLine(m_Map.TrackLine());
- CMoRecordset recs;
- //查询点与线相交的多边形
- recs = states.SearchShape(*line, moLineCross , TEXT(""));
- int recCount = recs.GetCount();
- while(!recs.GetEof())
- {
- CMoFields fields(recs.GetFields());//获取矩形框
- CMoField shapeField(fields.Item(COleVariant(TEXT("Shape"))));
- //读出Shape里的属性
- 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, 5);//重画5次选中区域
- recs.MoveNext();
- }
- }
- }
- //获取地图坐标
- void CMoLectureView::OnMouseMoveMap1(short Button, short Shift, long X, long Y)
- {
- // TODO: Add your control notification handler code here
- CMoPoint pt = ToMapPoint(m_Map,X,Y);
- m_X = pt.GetX();
- m_Y = pt.GetY();
- UpdateData(FALSE);
- }
- //设置量距操作状态
- void CMoLectureView::OnButton10()
- {
- // TODO: Add your control notification handler code here
- m_ToolNum = 4;
- m_Map.SetMousePointer(moArrow);
- }
- //设置量面积操作状态
- void CMoLectureView::OnButton11()
- {
- // TODO: Add your control notification handler code here
- m_ToolNum = 5;
- m_Map.SetMousePointer(moArrow);
- }
- //添加点
- void CMoLectureView::OnButton12()
- {
- // TODO: Add your control notification handler code here
- m_ToolNum = 6;
- m_Map.SetMousePointer(moArrow);
- }
- //添加线
- void CMoLectureView::OnButton13()
- {
- // TODO: Add your control notification handler code here
- m_ToolNum = 7;
- m_Map.SetMousePointer(moArrow);
- }
- //添加面
- void CMoLectureView::OnButton14()
- {
- // TODO: Add your control notification handler code here
- m_ToolNum = 8;
- m_Map.SetMousePointer(moArrow);
- }
- void CMoLectureView::OnAfterTrackingLayerDrawMap1(long hDC)
- {
- // TODO: Add your control notification handler code here
- //定义符号
- CMoSymbol sym;
- if(!sym.CreateDispatch("MapObjects2.Symbol"))
- return;
- sym.SetColor(moPurple);
- //画点
- int i;
- // Draw points
- sym.SetColor(moGreen);
- sym.SetSymbolType(moPointSymbol);
- sym.SetSize(5);
- sym.SetStyle(moCrossMarker );
- for ( i = 0; i < m_points.GetSize(); i++)
- m_Map.DrawShape(*m_points[i], sym);
- //draw lines
- sym.SetSymbolType(moLineSymbol);
- sym.SetStyle(moSolidLine);
- sym.SetSize(2);
- for ( 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);
- }
- //保存图层
- void CMoLectureView::OnButton15()
- {
- // TODO: Add your control notification handler code here
- }
- //设置点查询状态
- void CMoLectureView::OnButton16()
- {
- // TODO: Add your control notification handler code here
- m_ToolNum = 9;
- m_Map.SetMousePointer(moArrow);
- }
- //属性查询
- void CMoLectureView::OnButton17()
- {
- // TODO: Add your control notification handler code here
- UpdateData(TRUE);
- CString expression;
- expression.Format("(State_name Like '%%%s%%')", m_strQuery);
- //
- // Get the states layer and search
- //
- CMoLayers layers(m_Map.GetLayers());
- CMoMapLayer states(layers.Item(COleVariant(TEXT("STATES"))));
- CMoRecordset recs(states.SearchExpression(expression));
- //
- // Beep if not found. Otherwise, show the first state
- // found by zooming in and flashing.
- //
- if (recs.GetEof())
- ::MessageBeep(MB_ICONQUESTION);
- else
- {
- CMoFields fields(recs.GetFields());
- CMoField shapeField(fields.Item(COleVariant(TEXT("Shape"))));
- 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);
- }
- }
- //显示图层中所有对象名称
- void CMoLectureView::OnButton18()
- {
- // TODO: Add your control notification handler code here
- CMoRecordset recs; //图层的记录集
- CMoStrings strs; //MO的字符串类
- strs.CreateDispatch("MapObjects2.Strings");
- CMoLayers m_Layers=m_Map.GetLayers();
- CMoMapLayer m_MapLayer;
- VARIANT va;
- VariantInit(&va);
- va.vt=VT_I4;
- va.lVal=0;
- m_MapLayer=m_Layers.Item(va);
- //得到图层数据中NAME字段的记录,保存在strs中
- recs = m_MapLayer.GetRecords();
- VARIANT vt0;
- while(!recs.GetEof())
- {
- CMoFields mfs = recs.GetFields();
- CMoField mf = mfs.Item(COleVariant("POP1990"));
- vt0 = mf.Get_Value();
- if(vt0.vt == VT_I4)
- {
- long pop = mf.Get_Value().lVal;
- CString str;
- str.Format("%ld",pop);
- //CString s = mf.Get_Value().bstrVal;
- m_ListAttribute.AddString(str);
- recs.MoveNext();
- }
- else
- {
- MessageBox("该字段不能转变成CString类型,rn所以不能将其显示在组合框中!rn也不能对该字段进行属性查询!!");
- return;
- }
- }
- }
- //显示层中所有字段名称
- void CMoLectureView::OnButton19()
- {
- // TODO: Add your control notification handler code here
- CMoLayers m_Layers=m_Map.GetLayers(); //获得图层集合
- int nLayerCount = m_Layers.GetCount();
- VARIANT index;
- VariantInit(&index);
- index.vt=VT_I2;
- int i;
- int j;
- for(i=0;i<nLayerCount;i++)
- {
- CMoMapLayer layer;
- index.iVal = i;
- layer = m_Layers.Item(index);
- CMoRecordset rs = layer.GetRecords();
- CMoTableDesc tableDesc=rs.GetTableDesc();
- long fdOfTable = tableDesc.GetFieldCount();
- //遍历表描述,得到每一个字段的名称
- for(j=0;j<fdOfTable;j++)
- {
- AfxMessageBox(tableDesc.GetFieldName((short)j));
- }
- }
- }
- //保存点层
- void CMoLectureView::OnButton20()
- {
- // TODO: Add your control notification handler code here
- // Open a new connection
- //选择图层文件
- CFileDialog dlg(FALSE, ".shp","*.shp",OFN_HIDEREADONLY,"Shape文件(*.shp)|*.shp",NULL);
- CString str;
- if (dlg.DoModal()==IDOK)
- {
- str=dlg.GetPathName();
- }
- else
- return;
- //创建表格存放点的属性,并指定其存放的路径
- CMoDataConnection conn;
- if (!conn.CreateDispatch(TEXT("MapObjects2.DataConnection")))
- return;
- conn.SetDatabase(GetFileDirectory(str));
- 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("人口"));
- tableDesc.SetFieldType(2, moDouble);
- tableDesc.SetFieldPrecision(2, 15);
- tableDesc.SetFieldScale(2, 3); //
- //////////////////////保存点的信息
- //创建层,并与属性表关联
- VARIANT va;
- VariantInit(&va);
- va.vt = VT_BOOL;
- va.boolVal = false;
- CMoGeoDataset geoDataset(conn.AddGeoDataset(GetFileTitle(str), moPoint, tableDesc, va, va));
- CMoMapLayer layer;
- if (!layer.CreateDispatch(TEXT("MapObjects2.MapLayer")))
- return;
- layer.SetGeoDataset(geoDataset);
- // Add a record for each pointgon to the GeoDataset
- CMoRecordset recs(layer.GetRecords());
- CMoFields fields(recs.GetFields());
- CString featureName;
- for (int i = 0; i < m_points.GetSize(); i++)
- {
- recs.AddNew();
- // "Shape" = m_point
- SetValue(fields, TEXT("Shape"), LPDISPATCH(*m_points[i]));
- // "Name" = featureName
- featureName.Format("%s%d", "point", i);
- SetValue(fields, TEXT("Name"), featureName);
- // "Area" = m_point.GetArea()
- SetValue(fields, TEXT("Area"), 0.0);
- // "Perimeter" = m_point.GetPerimeter()
- SetValue(fields, TEXT("Perimeter"), 0.0);
- recs.Update();
- }
- // Add new layer to map
- CMoLayers layers(m_Map.GetLayers());
- layers.Add(layer);
- }
- //空间关系查询
- void CMoLectureView::OnButton21()
- {
- // TODO: Add your control notification handler code here
- m_ToolNum = 10;
- m_Map.SetMousePointer(moArrow);
- }
- //制作点专题图
- void CMoLectureView::OnButton22()
- {
- // TODO: Add your control notification handler code here
- CMoLayers m_Layers=m_Map.GetLayers();
- CMoMapLayer m_MapLayer;
- VARIANT va;
- VariantInit(&va);
- va.vt=VT_I4;
- va.lVal=0;
- m_MapLayer=m_Layers.Item(va);
- CMoRecordset recs(m_MapLayer.GetRecords());
- //对属性项的所有数据进行统计计算,并将所有统计结果赋给一个CMoStatistics对象
- CMoStatistics stats(recs.CalculateStatistics("POP1990"));
- //定义点渲染对象,计算点对象的点值
- CMoDotDensityRenderer ddRenderer;
- //创建一个新的CMoDotDensityRenderer对象
- VERIFY(ddRenderer.CreateDispatch(TEXT("MapObjects2.DotDensityRenderer")));
- ddRenderer.SetField("POP1990");
- double range = stats.GetMax() - stats.GetMin();
- ddRenderer.SetDotValue(range/500);
- ddRenderer.SetDotSize(3);
- ddRenderer.SetDotColor(moRed);
- //将设置好的CMoDotDensityRenderer对象与需要符号化的要素层挂接起来,并重画地图
- m_MapLayer.SetRenderer(ddRenderer);
- m_Map.Refresh();
- }
- //分级专题图
- void CMoLectureView::OnButton23()
- {
- // TODO: Add your control notification handler code here
- CMoLayers m_Layers=m_Map.GetLayers();
- CMoMapLayer m_MapLayer;
- VARIANT va;
- VariantInit(&va);
- va.vt=VT_I4;
- va.lVal=0;
- m_MapLayer=m_Layers.Item(va);
- CMoRecordset recs(m_MapLayer.GetRecords());
- //创建一个CMoClassBreakRender对象,并设置其属性
- CMoClassBreaksRenderer cbRenderer;
- VERIFY(cbRenderer.CreateDispatch(TEXT("MapObjects2.ClassBreaksRenderer")));
- cbRenderer.SetField("POP1990");
- //对属性项的所有数据进行统计计算,并将统计结果赋给一个CMoStatistics对象
- CMoStatistics stats(recs.CalculateStatistics("POP1990"));
- //利用给定的分级级数和统计结果按照标准偏差分级法来计算
- const int numClasses = 6;
- double breakVal = stats.GetMean() - (stats.GetStdDev() * ((double)numClasses / 2.0));
- for(int i=0; i<numClasses;i++)
- {
- if(stats.GetMin()<=breakVal&&breakVal<=stats.GetMax())
- {
- cbRenderer.SetBreakCount(cbRenderer.GetBreakCount() + 1);
- cbRenderer.SetBreak(cbRenderer.GetBreakCount() - 1, breakVal);
- }
- breakVal += stats.GetStdDev();
- }
- //用一组连续渐变色来表示区分每个级别
- cbRenderer.RampColors(moLightYellow,moBlue);
- m_MapLayer.SetRenderer(cbRenderer);
- m_Map.Refresh();
- }
- //地图输出
- void CMoLectureView::OnButton24()
- {
- // TODO: Add your control notification handler code here
- //选择输出文件
- CFileDialog dlg(false, ".bmp","*.bmp",OFN_HIDEREADONLY,"栅格文件(*.bmp)|*.bmp",NULL);
- CString str;
- if (dlg.DoModal()==IDOK)
- {
- str=dlg.GetPathName();
- }
- else
- return;
- RECT r;
- m_Map.GetWindowRect(&r);
- m_Map.ExportMap(moExportBMP,str, 2);
- }
- //激活鹰眼视图
- void CMoLectureView::OnButton25()
- {
- // TODO: Add your control notification handler code here
- pEyeDlg->ShowWindow(SW_SHOW);
- }