简体   繁体   中英

Intersection between two geographic lines

I'm using the DotSpatial C# library and I'm trying to use the following code to try to find the intersection point between two lines (I know they do intersect)

var geoproj = DotSpatial.Projections.KnownCoordinateSystems.Geographic.World.WGS1984;

var d1 = new FeatureSet { Projection = geoproj };
//var c1 = new Coordinate(31.877484, 34.736723);
//var c2 = new Coordinate(31.879607, 34.732362);
var c1 = new Coordinate(0, -1);
var c2 = new Coordinate(0, 1);
var line1 = new LineString(new[] { c1, c2 });
d1.AddFeature(line1);

var d2 = new FeatureSet { Projection = geoproj };
//var c3 = new Coordinate(31.882391, 34.73352);
//var c4 = new Coordinate(31.875502, 34.734851);
var c3 = new Coordinate(-1, 0);
var c4 = new Coordinate(1, 0);
var line2 = new LineString(new[] { c3, c4 });
d2.AddFeature(line2);

var inters = d1.Intersection(d2, FieldJoinType.All, null);
var feats = inters.Features;
foreach (var feat in feats)
{
    Console.WriteLine("{0}", feat.ToString());
}

The resulting feature set is always empty.

What am I doing wrong?

I also tried to swap the X and Y components for each coordinate (in case the first was assumed to be the longitude instead of the latitude)

Thanks!

EDIT: As per weston's comment below, I've changed the coordinates of the two lines to more obviously intersecting ones. The result is the same.

The intersection code you are using is basically a very limited shortcut. It is stuck with two conflicting ideas. The first idea is that featuresets all have the same feature type. That is, if you are working with a polygon featureset, all the features are polygons. The second idea is that the "intersection" of two lines is rarely a line. It is usually a point. So really that featureset intersecting code is designed to be used for intersecting polygons where the resulting shape is usually a polygon. It will also work for points where the results are always points. In your case, you probably want to find the union of the intersecting shapes, and throw out the shapes that do not intersect. You can accomplish this most easily by taking control of the features in a loop like the ones below. I have three examples, one that creates a point featureset from the intersections, and just assumes that all intersections will be points, one that keeps the original d1 feature if it intersects with a d2 feature, and another that unions all the intersecting d2 features with each d1 feature. This has the potential of creating some duplication of d2 content if a d2 feature intersects with more than one d1 feature. In any of these cases, it may not be clear what to do with the attributes, since the intersection could officially possess attributes that belong to multiple d2 shapes, not just one, so that would also have to be handled in a custom way that is meaningful for your specific application.

        var geoproj = DotSpatial.Projections.KnownCoordinateSystems.Geographic.World.WGS1984;

        var d1 = new FeatureSet { Projection = geoproj };
        //var c1 = new Coordinate(31.877484, 34.736723);
        //var c2 = new Coordinate(31.879607, 34.732362);
        var c1 = new Coordinate(0, -1);
        var c2 = new Coordinate(0, 1);
        var line1 = new LineString(new[] { c1, c2 });
        d1.AddFeature(line1);


        var d2 = new FeatureSet { Projection = geoproj };
        //var c3 = new Coordinate(31.882391, 34.73352);
        //var c4 = new Coordinate(31.875502, 34.734851);
        var c3 = new Coordinate(-1, 0);
        var c4 = new Coordinate(1, 0);
        var line2 = new LineString(new[] { c3, c4 });
        d2.AddFeature(line2);


        // To create a Point featureset with the intersections
        var result = new FeatureSet(FeatureType.Point) { Projection = geoproj };
        foreach (IFeature feature in d1.Features)
        {
            foreach (IFeature other in d2.Features)
            {
                if (feature.Intersects(other))
                {
                    result.AddFeature(feature.Intersection(other));
                }
            }
        }

        // To keep only d1 lines that intersect with d2
        result = new FeatureSet { Projection = geoproj };
        foreach(IFeature feature in d1.Features){
            foreach(IFeature other in d2.Features){
                if(feature.Intersects(other)){
                    result.AddFeature(feature); 
                }
            }
        }

        // Alternately to combine the intersecting lines into a cross
        result = new FeatureSet { Projection = geoproj };
        foreach (IFeature feature in d1.Features)
        {
            IFeature union = feature;
            Boolean keep = false;
            foreach (IFeature other in d2.Features)
            {
                if (feature.Intersects(other))
                {
                    union = union.Union(other);
                    keep = true;
                }
            }
            if (keep)
            {
                result.AddFeature(union);
            }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM