ArcGIS二次开发基础教程(11):网络分析之最短路径分析「建议收藏」

ArcGIS二次开发基础教程(11):网络分析之最短路径分析「建议收藏」ArcGIS二次开发基础教程(11):网络分析之最短路径分析最短路径分析这里直接调用了在mdb中建立好的网络数据集//全局变量privateINetworkDatasetmy_networkDataset;//网络数据集privateINAContextmy_NAContexts;//网络分析上下文privateIFeatureClassmy_InputFeatureCla…

大家好,又见面了,我是你们的朋友全栈君。

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应用技能大赛开发题答案点这里,尚在不定期更新中

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/163416.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

  • DataLoader详解

    DataLoader详解对数据集处理虽说很方便但在参数选取和其他的细节方面还容易出问题,尤其是最后一个Batch长度不足,会导致输出维度发生问题,若直接舍去,我还想要全部的数据结果使用方法①创建一个Dataset对象②创建一个DataLoader对象③循环这个DataLoader对象,将xx,xx加载到模型中进行训练train_loader=DataLoader(dataset,batch_size=batch_size,shuffle=shuffle,collate_fn=pad

  • 操作系统 进程调度算法_进程调度算法实验报告

    操作系统 进程调度算法_进程调度算法实验报告进程调度算法一、先来先服务(FCFS)基本思想:先到达的进程先进入就绪队列,先进行调度的原则。非抢占方式。二、短作业优先(SJF)基本思想:根据进程中的执行时间,选取执行时间最短的作业优先调度;可有抢占或非抢占方式。三、优先权高者优先(HPF)基本思想:系统根据作业的优先权进行作业调度,每次选取优先权高的作业优先调度。作业的优先权通常用一个整数表示,也叫做优先数。可有抢占或非抢占方式。…

  • NPN PNP开关电路[通俗易懂]

    NPN PNP开关电路[通俗易懂]   在设计电路板时需要用3.3v开关5v电源,所以这时候用到了NPN-PNP开关电路。我最开始的设计是这样的(npn采用的是9013,pnp采用的是8550):   开始没发现问题。真正调试的时候发现NPNQ1管烫的非常厉害。后才才发现原因:应该在QI集电极和Q2基极之间加上一个电阻。这是因为当向图中这样连接时VCC通过Q2直接加在了Q1的ce两端,Q2的be电压很小,相当于有一大…

  • phpstorm2021listen激活破解方法

    phpstorm2021listen激活破解方法,https://javaforall.cn/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

  • 集群和负载均衡_分布式负载均衡

    集群和负载均衡_分布式负载均衡基于集群的动态反馈负载均衡策略基于动态反馈机制的集群负载均衡算法研究目前应用最为广泛的集群计算技术主要分为三大类:高可用性集群技术、高性能计算集群技术和负载均衡集群技术。德国的CarlAdamPetri于1962年在他的博士论文《自动机通信》中提出了Petri网的概念,它是一种适合于描述异步、并发、分布式系统的图形数学工具。动态WRR调度算法…

    2022年10月13日
  • Linux系统怎么修改用户名_怎么更改微软用户名

    Linux系统怎么修改用户名_怎么更改微软用户名修改用户名(以CentOS-8为例)①进入root用户进行操作,在/etc/sudoers添加admin(新用户名)ALL=(ALL)ALLsurootvim/etc/sudoers②进入/etc/passwd将旧用户名改为admin(新用户名)vim/etc/passwd③进入/etc/group将旧用户名改为admin(新用户名)vim/etc/group④进入/etc/shadow将旧用户名改为admin(新用户名)

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号