MySQL – Understanding MBRContains and MBRWithin

mariadbMySQLspatialterminology

MBRContains(g1, g2) is defined as,

Returns 1 or 0 to indicate whether the minimum bounding rectangle of g1 contains the minimum bounding rectangle of g2. This tests the opposite relationship as MBRWithin().

MBRWithin(g1, g2) is defined as,

Returns 1 or 0 to indicate whether the minimum bounding rectangle of g1 is within the minimum bounding rectangle of g2. This tests the opposite relationship as MBRContains().

What exactly does contains and within mean in that context?

Best Answer

These are DE-91M (Wikipedia) terms, and they were standardized with Simple Features

but, Wikipedia probably covers it best, with two definitions,

  • a contains b: geometry b lies in a, and the interiors intersect.

  • a contains b iff no points of b lie in the exterior of a, and at least one point of the interior of b lies in the interior of a

The within relationship is the inverse of contains, with one catch "polygons do not contain their boundary." Other than that, within(a,b) = contains(b,a). Though interestingly, linestrings do contain all the points along the linestrings, except the terminal points.

For more information see the Wikipedia,