Tuesday 22 September 2015

python - Exporting features with attachments for use outside ArcGIS?



ArcGIS 10 features the new ability to add attachments to individual features to feature classes stored in a geodatabase. One use of this would be to add multiple pictures to a specific location, fire hydrants seem to be a popular example (important to know if you're a dog on the internet in need of a pit stop I suppose).


In the database the structure of an attachment enabled feature class is trifold:



  • the feature class itself,

  • a table holding the attachments and associated metadata (filename, size, etc), and

  • a relationship class defining the 1-to-many relationship between the two.


the 3 items that comprise an attachment-enabled feature class


My question is: how to export this data -- feature class, attachments, attachment metadata -- for consumption outside of Arcgis?*


Exporting the FC is easy, FeatureClassToFeatureClass works the same as on a non-attachment enabled FC. Exporting the table of attachments? Not so much. TableToTable to output.dbf extracts the attached file metadata only, to output.csv errors out with unsupported field type, and to output info table fails with a general field error.



This isn't all that surprising as those file types don't support a binary blob datatype. I was expecting/hoping to find a tool which would convert the attached binaries into their native format, e.g.


table to file system diagram


So, how about it? What do I need to do to get the data out?


* ironically my project of the moment which spawned this Q is to export for use within Arcgis, just not Arcgis Desktop...



Answer



http://support.esri.com/em/knowledgebase/techarticles/detail/41763


For ArcGIS 10.1+


from arcpy import da
import os


inTable = arcpy.GetParameterAsText(0)
fileLocation = arcpy.GetParameterAsText(1)

with da.SearchCursor(inTable,['DATA','ATT_NAME']) as cursor:
for row in cursor:
binaryRep = row[0]
fileName = row[1]
# save to disk
open(fileLocation + os.sep + fileName, 'wb').write(binaryRep.tobytes())
del row

del binaryRep
del fileName

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...