操作方法
ArcGIS Online 中推荐和支持的内容备份工作流是:导出并下载托管要素服务,或者通过分布式协作将托管要素服务复制到 ArcGIS Enterprise。 本文提供了一个代码示例,演示了如何将托管要素服务导出并下载为文件地理数据库格式。
ArcGIS Online 云存储安全可靠。 然而,意外情况时有发生,或者定期备份可能是一项业务需求。 因此,许多没有部署 ArcGIS Enterprise 的组织会定期导出并下载托管要素服务,获取备份副本。 此工作流的缺点在于,通过手动导出和下载的方式备份托管要素服务既耗时又费力。
利用 ArcGIS API for Python,可以自动将托管要素服务导出并下载为文件地理数据库,并保存到本地驱动器的指定位置。下方的代码示例演示了如何在 Jupyter Notebook 或独立脚本环境中执行此操作:
from arcgis.gis import GIS import datetime as dt username = input("Input your username: ") gis = GIS("https://arcgis.com", username)
运行此代码块后,系统将提示您输入密码。 然后,搜索需要下载的要素服务。 为 max_items 参数输入一个较大的值,确保列表返回所有要素服务。 在此示例中,查询仅搜索已登录用户的项目。 使用管理员权限连接,并修改查询,下载组织中其他用户拥有的、未共享或未启用导出的服务。 下方的单元格还会要求输入文件路径。由于 Jupyter Notebook 在将字符串用作下载函数的输出位置时会自动处理转义字符,因此您可以按常规方式输入路径。
Note: When using the Jupyter Notebook or a stand-alone script, a local folder location can be provided. However, ArcGIS Notebook on ArcGIS Online is not directly connected to the local folder on the machine. When running the script on ArcGIS Notebook, the following folder path must be provided: '/arcgis/home/<folder_name>' The folder path above can be accessed from the Files tab of ArcGIS Notebook.
folder_path = input("Please enter the file location to store the backups: ") num_items = int(input("How many items do you want to back up? ")) query_string = "type:Feature Service, owner:{}".format(username) items = gis.content.search(query=query_string, max_items=num_items, sort_field='modifed', sort_order='desc') print(str(len(items)) + " items will be backed up to " + folder_path +". See the list below:") items
系统将返回类似如下内容的列表:
[<Item title:"Title1" type:Feature Layer Collection owner:Username>, <Item title:"Title2" type:Feature Layer Collection owner:Username>, <Item title:"Title3" type:Feature Layer Collection owner:Username>, + about 100 more in my case...]
最后,我们创建一个函数。该函数会遍历要素服务列表(下方脚本中包含逻辑,避免下载托管要素图层视图,防止创建重复备份),生成 FGDB 标题,将每个要素服务导出并下载为 FGDB 文件,并最终从 ArcGIS Online 的“我的内容”中删除这些 FGDB 项目:
def download_as_fgdb(item_list, backup_location): for item in item_list: try: if 'View Service' in item.typeKeywords: print(item.title + " is view, not downloading") else: print("Downloading " + item.title) version = dt.datetime.now().strftime("%d_%b_%Y") result = item.export(item.title + "_" + version, "File Geodatabase") result.download(save_path=backup_location) result.delete() print("Successfully downloaded " + item.title) except: print("An error occurred downloading " + item.title) print("The function has completed") download_as_fgdb(items, folder_path)
文章 ID: 000022524
获取来自 ArcGIS 专家的帮助
下载 Esri 支持应用程序