Home  >  Article  >  Database  >  How to determine whether a point is within a specified polygon area through mysql

How to determine whether a point is within a specified polygon area through mysql

jacklove
jackloveOriginal
2018-06-08 18:22:463955browse

This article will introduce the method of using mysql to determine whether a point is within a specified polygon area, and provide a complete process.

Recommended related mysql video tutorials: "mysql tutorial"

1. Create a test table

CREATE TABLE `zone` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `polygongeo` polygon NOT NULL, PRIMARY KEY (`id`)
) ENGINE=MYISAM DEFAULT CHARSET=utf8;

Note: Spatial indexes can only be created in tables whose storage engine is MYISAM

2. Insert polygon data

insert into zone(polygongeo) values(POLYGONFROMTEXT('POLYGON((1 1,1 5,5 5,5 1,1 1))'));

3. Judgment Whether the point is in the polygon area

Test POINT(3, 4)

select AsText(polygongeo) from zone where MBRWithin(POLYGONFROMTEXT('POINT(3 4)'),polygongeo);

Output: POLYGON((1 1,1 5 ,5 5,5 1,1 1))
Represents point POINT(3, 4) in the polygon area
Test POINT(6, 1)

select AsText(polygongeo) from zone where MBRWithin(POLYGONFROMTEXT('POINT(6 1)'),polygongeo);

Output: Empty
Indicates point POINT(6, 1) outside the polygon area
Summary: mysql spatial query is not very suitable for map coordinates, so querying map coordinates can be implemented using mongodb, Reference: "Mongodb method to determine whether coordinates are within a specified polygon area"

This article explains how to determine whether a point is within a specified polygon area through mysql. For more related content, please pay attention to the PHP Chinese website.

Related recommendations:

How to call php imagemagick to achieve the effect of old photos

Explanation on calculating the Cartesian product of multiple sets in PHP

About the use and performance analysis of php file containing directory configuration open_basedir

The above is the detailed content of How to determine whether a point is within a specified polygon area through mysql. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn