[GIS] Determining what side of line point is on using PostGIS

postgis

I want this in order to generate house numbers, odd or even depending on wether a feature is on one side of a street line or not . Thanks


In the end i think the most portable way is to use this( BTW line is actually 'linestring' not segment):

ST_LineCrossingDirection( linefromAtoB,streetline) <0 where A is the point , B is ST_ShortestPoint(thepoint,streetline); if <0 then linefromAtoB enters through the left of The streetline; if >0 the it enters from right side of streetline). You have to determine what street line is closest to each point first.


I made a mistake:
C is the symmetric of A in relation to B= ST_ShortestPoint(thepoint,streetline).
C's coordinates are XC=2*(XB-XA) -XA and for y YC=2*(YB-YA) -YA.
ST_LineCrossingDirection( linefromAtoC,streetline) <0 where A is the point , ; if <0 then linefromAtoC enters through the left of The streetline; if >0 the it enters from right side of streetline). You have to determine what street line is closest to each point first.

Best Answer

If you are doing this in code, start by getting the closest point on the line to the point:

http://paulbourke.net/geometry/pointlineplane/

Note you will need to test each segment of a LINESTRING, and make sure the point falls on the segment as the raw computation is for an infinite line.

Then get the vector from this point to the line (pt.x - intersection.x, pt.y - intersection.y)

This will be a perpendicular to the line, within floating point rounding errors.

A line with slope x, y (end.x - start.x, end.y - start.y) has two perpendiculars.

-y, x is to the left, y, -x is to the right. Compare to your vector. You will only need to compare the signs of the x and y components.

There are other ways to do this and if you need very high performance this may not be the fastest.

Related Question