0

I am using ArcGIS Desktop 10.4.1 and ArcGIS Collector.

I've tried to follow the answer given to my last question here about naming attachments from Collector. When I run the script, I get the error message:

Traceback (most recent call last):
  File "D:\extract.py", line 20, in <module>
    for row in cursor:
RuntimeError: A column was specified that does not exist

I see from other users that have this problem that the problem can be related to the naming of the column (that I haven't named it correctly), but I can't find out the right way.

This is my script as it stands now:

import arcpy, os
from collections import defaultdict


inFC = r'D:\Drammen.gdb\Drammen' # Feature Class 
inTable = r'D:\Drammen.gdb\Drammen__ATTACH' # Attachment table
fileLocation = r'D:\Test' # Output location 

# Get dictionary of ObjectID and associated field value
myFeatures = dict()
with arcpy.da.SearchCursor(inFC, ['OID@', 'Sefrakid']) 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, ['DATA', 'ATT_NAME', 'ATTACHMENTID', '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

And this is the table from the FC (I want the column Sefrakid to be the name for the jpgs): enter image description here

And this is how the ATTACH table looks:

enter image description here

I do notice that compared to Midavalos answer in my recent post, I do have some REL_GLOBALID which he doesn't. Maybe that has something to do with it?

I am (as you now know) very inexperienced with Python (or programming in general), so it's not easy for me to see obvious errors in the script.

Can anyone point me to where the mistake is?

6
  • Counting the error msg line number "20", the issue seems to be on for loop because of the fields defined in this statement, with arcpy.da.SearchCursor(inTable, ['DATA', 'ATT_NAME', 'ATTACHMENTID', 'REL_OBJECTID']) as cursor: It may be REL_OBJECTID should be REL_GLOBALID
    – artwork21
    May 5, 2017 at 13:27
  • This worked, and I didn't get any error message. Sadly though, no images got extracet either. I ran the script from execfile(r'c:\my\script.py') in the python window. Is there a way to go thouroghly through the logs to see what it tried to do (and failed)? May 8, 2017 at 10:18
  • I think you may have a logic error somewhere, use print or msg statements line by line and check the variable values.
    – artwork21
    May 8, 2017 at 12:21
  • So that was helpful. When i print cursor, I get <da.SearchCursor object at 0x1ADEC448> - which is rather cryptic. However, when I print row, I get <memory at 0x1AA1D5D0>, u'46_0_ArcGISApp_1493913172455.jpg', 1, u'{B8FAEC96-F1B0-4260-8910-248C0CD51E96}') along with all other pictures I have as attachments. But all other values after that in my script will print nothing. So I guess it's the wrong row value or something with row[3] ? May 8, 2017 at 20:08
  • 1
    The row number is dictated by how you order them in the cursor fields list, eg. myList = ['rowHello', 'rowTest', 'rowTest2']. row[0] equals rowHello, row[1] equals rowTest.....
    – artwork21
    May 8, 2017 at 20:16

2 Answers 2

1

I'm afraid that you've fallen victim to the Mystery Attachment Table Schema. The format that ArcGIS "usually" uses for __ATTACH files is this:

enter image description here

However, under certain unknown conditions, the __ATTACH file format that is created by the Add Attachments tool comes out like this:

enter image description here

Technicians who experience the mystery __ATTACH file format swear that they've created the file through the Add Attachments tool like they've always done. It may be a case of differing ArcGIS Desktop versions, but I've not investigated that.

The really weird thing is that the second __ATTACH file format is actually much better to use. Relationships built using a feature class objectid are eventually bound for a bad breakup. :-)

0

As my comments noted:

  • Counting the error msg line number "20", the issue seems to be on for loop because of the fields defined in this statement, with arcpy.da.SearchCursor(inTable, ['DATA', 'ATT_NAME', 'ATTACHMENTID', 'REL_OBJECTID']) as cursor: It may be REL_OBJECTID should be REL_GLOBALID
  • The row number is dictated by how you order them in the cursor fields list, eg. ['DATA', 'ATT_NAME', 'ATTACHMENTID', 'REL_GLOBALID']. row[0] equals DATA, row[1] equals ATT_NAME, row[2] equals ATTACHMENTID.....
1
  • I think I marked this as solved too soon. I got to export pictures, but they just got named after the first row in "Sefrakid" (row[1]) and it made a counter, so the next one got named as the second row. This is wrong, as many of the rows dont have pictures, and it doesnt do multiple pictures for each object (like _2,_3). I think the script in itself was correct, but that it fails to recognize REL_GLOBALID, or check them against eachother. If there was someway to convert REL_GLOBALID to OBJECTID as in the tutorial I was referring to, or rename all the same characters in REL_GLOBALID? May 9, 2017 at 15:23

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.