Check intersection of two polyline features

782
6
Jump to solution
05-17-2012 01:52 AM
AysberqTundra
Occasional Contributor
Hello.

I want to get bool result of intersection of two polyline features.
The Code:

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; }


Can anyone help me?
Thanks.
0 Kudos
1 Solution

Accepted Solutions
NeilClemmons
Regular Contributor III
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.

View solution in original post

0 Kudos
6 Replies
NeilClemmons
Regular Contributor III
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.
0 Kudos
AysberqTundra
Occasional Contributor
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.


Hello Neil.
Thanks for reply.
I changed the code:
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;
}


I tested this function with different features. But the function returns only true;
Can you help me solve this problem?

Thanks.
0 Kudos
NeilClemmons
Regular Contributor III
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.
0 Kudos
AysberqTundra
Occasional Contributor
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.


I changed the code:

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;
}


But now MessageBox.Show(...) function returns only "0", CheckIntersection(...) function returns only false.
0 Kudos
NeilClemmons
Regular Contributor III
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.
0 Kudos
AysberqTundra
Occasional Contributor
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.


Thanks Neil.
It worked. It is the best solution for my problem.
Thanks.
0 Kudos