我对与多边形相交的线段有疑问。我有一些线(绿色)代表一些步行路径,一些约束多边形(黑色)代表多边形。
首先,我使用类似这样的方法创建多边形:
var geometryFactory = NtsGeometryServices.Instance.CreateGeometryFactory(srid:4326);
var vertices = new List<Coordinate>();
foreach (var coords in stringPolygon)
{
var coordinates = coords.Split(",");
var x = double.Parse(coordinates[0]);
var y = double.Parse(coordinates[1]);
var newCoordinate = new Coordinate(y,x);
vertices.Add(newCoordinate);
}
var outerRing = geometryFactory.CreateLinearRing(vertices.ToArray());
spatialData.FieldPolygon = geometryFactory.CreatePolygon(outerRing);
然后创建这样的线串:
var vertices = new List<Coordinate>();
foreach (var trip in tripSegments.Data)
{
var newCoordinate = new Coordinate(trip.Lng, trip.Lat);
vertices.Add(newCoordinate);
}
spatialData.TripLine = geometryFactory.CreateLineString(vertices.ToArray());
尝试相交
var intersect = spatialData.FieldPolygon.Boundary.Intersection(spatialData.TripLine);
也有所不同但没有运气
var intersect = spatialData.FieldPolygon.Boundary.Difference(spatialData.TripLine);
也尝试过使用WKT Reader和交集和差异的组合,例如这样(我想知道这是否是正确的方法):
var reader = new WKTReader();
var targetMultiPolygon = reader.Read(spatialData.TripLine.ToString());
var bufferPolygon = reader.Read(spatialData.FieldPolygon.Boundary.ToString());
var intersection = targetMultiPolygon.Intersection(bufferPolygon);
var targetClipped = targetMultiPolygon.Difference(intersection);
var wktTargetAfterClip = targetClipped.ToString();
但是我遇到了这样的错误(使用WKT方法时)。
TopologyException: found non-noded intersection between LINESTRING(26.563827556466403 43.52431484490672, 26.56386783617048 43.52429417990681) and LINESTRING(26.565492785081837 43.52349421574761, 26.56386783617048 43.52429417990681) [ (26.56386783617048, 43.52429417990681, NaN) ]
更新
我已使用
解决了WKT Reader的拓扑问题var bufferPolygon = reader.Read(spatialData.FieldPolygon.Boundary.ToString());
在阅读器上,我添加了Boundary
属性和该固定拓扑问题。但是仍然存在主要问题,正如在此处可以看到的那样。
我使用了这段代码来提取不相交的线,并将其添加到不同的图层,但是正如您在红色正方形中看到的那样,有些线是绿色的,因此它们应该是紫色的(虚线),因为它们是黑色多边形之外,但它们仍是绿色。
var outsideLines = new List<ILineString>();
foreach (ILineString lineString in geometries.Geometries)
{
var isIntersecting = lineString.Intersects(spatialData.FieldPolygon.Boundary);
if (!isIntersecting)
{
outsideLines.Add(lineString);
}
}
spatialData.IntersectedMultiLine = geometryFactory.CreateMultiLineString(outsideLines.ToArray());
现在,我的主要问题是:是否可以提取多边形外的线,而我是否在正确的轨道上?