Is it possible to intersect/clip line with polygon?

34 views
Skip to first unread message

Neven

unread,
May 22, 2020, 8:18:17 AM5/22/20
to NetTopologySuite
Hi all. 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:

picture_1.png

I'm now wondering if it is possible to extract line segments that are outside polygon (red lines)




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:

                var reader = new WKTReader();

               
var targetMultiPolygon = reader.Read(spatialData.TripLine.ToString());
               
var bufferPolygon = reader.Read(spatialData.FieldPolygon.ToString());

               
var intersection = targetMultiPolygon.Intersection(bufferPolygon);
               
var targetClipped = targetMultiPolygon.Difference(intersection);
               
var wktTargetAfterClip = targetClipped.ToString();


But I got error like this.

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) ]

Now, my questions is if it is possible to extrect lines that are outside polygon?



FObermaier

unread,
Jun 10, 2020, 2:33:19 AM6/10/20
to NetTopologySuite
Your approach of intersection and difference is the right way to handle this.
You need to ensure your input geometries are valid and you need to use a precision model to avoid the TopologyException.
Create your WKTReader using a GeometryFactory with a scaled precision model.

Neven

unread,
Jun 10, 2020, 3:03:51 AM6/10/20
to NetTopologySuite
@FOber,aier can you please explain what dou you mean by  Create your WKTReader using a GeometryFactory with a scaled precision model Could you provide me some example if it is possible?

Felix Obermaier

unread,
Jun 10, 2020, 4:44:03 AM6/10/20
to nettopol...@googlegroups.com

var gf = new GeometryFactory(new PrecisionModel(1e6));

var reader = new WKTReader(gf);

 

 

Von: nettopol...@googlegroups.com <nettopol...@googlegroups.com> Im Auftrag von Neven
Gesendet: Mittwoch, 10. Juni 2020 09:04
An: NetTopologySuite <nettopol...@googlegroups.com>
Betreff: Re: Is it possible to intersect/clip line with polygon?

 

@FOber,aier can you please explain what dou you mean by  Create your WKTReader using a GeometryFactory with a scaled precision model Could you provide me some example if it is possible?

Dana petak, 22. svibnja 2020. u 14:18:17 UTC+2, korisnik Neven napisao je:

Hi all. 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:

 

 

I'm now wondering if it is possible to extract line segments that are outside polygon (red lines)

 

Something like this (red lines in upper left corner)

--
You received this message because you are subscribed to the Google Groups "NetTopologySuite" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nettopologysui...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nettopologysuite/cd1b763d-b16e-4e07-8f84-a5b34e4a8d50o%40googlegroups.com.

Neven

unread,
Jun 10, 2020, 6:36:00 AM6/10/20
to NetTopologySuite
@FObermair Thank you. I ll try it.  Provided line of code new PrecisionModel(1e6)) what does it represent? Scaled Precision and why we did you choose exactly this one? Can we choose some other precision model? Thank you once again


Dana petak, 22. svibnja 2020. u 14:18:17 UTC+2, korisnik Neven napisao je:

FObermaier

unread,
Jun 10, 2020, 8:56:45 AM6/10/20
to NetTopologySuite

#WhereIsWaldo.png



Am Freitag, 22. Mai 2020 14:18:17 UTC+2 schrieb Neven:
Reply all
Reply to author
Forward
0 new messages