Я пытаюсь загрузить фотографии из слоя ArcOnline. Я хотел бы определить те, где атрибут Creator = значение Speci c, а затем сохранить все фотографии из одной точки в одной папке с objectid в качестве заголовка этой папки, а затем со значениями атрибутов objectid и asset_type в качестве фотографии имя "objid_assettype".
Я сталкиваюсь с ошибкой NameEr. Вот код, над которым я работаю, за исключением информации о слое и имени пользователя.
import requests import json import os import sys
# ARCGIS ONLINE USERNAME AND PASSWORD def get_new_sh():
username = ""
password = ""
config = {'username': username, 'password': password}
#token = securityhandlerhelper.securityhandlerhelper(config)
token = ''
#sh = arcrest.AGOLTokenSecurityHandler()
return token
def get_objectid_ID(objID, token):
# LAYER SERVICE URL
base_url = ''
# BUDILING COMPLETE URL
query_url = base_url + str(objID) + '?token=' + str(token) + '&f=json'
# ISSUING THE REST REQUEST
r = requests.get(query_url)
# loading the response into a dictionary
response_dic = json.loads(r.content)
return (response_dic['feature']['attributes']['OBJECTID'], response_dic['feature']['attributes']['Asset_Type'], response_dic['feature']['attributes']['Creator'])
def save_pics(objID, attachment_list, OBJECTID, Asset_Type, token, results_file):
cwd = os.getcwd()
Asset_Type = str(Asset_Type).replace('/','_')
Asset_Type = str(Asset_Type).replace(' ', '_')
new_cwd = cwd + '\\' + str(OBJECTID) + '_' + str(Asset_Type)
if os.path.exists(new_cwd):
print('skipped:' + new_cwd)
results_file.write('skipped:' + new_cwd + ",objid: " + str(objID) + "\n")
return
os.makedirs(new_cwd)
print('pulled:' + new_cwd)
results_file.write('pulled:' + new_cwd + ",objid: " + str(objID) + "\n")
base_url = ''
query_url = base_url + str(objID) + '/attachment/'
pic_count = 1
for pic in attachment_list:
name = str(objID) + '_' + str(pic['name'])
complete_name = new_cwd + '\\' + name
pic_url = query_url + str(pic['id']) + '?token=' + str(token)
r = requests.post(pic_url, stream=True)
with open(complete_name, 'wb') as f:
for chunk in r.iter_content(1024):
f.write(chunk)
del r
pic_count += 1
def download_pics():
# LOGGING IN AND GETTING AUTHORIZATION (TOKEN)
token = get_new_sh()
results_file = open('results.txt', 'a+')
# for a sequential range of object IDs (estimated from examining the layer on ArcGIS online)
for obj_id in range(37000,50000,1):
#print(obj_id)
try:
# adding onto the base string
query_url = '' \
'token=' + str(token) + \
'&f=json&objectIds=' + str(objID)
# executing the REST request
r = requests.get(query_url)
#loading the response into a dictionary
response_dic = json.loads(r.content)
# checking to see if the attachement list is empty, if not
if len(response_dic['attachmentGroups']) != 0:
pic_list = response_dic['attachmentGroups'][0]['attachmentInfos']
ids = get_objectid_ID(obj_ID, token)
OBJECTID = ids[0]
Asset_Type = ids[1]
Creator = ids[2]
if sades_id in sades_id_list:
if '' in Creator:
print('MB')
save_pics(obj_id, pic_list, OBJECTID, Asset_Type, token, results_file)
else:
with open("objID.txt", "a") as f:
f.write(str(obj_id) + "\n")
except:
print("Unexpected error:", sys.exc_info()[0])
#raise
sh = get_new_sh()
continue
if __name__ == "__main__":
download_pics()