1

I am attempting to convert a shapefile to WKT using ArcGIS 10.4. I have seen the question/answer posted here, Converting geometry to WKT using ArcPy?, which is helpful. However, when I follow the code and write it to the text file I create, the spatial reference is incorrect. The geometric shape is identical to the original shapefile, but the WKT maps to a different continent. I don't have experience writing to WKT, so I could be making an amateurish mistake that is causing my spatial reference to be off.

import arcpy

# Create new empty text file
outFile = open(r"my_path.txt", "w")

# My shapefile
fc = r"my_path.shp"

# Convert to WKT and write to file
cursor = arcpy.da.SearchCursor(fc, ["SHAPEFILE@WKT"])
for row in cursor:
    print(row[0])
    outFile.write((row[0]) + "\n")
del row
del cursor
outFile.close()

The projection that I want my WKT to be in is NAD_1983_UTM_Zone_13N.

  • Oops, in my haste, I wrote ["SHAPEFILE@WKT"] rather than ["SHAPE@WKT"] @Luke – Edward Dec 21 '16 at 1:15
4

Firstly, "SHAPEFILE@WKT" should be "SHAPE@WKT".

Secondly, your WKT will be in exactly the same coordinate system as your shapefile. If you're getting strange results, your data is probably not in NAD 1983 UTM Zone 13N, but some other spatial reference and is being projected on the fly when you view it in ArcGIS.

To specify the output spatial reference for your search cursor, pass in a SpatialReference object.

For example:

import arcpy

# My shapefile (which is in WGS84 Geographic)
fc = r"C:/Temp/test.shp"

#I want to output WKT in NAD 1983 UTM Zone 13N
sr = arcpy.SpatialReference('NAD 1983 UTM Zone 13N')
# or could use
# sr = arcpy.SpatialReference(26913)

#Print out and compare the spatial refs
print(arcpy.Describe(fc).SpatialReference.exportToString())
print(sr.exportToString())

# Convert to WKT and print
cursor = arcpy.da.SearchCursor(fc, ["SHAPE@WKT"])
for row in cursor:
    print(row[0])
    break # just print 1st row to demonstrate

# Convert to WKT projected on the fly and print
cursor = arcpy.da.SearchCursor(fc, ["SHAPE@WKT"], spatial_reference=sr)
for row in cursor:
    print(row[0])
    break # just print 1st row to demonstrate

del row
del cursor

Output:

GEOGCS['GCS_WGS_1984',DATUM['D_WGS_1984',SPHEROID['WGS_1984',6378137.0,298.257223563]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]];-400 -400 11258999068426.2;-100000 10000;-100000 10000;8.98315284119522E-09;0.001;0.001;IsHighPrecision
PROJCS['NAD_1983_UTM_Zone_13N',GEOGCS['GCS_North_American_1983',DATUM['D_North_American_1983',SPHEROID['GRS_1980',6378137.0,298.257222101]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]],PROJECTION['Transverse_Mercator'],PARAMETER['False_Easting',500000.0],PARAMETER['False_Northing',0.0],PARAMETER['Central_Meridian',-105.0],PARAMETER['Scale_Factor',0.9996],PARAMETER['Latitude_Of_Origin',0.0],UNIT['Meter',1.0]];-5120900 -9998100 10000;-100000 10000;-100000 10000;0.001;0.001;0.001;IsHighPrecision
MULTIPOLYGON (((-104.88510543531396 43.417242462433592, -104.48059706432281 43.667085868045774, -104.81372160513905 44.190567289328442, -105.15874345098445 43.893134663599653, -105.68222487226711 43.738469698220683, -104.88510543531396 43.417242462433592)))
MULTIPOLYGON (((509301.25108411006 4807156.9673131397, 541874.8360413569 4835028.9378186846, 514886.82021686854 4893055.9525310742, 487250.06276868912 4860015.9002533574, 445063.47957695409 4843052.0422064001, 509301.25108411006 4807156.9673131397)))
  • +1. If you want to look at the first row of a cursor (similar to to pandas .head()), you can use cursor.next()[0] – Paul Dec 20 '16 at 23:22
  • @Paul Yes .next() works as well if you specifically want just the first row. I used break intentionally to maintain the required code to loop through all rows so all the OP has to do is remove or comment that line. – user2856 Dec 20 '16 at 23:29
  • @Luke Thanks. I did read that arcpy.da.SearchCursor inherits the CRS from the feature class, so that's good for the confirmation. However, is it possible that an error occurred with the CRS in how I wrote the WKT to a new text file? (outFile.write((row[0] + "\n"). If the error doesn't exist in this code, then I'm scratching my head. I'm wondering whether the online mapping portal that we are using in which we upload the WKT is causing a problem. – Edward Dec 21 '16 at 1:22
  • 1
    @user76245 if your feature class is in NAD 83 UTM 13N and it's not displaying correctly in your online map, then your online map is not in UTM 13N and you need to find out what spatial reference it uses and pass that in to your search cursor (or use WGS84 Geographic as the online maps I tried accept that). WKT geometry just contains the coordinates, it doesn't contain any info about what spatial reference those coordinates are in. For example if I wanted to display some coordinates using WKT in Wicket, I need to pass in WKT in WGS84 – user2856 Dec 21 '16 at 1:35
  • @Luke [link] (arthur-e.github.io/Wicket/sandbox-gmaps3.html) was helpful showing that the script did what I intended. Thanks for the clarity of your answers - they pointed to the solution. The web map was expecting the WKT to be in Web Mercator projection. Once files were converted to Web Mercator, the shapefile -> WKT conversion worked. – Edward Dec 21 '16 at 20:49

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.