gpt4 book ai didi

多边形内的 mysql 查询点 - 无结果

转载 作者:IT老高 更新时间:2023-10-29 00:22:00 26 4
gpt4 key购买 nike

我很确定我在这里做错了很多事情,但我不确定是什么......

表格(减去几个字段):

CREATE TABLE IF NOT EXISTS `stuff` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`lat` decimal(12,7) NOT NULL,
`lon` decimal(12,7) NOT NULL,
`location` point NOT NULL,
UNIQUE KEY `id` (`id`),
KEY `distance` (`distance`),
KEY `lat` (`lat`),
KEY `lon` (`lon`),
SPATIAL KEY `location` (`location`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=5;

自动设置定位点的触发器:

DROP TRIGGER IF EXISTS `stuff_insert_defaults`;
DELIMITER //
CREATE TRIGGER `stuff_insert_defaults` BEFORE INSERT ON `stuff`
FOR EACH ROW SET NEW.location = PointFromText(CONCAT('POINT(',NEW.lat,' ',NEW.lon,')'))
//
DELIMITER ;

DROP TRIGGER IF EXISTS `stuff_update_location`;
DELIMITER //
CREATE TRIGGER `stuff_update_location` BEFORE UPDATE ON `stuff`
FOR EACH ROW SET NEW.location = PointFromText(CONCAT('POINT(',NEW.lat,' ',NEW.lon,')'))
//
DELIMITER ;

一些样本数据(科罗拉多州内的 4 个随机点):

/* 4 random points within the state of colorado */
INSERT INTO `stuff` (`id`, `distance`, `lat`, `lon`, `location`) VALUES
(1, 5.0000000, 40.2488450, -103.8003460, 0x000000000101000000f6622827da1f444001df6dde38f359c0),
(2, 5.0000000, 38.4849180, -107.8726700, 0x000000000101000000f19e03cb113e4340d28c45d3d9f75ac0),
(3, 5.0000000, 39.5040250, -105.3584800, 0x000000000101000000e6ae25e483c0434049111956f1565ac0),
(4, 5.0000000, 39.1904180, -106.8179680, 0x000000000101000000ed48f59d5f9843402b4b749659b45ac0);

科罗拉多州的粗略外部边界:

NW corner: 41.000497 -109.050149 
NE corner: 41.002380 -102.051881
SE corner: 36.993237 -102.041959
SW corner: 36.999037 -109.045220

应该返回我们插入的 4 条记录的查询:

SELECT *, AsText(location) FROM stuff 
WHERE Contains(
GeomFromText('POLYGON((41.000497 -109.050149, 41.002380 -102.051881, 36.993237 -102.041959, 36.999037 -109.045220, 41.000497 -109.050149))'), location );

我要返回的是...

nada... 
zip...
nil...
nothing...

最佳答案

Class Polygon 中所述(强调):

Polygon Assertions

  • The boundary of a Polygon consists of a set of LinearRing objects (that is, LineString objects that are both simple and closed) that make up its exterior and interior boundaries.

因此,您必须通过在起点完成来闭合多边形:

SELECT *, AsText(location) FROM stuff 
WHERE Contains(
GeomFromText('POLYGON((41.000497 -109.050149, 41.002380 -102.051881, 36.993237 -102.041959, 36.999037 -109.045220, 41.000497 -109.050149))'), location );

sqlfiddle 上查看.

关于多边形内的 mysql 查询点 - 无结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17073156/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com