I am trying to automatically add multiple photo attachments to a point using Add Attachments.
First I need to use Generate Match Table to create the match table, and am struggling to create a photo path for multiple attachments. My working folder has photos 18_1a, 18_1b, and 18_1c and my matching field is 18. This path works when there is a single "18" named photo, but the rest of the photos do not get picked up. How do I name these so the tool recognizes multiple attachments for my singular "18" point?
Solved! Go to Solution.
The tool only generates a 1:1 table, the match field and file name have to be exactly the same.
With this little script, you can do what you want:
# same parameters as in the tool
in_table = "TestPoints"
in_folder = r"N:\ags1_daten\gewaesserkataster\Projekt\Bearbeitung\test"
out_match_table = r"memory\match2"
key_field = "OBJECTID"
input_data_filter = "*" # eg "*.jpg"
relative_path = True
from pathlib import Path
# create match table
p = Path(out_match_table)
match_table = arcpy.management.CreateTable(str(p.parent), str(p.name))
arcpy.management.AddField(match_table, "Filename", "TEXT")
arcpy.management.AddField(match_table, "MatchID", "LONG")
# get all files that match your filter
in_files = list(Path(in_folder).glob(input_data_filter))
# for each row in in_table, get all files that match the key field or start with key field followed by "_"
with arcpy.da.InsertCursor(match_table, ["MatchID", "Filename"]) as i_cur:
with arcpy.da.SearchCursor(in_table, [key_field]) as s_cur:
for row in s_cur:
key = str(row[0])
for f in in_files:
if f.stem == key or f.stem.startswith(key + "_"):
fn = f.name if relative_path else str(f)
i_cur.insertRow([row[0], fn])
The tool only generates a 1:1 table, the match field and file name have to be exactly the same.
With this little script, you can do what you want:
# same parameters as in the tool
in_table = "TestPoints"
in_folder = r"N:\ags1_daten\gewaesserkataster\Projekt\Bearbeitung\test"
out_match_table = r"memory\match2"
key_field = "OBJECTID"
input_data_filter = "*" # eg "*.jpg"
relative_path = True
from pathlib import Path
# create match table
p = Path(out_match_table)
match_table = arcpy.management.CreateTable(str(p.parent), str(p.name))
arcpy.management.AddField(match_table, "Filename", "TEXT")
arcpy.management.AddField(match_table, "MatchID", "LONG")
# get all files that match your filter
in_files = list(Path(in_folder).glob(input_data_filter))
# for each row in in_table, get all files that match the key field or start with key field followed by "_"
with arcpy.da.InsertCursor(match_table, ["MatchID", "Filename"]) as i_cur:
with arcpy.da.SearchCursor(in_table, [key_field]) as s_cur:
for row in s_cur:
key = str(row[0])
for f in in_files:
if f.stem == key or f.stem.startswith(key + "_"):
fn = f.name if relative_path else str(f)
i_cur.insertRow([row[0], fn])
That worked perfectly thank you so much!!!!
Do you know why in the help documentation for the Add Attachments tool it says you can do multiple? "
Multiple files can be attached to a single feature class or table record. To accomplish this the Match Table should contain multiple records for that input ID (for example, record 1 has an InputID of 1 and a pathname pic1a.jpg, and record 2 has an InputID of 1 and a pathname pic1b.jpg).
You're confusing Add Attachments and Generate Attachment Match Table.
Your bullet point is from Add Attachments and is absolutely correct. This tool can add multiple attachments for the same feature. For that to happen, the feature's key has to occur in multiple records of the match table. This is what my second screenshot shows: ID 1 is there multiple times.
What I was talking about is Generate Attachment Table, which... generates an attachment table. This tool is only able to perfectly match ID and file name. The only way you get multiple records with the same ID here is to have the same filenames, but different file extensions (1.jpg, 1.jpeg, 1.png, etc).
Ah oopsies, yes that makes sense, thank you!
Hello Johannes,
I know this thread is old. But, I am attempting to do the same thing. I just can't figure out how to enter the script into the tool to be used. Any pointers?
You will need to enter it into the Python command window in ArcGIS Pro
Hi Johannes, thank you so much for your code and solution.
I seem to be running into an issue where only the first few records are imported into the match table (in your example, it would be as if only 1.txt, 1_a.txt and 1_b.txt were brought in and 2.txt and subsequent were ignored).
Do you have any idea why this might be happening?
Thanks so much!
Amelia
My guess is that your in_table is a layer that has a selection?