St_buffer() 给出不准确的结果
St_buffer() giving inaccurate results
我有以下代码:
set @lon = 121.4732134;
set @lat = 31.2304321;
set @point = point(@lon, @lat);
set @radius = .5;
set @polygon = ST_Buffer(@point, @radius);
select l.city,l.latitude,l.longitude,
st_distance_sphere(l.latlngindex, point(@lon, @lat)) as distance
from table_locations l
where st_within(l.latlngindex, @polygon)
order by distance
;
它运行良好,给出了结果,但只有前 5 或 6 个是准确的 wrt 之间的距离。其余的都不准确,我在几个网站上验证了这一点。
Table 结构-
`locationid`,
`latitude`,
`longitude`,
`latlngindex` point not null,
spatial index `latlngindex` (`latlngindex`)
样本插入 :
insert into `table_locations` values(2001,31.2372705, 121.4705291, Point(121.2372705, 31.4705291));
insert into `table_locations` values(2002,31.2328741, 121.4741493, Point(121.2328741, 31.4741493));
insert into `table_locations` values(2003,31.2300200, 121.4749245, Point(121.2300200, 31.4749245));
insert into `table_locations` values(2004,31.2302308, 121.4705508, Point(121.2302308, 31.4705508));
insert into `table_locations` values(2005,31.2391562, 121.4771425, Point(121.2391562, 31.4771425));
insert into `table_locations` values(2006,31.2331857, 121.4779539, Point(121.2331857, 31.4779539));
示例结果行 :
Lat Long distance
31.2397267', '121.4742061', '35019.00977766075'
(31.2397267, 121.4742061) 和 (31.2304321, 121.4732134) 之间的距离应该是 1004 米,而它给出的是 35019 米。
看起来你混淆了纬度和经度
DROP TABLE IF EXISTS `table_locations`;
CREATE TABLE `table_locations` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`latitude` DOUBLE NOT NULL,
`longitude` DOUBLE NOT NULL,
PRIMARY KEY (`id`)
);
INSERT INTO `table_locations` VALUES (1, 31.2396691, 121.4798393);
INSERT INTO `table_locations` VALUES (2001, 31.2372705, 121.4705291);
INSERT INTO `table_locations` VALUES (2002, 31.2328741, 121.4741493);
INSERT INTO `table_locations` VALUES (2003, 31.2300200, 121.4749245);
INSERT INTO `table_locations` VALUES (2004, 31.2302308, 121.4705508);
INSERT INTO `table_locations` VALUES (2005, 31.2391562, 121.4771425);
INSERT INTO `table_locations` VALUES (2006, 31.2331857, 121.4779539);
INSERT INTO `table_locations` VALUES (2007, 31.2397267, 121.4742061);
SET @lat = 31.2304321;
SET @lon = 121.4732134;
SET @p = point(@lon, @lat);
SET @r = 43.0;
SET @POLY = ST_Buffer(@p, @r);
SELECT
id
, latitude
, longitude
, ST_Distance_Sphere(POINT(longitude, latitude), @p) as dist
FROM
table_locations
WHERE st_within(POINT(longitude, latitude), @POLY)
ORDER BY
dist
;
Returns 值并显示第一个位置的距离 1204.9090584034252
我有以下代码:
set @lon = 121.4732134;
set @lat = 31.2304321;
set @point = point(@lon, @lat);
set @radius = .5;
set @polygon = ST_Buffer(@point, @radius);
select l.city,l.latitude,l.longitude,
st_distance_sphere(l.latlngindex, point(@lon, @lat)) as distance
from table_locations l
where st_within(l.latlngindex, @polygon)
order by distance
;
它运行良好,给出了结果,但只有前 5 或 6 个是准确的 wrt 之间的距离。其余的都不准确,我在几个网站上验证了这一点。
Table 结构-
`locationid`,
`latitude`,
`longitude`,
`latlngindex` point not null,
spatial index `latlngindex` (`latlngindex`)
样本插入 :
insert into `table_locations` values(2001,31.2372705, 121.4705291, Point(121.2372705, 31.4705291));
insert into `table_locations` values(2002,31.2328741, 121.4741493, Point(121.2328741, 31.4741493));
insert into `table_locations` values(2003,31.2300200, 121.4749245, Point(121.2300200, 31.4749245));
insert into `table_locations` values(2004,31.2302308, 121.4705508, Point(121.2302308, 31.4705508));
insert into `table_locations` values(2005,31.2391562, 121.4771425, Point(121.2391562, 31.4771425));
insert into `table_locations` values(2006,31.2331857, 121.4779539, Point(121.2331857, 31.4779539));
示例结果行 :
Lat Long distance
31.2397267', '121.4742061', '35019.00977766075'
(31.2397267, 121.4742061) 和 (31.2304321, 121.4732134) 之间的距离应该是 1004 米,而它给出的是 35019 米。
看起来你混淆了纬度和经度
DROP TABLE IF EXISTS `table_locations`;
CREATE TABLE `table_locations` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`latitude` DOUBLE NOT NULL,
`longitude` DOUBLE NOT NULL,
PRIMARY KEY (`id`)
);
INSERT INTO `table_locations` VALUES (1, 31.2396691, 121.4798393);
INSERT INTO `table_locations` VALUES (2001, 31.2372705, 121.4705291);
INSERT INTO `table_locations` VALUES (2002, 31.2328741, 121.4741493);
INSERT INTO `table_locations` VALUES (2003, 31.2300200, 121.4749245);
INSERT INTO `table_locations` VALUES (2004, 31.2302308, 121.4705508);
INSERT INTO `table_locations` VALUES (2005, 31.2391562, 121.4771425);
INSERT INTO `table_locations` VALUES (2006, 31.2331857, 121.4779539);
INSERT INTO `table_locations` VALUES (2007, 31.2397267, 121.4742061);
SET @lat = 31.2304321;
SET @lon = 121.4732134;
SET @p = point(@lon, @lat);
SET @r = 43.0;
SET @POLY = ST_Buffer(@p, @r);
SELECT
id
, latitude
, longitude
, ST_Distance_Sphere(POINT(longitude, latitude), @p) as dist
FROM
table_locations
WHERE st_within(POINT(longitude, latitude), @POLY)
ORDER BY
dist
;
Returns 值并显示第一个位置的距离 1204.9090584034252