为何fasterize函数生成的存在/缺失栅格全部为0?
问题:fasterize生成全0存在/缺失栅格的修复方案
我需要从一组坐标生成存在/缺失(presence/absence)栅格,已将坐标转换为多边形,但使用fasterize函数时,生成的栅格与模板栅格形状一致但全部填充为0。我认为读取名为"yes"的全1列的代码存在问题,但未触发报错且成功生成了栅格,作为空间数据处理新手,不知道如何修复。
使用的fasterize代码
paraster <- fasterize(paframe, cropbgEU, field = "yes", background = 0) %>% mask(cropbgEU)
多边形paframe与模板栅格cropbgEU详情
paframe信息
> paframe Simple feature collection with 2103 features and 1 field Geometry type: POLYGON Dimension: XY Bounding box: xmin: 1.576116 ymin: 41.1864 xmax: 28.48484 ymax: 70.51157 Geodetic CRS: WGS 84 First 10 features: yes geometry 1 1 POLYGON ((17.85235 59.86206... 2 1 POLYGON ((15.26105 59.28842... 3 1 POLYGON ((6.511416 52.38192... 4 1 POLYGON ((22.4303 60.42065,... 5 1 POLYGON ((20.73348 50.34674... 6 1 POLYGON ((21.05561 55.90442... 7 1 POLYGON ((25.47543 61.0417,... 8 1 POLYGON ((24.82326 60.19986... 9 1 POLYGON ((13.92114 53.08867... 10 1 POLYGON ((20.99356 56.31544...
cropbgEU信息
> cropbgEU class : RasterLayer dimensions : 4260, 4620, 19681200 (nrow, ncol, ncell) resolution : 0.008333333, 0.008333333 (x, y) extent : -10, 28.5, 36, 71.5 (xmin, xmax, ymin, ymax) crs : +proj=longlat +datum=WGS84 +no_defs source : r_tmp_2023-06-04_002524_22764_76822.grd names : layer values : -11.09583, 19.84583 (min, max)
已确认两者的范围(extent)和坐标参考系统(CRS)匹配,这曾是类似栅格全为NA问题的原因。
尝试过的方法及报错
参考rasterize函数相关问题的解决方法,使用field = paframe@data[,"yes"]时触发报错:
Error in h(simpleError(msg, call)) : error in evaluating the argument 'x' in selecting a method for function 'mask': trying to get slot "data" from an object (class "sf") that is not an S4 object
多边形创建代码
paframe <- presence %>% filter(yes %in% 1) %>% st_as_sf(coords=c("decimalLongitude","decimalLatitude"), crs = 4326) %>% # convert to an sf polygon object by buffering the points st_buffer(1)
原始presence数据前几行
> head(presence) decimalLatitude decimalLongitude yes 26 59.86205 17.85235 1 31 59.28843 15.26104 1 95 52.38192 6.51143 1 128 60.42064 22.43029 1 189 50.34675 20.73349 1 190 55.90442 21.05563 1
参考过相关博客文章,恳请提供修复方案。
内容的提问来源于stack exchange,提问作者procyon
相关产品推荐
相关产品推荐

