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
这样查出一个矩形符合条件的数据