5

I have two shapefiles - one of neighborhood polygons and another of centroids within those neighborhoods:

neighborhoods and centroids

For each centroid I would like to calculate:

  1. Distance to the next closest neighborhood (and the neighborhood's identifying attributes)

  2. Distance to the 2nd closest neighborhood (and the neighborhood's identifying attributes)

  3. Distance to the 3rd closest neighborhood (and the neighborhood's identifying attributes)

Is there a simple tool or plugin for QGIS that would allow me to calculate these values for each centroid? The desired output is a table with 3 rows for each centroid and columns of the centroid ID, neighborhood ID, and distance to that neighborhood.

5

I don't know of any easy way to do this in QGIS, but it can be very quickly done in PostGIS. Say you install Postgres and PostGIS, and import your points layer as "centroid", and polygon layer as "neighbourhood". This is the query you'd then execute:

SELECT c.id AS centroid_id, n.id AS neighbourhood_id, k.dist AS distance
FROM centroid as c
JOIN LATERAL (SELECT c.id AS point, n.id AS poly, ST_Distance(c.geom, n.geom) AS dist
    FROM neighbourhood as n
    WHERE ST_Distance(c.geom, n.geom) > 0.0
    ORDER BY point, dist
    LIMIT 3
) k ON true
WHERE c.id = point

OBS: Valid for Postgres 9.3+

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.