操作方法

操作方法:使用 Python 从地图服务中提取数据

Last Published: August 24, 2021

摘要

在某些情况下, 用户可在地图服务上启用 要素访问 功能,以轻松下载数据。 但是,可以从地图服务中 以 JSON 格式下载数据,并且  JSON 代码 可以转换为 shapefile 或要素类。 本文介绍了如何通过公开共享的地图服务使用  ArcPy 和其他内置的 Python 库进行上述操作。

过程

以下说明演示了如何使用 arcpy.JSONToFeatures_conversion() 函数在地图服务中查询要素、将 JSON 响应写入文件,以及将 JSON 文件转换为 shapefile。可按原样使用脚本,或将其格式化为带有 URL 参数的函数。

根据需要调整查询参数(大多数(但并非全部) 可能的参数都包含在内)。 输出文件存储在包含 Python 脚本的目录中。

  1. 导入所需的库。
# 对于 Python 2 import urllib2, urllib, os, arcpy
# 对于 Python 3 import urllib.parse, urllib.request, os, arcpy, json
  1. 如果访问受保护服务需要令牌,请使用下面的代码片段。 否则,请跳到步骤 3。
username = "adminaccount" password = "adminpassword" tokenURL = 'https://machine.domain.com/portal/sharing/rest/generateToken/' params = {'f': 'pjson', 'username': username, 'password': password, 'referer': 'https://machine.domain.com'} #对于 Python 2 req = urllib2.Request(tokenURL, urllib.urlencode(params)) response = urllib2.urlopen(req) #对于 Python 3 data = urllib.parse.urlencode(query=params).encode('utf-8') req = urllib.request.Request(tokenURL, data) response = urllib.request.urlopen(req) data = json.loads(response.read()) token = data['token']
  1. 指定所需 URL。
url = "https://sampleserver6.arcgisonline.com/arcgis/rest/services/Census/MapServer/3/query?"
  1. 查询参数。
#对于 Python 2 params = urllib.urlencode({'where': '1=1', 'geometryType': 'esriGeometryEnvelope', 'spatialRel': 'esriSpatialRelIntersects', 'relationParam': '', 'outFields': '*', 'returnGeometry': 'true', 'geometryPrecision':'', 'outSR': '', 'returnIdsOnly': 'false', 'returnCountOnly': 'false', 'orderByFields': '', 'groupByFieldsForStatistics': '', 'returnZ': 'false', 'returnM': 'false', 'returnDistinctValues': 'false', 'f': 'pjson' })
# 对于 Python 3 params = {'where': '1=1', 'geometryType': 'esriGeometryEnvelope', 'spatialRel': 'esriSpatialRelIntersects', 'relationParam': '', 'outFields': '*', 'returnGeometry': 'true', 'geometryPrecision':'', 'outSR': '', 'returnIdsOnly': 'false', 'returnCountOnly': 'false', 'orderByFields': '', 'groupByFieldsForStatistics': '', 'returnZ': 'false', 'returnM': 'false', 'returnDistinctValues': 'false', 'f': 'pjson',   'token': token } encode_params = urllib.parse.urlencode(params).encode("utf-8")
  1. 创建一个请求并使用 urllib 读取它。
# 对于 Python 2 request = urllib2.Request(url, params) response = urllib2.urlopen(request) json = response.read()
# 对于 Python 3 response = urllib.request.urlopen(url, encode_params) json = response.read()
  1. 将 JSON 响应写入文本文件。
with open("mapservice.json", "wb") as ms_json: ms_json.write(json)
  1. 使用 JSONToFeatures 函数将 JSON 转换为 shapefile。
ws = os.getcwd() + os.sep arcpy.JSONToFeatures_conversion("mapservice.json", ws + "mapservice.shp")
   
以下是 随 ArcMap 和 ArcGIS Server 一并安装的 Python 2.7 的 完整代码:
import urllib2, urllib, os, arcpy url = "https://sampleserver6.arcgisonline.com/arcgis/rest/services/Census/MapServer/3/query?" params = urllib.urlencode({'where': '1=1', 'geometryType': 'esriGeometryEnvelope', 'spatialRel': 'esriSpatialRelIntersects', 'relationParam': '', 'outFields': '*', 'returnGeometry': 'true', 'geometryPrecision':'', 'outSR': '', 'returnIdsOnly': 'false', 'returnCountOnly': 'false', 'orderByFields': '', 'groupByFieldsForStatistics': '', 'returnZ': 'false', 'returnM': 'false', 'returnDistinctValues': 'false', 'f': 'pjson' }) request = urllib2.Request(url, params) response = urllib2.urlopen(request) json = response.read() with open("mapservice.json", "wb") as ms_json:     ms_json.write(json) ws = os.getcwd() + os.sep arcpy.JSONToFeatures_conversion("mapservice.json", ws + "mapservice.shp")

以下是随 ArcGIS Pro 一并安装的  Python 3 的 完整代码:
import urllib.parse, urllib.request, os, arcpy, json arcpy.env.overwriteOutput = True # 指定内置 Portal for ArcGIS 帐户凭据 username = "portaluser" password = "portalpassword" tokenURL = 'https://server.domain.com/portal/sharing/rest/generateToken/' params = {'f': 'pjson', 'username': username, 'password': password, 'referer': 'https://server.domain.com'} data = urllib.parse.urlencode(query=params).encode('utf-8') req = urllib.request.Request(tokenURL, data) response = urllib.request.urlopen(req) data = json.loads(response.read()) token = data['token'] # 指定 REST URL 以返回 JSON url = "https://server.domain.com/server/rest/services/servicename/MapServer/0/query?" params = {'where': '1=1',           'geometryType': 'esriGeometryEnvelope',           'spatialRel': 'esriSpatialRelIntersects',           'relationParam': '',           'outFields': '*',           'returnGeometry': 'true',           'geometryPrecision': '',           'outSR': '',           'returnIdsOnly': 'false',           'returnCountOnly': 'false',           'orderByFields': '',           'groupByFieldsForStatistics': '',           'returnZ': 'false',           'returnM': 'false',           'returnDistinctValues': 'false',           'f': 'pjson',           'token': token           } encode_params = urllib.parse.urlencode(params).encode("utf-8") response = urllib.request.urlopen(url, encode_params) json = response.read() with open("mapservice.json", "wb") as ms_json:     ms_json.write(json) ws = os.getcwd() + os.sep arcpy.JSONToFeatures_conversion("mapservice.json", ws + "mapservice.shp", )

文章 ID:000019645

从 ArcGIS 专家处获得帮助

联系技术支持部门

下载 Esri 支持应用程序

转至下载选项

相关信息

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