Bolton-and-Menk-GIS / restapi

Python API designed to work externally with ArcGIS REST Services to query and extract data, and view service properties. Uses arcpy for some functions if available, otherwise uses open source alternatives to interact with the ArcGIS REST API. Also includes a subpackage for administering ArcGIS Server Sites.
GNU General Public License v2.0
93 stars 31 forks source link

How can I check to see if a feature has an attachment? #9

Closed boesiii closed 6 years ago

boesiii commented 6 years ago

Can I use restapi to check if a feature has an attachment?

CalebM1987 commented 6 years ago

Absolutely, this is very easy with restapi. While there is nothing such as Feature.hasAttachemnt, both the MapServiceLayer and FeatureLayer have a method to find attachments. See the following sample:

import restapi
import os

# sample esri layer for prominent peaks
url = 'https://services.arcgis.com/V6ZHFr6zdgNZuVG0/arcgis/rest/services/Prominent_Peaks_attach/FeatureServer/0'
lyr = restapi.FeatureLayer(url)

# check for attachments via OID (you can get a list of all OIDs by calling lyr.getOIDs() )
# OID 760 does NOT have an attachment at the time of me writing this sample
atts = lyr.attachments(760) #second param is gdbVersion if using different versions of database

if atts:
    # there are attachments, do something here such as download
    for att in atts:
        # first parm of download() is out location, second (optional) is output name otherwise defaults to att_name
        att.download(os.path.join(os.path.expanduser('~'), 'Desktop'))

else:
    print('no attachments found')

You can easily iterate though all features to report if they have attachments or not by calling lyr.getOIDs(). One thing to note here, checking for attachments can get a little chatty as it has to query the server for every OID checked. I hope this helps!