Problem with AddJoin - "The Value Cannot Be A Table"

2303
11
Jump to solution
04-26-2020 03:40 PM
Katie_Clark
MVP Regular Contributor

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!

Best,
Katie


“The goal is not simply to ‘work hard, play hard.’ The goal is to make our work and our play indistinguishable.”
- Simon Sinek
0 Kudos
11 Replies
MatthewWoodmansee
New Contributor III

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!

import arcpy
from arcpy import da
import os

inTable = arcpy.GetParameterAsText(0# attachment table name (_ATTACH)
fileLocation = arcpy.GetParameterAsText(1# folder of where the files will end up going
featureTable = arcpy.GetParameterAsText(2# feature class holding the data we need.
ProjectNum = arcpy.GetParameterAsText(3# project number user wants to export for

#featureTable = r"R:\Connections\GIS_Shared_Data.sde\GIS_shared_data.DBO.GlobalData_UTM\GIS_shared_data.DBO.AOC_testing"
featureFields = ['OBJECTID''ProjectNumber']

#use list comprehension to build a dictionary from SearchCursor
featureDict = {r[0]:(r[1:]) for r in arcpy.da.SearchCursor(featureTable, featureFields)}

#print featureDict

#related table, and the fields we want from that table
#inTable = r'R:\Connections\GIS_Shared_Data.sde\GIS_shared_data.DBO.AOC_testing__ATTACH'
TableFields = ['REL_OBJECTID''DATA''ATT_NAME']

#location of the folder where we place the attachments
#fileLocation = r'C:\Users\xxxxx\Desktop\TestingData\AttachmentTest'

#now run through the dictionary and create the files
with da.SearchCursor(inTable, TableFields) as cursor:
    for item in cursor:

        attachment = item[1]

        #item[0] is the unique ID (OBJECTID), take first item in Dictionary
        fileName1 = featureDict[item[0]][0]
        #print fileName1

        fileName2 = str(item[2]) #uses the "photo 1" text
        #print fileName2

        #add the ObjectID to the final version, then concat project number and attachment name
        fileNameFinal = str(item[0]) + "_" + str(fileName1) + "_" + fileName2
        #print fileNameFinal

        if fileName1 == ProjectNum:
            open(fileLocation + os.sep + fileNameFinal, 'wb').write(attachment.tobytes())

        del item
        del attachment
        del fileName1
        del fileName2
        del fileNameFinal

    del cursor
DavidPike
MVP Frequent Contributor

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.