[GIS] Converting geometry to WKT using ArcPy

arcpyconvertwell-known-text

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.

Best Answer

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