如何使用SpatialPolygon对SpatialGrid进行子集化

时间:2013-07-31 06:07:32

标签: r spatial

我正在尝试使用SpatialGrid中存在的1个多边形对SpatialPolygons进行子集化。我怎么能这样做?

我试过这种方式:

grd.clip <- grd[!is.na(over(grd, polygon))]

但我收到错误

Error in matrix(idx, gr@cells.dim[2], gr@cells.dim[1], byrow = TRUE)[rows,  : 
  (subscript) logical subscript too long

3 个答案:

答案 0 :(得分:4)

我尝试过一种不是理想解决方案的方法。我会把它放在这里以说明我最初的想法但我愿意在必要时删除它

最初问题的解决方案对我来说仍然不清楚,但如果有必要我会做得更好

library(sp)
library(rgdal)
library(raster)
library(latticeExtra)

加载shapefile

shp <- readOGR(dsn = "D:/Programacao/R/Stackoverflow/17962821", layer = "shp")
proj4string(shp)

创建网格拓扑

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)))

将SpatialGrid转换为rasterLayer

rgrid <- raster(extent(grid))
res(rgrid) <- c(16, 16)

给它一些数字

rgrid[] <- runif(ncell(grid), 1, 10)
proj4string(rgrid) <- CRS(proj4string(shp))
plot(rgrid)

rgrid

使用SPDF掩码光栅

rgrid_msk <- mask(rgrid,shp)
plot(rgrid_msk)

rgrid_msk

将其转换回网格保留属性值

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
Is 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'))

grid_ae

在与常规区域

交叉后保留SPDF属性的解决方案
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 it
spplot(grid, 'ID') +
  latticeExtra::layer(sp.polygons(polyg, fill = NA, col = 'blue'))´

plot layers

# 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')

plot clip

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  

从此Dropbox

下载数据

答案 1 :(得分:1)

以下R代码有效(请注意示例中缺少的,),在此答案结尾处给出了运行此代码所需的一些对象:

meuse.grid[!is.na(over(meuse.grid, sr)),]

如果这不能解决您的问题,请提供一个可重复的示例来说明问题。

需要一些对象:

 r1 = cbind(c(180114, 180553, 181127, 181477, 181294, 181007, 180409, 
 180162, 180114), c(332349, 332057, 332342, 333250, 333558, 333676, 
 332618, 332413, 332349))
 r2 = cbind(c(180042, 180545, 180553, 180314, 179955, 179142, 179437, 
 179524, 179979, 180042), c(332373, 332026, 331426, 330889, 330683, 
 331133, 331623, 332152, 332357, 332373))
 r3 = cbind(c(179110, 179907, 180433, 180712, 180752, 180329, 179875, 
 179668, 179572, 179269, 178879, 178600, 178544, 179046, 179110),
 c(331086, 330620, 330494, 330265, 330075, 330233, 330336, 330004, 
 329783, 329665, 329720, 329933, 330478, 331062, 331086))
 r4 = cbind(c(180304, 180403,179632,179420,180304),
 c(332791, 333204, 333635, 333058, 332791))

 sr1=Polygons(list(Polygon(r1)),"r1")
 sr2=Polygons(list(Polygon(r2)),"r2")
 sr3=Polygons(list(Polygon(r3)),"r3")
 sr4=Polygons(list(Polygon(r4)),"r4")
 sr=SpatialPolygons(list(sr1,sr2,sr3,sr4))
 srdf=SpatialPolygonsDataFrame(sr, data.frame(cbind(1:4,5:2), row.names=c("r1","r2","r3","r4")))

 data(meuse)
 coordinates(meuse) = ~x+y

 data(meuse.grid)
 gridded(meuse.grid) = ~x+y

答案 2 :(得分:0)

感谢Paul Hiemstra和Paulo Cardoso的见解。

问题是我必须将SpatialGrid对象转换为SpatialPixelsDataFrame对象。

所以这就是我所做的:

require(sp)
require(rgdal)

grid = GridTopology(cellcentre.offset=c(731888.0,7457552.0),cellsize=c(16,16),cells.dim=c(122,106))
grid = SpatialPixelsDataFrame(grid,
                          data=data.frame(id=1:prod(122,106)),
                          proj4string=CRS(proj4string(shp)))
plot(grid)

enter image description here

shp = readOGR(dsn = "...", layer = "shp")
bound = shp@polygons
bound = SpatialPolygons(bound, proj4string=CRS("+proj=utm +zone=22 +south +ellps=aust_SA +units=m +no_defs"))
plot(bound)

enter image description here

clip_grid = grid[!is.na(over(grid, bound)),]
plot(clip_grid)

enter image description here