以Network Dataset(网络数据集)方式实现的最短路径分析
转自原文 以Network Dataset(网络数据集)方式实现的最短路径分析
构建网络有两种方式,分别是网络数据集NetworkDataset和几何网络Geometric Network,这个网络结构数据的创建直接在Catalog中实现创建,进行最短路径分析,为了直接使用ArcGIS提供的功能,我选用的是NetworkDataset,主要记录下考虑单双行的最短路径的设计与实现(理想状态,不考虑转弯等要素)。
(A)对数据编辑的要求有一下几点:
(1) 添加属性字段,名称为Oneway,类型Text,默认值为空。
(2) 道路数据电子矢量化
(3) 对于单行线、禁行以及无限制通行方向道路属性值设置的要求:
由东往西或由北往南通行的单行路段,Oneway字段值设为:FT
由西往东或由南往北通行的单行路段,Oneway字段值设为:TF
禁止通行的路段,Oneway字段值为:N
双向通行的路段,Oneway字段值为:空字符
(B)路径分析的思路分为一下几步:
(1)读取shp文件和网络数据集数据
(2)创建网络分析上下文对象INAContext和网络分析对象INASolver(==IRouteNASolver)
(3)加载位置点图层,创建网络位置
(4)设置Solver参数(输出、容限值等)
(5)进行分析
(6)显示路线及结果信息
PS:对于道路走向不是正南正北,或者很难分辨是东西向还是南北向的,Oneway字段的赋值情况,还没有琢磨出万无一失的方法,有高人熟悉的,请指点。
(C)代码实现部分:
//初始化地图、网络数据集
private void Initial()
{
this.axMapControl1.ActiveView.Clear();
axMapControl1.ActiveView.Refresh();
pFeatureWorkspace =
OpenWorkspace(ConfigurationManager.ConnectionStrings["MdbPath"].ToString())
as IFeatureWorkspace;
pNetworkDataset = OpenNetworkDataset_Other(pFeatureWorkspace as IWorkspace, "TestNet_ND", "TestNet");
pNAContext = CreateNAContext(pNetworkDataset);
pInputFC = pFeatureWorkspace.OpenFeatureClass("stop");
pVertexFC = pFeatureWorkspace.OpenFeatureClass("TestNet_ND_Junctions");
IFeatureLayer pVertexFL = new FeatureLayerClass();
pVertexFL.FeatureClass = pFeatureWorkspace.OpenFeatureClass("TestNet_ND_Junctions");
pVertexFL.Name = pVertexFL.FeatureClass.AliasName;
axMapControl1.AddLayer(pVertexFL, 0);
IFeatureLayer pRoadFL = new FeatureLayerClass();
pRoadFL.FeatureClass = pFeatureWorkspace.OpenFeatureClass("道路中心线");
pRoadFL.Name = pRoadFL.FeatureClass.AliasName;
axMapControl1.AddLayer(pRoadFL,0);
ILayer pLayer;
INetworkLayer pNetworkLayer = new NetworkLayerClass();
pNetworkLayer.NetworkDataset = pNetworkDataset;
pLayer = pNetworkLayer as ILayer;
pLayer.Name = "Network Dataset";
axMapControl1.AddLayer(pLayer, 0);
//Create a Network Analysis Layer and add to ArcMap
INALayer naLayer = pNAContext.Solver.CreateLayer(pNAContext);
pLayer = naLayer as ILayer;
pLayer.Name = pNAContext.Solver.DisplayName;
axMapControl1.AddLayer(pLayer, 0);
pActiveView = axMapControl1.ActiveView;
pMap = pActiveView.FocusMap;
pGraphicsContainer = pMap as IGraphicsContainer;
}
//打开工作空间
private IWorkspace OpenWorkspace(string strMDBName)
{
IWorkspaceFactory pWorkspaceFactory = new AccessWorkspaceFactoryClass();
return pWorkspaceFactory.OpenFromFile(strMDBName, 0);
}
//打开网络数据集
private INetworkDataset OpenNetworkDataset(IWorkspace workspace,string strNDName)
{
IWorkspaceExtensionManager pWorkspaceExtensionManager;
IWorkspaceExtension pWorkspaceExtension;
IDatasetContainer2 pDatasetContainer2;
pWorkspaceExtensionManager = workspace as IWorkspaceExtensionManager;
int iCount = pWorkspaceExtensionManager.ExtensionCount;
for (int i = 0; i < iCount; i++)
{
pWorkspaceExtension = pWorkspaceExtensionManager.get_Extension(i);
if(pWorkspaceExtension.Name.Equals("Network Dataset"))
{
pDatasetContainer2=pWorkspaceExtension as IDatasetContainer2;
return
pDatasetContainer2.get_DatasetByName(esriDatasetType.esriDTNetworkDataset,
strNDName) as INetworkDataset;
}
}
return null;
}
private INetworkDataset OpenNetworkDataset_Other(IWorkspace workspace, string strNDName,string strRoadFeatureDataset)
{
IDatasetContainer3 pDatasetContainer3;
IFeatureWorkspace pFeatureWorkspace = workspace as IFeatureWorkspace;
pFeatureDataset = pFeatureWorkspace.OpenFeatureDataset(strRoadFeatureDataset);
IFeatureDatasetExtensionContainer pFeatureDatasetExtensionContainer =
pFeatureDataset as IFeatureDatasetExtensionContainer;
IFeatureDatasetExtension pFeatureDatasetExtension =
pFeatureDatasetExtensionContainer.FindExtension(esriDatasetType.esriDTNetworkDataset);
pDatasetContainer3 = pFeatureDatasetExtension as IDatasetContainer3;
if (pDatasetContainer3 == null)
return null;
IDataset pDataset = pDatasetContainer3.get_DatasetByName(esriDatasetType.esriDTNetworkDataset, strNDName);
return pDataset as INetworkDataset;
}
//创建网络分析上下文
private INAContext CreateNAContext(INetworkDataset networkDataset)
{
IDENetworkDataset pDENetworkDataset = GetDENetworkDataset(networkDataset);
INASolver pNASolver = new NARouteSolverClass();
INAContextEdit pNAContextEdit = pNASolver.CreateContext(pDENetworkDataset, pNASolver.Name) as INAContextEdit;
pNAContextEdit.Bind(networkDataset,new GPMessagesClass());
return pNAContextEdit as INAContext;
}
//根据点图层确定最短路径所用经历的点
private void LoadNANetWorkLocations(string strNAClassName, IFeatureClass inputFC, double dSnapTolerance)
{
INAClass pNAClass;
INamedSet pNamedSet;
pNamedSet = pNAContext.NAClasses;
pNAClass = pNamedSet.get_ItemByName(strNAClassName) as INAClass;
//删除已存在的位置点
pNAClass.DeleteAllRows();
//创建NAClassLoader,设置捕捉容限值
INAClassLoader pNAClassLoader = new NAClassLoaderClass();
pNAClassLoader.Locator = pNAContext.Locator;
if (dSnapTolerance > 0)
pNAClassLoader.Locator.SnapTolerance = dSnapTolerance;
pNAClassLoader.NAClass = pNAClass;
//字段匹配
INAClassFieldMap pNAClassFieldMap = new NAClassFieldMapClass();
pNAClassFieldMap.CreateMapping(pNAClass.ClassDefinition, inputFC.Fields);
pNAClassLoader.FieldMap = pNAClassFieldMap;
//pNAClassFieldMap.set_MappedField("OBJECTID", "OBJECTID");
//pNAClassLoader.FieldMap = pNAClassFieldMap;
//加载网络位置点数据
int iRows=0;
int iRowsLocated=0;
IFeatureCursor pFeatureCursor = pInputFC.Search(null, true);
pNAClassLoader.Load((ICursor)pFeatureCursor, null, ref iRows, ref iRowsLocated);
((INAContextEdit)pNAContext).ContextChanged();
}
private void SetSolverSettings()
{
//Set Route specific Settings
INASolver naSolver = pNAContext.Solver;
INARouteSolver cfSolver = naSolver as INARouteSolver;
cfSolver.OutputLines = esriNAOutputLineType.esriNAOutputLineTrueShapeWithMeasure;
// Set generic solver settings
// Set the impedance attribute
INASolverSettings naSolverSettings;
naSolverSettings = naSolver as INASolverSettings;
// Set the On
eWay Restriction if necessary
IStringArray restrictions;
restrictions = naSolverSettings.RestrictionAttributeNames;
restrictions.RemoveAll();
restrictions.Add("oneway");
naSolverSettings.RestrictionAttributeNames = restrictions;
////Restrict UTurns
//naSolverSettings.RestrictUTurns = esriNetworkForwardStarBacktrack.esriNFSBNoBacktrack;
//naSolverSettings.IgnoreInvalidLocations = true;
// Do not forget to update the context after you set your impedance
naSolver.UpdateContext(pNAContext, GetDENetworkDataset(pNAContext.NetworkDataset), new GPMessagesClass());
}
//路径分析
private void btnSolver_Click(object sender, EventArgs e)
{
this.Cursor = Cursors.WaitCursor;
lstOutput.Items.Clear();
lstOutput.Items.Add("分析中...");
LoadNANetWorkLocations("Stops", pInputFC, 80);
IGPMessages gpMessages = new GPMessagesClass();
INASolver naSolver = pNAContext.Solver;
SetSolverSettings();
pNAContext.Solver.Solve(pNAContext, gpMessages, new CancelTrackerClass());
if (gpMessages != null)
{
for (int i = 0; i < gpMessages.Count; i++)
{
switch (gpMessages.GetMessage(i).Type)
{
case esriGPMessageType.esriGPMessageTypeError:
lstOutput.Items.Add("错误 " +
gpMessages.GetMessage(i).ErrorCode.ToString() + " " +
gpMessages.GetMessage(i).Description);
break;
case esriGPMessageType.esriGPMessageTypeWarning:
lstOutput.Items.Add("警告 " + gpMessages.GetMessage(i).Description);
break;
default:
lstOutput.Items.Add("信息 " + gpMessages.GetMessage(i).Description);
break;
}
}
}
axMapControl1.Refresh();
lstOutput.Items.Add("Successful");
this.Cursor = Cursors.Default;
}
(D)上下效果图
以Network Dataset(网络数据集)方式实现的最短路径分析的更多相关文章
- ArcGIS 网络分析[1.2] 利用1.1的线shp创建网络数据集/并简单试验最佳路径
上篇已经创建好了线数据(shp文件格式)链接:点我 这篇将基于此shp线数据创建网络数据集. 在此说明:shp数据的网络数据集仅支持单一线数据,也就是说基于shp文件的网络数据集,只能有一个shp线文 ...
- Arcengine实现创建网络数据集札记(三)
后记 下面给出项目中用到的自定义的封装类. AE许可初始化封装类: public class AELicenseChecker { private static volatile AELicenseC ...
- 【AO笔记】有关使用AO基于shp文件创建网络数据集的一个细节
前些日子,和群友交流时提及shp文件创建单一网络数据集的创建问题. 由于项目需求,用shp文件创建网络合适,所以不得不去找一些古老的代码资料... 以前的研究中,创建网络数据集是根据IDatasetC ...
- 第五课第四周笔记4:Transformer Network变压器网络
Transformer Network变压器网络 你已经了解了 self attention,你已经了解了 multi headed attention.在这个视频中,让我们把它们放在一起来构建一个变 ...
- VirtualBox的四种网络连接方式
VirtualBox中有4中网络连接方式:a. NAT 网络地址转换模式(Network Address Translation)b. Bridged ...
- Arcengine实现创建网络数据集札记(二)
四 ArcEngine实现创建网络数据集 ArcEngine创建网络数据集的过程,与ArcMap设置的过程类似,主要通过六个步骤即可以实现. 1 定义网络数据集对象,并设置基本属性,包括网络数据集名称 ...
- Arcengine实现创建网络数据集札记(一)
一 引子 网络数据集,GIS空间分析基础的理论和知识,是最短路径分析.连通性分析等其他空间分析技术的数据基础. 以往,网络数据集的研究很少,此次项目开发过程中,对网络数据集以及arcengine创建网 ...
- VirtualBox的四种网络连接方式详解
VirtualBox中有4中网络连接方式: 1. NAT 2. Bridged Adapter 3. Internal 4. Host-only Adapter VMWare中有三种,其实他跟VMWa ...
- 我用过的Linux命令--虚拟机和宿主机的网络连接方式
VMWare提供了三种工作模式,它们是bridged(bridged模式:对应网卡vment0).NAT(网络地址转换模式:对应网卡vment8)和host-only(主机模式:对应网卡vment1) ...
随机推荐
- itchat转发指定的微信群里某个用户的发言到指定的群
复读机功能, 如果有比较多的用户,超出500人,那就得分开至少两个群,如何把一些消息自动复制到另一个群呢. 自动转发指定用户的发言,转发到别的群 # !/usr/bin/env python # -* ...
- CISP/CISA 每日一题 21
CISSP 每日一题(答)What is the term that identifies data ona disk after the data has supposedly been erase ...
- 《WPF》Expander控件简单美化
示例图: Expander控件功能很常见, 一般用于系统左侧的菜单收缩面板. 1.主要的组成 一个头部(header) 和 一个 内容(content) 组成. <Expander Expand ...
- oracle数据库spfile
http://blog.chinaunix.net/uid-8996530-id-3195808.html http://www.cnblogs.com/HondaHsu/p/4885318.html ...
- [D3JS] Add more map layer and color
import React, {Component} from 'react'; import * as d3 from 'd3'; import 'd3-geo'; import * as topoj ...
- js原生代码实现鼠标拖拽(超简单)
首先先来看这一张图 在这种图中,盒子的大小为512px,并且margin-left:-250px margin-top:140px;并通过一些样式让其在中部显示 这些样式都不是重要的,这里加个marg ...
- 使用node.js+babel,支持import/export语法
如果要在node里面支持import/export default语法步骤: 1.使用npm安装 babel的客户端工具 npm init 会生成package.json文件 2.接着安装bebel客 ...
- JAVA 水题
纯粹是让我来掌握熟练度的. 1.金蝉素数 某古寺的一块石碑上依稀刻有一些神秘的自然数. 专家研究发现:这些数是由1,3,5,7,9 这5 个奇数字排列组成的5 位素数,且同时去掉它的最高位与最低位数字 ...
- 【AtCoder Regular Contest 082】Derangement
[链接]点击打开链接 [题意] 在这里写题意 [题解] 贪心. 连续一块的p[i]==i的话,对答案的贡献就应该为(这个连续块的长度+1)/2; 长度为1的也正确. (也即两两相邻的互换位置.) [错 ...
- Android Set与List之间转化
List list = new ArrayList(set); Set set = new HashSet(list); //但是有一点,转换当中可能要丢失数据,尤其是从list转换到set的时候,因 ...