HOW TO
In ArcGIS Pro können mithilfe von Geoverarbeitungswerkzeugen Polygone erstellt werden, indem die X- und Y-Punktkoordinaten aus einer CSV-Datei referenziert werden. Dieser Vorgang kann auch mit dem ArcPy-Modul automatisiert werden. Führen Sie den in diesem Artikel beschriebenen Workflow aus, um Polygone aus einer CSV-Datei mithilfe einer eigenständigen Python-Datei (.py) zu erstellen. Das Python-Skript kann auch in Jupyter Notebook und im Python-Fenster ausgeführt werden.
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")
Mit dem folgenden Code-Block wird das voll funktionsfähige Skript demonstriert.
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")
Artikel-ID: 000029483
Unterstützung durch ArcGIS-Experten anfordern
Beginnen Sie jetzt mit dem Chatten