2

Spatialite has a the ability to calculate the distance between 2 geometries with it's Distance() function. There are other functions that work on LINESTRINGs. However I can't find out what units it returns it in. Is it metres? If I have 2 points, how do I calculate the distance between them in a spatialite query?

(For the record I'm using SRID 4326, i.e. WSG 86, i.e. the old traditional degrees of latitude and longitude).

MrValdez
  • 8,515
  • 10
  • 56
  • 79
Amandasaurus
  • 58,203
  • 71
  • 188
  • 248

2 Answers2

11

the unit returned by ST_Distance(), ST_Length() and ST_Area() exactly is the one defined by the corresponding SRID.

consequently, if you are using latitude and longitude (SRID=4326, WGS 84), any length will be measured in DEGREES, and any area in SQUARE DEGREES.

if you are interested in giving a more conventional unit (METERS, SQUARE METERS), you simply have to project your geometries into some appropriate 'planar' CRS (e.g. UTM) using ST_Transform()

Sandro Furieri
  • 126
  • 1
  • 2
  • Could you give an example of how to do this conversion in a function/expression? – Amandasaurus Nov 28 '11 at 10:42
  • 2
    @Rory Each degree latitude is exactly 60 nautical miles apart. That's 111,120 meters or 364,567 feet. Just multiply the value from Distance() with 111,120 to get the distance in meters. – Phil Nov 13 '12 at 21:37
  • @Phil I would expect that the latitude of each degree (in meters, miles etc.) depends on the geographic longitude and latitude (since the earth is not a perfect sphere). – Lefteris Mar 31 '14 at 05:04
  • @Lefteris Distance between each degree longitude varies depending on your geographic location, but distance between each degree latitude [remains a constant 60 nautical miles](http://geography.about.com/od/learnabouttheearth/a/nauticalmiles.htm). The important thing is the conversion ratio between latitude degrees and meters. Once you know that, you can apply it to the output of Distance(). – Phil Mar 31 '14 at 13:45
0

In version 2.4.4 and higher there is a function PtDistWithin() which returns meters for a distance query. See doc. section "SQL functions for distance relationships": http://www.gaia-gis.it/spatialite-2.4.0-4/spatialite-sql-2.4-4.html#p13

Stefan
  • 1,036
  • 1
  • 10
  • 14