I am new to R-Gis.
Here is what I need to do, I need to make a raster inside the larger polygon (image above) each cell has to be 10x10 m, mask it to cut cells outside the the larger polygon and then assign a different value for each cell in the smaller polygons. Furthermore, I'd like to have a list (dataframe) with the coordinate of the center of each cell and the value associated to it.
And here is my code (not much):
library(tidyverse)
library(sp)
library(raster)
coords <-structure(list(id = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L,
3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L), .Label = c("1.1", "2.1", "3.1",
"3.2"), class = "factor"), lon = c(-47.720315, -47.7135780555556,
-47.7122261111111, -47.7175261111111, -47.7187488888889, -47.715595,
-47.7135130555556, -47.7166461111111, -47.7181161111111, -47.7169469444444,
-47.71553, -47.716925, -47.7161630555556, -47.71552, -47.7139961111111,
-47.7148438888889), lat = c(-22.5507461111111, -22.5456230555556,
-22.549705, -22.5537280555556, -22.550825, -22.5479419444444,
-22.5494280555556, -22.5521630555556, -22.5507561111111, -22.549725,
-22.5507461111111, -22.5515380555556, -22.54924, -22.5486061111111,
-22.5494769444444, -22.55026)), class = "data.frame", .Names = c("id",
"lon", "lat"), row.names = c(NA, -16L))
coord.poly <- coords %>%
nest(-id) %>%
mutate(Poly = purrr::map(data, ~dplyr::select(., lon, lat) %>% Polygon()),
polys = map2(Poly, id, ~Polygons(list(.x),.y))
) %>%
{SpatialPolygons(.$polys)}
proj4string(coord.poly) <- CRS("+proj=utm +zone=23 +south +ellps=WGS84 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs")
r <- raster(coord.poly, ncol = 100, nrow = 100) #don't know how to set cell size in meters :/
r[] <- 1:length(r)
plot(mask(r, coord.poly), axes = TRUE)
EDIT: forgot to include the libraries