【问题标题】:How can I extract values from one stars object using the extent of another stars object in R?如何使用 R 中另一个星星对象的范围从一个星星对象中提取值?
【发布时间】:2021-12-07 09:19:14
【问题描述】:

我对在 R 中使用 stars 包很陌生,但我无法弄清楚如何创建一个 stars 对象 C,它具有来自对象 A 的值但对象 B 的范围。具体来说,我有一张地图欧洲春季平均气温(对象 A),我想使用包含落叶阔叶林存在的单独星星对象(对象 B)对其进行裁剪。

对象 A:https://i.stack.imgur.com/DQsZn.jpg

> CRU.SpringT.2009.2018_EU
stars object with 2 dimensions and 1 attribute
attribute(s):
                                   Min.  1st Qu.   Median     Mean  3rd Qu.     Max.   NA's
CRU.SpringT.2009.2018_EU.tif  -15.81895 1.201286 5.480992 4.979221 8.204463 17.55605 310479
dimension(s):
  from   to offset      delta refsys point values x/y
x    1 1440    -10  0.0416667 WGS 84 FALSE   NULL [x]
y    1  672     65 -0.0416667 WGS 84 FALSE   NULL [y]

对象 B:https://i.stack.imgur.com/6dons.jpg

> Dec.BL_EU3
stars_proxy object with 1 attribute in 1 file(s):
$Consensus_reduced_class_3.tif
[1] "[...]/Consensus_reduced_class_3.tif"

dimension(s):
   from    to offset       delta                       refsys point values x/y
x 20401 27600   -180  0.00833333 +proj=longlat +datum=WGS8... FALSE   NULL [x]
y  3001  6360     90 -0.00833333 +proj=longlat +datum=WGS8... FALSE   NULL [y]
call_list:
[[1]]
x[i = i, drop = drop, crop = crop]
attr(,".Environment")
<environment: 0x000002a17a1ecac0>

[[2]]
x[i = i, drop = drop, crop = crop]
attr(,".Environment")
<environment: 0x000002a178d61dc0>

[[3]]
e1/e2
attr(,".Environment")
<environment: 0x000002a177987b10>

两个对象都使用相同的 bbox 进行裁剪。目标是生成的对象(对象 C)具有 B 的范围,但具有 A 的温度值。

Dropbox 链接到 tif 文件:

对象 A:https://www.dropbox.com/s/lwvdxnis7k38e18/CRU.SpringT.2009.2018_EU.tif?dl=0

对象 B:https://www.dropbox.com/s/uybxk40z853mu7a/EU%20Dec%20Broadleaf.tif?dl=0

【问题讨论】:

  • 嗨@Ben Lee。请提供Object AObject B 的输出摘要。没有这些信息,就很难提供帮助
  • 嗨@lovalery,我已经编辑了我的提交以包含输出摘要。
  • 不确定我是否理解。你说Both objects are cropped using the same bbox.所以A和B的程度相同。如果您想在对象 C 中获得 A 的温度和 B 的范围,这就像想要在 A 的范围内获得 A 的温度。在这种情况下,像这样复制 object_A 就足够了object_C &lt;- object_AAm我错过了什么?
  • @lovalery 抱歉,我可能用错误的语言描述了我想要的内容。我想要一个看起来像 B 的星星对象(也就是说,它只有存在落叶阔叶林的值),但是,显示的值不是 1,而是来自 A 的温度值。现在 B 只是存在/不存在森林,但我想要一个连续的温度值,只要存在 = 1。
  • 好的。谢谢。现在对我来说更清楚了:-)。没有手头的数据并不容易,但我会尽力给你一个答案,让你尝试(至少!)让你走上正确的轨道:-)

标签: r r-stars


【解决方案1】:

就是这样!使用这些文件要容易得多:-) 请在下面找到一种可能的解决方案来解决您的问题。它适用于我的电脑。我希望对你来说也是一样的:-)

我特意详细说明了代码,以便您更容易理解。当然,如果需要,您可以修改表单以使其更紧凑。

伪代表!!

library(sf)
library(stars)
library(ggplot2)

A <- read_stars("CRU.SpringT.2009.2018_EU.tif")
B <- read_stars("consensus_full_class_3.tif")

# First step: Transform B to have the same extent and resolution of A
B_cropped_resample <- st_warp(B, A, use_gdal = TRUE)


# Second step: convert 0 to NA values in your object B (i.e. deciduous forests)
B_cropped_resample[B_cropped_resample == 0] <- NA # B should contain only NA and 1 values 


# Third step: convert B into `sf` object with 'points' geometry
B_cropped_resample_sf <- st_as_sf(B_cropped_resample, as_points = TRUE, na.rm = TRUE)


# Fourth step : extract values of object A with the object `B_cropped_resample_sf`
C <- st_extract(A, B_cropped_resample_sf) # C is a sf object which contains the 
                                          # values of A at the corresponding points

# Sixth step: convert 'sf' object back into stars 'object' and split 'sf' point 
# geometry into two 'stars' dimensions
C <- st_as_stars(C, name = attr(C, "CRU.SpringT.2009.2018_EU.tif", "geometry"))
C <- st_sfc2xy(C)


# Seventh step: plot the resulting 'stars' object which shows the temperatures (i.e.  
# values from object A) for each deciduous forest (i.e. locations from object B)
ggplot2::ggplot() + geom_stars(data = C) +
  coord_equal()

请找到运行代码后得到的图片:

【讨论】:

  • 希望对您有所帮助。请随时通知我结果。干杯。
  • 嗨@lovalery,这似乎非常接近我想要的!但是,结果目前是点列表而不是星星对象(至少我认为):&gt; C Simple feature collection with 6773164 features and 1 field Geometry type: POINT Dimension: XY Bounding box: xmin: -9.995833 ymin: 37.00417 xmax: 49.99583 ymax: 64.99583 CRS: +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 有没有办法将其转换回星星对象?
  • 当然是的 :-) 我会完成我的回答。
  • 已编辑。请随时通知我结果!如果一切正常,请考虑将此答案标记为已接受。如果没有,请随时告诉我还有什么问题。干杯。
  • 谢谢@lovalery,但现在我遇到了一个新问题。我希望使用 ggplot() + geom_stars(data = C) 使用 ggplot2 绘制此对象,但是当我尝试使用新对象执行此操作时,我收到错误 Error in check.length(gparname): 'gpar' element 'fontsize' must not be length 0 和警告 In addition: Warning message: Removed 6773164 rows containing missing values (geom_sf)。我认为这可能是因为该对象只有一个维度(请参阅以下评论)
猜你喜欢
  • 2021-11-13
  • 2014-02-23
  • 2011-09-24
  • 1970-01-01
  • 2012-09-05
  • 1970-01-01
  • 2021-06-21
  • 2021-05-26
  • 1970-01-01
相关资源
最近更新 更多