1

I'm trying to export photo attachments from a railroad crossings dataset taken with ArcGIS Collector, and would like to customize the naming convention so that the file name contains the crossing number. I stumbled across a script from @Midavalo that accomplishes this, however the files exported are no longer JPEG format. I have almost no knowledge of Python.

Feature table:

enter image description here

Attachment table:

enter image description here

Modified script:

import arcpy, os
from collections import defaultdict

inFC = r'C:\Users\Desktop\scratch\CustomExportAttachments\CopyFeatures.gdb\Crossings'
inTable = r'C:\Users\Desktop\scratch\CustomExportAttachments\CopyFeatures.gdb\Crossings__ATTACH'
fileLocation = r'C:\Users\Desktop\scratch\CustomExportAttachments\Attachments'

# Get dictionary of ObjectID and associated field value
myFeatures = dict()
with arcpy.da.SearchCursor(inFC, ['OID@', 'CROSSING']) as cursor:
    for row in cursor:
        myFeatures[row[0]] = row[1]

# Create dictionary to count usage of the field value (to increment files)
valueUsage = defaultdict(int)

# Loop through attachments, incrementing field value usage, and using that
# increment value in the filename
with arcpy.da.SearchCursor(inTable, ['ATTACHMENTID', 'ATT_NAME', 'REL_OBJECTID']) as cursor:
    for row in cursor:
        if row[3] in myFeatures:
            attachment = row[0]
            fieldValue = myFeatures[row[3]] # Value of specified field in feature class
            valueUsage[fieldValue] += 1 # Increment value
            filename = "{0}_{1}".format(fieldValue, valueUsage[fieldValue]) # filename = FieldValue_1
            output = os.path.join(fileLocation, filename) # Create output filepath
            open(output, 'wb').write(attachment.tobytes()) # Output attachment to file

Output folder:

enter image description here

Why are the photo attachments exporting as "File" instead of JPEG, and how should I change the script in order to make this happen?

0

1 Answer 1

3

Best guess: the filename is only the name, no extension. You could probably do a quick addition in the code if you know all the files are JPEG.

Change the 2nd to last line to:

output = os.path.join(fileLocation, filename +".jpg")

This should get your JPEG extension back. Obviously this solution could be a problem if you have different file types you're exporting.

1
  • Thanks @KHibma! How would I change the script to use two or more fields from the feature class in the naming convention? Commented Jan 14, 2021 at 16:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.