AnsweredAssumed Answered

arcpy.CopyFeatures and gdb Attachments

Question asked by tony.farndon on Jan 15, 2015

Hi

 

Does anyone have any clever ideas (or something obvious I am missing) to properly copy across gdb attachments in a python script.

 

So I have a featureclass in an Enterprise GDB with gdb attachments enabled, and I want to export a subset of these features into an fgdb, and bring along all the gdb attachments with it.

 

If I use a simple copy/paste eg arcpy.Copy_management(), this brings across all 000,000's of the features and the GB's of attachments. I cannot find a way to have arcpy.Copy work on a subset selection?

 

Using arcpy.CopyFeatures works great for the export of the featureclass, my makeFeatureLayer call is giving me just the subset I need. However arcpy.CopyFeatures does not bring in the gdb attachments. So what I tried is enabling attachments on the copyFeatures new layer, then arcpy.append from the old gdb attach table to the new.

 

sourceFC = "someegdb.sde\\someSourceFeatureclass"
targetFC = "somefgdb.gdb\\someFeatureClassSubset"
layerName = "selectionLayer"
expression = "foo=999"
arcpy.MakeFeatureLayer_management(sourceFC, layerName, expression)
arcpy.CopyFeatures_management(layerName,targetFC)
arcpy.EnableAttachments(targetFC)
attachTempTable = "attachmentSubSelection"
attachTargetTable = targetFC+"__attach"
where = 'where REL_GLOBALID in (SELECT "GLOBAL_ID" FROM %s %s)' % (sourceFC ,expression)
arcpy.MakeTableView_management(sourceFC+"__ATTACH", attachTempTable, where)
arcpy.Append_management(attachTempTable, attachTargetTable, "NO_TEST")

 

 

The biggest problem I am finding with this approach is the way gdb attachments uses the GLOBAL_ID / REL_GLOBALID. arcpy.CopyFeatures() just recreates all globalId's in the new layer, but arcpy.Append has brought in the old REL_GLOBALID's.

 

A rather clunky workaround I am using is to use the feature centroid to update the REL_GLOBALID, so I go through the sourceFeatureclass, record the old globalId and old centroid

 

centroidDict = {}
with arcpy.da.SearchCursor(sourceFC,["GLOBAL_ID","SHAPE@XY"],expression) as cursor:
    for row in cursor:
        centroidDict[str(row[1])] = row[0]
del row, cursor

 

Then get the new global id based on centroid

 

newCentroidDict = {}
with arcpy.da.SearchCursor(targetFC,["GLOBAL_ID","SHAPE@XY"]) as cursor:
    for row in cursor:
        newCentroidDict[centroidDict[str(row[1])]] = row[0]
del row,cursor

 

And finally update the REL_GLOBALID

with arcpy.da.UpdateCursor(attachTargetTable,["REL_GLOBALID"]) as cur:
    for row in cur:
        row[0] = newCentroidDict[row[0]]
        cur.updateRow(row)
del row,cur

 

Whilst this 'works' there is a clear falecy in the approach, where the features share the same centroid.

 

So, does anyone have any other ideas, ones that mean I am not having to work against/copy entire/alter the enterprise gdb full layer?

Thanks

Outcomes