4

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.

You can see in image below: enter image description here

I'm now wondering if it is possible to extract line segments that are outside polygon (red lines in upper left corner) enter image description here

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. enter image description here

                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?

Svinjica
  • 2,389
  • 2
  • 37
  • 66
  • 1
    The exception says that your Linestring is invalid and with invalid data, the library cannot process a dif or intersection calculation. You'd have to fix your source data first (most likely self intersecting line strings) – MichaC May 22 '20 at 12:38
  • Does this answer your question? [What does the following error mean: TopologyException: found non-nonded intersection between LINESTRING](https://stackoverflow.com/questions/13662448/what-does-the-following-error-mean-topologyexception-found-non-nonded-intersec) – MichaC May 22 '20 at 12:39
  • @MichaC Thank you for comment. So, if I understood correctly....if we ignore topological error, workflow above should work?. Or its not possible to extract lines that are outside polygon with NetTopology Suit.. – Svinjica May 22 '20 at 12:44
  • its totally possible to calculate differences or intersections, yeah, and, in your case you'd basically just have to test if the there is any part of the linestring outside the black polygon. – MichaC May 22 '20 at 12:51
  • @MichaC Ok, thank you...Can u please post some link or example of approach how to do it because I'm not finding any. You can post it as answer, i ll marked as valid. – Svinjica May 22 '20 at 12:54

0 Answers0