使用Arcgis做路径规划(使用python脚本调用)

使用Arcgis做路径规划(使用python脚本调用)场景 如何在全部的铁路线路数据中找到点到点的路径 并且点与点之前的路线可能跨越其它点 也就是路径规划的问题 须知 argis 计算机制图 应用 包含了全球范围内的底图 地图数据 应用程序 以及可配置的应用模板和开发人员使用的 GIS 工具和 API 可用于创建 Web 地图 发布 GIS 服务 共享地图 数据和应用程序

大家好,我是讯享网,很高兴认识大家。

场景

如何在全部的铁路线路数据中找到点到点的路径,并且点与点之前的路线可能跨越其它点-也就是路径规划的问题。

须知

argis: “计算机制图”应用,包含了全球范围内的底图、地图数据、应用程序,以及可配置的应用模板和开发人员使用的 GIS 工具和 API,可用于创建 Web 地图、发布GIS服务、共享地图、数据和应用程序

路径规划:根据起始出发点和到达点(可设置途经点和障碍点),在已确定的路网数据中找到最优路线(最短路程,最低时间)

工具

argis10.2

python2(建议使用arcgis自带python)

数据

以铁路数据为例:

路网数据(全国铁路线路shp文件)

列车行驶中所有的点到点的列表数据(shp文件形式,可用cvs转成shp)

步骤

argis配置

配置允许进行网络数据集操作

点击菜单customize->extensions,勾选network analyst

在这里插入图片描述
讯享网

点击菜单customize->toolbar,勾选network analyst

在这里插入图片描述

数据导入

  1. 打开ArcCatalog,找到文件树后右键新建“File Geodatabase”,随后在新建的gdb上右键新建“feature dataset ”,之后在dataset上右键,选择“Import”–>“Feature Class”导入你的路网数据

    在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

结果如下:

在这里插入图片描述

2.线处理:进行线要素的处理

相交线打断。在arcToolBox的data management tools —>features—>featuretoline。
在点要素处打断线。在arcToolBox的data management tools —>features—>split line at point。

  1. 构建拓扑结构:在数据集dataset上右键,New —> Network Dataset…

    在这里插入图片描述

结果如下:

在这里插入图片描述

  1. 新建一次分析:network analyst —> New Route

    在这里插入图片描述

结果如下,左侧为本次分析的数据集界面:

在这里插入图片描述

  1. 增加途经点:Stops 右键 -> Load Locations…

在这里插入图片描述

选择途经点图层导入途经点

结果如下:

在这里插入图片描述

  1. 分析路径:
    点击 solve

在这里插入图片描述

在这里插入图片描述

左侧下的Routes就是我们需要的数据

python执行准备

  1. 脚本:以下代码
  2. 前面配置的网络数据集
  3. 火车站数据以出发点,结束点两个点为一组,全部路线数据(shp),如下图所示,相同的sort为一组:
    在这里插入图片描述
  4. 执行脚本

PS: 数据量多时,可同时执行多个脚本,但是执行的参数需要错开,并且网络数据集需要每个脚本一个,避免出现操作文件是出现冲突,导致异常。

#调用argis接口 根据点查找路径,并将路径保存到shp文件中 import arcpy from arcpy import env try: #Check out the Network Analyst extension license arcpy.CheckOutExtension("Network") #Set environment settings 配置argis的当前基础数据的数据文件地址 env.workspace = "C:/Documents/ArcGIS/routeana0.gdb" env.overwriteOutput = True #Set local variables 配置好的网络数据集(NetworkDataset)位置 即基础数据 inNetworkDataset = "train/train_ND" #输出的经过点的图层名称 outNALayerName = "StationRoute" impedanceAttribute = "Length" inAddressLocator = "train_station_copy" #所有经过点的图层位置 allinFeatures = "s2sdis" #输入经过点数据表位置 inAddressTable = "G:/ArcGIS/python/StopAddresses.csv" #输入经过点数据表 字段 inAddressFields = "Name Name VISIBLE NONE" #输出经过点图层名称 outStops = "GeocodedStops" #输出经过点图层保存地址 outLayerFile = "G:/ArcGIS/python" + "/" + outNALayerName + ".lyr" searchTolerance = "3000 Meters" #Create a new Route layer. For this scenario, the default value for all the #remaining parameters statisfies the analysis requirements outNALayer = arcpy.na.MakeRouteLayer(inNetworkDataset, outNALayerName, impedanceAttribute) #Get the layer object from the result object. The route layer can now be #referenced using the layer object. 得到输出图层集 outNALayer = outNALayer.getOutput(0) #Get the names of all the sublayers within the route layer. #得到输出图层名称 subLayerNames = arcpy.na.GetNAClassNames(outNALayer) #Stores the layer names that we will use later 根据名称获取对应图层 stopsLayerName = subLayerNames["Stops"] routesLayerName = subLayerNames["Routes"] #需要分析多次 筛选多批次的经过点 数据量大时可分批执行 for i in range(int(start),int(end)) : 筛选当前批次的经过点到临时图层station2station0 inFeatures = arcpy.Select_analysis(allinFeatures, "station2station0", '"sort" = '+str(i)) #Load the geocoded address locations as stops mapping the address field from #geocoded stop features as Name property using field mappings. #网络数据集中清除原来的点,并加入输出经过点图层中的点 arcpy.na.AddLocations(outNALayer, stopsLayerName, inFeatures, "Name Name #", searchTolerance, append = "CLEAR",exclude_restricted_elements = "EXCLUDE") #Solve the route layer, ignore any invalid locations such as those that #can not be geocoded #执行查找路径方法 可能存在找不到路径的情况 捕捉异常,不打断循环 try: result = arcpy.na.Solve(outNALayer,"SKIP") except Exception as e: # If an error occurred, print line number and error message import traceback, sys tb = sys.exc_info()[2] print "An error occured on line %i" % tb.tb_lineno # 删除临时图层,避免下次创建图层时出错 arcpy.Delete_management("station2station0") print str(e) print str(i) continue # 删除临时图层,避免下次创建图层时出错 arcpy.Delete_management("station2station0") #Get the polygons sublayer from the service area layer #获取结果中路径的图层 routes_sublayer = arcpy.mapping.ListLayers(outNALayer, routesLayerName)[0] #将获得的路径汇总到时s2s的shp中 arcpy.Append_management([routes_sublayer], "s2s", "TEST") print("Script completed successfully") except Exception as e: # If an error occurred, print line number and error message import traceback, sys tb = sys.exc_info()[2] print ("An error occured on line %i" % tb.tb_lineno) print (str(e)) 

讯享网
小讯
上一篇 2025-02-21 19:03
下一篇 2025-03-23 21:50

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/44230.html