Mysql: find polygon within certain radius

430 Views Asked by At

In my database I have polygons stored. Now I need to search all the polygons that are within a certain radius. Even if the polygon is only a small part inside the region, then it should be included inside the results (so once there is a minimal match, there is a match).

What is the best way to do this? I have been thinking about creating another polygon and search everything that intersects this, but don't know if this is a valid method?

2

There are 2 best solutions below

0
On

Yes, I think it is the best approach. You can create a polygon using ST_BUFFER and then you can use ST_INTERSECT to find if polygons will intersect your polygon.

May be you can also do it using ST_DISTANCE. It will calculate minimum distance of a point from polygon.

Select ST_DISTANCE(polygons,POINT(x, y)) as distance, polygon_id from your_polygon_table WHERE distance <= 10 
0
On

I struggled with the same issue and came up with the following that works well.

select this.poly, other.poly
from table this, table other
where this.name = 'name of subject polygon'
  and ST_Distance(ST_Centroid(this.polygon),ST_Centroid(other.polygon)) < 'desired distance';

This will work with the initial point is in the same table as the other polygons. If you have a fixed initial point you can simplify the query as follows:

select poly from table
where ST_Distance('26.36, -81.07',ST_Centroid(other.polygon)) < 'desired distance';