5

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.

asked May 13, 2011 at 14:21
8
  • "send that to another feature class" ... is that local or through another REST call? Commented May 13, 2011 at 14:23
  • Another REST call Commented May 13, 2011 at 14:32
  • 1
    Would dependencies on Esri's WPF SDK assemblies be acceptable? Commented 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. Commented May 13, 2011 at 14:56
  • 1
    Have you tried Vish's GeoJson.NET ? Commented May 13, 2011 at 15:00

2 Answers 2

4

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);
 }
 }
}
answered May 13, 2011 at 19:07
0
2

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

answered May 13, 2011 at 14:58

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.