ArcEngine中最短路径的实现 - 图文
更新时间:2024-03-15 10:31:01 阅读量: 综合文库 文档下载
ArcEngine中最短路径的实现
最短路径分析属于ArcGIS的网络分析范畴。而ArcGIS的网络分析分为两类,分别是基于几何网络和网络数据集的网络分析。它们都可以实现最短路径功能。下面先介绍基于几何网络的最短路径分析的实现。以后会陆续介绍基于网络数据集的最短路径分析以及这两种方法的区别。
几何网络是一种特殊的特征要素类,由一系列不同类别的点要素和线要素(可以度量并能图形表达)组成的,可在FeatureDataset下面创建, 可进行图形与属性的编辑。包括流向分析和追踪分析两大功能。主要接口是ITraceFlowSolver。我们先在一幅地图上做出一个几何网络才能进行最 短路径分析。下面是主要的一些步骤:
1、打开ArcCatalog,连接到包含地图的文件夹。 2、在空白处,右键新建一个“Personal GeoDatabase”。
3、在生成的Personal GeoDatabase上右键新建一个feature dataset。 4、双击Personal GeoDatabase进去,找到刚才new出的feature dataset,右键Import导入Feature Class(Single),选择要建立几何网络的图层或者shape文件。
5、然后再右键新建一个Geometric Network,选择从已存在的图元中建立几何网络。
6、打开ArcMap,把刚才建立的“Personal GeoDatabase Feature Class”添加到地图中,这样几何网络就建立好了。
这样我们就建立好一个几何网络了。我们现在要通过编程来实现最短路径,用到的接口主要有 INetworkCollection,IGeometricNetwork,IPointToEID,ITraceFlowSolverGEN(它实现 了ITraceFlowSolver的接口),INetSchema,IEIDHelper等。主要步骤如下: 1、获取几何网络工作空间
2、定义一个边线旗数组,把离点串最近的网络元素添加进数组 3、设置开始和结束边线的权重 4、进行路径分析 5、得到路径分析的结果
上次介绍了用几何网络实现的“最短路径”,这次用网络数据集实现真正的最短路径功能,跟上次一样,先处理下数据。
1、先打开ArcCatalog,连接到目标文件夹,假定该文件下有一个名为road的道路图层。
2、在road图层上右键新建一个网络数据集,并按照其默认设置直至完成。
3、打开该地图的工作空间,把刚才新建的网络数据集添加工作空间中。
4、在网络分析菜单中选择新建最近设施点。
这时在工作空间里,可以看到多了一个名为“Closest Facility”的图层。它下面还有4个子图层,名字分别为 “Facilities”,“Incidents”,“Barriers”,“Routes”。“Facilities”就是设施点图层,也就是目的 点,“Incidents”的意思就是出发点,“Barriers”是障碍点,意思就是地图某条道路附近有一个障碍点,如果障碍点与道路距离在容限范围 内,则表示此道路不通,“Routes”就是最终的结果。这样我们编程实现最短路径的思路就出现了: 1、添加出发点。 2、添加目的点。
3、生成最优路径,获取结果。
这里的添加出发点或者目的点,是往“Facilities”或“Incidents”图层上添加元素。获取结果也是从“Routes”中获取 Polyline。往“Facilities”或“Incidents”图层上添加元素用到的主要方法是INALocator的
QueryLocationByPoint函数,生成路径主要接口是INASolver和它的Solve方法。获取结果是按属性查找,因为 “Routes”类其实就是一个图层类,只不过只是存在于内存。
1 CMapControlDefault m_map;
2 IPointCollectionPtr m_ipPointCollection; 3
4 ILayerPtr ipLayer = m_map.GetLayer(0); // 网络数据集 5 INALayerPtr ipNaLayer = ipLayer; 6 if (NULL == ipNaLayer) 7 {
8 return; 9 } 10
11 INAContextPtr ipNaContext;
12 HRESULT hr = ipNaLayer->get_Context(&ipNaContext);
13 INAClassLoaderPtr ipNAClassLoader(CLSID_NAClassLoader); 14 INALocatorPtr ipNALocator = NULL;
15 hr = ipNaContext->get_Locator(&ipNALocator); 16 ipNALocator->put_SnapToleranceUnits(esriMeters); 17 ipNALocator->put_SnapTolerance(200); 18 ipNaContext;
19 hr = ipNAClassLoader->putref_Locator(ipNALocator); 20
21 INamedSetPtr ipNamedSet = NULL;
22 ipNaContext->get_NAClasses(&ipNamedSet); 23
24 CString szName = \
25 BSTR bstrName = szName.AllocSysString(); 26 INAClassPtr ipNAFacilitiesClass = NULL; 27 hr = ipNamedSet->get_ItemByName(bstrName, (IUnknown**)&ipNAFacilitiesClass); 28 szName = \
29 bstrName = szName.AllocSysString(); 30 INAClassPtr ipNAIncidentsClass = NULL; 31 hr = ipNamedSet->get_ItemByName(bstrName, (IUnknown**)&ipNAIncidentsClass); 32 szName = \
33 bstrName = szName.AllocSysString(); 34 INAClassPtr ipNARoutesClass = NULL; 35 hr = ipNamedSet->get_ItemByName(bstrName, (IUnknown**)&ipNARoutesClass); 36
37 INALocationPtr ipNALocation1(CLSID_NALocation); 38 INALocationPtr ipNALocation2(CLSID_NALocation);
39 ipNAClassLoader->get_Locator(&ipNALocator); 40 IPointPtr ipBeginPoint(CLSID_Point);
41 m_ipPointCollection->get_Point(0, &ipBeginPoint); 42 IPointPtr ipEndPoint(CLSID_Point);
43 m_ipPointCollection->get_Point(1, &ipEndPoint); 44 IPointPtr ipPoint1(CLSID_Point); 45 IPointPtr ipPoint2(CLSID_Point); 46 double dbLVal = 0.0;
47 ipNALocator->QueryLocationByPoint(ipBeginPoint, &ipNALocation1, &ipPoint1, &dbLVal);
48 ipNALocator->QueryLocationByPoint(ipEndPoint, &ipNALocation2, &ipPoint2, &dbLVal); 49
50 INALocationObjectPtr ipNALocationObject = NULL;
51 IFeatureClassPtr ipFeatureClass = ipNAIncidentsClass; 52 IFeaturePtr ipFeature = NULL;
53 ipFeatureClass->CreateFeature(&ipFeature); 54 IRowSubtypesPtr ipRowSubtypes = ipFeature; 55 ipRowSubtypes->InitDefaultValues(); 56 ipFeature->putref_Shape(ipBeginPoint); 57 ITablePtr ipTable = NULL;
58 ipFeature->get_Table(&ipTable); 59 long nIndex = 0; 60 szName = \
61 bstrName = szName.AllocSysString(); 62 ipTable->FindField(bstrName, &nIndex); 63 VARIANT var_int; 64 var_int.intVal = 1; 65 var_int.vt = VT_INT;
66 ipFeature->put_Value(nIndex, var_int); 67 szName = \
68 bstrName = szName.AllocSysString(); 69 ipTable->FindField(bstrName, &nIndex);
70 ipFeature->put_Value(nIndex, COleVariant(\71 ipNALocationObject = ipFeature;
72 ipNALocationObject->put_NALocation(ipNALocation1); 73 ipFeature->Store();
74 IFieldsPtr ipFields(CLSID_Fields); 75 hr = ipTable->get_Fields(&ipFields); 76 long nFieldCount = 0;
77 hr = ipFields->get_FieldCount(&nFieldCount); 78 for (int k = 0; k < nFieldCount; k++) 79 {
80 IFieldPtr ipField(CLSID_Field);
81 ipFields->get_Field(k, &ipField); 82 BSTR bstrFieldName;
83 ipField->get_Name(&bstrFieldName); 84 CString szFieldName = bstrFieldName; 85 } 86
87 ipFeatureClass = ipNAFacilitiesClass;
88 ipFeatureClass->CreateFeature(&ipFeature); 89 ipRowSubtypes = ipFeature;
90 ipRowSubtypes->InitDefaultValues(); 91 ipFeature->putref_Shape(ipEndPoint); 92 ipTable = NULL;
93 ipFeature->get_Table(&ipTable); 94 nIndex = 0;
95 szName = \
96 bstrName = szName.AllocSysString(); 97 ipTable->FindField(bstrName, &nIndex); 98 var_int.intVal = 2;
99 ipFeature->put_Value(nIndex, var_int); 100 szName = \
101 bstrName = szName.AllocSysString(); 102 ipTable->FindField(bstrName, &nIndex);
103 ipFeature->put_Value(nIndex, COleVariant(\104 ipNALocationObject = ipFeature;
105 ipNALocationObject->put_NALocation(ipNALocation2); 106 ipFeature->Store(); 107
108 INAClosestFacilitySolverPtr ipNACFSolver = NULL; 109 INASolverPtr ipNASolver = NULL;
110 ipNaContext->get_Solver(&ipNASolver); 111
112 IGPMessagesPtr ipGPM(CLSID_GPMessages);
113 ITrackCancelPtr ipTrackCancel(CLSID_TrackCancel); 114 VARIANT_BOOL bIsPartialSolution; 115 ipNASolver->Solve(ipNaContext, ipGPM, ipTrackCancel, &bIsPartialSolution); 116
117 szName = \
118 bstrName = szName.AllocSysString(); 119 ipNARoutesClass = NULL; 120 hr = ipNamedSet->get_ItemByName(bstrName, (IUnknown**)&ipNARoutesClass); 121
122 IFeatureClassPtr ipFeatureClassRoutes = ipNARoutesClass;
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
IFeatureCursorPtr ipFCursor = NULL;
IQueryFilterPtr ipQueryFilter(CLSID_QueryFilter); CString szQueryFilter(\
BSTR bstr_QueryFilter = szQueryFilter.AllocSysString(); ipQueryFilter->put_WhereClause(bstr_QueryFilter); VARIANT_BOOL bCycle = VARIANT_FALSE; try {
ipFeatureClassRoutes->Search(ipQueryFilter, bCycle, &ipFCursor); }
catch (CException* e) {
CString szErrorMsg;
e->GetErrorMessage(szErrorMsg.GetBuffer(MAX_PATH), MAX_PATH); szErrorMsg.ReleaseBuffer(); e->Delete();
szErrorMsg += \
OutputDebugStr(szErrorMsg); }
catch () {
OutputDebugStr(\}
IFeaturePtr ipLineFeature = NULL;
hr = ipFCursor->NextFeature(&ipLineFeature); while (ipLineFeature != NULL) {
IGeometryPtr ipGeometry = NULL; IPolylinePtr ipPolyLine = NULL;
ipLineFeature->get_Shape(&ipGeometry); esriGeometryType type;
ipGeometry->get_GeometryType(&type); if (type == esriGeometryPolyline) {
ipPolyLine = ipGeometry; AddPolyline(ipPolyLine, 4); }
hr = ipFCursor->NextFeature(&ipLineFeature); }
IActiveViewPtr ipActiveView = NULL; ipActiveView = m_map.GetActiveView(); ipActiveView->Refresh();
正在阅读:
2016年秋季万安学校六年级(4)班思品教学工作总结(徐秀青)06-11
运动会稿子03-06
电脑让我着了迷作文550字07-12
实验三十三:主动隔振和被动隔振实验03-13
尝试作文600字初中(优秀8篇)03-26
五年级作文:友情最珍贵作文500字05-05
求职简历自我介绍(优秀8篇)03-27
- 多层物业服务方案
- (审判实务)习惯法与少数民族地区民间纠纷解决问题(孙 潋)
- 人教版新课标六年级下册语文全册教案
- 词语打卡
- photoshop实习报告
- 钢结构设计原理综合测试2
- 2014年期末练习题
- 高中数学中的逆向思维解题方法探讨
- 名师原创 全国通用2014-2015学年高二寒假作业 政治(一)Word版
- 北航《建筑结构检测鉴定与加固》在线作业三
- XX县卫生监督所工程建设项目可行性研究报告
- 小学四年级观察作文经典评语
- 浅谈110KV变电站电气一次设计-程泉焱(1)
- 安全员考试题库
- 国家电网公司变电运维管理规定(试行)
- 义务教育课程标准稿征求意见提纲
- 教学秘书面试技巧
- 钢结构工程施工组织设计
- 水利工程概论论文
- 09届九年级数学第四次模拟试卷
- 路径
- ArcEngine
- 实现
- 图文
- 科文学院2010—2011学年度第二学期辅导员(班主任)考核表 - 图
- 田径集训队训练方案
- 招教心理学练习题-分章节
- 《光纤通信》习题解答
- 集团建立企业年金计划研究报告(节选)
- 采购员必备知识
- 高二语文选修传记选读练习:第三专题+课时跟踪检测(六) 罗曼+
- 现代学徒制视阈下中职林业专业教师社会化服务能力提升探索-2019
- 项目三 网络数据库技术实训 - 图文
- 冶金机械
- 包装设计心得体会3篇
- 防汛值班制度精品资料
- 第三章 账户和复式记账测试
- 一等奖教案2018-2019学年青岛版五四制小学数学五年级上册《分数
- 浅析科研院所项目绩效预算管理
- 配套K12河北中考语文总复习专题六句子排序与衔接检测
- 用经济学智慧解读中国2018尔雅答案完整版
- 软件需求规格说明书(案例)
- 从字体看性格 笔迹心理学 - 图文
- (精品)基于DEM的梯田自动提取模型研究 - 图文