When a user click on a map, I first want to get closest point (placename), then use that point to get number of points (schools) within 10km buffer / radius
...
$str = pg_query($conn, "SELECT ST_Buffer(a.geom, 1000) as buffer, a.*, s.*, ST_Distance(a.geom, r.ref_geom) AS distance FROM placenames a CROSS JOIN (SELECT ST_MakePoint($coordinate)::geography AS ref_geom) AS r INNER JOIN local s ON ST_Intersects(a.geom, s.geom) WHERE ST_DWithin(a.geom, r.ref_geom, '$radius') ORDER BY ST_Distance(a.geom, r.ref_geom)"
if(pg_num_rows($str) > 0){
$area = $fetch['placename'];
$buffer = $fetch['buffer'];
//How do I use this buffer and get all schools within that buffer
}
3 dots means there is a code on top
ST_Distance
orST_Buffer
in a distance search query. Instead useST_DWithin
, which can use an index.