Create a grid of regular sample points in R

My knowledge of R is still pretty simple, and I need help. I am trying to create a grid over the area of ​​study. I would like the grid cells to be centered on the sample points that I generated on a spatial polygon with a function spsample. Here is my code:

### 1 - CREATE SPATIAL POLYGON ###
library(sp)
# arrange coordinates of future spatial polygon into a 2-column matrix    
xym <- matrix(c(-95000, 90000, 90000, -95000, 443100, 443100, 590000, 590000), nrow = 4, ncol = 2)

# create Polygon
p <- Polygon(xym)

# wrap Polygon into Polygons object
ps <- Polygons(list(p), 1)

# wrap Polygons object into SpatialPolygons object
sps <- SpatialPolygons(list(ps))

# set CRS of polygon (NAD83 Québec Lambert)
proj4string(sps) <- CRS("+init=epsg:32198")

data <- data.frame(f = 99.9)
spdf <- SpatialPolygonsDataFrame(sps, data)
spdf


### 2 - GENERATE REGULAR SAMPLING POINTS ###
library(raster)

ptsreg <- spsample(spdf, 1000, type = "regular")
plot(spdf, col = "azure2")
points(ptsreg, pch = 16, col = "black")

I would like these points to be the centroids of the mesh cells. I know that I could create square polygons from single central coordinates (see Create square polygons from single-center coordinates and an area in R ). I'm not sure that I have the coordinates of the points that I created, though ...

SpatialPolygonsDataFrame ( ), - (, !):

Sampling points and field of study

! !

+4
2

raster:

library(raster)
ptsreg <- spsample(spdf, 1000, type = "regular")
r <- rasterFromXYZ(coordinates(ptsreg))
r[] <- 1:ncell(r) # add some values

# check the results are correct
plot(r)
points(ptsreg, pch = ".", col = "black")
+2

, SpatialPixels :

ptsreg <- spsample(spdf, 1000, type = "regular")
cells = SpatialPixels(ptsreg)

# check results
plot(cells)
points(ptsreg, pch = 16, col = "black")

.

0

Source: https://habr.com/ru/post/1659746/


All Articles