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
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:
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.
Thanks @magisan, looks like I will keep using QGIS for this as it takes four clicks to get all of this done
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.
OID 2 is a multipart polygon. Output:
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.