【问题标题】:Using the POST Method for Batch Geocoding with ArcGIS Server REST API?通过 ArcGIS Server REST API 使用 POST 方法进行批量地理编码?
【发布时间】:2019-10-26 07:39:44
【问题描述】:

我正在尝试访问我的地理编码服务器的 REST API:

[https://locator.stanford.edu/arcgis/rest/services/geocode/USA_StreetAddress/GeocodeServer](ArcGIS 服务器 10.6.1)

...使用 POST 方法(顺便说一句,可以使用一两个示例,似乎只有这个非常简短的“注意”关于何时使用 POST,而不是如何使用:https://developers.arcgis.com/rest/geocode/api-reference/geocoding-geocode-addresses.htm#ESRI_SECTION1_351DE4FD98FE44958C8194EC5A7BEF7D)。

我正在尝试使用 requests.post(),并且我想我已经设法让令牌被接受,等等...,但我不断收到 400 错误。

根据以前的经验,这意味着数据格式的某些内容不好,但我已经直接从 Esri 支持站点剪切并粘贴了这个测试对。

# import the requests library
import requests

# Multiple address records
addresses={ 
 "records": [
 {
 "attributes": {
 "OBJECTID": 1,
 "Street": "380 New York St.",
 "City": "Redlands",
 "Region": "CA",
 "ZIP": "92373"
 }
 },
 {
 "attributes": {
 "OBJECTID": 2,
 "Street": "1 World Way",
 "City": "Los Angeles",
 "Region": "CA",
 "ZIP": "90045"
 }
 }
 ]
}

# Parameters
# Geocoder endpoint
URL = 'https://locator.stanford.edu/arcgis/rest/services/geocode/USA_StreetAddress/GeocodeServer/geocodeAddresses?'
# token from locator.stanford.edu/arcgis/tokens
mytoken = <GeneratedToken>
# output spatial reference id 
outsrid = 4326
# output format
format = 'pjson'
# params data to be sent to api 
params ={'outSR':outsrid,'f':format,'token':mytoken}

# Use POST to batch geocode
r = requests.post(url=URL, data=addresses, params=params)

print(r.json())
print(r.text)

这是我一直得到的:

{'error': {'code': 400, 'message': 'Unable to complete operation.', 'details': []}}

【问题讨论】:

标签: python rest post geocoding arcgis-server


【解决方案1】:

我不得不玩这个比我想承认的时间更长,但诀窍(我猜)是使用正确的请求标头将原始地址转换为 JSON 字符串使用json.dumps()

import requests
import json

url = 'http://sampleserver6.arcgisonline.com/arcgis/rest/services/Locators/SanDiego/GeocodeServer/geocodeAddresses'
headers = { 'Content-Type': 'application/x-www-form-urlencoded' }
addresses = json.dumps({ 'records': [{ 'attributes': { 'OBJECTID': 1, 'SingleLine': '2920 Zoo Dr' }}] })

r = requests.post(url, headers = headers, data = { 'addresses': addresses, 'f':'json'})

print(r.text)

【讨论】:

  • 就是这样! @Esri 需要更好地记录他们的 REST API。
  • 完全披露,我发现这一点的方法是使用 Charles/Fiddler 将失败的 python 请求与直接在浏览器中发出的成功请求进行比较。除了服务器对标头有点挑剔之外,另一位只是通用的 python 东西,而不是特定于“Esri”的东西。几年前,pat 向我提到他想在每个 REST 文档页面中包含多种语言的代码 sn-ps,但要让所有人都满意很难。例如,其他人会希望看到 2.x sn-p。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-01-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-04
  • 1970-01-01
相关资源
最近更新 更多