arcpy 最短路径

# encoding: utf-8
import arcpy
from arcpy import env
try:
    # Check out the Network Analyst extension license
    arcpy.CheckOutExtension("Network")
    # Set environment settings
    env.workspace = "d:/OD2.gdb"
    env.overwriteOutput = True
    # Set local variables
    inNetworkDataset =r"d:\dl\osm\km_ND.nd"
    outNALayerName = "BestRoute"
    impedanceAttribute = "Length"
    fcs = arcpy.ListFeatureClasses()
    for outStops in fcs:
        try:
            outNALayer = arcpy.na.MakeRouteLayer(inNetworkDataset, outNALayerName,impedanceAttribute)
            outNALayer = outNALayer.getOutput(0)
            subLayerNames = arcpy.na.GetNAClassNames(outNALayer)
            stopsLayerName = subLayerNames["Stops"]
            arcpy.na.AddLocations(outNALayer, stopsLayerName, outStops, "", "5000 Meters",)
            arcpy.na.Solve(outNALayer, "SKIP")
            routelayer = arcpy.mapping.ListLayers(outNALayer)
            arcpy.CopyFeatures_management(routelayer[3], r"d:\short2.gdb"+"\\"+"result"+outStops)
        except Exception as e:
            print outStops
            print "cw"
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)

这里面存在最短路径解算不出来的情况(arcmap中野解算不出来),可能原因是拓扑错误,代码顺序基本对应arcmap操作,只是代码只找到了先生成OD点shp的方式,arcmap可以直接添加点。

 

转载自:https://blog.csdn.net/A873054267/article/details/87798506

You may also like...