Select to view content in your preferred language

How to download images from layer

2100
8
Jump to solution
11-24-2022 01:13 AM
CeciliaMartinelli
New Contributor II

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

0 Kudos
1 Solution

Accepted Solutions
JohannesLindner
MVP Frequent Contributor

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)

 


Have a great day!
Johannes

View solution in original post

0 Kudos
8 Replies
JohannesLindner
MVP Frequent Contributor

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?


Have a great day!
Johannes
0 Kudos
CeciliaMartinelli
New Contributor II

It is an actual Attachment table

0 Kudos
JohannesLindner
MVP Frequent Contributor

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)

 


Have a great day!
Johannes
0 Kudos
Mei
by
New Contributor

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.

0 Kudos
JohannesLindner
MVP Frequent Contributor

You could use Remove Attachments and use the feature layer as both input table and match table.


Have a great day!
Johannes
0 Kudos
Mei
by
New Contributor

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.

0 Kudos
DebbieGoeldner
New Contributor II

https://community.esri.com/t5/arcgis-online-questions/remove-attachments-in-batches-from-feature-ser...

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")

 

0 Kudos
DanPatterson
MVP Esteemed Contributor

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.


... sort of retired...