ArcGIS Pro relationship class attachment

835
2
Jump to solution
07-14-2022 12:54 AM
Labels (1)
IlkaIllers1
Occasional Contributor III

I downloaded a feature class with attachments from AGOL using this guide How To: Download attachments from a hosted feature service (esri.com). Then, I downloaded the attachments with the script described here How do I download all ArcGIS Online attachments fr... - Esri Community.

I had to rename all of the values in "ATT_NAME" because they were all called either Photo 1.jpg or Photo 2.jpg.

Now I have been asked whether it is possible to rename them with a name given in a field in the original feature class. I thought it might be possible since they are connected with the relationship class  ("_ATTACHREL"), but can't figure out how. 

I would be very grateful for any help, thanks! 

0 Kudos
1 Solution

Accepted Solutions
RhettZufelt
MVP Frequent Contributor

I use the following code to do just that. 

In this case, all my attachments are .jpg files, so I just append that filename extension.  If you have other/mixed file types, would need to modify to extract that info from the _ATTACH table.

import arcpy
import os

inFeatureClass = r'Database Connections\...\Signs'         # feature class with attachments
pointFieldsList = ['GlobalID','MUTCDcode']                 # fields to load into cursor

global_dict = {}                                           # dictionary to hold globalIDs and "name" field

fileLocation = r'C:\tmp\Attachments'                       # folder where attachments are saved


with arcpy.da.SearchCursor(inFeatureClass, pointFieldsList) as point_cursor:  # cursor to make dict of globalID's
        for row in point_cursor:
          if row[1]:
            row1 = row[1]                                 # I'm using "MUTCDcode" as my name field, but is
          else:                                           # not always populated.  if Null, substitute "NA"
            row1 = "NA"
          global_dict[row[0]] = [row[0],row1]             # add to dict with globallID as key, name as value


inTable = r'Database Connections\SignDatabase.sde\SignsDatabase.DBO.Sign__ATTACH'   #  attachment table for feature class
tableFieldsList = ['REL_GLOBALID', 'DATA', 'ATT_NAME', 'ATTACHMENTID']              # field list in attachment table


with arcpy.da.SearchCursor(inTable, tableFieldsList) as table_cursor:
    for item in table_cursor:
        attachment = item[1]                                                        # grab attachemnt from blob field
        filename = global_dict[item[0]][1] + '_' + str(item[3]) + '.jpg'            # create filename from value in FC dict that has matching GlobalID
        open(fileLocation + os.sep + filename, 'wb').write(attachment.tobytes())    # write the attachment as a file.
        

 

R_

View solution in original post

0 Kudos
2 Replies
RhettZufelt
MVP Frequent Contributor

I use the following code to do just that. 

In this case, all my attachments are .jpg files, so I just append that filename extension.  If you have other/mixed file types, would need to modify to extract that info from the _ATTACH table.

import arcpy
import os

inFeatureClass = r'Database Connections\...\Signs'         # feature class with attachments
pointFieldsList = ['GlobalID','MUTCDcode']                 # fields to load into cursor

global_dict = {}                                           # dictionary to hold globalIDs and "name" field

fileLocation = r'C:\tmp\Attachments'                       # folder where attachments are saved


with arcpy.da.SearchCursor(inFeatureClass, pointFieldsList) as point_cursor:  # cursor to make dict of globalID's
        for row in point_cursor:
          if row[1]:
            row1 = row[1]                                 # I'm using "MUTCDcode" as my name field, but is
          else:                                           # not always populated.  if Null, substitute "NA"
            row1 = "NA"
          global_dict[row[0]] = [row[0],row1]             # add to dict with globallID as key, name as value


inTable = r'Database Connections\SignDatabase.sde\SignsDatabase.DBO.Sign__ATTACH'   #  attachment table for feature class
tableFieldsList = ['REL_GLOBALID', 'DATA', 'ATT_NAME', 'ATTACHMENTID']              # field list in attachment table


with arcpy.da.SearchCursor(inTable, tableFieldsList) as table_cursor:
    for item in table_cursor:
        attachment = item[1]                                                        # grab attachemnt from blob field
        filename = global_dict[item[0]][1] + '_' + str(item[3]) + '.jpg'            # create filename from value in FC dict that has matching GlobalID
        open(fileLocation + os.sep + filename, 'wb').write(attachment.tobytes())    # write the attachment as a file.
        

 

R_

0 Kudos
IlkaIllers1
Occasional Contributor III

Thanks so much!

0 Kudos