MySQL中的point用于表示GIS中的地理坐标,在GIS中广泛使用,本文主要讲解point类型的简单使用
CREATE TABLE `test-point` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '序号',
`point` point NOT NULL COMMENT '经纬度',
`text` varchar(50) DEFAULT NULL COMMENT '描述',
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=10 DEFAULT CHARSET=utf8mb4;
INSERT INTO `test-point` ( point,text ) VALUES ( st_GeomFromText ( 'POINT(1 1)' ),'第1个点');
INSERT INTO `test-point` ( point,text ) VALUES ( st_PointFromText ( 'POINT(2 2)' ),'第2个点');
注意:其中st_GeomFromText ,st_PointFromText 是将字符串转换成point类型的数据,这两个都可以,其中st_GeomFromText 是父级,st_PointFromText 是子级.
SELECT id,st_x(point) x,st_y(point) y,point,text FROM `test-point`
SELECT id,st_AsText(point),text FROM `test-point`
注意:st_x(point)是获取POINT(1 2)中的1,st_y(point)是获取POINT(1 2)中的2,st_AsText(point)是将point类型转换成字符串.
update `test-point` set point=st_PointFromText('POINT(5 5)') where id =10;
update `test-point` set point=st_GeomFromText('POINT(6 6)') where id =10;
SELECT
id,
st_x ( point ) latitude,
st_y ( point ) longitude,
round(( ST_DISTANCE_SPHERE ( st_GeomFromText ( 'POINT (1 1)' ), point )), 1 ) AS distance
FROM
`test-point`
注意:st_distance_sphere函数是将坐标距离转换成米也可以使用st_distance
st_distance_sphere函数的计算结果要比st_distance转换为米的结果更精确。本人测试st_distance函数发现,随着点与点之间的距离增加误差越来越大.
SELECT
id,
st_x ( point ) latitude,
st_y ( point ) longitude,
round(( ST_DISTANCE_SPHERE ( st_GeomFromText ( 'POINT (1 1)' ), point )), 1 ) AS distance
FROM
`test-point`
HAVING
distance <=2000