Skip to content

Commit

Permalink
add rast2colormap
Browse files Browse the repository at this point in the history
  • Loading branch information
kongdd committed Aug 12, 2024
1 parent 2ebeeaf commit c5d8aa4
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Suggests:
rmarkdown,
covr
VignetteBuilder: knitr
RoxygenNote: 7.2.3
RoxygenNote: 7.3.2
Roxygen: list(markdown = TRUE)
Encoding: UTF-8
Depends:
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export(overlap_fraction)
export(plot)
export(rast)
export(rast2SpatialGrid)
export(rast2colormap)
export(rast2poly)
export(rast_array)
export(rast_cellId)
Expand All @@ -43,6 +44,7 @@ export(st_range)
export(st_read)
export(st_rect)
export(st_write)
export(value2colormap)
export(write_shp)
import(sf)
importFrom(data.table,as.data.table)
Expand Down
17 changes: 17 additions & 0 deletions R/tools_rast.R
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,20 @@ rast_dim <- function(f) {

listk(lon, lat)
}

#' @export
value2colormap <- function(x, brks, cols) {
col2dt <- function(cols) col2rgb(cols) %>% t() %>% as.data.table()
colormap = col2dt(cols)
ind = findInterval(x, brks) # %>% summary()
colormap[ind, ] |> as.matrix()
}

#' @export
rast2colormap <- function(r, brks, cols, fout = "out.tif") {
x = values(r)
r2 = c(r, r, r)
val = value2colormap(x, brks, colors)
values(r2) = val
writeRaster(r2, fout)
}

0 comments on commit c5d8aa4

Please sign in to comment.