"Feature attachments" is the new feature from ArcGIS 10 on wards. How to extract feature attachments and save to disk using Python?
from arcpy import da import os with da.SearchCursor(r"c:\temp\demo.gdb\table",['blobFieldname','fileName']) as cursor: for row in cursor: binaryRep = row[0] fileName = row[1] # save to disk open(r"c:\saveFolder" + os.sep + fileName, 'wb').write(binaryRep.tobytes()) del row del binaryRep del fileName
Hi Xander,
Thanks for your response.
Actually I was looking about "Feature Attachments", which involves a relationship table to store the attachments for features.
Please go through "Query attachments" section in below link, which talks about performing the same task in .Net.
http://help.arcgis.com/en/sdk/10.0/arcobjects_net/conceptualhelp/index.html#//0001000001qr000000
from arcpy import da import os # fc = r"C:\Project\_LearnPython\Attachments\test.gdb\yourFCname" tbl = r"C:\Project\_LearnPython\Attachments\test.gdb\yourFCname__ATTACH" fldBLOB = 'DATA' fldAttName = 'ATT_NAME' outFolder = r"C:\Project\_LearnPython\Attachments" with da.SearchCursor(tbl,[fldBLOB,fldAttName]) as cursor: for row in cursor: binaryRep = row[0] fileName = row[1] # save to disk open(outFolder + os.sep + fileName, 'wb').write(binaryRep.tobytes()) del row del binaryRep del fileName
How would I go about added the relationship data into my python script.
"If you want to access the feature itself, you will have to use the "REL_OBJECTID" field from the attachment table and query the featureclass on OBJECTID = REL_OBJECTID..."
I know you mentioned this way, but since it is a relationship does it need to be pathed to a geodatabase? Or how does it pull the relationship information with the pictures?
def main(): global arcpy import arcpy, os # fixed settings fldBLOB = 'DATA' fldAttName = 'ATT_NAME' fldRelOID = 'REL_OBJECTID' # your settings (edit these) fc = r"C:\Project\_LearnPython\Attachments\test.gdb\Meldingen" tbl = r"C:\Project\_LearnPython\Attachments\test.gdb\Meldingen__ATTACH" outFolder = r"C:\Project\_LearnPython\Attachments" fldRelatedInfo = 'SomeFieldInFeatureClass' # should be valid column in FC with arcpy.da.SearchCursor(tbl,[fldBLOB,fldAttName,fldRelOID]) as cursor: for row in cursor: binaryRep = row[0] fileName = row[1] relOID = row[2] # access related information myRelatedInfo = QueryRelatedData(fc, fldRelatedInfo, relOID) # do something with the related info # save attachment to disk open(outFolder + os.sep + fileName, 'wb').write(binaryRep.tobytes()) del row del binaryRep del fileName def QueryRelatedData(fc, fldRelatedInfo, relOID): fldOID = 'OBJECTID' expression = arcpy.AddFieldDelimiters(fc, fldOID) + " = {0}".format(relOID) with arcpy.da.SearchCursor(fc, (fldOID, fldRelatedInfo), where_clause=expression) as cursor: for row in cursor: return row[1] break del row if __name__ == '__main__': main()
Hi Xander,
I'm working with this code sample in 10.2 to access related information from downloaded attachments. The first problem I'm running into is that my myFeatureClass_ATTACH has no REL_OBJECTID field, only a REL_GLOBALID field. When I adjust the code to use the expression to match the global id's instead of the object id's, I'm getting the following error:
I'm not sure why it's still trying to select OBJECTID, which is maybe the problem (?) -- anyway, see my modified code below. Please let me know if you have any ideas. Any guidance is much appreciated! Thanks -
Trill
def main(): global arcpy import arcpy, os # fixed settings fldBLOB = 'DATA' fldAttName = 'ATT_NAME' fldRelGID = 'REL_GLOBALID' # your settings (edit these) ## related feature class fc = r"C:\Forstmobil_2014.12.01\Geodaten\Forstmobil_2014.12.01.gdb\Baumkontrolle_AGS_online" ## attachment table tbl = r"C:\Forstmobil_2014.12.01\Geodaten\Forstmobil_2014.12.01.gdb\Baumkontrolle_AGS_online__ATTACH fldRelatedInfo = 'CreationDate' # should be valid column in FC with arcpy.da.SearchCursor(tbl,[fldBLOB,fldAttName,fldRelGID]) as cursor: for row in cursor: binaryRep = row[0] fileName = row[1] relGID = row[2] # access related information myRelatedInfo = QueryRelatedData(fc, fldRelatedInfo, relGID) print myRelatedInfo def QueryRelatedData(fc, fldRelatedInfo, relGID): fldGID = 'GlobalID' expression = arcpy.AddFieldDelimiters(fc, fldGID) + " = {0}".format(relGID) with arcpy.da.SearchCursor(fc, (fldGID, fldRelatedInfo), where_clause=expression) as cursor: for row in cursor: return row[1] break del row if __name__ == '__main__': main()
Strange, in your code there is no reference to the OBJECTID field. Maybe due to the attachments it is trying to include it, but if the relationship is based on GlobalID, then there will be no OBJECTID (?).
The only thing I saw missing is the quote at the end of line 14 (but I think that is a copy error).