HOW TO

Create polygons from a CSV file with coordinates using ArcPy

Last Published: March 23, 2023

Summary

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.

Procedure

Note:
All field values in the CSV file must be numeric data type. If there are text field values, the Python script fails.
  1. Create a file with the .py extension.
  2. Specify the script below in the file before running the script.
    1. Import the necessary modules.
import csv
import os
import arcpy
  1. Define the CSV file folder path, CSV file name, the new shapefile name, and the spatial reference as variables.
in_dir = r"<CSVFileFolderPath>"
csvFile = "<fileName>.csv"
fc = "<newShapefileName>"
in_csv = os.path.join(in_dir, csvFile)
sr = arcpy.SpatialReference(<projectedCoordinateSystemWKID>)
  1. Apply the CreateFeatureclass() function to create a polygon shapefile in the folder specified in Step 2(b).
arcpy.management.CreateFeatureclass(
    in_dir, fc, geometry_type="POLYGON", spatial_reference=sr
)
out_feature_class = os.path.join(in_dir, fc)
  1. Apply the AddField() function to add the field name in the shapefile.
arcpy.management.AddField(out_feature_class, "<fieldName>", "LONG")
  1. Apply the InsertCursor() function to insert a new row in an attribute table. Apply the append() function to add the point to the feature's array of points. Apply the arcpy.Polygon() function to create the polygon. The following query statements iterate through the data in the CSV file. 'row[0]' refers to the position of the point group ID number, 'row[1]' refers to the position of the X coordinate data, and 'row[2]' refers to the position of the Y coordinate data in the CSV file.
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])
  1. Apply the print() function to show a message when the polygons are created.
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")
  1. Open the newly created shapefile in ArcGIS Pro using a folder connection in the Catalog pane. Refer to ArcGIS Pro: Add a shapefile to the map for more information.

Article ID: 000029483

Software:
  • ArcGIS Pro 3 1
  • ArcGIS Pro 3 0
  • ArcGIS Pro 2 8 x
  • ArcGIS Pro 2 x

Receive notifications and find solutions for new or common issues

Get summarized answers and video solutions from our new AI chatbot.

Download the Esri Support App

Related Information

Discover more on this topic

Get help from ArcGIS experts

Contact technical support

Download the Esri Support App

Go to download options