How to find out if a point is within the area bounded by a polygon?

3400
1
09-10-2010 01:14 PM
PatrickBurke
New Contributor II
I want to find out if a point is within the area bounded by a polygon.  Here's what got so far:

IRelationalOperator2 pointRelationalOperator = MyPoly as IRelationalOperator2;
bool ok = pointRelationalOperator.Within(MyPoly as IGeometry); //this is ok the polygon contains itself

bool ok = pointRelationalOperator.Contains(MyPoly as IGeometry); //this is also ok

Next convert Polygon into a PointCollection, grab a point, and see if that point is "within()" the polygon.
IPointCollection4 pc4 = MyPoly as IPointCollection4;
for (int i = 0; i < pc4.PointCount; i++)
{
IPoint thisPoint = pc4.get_Point(i);
bool isPointInPolygon = pointRelationalOperator.Within(thisPoint); //tried both Within and Contains
isPointInPolygon = pointRelationalOperator.Contains(thisPoint);
}


isPointInPolygon is always false.  😞  
The polygon is non-empty, it is closed and it returns a length and has 347 points.


Fyi, I am creating the polygon by loading up a shape file, then merging all the feature classes into one polygon according to this tutorial, http://help.arcgis.com/en/sdk/10.0/arcobjects_net/conceptualhelp/index.html#//00010000009t000000
0 Kudos
1 Reply
ThavitinaiduGulivindala
Occasional Contributor
Hi,
If you a pass point, you can use CONTAINS method to check whether the point is COMPLETELY inside the polygon but return false if the point is on the boundary line or outside the polygon.


IPointCollection4 pc4 = MyPoly as IPointCollection4;
for (int i = 0; i < pc4.PointCount; i++)
{
IPoint thisPoint = pc4.get_Point(i);
bool isPointInPolygon = pointRelationalOperator.Within(thisPoint); //tried both Within and Contains
isPointInPolygon = pointRelationalOperator.Contains(thisPoint);
}

In th above piece of code, you are checking whether the polygon vertices are present inside the polygon. But the vertices fall on the boundary line of the polygon and hence CONTAINS method always returns false.

http://edndoc.esri.com/arcobjects/9.1/componenthelp/esrigeometry/IRelationalOperator_Contains.htm
0 Kudos