PostgreSQL latitude longitude query

后端 未结 5 745
故里飘歌
故里飘歌 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:10

    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.

提交回复
热议问题