使用PostGis函数实现查询多边形范围内的数据

本文实现过程是基于瀚高数据库 瀚高数据库里装了PostGis插件 这样才可以实现该操作。

请确保使用的数据库支持PostGIS或者装了PostGIS插件。

先在现有的表增加一个类型为 geometry(point,4326) 的字段。

alter table 表名 add 字段名 geometry(point,4326);

造一些假数据

INSERT INTO `dm_sample_point` (location, longitude, latitude) VALUES ('point(112.89763 9.54895)', '112.89763', '9.54895');
INSERT INTO `dm_sample_point` (location, longitude, latitude) VALUES ('point(113.89763 9.64895)', '113.89763', '9.64895');
INSERT INTO `dm_sample_point` (location, longitude, latitude) VALUES ('point(114.89763 9.74895)', '114.89763', '9.74895');
INSERT INTO `dm_sample_point` (location, longitude, latitude) VALUES ('point(115.89763 9.84895)', '115.89763', '9.84895');

location为点对象 坐标系为4326。

longitude是经度。

latitude是维度。

 @Select({" <script> " +
            " select * from 表名 test" +
            " <where> " +
            "test.island_id = #{id} " +
            "<when test=\"locations !=null and locations.size>0\">" +
            "and ST_CONTAINS(" +
            "ST_TRANSFORM(ST_SetSRID(ST_GEOMFROMTEXT('POLYGON((" +
            "<foreach item = 'item' collection = 'locations' index = 'index' separator = ','>" +
            "${item.longitude} ${item.latitude}" +
            "</foreach>" +
            "))'), 4326), ST_SRID(test.location))," +
            "test.location)" +
            "</when>" +
            "</where>" +
            " </script> "})

该SQL是接收一个名为locations的List集合 集合里是对象 对象里是经度和维度两个字段。

在SQL中循环该集合中的值 通过PostGIS函数来形成多边形。

通过ST_CONTAINS函数来查询出 location 字段的经纬度是否在该多边形中。

ST_SetSRID是设置坐标系为4326 这一步必须 不然会报错。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值