I have a bunch of LINESTRINGs and need to classify them based on how strait they are. My idea is to compare the length of the LINESTRING to the distance between the first and last POINTs.

I'm thinking of something along these lines (assume the srs units are in meters):

ST_Length_Spheroid(geom) - ST_Distance_Spheroid(ST_PointN(geom, 1), ST_PointN(geom, St_NumPoints(geom)))

Is there a better way to do this?

  • 4
    Sounds like you're calculating the sinuosity, which seems pretty right-on to me.
    – dbaston
    Commented May 1, 2014 at 16:07
  • 1
    Why is this a duplicate? The linked question has to do with sinuosity, but the similarity ends there.
    – dbaston
    Commented May 2, 2014 at 13:04

2 Answers 2


Assuming you add the appropriate ellipsoid parameter to the ST_Distance_Spheroid and ST_Length_Spheroid functions, your approach looks sound for calculating sinuosity (as dbaston said).

Another approach to consider is to calculate maximum deviation (or offset) of the intermediate points from the geodesic (ie, 'straight') line. The question then becomes "How to compute/create a geodesic in PostGIS". I'm unsure if ST_Project can be used for such a purpose.


I do not know the exact syntax implementation in postgis but you can calculate sinuosity with the following pseudo-code:

d = sqrt( (startPoint[X] - endPoint[X]) ** 2 + (startPoint[Y] - endPoint[Y]) ** 2 )
  sinuosity = (d / lineLength)
  • The problem with that is it uses planar (Euclidean) geometry. The question proposes using the accurate spheroidal forms of the distance and length functions.
    – Martin F
    Commented May 1, 2014 at 19:22

Not the answer you're looking for? Browse other questions tagged or ask your own question.