9

I want to use the IsNear method of IRelationalOperator2. The documentation says that GeometryBag supports the IRelationalOperator2 interface. But this just doesn't work and results in an exception "... unable to cast COM object ... No such interface supported..."

IRelationalOperator2 relationalOperator = (IRelationalOperator2)geometry;

The same code works fine with IRelationalOperator but there is no IsNear method:

IRelationalOperator relationalOperator = (IRelationalOperator)geometry;

What is wrong - the code, the documentation or is it a bug? "geometry" is a geometry of geometry type esriGeometryBag and contains some polylines. I used the straight cast to get the .NET exception.

2
  • I forgot to say that I use 10.0 SP2 Commented Aug 18, 2011 at 5:26
  • 1
    This problem still exists in ArcGis 10.1 (and also ArcGis 10.0 SP 5). Unfortunatly nobody sent this bug to ESRI until now, so I informed the local distributor and ESRI registered the bug now. I hope it will get fixed with the next service pack.
    – dpalmetz
    Commented Sep 3, 2012 at 6:23

2 Answers 2

6

There's definitely something fishy going on here with 10.0 sp2.

I can cast a geometry bag to IRelationalOperator2, otherwise this test would say "unable to cast", plus I'd get a null reference exception.

However, on the first call to IsNear I get the exception.

System.InvalidCastException occurred
  Message=Unable to cast COM object of type 'ESRI.ArcGIS.Geometry.GeometryBagClass' to interface type 'ESRI.ArcGIS.Geometry.IRelationalOperator2'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{839F5C7E-ED5F-4B3F-8F97-C0A9CC4817EE}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).
  Source=ESRI.ArcGIS.Geometry

Here's the test:

private void GBTest(IPoint pnt)
{
    // this line does not fail
    var relop = new GeometryBagClass() as IRelationalOperator2;

    if (relop == null)
        Debug.Print("unable to cast"); // this doesn't print out

    var coll = relop as IGeometryCollection;
    coll.AddGeometry(pnt);
    //((ISpatialIndex)coll).AllowIndexing = true;
    //((ISpatialIndex)coll).Invalidate();

    var pnt2 = ((IClone)pnt).Clone() as IPoint;
    ((ITransform2D)pnt2).Move(100.0, 100.0);
    if (relop.IsNear(pnt2, 1000.0)) // exception here
        Debug.Print("test 1 fail");

    if (!relop.IsNear(pnt2, 10.0))
        Debug.Print("test 2 fail");
}

Here's another test. Notice how I can cast without an exception, but get an InvalidCast exception when calling IsNear.

private void GBTest(IPoint pnt)
{
    var coll = new GeometryBagClass();
    ((IGeometry)coll).SpatialReference = pnt.SpatialReference;

    coll.AddGeometry(pnt);
    coll.GeometriesChanged();

    var relop = (IRelationalOperator2)coll; // would expect the exception here

    var pnt2 = ((IClone)pnt).Clone() as IPoint;
    ((ITransform2D)pnt2).Move(100.0, 100.0);
    if (relop.IsNear(pnt2, 1000.0)) // exception here
        Debug.Print("test 1 fail");

    if (!relop.IsNear(pnt2, 10.0))
        Debug.Print("test 2 fail");
}
4
  • Kirk, I did the same. Your test will never be true because at this time "relop" has a value of {ESRI.ArcGIS.Geometry.GeometryBagClass} with a count of 0. The "null" or the exception happens if You try to cast a real GeometryBag (includes data). Commented Aug 18, 2011 at 5:42
  • @Georg I added code showing how I can cast a non-empty geometrybagclass to IRelationalOperator2. The puzzling thing to me is that an InvalidCast exception isn't thrown when I actually do the cast - only when calling IsNear. Commented Aug 18, 2011 at 13:36
  • Kirk, thank You for the code and Your comments. I can reproduce Your results. One strange thing: after the actually cast and before calling IsNear the Locals in Visual Studio show some problems for "relop" - all IGeometry5-Entries have a value of "((ESRI.ArcGIS.Geometry.GeometryBagClass)(relop)).IGeometry5_xxxx' threw an exception of Type 'System.Invalid.CastException'. But the exception is not actually thrown. This happens when calling IsNear. Commented Aug 18, 2011 at 15:56
  • I also experienced weird cast behavior at 10 (not sure if any SPs were installed), this time with the VariantStreamIO class. I'll try to isolate and reproduce the problem. If this is a bug, it's a serious one indeed.
    – Petr Krebs
    Commented Aug 18, 2011 at 16:02
0

I would guess that it is a bug with the documentation since a GeometryBag is a collection of points/lines/polygons. Doing some ITopologicalOperator operations on a mixed and matched collection of geometry types could be impossible. If you used an IGeometryCollection of polylines though I think your solution would work.

Your Answer

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

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