Select to view content in your preferred language

Feature Class Z to ASCII File Names

699
1
03-12-2019 10:17 AM
RussellNasrallah1
New Contributor

Hello All, 
I have a seemingly minor problem which likely requires some Python coding. I am not currently familiar with python (more of a Matlab person), and was wondering if anyone had any advice. 
I am attempting to use the Feature Class Z to ASCII tool to make text files with my Z elevations. This tool performs correctly in all respects except 1: It names the text files based on the OID (or FID) column instead of a unique column (named 'Id') that is in my attribute table. Up until this point we have had a separate, hand made file that converts from FID to Id in subsequent analysis, but it is incredibly prone to error and confusing to use. 
Is there any simple way to modify this tool so that it outputs names based on a chosen column instead of the default OID column? 
I  have attached a zip file of a z-interpolated shapefile below. The 'Id' column is the desired naming column. 

The Code is below:

'''****************************************************************************
Name: FeatureClassZToASCII Example
Description: This script demonstrates how to use the
 FeatureClassZToASCII tool to create generate files for all
 z-aware point features in a given workspace.
****************************************************************************'''
import arcpy
import exceptions, sys, traceback
from arcpy import env

try:
 # Obtain a license for the ArcGIS 3D Analyst extension
 arcpy.CheckOutExtension('3D')
 # Set environment settings
 env.workspace = 'C:/data'
 # List all points in the target workspace
 fcList = arcpy.ListFeatureClasses("*", "POINT")
 if fcList:
 # Set Local Variables
 outFolder = "C:/output"
 outFormat = "GENERATE"
 delimeter = "SPACE"
 decimal = "FIXED"
 digits = 3
 dec_sep = "DECIMAL_POINT"
 for fc in fcList: 
 # Use Describe method to evaluate whether the feature class is z-aware
 desc = arcpy.Describe(fc)
 if desc.hasZ == True:
 # Define the output file name by replacing '.shp' with _ascii.txt
 outName = fc.replace('.shp', '') + "_ascii.txt"
 #Execute FeatureClassZToASCII_3d
 arcpy.FeatureClassZToASCII_3d(fc, outFolder, outName, outFormat, delimeter, decimal, digits, dec_sep)
 else:
 print "There are no feature classes in the " + env.workspace + " directory."


except arcpy.ExecuteError:
 print arcpy.GetMessages()
except:
 # Get the traceback object
 tb = sys.exc_info()[2]
 tbinfo = traceback.format_tb(tb)[0]
 # Concatenate error information into message string
 pymsg = 'PYTHON ERRORS:\nTraceback info:\n{0}\nError Info:\n{1}'\
 .format(tbinfo, str(sys.exc_info()[1]))
 msgs = 'ArcPy ERRORS:\n {0}\n'.format(arcpy.GetMessages(2))
 # Return python error messages for script tool or Python Window
 arcpy.AddError(pymsg)
 arcpy.AddError(msgs)

I assume that the root of the problem is the 'fclist' variable. Any help in solving this problem is appreciated. 

Ps. Bonus points if anyone can give some guidance on how to include this as a tool in arcgis so I don't have to run it in an IDE

0 Kudos
1 Reply
DanPatterson_Retired
MVP Emeritus

your code is hard to read... for reference

/blogs/dan_patterson/2016/08/14/script-formatting 

maybe dump the try, except block to make the error line more visible in the trackback

0 Kudos