Hi, I've written a short piece of code that takes a shapefile, copies to a .gdb, adds WKT attributes and outputs as an excel spreadsheet. The code is:
import arcpy
import os
###Get parameters as variables
inputshp = arcpy.GetParameterAsText(0)
ouputDest = arcpy.GetParameterAsText(1)
outputName = arcpy.GetParameterAsText(2)
###Copy shapefile to feature class in Default.gdb and add a field called WKT
intFC = arcpy.FeatureClassToFeatureClass_conversion(in_features = inputshp, out_path = arcpy.env.workspace, out_name = inputshp)
arcpy.AddField_management(in_table = intFC, field_name = "WKT", field_type = TEXT, field_length = 100000)
###Populate WKT column with WKT geometery
with arcpy.da.UpdateCursor(intFC,["WKT", "SHAPE@WKT"]) as cursor:
for row in cursor:
row[0] = row[1]
cursor.updateRow(row)
###Set output folder to output folder as given by user
arcpy.env.workspace = outputDest
###Create xls output
TableToExcel_conversion (intFC, outputName)
###Delete intermittent feature class
Delete_management (intFC)
print "Finished"
Thus is then implemented in a tool with input parameters of: Shapefile, destination folder and excel spreadsheet name. In trying to run the tool however, I keep receiving the error:
Traceback (most recent call last):
File "S:\Works and Infrastructure Services\Infrastructure and Design\Technical Services\Asset Management\Robin\Python\AddWKT.py", line 10, in <module>
intFC = arcpy.FeatureClassToFeatureClass_conversion(in_features = inputshp, out_path = arcpy.env.workspace, out_name = inputshp)
File "c:\program files (x86)\arcgis\desktop10.3\arcpy\arcpy\conversion.py", line 1790, in FeatureClassToFeatureClass
raise e
ExecuteError: ERROR 999999: Error executing function.
Failed to execute (FeatureClassToFeatureClass).
I've tried and failed to diagnose this myself- I'm pretty new to python and this is a means to an end but also a learning experience- could anybody tell me what I'm doing wrong here?
Edit: If anyone can give me pointers on how to format a codeblock within this forum that would also be handy!
Solved! Go to Solution.
Robin, what is the value for 'inputshp'? Is it a path or just a filename?? If a filename, does it contain the .shp extension?? That might be causing the error, i.e. the FeatureClassToFeatureClass_conversion is trying to create a featureclass named something.shp in the file geodatabase. You can't have a '.' in the featureclass name.
Hope this helps, Matthew
/blogs/dan_patterson/2016/08/14/script-formatting for code formatting.
When you are reformatting, throw a print statement in to see what "SHAPE@WKT" returns.
Also, try Copy instead of featureclasstofeatureclass.
Can the field length be 100000? I would give that a try manually (although I suspect it can)
TableToExcel_conversion most likely going to fail if the field length can't be replicated in excel
But line numbers might help because the 999... error is generic
Thanks Dan. I have performed the same operation manually which returned the desired result, addressing points regarding field length, field length in excel and (possibly?) the nature of the "SHAPE@WKT" result. That is, field length of 100000 characters is possible (in a feature class, hence performing the conversion rather than updating the shapefile directly- if there is a way of simply stating greatest possible threshold on field length I don't know it although that would be useful as the WKT strings can be very long). The update cursor script works when it is run from the python window in ArcGIS directly. Conversion to excel works in manual test cases so far, although I suppose in theory the WKT string could end up surpassing an excel character limit.
I'll try replacing the conversion with a copy function. I'm also going to try replicating the script in model builder to see if I can glean any more information. Thanks for the info regarding code formatting.
Hi Dan, as you can see below, I've resolved the initial error, but as you predicted it looks as though it is now falling over at the excel conversion. I've tried manually performing the process and it looks as though it is indeed running into the excel character limit- so this is something I will need to think my way around- but the error I'm receiving is different to the character limit error when manually performing the process. Just for arguments sake, can you pinpoint what might be going wrong with the script at that point? Revised script:
import arcpy
import os
###Get parameters as variables
inputshp = arcpy.GetParameterAsText(0)
ouputDest = arcpy.GetParameterAsText(1)
outputName = arcpy.GetParameterAsText(2)
###Copy shapefile to feature class in Default.gdb and add a field called WKT
intFC = arcpy.FeatureClassToFeatureClass_conversion(in_features = inputshp, out_path = arcpy.env.workspace, out_name = "intFC")
arcpy.AddField_management(in_table = intFC, field_name = "WKT", field_type = "TEXT", field_length = 100000)
###Populate WKT column with WKT geometery
with arcpy.da.UpdateCursor(intFC,["WKT", "SHAPE@WKT"]) as cursor:
for row in cursor:
row[0] = row[1]
cursor.updateRow(row)
###Set output folder to output folder as given by user
arcpy.env.workspace = "outputDest"
###Create xls output
arcpy.TableToExcel_conversion (intFC, outputName)
###Delete intermittent feature class
arcpy.Delete_management (intFC)
print "Finished"
And the error is:
Traceback (most recent call last):
File "S:\Works and Infrastructure Services\Infrastructure and Design\Technical Services\Asset Management\Robin\Python\AddWKT.py", line 25, in <module>
arcpy.TableToExcel_conversion (intFC, outputName)
File "c:\program files (x86)\arcgis\desktop10.3\arcpy\arcpy\conversion.py", line 81, in TableToExcel
raise e
ExecuteError: Failed to execute. Parameters are not valid.
ERROR 000814: Invalid file type
Failed to execute (TableToExcel).
I think I might be incorrectly referencing the outputName parameter?
Ok- resolved this as well- for anyone following along, the output name needed to be concatenated with a ".xls"
Not to mention I dropped a letter in the initial outputDest variable... whoops. Now I've gotta completely rethink the whole thing as apparently excel output is gonna lead to trouble down the road with any ridiculously long WKT geometries. Thanks everyone for your help.
Your error says, "Parameters are not valid." So, what are the parameters?
print(intFC, outputName)
Next step, check the help doc and make sure the parameters are exactly the correct data type: Table To Excel—Conversion toolbox | ArcGIS Desktop
edit: the line specifying your workspace is probably wrong. It should be the following, to use the variable value, not the string "outputDest":
arcpy.env.workspace = outputDest
To be honest, environment variables are sometimes not worth the hidden trouble they can cause if not used carefully.
Can you try specifying a completely different value for in_features and out_name? It may be trying to read/write the same file.
I wasn't sure if that might be contributing- essentially I want to take a shapefile in a folder location and create a temporary representation in the Default.gdb, but I'm now wondering if the workspace environment is being set to the folder location of the shapefile when it takes that input. If there was a way to work with the feature class representation without writing it to disk that would be ideal. I'm looking at the CopyFeatures option now as suggested by Dan but again it seems that workspace environment is being defined by the input.
Robin, what is the value for 'inputshp'? Is it a path or just a filename?? If a filename, does it contain the .shp extension?? That might be causing the error, i.e. the FeatureClassToFeatureClass_conversion is trying to create a featureclass named something.shp in the file geodatabase. You can't have a '.' in the featureclass name.
Hope this helps, Matthew
Thanks Matthew, looks like that did the trick. Thanks as well Darren, I think you were essentially saying the same thing. Few more bugs to iron out now but at least it's getting past the first function!