# How do you determine how straight a LineString is? [duplicate]

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?

-

## marked as duplicate by Fezter, PolyGeo♦, BradHards, Simbamangu, Devdatta TengsheMay 2 '14 at 6:19

Sounds like you're calculating the sinuosity, which seems pretty right-on to me. – dbaston May 1 '14 at 16:07
@dbaston thanks! Now I have something to Google. – ilia choly May 1 '14 at 16:36
Why is this a duplicate? The linked question has to do with sinuosity, but the similarity ends there. – dbaston May 2 '14 at 13:04

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 May 1 '14 at 19:22