Hi everyone,
I have a points layer where for each point there is a photo as an attachment. Now, these photos are scattered in several folders and I would like to create one folder that collects all the photos. It would be really annoying to look for each photo, so I was thinking is there a way to download all the photos from a layer in an automatic way? Maybe with a tool or a script? I searched online but I didn't find anything...
I'm using ArcGis Pro 3.0.2
Thank you for the help
Solved! Go to Solution.
Then you should be able to use this simple script in ArcGIS Pro's Python Window:
# edit these variables
layer_name = "Layer"
output_folder = "N:/.../temp"
from pathlib import Path
attachment_table = arcpy.Describe(layer_name).catalogPath + "__ATTACH"
with arcpy.da.SearchCursor(attachment_table, ["DATA", "ATT_NAME"]) as cursor:
for data, name in cursor:
print(f"currently exporting {name}")
out_file = Path(output_folder, name)
out_file.write_bytes(data)
Are we talking about an actual Attachment Table, which is created with the Attachment tools and managed by ArcGIS? Or are we talking about a "normal" feature class with a field that contains a file path?
It is an actual Attachment table
Then you should be able to use this simple script in ArcGIS Pro's Python Window:
# edit these variables
layer_name = "Layer"
output_folder = "N:/.../temp"
from pathlib import Path
attachment_table = arcpy.Describe(layer_name).catalogPath + "__ATTACH"
with arcpy.da.SearchCursor(attachment_table, ["DATA", "ATT_NAME"]) as cursor:
for data, name in cursor:
print(f"currently exporting {name}")
out_file = Path(output_folder, name)
out_file.write_bytes(data)
Hi Johannes,
do you know a way to automatically delete the downloaded attachments?
My problem is the following: I have a layer in ArcGIS online in which photos are uploaded by fieldworkers. In order to reduce the amount of data on ArcGIS online I would like an automatic download and delete of only the images, but the layer should remain online with the fetaures.
You could use Remove Attachments and use the feature layer as both input table and match table.
That does not work. I have now tried to run the comman individualy, unfortunately there is nor reasonable error message, only that the attachments were not removed. Do you know what could be the reason? Editing and synchronisation on the layer are allowed.
This doesn't automatically delete them, but if you connect to the portal in ArcPro and run the python script, it removes the images.
# Delete all attachments
# Instructions from the URL:
# https://community.esri.com/t5/arcgis-online-questions/remove-attachments-in-batches-from-feature-ser...
from arcgis.gis import GIS
import arcgis
from arcgis.features import FeatureLayerCollection
gis = GIS("https://www.arcgis.com", "username", "password") # Replace your username and password
featureService = gis.content.get('feature service layer id')
flayer=featureService.layers[0] #You may need to change layer id from 0 to your own layer number
whereclause = "OBJECTID > 0" #for example "status ='done'" queries features where the value in the "status" field is "done"
for feature in flayer.query(where=whereclause):
f_oid=feature.attributes.get("OBJECTID") #You might need to replace OBJECTID with the name of the objectid field in your dataset.
att_list=flayer.attachments.get_list(oid=f_oid)
if att_list:
for att in att_list:
att_id= att['id']
flayer.attachments.delete(f_oid,att_id)
print(f"Attachments were deleted for feature {f_oid}")
else:
print(f"No attachments found for feature {f_oid}")
print ("Finished")
From here
Working with the Attachments geoprocessing tools—ArcGIS Pro | Documentation
Maybe you can generate an attachment table, to get the file paths, then use that information to consolidate all the images into one folder. Once assembled, produce a new column in the table fixing the path to their new location and then re-adding the attachments.