I have MySQL database with polygons, and also the database with points (column longitude and column latitude), is it possible to return all points inside one polygon?
[GIS] Does MySQL has function which will return the points inside polygon
MySQLpolygon
Related Question
- [GIS] find lat,long inside polygon with regular interval of time using thesql
- [GIS] Determine which points are contained inside a given polygon
- [GIS] Passing data from thesql/PHP into Leaflet
- [GIS] Convert lat/lon thesql columns to geometry, check if point is in polygon, return polygon id
- [GIS] Select all points within a a geojson polygon from Mysql
- [GIS] Using Polygon centroids tool – points aren’t always inside polygon, can I fix that
Best Answer
If you are using MySQL 5.6.1+, take a look at ST_Contains.
Given table called points and polygons, with a primary key called
id
and a geometry column calledgeometry
, this should work:SELECT points.id FROM polygons, points WHERE ST_CONTAINS(polygons.geom, points.geom);
If there's no geometry column for the points table, but there are latitude and longitude columns, you can use the MySQL
Point()
function to convert long/lat columns to a point, like this:SELECT points.id FROM polygons, points WHERE ST_CONTAINS(polygons.geom, Point(points.longitude, points.latitude));
Note that MySQL versions prior to 5.6.1 provide a Contains function which uses the MBR of the polygon, not the actual polygon boundaries, which might also work for you.