Home >Database >Mysql Tutorial >Use mysql to determine whether a point is within a specified polygon area

Use mysql to determine whether a point is within a specified polygon area

jacklove
jackloveOriginal
2018-06-20 17:01:452656browse

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.

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. Determine 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 Inside

Test POINT(6, 1)

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

Output: Empty
indicates that point POINT(6, 1) is in the polygon area External

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 use mysql to determine whether a point is within a specified polygon area. For more related content, please pay attention to the PHP Chinese website.

Related recommendations:

Use imagemagick in php to achieve old photo effects

How to calculate multiple sets through php Relevant knowledge of the Cartesian product

Related content of the sharing interface developed by WeChat

The above is the detailed content of Use mysql to determine whether a point is within a specified polygon area. 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
Previous article:Unlockable queryNext article:Unlockable query