[geos-devel] clipping a line between 2 points

Stephen Woodbridge woodbri at swoodbridge.com
Tue Oct 9 09:55:10 EDT 2007


Bart van den Eijnden (OSGIS) wrote:
> Hi list,
> 
> I am looking for a function which can do the following:
> 
> -clip a line between 2 points. So basically I want the line segment between
> two points to be returned.
> 
> Is there such a thing in GEOS?
> 
> Another question, what if the points don't exactly lie on the line, is there
> an algorithm for doing the same thing in that situation (i.e. first snap the
> points to the line, and then clip)?

Bart,

I do not believe there is a function(s) that will do this in GEOS, but 
the algorithm would be something like:

1) get the polyline segments
2) drop a normal from the points onto each segment
3) if the point is off the segment (ie on the projection of the segment 
beyond the end points), pick the nearest endpoint.
4) remember the nearest point and what segment it is on
5) construct a new polyline starting at the first nearest point on the 
segments, pick up the intervening points and end with the last nearest 
point.

That about does it. There are some corner cases you might want to check 
for, like a point is at the center of a circle and multiple segments lay 
on the perimeter. I would take the first point found for the closest. 
There are probably others that are escaping my grasp at the moment.

Hope this helps,
   -Steve



More information about the geos-devel mailing list