I am writing a windows console app in VS 2010 to perform an overlay using the ArcGIS Rest API.
My first REST query returns information about the geometry for a specific feature. I then want to send that geometry to another feature class, to return data about the shared area.
I am using the DeserializeObject method to convert the query results to a Dictionary. How do I take the geometry from that result set and post it to a new query?
Update:
What if I user the JavaScriptSerializer.Serialize method on the geometry string? Is there a way to pass the resulting JSON string to the REST service call? I already tried just assigning it to the "geometry" value in the querystring, but that failed.
-
"send that to another feature class" ... is that local or through another REST call?Kirk Kuykendall– Kirk Kuykendall2011年05月13日 14:23:24 +00:00Commented May 13, 2011 at 14:23
-
Another REST callBritt Wescott– Britt Wescott2011年05月13日 14:32:27 +00:00Commented May 13, 2011 at 14:32
-
1Would dependencies on Esri's WPF SDK assemblies be acceptable?Kirk Kuykendall– Kirk Kuykendall2011年05月13日 14:42:09 +00:00Commented May 13, 2011 at 14:42
-
I've considered that, I wanted to see if there was a way to do it just with REST calls before I opted for that route.Britt Wescott– Britt Wescott2011年05月13日 14:56:53 +00:00Commented May 13, 2011 at 14:56
-
1Have you tried Vish's GeoJson.NET ?Kirk Kuykendall– Kirk Kuykendall2011年05月13日 15:00:54 +00:00Commented May 13, 2011 at 15:00
2 Answers 2
The code below works for me.
using System;
using System.Text;
using System.Net;
using Newtonsoft.Json;
using System.IO;
using Newtonsoft.Json.Linq;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
try
{
string baseUrl = @"http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StateCityHighway_USA/MapServer";
string jsonGeom = GetStateGeom(baseUrl,1, "Colorado");
string featuresJson = QueryHighways(baseUrl, 0, jsonGeom);
Console.WriteLine(featuresJson);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.ReadLine();
}
private static string GetStateGeom(string baseUrl, int layerID,string state)
{
string url = String.Format("{0}/{1}/query?text={2}&f=pjson",baseUrl,layerID,state);
var wc = new WebClient();
var strm = wc.OpenRead(new Uri(url));
var strmReader = new StreamReader(strm);
var json = strmReader.ReadToEnd();
//Console.WriteLine(json);
strm.Close();
var jObj = JsonConvert.DeserializeObject(json) as JObject;
JArray features = (JArray)jObj["features"];
if (features.Count == 0)
{
Console.WriteLine("state of {0} not found", state);
return null;
}
string jsonGeom = ((JObject)features[0])["geometry"].ToString();
return jsonGeom;
}
private static string QueryHighways(string baseUrl, int layerID,string jsonGeom)
{
string url = String.Format("{0}/{1}/query", baseUrl, layerID);
var wc = new WebClient();
wc.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
StringBuilder sb = new StringBuilder();
sb.Append("geometryType=esriGeometryPolygon");
sb.Append(String.Format("&geometry={0}",jsonGeom));
sb.Append("&inSR=&spatialRel=esriSpatialRelIntersects");
sb.Append("&returnGeometry=true");
sb.Append("&relationParam=");
sb.Append("&where=");
sb.Append("&returnCountOnly=false");
sb.Append("&maxAllowableOffset=");
sb.Append("&f=pjson");
var bytes = Encoding.ASCII.GetBytes(sb.ToString());
var respBytes = wc.UploadData(url, "POST", bytes);
return Encoding.ASCII.GetString(respBytes);
}
}
}
There is some JSON helpers i ArcObjects that you might be able to use, but I'm not sure if they are supported on the client side though: SOESupport.JsonObject
Explore related questions
See similar questions with these tags.