4

I have a polygon grid with 1 square kilometer area cells, and over it a set of points, like so:

points over grid

What i need to do is select and extract only a single random point per grid cell.

I could do that manually and it wouldn't take too much time as the area i have to cover is not that big, but is there is another way to do this?

5

You can easily achieve this using function from processing toolbox:

  1. Take grid cell and join its id (or other unique) attribute to related points - Use Join attributes by location and join unique attribute (i.e. "id_grid") from grid layer to point layer. This create new point layer with new attribute - "id_grid" of corresponding grid.

  2. Select only one point per gird - Use Random selection within subsets. As input choose your new layer from step one, for ID Field select "id_grid" and set number required points to be selected (in your case 1)

  3. Extracting to new point layer - Right click on layer, select Save As..., set output parametres (format, CRS...) and check Save only selected features

1

Another solution is to create an R Script GUI widget, which will run just like any other tool in the processing toolbox. The script below will allow you to select from you layers a polygon and a point data set, and will return a randomly sampled set of points wherein no more than one point will remain in within each individual polygon:

##polygons=vector
##points=vector
##output=output vector
library(sp)

samp <- lapply(1:nrow(polygons),function(i){
  x <- points[polygons[i,],]
  if (nrow(x) == 0){
    return(NULL)
  } else {
    sub <- sample(1:nrow(x), size = 1)
    return(x[sub,])
  }
})

output = Reduce(rbind,samp[unlist(lapply(samp,function(i) class(i) != "NULL"))])

Note that if this is your first time running an R Script in QGIS, the first time you run it may take several minutes because it has to install the libraries. Each time after that will be significantly faster.

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.