Select to view content in your preferred language

Export feature class to CSV with geometry

2446
3
06-11-2023 11:38 AM
Moseleyi
Emerging Contributor

I'm trying to export an Attribute Table to a CSV file where the geometry is in, for example, WKT. I can't find any help regarding this and it's a quite simple task in QGIS via Export feature.

Everything I tried so far exports feature classes to SHP file or attribute table to DBF. I just need simple CSV that includes geometry

0 Kudos
3 Replies
ChristopherCounsell
MVP Regular Contributor

I don't believe you can add the x/y fields as part of a single processing tool option. There's discussion on this not being possible historically.

The common method to achieve this is:

  1. Add an x and a y field to the feature class
  2. Calculate geometry of your features for each x/y field
  3. Export to csv
  4. (optional) remove the x/y fields, noting that they need to be recalculated for new features or if your features move

You could try using ModelBuilder or the like to build an all in one tool that does this. Maybe have it drop the fields at the end after the export, or do the calculations within the model so as not to modify the original dataset... If this is functionality you'd like, it would make for a great ArcGIS Idea.

Moseleyi
Emerging Contributor

Thanks @magisan, looks like I will keep using QGIS for this as it takes four clicks to get all of this done

VinceE
by
Frequent Contributor

I'm not super familiar with WKT representations of geometry objects, but would something like this work for you?

import arcpy
import csv

# Leave the quotes and the leading "r" in both lines below. Replace my text with your paths.
input_fc = r"THE FULL PATH TO YOUR FEATURE CLASS"
out_csv = r"THE FULL PATH OF A CSV TO BE CREATED"

# Get all the field names. Explicity specify "SHAPE@" for the full geometry object.
fields_curs = ["SHAPE@"] + [f.name for f in arcpy.ListFields(input_fc) if f.type != "Geometry"]

# Create a writeable CSV file. Create the writer object.
with open(out_csv, "w", newline="") as csvfile:
    csvwriter = csv.writer(csvfile, delimiter=",")
    csvwriter.writerow(fields_curs)  # Remove this line if you don't want headers.

    # Write each row in the feature class, including the WKT geometry, to the CSV.
    with arcpy.da.SearchCursor(input_fc, fields_curs) as scurs:
        for geom, *flds in scurs:
            csvwriter.writerow([geom.WKT, *flds])

 

Assuming you replace the two variables at the top with your actual paths, this should give you an output that contains those geometries as WKT. After updating those variables, you should be able to copy/paste this directly into the Python window within ArcGIS Pro.

VinceE_0-1687975631153.png

OID 2 is a multipart polygon. Output:

VinceE_1-1687975828305.png

Assuming I have understood what you're after, this would allow you to stay in the Esri environment. But, if all it takes is a couple clicks in QGIS, then that sounds like a pretty good option too.

0 Kudos