# How can I measure linestring on postgis?

I have a linestring and I want to measure this line. I try it on google maps. the result is that 0,65 km https://www.google.com/maps/dir/40.144048,26.45235/40.138443+26.450246/@40.1412448,26.4467258,16z/data=!3m1!4b1!4m6!4m5!1m0!1m3!2m2!1d26.450246!2d40.138443

and I measure it with postgis sql;

``````SELECT ST_Length(ST_GeomFromText('LINESTRING(40.144048 26.45235,40.138443 26.450246)'));
``````

the result is :0.00598688909200328 km

``````SELECT ST_Length_Spheroid('LINESTRING(40.144048 26.45235,40.138443 26.450246)',
CAST('SPHEROID["WGS 84",6378137,298.257223563]' As spheroid) );
``````

the result is :605.657141879302 km

I wonder that which one is correct lenght? please say some advice for me ?

• POSTGIS is your Data in Geography TYPE? gis.stackexchange.com/questions/6681/… then it should be correct
– Mapperz
Dec 3, 2014 at 21:34
• Are you sure that second value isn't 606 meters (not km) which would correspond to ~0.61 km? Dec 3, 2014 at 21:50
• The first PostGIS value is 0.005986... degrees, not km Dec 3, 2014 at 21:52

In your `ST_GeomFromText` code, you are putting the lat and long in EPSG:4326 coordinate system, so your distance is in degrees. Means 0.00598 is in degrees, not in km. Try to enter the coordinates in EPSG:3857, the result should be around 650 meters.

• The WKT is not necessarily EPSG:4326 lon/lat. You'd need to use `ST_GeomFromText('LINESTRING(40.144048 26.45235,40.138443 26.450246)', 4326)` Dec 3, 2014 at 23:43
When you wrote: `LINESTRING(40.144048 26.45235,40.138443 26.450246)` as the WKT, the results are Cartesian. It isn't necessarily degrees (in particular, it isn't in EPSG:4326). So the result of measuring it is just arbitrary Cartesian distance. So you can calculate it with a simple square-root of sum of squares. If you're assuming the input is in degrees, then the output is in degrees. If you're assuming the input is in nautical miles, the the output is in nautical miles.