How to modify script to get photos of collector with desire filename

918
3
Jump to solution
08-29-2021 09:15 PM
MaryamPanji
New Contributor III

I’m trying to export attachments (photos) with area_ID at the begining.

I have a feature class, a table that has _ATTACH in its name, and a relationship class. I use _ATTACH table and http://support.esri.com/technical-article/000011912 to export photos to a local folder. The name of photos look like this:

17_Photo1.jpg

18_Photo2.jpg

19_Photo3.jpg

20_Photo4.jpg

I want to get ride of 17,18,19 and 20 but add a field from the feature class at the beginning of the photos name.

How can I do that?

The field is area_ID and I want the photo names to become like this:

2AFD_Photo1.jpg

2AFD_Photo2.jpg

2AFD _Photo3.jpg

2AFD _Photo4.jpg

The script I use based on http://support.esri.com/technical-article/000011912 

The script:
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 = 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

 

['DATA', 'ATT_NAME','ATTACHMENTID'] all these fields are in -ATTACH table and I want to add the area_ID instead of 'ATTACHMENTID' which is in the origin feature class.

 

Thanks

0 Kudos
1 Solution

Accepted Solutions
DougBrowning
MVP Esteemed Contributor

This is the way.  Just do a join then you can grab the other field.  I do it like this.

# to get the names and PrimaryKey
photoForm = "Photos"
# actual attachements table
attachName = "Photos__ATTACH"

outDir = r"C:\tmp\Photos"

# should be standard
fldBLOB = 'DATA'
fldAttName = 'ATT_NAME'

dateLoadedInDb = "2020-09-01"

# vars--------------------

print "Starting"

# join to  form to get the PrimaryKey and state info
# Note the first year they called it PlotKey instead
arcpy.MakeTableView_management(inDB + "\\" + attachName, "attachView")
arcpy.AddJoin_management("attachView", "REL_GLOBALID", inDB + "\\" + photoForm, "globalid")
# funky due to the join
fieldList = [attachName + "." + fldBLOB, attachName + "." + fldAttName, photoForm + ".PlotKey", photoForm + ".Office", photoForm + ".filename_T1", photoForm + ".filename_T2", photoForm + ".filename_T3" \
            ,photoForm + ".filename_Soil", photoForm + ".MiscPhoto1filename", photoForm + ".MiscPhoto2filename", photoForm + ".MiscPhoto3filename", photoForm + ".MiscPhoto4filename", photoForm + ".MiscPhoto5filename"]
with arcpy.da.SearchCursor("attachView", fieldList) as cursor:
   for row in cursor:
      binaryRep = row[0]
     # full access to fields here
    

View solution in original post

3 Replies
David_Brooks
MVP Regular Contributor

Off the top of my head, you need to create a field in your attachment table into which you'll copy your feature class name values. Then you. Wed to add a table join between the FC Global ID and your attachment tables RELGLOBALID and then field calc the values across. Then you can rename your attachments accordingly via your script 


David
..Maps with no limits..
DougBrowning
MVP Esteemed Contributor

This is the way.  Just do a join then you can grab the other field.  I do it like this.

# to get the names and PrimaryKey
photoForm = "Photos"
# actual attachements table
attachName = "Photos__ATTACH"

outDir = r"C:\tmp\Photos"

# should be standard
fldBLOB = 'DATA'
fldAttName = 'ATT_NAME'

dateLoadedInDb = "2020-09-01"

# vars--------------------

print "Starting"

# join to  form to get the PrimaryKey and state info
# Note the first year they called it PlotKey instead
arcpy.MakeTableView_management(inDB + "\\" + attachName, "attachView")
arcpy.AddJoin_management("attachView", "REL_GLOBALID", inDB + "\\" + photoForm, "globalid")
# funky due to the join
fieldList = [attachName + "." + fldBLOB, attachName + "." + fldAttName, photoForm + ".PlotKey", photoForm + ".Office", photoForm + ".filename_T1", photoForm + ".filename_T2", photoForm + ".filename_T3" \
            ,photoForm + ".filename_Soil", photoForm + ".MiscPhoto1filename", photoForm + ".MiscPhoto2filename", photoForm + ".MiscPhoto3filename", photoForm + ".MiscPhoto4filename", photoForm + ".MiscPhoto5filename"]
with arcpy.da.SearchCursor("attachView", fieldList) as cursor:
   for row in cursor:
      binaryRep = row[0]
     # full access to fields here
    
MaryamPanji
New Contributor III

Thanks David and Doug,

 

Yes, I created a view and used join as you suggested and used the script above with a minor change, as I wanted the script creates a folder based on area_ID. I put my final script in case someone wants to create folders for records for each site: with the below script you don't need to create a script tool. Just open arcpy in arcmap and run it.

#import arcpy
import os

arcpy.env.overwriteOutput= True

photoForm = "SiteVisits"
attachName = "SiteVisits__ATTACH"

outDir = r'\\GIS\geo$\G\Temp'

def create_dir(filename):
    if not os.path.exists(os.path.dirname(filename)):
        try:
            os.makedirs(os.path.dirname(filename))
        except OSError as exc: 
            if exc.errno != errno.EEXIST:
                raise

print "Starting"


arcpy.MakeTableView_management(attachName, "attachView")
arcpy.AddJoin_management("attachView", "REL_GLOBALID", photoForm, "GLOBALID")
fieldList = [attachName + ".DATA", attachName + ".ATT_NAME", photoForm + ".Area_ID", photoForm + ".created_date"]
with arcpy.da.SearchCursor("attachView", fieldList) as cursor:
   for row in cursor:
        binaryRep = row[0]
        filenum = str(row[1]) 
        filename = r"{}\Photos\{}\{}".format(outDir, row[2], str(row[3])[:4] +"_" + filenum)
        create_dir(filename)
        open(filename, 'wb').write(binaryRep.tobytes())
        del row
        del filenum
        del filename
        del binaryRep

 

0 Kudos