Determines whether a point is within a specified polygon area via MySQL

Source: Internet
Author: User
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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.