beyzad Posted July 22, 2018 Share Posted July 22, 2018 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. Link to comment Share on other sites More sharing options...
requinix Posted July 22, 2018 Share Posted July 22, 2018 Looks like you want ST_CONTAINS. Link to comment Share on other sites More sharing options...
beyzad Posted July 22, 2018 Author Share Posted July 22, 2018 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 ... Link to comment Share on other sites More sharing options...
requinix Posted July 22, 2018 Share Posted July 22, 2018 That disclaimer is no longer present in the docs since at least 5.5. What version of MySQL are you using? Link to comment Share on other sites More sharing options...
Barand Posted July 23, 2018 Share Posted July 23, 2018 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 | +------+ Link to comment Share on other sites More sharing options...
beyzad Posted July 24, 2018 Author Share Posted July 24, 2018 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 Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.