【POSTGIS】判定点位是否在范围内

项目上的需求,要判断当前打卡位置是否在项目范围10m内。

先做需求分析:

(1)既然有10m的缓冲范围,那么就要用到st_buffer函数;

(2)既然以m为单位,就要用到投影坐标系,如果前端传过来的是经纬度坐标,需要使用st_transform进行转换

(3)要求在范围内,就是判断点是否包含在面内,就需要用到st_contains函数

(4)根据业务上的需求,应该是要有项目的标识码来确定一条矢量记录

(5)返回值很简单,就是true或者false

组合以上的需求,得到如下sql语句

sql 复制代码
select st_contains(
               (select st_transform(st_buffer(st_transform(geom,4528),10),4490)
                FROM tablename
                where xmbm = '${xmbm}'),
                st_geomfromtext('POINT(${x} ${y})',4490)
        )

考虑实际情况可能有项目记录查不到的情况,这时候返回值是null,无法转换成要求的布尔值形式,集成到java程序中会报错,于是添加个case语句,防止返回null值

sql 复制代码
        with buffer_res as (
        select st_contains(
               (select st_transform(st_buffer(st_transform(geom,4528),10),4490)
                FROM tablename
                where xmbm = '${xmbm}'),
                st_geomfromtext('POINT(${x} ${y})',4490)
        ) res)
        select case when buffer_res.res is null then false else buffer_res.res end from buffer_res
相关推荐
骐骥13 个月前
开源WebGIS全流程常用技术栈
geoserver·postgis·webgis
李佩锦peijin4 个月前
Python导入Shapefile到PostGIS的常见问题和解决方案
python·postgresql·qgis·postgis
高冷的恒哥10 个月前
PostgreSQL10安装postgis插件
postgis
@王坤10 个月前
postgis函数学习
学习·postgis
LEILEI18A1 年前
go gin gorm连接postgres postgis输出geojson
go·gin·postgis·geojson·gorm
csucoderlee1 年前
mysql使用st_distance_sphere函数报错Incorrect arguments to st_distance_sphere
数据库·mysql·postgis
GIS从业者1 年前
postgis数据库从一张表中过滤出一部分数据到新表中
postgis
峥嵘371 年前
利用PostGIS自带工具导入shp数据
postgis
LEILEI18A1 年前
postgis mvt矢量切片 django drf mapboxgl
postgresql·django·postgis·mvt·矢量切片