ArcGIS二次开发基础教程(11):网络分析之最短路径分析

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();
}

历届GIS应用技能大赛开发题答案点这里,尚在不定期更新中

  • 12
    点赞
  • 74
    收藏
    觉得还不错? 一键收藏
  • 13
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值