【问题标题】:What is the equivalent of R's paste0() for raster layers?What is the equivalent of R\'s paste0() for raster layers?
【发布时间】:2022-12-28 02:01:48
【问题描述】:

I have three overlapping rasters with the same pixel size. raster1 has the values 1-10, raster2 has the values 1-4 and raster3 has the values 1-6. I want to create a new layer that pastes their values together, to get a unique ID. For example, for a pixel in which raster1=10, raster2=3, raster3=5 I want the output to be a raster with a pixel value of "10-3-5".

【问题讨论】:

标签: r raster arcgis


【解决方案1】:

You can use unique(x, as.raster=TRUE) with "terra"

Example data

library(terra)
r <- rast(ncols=5, nrows=5)
values(r) <- rep(1:5, each=5)
s <- c(r, round(r/3), r+2)

Solution

u <- unique(s, as.raster=TRUE)

Inspect the results

plot(u)

u
#class       : SpatRaster 
#dimensions  : 5, 5, 1  (nrow, ncol, nlyr)
#resolution  : 72, 36  (x, y)
#extent      : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
#coord. ref. : lon/lat WGS 84 
#source      : memory 
#categories  : label, lyr.1, lyr.1.1, lyr.1.2 
#name        : label 
#min value   : 1_0_3 
#max value   : 5_2_7 

levels(u)
#[[1]]
#  ID label
#1  0 1_0_3
#2  1 2_1_4
#3  2 3_1_5
#4  3 4_1_6
#5  4 5_2_7

【讨论】:

  • Thank you for your reply! I tried it and the labels seem good, however when I compared the three input rasters to the output one it seems something is not working well? When I ran r1 &lt;- rast(ncols=5, nrows=5) values(r1) &lt;- rep(c(1,2,3,4,5), each=5) r2 &lt;- r1 r3 &lt;- r1 s &lt;- c(r1,r2,r3) u &lt;- terra::unique(s,as.raster=TRUE), levels(u) looks like the output I needed. However, plot(u$label) shows that not all the lables appear in the final raster. For example 4_4_4 is missing.
  • That works for me, not sure why it would fail for you. Perhaps re-install the dev version?
猜你喜欢
  • 2022-12-02
  • 2022-12-28
  • 2022-11-28
  • 2022-12-27
  • 2022-12-02
  • 2022-12-02
  • 2022-12-27
  • 2022-12-02
  • 2022-12-01
相关资源
最近更新 更多