[英]Intersection between two geographic lines
我正在使用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特征相交),另一个示例将所有相交的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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.