Home > Database > Mysql Tutorial > 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
Release: 2019-02-22 16:37:24
Original
4014 people have browsed it

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;
Copy after login

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))'));
Copy after login

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);
Copy after login

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);
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template