我从gis.stackexchange网站。其目的是将起点和终点的列表转换为一个gis形状文件,其中包含以这些相同端点为起点和终点的线。我以前没有用过python,所以请耐心等待。在
脚本如下:
import arcpy
outPath = r"S:\GIS data\Matthew Damage areas" #output path for the feature class
featureClass = "roadSegments.shp" #the name of your shapefile
spatialRef = arcpy.SpatialReference("WGS 1984") #the spatial reference you want; set it to whatever you want
arcpy.CreateFeatureclass_management(outPath, featureClass, "POLYLINE", " ", "DISABLED", "DISABLED", spatialRef) #sets up the shapefile we're using
arcpy.AddField_management(outPath + "\\" + featureClass, "segmentID", "SHORT", 15) #I assume that each segment has some sort of ID number that you might want to import into
roadArray = arcpy.Array() #this will hold the coordinates for each line segment until we write it to the shapefile.
roadFile = open(r"S:\GIS data\Matthew Damage areas\roadlines.csv") #the path to your road lines file
headerLine = roadFile.readline()
indexList = headerLine.split(",")
#now we need the index locations for the columns that contain the latitude and longitude points for the starting and ending points for each line segment. I'm also assuming that each segment has some sort of ID value in your segment. Change the value inside the parenthesis to reflect whatever you named your column for those values.
idValueIndex = indexList.index('segmentID')
fromLatValueIndex = indexList.index('fromLat')
fromLongValueIndex = indexList.index('fromLong')
toLatValueIndex = indexList.index('toLat')
toLongValueIndex = indexList.index('toLong')
roadCursor = arcpy.InsertCursor(outPath + "\\" + featureClass)
for fileLine in roadFile.readlines():
readline = fileLine.split(",")
idNumber = int(readline[idValueIndex])
fromLat = float(readline[fromLatValueIndex])
fromLong = float(readline[fromLongValueIndex])
toLat = float(readline[toLatValueIndex])
toLong = float(readline[toLongValueIndex])
#now let's add the points and draw the lines
fromVertex = arcpy.Point(fromLong, fromLat)
roadArray.add(fromVertex)
toVertex = arcpy.Point(toLong, toLat)
roadArray.add(toVertex)
roadCursor = arcpy.InsertCursor(outPath + "\\" + featureClass)
feature = roadCursor.newRow()
polyline = arcpy.Polyline(roadArray, spatialRef)
feature.shape = polyline
feature.setValue("segmentID", idNumber) #adds the segmentID in the attribute table
roadCursor.insertRow(feature)
roadArray.removeAll()
del roadCursor
print "Finished!"
print arcpy.GetMessages()
已创建形状文件,但文件不包含数据。在
错误:
Runtime error
Traceback (most recent call last):
File "", line 23, in
ValueError: 'toLong' is not in list
假定
roadlines.csv
的第一行是segmentID,fromLat,fromLong,toLat,toLong
,您需要从标题中删除final\n
:您还可以使用Python CSV module简化代码
相关问题 更多 >
编程相关推荐