我是python編程的新手,我有一個相當簡單的項目,但遇到一些困難。我想(a)提取shapefile(多邊形)頂點的XY座標,(2)將所有座標寫入一個csv文件,其中第一列有X座標,第二列有Y座標。我迄今爲止編寫的代碼將頂點座標寫入csv文件,但座標的每個數字都放在不同的列中。使用Python將XY座標寫入CSV文件
這裏是到目前爲止我的代碼:
import arcpy, os, csv
from arcpy import env
workspace = "J:/Folder/"
arcpy.env.overwriteOutput = True
myPath = workspace
oFile = open(myPath + "xyCoord.csv", "w")
polygon = myPath + "Polygon2.shp"
writer = csv.writer(oFile, delimiter = ',', dialect = 'excel', lineterminator = '\n')
writer.writerow(['X', 'Y'])
for row in arcpy.da.SearchCursor(polygon, ["[email protected]", "[email protected]"]):
print ("Feature {0}:".format(row[0]))
partnum = 0 # Prints the current multipont's ID
for part in row[1]:
print ("Part {0}:".format(partnum)) # Prints the part number
for vertex in part:
print ("{0}, {1}".format(vertex.X, vertex.Y))
writer.writerow(str(vertex.X) + str(vertex.Y))
partnum += 1
oFile.close()