两条地理线之间的交集
本文关键字:之间 两条 | 更新日期: 2023-09-27 18:34:22
我正在使用 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 分量(以防第一个被假定为经度而不是纬度(
谢谢!
编辑:根据韦斯顿下面的评论,我已将两条线的坐标更改为更明显的相交坐标。结果是一样的。
您使用的交集代码基本上是一个非常有限的快捷方式。 它被两个相互冲突的想法所困。 第一个想法是所有功能集都具有相同的特征类型。 也就是说,如果您正在使用面要素集,则所有要素都是面。 第二个想法是,两条线的"交点"很少是一条线。 这通常是一个点。 因此,实际上,要素集相交代码旨在用于相交面,其中生成的形状通常是面。 它也适用于结果始终为点的点。 在您的情况下,您可能希望找到相交形状的并集,并抛出不相交的形状。 您可以通过控制循环中的功能来最轻松地完成此操作,如下所示。 我有三个示例,一个示例从交点创建一个点要素集,并假设所有交点都是点,一个示例在原始 d1 要素与 d2 要素相交时保留原始 d1 要素,另一个示例将所有相交的 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);
}
}