AnsweredAssumed Answered

Accessing Feature Class Objects from a Geodatabase with ArcPy

Question asked by tab5ay_uvalibrary on Jul 28, 2019
Latest reply on Jul 29, 2019 by LukeW

Is there a way to access the feature class objects directly from a geodatabase so that they can be passed into a function?

 

I need to convert a batch of .kml files to .gdbs and then remove attribute fields I don't need from the resulting File Geodatabase Feature Classes. To do this I used the KMLToLayer_conversion tool and that worked fine. It created a geodatabase (and .lyrx file) for each .kml file and in ArcGIS Pro the Points and Polylines FGDB Feature Classes show up under a folder called "Placemarks" in the Catolog's folder explorer. Everything is A ok at this point.

 

I then tried to use the DeleteField_management tool to delete the attribute field names I don't want. This tool takes a table, feature class, feature layer, or raster dataset as its first argument and a list of attribute names to be deleted as its second argument. The problem is, I don't know how to access the FGDB Feature Classes in order to pass them to this function. I have tried a bunch of different things to no avail. The following is my code:

 

import arcpy as ap
import os

 

field_list =  ["FolderPath", "SymbolID", "AltMode", "Base", "TimeSpan", "TimeStamp", "EndTime", "Snippet"]

 

ap.env.workspace = "G:\\My Drive\\GDBs"

 

for gdb in ap.ListFiles():
            ap.env.workspace = os.path.join("G:\\My Drive\\GDBs", gdb, "Placemarks")
            ap.DeleteField_management([ap.ListFeatureClasses], "field_list")

 

This gives me the following errors:

 

runfile('G:/My Drive/One Shared Story/Python Scripts/DELETEFIELDS.py', wdir='G:/My Drive/Python Scripts')
Traceback (most recent call last):

File "<ipython-input-7-f9a0af40c03f>", line 1, in <module>
runfile('G:/My Drive/Python Scripts/DELETEFIELDS.py', wdir='G:/My Drive/Python Scripts')

File "C:\Users\Tab5a\AppData\Local\ESRI\conda\envs\arcgispro-py3-clone\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 827, in runfile
execfile(filename, namespace)

File "C:\Users\Tab5a\AppData\Local\ESRI\conda\envs\arcgispro-py3-clone\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)

File "G:/My Drive/Python Scripts/DELETEFIELDS.py", line 13, in <module>
ap.DeleteField_management([ap.ListFeatureClasses], ["FolderPath"])

File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\management.py", line 4449, in DeleteField
raise e

File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\management.py", line 4446, in DeleteField
retval = convertArcObjectToPythonObject(gp.DeleteField_management(*gp_fixargs((in_table, drop_field), True)))

File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\geoprocessing\_base.py", line 506, in <lambda>
return lambda *args: val(*gp_fixargs(args, True))

RuntimeError: Object: Error in executing tool

 

Note that I have tried without the "Placemarks" folder as it does not "exist" if not viewed from ArcGIS Pro. Also note that in the for loop, ap.ListFeatureClasses gives the list ["Points", "Polylines"]. I have also tried to pass the string path into the parameter to no avail (G:\My Drive\GDBs\example.gdb\Placemarks\Points and G:\My Drive\GDBs\example.gdb\Placemarks\Polylines). The problem seems to be converting between an ArcObject and a PythonObject. I'm not sure that the tool is using an object, because the ["Points", "Polylines"] list returned by ap.ListFeatureClasses is populated by strings.

 

Is there a way I can access the feature class object itself and pass it into this function? Or am I just being dense about something here? Any help would be greatly appreciated. 

Outcomes