I have question regarding intersecting line segment with polygon. I have some line (green) that represents some walking path and some restriction polygon (black) that represents polygon.
I'm now wondering if it is possible to extract line segments that are outside polygon (red lines in upper left corner)
First, I created polygon using something like this:
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);
Then created linestring like this:
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());
Tried with Intersection
var intersect = spatialData.FieldPolygon.Boundary.Intersection(spatialData.TripLine);
And also with difference but without an luck
var intersect = spatialData.FieldPolygon.Boundary.Difference(spatialData.TripLine);
Also tried with WKT Reader and combination of intersection and difference like this (I'm wondering if this is even right approach):
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();
But I got error like this (when using WKT approach).
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) ]
UPDATE
I've fixed the topology issue whit WKT Reader with
var bufferPolygon = reader.Read(spatialData.FieldPolygon.Boundary.ToString());
on reader I added Boundary
property and that fixed topology issue. But main problem still remains as it can be seen here.
I used this block of code to extract lines that don't intersect and that are added to different layer but as you can see in red square there are some lines that are green and they should be purple (dashed) because they are outside black polygon but they are still green.
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());
Now, my main questions is: is it possible to extract lines that are outside polygon and am I on the right track?