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) 五公里的所有数据

查询出来的数据以米为单位

 

posted @ 2020-05-20 17:50  菜鸟的进击  阅读(1758)  评论(0编辑  收藏  举报