I am attempting to automate a process of exporting photos from a Survey123 geodatabase and give the output a custom filename, modified from the general script for exporting photos provided by Esri. Here is what I have so far. I am trying to join the Feature Layer, image_repeat table and image_repeat__ATTACH table before exporting the photos so that I can use fields that are in the feature class attribute table in the output file names.
import arcpy
from arcpy import da
import os
featureTable = arcpy.GetParameterAsText(0) # the Feature layer
repeatTable = arcpy.GetParameterAsText(1) # the image_repeat table
attachTable = arcpy.GetParameterAsText(2) # the image_repeat__ATTACH table
fileLocation = arcpy.GetParameterAsText(3) # Output folder location for the photos
arcpy.AddJoin_management(repeatTable, "parentglobalid", featureTable, "globalid")
arcpy.AddJoin_management(repeatTable, "Nutrient_Bank_Monitoring.globalid", attachTable, "REL_GLOBALID")
with da.SearchCursor(repeatTable, ['DATA', 'ATT_NAME', 'ATTACHMENTID', 'site', 'plot']) as cursor:
for item in cursor:
attachment = item[0] # blob data type
site = str(item[3])
plotNum = str(item[4])
filenum = "ATT" + str(item[2]) + "_" # i.e. ATT531_
filename = site + "_Plot" + plotNum + "_" + filenum + str(item[1]) #i.e. miyagi_creek_Plot6_ATT531_plot_image-20200324-143302.jpg
open(fileLocation + os.sep + filename, 'wb').write(attachment.tobytes())
del item
del filenum
del filename
del attachment
However, I get the following error message - "The value cannot be a table":
I have no problem doing the joins manually in ArcMap, so I'm unclear why it won't work in the script. Is it possible to do what I'm trying to do?
I am obviously still a python newbie, so many thanks in advance for any help or advice!
Solved! Go to Solution.
Thank you both David and Katherine for your help. I am posting my final code (with commented-out manual data tests and print statements) in case this will help anyone else. I added in an "If" statement for the user to chose a project number (there will be lots of different projects using this one Collector application). I also used a lot of the code from another thread (I had forgotten how helpful list comprehensions are!: Is it possible to use a field for the filename of an attachment export?
Here is my code - thanks again!
You can reference the data via a cursor through the dataset path itself, not even sure if it works on a layer. Also save the layer after the join then use that path in the cursor.
for fields input into the cursor you can pass in a list of field names as strings:
cursor_fields = ["Field1", "Field2", "Field3"]
with arcpy.da.cursor(path, cursor_fields) as cursor...
throw some print statements into the variables you're creating in the cursor, my guess is your final path is ending up a bit funky after concatenation, i'd use os.path.join instead.