MySQL Spatial – Fixing MBRCovers Returning True for Points Outside Polygon

MySQLspatial

I'm having big trouble figuring out why the MBRCovers is returning true even tough the point I give as a parameter is outside the polygone supplied.

The point is to find if a row has it's coordinates inside the polygon :

 SELECT   i.coordinates
        FROM     instances i
        WHERE 
              MBRCovers( //Check if arg a covers arg b
                ST_Buffer( // Create a buffer around the line string 
                    ST_GeomFromText('LineString(20.65322 52.2025,20.65381 52.20718,20.65059 52.20644,20.6452 52.20512,20.63083 52.2016,20.62956 52.20131,20.62775 52.20088,20.62565 52.20034,20.62511 52.2002,20.62458 52.20007,20.62376 52.19985,20.62312 52.19969,20.6229 52.19965,20.62252 52.19956,20.62044 52.19906,20.6204 52.19905,20.61995 52.19893,20.61874 52.19865,20.61831 52.19855,20.61805 52.19852,20.61779 52.19849,20.61716 52.19847,20.61696 52.19849,20.61627 52.19853,20.61246 52.19869,20.61117 52.19875,20.61008 52.1988,20.60932 52.19883,20.60909 52.19884,20.60909 52.19878,20.60907 52.19864,20.60903 52.19851,20.60899 52.19834,20.60903 52.19819,20.60913 52.19812,20.60924 52.19807,20.6098 52.19786,20.60999 52.19779,20.61012 52.19767,20.61025 52.1969,20.61028 52.1968,20.61028 52.19669,20.61029 52.19652,20.61029 52.19641,20.61026 52.19605,20.61025 52.19593,20.61024 52.19578,20.61024 52.19573,20.61027 52.19568,20.61032 52.19563,20.61041 52.1956,20.61049 52.19559,20.61059 52.19559,20.61071 52.19558,20.61093 52.19557,20.6111 52.19556,20.6117 52.19553,20.61274 52.19547,20.61295 52.19544,20.61308 52.19537,20.61314 52.1953,20.61311 52.19503,20.61306 52.19458,20.61305 52.19447,20.61303 52.19435,20.61294 52.19394,20.61292 52.19384,20.6129 52.19369,20.61288 52.19357,20.61286 52.19344,20.61283 52.19333,20.6128 52.19325,20.61276 52.19316,20.61219 52.19215,20.61211 52.19198,20.61204 52.19179,20.612 52.19166,20.61199 52.19138,20.612 52.1912,20.61199 52.19079,20.61193 52.18964,20.61189 52.18917,20.61188 52.1889,20.61187 52.18878,20.61188 52.18845,20.61183 52.18732,20.61179 52.18662,20.61179 52.18658,20.61179 52.18653,20.61181 52.18631,20.6118 52.18587,20.61171 52.1842,20.61168 52.18373,20.61162 52.18277,20.61158 52.18199,20.61156 52.18153,20.61156 52.18143,20.61154 52.18066,20.61153 52.18039,20.61151 52.1798,20.61149 52.17945,20.61148 52.17923,20.61146 52.17888,20.61145 52.17839,20.61133 52.17527,20.61132 52.17509,20.61132 52.17497,20.61134 52.17409,20.61145 52.17225,20.6115 52.17113,20.61151 52.17096,20.61154 52.17053,20.61159 52.16956,20.61162 52.16909,20.61166 52.16862,20.61168 52.16804,20.61178 52.16647,20.6119 52.16457,20.61202 52.16266,20.61208 52.16159,20.61216 52.16027,20.61218 52.15997,20.61227 52.15826,20.61229 52.15798,20.6123 52.15778,20.61246 52.15517,20.61247 52.15504,20.61254 52.15482,20.61261 52.15462,20.61273 52.15439,20.61286 52.15418,20.61303 52.15395,20.61505 52.15132,20.61507 52.1513,20.61554 52.15067,20.61559 52.15058,20.61562 52.15052,20.61562 52.15045,20.61559 52.1504,20.61552 52.1503,20.61547 52.15027,20.61536 52.15023,20.61521 52.15021,20.61501 52.1502,20.6144 52.15018,20.61358 52.15014,20.61061 52.14998,20.61052 52.14999,20.61043 52.14998,20.61034 52.14997,20.61026 52.14995,20.61019 52.14991,20.61014 52.14988,20.61009 52.14982,20.61005 52.14976,20.60984 52.14902,20.60969 52.1486,20.60959 52.14827,20.60949 52.14788,20.60944 52.14764,20.60941 52.1475,20.60933 52.14698,20.60928 52.14644,20.60927 52.14631,20.60925 52.14627,20.60922 52.14624,20.60917 52.14618,20.60914 52.14616,20.60912 52.14614,20.6091 52.14611,20.60909 52.14609,20.60909 52.14607,20.6091 52.14603,20.60912 52.146,20.60915 52.14597,20.60919 52.14594,20.60925 52.14587,20.60926 52.14583,20.60927 52.14575,20.60928 52.14565,20.60929 52.1454,20.60937 52.14455,20.6094 52.14393,20.60945 52.14328,20.60949 52.14273,20.6096 52.14139,20.60962 52.14136,20.60967 52.14132,20.6097 52.1413,20.6098 52.14126,20.60987 52.14125,20.60995 52.14124,20.6101 52.14127,20.6103 52.14128,20.61355 52.14143,20.61455 52.14147,20.61535 52.1415,20.61571 52.14151,20.61593 52.14152,20.61606 52.14151,20.6162 52.14147,20.61628 52.14142,20.61635 52.14136,20.6164 52.14128,20.61644 52.14116,20.61667 52.13967,20.61692 52.13819,20.61702 52.13756,20.6173 52.13589,20.61733 52.13574,20.61736 52.13557,20.61745 52.13502,20.61752 52.1345,20.61763 52.13384,20.61802 52.13154,20.61809 52.1311,20.61815 52.13071,20.61827 52.12999,20.61829 52.12991,20.6184 52.12919,20.6188 52.12684,20.61885 52.12654,20.61886 52.12645,20.61892 52.12608,20.61931 52.12371,20.61934 52.12355,20.61986 52.12036,20.61989 52.12019,20.61999 52.11958,20.6201 52.11887,20.62017 52.11846,20.62032 52.11749,20.62053 52.11631,20.62059 52.11613,20.62067 52.11598,20.62076 52.11587,20.62094 52.11572,20.62106 52.11566,20.62122 52.11558,20.62145 52.1155,20.62174 52.11543,20.62225 52.11531,20.62241 52.11527,20.62273 52.1152,20.62331 52.11506,20.62377 52.11495,20.62413 52.11487,20.62472 52.11473,20.62502 52.11465,20.62533 52.11455,20.62571 52.11441,20.62614 52.11423,20.62665 52.11396,20.62691 52.11379,20.62717 52.11361,20.62744 52.11337,20.62769 52.11312,20.62815 52.11259,20.62871 52.11196,20.62881 52.11185,20.62921 52.11134,20.62983 52.11058,20.63006 52.11027,20.63038 52.10988,20.62925 52.10946,20.62871 52.10926,20.62856 52.1092,20.62839 52.10913,20.62818 52.10906,20.62744 52.10878,20.62584 52.10818,20.62572 52.10845,20.62557 52.10879,20.62524 52.10874,20.62508 52.10871,20.625 52.10889)') ,
                    0.00001

                ),
                i.coordinates // the column coordinates of type point
              )

The query result is POINT(20.622755748078 52.198545278987) which you will see below is well outside the polygon.

Below are screenshots I've been using for debugging from this tool :

Full route

The polygon generated using the ST_Buffer

zoom on polygon

A zoom on the polygon in the area closest to the result returned

point

And the point returned by the qyery

Obviously the point is well outside of the generated polygon! I can't understand where the problem comes from. Or maybe it's so obvious that I can't figure it out.

Any help would be much appreciated.

Best Answer

According to the MySQL documentation, MBRCovers() uses the Minimum Bounding Rectangles (hence the MBR in MBRCovers) to test the spatial relationship. Imagine the smallest rectangle that you can draw around your line to contain the whole line. That is a big rectangle.

So MBRCovers() test if the whole bounding rectangle of geometry #1 (your line) covers geometry #2 (your points). So obviously, it returns true!

Try ST_Contains() instead of MBRCovers().