操作方法
在 ArcGIS Pro 中,可以使用地理处理工具通过引用 CSV 文件中的 x 和 y 点坐标数据来创建面。 此过程也可以使用 ArcPy 模块自动执行。 按照本文中描述的工作流,使用独立的 Python 文件 (.py) 从 CSV 文件创建面。 Python 脚本也在 Jupyter Notebook 和 Python 窗口中运行。
Note: All field values in the CSV file must be numeric data type. If there are text field values, the Python script fails.
import csv import os import arcpy
in_dir = r"<CSVFileFolderPath>" csvFile = "<fileName>.csv" fc = "<newShapefileName>" in_csv = os.path.join(in_dir, csvFile) sr = arcpy.SpatialReference(<projectedCoordinateSystemWKID>)
arcpy.management.CreateFeatureclass( in_dir, fc, geometry_type="POLYGON", spatial_reference=sr ) out_feature_class = os.path.join(in_dir, fc)
arcpy.management.AddField(out_feature_class, "<fieldName>", "LONG")
with arcpy.da.InsertCursor(out_feature_class, ["SHAPE@", "<fieldName>"]) as cursor: point_list = [] parcel_value = None is_first_pass = True with open(in_csv, mode="r") as csv_file: csv_reader = csv.reader(csv_file, delimiter=",") header = next(csv_reader) for row in csv_reader: if is_first_pass: parcel_value = row[0] is_first_pass = False elif row[0] != parcel_value: polygon = arcpy.Polygon(arcpy.Array(point_list), sr) cursor.insertRow([polygon, parcel_value]) parcel_value = row[0] point_list = [] point_list.append(arcpy.Point(row[1], row[2])) polygon = arcpy.Polygon(arcpy.Array(point_list), sr) cursor.insertRow([polygon, parcel_value])
print("COMPLETED")
以下代码块演示了完整工作脚本。
import csv import os import arcpy in_dir = r"C:\Users\ISC-DT25\Documents\Artwork\Art 29483" csvFile = "par_point-TEST.csv" fc = "polygonFromCSV" in_csv = os.path.join(in_dir, csvFile) sr = arcpy.SpatialReference(32049) arcpy.management.CreateFeatureclass( in_dir, fc, geometry_type="POLYGON", spatial_reference=sr ) out_feature_class = os.path.join(in_dir, fc) arcpy.management.AddField(out_feature_class, "POINTID", "LONG") with arcpy.da.InsertCursor(out_feature_class, ["SHAPE@", "POINTID"]) as cursor: point_list = [] parcel_value = None is_first_pass = True with open(in_csv, mode="r") as csv_file: csv_reader = csv.reader(csv_file, delimiter=",") header = next(csv_reader) for row in csv_reader: if is_first_pass: parcel_value = row[0] is_first_pass = False elif row[0] != parcel_value: polygon = arcpy.Polygon(arcpy.Array(point_list), sr) cursor.insertRow([polygon, parcel_value]) parcel_value = row[0] point_list = [] point_list.append(arcpy.Point(row[1], row[2])) polygon = arcpy.Polygon(arcpy.Array(point_list), sr) cursor.insertRow([polygon, parcel_value]) print("COMPLETED")
获取来自 ArcGIS 专家的帮助
下载 Esri 支持应用程序