Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Export feature class to a TSV file with geometry in WKT format
import arcpy
class Toolbox(object):
def __init__(self):
self.label = "TSV Toolbox"
self.alias = "TSV Toolbox"
self.tools = [TSVTool]
class TSVTool(object):
def __init__(self):
self.label = "Export Features"
self.description = """
Export features to TSV file with geometry in WKT.
The WKT is the last field in the TSV file.
"""
self.canRunInBackground = True
def getParameterInfo(self):
paramInp = arcpy.Parameter(name="in_inp", displayName="Input Dataset", direction="Input", datatype="Table View",
parameterType="Required")
paramTSV = arcpy.Parameter(name="in_tsv", displayName="Output TSV File", direction="Output", datatype="File",
parameterType="Required")
paramSep = arcpy.Parameter(name="in_sep", displayName="Field Separator", direction="Input", datatype="String",
parameterType="Required")
paramSep.value = "tab"
paramHdr = arcpy.Parameter(name="in_hdr", displayName="Write Header", direction="Input", datatype="Boolean",
parameterType="Required")
paramHdr.value = False
return [paramInp, paramTSV, paramSep, paramHdr]
def isLicensed(self):
return True
def updateParameters(self, parameters):
return
def updateMessages(self, parameters):
return
def asText(self, o):
return o.encode('UTF-8', 'replace') if isinstance(o, unicode) else str(o)
def execute(self, parameters, messages):
fc = parameters[0].valueAsText
tsv = parameters[1].valueAsText
description = arcpy.Describe(fc)
shape_name = description.shapeFieldName
field_names = [field.name for field in description.fields]
field_names.remove(shape_name)
field_names.append(shape_name + "@WKT")
sep = parameters[2].valueAsText
sep = '\t' if sep == "tab" else sep[0]
with open(tsv, 'wb') as f:
if parameters[3].value:
f.write(sep.join(field_names))
f.write('\n')
with arcpy.da.SearchCursor(fc, field_names) as cursor:
for row in cursor:
f.write(sep.join([self.asText(r) for r in row]))
f.write('\n')
return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment