在SpatialPixelsDataFrame上覆盖shapefile时生成的NA

时间:2012-11-26 12:48:19

标签: r shapefile rgdal

当将一系列流域多边形(从shapefile)覆盖到SpatialPixelsDataFrame对象上时,我遇到了一个奇怪的问题。有70个集水区,但我收集了60个集水区NA

数据在Dropbox上可用,以下脚本将下载,解压缩等文件并创建可重现的示例。

## load packages required
require(rgdal)
require(sp)

## proj4 string
BNG <- CRS("+init=epsg:27700")

## load in the grid
download.file("https://www.dropbox.com/s/7dvcf6ddo6r3043/depogrid.rds?dl=1",
              "./depogrid.rds", method = "wget")
myGrid <- readRDS("depogrid.rds")

## convert to spatial pixels
depo.sp <- SpatialPixelsDataFrame(data.matrix(myGrid[, 1:2]),
                                  data = myGrid[, 3, drop = FALSE],
                                  proj4string = BNG)

## load the shapefile
download.file("https://www.dropbox.com/s/bwzmeumnrja3c0o/dart.zip?dl=1",
              "./dart.zip", method = "wget")
unzip("./dart.zip")
shp <- readOGR(".", "DARTall")
## project shp to OSGB36
shp <- spTransform(shp, BNG)

## overlay shapefile on depo grid
depo.dart <- over(shp, depo.sp)

问题是NA s:

> head(depo.dart)
  Deposition
0         NA
1         NA
2         NA
3         NA
4         NA
5  0.2604089
> sum(is.na(depo.dart[,1]))
[1] 60
> nrow(depo.dart)
[1] 70

由于网格覆盖整个英国地区且集水区很小,不应该成为任何NA,所以我可能做错了!如果是这样,我做错了什么,我该如何解决这个问题呢?我想最终得到Deposition中70个流域中每个流域的SpatialPixelsDataFrameshp数据)的平均值。

[注意网格是虚拟数据;我无法分发实际的沉积网格,但数据和对象基本上(完全)与depo.sp相同。

其他信息

> over(shp, depo.sp, returnList = TRUE)
[[1]]
[1] Deposition
<0 rows> (or 0-length row.names)

[[2]]
[1] Deposition
<0 rows> (or 0-length row.names)

[[3]]
[1] Deposition
<0 rows> (or 0-length row.names)

[[4]]
[1] Deposition
<0 rows> (or 0-length row.names)

[[5]]
[1] Deposition
<0 rows> (or 0-length row.names)

[[6]]
     Deposition
3143  0.2604089

[[7]]
[1] Deposition
<0 rows> (or 0-length row.names)

[[8]]
     Deposition
3289  0.1139663

[[9]]
[1] Deposition
<0 rows> (or 0-length row.names)

[[10]]
[1] Deposition
<0 rows> (or 0-length row.names)

[[11]]
[1] Deposition
<0 rows> (or 0-length row.names)

[[12]]
[1] Deposition
<0 rows> (or 0-length row.names)

[[13]]
[1] Deposition
<0 rows> (or 0-length row.names)

[[14]]
[1] Deposition
<0 rows> (or 0-length row.names)

[[15]]
[1] Deposition
<0 rows> (or 0-length row.names)

[[16]]
[1] Deposition
<0 rows> (or 0-length row.names)

[[17]]
[1] Deposition
<0 rows> (or 0-length row.names)

[[18]]
[1] Deposition
<0 rows> (or 0-length row.names)

[[19]]
[1] Deposition
<0 rows> (or 0-length row.names)

[[20]]
     Deposition
3585  0.8610678

[[21]]
     Deposition
3288  0.2242067

[[22]]
[1] Deposition
<0 rows> (or 0-length row.names)

[[23]]
[1] Deposition
<0 rows> (or 0-length row.names)

[[24]]
[1] Deposition
<0 rows> (or 0-length row.names)

[[25]]
[1] Deposition
<0 rows> (or 0-length row.names)

[[26]]
     Deposition
3141  0.3296081

[[27]]
[1] Deposition
<0 rows> (or 0-length row.names)

[[28]]
[1] Deposition
<0 rows> (or 0-length row.names)

[[29]]
     Deposition
3438  0.3609788

[[30]]
[1] Deposition
<0 rows> (or 0-length row.names)

[[31]]
[1] Deposition
<0 rows> (or 0-length row.names)

[[32]]
     Deposition
3287  0.4812801

[[33]]
[1] Deposition
<0 rows> (or 0-length row.names)

[[34]]
[1] Deposition
<0 rows> (or 0-length row.names)

[[35]]
[1] Deposition
<0 rows> (or 0-length row.names)

[[36]]
[1] Deposition
<0 rows> (or 0-length row.names)

[[37]]
[1] Deposition
<0 rows> (or 0-length row.names)

[[38]]
[1] Deposition
<0 rows> (or 0-length row.names)

[[39]]
[1] Deposition
<0 rows> (or 0-length row.names)

[[40]]
[1] Deposition
<0 rows> (or 0-length row.names)

[[41]]
     Deposition
3436  0.3223757

[[42]]
[1] Deposition
<0 rows> (or 0-length row.names)

[[43]]
[1] Deposition
<0 rows> (or 0-length row.names)

[[44]]
[1] Deposition
<0 rows> (or 0-length row.names)

[[45]]
[1] Deposition
<0 rows> (or 0-length row.names)

[[46]]
[1] Deposition
<0 rows> (or 0-length row.names)

[[47]]
[1] Deposition
<0 rows> (or 0-length row.names)

[[48]]
[1] Deposition
<0 rows> (or 0-length row.names)

[[49]]
[1] Deposition
<0 rows> (or 0-length row.names)

[[50]]
[1] Deposition
<0 rows> (or 0-length row.names)

[[51]]
[1] Deposition
<0 rows> (or 0-length row.names)

[[52]]
[1] Deposition
<0 rows> (or 0-length row.names)

[[53]]
[1] Deposition
<0 rows> (or 0-length row.names)

[[54]]
[1] Deposition
<0 rows> (or 0-length row.names)

[[55]]
[1] Deposition
<0 rows> (or 0-length row.names)

[[56]]
[1] Deposition
<0 rows> (or 0-length row.names)

[[57]]
[1] Deposition
<0 rows> (or 0-length row.names)

[[58]]
[1] Deposition
<0 rows> (or 0-length row.names)

[[59]]
[1] Deposition
<0 rows> (or 0-length row.names)

[[60]]
[1] Deposition
<0 rows> (or 0-length row.names)

[[61]]
[1] Deposition
<0 rows> (or 0-length row.names)

[[62]]
     Deposition
2580  0.2820617

[[63]]
[1] Deposition
<0 rows> (or 0-length row.names)

[[64]]
[1] Deposition
<0 rows> (or 0-length row.names)

[[65]]
[1] Deposition
<0 rows> (or 0-length row.names)

[[66]]
[1] Deposition
<0 rows> (or 0-length row.names)

[[67]]
[1] Deposition
<0 rows> (or 0-length row.names)

[[68]]
     Deposition
3141  0.3296081
3287  0.4812801
3288  0.2242067
3438  0.3609788

[[69]]
[1] Deposition
<0 rows> (or 0-length row.names)

[[70]]
[1] Deposition
<0 rows> (or 0-length row.names)
> str(over(shp, depo.sp, returnList = TRUE))
List of 70
 $ :'data.frame':   0 obs. of  1 variable:
  ..$ Deposition: num(0) 
 $ :'data.frame':   0 obs. of  1 variable:
  ..$ Deposition: num(0) 
 $ :'data.frame':   0 obs. of  1 variable:
  ..$ Deposition: num(0) 
 $ :'data.frame':   0 obs. of  1 variable:
  ..$ Deposition: num(0) 
 $ :'data.frame':   0 obs. of  1 variable:
  ..$ Deposition: num(0) 
 $ :'data.frame':   1 obs. of  1 variable:
  ..$ Deposition: num 0.26
 $ :'data.frame':   0 obs. of  1 variable:
  ..$ Deposition: num(0) 
 $ :'data.frame':   1 obs. of  1 variable:
  ..$ Deposition: num 0.114
 $ :'data.frame':   0 obs. of  1 variable:
  ..$ Deposition: num(0) 
 $ :'data.frame':   0 obs. of  1 variable:
  ..$ Deposition: num(0) 
 $ :'data.frame':   0 obs. of  1 variable:
  ..$ Deposition: num(0) 
 $ :'data.frame':   0 obs. of  1 variable:
  ..$ Deposition: num(0) 
 $ :'data.frame':   0 obs. of  1 variable:
  ..$ Deposition: num(0) 
 $ :'data.frame':   0 obs. of  1 variable:
  ..$ Deposition: num(0) 
 $ :'data.frame':   0 obs. of  1 variable:
  ..$ Deposition: num(0) 
 $ :'data.frame':   0 obs. of  1 variable:
  ..$ Deposition: num(0) 
 $ :'data.frame':   0 obs. of  1 variable:
  ..$ Deposition: num(0) 
 $ :'data.frame':   0 obs. of  1 variable:
  ..$ Deposition: num(0) 
 $ :'data.frame':   0 obs. of  1 variable:
  ..$ Deposition: num(0) 
 $ :'data.frame':   1 obs. of  1 variable:
  ..$ Deposition: num 0.861
 $ :'data.frame':   1 obs. of  1 variable:
  ..$ Deposition: num 0.224
 $ :'data.frame':   0 obs. of  1 variable:
  ..$ Deposition: num(0) 
 $ :'data.frame':   0 obs. of  1 variable:
  ..$ Deposition: num(0) 
 $ :'data.frame':   0 obs. of  1 variable:
  ..$ Deposition: num(0) 
 $ :'data.frame':   0 obs. of  1 variable:
  ..$ Deposition: num(0) 
 $ :'data.frame':   1 obs. of  1 variable:
  ..$ Deposition: num 0.33
 $ :'data.frame':   0 obs. of  1 variable:
  ..$ Deposition: num(0) 
 $ :'data.frame':   0 obs. of  1 variable:
  ..$ Deposition: num(0) 
 $ :'data.frame':   1 obs. of  1 variable:
  ..$ Deposition: num 0.361
 $ :'data.frame':   0 obs. of  1 variable:
  ..$ Deposition: num(0) 
 $ :'data.frame':   0 obs. of  1 variable:
  ..$ Deposition: num(0) 
 $ :'data.frame':   1 obs. of  1 variable:
  ..$ Deposition: num 0.481
 $ :'data.frame':   0 obs. of  1 variable:
  ..$ Deposition: num(0) 
 $ :'data.frame':   0 obs. of  1 variable:
  ..$ Deposition: num(0) 
 $ :'data.frame':   0 obs. of  1 variable:
  ..$ Deposition: num(0) 
 $ :'data.frame':   0 obs. of  1 variable:
  ..$ Deposition: num(0) 
 $ :'data.frame':   0 obs. of  1 variable:
  ..$ Deposition: num(0) 
 $ :'data.frame':   0 obs. of  1 variable:
  ..$ Deposition: num(0) 
 $ :'data.frame':   0 obs. of  1 variable:
  ..$ Deposition: num(0) 
 $ :'data.frame':   0 obs. of  1 variable:
  ..$ Deposition: num(0) 
 $ :'data.frame':   1 obs. of  1 variable:
  ..$ Deposition: num 0.322
 $ :'data.frame':   0 obs. of  1 variable:
  ..$ Deposition: num(0) 
 $ :'data.frame':   0 obs. of  1 variable:
  ..$ Deposition: num(0) 
 $ :'data.frame':   0 obs. of  1 variable:
  ..$ Deposition: num(0) 
 $ :'data.frame':   0 obs. of  1 variable:
  ..$ Deposition: num(0) 
 $ :'data.frame':   0 obs. of  1 variable:
  ..$ Deposition: num(0) 
 $ :'data.frame':   0 obs. of  1 variable:
  ..$ Deposition: num(0) 
 $ :'data.frame':   0 obs. of  1 variable:
  ..$ Deposition: num(0) 
 $ :'data.frame':   0 obs. of  1 variable:
  ..$ Deposition: num(0) 
 $ :'data.frame':   0 obs. of  1 variable:
  ..$ Deposition: num(0) 
 $ :'data.frame':   0 obs. of  1 variable:
  ..$ Deposition: num(0) 
 $ :'data.frame':   0 obs. of  1 variable:
  ..$ Deposition: num(0) 
 $ :'data.frame':   0 obs. of  1 variable:
  ..$ Deposition: num(0) 
 $ :'data.frame':   0 obs. of  1 variable:
  ..$ Deposition: num(0) 
 $ :'data.frame':   0 obs. of  1 variable:
  ..$ Deposition: num(0) 
 $ :'data.frame':   0 obs. of  1 variable:
  ..$ Deposition: num(0) 
 $ :'data.frame':   0 obs. of  1 variable:
  ..$ Deposition: num(0) 
 $ :'data.frame':   0 obs. of  1 variable:
  ..$ Deposition: num(0) 
 $ :'data.frame':   0 obs. of  1 variable:
  ..$ Deposition: num(0) 
 $ :'data.frame':   0 obs. of  1 variable:
  ..$ Deposition: num(0) 
 $ :'data.frame':   0 obs. of  1 variable:
  ..$ Deposition: num(0) 
 $ :'data.frame':   1 obs. of  1 variable:
  ..$ Deposition: num 0.282
 $ :'data.frame':   0 obs. of  1 variable:
  ..$ Deposition: num(0) 
 $ :'data.frame':   0 obs. of  1 variable:
  ..$ Deposition: num(0) 
 $ :'data.frame':   0 obs. of  1 variable:
  ..$ Deposition: num(0) 
 $ :'data.frame':   0 obs. of  1 variable:
  ..$ Deposition: num(0) 
 $ :'data.frame':   0 obs. of  1 variable:
  ..$ Deposition: num(0) 
 $ :'data.frame':   4 obs. of  1 variable:
  ..$ Deposition: num [1:4] 0.33 0.481 0.224 0.361
 $ :'data.frame':   0 obs. of  1 variable:
  ..$ Deposition: num(0) 
 $ :'data.frame':   0 obs. of  1 variable:
  ..$ Deposition: num(0)

1 个答案:

答案 0 :(得分:3)

如果您愿意接受实验并且愿意使用raster包,那么这是一个可能的解决方案。请告诉我们。

当在栅格上覆盖多边形时,诀窍是指定small = TRUE,因为多边形的中心并不总是包含细胞中心(参见?extract)。

  

如果y表示多边形,则还会相对返回一个值   小多边形(例如小于Raster单个单元格的小多边形*   对象),或具有奇怪形状的多边形,否则没有值   返回是因为它们不包括任何栅格单元中心。

library(raster)
shp.raster <- raster(shp)
depo.sp.raster <- raster(depo.sp)
plot(depo.sp.raster)
plot(shp, add = TRUE)
zoom(depo.sp.raster) # click in the corners around the area to zoom in
plot(shp, add = TRUE)

res.out <- extract(depo.sp.raster, shp, layer = 1, small = TRUE)
table(unlist(lapply(res.out, is.null)))
> head(res.out)
[[1]]

0.0334963 

[[2]]

0.2604089 

[[3]]
[1] 0.5466648 0.7929624 0.4214012

[[4]]
[1] 0.2604089 0.3149418 0.5343358 0.0334963

[[5]]

0.5561869 

[[6]]

0.2604089

enter image description here