AnsweredAssumed Answered

JSONConverterGeometryClass creates invalid geometries

Question asked by HimBromBeere on May 22, 2018
Latest reply on May 24, 2018 by HimBromBeere

I´m creating a SeverObjectExtension using ArcObjects for Server 10.6. Here I have a method that reads a geometry from HTTP-request as follows:

public IGeometry Json2Geometry(JsonObject json)
{
    IGeometry geometry = null;

    IJSONReader jsonReader = new JSONReaderClass();
    jsonReader.ReadFromString(json.ToJson());
    IJSONDeserializer jsonDeserializer = null;

    jsonDeserializer = new JSONConverterGeometry() as IJSONDeserializer;

    jsonDeserializer.InitDeserializer(jsonReader, null);
    geometry = ((IJSONConverterGeometry)jsonDeserializer).ReadGeometry(jsonReader, esriGeometryType.esriGeometryAny, false, false);

    return geometry;
}

Now I´m doing something with that geometry. In my case I cast it to IPolyline:

 

if(geometry.GeometryType == esriGeometryPolyline)
{
    var polyline = (IPolyline) geometry;
    ...
}

I get an InvalidCastException on the coe above. So what does ReadGeometry return if not an instance of IPolyline (in case of geometrytype polyline as shown above).

 

The JSON looks like this:

{
  "paths":
    [
      [
        [486893.89699999994,5755807.910000002],
        [486904.18500000244,5755744.383000003]
      ]
    ],
  "spatialReference":{"wkid":25832,"latestWkid":25832}
}

 

I also tried to create a completely new polyline based on two points. Then I converted that geometry to json using the following:

 

ILine line = new LineClass();
(line as IGeometry).SpatialReference = point1.SpatialReference;
line.PutCoords(poin1, point2);

object optional = Missing.Value;
IPolyline pline = new PolylineClass();
((ISegmentCollection)pline).AddSegment((ISegment)line, ref optional, ref optional);
((ITopologicalOperator)pline).Simplify();

((IJSONConverterGeometry)jsonSerializer).WriteGeometry(jsonWriter, null, pline, false);

When I convert that back to an IGeometry using ReadGeometry as mentioned above I also get that exception.

 

Update: I also tried to use the less generic method ReadPolyline, which should return an IPolyline. Instead I get the mentioned exception. So whatever the method returns, it´s not an IPolyline.

Outcomes