【问题标题】:How to capture data within dataframe when using st_rasterize?使用 st_rasterize 时如何在数据框中捕获数据?
【发布时间】:2020-02-17 10:28:25
【问题描述】:

我有一个多边形:

p1 <- rbind(c(-180,-20), c(-140,55), c(10, 0), c(-140,-60), c(-180,-20))
hole <- rbind(c(-150,-20), c(-100,-10), c(-110,20), c(-150,-20))
p1 <- list(p1, hole)

我创建了一个simple feature 多边形对象:

library(sf)
poly_sfc <- st_sfc(st_polygon(p1))

现在添加一个简单的数据框:

data <- data.frame(name = "Los Angeles", language = "English", weather ="sunny")
df <- st_sf(data, geometry = poly_sfc)

我可以看到它已将数据添加到整体sfc

Simple feature collection with 1 feature and 3 fields
geometry type:  POLYGON
dimension:      XY
bbox:           xmin: -180 ymin: -60 xmax: 10 ymax: 55
epsg (SRID):    NA
proj4string:    NA
         name language weather                       geometry
1 Los Angeles  English   sunny POLYGON ((-180 -20, -140 55...

现在我想将它栅格化,我可以使用它:

library(star)
r <- st_rasterize(df, options = "ALL_TOUCHED=TRUE")

但是,当我查看栅格 r 时,df 中的数据已被删除。

stars object with 2 dimensions and 1 attribute
attribute(s):
      ID        
 Min.   :1      
 1st Qu.:1      
 Median :1      
 Mean   :1      
 3rd Qu.:1      
 Max.   :1      
 NA's   :34597  
dimension(s):
  from  to offset     delta refsys point values    
x    1 328   -180  0.580682     NA    NA   NULL [x]
y    1 199     55 -0.580682     NA    NA   NULL [y]

如何确保来自df 的数据被传递到栅格中?有没有办法通过st_rasterize 做到这一点??

【问题讨论】:

    标签: r dataframe r-raster sf rasterizing


    【解决方案1】:

    所以你从 df made 开始。所以要制作一个栅格:

    library(raster)
    r <- raster(ncol = 180, nrow = 180) # create a base of the raster
    extent(r) <- extent(df) # extent the raster to your df
    rp <- rasterize(df, r)
    

    这就是结果

    【讨论】:

    • 我认为这不能回答这个问题。我正在尝试将数据帧从 sfc 捕获到栅格中
    猜你喜欢
    • 2016-06-11
    • 2019-12-28
    • 1970-01-01
    • 2017-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多