admin管理员组文章数量:1406937
I'm trying to use RGB
and colorize
functions from terra
to create a RGB color gradient on my SpatRaster
r, however, I get errors and I don't know how to fix them.
# Generate random point in a data frame
df <- data.frame(lon = rnorm(10000, 5.5), lat = rnorm(10000, 52.5))
# Create SpatRaster
r1 <- rast(nrows=250, ncols=250, xmin=min(df[1]), xmax=max(df[1]),
ymin=min(df[2]), ymax=max(df[2]))
# Create SpatRaster from df
r <- rasterize(as.matrix(df[1:2]), r1)
plot(r)
# try to use RGB from terra function to create RGB color on my SpatRaster
RGB(r) <- 1:3
Error: [set.RGB] value(s) are not value layer numbers
# try to use colorize from terra function to create RGB color on my SpatRaster
colorize(r, to="rgb")
Error: [colorize] x has no color table
Then, I'm also trying to use coltab(r)
as suggested but unsuccessful,
coltab(r) <- data.frame(value=1:250, col=c("red", "blue"))
colorize(r, to="rgb")
plot(r)
I'm only obtain a red raster.
Any suggestions? Thanks in advance.
I'm trying to use RGB
and colorize
functions from terra
to create a RGB color gradient on my SpatRaster
r, however, I get errors and I don't know how to fix them.
# Generate random point in a data frame
df <- data.frame(lon = rnorm(10000, 5.5), lat = rnorm(10000, 52.5))
# Create SpatRaster
r1 <- rast(nrows=250, ncols=250, xmin=min(df[1]), xmax=max(df[1]),
ymin=min(df[2]), ymax=max(df[2]))
# Create SpatRaster from df
r <- rasterize(as.matrix(df[1:2]), r1)
plot(r)
# try to use RGB from terra function to create RGB color on my SpatRaster
RGB(r) <- 1:3
Error: [set.RGB] value(s) are not value layer numbers
# try to use colorize from terra function to create RGB color on my SpatRaster
colorize(r, to="rgb")
Error: [colorize] x has no color table
Then, I'm also trying to use coltab(r)
as suggested https://stackoverflow/a/73833702/15834162 but unsuccessful,
coltab(r) <- data.frame(value=1:250, col=c("red", "blue"))
colorize(r, to="rgb")
plot(r)
I'm only obtain a red raster.
Any suggestions? Thanks in advance.
Share Improve this question asked Mar 4 at 10:07 TristanTristan 4703 silver badges11 bronze badges1 Answer
Reset to default 0Example data
library(terra)
r <- rast(nrows=16, ncols=16, xmin=0, xmax=1, ymin=0, ymax=1, vals=1:256)
Add a color table
coltab(r) <- data.frame(ID=1:256, col=rainbow(256))
plot(r)
Now that you have a single-layer raster with a color table you could (but probably shouldn't) create a three-layer RGB raster:
x <- colorize(r, to="rgb")
plot(x[[1:3]])
And now that you have three layers, you could use RGB
to set/change the color channels:
RGB(x) <- 3:1
PS: The example data you provided only has a single layer. Hence RGB
does not work. And the raster only has a one unique value, so you would not expect multiple colors?
版权声明:本文标题:r - Create a Red-Green-Blue (RGB) color on SpatRaster using terra RGB and colorize functions? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745051701a2639695.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论