两条地理线之间的交叉点 [英] Intersection between two geographic lines

查看:162
本文介绍了两条地理线之间的交叉点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 DotSpatial C#库,我试图使用下面的代码来尝试找到两行之间的交点(我知道它们相交)

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

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

var d2 = new FeatureSet {Projection = geoproj};
// var c3 =新坐标(31.882391,34.73352);
// var c4 =新坐标(31.875502,34.734851);
var c3 =新坐标(-1,0);
var c4 =新坐标(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分量(假设第一个坐标是经度而不是纬度)

谢谢!

编辑: Weston的评论如下,我已经将两条线的坐标更改为更明显相交的坐标。结果是一样的。

解决方案

您使用的十字路口基本上是一个非常有限的捷径。它陷入了两个相互冲突的想法。第一个想法是特征集都具有相同的特征类型。也就是说,如果您正在使用多边形特征集,则所有特征都是多边形。第二个想法是两条线的交点很少是一条线。它通常是一个点。所以真正的特征集相交代码被设计用于相交的多边形,其中所得到的形状通常是多边形。它也适用于结果始终为点的点。在你的情况下,你可能想找到相交形状的联合,并丢弃不相交的形状。您可以通过控制下面的循环中的功能来最轻松地完成此操作。我有三个例子,一个从交点创建一个点特征集,并且假设所有交点都是点,一个如果与d2特征相交时保持原始d1特征,另一个则将所有相交的d2特征与每个d1功能。如果一个d2特征与多个d1特征相交,这就有可能创建一些d2内容的重复。在这些情况中的任何一种情况下,可能不清楚如何处理这些属性,因为交集可能正式拥有属于多个d2形状的属性,而不仅仅是一个,所以还必须以自定义的方式处理有意义的为您的具体应用程序。

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

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


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


//创建包含交点的点特征集
var result = new FeatureSet(FeatureType.Point){Projection = geoproj};
foreach(d1.Features中的IFeature特性)
{
foreach(d2.Features中的IFeature其他)
{
if(feature.Intersects(other))
{
result.AddFeature(feature.Intersection(other));
}
}
}

//仅保留与d2相交的d1行
result = new FeatureSet {Projection = geoproj};
foreach(d1.Features中的IFeature特性){
foreach(d2.Features中的IFeature other){
if(feature.Intersects(other)){
result.AddFeature特征);
}
}
}

//交叉将交叉线组合成交叉
result = new FeatureSet {Projection = geoproj};
foreach(d1.Features中的IFeature特性)
{
IFeature union = feature;
布尔keep = false;
foreach(ifeature other in d2.Features)
{
if(feature.Intersects(other))
{
union = union.Union(other);
keep = true;


if(keep)
{
result.AddFeature(union);
}
}


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

这篇关于两条地理线之间的交叉点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆