




免费预览已结束,剩余26页可下载查看
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
MapObjects二次开发实习讲义一、 创建一个MapObjects工程1、 新建VC工程Step 1:新建工程Step 2:选择Single DocumentStep 3:选择CFormView2、 添加MapObjects组件Step 1:选择Project-Add to Project-ComponentStep 2:选择MapObjects组件Step 3:选择只添加一个MapObjects对象(全部选中)Step 4:将MapObjects组件添加到Form,并定义变量Step 5:为MapObjects控件定义变量3、 工程整理Step 1:将MapObjects2.h、MapHelper.h、MapHelper.cpp、pedef.h拷贝至工程目录Step 2:选择Project-Add to Project-Files将MapObjects2.h、MapHelper.h、MapHelper.cpp添加到工程中Step 3:在Stdafx.h中添加#Include “MapObjects.h”和“Maphelper.h”Step 4:编译工程,成功,一个标准的MapObjects工程创建成功。二、 加载图层1、添加矢量图层/添加图层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();elsereturn;/封装在MapHelper.h中的全局函数 AddLayer(m_Map,str);2、添加栅格图层/添加栅格影像图层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();elsereturn;/封装在MapHelper.h中的全局函数AddImageLayer(m_Map, str);三、 图层管理1、图层数量/获得层数void CMoLectureView:OnButton3() / TODO: Add your control notification handler code hereCMoLayers m_Layers=m_Map.GetLayers(); /获得图层集合int nLayerCount = m_Layers.GetCount();2、遍历图层/遍历图层void CMoLectureView:OnButton4() / TODO: Add your control notification handler code hereCMoLayers m_Layers=m_Map.GetLayers(); /获得图层集合int nLayerCount = m_Layers.GetCount();int i;VARIANT index;VariantInit(&index);index.vt=VT_I2;for(i=0;inLayerCount;i+)CMoMapLayer layer;index.iVal = i;layer = m_Layers.Item(index);AfxMessageBox(layer.GetName(); 遍历图层可以得到每一个图层对象Layer,并对每一个图层对象可以遍历图层对象的属性、操作图层。如设置图层的可见性:layer.SetVisible(FALSE);万能变量的定义:(1)方式1VARIANT index;VariantInit(&index);index.vt=VT_I2;index.iVal = i;(2)方式2long nCount = 0;COleVariant index;index = COleVariant(nCount,VT_I4);3、得到层中所有特征/遍历图层记录void CMoLectureView:OnButton5() / TODO: Add your control notification handler code hereCMoLayers m_Layers=m_Map.GetLayers(); /获得图层集合int nLayerCount = m_Layers.GetCount();VARIANT index;VariantInit(&index);index.vt=VT_I2;int i;for(i=0;inLayerCount;i+)CMoMapLayer layer;index.iVal = i;layer = m_Layers.Item(index);CMoRecordset rs = layer.GetRecords();int j=0;rs.MoveFirst();for(j=0;jrs.GetCount();j+)CMoFields fs = rs.GetFields();rs.MoveNext();四、 地图操作1、地图放大ZoomIn(m_Map);2、地图缩小ZoomOut(m_Map);3、地图漫游Pan(m_Map);4、设置鼠标状态m_Map.SetMousePointer(moZoomOut);5、 全图操作CMoRectangle f = m_Map.GetFullExtent(); m_Map.SetExtent(f);6、 地图依据比例尺缩放void CMoLectureView:OnMouseDownMap1(short Button, short Shift, long X, long Y) / TODO: Add your control notification handler code hereif(m_ToolNum = 1)/地图放大/ZoomIn(m_Map);/地图依据比例尺放大CMoRectangle f = m_Map.GetExtent();f.ScaleRectangle(.5);m_Map.SetExtent(f);else if(m_ToolNum = 2)/地图依据比例尺缩小CMoRectangle f = m_Map.GetExtent();f.ScaleRectangle(1.5);m_Map.SetExtent(f);else if(m_ToolNum = 3)Pan(m_Map);7、 地图拉框放大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);五、 事件响应1、获取鼠标漫游地图坐标Step 1:给地图添加鼠标漫游事件,右键点击地图Step 2:添加MouseMoveMap1消息Step 3:在OnMouseDownMap1消息中添加函数进行放大、缩小、漫游等事件相应void CMoLectureView:OnMouseDownMap1(short Button, short Shift, long X, long Y) / TODO: Add your control notification handler code hereif(m_ToolNum = 1)ZoomIn(m_Map);Step 4:在MouseMoveMap1消息中添加获取地图坐标/获取地图坐标void CMoLectureView:OnMouseMoveMap1(short Button, short Shift, long X, long Y) / TODO: Add your control notification handler code hereCMoPoint pt = ToMapPoint(m_Map,X,Y);m_X = pt.GetX();m_Y = pt.GetY();UpdateData(FALSE);2、量距操作Step1:定义工具,与一个菜单或按钮关联/设置量距操作void CMoLectureView:OnButton10() / TODO: Add your control notification handler code herem_ToolNum = 4;m_Map.SetMousePointer(moArrow);Step 2:在OnMouseDownMap1消息中添加函数获取量距距离操作的代码else if(m_ToolNum = 4)/量距操作CMoLineline = m_Map.TrackLine();CString str;str.Format(The Distance of Line is %.3f,line.GetLength();AfxMessageBox(str);2、量面积操作Step1:定义工具,与一个菜单或按钮关联/设置量面积操作状态void CMoLectureView:OnButton11() / TODO: Add your control notification handler code herem_ToolNum = 5;m_Map.SetMousePointer(moArrow);Step 2:在OnMouseDownMap1消息中添加函数获取量距距离操作的代码else if(m_ToolNum = 5)/量面积操作CMoPolygonpolygon = m_Map.TrackPolygon();CString str;str.Format(The area is %.3f,polygon.GetArea();AfxMessageBox(str);六、 添加点、线、面对象1、 添加点对象Step 1定义工具,与一个菜单或按钮关联/添加点void CMoLectureView:OnButton12() / TODO: Add your control notification handler code herem_ToolNum = 6;m_Map.SetMousePointer(moArrow);Step 2 获取点坐标1) 先定义点组在类中添加点组类/对象组class CMoPointArray : public CTypedPtrArray ;class CMoPolyArray : public CTypedPtrArray ;class CMoLineArray : public CTypedPtrArray ;class CMoRectangleArray : public CTypedPtrArray ;class CMoEllipseArray : public CTypedPtrArray ;2) 添加点组对象CMoPointArray m_points;/ Points to be saved.需要指出:CTypedPtrArray为MFC的模板类,需要在StaAfx.h中包含:#include /添加使得可以使用模板类来编程3) 在在OnMouseDownMap1消息中添加获取点坐标的代码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 pointCMoRectangle 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);Step 3 在OnAfterTrackingLayerDrawMap1中绘制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 pointssym.SetColor(moRed);sym.SetSymbolType(moPointSymbol);sym.SetStyle(moCircleMarker);for ( i = 0; i m_points.GetSize(); i+)m_Map.DrawShape(*m_pointsi, sym);/draw linessym.SetSymbolType(moLineSymbol);sym.SetStyle(moSolidLine);sym.SetSize(2);for ( i = 0; i m_lines.GetSize(); i+)m_Map.DrawShape(*m_linesi, sym);/ Draw polygons sym.SetSymbolType(moFillSymbol);sym.SetStyle(moGrayFill); for (i = 0; i GetExtent();VARIANT va;VariantInit(&va);va.vt = VT_DISPATCH;va.pdispVal = r.m_lpDispatch; tLayer.Refresh(true, va);3、 添加面对象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);4、 保存对象图层/保存点层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();elsereturn;/创建表格存放点的属性,并指定其存放的路径CMoDataConnection conn;if (!conn.CreateDispatch(TEXT(MapObjects2.DataConnection)return;conn.SetDatabase(GetFileDirectory(str);if (!conn.Connect()return;/ Define the geodatasetCMoTableDesc tableDesc;if (!tableDesc.CreateDispatch(TEXT(MapObjects2.TableDesc)return;/ set the field names, types, and lengthstableDesc.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 placestableDesc.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 GeoDatasetCMoRecordset recs(layer.GetRecords();CMoFields fields(recs.GetFields();CString featureName;for (int i = 0; i m_points.GetSize(); i+)recs.AddNew();/ Shape = m_pointSetValue(fields, TEXT(Shape), LPDISPATCH(*m_pointsi);/ Name = featureNamefeatureName.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 mapCMoLayers layers(m_Map.GetLayers();layers.Add(layer);七、 空间查询1、点查询Step 1定义工具,与一个菜单或按钮关联/设置点查询状态void CMoLectureView:OnButton16() / TODO: Add your control notification handler code herem_ToolNum = 9;m_Map.SetMousePointer(moArrow);Step 2:在OnMouseDownMap1消息中添加点查询操作代码else if(m_ToolNum = 9)/点查询CMoLayerslayers(m_Map.GetLayers();/得到所有图层VARIANT vItem;/万能变量VariantInit(&vItem);/万能变量初始化vItem.vt = VT_I4;/将类型设置为整型vItem.lVal = 0;/将万能变量初始值设为0CMoMapLayerstates(layers.Item(vItem);/将当前层设为第vItem层const double tolerance = m_Map.ToMapDistance(float)10);/将容忍度设为10CMoPointmapPt(m_Map.ToMapPoint(float)X, (float)Y);CMoRecordsetrecs(states.SearchByDistance(mapPt, tolerance, TEXT();/寻找与该点近周的一个矩形框if (recs.GetEof():MessageBeep(MB_ICONQUESTION);/如果没有成功elseCMoFields 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 statem_Map.Refresh();/ force redrawm_Map.FlashShape(shape, 5);/重画5次选中区域2、空间关系选择(1)按照点与多边形的关系查询else if(m_ToolNum = 10)/点查询CMoLayerslayers(m_Map.GetLayers();/得到所有图层VARIANT vItem;/万能变量VariantInit(&vItem);/万能变量初始化vItem.vt = VT_I4;/将类型设置为整型vItem.lVal = 0;/将万能变量初始值设为0CMoMapLayerstates(layers.Item(vItem);/将当前层设为第vItem层CMoPointmapPt(m_Map.ToMapPoint(float)X, (float)Y);CMoRecordsetrecs;/查询在点所在的多边形recs = states.SearchShape(mapPt, moPointInPolygon, TEXT();if (recs.GetEof():MessageBeep(MB_ICONQUESTION);/如果没有成功elseCMoFields 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 statem_Map.Refresh();/ force redrawm_Map.FlashShape(shape, 5);/重画5次选中区域(2)按照线与多边形的关系查询else if(m_ToolNum = 10)/线查询CMoLayerslayers(m_Map.GetLayers();/得到所有图层VARIANT vItem;/万能变量VariantInit(&vItem);/万能变量初始化vItem.vt = VT_I4;/将类型设置为整型vItem.lVal = 0;/将万能变量初始值设为0CMoMapLayerstates(layers.Item(vItem);/将当前层设为第vItem层CMoLine* line = new CMoLine(m_Map.TrackLine();CMoRecordsetrecs;/查询点与线相交的多边形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 statem_Map.Refresh();/ force redrawm_Map.FlashShape(shape, 5);/重画5次选中区域recs.MoveNext();八、 属性查询1、SQL属性查询/属性查询void CMoLectureView:OnButton17() / TODO: Add your control notification handler code hereUpdateData(TRUE);CString expression;expression.Format(State_name Like %s%), m_strQuery);/ Get the states layer and search/CMoLayerslayers(m_Map.GetLayers();CMoMapLayerstates(layers.Item(COleVariant(TEXT(STATES);CMoRecordset recs(states.SearchExpression(expression);if (recs.GetEof():MessageBeep(MB_ICONQUESTION);elseCMoFields 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 statem_Map.Refresh();/ force redrawm_Map.FlashShape(shape, 3);2、属性遍历/显示图层中所有对象名称void CMoLectureView:OnButton18() / TODO: Add your control notification handler code hereCMoRecordset 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(STATE_NAME);vt0 = mf.Get_Value();if(vt0.vt = VT_BSTR)CString s = mf.Get_Value().bstrVal;m_ListAttribute.AddString(s);recs.MoveNext();九、 专题图1、点专题图/制作点专题图void CMoLectureView:OnButton22() / TODO: Add your control notification handler code hereCMoLayers 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/5);ddRenderer.SetDotSize(2);ddRenderer.SetDotColor(moRed);/将设置好的CMoDotDensityRenderer对象与需要符号化的要素层挂接起来,并重画地图m_MapLayer.SetRenderer(ddRenderer);m_Map.Refresh();2、分级专题图/分级专题图void CMoLectureView:OnButton23() / TODO: Add your control notification handler code hereCMoLayers 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; inumClasses;i+)if(stats.GetMin()=breakVal&breakValRecalcLayout();ResizeParentToFit();/初始化鹰眼无模态对话框pEyeDlg = new CEyeDl
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 营养师考试备考 2025年实操技能冲刺模拟试卷
- 2025年春季英语四六级专项训练:冲刺押题模拟试卷
- 2025年公务员考试行测言语理解专项试卷:逻辑判断与推理能力冲刺押题
- 2025年CPA考试 会计科目全真模拟试卷及解题技巧
- 2026届佛山市普通高中化学高三第一学期期末质量检测模拟试题含解析
- 安徽省示范中学培优联盟2026届高二化学第一学期期末经典试题含答案
- 王牌交易平台拆分课件
- 2026届安徽省部分高中化学高一上期中质量检测模拟试题含解析
- 言情小说竞赛题目及答案
- 第十三讲蛋白质分子设计
- 超星尔雅学习通《形势与政策(2025春)》章节测试及答案(全国)
- 标准化管理意识培训课件
- 2025年幼儿教师资格保教知识与能力试题及答案
- (2025年标准)职工转岗协议书
- 湖南省衡阳市祁东县2024-2025学年七年级下学期期末考试英语试卷(含笔试答案无听力音频及袁恩)
- 2025年电动洗胃考试试题及答案
- 警察警械使用培训课件
- 2025年安徽省农业职业技能大赛(水生物病害防治员)备赛试题库(含答案)
- 图画捉迷藏-A4打印版
- 受限空间作业票
- 盘扣式外脚手架施工方案
评论
0/150
提交评论