<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic arcpy.CopyFeatures and gdb Attachments in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/arcpy-copyfeatures-and-gdb-attachments/m-p/51964#M4125</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Does anyone have any clever ideas (or something obvious I am missing) to properly copy across gdb attachments in a python script.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;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?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;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")&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;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&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;centroidDict = {}
with arcpy.da.SearchCursor(sourceFC,["GLOBAL_ID","SHAPE@XY"],expression) as cursor:
&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in cursor:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; centroidDict[str(row[1])] = row[0]
del row, cursor&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Then get the new global id based on centroid&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;newCentroidDict = {}
with arcpy.da.SearchCursor(targetFC,["GLOBAL_ID","SHAPE@XY"]) as cursor:
&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in cursor:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; newCentroidDict[centroidDict[str(row[1])]] = row[0]
del row,cursor&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;And finally update the REL_GLOBALID&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;with arcpy.da.UpdateCursor(attachTargetTable,["REL_GLOBALID"]) as cur:
&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in cur:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row[0] = newCentroidDict[row[0]]
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cur.updateRow(row)
del row,cur&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Whilst this 'works' there is a clear falecy in the approach, where the features share the same centroid.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;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?&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Fri, 10 Dec 2021 22:00:18 GMT</pubDate>
    <dc:creator>AnthonyFarndon</dc:creator>
    <dc:date>2021-12-10T22:00:18Z</dc:date>
    <item>
      <title>arcpy.CopyFeatures and gdb Attachments</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-copyfeatures-and-gdb-attachments/m-p/51964#M4125</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Does anyone have any clever ideas (or something obvious I am missing) to properly copy across gdb attachments in a python script.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;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?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;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")&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;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&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;centroidDict = {}
with arcpy.da.SearchCursor(sourceFC,["GLOBAL_ID","SHAPE@XY"],expression) as cursor:
&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in cursor:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; centroidDict[str(row[1])] = row[0]
del row, cursor&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Then get the new global id based on centroid&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;newCentroidDict = {}
with arcpy.da.SearchCursor(targetFC,["GLOBAL_ID","SHAPE@XY"]) as cursor:
&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in cursor:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; newCentroidDict[centroidDict[str(row[1])]] = row[0]
del row,cursor&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;And finally update the REL_GLOBALID&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;with arcpy.da.UpdateCursor(attachTargetTable,["REL_GLOBALID"]) as cur:
&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in cur:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row[0] = newCentroidDict[row[0]]
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cur.updateRow(row)
del row,cur&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Whilst this 'works' there is a clear falecy in the approach, where the features share the same centroid.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;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?&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Dec 2021 22:00:18 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-copyfeatures-and-gdb-attachments/m-p/51964#M4125</guid>
      <dc:creator>AnthonyFarndon</dc:creator>
      <dc:date>2021-12-10T22:00:18Z</dc:date>
    </item>
  </channel>
</rss>

