Last active
July 28, 2023 04:42
-
-
Save tannerjt/ecdc84a0bce0c34e34453a2e6573c6f1 to your computer and use it in GitHub Desktop.
Extract ArcGIS Feature Service Attachments into Field URL's
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Author: Joshua Tanner | |
#Data: 7/7/2017 | |
#Summary: Extract ArcGIS Feature Service Attachments into Field URL's | |
import copy, json, sys | |
from arcgis.gis import GIS | |
photo_field_name = 'attachment_url' | |
USERNAME = '' | |
PASSWORD = '' | |
ITEMID = '4e4e9be76fae4457b9ef5b11356164c8' | |
ago_gis = GIS('https://www.arcgis.com/', USERNAME, PASSWORD) | |
item = ago_gis.content.get(ITEMID) | |
lyr = item.layers[0] | |
data = lyr.query(where='1=1',out_fields='*') | |
maxAttachments = 0 | |
fieldNames = [] | |
def generateUrls(attachments, objectid): | |
urls = [] | |
for item in attachments: | |
urls.append("{}/{}/attachments/{}".format(lyr.url,objectid,item["id"])) | |
return urls | |
def getAttachments(f): | |
objectid = f.attributes['OBJECTID'] | |
attachments = lyr.attachments.get_list(objectid) | |
return attachments | |
def generateAttachmentFieldNames(): | |
fieldNames = [] | |
for i in range(maxAttachments): | |
fieldNames.append(photo_field_name + "_{}".format(i+1)) | |
return fieldNames | |
def removeDups(original, matches): | |
notDups = [] | |
for f in original: | |
if f not in matches: | |
notDups.append(f) | |
return notDups | |
def generateNewFieldDefs(fields): | |
defs = [] | |
for f in fields: | |
d = { "name": f, | |
"editable": True, | |
"alias": f, | |
"type": "esriFieldTypeString", | |
"length": 256, | |
"nullable": True} | |
defs.append(d) | |
return defs | |
def createDefinition(): | |
fields = copy.deepcopy(lyr.manager.properties.fields) | |
#make valid python dicts | |
for i, f in enumerate(fields): | |
f = '''{}'''.format(f) | |
f = json.loads(f) | |
fields[i] = f | |
matches = [f['name'] for f in fields if f['name'] in fieldNames] | |
newFields = removeDups(fieldNames, matches) | |
newFieldDefs = generateNewFieldDefs(newFields) | |
return newFieldDefs | |
def updateUrls(feature, urls): | |
for i, url in enumerate(urls): | |
feature.attributes[fieldNames[i]] = url | |
status = lyr.edit_features(updates=[feature]) | |
print(status) | |
def getURLs(): | |
for f in data.features: | |
urls = generateUrls(getAttachments(f), f.attributes['OBJECTID']) | |
updateUrls(f, urls) | |
# Find max attachements | |
for f in data.features: | |
l = len(getAttachments(f)) | |
if l > maxAttachments: | |
maxAttachments = l | |
if(maxAttachments > 0): | |
fieldNames = generateAttachmentFieldNames() | |
else: | |
print('No Attachments in Feature Layer') | |
sys.exit() | |
# Update lyr definition | |
fieldDefs = createDefinition() | |
status = {'success': False} | |
if(len(fieldDefs) > 0): | |
print('Adding new attachment URL fields') | |
status = lyr.manager.add_to_definition({"fields": fieldDefs}) | |
if(status['success']): | |
getURLs() | |
else: | |
#fields already there, try and update | |
print('Fields already exist. Updating.') | |
getURLs() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment