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可以直接添加点。

 

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值