I am working with PostGIS and PostgreSQL. I have 2 layers of data.
polygons with fields id, quality and geom (geometry shape).points with fields id and geom (geometry shape).For each point (example point marked yellow), I would like to find the nearest point which shows a strong characteristic but this is within a range (also yellow). To get this quality, I have to look at the details of polygons surrounding the points under investigation (green).
SELECT pt1.id, pt2.id, p.id, sum(p.quality) as quality_total
FROM polygons p, points pt1, points pt2
WHERE ST_DWithin(pt1.geom, pt2.geom, 100) --points maximum 100 units away from each other
AND ST_DWithin(p.geom, pt2.geom, 10) --polygons maximum 10 units away from point pt2
AND (pt1.id = 1 OR pt1.id = 2) --specific points
GROUP BY quality, pt1.id, pt2.id, p.id
Above is my attempt so far. As you can see it is for only specific points. I was hoping the output would be for all points, like:
pt1.id,pt2.id,sum(p.quality)
to mean that pt1.id is a point, pt2.id is the point nearest to pt1 (exhibiting a quality) and sum(p.quality) is the sum of the quality of all the polygons in pt2's range. I am at a loss how to continue building the query. I would appreciate any help. I suppose this is a form of clustering problem but I am not so educated here.