大家好,又见面了,我是你们的朋友全栈君。
ArcGIS二次开发基础教程(11):网络分析之最短路径分析
最短路径分析
这里直接调用了在mdb中建立好的网络数据集
//全局变量
private INetworkDataset my_networkDataset;//网络数据集
private INAContext my_NAContexts;//网络分析上下文
private IFeatureClass my_InputFeatureClass;//存储输入点要素类
private IActiveView my_ActiveView;
private IGraphicsContainer my_GraphicsContainer;
bool NetworkAnalysis = false;//分析准备
int count = 0;//节点数目
//网络分析初始化
private void initNetworkAnalysis()
{
openFileDialog1.Title = "打开网络数据集数据库";
openFileDialog1.Filter = "Personal GeoDatabase(*.mdb)|*.mdb";
openFileDialog1.Multiselect = false;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
//打开工作空间
IFeatureWorkspace featureWorkspace = my_OpenWorkspace(openFileDialog1.FileName);
//打开网络数据集
my_networkDataset = my_OpenNetworkDataset(featureWorkspace as IWorkspace, "NetDataset_ND", "NetDataset");
//创建网络分析上下文
my_NAContexts = my_CreateSolverContext(my_networkDataset);
//获取输入点要素类
my_InputFeatureClass = featureWorkspace.OpenFeatureClass("plan");
//添加road图层
IFeatureLayer layer = new FeatureLayerClass();
IFeatureClass myClass = featureWorkspace.OpenFeatureClass("road");
layer.FeatureClass = myClass;
layer.Name = myClass.AliasName;
axMapControl1.AddLayer(layer);
//添加NetDatset_ND_Junctions
myClass = featureWorkspace.OpenFeatureClass("NetDataset_ND_Junctions");
layer.FeatureClass = myClass;
layer.Name = myClass.AliasName;
axMapControl1.AddLayer(layer);
//添加网络数据集图层
INetworkLayer netLayer = new NetworkLayerClass();
netLayer.NetworkDataset = my_networkDataset;
ILayer my_layer = netLayer as ILayer;
my_layer.Name = "Network Dataset";
axMapControl1.AddLayer(my_layer);
//添加网络分析图层
INALayer NALayer = my_NAContexts.Solver.CreateLayer(my_NAContexts);
my_layer = NALayer as ILayer;
my_layer.Name = my_NAContexts.Solver.DisplayName;
axMapControl1.AddLayer(my_layer);
my_ActiveView = axMapControl1.ActiveView;
my_GraphicsContainer = axMapControl1.ActiveView.FocusMap as IGraphicsContainer;
}
}
//打开工作空间
private IFeatureWorkspace my_OpenWorkspace(string strMDBName)
{
IWorkspaceFactory workspaceFactory = new AccessWorkspaceFactoryClass();
IWorkspace workspace = workspaceFactory.OpenFromFile(strMDBName, 0);
return workspace as IFeatureWorkspace;
}
//打开网络数据集
private INetworkDataset my_OpenNetworkDataset(IWorkspace networkDatasetWorkspace, String networkDatasetName,string featureDatasetName)
{
if (networkDatasetWorkspace == null || networkDatasetName == "")
{
return null;
}
IDatasetContainer3 datasetContainer3 = null;
// Geodatabase network dataset workspace
ESRI.ArcGIS.Geodatabase.IFeatureWorkspace featureWorkspace = networkDatasetWorkspace as ESRI.ArcGIS.Geodatabase.IFeatureWorkspace; // Dynamic Cast
ESRI.ArcGIS.Geodatabase.IFeatureDataset featureDataset = featureWorkspace.OpenFeatureDataset(featureDatasetName);
ESRI.ArcGIS.Geodatabase.IFeatureDatasetExtensionContainer featureDatasetExtensionContainer = featureDataset as ESRI.ArcGIS.Geodatabase.IFeatureDatasetExtensionContainer; // Dynamic Cast
ESRI.ArcGIS.Geodatabase.IFeatureDatasetExtension featureDatasetExtension = featureDatasetExtensionContainer.FindExtension(ESRI.ArcGIS.Geodatabase.esriDatasetType.esriDTNetworkDataset);
datasetContainer3 = featureDatasetExtension as ESRI.ArcGIS.Geodatabase.IDatasetContainer3; // Dynamic Cast
if (datasetContainer3 == null)
return null;
ESRI.ArcGIS.Geodatabase.IDataset dataset = datasetContainer3.get_DatasetByName(ESRI.ArcGIS.Geodatabase.esriDatasetType.esriDTNetworkDataset, networkDatasetName);
return dataset as ESRI.ArcGIS.Geodatabase.INetworkDataset; // Dynamic Cast
}
//创建网络分析上下文
private INAContext my_CreateSolverContext(INetworkDataset networkDataset)
{
IDatasetComponent datasetComponent = networkDataset as IDatasetComponent;
IDENetworkDataset deNetworkDataset = datasetComponent.DataElement as IDENetworkDataset;
INASolver naSolver = new NARouteSolver();
INAContextEdit naContextEdit = naSolver.CreateContext(deNetworkDataset, naSolver.Name) as INAContextEdit;
naContextEdit.Bind(networkDataset, new GPMessagesClass());
return naContextEdit as INAContext;
}
private void 网络分析准备()
{
NetworkAnalysis = true;
//清除输入点要素
ITable table = my_InputFeatureClass as ITable;
table.DeleteSearchedRows(null);
//清除规划路径
table = my_NAContexts.NAClasses.get_ItemByName("Routes") as ITable;
table.DeleteSearchedRows(null);
//清除Stops
INAClass naClass = my_NAContexts.NAClasses.get_ItemByName("Stops") as INAClass;
table = naClass as ITable;
table.DeleteSearchedRows(null);
//清除Barriers
naClass = my_NAContexts.NAClasses.get_ItemByName("Barriers") as INAClass;
table = naClass as ITable;
table.DeleteSearchedRows(null);
my_GraphicsContainer.DeleteAllElements();
count = 0;
my_ActiveView.Refresh();
MessageBox.Show("请选择规划点");
}
private void 添加点(object sender,IMapControlEvents2_OnMouseDownEvent e)
{
if (NetworkAnalysis == true)
{
IPoint pt;
pt = my_ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(e.x, e.y);
IFeature feature = my_InputFeatureClass.CreateFeature();
feature.Shape = pt;
feature.Store();
count++;
ITextElement textElement = new TextElementClass();
textElement.Text = count.ToString();
textElement.Symbol = new TextSymbol();
IElement ele = textElement as IElement;
ele.Geometry = pt;
my_GraphicsContainer.AddElement(ele, 0);
my_ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
}
}
//加载站点
private void LoadNetworkLocation(int snapTolerance)
{
//清除项
INAClass naClass = my_NAContexts.NAClasses.get_ItemByName("Stops") as INAClass;
naClass.DeleteAllRows();
//加载网络分析对象,设置容差值
INAClassLoader naClassLoader = new NAClassLoaderClass();
naClassLoader.Locator = my_NAContexts.Locator;
if (snapTolerance > 0)
naClassLoader.Locator.SnapTolerance = snapTolerance;
naClassLoader.NAClass = naClass;
//加载网络分析类
int rowsIn = 0;
int rowsLocated = 0;
IFeatureCursor cursor = my_InputFeatureClass.Search(null, true);
naClassLoader.Load((ICursor)cursor, null, ref rowsIn, ref rowsLocated);
((INAContextEdit)my_NAContexts).ContextChanged();
}
//最短路径分析
private void 实施分析()
{
IGPMessages messages = new GPMessagesClass();
LoadNetworkLocation(80);
INASolver naSolver = my_NAContexts.Solver;
naSolver.Solve(my_NAContexts, messages, null);
//实施后删除输入点
ITable table = my_InputFeatureClass as ITable;
table.DeleteSearchedRows(null);
my_ActiveView.Refresh();
}
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/163416.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...