Arc Engine – How to Determine if a Point Lies Within a Polyline in ArcGIS

arcgis-10.0arcgis-enginepointpolyline-creation

I recently asked a similiar question on this, however I needed to know if there is a way to solve this if I only have a IPolyline Feature, and not the whole layer.

I want to know if I can determine if a point lays within my Polyline feature.

I tried using both intersect and difference from ITopologicalOperator, but still no use.

Please Help:

  private bool PointLaysWithinLine(IPolyline currentPath, IPoint iPoint)
    {
        IPolyline newLine = new PolylineClass();
        ((IPointCollection)newLine).AddPoint(iPoint);
        newLine.Project(currentPath.SpatialReference);

//This doesn't work
        IPolyline intersection = ((ITopologicalOperator)currentPath).Intersect(newLine, esriGeometryDimension.esriGeometryNoDimension) as IPolyline;

//This also doesn't work
        IPolyline difference = ((ITopologicalOperator)currentPath).Difference(newLine) as IPolyline;

        return currentPath.Length != difference.Length; 
    }

*************************UPDATE*************************

So after playing around for 2 days with user's comments and ideas. I came up with this code (thanks jakub)

point.SpatialReference = IMap.SpatialReference;
ISpatialFilter spatialFilter = new SpatialFilter();
spatialFilter.Geometry = point;
spatialFilter.GeometryField = ((IFeatureLayer)layer).FeatureClass.ShapeFieldName;
spatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects;
IFeatureCursor flowPathCursor = ((IFeatureLayer)layer).FeatureClass.Search(spatialFilter, true);

if (flowPathCursor.NextFeature() != null)
   result = true;

The above code works for MOST time, however, some rare case it still CANNOT find the feature. Note that I USED snapping (vertices/edges) so it should be dead on.

Also I tried using the slope y=mx+b method, and IIdentify2.Identify. They all work MOST time but some rare case (i notice it happening most often on straight EDGES).

Any one have any idea? This is driving me insane.

Best Answer

Use IRelationalOperator, either touches or not disjoint.