Jump to content

Recommended Posts

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

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

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
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.