简体   繁体   English

两条地理线之间的交点

[英]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) 我正在使用DotSpatial C#库,并且尝试使用以下代码来查找两条线之间的交点(我知道它们确实相交)

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) 我还尝试为每个坐标交换X和Y分量(如果假定第一个是经度而不是纬度)

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. 我有三个示例,一个示例从交点​​创建一个点要素集,并假设所有的交点都是点,一个示例将保留原始d1特征(如果与d2特征相交),另一个示例将所有相交的d2特征与每个d1功能。 This has the potential of creating some duplication of d2 content if a d2 feature intersects with more than one d1 feature. 如果d2特征与多个d1特征相交,则可能会造成d2内容重复。 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. 在任何一种情况下,都不清楚如何处理属性,因为相交可以正式拥有属于多个d2形状的属性,而不仅仅是一个,因此也必须以一种自定义方式处理对您的特定应用有意义。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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