HOW TO
In ArcGIS Pro, geoprocessing tools can be used to create polygons by referencing the x and y point coordinate data from a CSV file. This process can also be automated using the ArcPy module. Follow the workflow described in this article to create polygons from a CSV file using a stand-alone Python file (.py). The Python script also runs in Jupyter Notebook and the Python window.
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")
The code block below demonstrates the full working script.
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")
Article ID: 000029483
Get help from ArcGIS experts
Start chatting now