Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PostgreSQL latitude longitude query

i have latitude and longitude columns in location table in PostgreSQL database, and I am trying to execute distance query with a PostgreSQL function.

I read this chapter of the manual:

https://www.postgresql.org/docs/current/static/earthdistance.html

but I think I'm missing something there.

How should I do that? Are there more examples available

like image 428
Idan Avatar asked Apr 05 '12 19:04

Idan


People also ask

What data type is latitude in PostgreSQL?

In PostGIS, for points with latitude and longitude there is geography datatype. Order is Longitude, Latitude - so if you plot it as the map, it is (x, y).

What data type is longitude?

p. precision you should use DECIMAL . Latitudes range from -90 to +90 (degrees), so DECIMAL(10,8) is ok for that, but longitudes range from -180 to +180 (degrees) so you need DECIMAL(11,8) .

How do you find the distance between two latitudes and longitudes?

from math import cos, asin, sqrt, pi def distance(lat1, lon1, lat2, lon2): p = pi/180 a = 0.5 - cos((lat2-lat1)*p)/2 + cos(lat1*p) * cos(lat2*p) * (1-cos((lon2-lon1)*p))/2 return 12742 * asin(sqrt(a)) #2*R*asin... And for the sake of completeness: Haversine on Wikipedia.

How do you find the distance between two points in PostGIS?

SELECT ST_Distance_Sphere(ST_MakePoint(103.776047, 1.292149),ST_MakePoint(103.77607, 1.292212)); which gives 7.457 meters. Your second set of points are 62.74 meters away from each other, based on the same query.


2 Answers

Here's another example using the point operator:

Initial setup (only need to run once):

create extension cube; create extension earthdistance; 

And then the query:

select (point(-0.1277,51.5073) <@> point(-74.006,40.7144)) as distance;       distance      ------------------  3461.10547602474 (1 row) 

Note that points are created with LONGITUDE FIRST. Per the documentation:

Points are taken as (longitude, latitude) and not vice versa because longitude is closer to the intuitive idea of x-axis and latitude to y-axis.

Which is terrible design... but that's the way it is.

Your output will be in miles.

Gives the distance in statute miles between two points on the Earth's surface.

like image 60
Steve Tauber Avatar answered Sep 19 '22 16:09

Steve Tauber


This module is optional and is not installed in the default PostgreSQL instalatlion. You must install it from the contrib directory.

You can use the following function to calculate the approximate distance between coordinates (in miles):

 CREATE OR REPLACE FUNCTION distance(lat1 FLOAT, lon1 FLOAT, lat2 FLOAT, lon2 FLOAT) RETURNS FLOAT AS $$ DECLARE                                                        x float = 69.1 * (lat2 - lat1);                                y float = 69.1 * (lon2 - lon1) * cos(lat1 / 57.3);         BEGIN                                                          RETURN sqrt(x * x + y * y);                                END   $$ LANGUAGE plpgsql; 
like image 24
strkol Avatar answered Sep 18 '22 16:09

strkol