private bool CheckIntersection(IFeature feat1, IFeature feat2) { IPolyline pLine1, pLine2; pLine1 = feat1 as IPolyline; pLine2 = feat2 as IPolyline; ITopologicalOperator topoOp = pLine1 as ITopologicalOperator; IGeometry pGeom = topoOp.Intersect(pLine2, esriGeometryDimension.esriGeometry0Dimension); //Here I get this error: System.NullReferenceException: //Object reference not set to an instance of an object. if (pGeom == null) { return false; } else return true; }
Solved! Go to Solution.
The problem is that you're casting from IFeature to IPolyline. This is an invalid cast. You need to be getting the Shape of the feature and casting that to IPolyline, not the feature object itself.
private bool CheckIntersection(IFeature feat1, IFeature feat2) { IPolyline pLine1, pLine2; pLine1 = feat1.Shape as IPolyline; pLine2 = feat2.Shape as IPolyline; ITopologicalOperator topoOp = pLine1 as ITopologicalOperator; IGeometry pGeom = topoOp.Intersect(pLine2, esriGeometryDimension.esriGeometry0Dimension); if (pGeom == null) { return false; } else return true; }
The Intersect method returns a point collection when called using the 0-dimension flag. Your code should check that this point collection is not null as well as check to make sure the point collection contains at least one point.
private bool CheckIntersection(IFeature feat1, IFeature feat2) { IPolyline pLine1, pLine2; pLine1 = feat1.Shape as IPolyline; pLine2 = feat2.Shape as IPolyline; ITopologicalOperator topoOp = pLine1 as ITopologicalOperator; IGeometry pGeom = topoOp.Intersect(pLine2, esriGeometryDimension.esriGeometry0Dimension); IPointCollection pcol = pGeom as IPointCollection; MessageBox.Show(pcol.PointCount.ToString()); if (pcol.PointCount == 0) { return false; } else return true; }
If the point collection contains no points then the two geometries do not intersect in such a way that the intersection is defined as a point. If your goal is simply to determine if two polylines share a spatial relationship with each other then you may be better served by calling IRelationalOperator.Disjoint.