15

I would like to be able to convert a single feature (or potentially many features) in an ArcGIS layer into Well Known Text (WKT) format during a geoprocessing operation using Python and ArcPy (no non-ArcPy modules). The purpose is to then pass the WKT on to SQL Server Spatial and do additional processing outside of the ArcGIS GP toolset. Is there a method for converting a ArcGIS geometry features to WKT via ArcPy?

I have already read the following, without finding what I am after:

There apparently used to be a tool called "Write Features To Text File" (which appears to be a Python script) that was in the Samples toolbox, but that toolbox was deprecated at version 10 and I cannot find a copy of the Samples Toolbox (I have v10.0) on my machine. If there are no current solutions, if someone could just point me to a copy of the sample, I would be totally fine with using that tool, too.

4 Answers 4

29

A da.searchcursor should work for you.

for row in arcpy.da.SearchCursor("path2data", ["SHAPE@WKT"]):
  print row[0]

POINT Z (-119.53753379999995 49.854383300000052 303.14500000000407)

doc here: http://resources.arcgis.com/en/help/main/10.1/index.html#//002z0000001t000000

Note: SHAPE@JSON, SHAPE@WKB, and SHAPE@WKT tokens were made available at ArcGIS 10.1 Service Pack 1.

Or, if you're after the samples toolbox like you mentioned - its there, just deprecated. You can call into it with scripts still. If you need the actual toolbox you can use, its here on the old Model and Script Gallery

EDIT (extra example)...Because you asked so nicely: :)

for row in arcpy.da.SearchCursor("GPX_Layer", ["SHAPE@WKT"],where_clause="TYPE = 'a'"):
    print row[0]

It's just the same as a "select by attributes" type of query/expression. I have a field called "TYPE" and and a value 'a'.

3
  • +1 For including the example---would it be asking too much for a second one showing how the where_clause would be implemented? :)
    – elrobis
    Commented Apr 17, 2013 at 22:00
  • While I unfortunately do not have access to 10.1 yet, I was ultimately able to update the Python script that you linked to. Thanks! Commented Apr 18, 2013 at 19:29
  • +1 for "POINT Z " it seems that the "POINT" do not support 3D in arcpy.
    – geogeek
    Commented Oct 10, 2013 at 9:41
7

Just adding this in case it's useful for someone...

# Convert to WKT by field name (Shape)
file_path = 'C:\shapefile.shp'
query= arcpy.SearchCursor(file_path)
for row in query:
  the_geom=row.getValue('Shape') # Get Geometry field
  wkt = the_geom.WKT # Convert to WKT, can also use WKB, JSON etc
6

I'm not equipped to confirm this, but this page documenting SearchCursor (arcpy.da), discussing the field_names parameter, notes the following:

FROM ArcGIS Help 10.1 - SearchCursor (arcpy.da)

Additional information can be accessed using tokens (such as OID@) in place of field names:

SHAPE@WKT —The well-known text (WKT) representation for OGC geometry. It provides a portable representation of a geometry value as a text string

Note: SHAPE@JSON, SHAPE@WKB, and SHAPE@WKT tokens were made available at ArcGIS 10.1 Service Pack 1.

What do you think?

1
  • 1
    +1 Also, I think @RyanDalton may be able to find Write Features to Text File here.
    – PolyGeo
    Commented Apr 17, 2013 at 21:55
3

The Write Features to Text File tool can be found here.

There is a related ArcGIS Idea here that has attracted, in my opinion, perplexingly few votes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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