vangelo-esristaff

When is a FID not a FID?

Blog Post created by vangelo-esristaff Employee on Jan 8, 2015

Actually, that turns out to be a trick question -- The answer is never.

 

I had some ArcObjects Java code that needed to make XML from a shapefile or file geodatabase table.  It turns out that the easiest way to get shapefile to XML is by copying the shapefile to a file geodatabase via FeatureClassToFeatureClass (as a GPTool), then using a GdbExporter (IGdbXmlExport interface) to create the XML.  Then my client threw in a twist: Sometimes, only the schema is necessary.  So sometimes I'll be copying a million row shapefile to FGDB, and then not using those rows when the XML is generated.  It saves on XML file size and processing time to go straight to schema (gdbXmlExport.exportDatasetsSchema(...) vice gdbXmlExport.exportDatasets(...)), but how to save time on the copy as well?

 

The answer is to apply a WHERE clause to the FeatureClassToFeatureClass GPTool which results in no rows.  But there's still a hitch:  What WHERE clause to use?

 

It turns out that ArcObjects honors the old SQL trick for selecting zero rows ("WHERE 1 = 0") with file geodatabase, but not for shapefile.  In theory, using the objectid column with a negative value should work, but using FeatureClass.getOIDFieldName() turns out to be surprisingly convoluted when you start with a simple String name.

 

The solution turned out to be simpler than I had feared: Shapefiles have a virtual column named "FID"  (zero-based row number).  This is true of both J Random Shapefile and a shapefile that had a string "FID" column defined in an attempt to break a "FID < 0" query -- The dBase FID field is renamed (to "FID_1" in my case), and the virtual FID is retained.  So in the end I used an 'if' test to set the WHERE clause, "1 < 0" for FGDB and "FID < 0" for shapefile, quick and easy.

Outcomes