0

我有一个包含 75 列和近 4000 行的 .CSV 文件。我需要为整个 .CSV 文件创建一个 shapefile(点),每列作为一个字段。所有 75 列都需要转移到新的 shapefile 中,每列代表一个字段。

关于这个话题似乎已经有很多了,但是我能找到的所有内容都可以找到包含少量列的 .csv 文件。

https://gis.stackexchange.com/questions/17590/why-is-an-extra-field-necessary-when-creating-point-shapefile-from-csv-files-in

https://gis.stackexchange.com/questions/35593/using-the-python-shape-library-pyshp-how-to-convert-csv-file-to-shp

这个脚本看起来很接近我需要完成的工作,但它再次为 .CSV 中的每一列添加了一个字段,在这个例子中,它有三个字段;日期、纬度、经度。

import arcpy, csv
arcpy.env.overwriteOutput = True

#Set variables
arcpy.env.workspace = "C:\\GIS\\StackEx\\"
outFolder = arcpy.env.workspace
pointFC = "art2.shp"
coordSys = "C:\\Program Files\\ArcGIS\\Desktop10.0\\Coordinate Systems" + \
           "\\Geographic Coordinate Systems\\World\\WGS 1984.prj"
csvFile = "C:\\GIS\\StackEx\\chicken.csv"
fieldName = "DATE1"

#Create shapefile and add field
arcpy.CreateFeatureclass_management(outFolder, pointFC, "POINT", "", "", "", coordSys)
arcpy.AddField_management(pointFC, fieldName, "TEXT","","", 10)

gpsTrack = open(csvFile, "r")

headerLine = gpsTrack.readline()
#print headerLine
#I updated valueList to remove the '\n'
valueList = headerLine.strip().split(",")
print valueList
latValueIndex = valueList.index("LAT")
lonValueIndex = valueList.index("LON")
dateValueIndex = valueList.index("DATE")

# Read each line in csv file
cursor = arcpy.InsertCursor(pointFC)
for point in gpsTrack.readlines():

   segmentedPoint = point.split(",")
   # Get the lat/lon values of the current reading                    
   latValue = segmentedPoint[latValueIndex]
   lonValue = segmentedPoint[lonValueIndex]
   dateValue = segmentedPoint[dateValueIndex]
   vertex = arcpy.CreateObject("Point")
   vertex.X = lonValue
   vertex.Y = latValue
   feature = cursor.newRow()
   feature.shape = vertex
   feature.DATE1 = dateValue
   cursor.insertRow(feature)

del cursor

有没有一种更简单的方法可以使用 python 创建 shapefile,而无需为 .CSV 文件中的所有 75 列添加字段?任何帮助是极大的赞赏。

4

1 回答 1

0

只需选择您需要的列;您不需要使用所有列。

使用csv模块读取文件,然后从每一行中挑选出 2 个值:

import csv

cursor = arcpy.InsertCursor(pointFC)
with open('yourcsvfile.csv', 'rb') as csvfile:
    reader = csv.reader(csvfile)
    for row in reader:
        point = arcpy.CreateObject("Point")
        point.X, point.Y = float(row[5]), float(row[27])  # take the 6th and 28th columns from the row
        cursor.insertRow(point)        
于 2013-04-03T15:32:02.380 回答