2017-11-13 1 views
-1

gis.stackexchange 웹 사이트에서 파이썬 스크립트를 복사했습니다. 목적은 시작점과 끝점 목록을 같은 끝점에서 시작하고 끝나는 선이있는 gis 모양 파일로 변환하는 것입니다. 나는 전에 파이썬을 사용하지 않았다. 그래서 나에게 인내심을 가져라. 여기 라인이있는 shapefile을 만들려고 파이썬에서 끝 점이있는 csv 파일을 만듭니다.

는 스크립트입니다

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

+1

Python CSV module를 사용하여 코드를 단순화 할 수있는 작업 'roadlines.csv' 파일의 첫 번째 줄입니다. – Indent

+0

옙 CSV에서 열 이름을 제공해야합니다. 첫 번째 줄 –

+0

이름, 세그먼트 ID, 워크 시트, fromLat, fromLong, toLat, toLong –

답변

0

roadlines.csv의 첫 번째 줄을 Presumming하면 헤더에서 최종 \n을 제거해야합니다 segmentID,fromLat,fromLong,toLat,toLong입니다 :

headerLine = roadFile.readline().strip() 

또한

관련 문제