【问题标题】:NAs returned during extract mean values from a raster从栅格提取平均值期间返回的 NA
【发布时间】:2020-05-13 14:00:33
【问题描述】:

我正在从具有多边形 shapefile 的栅格中提取平均值。光栅和矢量的 CRS 相同,但提取函数也返回 NA 值。

library(rgdal)
library(raster)
options(stringsAsFactors = FALSE)
Shapefile <- readOGR("Fishnet_geolocated.shp",layer="Fishnet_geolocated")
plot(Shapefile)
CHM_Napi<-raster("Napi_CRS.tif")
crs(Shapefile)
crs(CHM_Napi)
Napi_extract <- raster::extract(CHM_Napi, # the raster that you wish to extract values from
                                Shapefile, # a point, or polygon spatial object
                                fun = mean, # extract the MEAN value from each plot
                                sp = TRUE) # create spatial object
class(Napi_extract)
summary(Napi_extract$Napi_CRS)

运行此代码后,我得到一个结果-

> Shapefile <- readOGR("Fishnet_geolocated.shp",layer="Fishnet_geolocated")
OGR data source with driver: ESRI Shapefile 
Source: ""
with 63 features
It has 5 fields
Integer64 fields read as strings:  id 
> plot(Shapefile)
> CHM_Napi<-raster("Napi_CRS.tif")
> crs(Shapefile)
CRS arguments:
 +proj=utm +zone=36 +south +datum=WGS84 +units=m +no_defs +ellps=WGS84
+towgs84=0,0,0 
> crs(CHM_Napi)
CRS arguments:
 +proj=utm +zone=36 +south +datum=WGS84 +units=m +no_defs +ellps=WGS84
+towgs84=0,0,0 
> class(Napi_extract)
[1] "SpatialPolygonsDataFrame"
attr(,"package")
[1] "sp"
> summary(Napi_extract$Napi_CRS)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.    NA's 
     NA      NA      NA     NaN      NA      NA      63 

提取函数中是否缺少某些内容,我是否应该重新投影栅格和 shapefile。附言在 QGIS 中,矢量层和栅格层都重叠,但在 R 中没有。任何有用的提示都表示赞赏。

【问题讨论】:

    标签: r extract raster


    【解决方案1】:

    栅格中可能存在 NA 值。像这样使用na.rm=TRUE

    Napi_extract

    【讨论】:

      猜你喜欢
      • 2018-12-01
      • 2021-09-29
      • 2022-06-15
      • 1970-01-01
      • 2022-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多