r - How to subset SpatialGrid using SpatialPolygon -
i trying subset spatialgrid
1 polygon present in class spatialpolygons
. how can this?
i tried way:
grd.clip <- grd[!is.na(over(grd, polygon))]
but error
error in matrix(idx, gr@cells.dim[2], gr@cells.dim[1], byrow = true)[rows, : (subscript) logical subscript long
i've tried way turn out not desired solution. i'll keep here illustrate original idea i'll willing delete if necessary
the solution initial question still not clear me i'll make better if necessary
library(sp) library(rgdal) library(raster) library(latticeextra)
load shapefile
shp <- readogr(dsn = "d:/programacao/r/stackoverflow/17962821", layer = "shp") proj4string(shp)
create grid topology
grid <- gridtopology(cellcentre.offset=c(731888.0,7457552.0), cellsize=c(16,16),cells.dim=c(122,106)) grid <- spatialgrid(grid, proj4string=crs(proj4string(shp)))
convert spatialgrid rasterlayer
rgrid <- raster(extent(grid)) res(rgrid) <- c(16, 16)
give numbers
rgrid[] <- runif(ncell(grid), 1, 10) proj4string(rgrid) <- crs(proj4string(shp)) plot(rgrid)
mask raster spdf
rgrid_msk <- mask(rgrid,shp) plot(rgrid_msk)
convert grid retaining attribute values
grid_ae <- as(rgrid_msk, 'spatialpointsdataframe') grid_ae <- grid_ae[!is.na(grid_ae@data$layer), ] gridded(grid_ae) <- true summary(grid_ae) > summary(grid_ae) object of class spatialpixelsdataframe coordinates: min max x 731912 733816 y 7457560 7459224 projected: true proj4string : [+proj=utm +zone=22 +south +ellps=aust_sa +units=m +no_defs] number of points: 7814 grid attributes: cellcentre.offset cellsize cells.dim x 731920 16 119 y 7457568 16 104 data attributes: min. 1st qu. median mean 3rd qu. max. 1.005 3.231 5.523 5.512 7.748 9.999 spplot(grid_ae) + latticeextra::layer(sp.polygons(shp, fill = na, col = 'red'))
the solution preserve attributes of spdf after intersect regular area
library(rgeos) library(rgdal) library(sp) library(latticeextra) grid <- readogr(dsn = 's:/temporarios', layer = 'grid') proj4string(grid) <- crs('+init=epsg:4326') grid class : spatialpolygonsdataframe features : 110 extent : -9.6, -7.95, 36.45, 37.95 (xmin, xmax, ymin, ymax) coord. ref. : +init=epsg:4326 +proj=longlat +datum=wgs84 +no_defs +ellps=wgs84 +towgs84=0,0,0 variables : 1 names : id min values : 652 max values : 761 summary(grid@data) id min. :652.0 1st qu.:679.2 median :706.5 mean :706.5 3rd qu.:733.8 max. :761.0 polyg <- readogr(dsn = 's:/temporarios', layer = 'polyg') proj4string(polyg) <- crs('+init=epsg:4326') # plot spplot(grid, 'id') + latticeextra::layer(sp.polygons(polyg, fill = na, col = 'blue'))ยด
# clip clipgrid <- gintersection(grid, polyg, byid = t, id = as.character(grid@data$id)) cells <- row.names(clipgrid) cells <- split(cells, ' ') clipspdf <- as(clipgrid, 'spatialpolygonsdataframe') clipspdf@data$id <- as.numeric(row.names(clipspdf@data)) spplot(clipspdf, 'id')
summary(clipspdf@data) dummy id min. :0 min. :665.0 1st qu.:0 1st qu.:687.8 median :0 median :706.5 mean :0 mean :706.4 3rd qu.:0 3rd qu.:725.2 max. :0 max. :747.0
download data dropbox
Comments
Post a Comment