1

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.

1

3 Answers 3

2

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

6
  • This sounds like it should work. However, when I try to Union the points with the buffered polygon layer I get: GEOS geoprocessing error: difference failed. I checked that geometries are correct (and coord systems ). Any idea why this may be the case? Likely because I have some polygons with no points? Commented Jul 5, 2022 at 17:07
  • You should use intersection, not union. (I used the one under Vector - geoprocessing tools, there are a few other intersection tools in the processing toolbox) When you say you checked the coord systems, I assume you checked that both the layers have the same projection? Commented Jul 5, 2022 at 17:26
  • Ah with intersection it works. if you could delete the last two sentences of the first paragraph (which I think are misleading) I am happy to take this as the answer :-) Thank you! Commented Jul 5, 2022 at 17:35
  • The only problem with this approach will be the very long table for the dataset, but this I can fix by splitting the data up into smaller batches - if someone has another approach where I do not have to do this I would test it as well. Commented Jul 5, 2022 at 17:45
  • Sorry, I messed up in the initial point, writing union and not intersection in the 2nd sentence. Also, do I understand you correctly that you only want points with a distance outside the original polygon, i.e. for my example data set, only the three points in the middle (not the four upper, the one to the left and the one at the bottom)? Commented Jul 5, 2022 at 17:54
0

I'd suggest to create a buffer layer with 100m (don't dissolve output). Then use count points in polygons tool to add the nr of points within the buffer. you'll end up with the original table plus the nr of points

I saw another possible solution via the field calculator here: How to count features within a radius distance?

1
  • Thanks for the answer. I probably should have clarified, I need the x/y locations of the points, not only the number of points. Commented Jul 5, 2022 at 15:56
0

To add a field to the points layer indicating the polygon which it "belongs" open the field calculator in the points layer and calculate a new field with this expression.

aggregate( 'Polygons', -- replace Polygons with the names/id of your polygons layer 'array_agg', $id, -- put here what you want to pass from the polygons layer to the points layer distance(geometry(@parent), $geometry) <= 800 -- set here the buffer size )[0] 

Remember, $id returns the id from the features, if you need to pass a field instead, just replace it with the name of the field inside double quotes (e.g. "field1").

Notes:

  • If one point "belongs" to more than one polygon this will be not handled, simply the point will take the values of the first evaluated polygon.
  • If a point doesn't "belong" to any polygon, it will take the NULL value.
2
  • Thanks! Indeed the problem is that one point often (not always) belongs to many polygons so that you solution does not work for most cases. I clarified the question in that regard. Commented Jul 5, 2022 at 16:17
  • And what about using a list type field to store multiple values of the polygons? Commented Jul 5, 2022 at 16:34

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.