I have two tables in a PostgreSQL / PostGIS database 1 polygon and the other point data How do I modify the following query so it returns only the smallest polygon nearest the point The red in the picture
QUERY
select pt.point_id, ply.polygon_id,ply.admin_level
from point_table pt
join polygon_table as ply
on ST_WITHIN(pt.geometry, ply.geometry)
WHERE pt.point_id = 13424428 -- To Test only
The above returns
point_id poly_id admin_level
13424428 -1627120 10
13424428 -295351 8
13424428 -180837 6
13424428 -151336 5
13424428 -58447 4
13424428 -62149 2
I need only the first row for this point in polygon with admin_level 10
EDIT This Query will form a link table to join the point to it’s nearest parent polygon It will look something like this, there could be multiply points in the same polygon
point_id poly_id admin_level
13424428 -1627120 10
13424429 -1627120 10
13424430 -1627120 10
13424422 -1627121 4
13424423 -1627121 4
13424434 -1627121 4
ORDER BY ST_Distance(...) LIMIT 1
would do it, but only for a single point.