如何在全部的铁路线路数据中找到点到点的路径,并且点与点之前的路线可能跨越其它点-也就是路径规划的问题。
argis: “计算机制图”应用,包含了全球范围内的底图、地图数据、应用程序,以及可配置的应用模板和开发人员使用的 GIS 工具和 API,可用于创建 Web 地图、发布GIS服务、共享地图、数据和应用程序
路径规划:根据起始出发点和到达点(可设置途经点和障碍点),在已确定的路网数据中找到最优路线(最短路程,最低时间)
argis10.2
python2(建议使用arcgis自带python)
以铁路数据为例:
路网数据(全国铁路线路shp文件)
列车行驶中所有的点到点的列表数据(shp文件形式,可用cvs转成shp)
配置好argis,在argis客户端计算出一次路径分析,保证argis可执行
点击菜单customize->extensions,勾选network analyst
点击菜单customize->toolbar,勾选network analyst
创建dataset时需要选择对应的投影和地理坐标系
2.线处理:进行线要素的处理
相交线打断。在arcToolBox的data management tools —>features—>featuretoline。
在点要素处打断线。在arcToolBox的data management tools —>features—>split line at point。
结果如下:
左侧下的Routes就是我们需要的数据
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))