1

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

Source data

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...

WKT to shapefile

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

I know a workaround that involves Joining the source csv to the output shapefile, but I'm sure there's a more elegant solution.

  • I think you're lucky to get that, the geometries are being exported as an array of geometries.. I would create a feature class and use an insert cursor personally but I 'spose if it works why fight it. If you can join then have a read of join field desktop.arcgis.com/en/arcmap/10.3/tools/data-management-toolbox/… which permanently joins the tables - it would be the quickest way. – Michael Stimson Jun 8 '18 at 11:53
  • Please note though GIS.SE isn't a code review site, generally a question should be about a specific problem you're encountering with a minimum block of code and error messages. – Michael Stimson Jun 8 '18 at 11:56
  • As a QGIS and ogr2ogr user, I'd be very surprised if ArcGIS would not support such a basic feature, importing csv with wkt geometries? – pLumo Jun 8 '18 at 12:01
  • @MichaelStimson I see, I'd love to know how to use an insert cursor and understand how they work! – Theo F Jun 8 '18 at 15:07
1

Since you're going from a table to a shapefile you'll have to create a new shapefile and add each field individually. Then initiate an insert cursor for your new shapefile and a search cursor on your table. Change the WKT to a geometry object. Add all field values. Update the new feature's geometry with the "SHAPE@" token.

This is untested but it should point you in the right direction:

print "importing"
import os
import arcpy

#input csv
File = "C:\Users\e1b8\Desktop\E1B8\Outside\Book1.csv"

#create shapefile
print "creating new feature class"
outShp = "C:\\Users\\Team\\Documents\\Theo Laptop Folder\\Tasks\\Quick tasks\\WKTtest\\WKTShapes.shp"
outPath, outName = os.path.split (outShp)
sr = arcpy.SpatialReference(27700)
arcpy.arcpy.CreateFeatureclass_management (outPath, outName, "POLYGON",
                                           spatial_reference = sr)

#add fields
cursFlds = []
desc = arcpy.Describe (outShp)
shpFld = desc.shapeFieldName
oidFld = desc.OIDFieldName
for fld in arcpy.ListFields (File):
    if fld.name in [shpFld, oidFld]: continue
    print "adding", fld.name
    arcpy.AddField_management (outShp,
                               fld.name,
                               fld.type,
                               fld.precision,
                               fld.scale,
                               fld.length,
                               fld.alias,
                               fld.isNullable,
                               fld.required,
                               fld.domain)
    #cursor fields - add field name
    cursFlds += [fld.name]

#get wkt field index
field1 = "WKT"
wktIndex = cursFlds.index (field1)

#insert
print "iteratng csv"
with arcpy.da.SearchCursor (File, cursFlds) as sCurs:
    #insert cursor new shapefile
    with arcpy.da.InsertCursor (outShp, cursFlds + ["SHAPE@"]) as iCurs:
        for row in sCurs:
            #get wkt string
            wkt = row [wktIndex]
            #create geometry object from wkt
            geom = arcpy.FromWKT (wkt, sr)
            #create out row
            outRow = list (row) + [geom]
            #insert row
            iCurs.insertRow (outRow)

print "created:"
print outShp

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.