2

I use the following code to check if a point feature is within a polygon feature

pFeaBlock is a polygon feature and Fea is a point feature

int blockID = -1;
IFeatureCursor pBlockCursor = this._blockLayer.Search(null, false);
IFeature pFeaBlock = pBlockCursor.NextFeature();
while (pFeaBlock != null)
{
    if (!pFea.Shape.IsEmpty)
    {
      IPolygon pPolygon = (IPolygon)pFea.Shape;
      IRelationalOperator pRelOperator = (IRelationalOperator)pPolygon;
      if (pRelOperator.Within(pFeaBlock.Shape) == true)
      {
          blockID = (int)pFeaBlock.get_Value(2);
      }
     }

     pFeaBlock = pBlockCursor.NextFeature();
 }

but I always get he COMException on

if (pRelOperator.Within(pFeaBlock.Shape) == true)

Is anyone familiar this interface? Thanks in advance

  • Does this happen on the loop's first iteration or with a specific feature? – tomfumb Jul 15 '13 at 22:10
  • p.s. there is a slightly safer way to write your loop that is not open to infinite loops, something like while((IFeature pFeaBlock = pBlockCursor.NextFeature()) != null) – tomfumb Jul 15 '13 at 22:12
  • Do your datasets have a coordinate system? If not they need to be set. Also not sure but maybe the coordinate systems of the features need to be the same. – Hornbydd Jul 15 '13 at 23:07
  • Whats the COMExpection error message, or error code. You can look up the error code in the ESRI doc. You can usually find the error codes by searching the ESRI doc for the positive version of the number. Ie, for -2147220985 search for 2147220985. Different components have thier own set of error codes, here are the Geodatabase values. (resources.arcgis.com/en/help/arcobjects-net/componenthelp/…) – travis Jul 15 '13 at 23:56
  • Cast pFeaBlock.Shape to an IPoint and do a null check on the IPoint before you pass it in to Within, also I usually use .Contains() instead of .Within() – patrick Aug 15 '13 at 13:10
2

First, check if the relational Operator is Null. But your problem is with the geometries: You need to be sure that the relational operatior can be used. For that both spatial references need to be the same. You can useSpatialReference.FactoryCodefor check it.

while ((IFeature pFeaBlock = pBlockCursor.NextFeature()) != null)
{
  if (!pFea.Shape.IsEmpty)
  {
    IPolygon pPolygon = (IPolygon)pFea.Shape;
    if (pPolygon.SpatialReference.FactoryCode == pFeaBlock.Shape.SpatialReference.FactoryCode)
    {        
      IRelationalOperator pRelOperator = (IRelationalOperator)pPolygon;
      if (pRelOperator.Within(pFeaBlock.Shape) == true)
      {
        blockID = (int)pFeaBlock.get_Value(2);
      }
    }
  }   
}
|improve this answer|||||
1

for checking if polygon contains a point it's better to use the Contain method instead the Within method.

Dim aRing As IPointCollection = New RingClass()
aRing.AddPoint(new Point() With {.X=412853 ,.Y = 3785339})
aRing.AddPoint(new Point() With {.X=412990 ,.Y = 3785276})
aRing.AddPoint(new Point() With {.X=412926 ,.Y = 3784889})
aRing.AddPoint(new Point() With {.X=412782 ,.Y = 3784940})
aRing.AddPoint(new Point() With {.X=412853 ,.Y = 3785339})
Dim geo As IGeometry = aRing
Dim poly As IGeometryCollection = New Polygon()
poly.AddGeometry(aRing)
Dim spdef As New SpatialReferenceEnvironment()

Dim op As IRelationalOperator = poly
Dim poly1 As IPolygon = poly
poly1.SpatialReference = spdef.CreateProjectedCoordinateSystem(esriSRProjCSType.esriSRProjCS_WGS1972UTM_39N)

Dim outpoint As IPoint = New Point() With {.X = 412728, .Y = 3785165}
outpoint.SpatialReference = spdef.CreateProjectedCoordinateSystem(esriSRProjCSType.esriSRProjCS_WGS1972UTM_39N)

Dim insidePoint As IPoint = New Point() With {.X = 412878, .Y = 3785119}
insidePoint.SpatialReference = spdef.CreateProjectedCoordinateSystem(esriSRProjCSType.esriSRProjCS_WGS1972UTM_39N)

Dim results = op.Contains(outpoint)' return false
Dim results = op.Contains(insidePoint) ' return true
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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