在 MySQL 5.6 的多边形内部找到 Long/Lat
Find Long/Lat inside of polygon with MySQL 5.6
我有一个 locations
table,其中有很多行的位置和数据。其中有 2 列 - 经度和纬度。我想从 table 中找到定义的地图多边形区域中的记录。
我有第二个 table,我刚刚根据一些名为 polyThing
的教程创建了它,它具有我定义的边界...
CREATE TABLE polyThing
(
`ID` int auto_increment primary key,
`boundary` polygon not null,
`testarea` varchar(200) NOT NULL
);
INSERT INTO polyThing (boundary, testarea)
VALUES(
PolygonFromText(
'POLYGON((
-114.018522 46.855932 ,
-113.997591 46.856030 ,
-113.997447 46.848626 ,
-114.018161 46.848824 ,
-114.018522 46.855932 ))'
), 'Test Area 1'
);
我想从 locations
中查找此定义的多边形内的记录,并且我一直在尝试与此类似的查询。无论我尝试什么,我都得到 0 条记录。
SELECT *
FROM locations
WHERE ST_CONTAINS(
(SELECT boundary FROM polyThing
WHERE polyThing.testarea = 'Test Area 1')
, Point(Longitude, Latitude))
这里是locations
table和数据:
CREATE TABLE `locations` (
`LocationID` int(11) NOT NULL,
`Location` varchar(200) NOT NULL,
`Longitude` varchar(200) NOT NULL,
`Latitude` varchar(200) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 ROW_FORMAT=COMPACT;
INSERT INTO `locations` (`LocationID`, `Location`, `Longitude`, `Latitude`) VALUES
(1, 'In the polygon', '-114.007191', '46.853019'),
(2, 'In the polygon', '-114.003798', '46.851045'),
(3, 'Not in', '-114.016934', '46.866098');
喜欢在正确的方向上推动。
我猜 (Longitude, l.Latitude)
在位置?
SELECT l.*, ST_CONTAINS(p.boundary, POINT(l.Longitude, l.Latitude))
FROM locations l
CROSS JOIN polyThing p;
SELECT l.*
FROM locations l
JOIN polyThing p
ON ST_CONTAINS(p.boundary, POINT(l.Longitude, l.Latitude))
AND p.testarea = 'Test Area 1';
输出
我有一个 locations
table,其中有很多行的位置和数据。其中有 2 列 - 经度和纬度。我想从 table 中找到定义的地图多边形区域中的记录。
我有第二个 table,我刚刚根据一些名为 polyThing
的教程创建了它,它具有我定义的边界...
CREATE TABLE polyThing
(
`ID` int auto_increment primary key,
`boundary` polygon not null,
`testarea` varchar(200) NOT NULL
);
INSERT INTO polyThing (boundary, testarea)
VALUES(
PolygonFromText(
'POLYGON((
-114.018522 46.855932 ,
-113.997591 46.856030 ,
-113.997447 46.848626 ,
-114.018161 46.848824 ,
-114.018522 46.855932 ))'
), 'Test Area 1'
);
我想从 locations
中查找此定义的多边形内的记录,并且我一直在尝试与此类似的查询。无论我尝试什么,我都得到 0 条记录。
SELECT *
FROM locations
WHERE ST_CONTAINS(
(SELECT boundary FROM polyThing
WHERE polyThing.testarea = 'Test Area 1')
, Point(Longitude, Latitude))
这里是locations
table和数据:
CREATE TABLE `locations` (
`LocationID` int(11) NOT NULL,
`Location` varchar(200) NOT NULL,
`Longitude` varchar(200) NOT NULL,
`Latitude` varchar(200) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 ROW_FORMAT=COMPACT;
INSERT INTO `locations` (`LocationID`, `Location`, `Longitude`, `Latitude`) VALUES
(1, 'In the polygon', '-114.007191', '46.853019'),
(2, 'In the polygon', '-114.003798', '46.851045'),
(3, 'Not in', '-114.016934', '46.866098');
喜欢在正确的方向上推动。
我猜 (Longitude, l.Latitude)
在位置?
SELECT l.*, ST_CONTAINS(p.boundary, POINT(l.Longitude, l.Latitude))
FROM locations l
CROSS JOIN polyThing p;
SELECT l.*
FROM locations l
JOIN polyThing p
ON ST_CONTAINS(p.boundary, POINT(l.Longitude, l.Latitude))
AND p.testarea = 'Test Area 1';
输出