Select to view content in your preferred language

Loop to all items of a Feature Layer using ArcPy

5119
1
Jump to solution
01-24-2022 10:56 AM
Manu
by
Occasional Contributor

I have found some examples of how to iterate through the contents of a Feature Class. However, this does not work for a Feature Layer:

import arcpy
import arcgis
from arcgis.gis import GIS
gis = GIS(None, arcgis_usr, arcgis_pw, verify_cert = False)

search_result = gis.content.search("ArcGIS_content", "Feature Class")
item = search_result[0]
FeatureLayer = item.layers[0]

with arcpy.da.SearchCursor(FeatureLayer, ["OID@"]) as cursor:
for row in cursor:
    attachments_list = FeatureLayer.attachments.get_list(oid = row[0])
    if len(attachments_list) >= 1:
        print("Found", str(len(attachments_list)), "attachments")

Is there an efficient way to do what I tried here with a FeatureLayer; namely, to iterate through all of its features (in my case point features) and extract information (the idea is to download the attachments and save coordinates and some other attribute values)?

0 Kudos
1 Solution

Accepted Solutions
HuubZwart
Occasional Contributor

You won't need a cursor for that. See feature layer documentation to query for objectIds and then list atttachments here and here

Continuing from your script:

 

import arcpy
import arcgis
from arcgis.gis import GIS
gis = GIS(None, arcgis_usr, arcgis_pw, verify_cert = False)

search_result = gis.content.search("ArcGIS_content", "Feature Class")
item = search_result[0]
fl = item.layers[0]
fs = fl.query(where="1=1", outFields="field1,field2")
oidFieldName = "objectid" # replace when necessary
oids = [f.attributes[oidFieldName] for f in fs.features]
attachments = [fl.attachments.get_list(oid=oid)} for oid in oids]

 

 attachments is a list of lists, where the nested list is shaped like this (for each oid):

 

[{'contentType': 'image/png',
  'id': 1,
  'name': 'AppTemplate.png',
  'parentObjectId': 1,
  'size': 1394},
 {'contentType': 'image/png',
  'id': 2,
  'name': 'AppTemplate.png',
  'parentObjectId': 1,
  'size': 1394},
 {'contentType': 'image/png',
  'id': 4,
  'name': 'AppTemplate.png',
  'parentObjectId': 1,
  'size': 1394}]

 

You can download these using: 

 

import os
local_path = "SOME_FOLDER"
for attachment_list in attachments:
    for attachment in attachment_list:
        fl.attachments.download(save_path=os.path.join(local_path, attachment['name']), oid=attachment['parentObjectId', attachment_id=attachment['id'])

 

View solution in original post

1 Reply
HuubZwart
Occasional Contributor

You won't need a cursor for that. See feature layer documentation to query for objectIds and then list atttachments here and here

Continuing from your script:

 

import arcpy
import arcgis
from arcgis.gis import GIS
gis = GIS(None, arcgis_usr, arcgis_pw, verify_cert = False)

search_result = gis.content.search("ArcGIS_content", "Feature Class")
item = search_result[0]
fl = item.layers[0]
fs = fl.query(where="1=1", outFields="field1,field2")
oidFieldName = "objectid" # replace when necessary
oids = [f.attributes[oidFieldName] for f in fs.features]
attachments = [fl.attachments.get_list(oid=oid)} for oid in oids]

 

 attachments is a list of lists, where the nested list is shaped like this (for each oid):

 

[{'contentType': 'image/png',
  'id': 1,
  'name': 'AppTemplate.png',
  'parentObjectId': 1,
  'size': 1394},
 {'contentType': 'image/png',
  'id': 2,
  'name': 'AppTemplate.png',
  'parentObjectId': 1,
  'size': 1394},
 {'contentType': 'image/png',
  'id': 4,
  'name': 'AppTemplate.png',
  'parentObjectId': 1,
  'size': 1394}]

 

You can download these using: 

 

import os
local_path = "SOME_FOLDER"
for attachment_list in attachments:
    for attachment in attachment_list:
        fl.attachments.download(save_path=os.path.join(local_path, attachment['name']), oid=attachment['parentObjectId', attachment_id=attachment['id'])