This article describes a method that uses MySQL to determine whether a point is within a specified polygon area and provides a complete process.
1. Create a test table
CREATE TABLE ' zone ' (' id ' int (ten) unsigned NOT null auto_increment, ' Polygongeo ' polygon not NULL, PRIMARY KEY (' id ')) EN Gine=myisam DEFAULT Charset=utf8;
Note: Spatial indexes can only be created in tables where the storage engine is MyISAM
2. Inserting polygon Data
Insert into zone (Polygongeo) VALUES (Polygonfromtext (' POLYGON ((1 5,5 5,5 1) ');
3. Determine if the point is in the polygon area
Test point (3, 4)
Select Astext (Polygongeo) from the zone where Mbrwithin (Polygonfromtext (' Point (3 4) '), Polygongeo);
Output: POLYGON ((1 5,5 5,5 1))
Represents the dot point (3, 4) within the polygon area
Test point (6, 1)
Select Astext (Polygongeo) from the zone where Mbrwithin (Polygonfromtext (' point (6 1) '), Polygongeo);
Output: Empty
Represents the dot point (6, 1) outside the Polygon area
Summary: MySQL spatial query is not very suitable for map coordinates, so query map coordinates can be implemented using MongoDB, refer to: "MongoDB determines whether the coordinates are within the specified polygon area of the method"
This article explains how to use MySQL to determine whether the point is within the specified polygon area, more relevant content please focus on the PHP Chinese web.
Related recommendations:
Use ImageMagick in PHP to achieve old photo effects
How to calculate the Cartesian product of multiple sets with PHP