f*****c 发帖数: 3257 | 1 我有美国road shape file和全美500个城市的longtitude and latitude。
现在需要计算每对城市之间的road distance.
我从网上找的办法是用python写code,可是总是出错。
各位大神帮忙看一下 。由于没有时间变量,所以里面关于时间的我都给省去了
import arcpy, os, sys
arcpy.env.overwriteOutput = True
try:
# costDic1 = {}
f = open(r"C:UsersUserDocumentsArcGISindia.txt", 'r+')
layer1 = "Route" #an open in-memory layer
for line in f:
twoPs = line.split(",")
# if the line contains number
if twoPs[0].isdigit():
OPoint = arcpy.Point()
DPoint = arcpy.Point()
pointGeometryList = []
OPoint.X = twoPs[1]
OPoint.Y = twoPs[2]
DPoint.X = twoPs[3]
DPoint.Y = twoPs[4]
pointGeometry = arcpy.PointGeometry(OPoint)
pointGeometryList.append(pointGeometry)
pointGeometry = arcpy.PointGeometry(DPoint)
pointGeometryList.append(pointGeometry)
myPoints = "Mypoints"
arcpy.CopyFeatures_management(pointGeometryList, myPoints)
arcpy.na.AddLocations(layer1,"Stops",myPoints)
arcpy.Solve_na(layer1)
# get the cost info from the layer
# rows = arcpy.SearchCursor(lyrName + "\Routes")
# for row in rows:
# costDic1[twoPs[0]] = row.Total_TravelTime
# delete stops and route from the layer
arcpy.DeleteRows_management(lyrName + "\Stops")
arcpy.DeleteRows_management(lyrName + "\Routes")
f.close
# Output the result to a txt file
fOut = open(r"C:UsersUserDocumentsArcGISOut.txt", 'w')
# for costKey in costDic1.keys():
# line = str(costKey) + "," + str(costDic1[costKey])
# fOut.writelines(line)
fOut.close
except:
print "NO RESULTS" |
|