Search code examples
phpmysqlpolygonpointmysql-5.7

How to return value if point exactly inside polygon in mysql


I want to return value from table where point exactly exist in polygon field
I wrote query but is not accurate, value returned even point be around the polygon

"select `name`,`loc_id` FROM `locations` where MBRContains(`polygons`,ST_GeomFromText('Point($loc[0] $loc[1])'))= 1"

Solution

  • In mysql instead of MBR ( minimum bounding rectangle) you should use ST_CONTAINS

    "select `name`,`loc_id` 
        FROM `locations` 
        where ST_CONTAINS(`polygons`,ST_GeomFromText('Point($loc[0] $loc[1])'))= 1"
    

    ST_CONTAINS returns true only if one feature contains the other and due the fact that your are checking for point so this should give you a better result