Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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.

share|improve this question
    
"send that to another feature class" ... is that local or through another REST call? –  Kirk Kuykendall May 13 '11 at 14:23
    
Another REST call –  Seth P. May 13 '11 at 14:32
1  
Would dependencies on Esri's WPF SDK assemblies be acceptable? –  Kirk Kuykendall May 13 '11 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. –  Seth P. May 13 '11 at 14:56
    
Have you tried Vish's GeoJson.NET ? –  Kirk Kuykendall May 13 '11 at 15:00
show 3 more comments

2 Answers

up vote 3 down vote accepted

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);
        }
    }
}
share|improve this answer
add comment

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.