两条地理线之间的交汇点

时间:2013-10-07 08:04:47

标签: c# geometry gis intersection dotspatial

我正在使用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());
}

生成的功能集始终为空。

我做错了什么?

我还试图为每个坐标交换X和Y分量(假设第一个被认为是经度而不是纬度)

谢谢!

编辑:根据下面的韦斯顿评论,我已经将两条线的坐标更改为更明显的相交线。结果是一样的。

1 个答案:

答案 0 :(得分:2)

您正在使用的交叉口代码基本上是一个非常有限的快捷方式。它坚持两个相互矛盾的想法。第一个想法是功能集都具有相同的功能类型。也就是说,如果使用多边形要素集,则所有要素都是多边形。第二个想法是两条线的“交叉点”很少是一条线。这通常是一个观点。实际上,特征集交叉代码被设计用于交叉多边形,其中生成的形状通常是多边形。它也适用于结果始终为点的点。在您的情况下,您可能希望找到相交形状的并集,并抛出不相交的形状。您可以通过控制循环中的功能来最轻松地完成此操作,如下所示。我有三个例子,一个从交叉点创建一个点特征集,并假设所有交点都是点,一个保持原始d1特征(如果它与d2特征相交),另一个将所有相交的d2特征与每个d1功能。如果d2特征与多个d1特征相交,则可能会产生一些d2内容的重复。在任何这些情况下,可能不清楚如何处理属性,因为交集可以正式拥有属于多个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);
            }
        }