Read geometry from JSON object

10958
5
Jump to solution
11-24-2012 06:52 AM
XintaoLiu
Occasional Contributor II
In Flex 3, I convert a featureset (including several simple polygons) to JSON object: var geomJSON:Object = myFeatureSet.toJSON();

Then I pass the geomJSON as a parameter to a web service (developed using VS2010 C# language). Now the problem is: how to read the polygons as ESRI IPolygon inside the C# web service? What kinds of ESRI classes should I use?

Many thanks in advance! And sorry for the cross posting.
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
TomSchuller
Occasional Contributor III
0 Kudos
5 Replies
IvanBespalov
Occasional Contributor III
http://json.org/ - scroll page down and you'll see JSON utilities links for C#.

UPD
ArcObjects Help for Java - com/esri/arcgis/server/json package
ArcObjects API Reference for .NET - JSONObject Class
0 Kudos
EstherColero
New Contributor III
Hello Xinato,

You need the class ESRI.ArcGIS.SOESupport.Conversion. I use this code in Desktop, but probably it works for you. Try something like this:

using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.SOESupport;

public class JSonBiz
{
 private const string JSON_GEOMETRY = "geometry";
 private const string JSON_SPATIAL_REFERENCE = "spatialReference";
 private const string JSON_RINGS = "rings";
 private const string JSON_PATHS = "paths";
 private const string JSON_POINTS = "points";

 private const string JSON_Z = "z";
 public static IGeometry ToGeometry(string strJson)
 {
  IGeometry pGeometry = null;
  JsonObject pJsonObject = null;
  JsonObject pJsonObjectGeometria = null;
  JsonObject pJsonObjectSpatialReference = null;
  ISpatialReference pSpatialReference = null;

  try {
   //Parse
   pJsonObject = new JsonObject(strJson);

   //Get geometry
   if (pJsonObject.TryGetJsonObject(JSON_GEOMETRY, pJsonObjectGeometria)) {
    pGeometry = getGeometry(pJsonObjectGeometria);

    //Set spatial reference
    if ((pGeometry != null) && pJsonObject.TryGetJsonObject(JSON_SPATIAL_REFERENCE, pJsonObjectSpatialReference)) {
     pSpatialReference = Conversion.ToSpatialReference(pJsonObjectSpatialReference.ToJson());
     if ((pSpatialReference != null)) {
      pGeometry.SpatialReference = pSpatialReference;
     }
    }
   }
  } catch (Exception ex) {
   pGeometry = null;
   //Maybe show your own error
  }

  return pGeometry;
 }

 private static IGeometry getGeometry(JsonObject jsonObjectGeometry)
 {
  IGeometry pGeometry = null;
  IPoint pPoint = null;
  IZAware pZAware = default(IZAware);
  double dblZ = 0;
  object[] objArray = null;

  //Test all 
  if (jsonObjectGeometry.TryGetArray(JSON_RINGS, objArray)) {
   //Polygon
   pGeometry = Conversion.ToGeometry(jsonObjectGeometry, esriGeometryType.esriGeometryPolygon);
  } else if (jsonObjectGeometry.TryGetArray(JSON_PATHS, objArray)) {
   //Polyline
   pGeometry = Conversion.ToGeometry(jsonObjectGeometry, esriGeometryType.esriGeometryPolyline);
  } else if (jsonObjectGeometry.TryGetArray(JSON_POINTS, objArray)) {
   //Multipoint
   pGeometry = Conversion.ToGeometry(jsonObjectGeometry, esriGeometryType.esriGeometryMultipoint);
  } else {
   //Point
   try {
    pPoint = Conversion.ToGeometry(jsonObjectGeometry, esriGeometryType.esriGeometryPoint);

    //z value
    if (jsonObjectGeometry.Exists(JSON_Z) && jsonObjectGeometry.TryGetAsDouble(JSON_Z, dblZ)) {
     //Test value
     if (double.IsNaN(dblZ)) {
      dblZ = 0;
     }

     //Set value
     pZAware = pPoint;
     pZAware.ZAware = true;
     pPoint.Z = dblZ;
    }

    pGeometry = pPoint;
   } catch (Exception ex) {
    pGeometry = null;
   }

   //Envelope
   if (pGeometry == null) {
    try {
     pGeometry = Conversion.ToGeometry(jsonObjectGeometry, esriGeometryType.esriGeometryEnvelope);
    } catch (Exception ex2) {
     pGeometry = null;
    }
   }
  }

  //Simplify (Maybe you don't need it)
  if ((pGeometry != null)) {
   ITopologicalOperator pTopologicalOperator = pGeometry;
   pTopologicalOperator.Simplify();
  }

  return pGeometry;
 }

 public static string ToJSon(ref IGeometry pGeometry)
 {
  string strJson = null;

  strJson = System.Text.Encoding.ASCII.GetString(ESRI.ArcGIS.SOESupport.Conversion.ToJson(pGeometry));

  return strJson;
 }
}


Hope it helps.

Regards
Esther
0 Kudos
TomSchuller
Occasional Contributor III
0 Kudos
ChristinaMosnick
New Contributor II

The original link is broken.  Here is the updated link I think..

0 Kudos
XintaoLiu
Occasional Contributor II
Thank all you guys!
0 Kudos