操作方法

操作方法:使用 ArcGIS API for Python 从 ArcGIS Online 下载要素服务项目

Last Published: October 17, 2022

摘要

在 ArcGIS Online 中使用要素服务时,可以在 ArcGIS Online 中创建所有要素服务的副本作为备份;或者,在有存储空间限制时这样做也很有用。 单独下载每个要素服务可能非常耗时,尤其是在大批量下载的情况下。 以下 Python 脚本允许用户下载所有要素服务项目的副本。 完成该过程后,就可以安全地删除这些项目。

过程

以下脚本可执行用户要素服务项目搜索,并以所需格式将项目导出为单个文件。 在 ArcGIS Online 中创建文件后,可以将其下载到脚本下载部分中指定的文件路径。

  1. 在 Python 中,导入所需的库。
import arcgis from arcgis.gis import GIS
  1. 定义登录凭据。
gis = GIS(None,'username', 'password', verify_cert=False)
  1. 定义下载格式和指定的输出文件路径。 
def downloadUserItems(owner, downloadFormat): try: # Search items by username items = gis.content.search('owner:{0}'.format(owner)) print(items) # Loop through each item and if equal to Feature service then download it for item in items: if item.type == 'Feature Service': result = item.export('sample {}'.format(item.type), downloadFormat) result.download(r'file path of where to store the download')   # Delete the item after it downloads to save space (OPTIONAL)   result.delete() except Exception as e: print(e)
  1. 下载所生成的文件。
downloadUserItems('username', downloadFormat='Shapefile')

以下是完整脚本示例:

import arcgis from arcgis.gis import GIS gis = GIS(None,'username', 'password', verify_cert=False) # Download all data from a user def downloadUserItems(owner, downloadFormat): try: # Search items by username items = gis.content.search('owner:{0}'.format(owner)) print(items) # Loop through each item and if equal to Feature service then download it for item in items: if item.type == 'Feature Service': result = item.export('sample {}'.format(item.type), downloadFormat) result.download(r'file path of where to store the download')   # Delete the item after it downloads to save on space   result.delete() except Exception as e: print(e) # Function takes in two parameters. Username and the type of download format downloadUserItems('username', downloadFormat='Shapefile')

downloadUserItems 函数可接收两个参数:

  • owner[字符串] - 用户名
  • downloadFormat[字符串] - 下载输出文件的格式
注: 输出有多种文件格式,其中包括但不限于:shapefile (SHP)、CSV、KML、FGDB、要素集合。

文章 ID:000018909

从 ArcGIS 专家处获得帮助

联系技术支持部门

下载 Esri 支持应用程序

转至下载选项

相关信息

发现关于本主题的更多内容