Select to view content in your preferred language

How to convert WKT polygon data in csv file into ArsGIS pro layer

4465
2
06-28-2023 09:13 PM
Labels (2)
DaveKim2
New Contributor

Hi all, 

I have a csv file having WKT polygon data and tried to convert them into arcgis pro polygon layer features. I just added the csv file into my project and looked around relevant functions and attributes of the csv file, but could not find the wat to convert polygon data in a csv file into arcgis polygon layer. How can I convert the WKT polygon data into polygon layer feature?

0 Kudos
2 Replies
VinceE
by
Frequent Contributor

I am not aware of a way to do this by adding the CSV and running a built-in geoprocessing tool (someone else certainly might be), but if I understand you correctly, I believe the below will achieve what you're looking for using Python.

import arcpy
import csv

arcpy.env.overwriteOutput = True

# INPUTS---------------------------------------------#
input_csv = r"FULL PATH TO YOUR CSV"

# Output Geometry Information - type and the integer EPSG code that refers to the spatial reference.
geom_type = "POLYGON"  # This could also be POINT, POLYLINE, etc.
geom_sr = 3857  # you can Google your spatial reference name and "EPSG" to get this number

# Output Location and Name
out_gdb = r"FULL PATH TO YOUR GEODATABASE"
out_name = "GEOMETRY_FROM_WKT"  # Leave if you want, this is the output feature class name

#-------------------------------------------------------#
# Create a blank output FC using the provided geometry type and spatial reference.
out_fc = arcpy.management.CreateFeatureclass(out_path=out_gdb,
                                             out_name=out_name,
                                             geometry_type=geom_type,
                                             spatial_reference=geom_sr, )

# Open CSV. Create Reader object.
with open(input_csv, "r", newline="") as csvfile:
    csvreader = csv.reader(csvfile, delimiter=",")

    # Create InsertCursor.
    # For each row in CSV, attempt to convert WKT in column 1 to Esri Geometry object.
    # All other columns are read, but ignored.
    with arcpy.da.InsertCursor(out_fc, "SHAPE@") as icurs:
        for wkt_geom, *other_columns in csvreader:

            # Try to convert the WKT to a geom object.
            try:
                print(f"CONVERTING WKT GEOM...\n{wkt_geom}")
                geom_from_wkt = arcpy.FromWKT(wkt_geom, arcpy.SpatialReference(3857))

            # Skip if conversion fails, could be bad formatting or a spreadsheet header.
            except TypeError:
                print(f"SKIPPING {wkt_geom}; IS THIS A HEADER?")
                continue

            # Attempt to write the Geometry object to the new feature class.
            icurs.insertRow([geom_from_wkt])




If you update the five items in the "INPUTS" section, you should be able to then copy and paste this whole thing into the Python window in ArcGIS Pro, and hit "enter" to run it. You want to leave the quotes, and also the preceding "r" where applicable.

Here is what my input CSV looks like. Note that headers are optionally used, and other fields (columns) can be included, but they will be ignored in the output:

VinceE_0-1688062083881.png

And the outputs:

VinceE_1-1688062264866.png

Note there is a single part, a multipart (two triangles in the middle), and a polygon with internal rings (a hole). This script will ignore headers in the CSV, and it essentially ignores fields other than column 1, which is where the WKT geometry must be stored.

 

DaveKim2
New Contributor

VinceE,

Thank you so much for detailed explanation and python code to convert WKT polygon to arcgis shape file. I am going to put this code into the python window in arcgis pro and I will let you know the result.

Thanks

0 Kudos