PostgreSQL 添加postgis插件實現獲取經緯度間的距離


概念: geometry和geography的區別

geometry:planar 平面坐標系【supported by SQL Server conforms to the Open Geospatial Consortium (OGC) Simple Features for SQL Specification version 1.1.0.】
geography: terrestrial 地理坐標系【stores ellipsoidal (round-earth) data, such as GPS latitude and longitude coordinates.】

安裝 postgresql-12 可postgis 網上查找與postgresql對應版本的postgis

   sudo apt install postgresql-12
   sudo add-apt-repository ppa:ubuntugis/ppa
   sudo apt-get install postgis
   sudo -i -u postgres

安轉完成之后:執行

sudo -i -u postgres

進入psql命令行/切換到需要的gis的數據庫執行

CREATE EXTENSION postgis;
CREATE EXTENSION postgis_topology;

之后在所需表新建字段

alter 表名
    add where_is geography;

將經緯度信息轉到geography

update表名 set where_is=ST_POINT(lat,lng) where 1=1;
lat 為單獨字段,lng為單獨字段

具體查詢信息

select  *, st_distance(t_shop.where_is,ST_POINT(31.23037,121.4737)) as distance   FROM t_shop  where st_distance(t_shop.where_is,ST_POINT(31.23037,121.4737))  < 5000 ORDER BY distance;
查詢距離坐標點(31.23037,121.4737) 五公里的所有數據

查詢出來的數據以米為單位

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM