方法
ユーザーがデータをダウンロードしやすくするために、マップ サービス上でフィーチャ アクセス機能を有効にすることがあります。 ただし、データは JSON 形式でマップ サービスからダウンロードでき、JSON コードをシェープファイルやフィーチャクラスに変換することができます。 この記事では、ArcPy やその他の組み込みの Python ライブラリを使用し、一般公開されているマップ サービスでその操作を行う手順を説明します。
次の手順では、マップ サービスでフィーチャを検索し、ファイルへの JSON 応答を記述し、arcpy.JSONToFeatures_conversion() 関数を使用して JSON ファイルをシェープファイルに変換する方法について説明します。可能であれば、スクリプトはそのまま使用するか、URL 引数を取る関数に書式設定してください。
クエリ パラメーターは必要に応じて調整します (考えられるパラメーターのほとんどは含まれていますが、すべては含まれていません)。 出力ファイルは、Python スクリプトを含むディレクトリに格納されています。
# Python 2 の場合 import urllib2, urllib, os, arcpy
# Python 3 の場合 import urllib.parse, urllib.request, os, arcpy
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 の場合
req = urllib.Request(tokenURL, urllib.urlencode(params))
response = urllib.parse.urlopen(req)
data = json.load(response)
token = data['token']
params= {'f': 'pjson', 'token': token}
url = "https://sampleserver6.arcgisonline.com/arcgis/rest/services/Census/MapServer/3/query?"
# 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'
}
encode_params = urllib.parse.urlencode(params).encode("utf-8")
# 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()
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")
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")
import urllib.parse, urllib.request, os, arcpy
url = "https://sampleserver6.arcgisonline.com/arcgis/rest/services/Census/MapServer/3/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'
}
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 エキスパートのサポートを受ける
今すぐチャットを開始