使用 fastize 和 st_cast 的错误

问题描述

我正在尝试从点数据栅格化我的数据框。虽然下面的代码在使用 rasterize 时可以工作,但是,如果不是几天的话,它需要几个小时才能完成,所以我想到了使用 fasterize,因为我已经阅读了很多次。但是,它为我提供了错误

这是我在使用虚拟数据复制错误时的尝试:

#dataset

Rows: 50
Columns: 7
$ id                                   <int> 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,...
$ longitude                            <dbl> -113.5651,-113.6501,-113.6146,-113.5791,-113.5435,-113.5080,-113.7351,-113.6996,-113.6641,-113.6286,-113....
$ latitude                             <dbl> 54.12708,54.10625,54.08542,54.08...
$ cropland_to_cropland                 <dbl> 1.00000000,1.00000000,0.25000000,1.00000...
$ deciduous_needleleaf_to_mixed_forest <dbl> 0.000,0.000,0.125,0.0...
$ savanna_to_savanna                   <dbl> 0.000,0.0...
$ savanna_to_grassland                 <dbl> 0.000,0.0...

#convert to raster using dummy raster code

a <- raster(xmn=-100,xmx=100,ymn=-90,ymx=90,res=10) 
a <- setValues(a,1:ncell(a))

class      : RasterLayer 
dimensions : 18,360  (nrow,ncol,ncell)
resolution : 10,10  (x,y)
extent     : -100,100,-90,90  (xmin,xmax,ymin,ymax)
crs        : +proj=longlat +datum=wgs84 +no_defs 
source     : memory
names      : layer 
values     : 1,360  (min,max)

forest_cover <- u %>% 
       # convert to spatial features
       st_as_sf(coords = c("longitude","latitude"),crs = 4326) %>% 
      st_transform(crs = projection(a)) %>%
      # rasterize points
       fasterize(a) %>% 
  # project to albers equal-area for mapping
       projectRaster(crs = st_crs("ESRI:102003")$proj4string,method = "ngb") %>% 
     # trim off empty edges of raster
      trim()

#error:
1. Error in fasterize(.,a) : sf geometry must be polyGON or MULTIpolyGON

#step 2. combine the point features because st_cast(x,"polyGON") does not work,as shown:

u %>% 
     # convert to spatial features
    st_as_sf(coords = c("longitude",crs = 4326) %>% 
    st_transform(crs = projection(a)) %>%
    st_cast("polyGON")

#error
2. Error in st_cast.sfc(st_geometry(x),to,group_or_split = do_split) : 
  use smaller steps for st_cast; first cast to MULTILInesTRING or polyGON?

#As mentioned before st_combine() works in this,so I tried it in the fasterize code:

forest_cover <- u %>% 
       # convert to spatial features
       st_as_sf(coords = c("longitude",crs = 4326) %>% 
      st_transform(crs = projection(a)) %>%
    st_combine() %>%
    st_cast("polyGON") %>%
      # rasterize points
       fasterize(a) %>% 
  # project to albers equal-area for mapping
       projectRaster(crs = st_crs("ESRI:102003")$proj4string,method = "ngb") %>% 
     # trim off empty edges of raster
      trim()

#Error

3. Error in fasterize(.,a) : 
  Expecting a single string value: [type=NULL; extent=0].

可重现的代码

structure(list(id = 1:50,longitude = c(-113.565053896692,-113.650126836643,-113.614592294281,-113.579057751919,-113.543523209557,-113.507988667195,-113.735129352686,-113.699612652392,-113.664095952097,-113.628579251803,-113.593062551509,-113.557545851214,-113.52202915092,-113.486512450626,-113.820061592082,-113.784562711256,-113.749063830431,-113.713564949605,-113.678066068779,-113.642567187953,-113.607068307127,-113.571569426301,-113.536070545475,-113.500571664649,-113.465072783824,-113.869442617844,-113.833961533926,-113.798480450008,-113.76299936609,-113.727518282172,-113.692037198254,-113.656556114336,-113.621075030418,-113.5855939465,-113.550112862583,-113.514631778665,-113.479150694747,-113.443669610829,-112.272793841538,-112.23731275762,-112.201831673702,-113.918789209261,-113.883325899729,-113.847862590198,-113.812399280666,-113.776935971134,-113.741472661602,-113.70600935207,-113.670546042538,-113.635082733006
),latitude = c(54.1270833284761,54.1062499951447,54.0854166618132,54.0645833284818,54.0437499951503,54.0229166618189,54.0229166618189),cropland_to_cropland = c(1,1,0.25,0.333333333333333,0.5,0.0769230769230769,0.0909090909090909,0.285714285714286,1),deciduous_needleleaf_to_mixed_forest = c(0,0),savanna_to_savanna = c(0,savanna_to_grassland = c(0,0)),row.names = c(NA,50L),class = "data.frame")

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...