Skip to main content
added 92 characters in body
Source Link
Spacedman
  • 66.5k
  • 6
  • 83
  • 121

I don't understand the "minimum distance" thing you mentioned.

Here's how to generate 1000 points uniformly within cells but with the number in each cell weighted by the cell value:

Make a test 3x4 raster with some positive random numbers:

> set.seed(12)
> r = raster(matrix(runif(12),3,4))

Get the cell half-width for later:

> hs = res(r)/2

Now work out which cell each of our 1000 points is going in by sampling from the number of cells (12) with replacement, weighted by the value in the cells:

> ptscell = sample(1:12, 1000, prob=r[], replace=TRUE)

Now find the centre of those 1000 cell numbers:

> centres = xyFromCell(r,ptscell)

And generate random uniform points in the cell by using the centre and the half-width/height from earlier:

> pts = cbind(runif(nrow(centres),centres[,1]-hs[1],centres[,1]+hs[1]),runif(nrow(centres),centres[,2]-hs[2],centres[,2]+hs[2]))

Voila:

> plot(r)
> points(pts)

points weighted by raster value

I don't understand the "minimum distance" thing you mentioned.

Here's how to generate 1000 points uniformly within cells but with the number in each cell weighted by the cell value:

Make a test 3x4 raster with some positive random numbers:

> set.seed(12)
> r = raster(matrix(runif(12),3,4))

Get the cell half-width for later:

> hs = res(r)/2

Now work out which cell each of our 1000 points is going in by sampling from the number of cells (12) with replacement, weighted by the value in the cells:

> ptscell = sample(1:12, 1000, prob=r[], replace=TRUE)

Now find the centre of those 1000 cell numbers:

> centres = xyFromCell(r,ptscell)

And generate random uniform points in the cell by using the centre and the half-width/height from earlier:

> pts = cbind(runif(nrow(centres),centres[,1]-hs[1],centres[,1]+hs[1]),runif(nrow(centres),centres[,2]-hs[2],centres[,2]+hs[2]))

Voila:

> plot(r)
> points(pts)

I don't understand the "minimum distance" thing you mentioned.

Here's how to generate 1000 points uniformly within cells but with the number in each cell weighted by the cell value:

Make a test 3x4 raster with some positive random numbers:

> set.seed(12)
> r = raster(matrix(runif(12),3,4))

Get the cell half-width for later:

> hs = res(r)/2

Now work out which cell each of our 1000 points is going in by sampling from the number of cells (12) with replacement, weighted by the value in the cells:

> ptscell = sample(1:12, 1000, prob=r[], replace=TRUE)

Now find the centre of those 1000 cell numbers:

> centres = xyFromCell(r,ptscell)

And generate random uniform points in the cell by using the centre and the half-width/height from earlier:

> pts = cbind(runif(nrow(centres),centres[,1]-hs[1],centres[,1]+hs[1]),runif(nrow(centres),centres[,2]-hs[2],centres[,2]+hs[2]))

Voila:

> plot(r)
> points(pts)

points weighted by raster value

Source Link
Spacedman
  • 66.5k
  • 6
  • 83
  • 121

I don't understand the "minimum distance" thing you mentioned.

Here's how to generate 1000 points uniformly within cells but with the number in each cell weighted by the cell value:

Make a test 3x4 raster with some positive random numbers:

> set.seed(12)
> r = raster(matrix(runif(12),3,4))

Get the cell half-width for later:

> hs = res(r)/2

Now work out which cell each of our 1000 points is going in by sampling from the number of cells (12) with replacement, weighted by the value in the cells:

> ptscell = sample(1:12, 1000, prob=r[], replace=TRUE)

Now find the centre of those 1000 cell numbers:

> centres = xyFromCell(r,ptscell)

And generate random uniform points in the cell by using the centre and the half-width/height from earlier:

> pts = cbind(runif(nrow(centres),centres[,1]-hs[1],centres[,1]+hs[1]),runif(nrow(centres),centres[,2]-hs[2],centres[,2]+hs[2]))

Voila:

> plot(r)
> points(pts)