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)


相关推荐

  • Random函数用法

    Random函数用法一、random模块简介Python标准库中的random函数,可以生成随机浮点数、整数、字符串,甚至帮助你随机选择列表序列中的一个元素,打乱一组数据等。二、random模块重要函数1)、random()返回0<=n<1之间的随机实数n;2)、choice(seq)从序列seq中返回随机的元素;importrandoma=rand…

  • MySQL清空表数据

    MySQL清空表数据清空表数据一共有三种方式1、truncate(速度很快)自增字段清空从1开始全表清空首选2、drop直接删表…啥都没了啥都没了…………3、delete速度慢的一批自增字段不清空MySQL清空表数据命令:truncate说明:删除内容、释放空间但不删除定义,也就是数据表的结构还在。与drop不同的是,它只是清空表数据而已,它比较温柔。truncatetable表名注意:不能与where一起使用。truncate删除数据后是不可以rollback

  • AD域安装

    AD域安装https://www.cnblogs.com/BugBrother/p/6760504.html

  • DirectSound的应用

    DirectSound的应用

    2021年11月14日
  • menuconfig 配置选项详解

    menuconfig 配置选项详解转自:http://www.blog.chinaunix.net/uid-15887868-id-2758315.html在menuconfig中配置:详细介绍内核配置选项及删改情况第一部分:全部删除Codematurityleveloptions—&gt;代码成熟等级选项[]Promptfordevelopmentand/orincompletecode/drivers…

  • c++ bool类型函数[通俗易懂]

    c++ bool类型函数[通俗易懂]bool型函数指的是返回值为bool类型的函数,其调用方式和int型函数没有太大的区别。bool型变量的值只有真(true)和假(false)。bool可用于定义函数类型为布尔型,函数里可以有returntrue;returnfalse之类的语句。示例:#includeusing namespace std;bool cmp(int a,int b){    

发表回复

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

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