QGIS – Extract by Location/Within Distance and Add Location to Output

qgisselect-by-locationwithin

I have polygons with points inside (see Fig 1). You can find an example here: https://drive.google.com/drive/folders/1VccEmv-EtjZ9tG6J_ozp3E37bBRbtvNn?usp=sharing

I want to know the locations of all points in a certain distance around a polygon. I can buffer each polygon by in QGIS, and QGIS can iterate over each polygon to extract the points. An example for one selected polygon (bottom one) is shown in the next figure.

However, the output file is just a list of points, but does not give an indication about which polygon/location was used to find them. The ordering of the points is also random (i.e. it is not starting from the polygon outwards). Is there any way to add this information to the output file, e.g. as column with the ID of the polygon?

Note that one point can be in the buffer for many polygons.

I have thought about finding the center of the selected points but that only works if the polygon is in the middle of many other polygons – if I try this for a polygon which is at the edge the center of the selected points will not be in the polygon.

Note that the dataset includes ~2mil polygons and ~10mil points.

Points in polygons.

Points selected for one polygon using within distance.

Best Answer

If you first buffer the polygons then the buffer layer will get all the attributes from the input layer. Then do an intersection with the point layer as the first layer and the buffer layer as the second. Then you will have one point for each point / polygon combination from your input layers with the attributes from both layers.

enter image description here

My input poly and point layers and the buffered polygon layer:

Then I do a intersection and I get a new layer with 19 points - one for each unique point / polygon combination:

enter image description here

Related Question