Jump to content
beyzad

How to findout if a point is in polygon area or not

Recommended Posts

Posted (edited)

Hi there.

I got a table named `area` that has a POLYGON field.

There are some rows with specified area in table.

Now i want to check if a point ("59.5594597, 36.3556769" for example) is within the polygons or not.

 

searched a lot and none works.

my polygon has 102 points in case of need.

Thanks.

Edited by beyzad

Share this post


Link to post
Share on other sites
Just now, requinix said:

Looks like you want ST_CONTAINS.

Used that. also used within. none works.

In this page: http://www.justskins.com/forums/point-in-polygon-99698.html

a guy said:

Quote

"Currently, MySQL does not implement these functions according to the
specification. Those that are implemented return the same result as the
corresponding MBR [minimal bounding rectangle]-based functions. This
includes functions in the following list other than Distance() and
Related()."

The minimal bounding rectangle of your polygon is POLYGON((0.8 0.8, 0.8
2.6, 2.6 2.6, 2.6 0.8, 0.8 0.8)). This rectangle includes all three
points. Apparently MySQL will implement proper geometric comparisons
"when they get around to it"; don't ask me why, since the basic
point-in-polygon test only takes around 10 lines of code.
 

Not sure if these functions works fine for polygons with 100+ points or not ...

Share this post


Link to post
Share on other sites

That disclaimer is no longer present in the docs since at least 5.5. What version of MySQL are you using?

Share this post


Link to post
Share on other sites

Example (Mysql 5.7)

point table

mysql> SELECT name
    ->      , x
    ->      , y
    ->      , ST_AsText(loc) as loc
    -> FROM point;
+------+------+------+------------+
| name | x    | y    | loc        |
+------+------+------+------------+
| A    |    1 |    1 | POINT(1 1) |
| B    |    5 |    6 | POINT(5 6) |
| C    |    2 |    8 | POINT(2 8) |
+------+------+------+------------+

Then

mysql> SET @p1 = 'POLYGON((0 0,10 0,10 10,0 10,0 0),(5 5,7 5,7 7,5 7, 5 5))';

mysql> SELECT name FROM point where ST_CONTAINS(ST_GEOMFROMTEXT(@p1), loc);
+------+
| name |
+------+
| A    |
| C    |
+------+

 

polygon.PNG

Share this post


Link to post
Share on other sites
Posted (edited)

Oops!

All the queries i tested and the code you used works.

The only problem was the noob site https://www.keene.edu/campus/maps/tool/ i used to draw my polygon, has lat,lng reversed.

So my polygon and points i exported from there was all wrong.

 

Thanks for your help.

cheers

Edited by beyzad

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.