PostgreSQL latitude longitude query

后端 未结 5 747
故里飘歌
故里飘歌 2020-12-13 02:31

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

5条回答
  •  孤城傲影
    2020-12-13 03:11

    Assuming you've installed the earthdistance module correctly, this will give you the distance in miles between two cities. This method uses the simpler point-based earth distances. Note that the arguments to point() are first longitude, then latitude.

    create table lat_lon (
      city varchar(50) primary key,
      lat float8 not null,
      lon float8 not null
    );
    
    insert into lat_lon values
    ('London, GB', 51.67234320, 0.14787970),
    ('New York, NY', 40.91524130, -73.7002720);
    
    select 
      (
      (select point(lon,lat) from lat_lon where city = 'London, GB') <@>
      (select point(lon,lat) from lat_lon where city = 'New York, NY')
      ) as distance_miles
    
    distance_miles
    --
    3447.58672105301
    

提交回复
热议问题