JSONConverterGeometryClass creates invalid geometries

772
1
05-22-2018 03:45 AM
CarstenSchumann
Occasional Contributor

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.

0 Kudos
1 Reply
CarstenSchumann
Occasional Contributor

Seems like some very strange things gone on when debugging in Visual Studio 2015. Actually we don´t get an InvalidCastException - this is only what VS shows when debugging the code - but a COMException. Having determined that strange error it was easy to get the actual problem which was caused by a different spatial reference than that within my geodatabase.

So when we don´t debug the program, we get the COMException which clearly shows the actual error, instead of the InvalidCastException indicated from the debugger in VS2015. 

This is not really the answer to the question - which has obviously solved itself. That´s why I didn´t mark it as the answer also.

0 Kudos