Select to view content in your preferred language

Extract Feature Attachments

21183
32
10-24-2013 01:13 AM
Ranga_Tolapi
Frequent Contributor
"Feature attachments" is the new feature from ArcGIS 10 on wards. How to extract feature attachments and save to disk using Python?
Tags (2)
0 Kudos
32 Replies
TrilliumLevine1
Deactivated User

I figured out the problem with the expression -- extra quotes are needed around the curly braces.  Line 28 (or 35 in your snippet) should therefore read:

expression = arcpy.AddFieldDelimiters(fc, fldOID) + " = '{0}'".format(relOID)

0 Kudos
XanderBakker
Esri Esteemed Contributor

Yeah that makes sense. Glad you figured it out.

0 Kudos
BillSpiking__GISP
Frequent Contributor

Hi Xander!

I know this thread is quite old, but I thought I'd give it a try anyway.  I'm trying to implement your "QueryRelated Data" function in 10.3 and I'm getting the runtime errors below. 

Runtime error

Traceback (most recent call last):

  File "<string>", line 36, in <module>

  File "<string>", line 17, in main

RuntimeError: cannot open 'C:\PhotoLog\AGO\gdb\GPO_PhotoLog.gdb\Photo_Video_Documentation_ATTACH'

Has anything changed with 10.3 that would cause these errors?  I've provided my code as well.  Any help would be greatly appreciated!

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:\PhotoLog\AGO\gdb\GPO_PhotoLog.gdb\Photo_Video_Documentation" 

    ## attachment table 

    tbl = r"C:\PhotoLog\AGO\gdb\GPO_PhotoLog.gdb\Photo_Video_Documentation_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()

0 Kudos
XanderBakker
Esri Esteemed Contributor

Hi William Spiking ,

Just did a test with 10.3 and the code (stil) works. I do notice that in the name of the attachment table you have a single underscore:

'C:\PhotoLog\AGO\gdb\GPO_PhotoLog.gdb\Photo_Video_Documentation_ATTACH'

This should be a double underscore __ATTACH:

'C:\PhotoLog\AGO\gdb\GPO_PhotoLog.gdb\Photo_Video_Documentation__ATTACH'

Please try again with that little correction.

0 Kudos
BillSpiking__GISP
Frequent Contributor

That did the trick!  Can't believe I missed that!

Thank you!

0 Kudos
XanderBakker
Esri Esteemed Contributor

It is a common mistake as it happened to me too. Due to this kind of errors, I normally tend to copy the path from the source tab of the layer or table to be sure to have the right path and name of the object class,

0 Kudos
XanderBakker
Esri Esteemed Contributor
Would you be able to extend your code in python to be able to access  images already stored as a BLOB field in SQL Server 2012 (non-GIS) as  attachments without having to copy the images into ESRI feature classes  thus eliminating data duplication of attachment files?      


Hi Michael,

Could you explain this a little more? Do you have images stored in SQL Server 2012, and you want to use them as attachment, without the need to convert them to an Esri attachment table? If that's the case, I don't think ArcGIS will recognize them as attachments.

If you would like to extract the BLOB attachments stored in SQL Server 2012 by code, I think you should be able to do so. If the SQL Server 2012 table can be accessed by ArcGIS, you can loop through it and extract the attachments (like in the code, save it to disk).

After saving the files to disk, I guess you can use "Generate Attachment Match Table (Data Management)" and Add the attachments (which would duplicate or "triplicate" the data). More on this in the Help topic "Working with the Attachments geoprocessing tools".

Not sure, if this is the answer you are looking for.

Kind regards,

Xander
0 Kudos
MichaelVolz
Esteemed Contributor
I'm not a Database Administrator (DBA), but the DBAs at my organization are always trying to minimize unnecessary data duplication which seems what is occurring with the attachments if you already have the data stored in another format other than feature classes that recognize attachments.

Don't you think it would be more efficient if attachments could just be created in the native format that the original attachment files were stored in?
0 Kudos
XanderBakker
Esri Esteemed Contributor
Hi Michael,

The DBAs (and you) are right; data duplication creates a lot of extra work (apart from the additional data storage). Maybe in the future we'll see some extra functionality in ArcGIS allowing us to create attachments in the form of "hyperlinks" to existing data.

I see two ideas that kinda want the same as you're proposing:
Geodatabase Attachments for large organizations
Geodatabase Attachments Storage Location Option

I think we should promote those ideas.

Kind regards,

Xander
0 Kudos
MichaelVolz
Esteemed Contributor
I had already promoted both of these ideas weeks ago.  Now I am hoping that other GIS users will join the bandwagon so that ESRI see the need for this additional functionality.