mysql 空间函数

ST_GeomFromText:将文本表示的几何对象转换为几何对象。

SELECT ST_GeomFromText('POINT(1 1)');

ST_AsText:将几何对象转换为文本表示。

SELECT ST_AsText(ST_GeomFromText('POINT(1 1)'));

ST_Contains:判断一个几何对象是否包含另一个几何对象。

SELECT ST_Contains(ST_GeomFromText('POLYGON((0 0, 0 2, 2 2, 2 0, 0 0))'), ST_GeomFromText('POINT(1 1)'));

ST_Distance:计算两个几何对象之间的距离。

SELECT ST_Distance(ST_GeomFromText('POINT(1 1)'), ST_GeomFromText('POINT(2 2)'));

ST_Intersection:计算两个几何对象的交集。

SELECT ST_Intersection(ST_GeomFromText('POLYGON((0 0, 0 2, 2 2, 2 0, 0 0))'), ST_GeomFromText('POLYGON((1 1, 1 3, 3 3, 3 1, 1 1))'));

ST_Buffer:根据给定的半径创建一个缓冲区。

SELECT ST_Buffer(ST_GeomFromText('POINT(1 1)'), 1);

ST_DISTANCE_SPHERE: 计算给定的坐标点到目标的坐标点距离

select ST_DISTANCE_SPHERE(position,POINT(#{longitude} , #{latitude})) distance 

ST_Within: 判断一个点是否在由坐标点组成的几何内

select ST_Within(GeomFromText('POINT(${longitude} ${latitude})'), points) ;

JAVA代码处理坐标点信息:

obj.setPosition("POINT(" + longitude + " " + latitude + ")");

mapper.xml 需要将坐标转换为point类型的文本
st_PointFromText(#{position})

同样修改的时候:
update table_name set position = st_PointFromText(#{position})

查询将point类型的数据转换为文本或获取经纬度
  select   AsText(position) position,
           ST_X(position) longitude,
           ST_Y(position) latitude
  from table_name


根据距离排序查询:

 select
          ST_X(position) longitude,
          ST_Y(position) latitude,
          ST_DISTANCE_SPHERE(position,POINT(#{longitude} , #{latitude})) distance
 from table_name
 where #{maxRadius} >= ST_DISTANCE_SPHERE(position,POINT(#{longitude} , #{latitude}))
 order by distance asc

区域范围查询: 给定一个坐标点:

如: 116.396733,39.924775

查询300米范围内的数据:

116.393733  /  39.921775

116.399733  /  39.927775

sql:

116.399733 > 经度 > 116.393733 

and 

39.927775 > 纬度 > 39.921775

这样查出一个矩形符合条件的数据

  • 8
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值