Exporting WKT string from CSV to Shapefile

3340
5
06-07-2018 09:24 AM
TheodoreF
Occasional Contributor II

Hi,

I have a CSV storing addresses, reference numbers, etc... and also a WKT string of boundary coordinates for each property:

The script (ammended from another user's post in 2012) sccessfully converts the CSV to a shapefile, with the spatial refeence set to EPSG- 27700:

# convert well known text to geometry, and compile shapes into a single feature class...
# 11/15/2012
import arcpy

File = "C:\\Users\\Team\\Documents\\Theo Laptop Folder\\Tasks\\Quick tasks\\WKTtest\\WKT_to_QGISmakealayerCSV2.csv"

# dimension the WKT string field and poly ID field...
# the field holding the WKT string...
field1 = "WKT"
# the field holding the unique ID...
field2 = "Our_ref"

# set up the empty list...
featureList = []

# set the spatial reference to a known EPSG code...
sr = arcpy.SpatialReference(27700)
# iterate on table row...
cursor = arcpy.SearchCursor(File)
row = cursor.next()
while row:
    print (row.getValue(field2))
  
    WKT = row.getValue(field1)
    # this is the part that converts the WKT string to geometry using the defined spatial reference...
    temp = arcpy.FromWKT(WKT, sr)
    # append the current geometry to the list...
    featureList.append(temp)

    row = cursor.next()
  
# copy all geometries in the list to a feature class...
arcpy.CopyFeatures_management(featureList, "C:\\Users\\Team\\Documents\\Theo Laptop Folder\\Tasks\\Quick tasks\\WKTtest\\WKTShapes.shp")  

# clean up...
del row, temp, WKT, File, field1, featureList, cursor

This is in python 2.7 language fyi. (I'd love this in 3.6 if possible...)

Now when I drag the newly created shapefile into Arc it all looks great, but the fields from the CSV aren't carried across...

How can I ammend the code to include all the original string fields from the CSV?

thank you

Tags (3)
0 Kudos
5 Replies
DanPatterson_Retired
MVP Esteemed Contributor

You could add a 'Join' into the workflow since your key field would be the id's of the geometry.  That would just simplify things

0 Kudos
TheodoreF
Occasional Contributor II

I tried using Join Field to join the shapefile with the CSV (converted to an ESRI table so it has OIDs), but they don't match up. The shapefile of the polygons has an FID field starting at 0 and running to 5 (6 polygons), but the ObjectIDs on the table start at 1, running to 6. So everything is out of sync by 1.

On the CSV I added a 'Join_id' column and filled it with 0 to 5, but the Join Field tool wouldn't recognise this as an OID field for use in the join.

UPDATE: so using the Add Join tool I can use my 'Join_id' column in the CSV to join to the shapefile. This tool only works off layers in the ToC of course... I'd be looking to run this script outside of ArcGIS, in the background.

I'm sure it must be possible to pull the columns from the original csv through...

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

That is a problem with FID and OBJECTID.  The easiest solution I have found is to add a column at the source so it is in the geodatabase table and calculate a new

'IDjoin' using the field calculator, ie OBJECTID - 1

then you will have a zero-based field.

Alternately, do the reverse in the csv to get an id field that is 1 based

TheodoreF
Occasional Contributor II

So I've created a new field in the source csv, run Table to Table on that (which gives it an OID), then used this new table in the Join Field tool.

It works fine as a workaround for now. Thanks for suggesting the join.

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

glad it worked out... when I switched to gdb's I discovered than since featureclass tables are 1-based and I am used to 0-based indexing with python and its ilk and shapefiles