【问题标题】:Formatting RestSharp request for ESRI geocoder格式化 ESRI 地理编码器的 RestSharp 请求
【发布时间】:2016-11-04 05:49:12
【问题描述】:

我有一些代码使用 RestSharp 格式化 JSON 请求以访问 ESRI 地理编码 API。该代码有效,但我想知道是否有更好的方法将请求转换为正确的格式,这里是我所拥有的示例,下面是请求应该是什么样子的示例。

request = new RestRequest("geocodeAddresses", Method.POST);
        request.AddHeader("Content-Type", "application/x-www-form-urlencoded");

        //Format the request properly
        var attributes = new Dictionary<string, object>();
        attributes.Add("OBJECTID", address.Address);
        attributes.Add("Address", address.Address);
        attributes.Add("City", address.City);
        attributes.Add("Region", address.State);
        attributes.Add("Postal", address.ZipCode);

        JsonObject attributesObj = new JsonObject();
        foreach (var parms in attributes)
        {
            attributesObj.Add(parms);
        }


        JsonObject recordsObj = new JsonObject();
        recordsObj.Add("attributes", attributesObj);
        JsonArray EsriRequest = new JsonArray();
        EsriRequest.Add(recordsObj);
        JsonObject addressObj = new JsonObject();
        addressObj.Add("records", EsriRequest);




        request.AddParameter("addresses",
            addressObj.ToString());
        request.AddParameter("token", esriToken.ToString());
        request.AddParameter("f", "json");
        request.OnBeforeDeserialization = resp => { resp.ContentType = "application/json"; };
        IRestResponse<EsriAddress> responseData = client.Execute<EsriAddress>(request);

请求输出样本:

http://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer/geocodeAddresses?addresses={"records":[{"attributes":{"OBJECTID":1,"Address":"380 New York St.","City":"Redlands","Region":"CA","Postal":"92373"}},{"attributes":{"OBJECTID":2,"Address":"1 World Way","City":"Los Angeles","Region":"CA","Postal":"90045"}}]}&sourceCountry=USA&token=<YOUR TOKEN>&f=pjson

我目前只发送一个地址,但理论上 api 一次可以发送多个地址。

【问题讨论】:

  • 这是唯一的方法吗?

标签: c# json rest restsharp esri


【解决方案1】:

以下是我如何将一批地址发送到ArcGIS REST API 中的 geocodeAddresses 方法。我没有使用RestSharp,只是HttpClient

string token = GetToken();
string url = "http://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer/geocodeAddresses"; 

using (var client = new HttpClient())
{
    using (var content = new MultipartFormDataContent())
    {
        var values = new[]
        {
            new KeyValuePair<string, string>("token", token),
            new KeyValuePair<string, string>("forStorage", "true"),
            new KeyValuePair<string, string>("MaxBatchSize", "1000"),
            new KeyValuePair<string, string>("outFields", "*"),
            new KeyValuePair<string, string>("f", "json"),
            new KeyValuePair<string, string>("addresses", inputJson)  // json string containing an array of a complex type
        };

        foreach (var keyValuePair in values)        
            content.Add(new StringContent(keyValuePair.Value), keyValuePair.Key);

        var response = await client.PostAsync(url, content);
        Task<string> responseString = response.Content.ReadAsStringAsync();
        string outputJson = await responseString; 
    }
}

【讨论】:

  • 我正在回顾这个项目,我认为这是正确的答案,但是你知道使用这种批处理方法是否会花费积分吗?
  • 对于其他需要此功能的人,此方法有效。还不确定它是否需要积分。
  • @TravisJ - 如果您将“forStorage”参数设置为 true,这将消耗积分。如果您只是进行地理编码以在地图上显示点,而不存储输出数据,则可以将此参数设置为 false,并且不会收取任何费用。这是一些more info on free vs paid operations in the Esri ArcGIS JS API.。仅供参考 - 地理编码消耗 40 credits per 1000 addresses geocoded.
  • 谢谢。这将有助于为我的客户节省一些钱。
  • 您必须仔细检查 Esri 使用条款,但您​​可以将地理编码结果存储更长的时间,然后对照所有最近地理缓存的地址检查输入地址。如果可用,您可以使用缓存结果,进一步节省信用使用量。
猜你喜欢
  • 1970-01-01
  • 2011-01-17
  • 2021-01-04
  • 1970-01-01
  • 2015-07-09
  • 2016-07-26
  • 2013-01-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多