I am using Arcgis Desktop 10.4.1 and Arcgis Collector.
I've followed the guide for exporting attachments as described on ESRI's webpage here: http://support.esri.com/technical-article/000011912
It works really well, but I want to change the filenames. I want each filename to have the name from a field from the shapefile, and then a suffix (like _1,_2,_3)
I did fiddle a bit with this line, trying to add field names and whatnot, but I didnt get it to work.
filename = filenum + str(item[1])
Anyone able to point me in the right direction?
Script from link:
import arcpy
from arcpy import da
import os
inTable = arcpy.GetParameterAsText(0)
fileLocation = arcpy.GetParameterAsText(1)
with da.SearchCursor(inTable, ['DATA', 'ATT_NAME', 'ATTACHMENTID']) as cursor:
for item in cursor:
attachment = item[0]
filenum = "ATT" + str(item[2]) + "_"
filename = filenum + str(item[1])
open(fileLocation + os.sep + filename, 'wb').write(attachment.tobytes())
del item
del filenum
del filename
del attachment
Answer
To create a filename based on a field value in your feature class (rather than in the attachment table which your script is based on) you will need to get a dictionary of ObjectID and associated value from your specified field in your feature class, then another dictionary to count usage (as the values are used and files output).
I've based the increments on the field value - If the value is MyFieldValue
and the file is the 5th file to be output with that value, the filename will be MyFieldValue_5
. If the next file is the first one called YourFieldValue
it would output as YourFieldValue_1
.
import arcpy, os
from collections import defaultdict
inFC = r'D:\GIS\SE\GISSE.gdb\FCWithAtt' # Feature Class
inTable = r'D:\GIS\SE\GISSE.gdb\FCWithAtt__ATTACH' # Attachment table
fileLocation = r'D:\GIS\SE\TestOutput' # Output location
# Get dictionary of ObjectID and associated field value
myFeatures = dict()
with arcpy.da.SearchCursor(inFC, ['OID@', 'TextField']) 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
For example I created a point feature class with three features. Note the values in TextField
I attached some images to each feature, including 5 images attached to a single feature. See in the field REL_OBJECTID
that there are five images attached to feature class record with OBJECTID = 3
.
The above script output those seven attachments, incrementing numbers on the filenames based on the field value.
No comments:
Post a Comment