R语言leafem包,addGeotiff函数,将GeoTIFF文件直接叠加到活页地图

R语言leafem包,addGeotiff函数,将GeoTIFF文件直接叠加到活页地图


001
# Wed Nov 04 00:57:58 2020 -

# 字符编码:UTF-8
# R 版本:R x64 4.0.3 for window 10
# cgh163email@163.com
# 个人笔记不负责任
# —— 拎了个梨🍐
.rs.restartR()
# addGeotiff函数,将GeoTIFF文件直接叠加到活页地图
require(leafem)
rm(list=ls());gc()

# install.packages('htmlwidgets')
if (interactive()) {
  library(leaflet)
  library(leafem)
  library(stars)

  tif = system.file("tif/L7_ETMs.tif", package = "stars")
  x1 = read_stars(tif)
  x1 = x1[, , , 3] # band 3

  tmpfl = tempfile(fileext = ".tif")

  write_stars(st_warp(x1, crs = 4326), tmpfl)

  myCustomJSFunc = htmlwidgets::JS(
    "
      pixelValuesToColorFn = (raster, colorOptions) => {
        const cols = colorOptions.palette;
        var scale = chroma.scale(cols);

        if (colorOptions.breaks !== null) {
          scale = scale.classes(colorOptions.breaks);
        }
        var pixelFunc = values => {
          let clr = scale.domain([raster.mins, raster.maxs]);
          if (isNaN(values)) return colorOptions.naColor;
          return clr(values).hex();
        };
        return pixelFunc;
      };
    "
  )

  leaflet() %>%
    addTiles() %>%
    addGeotiff(
      file = tmpfl
      , opacity = 0.9
      , colorOptions = colorOptions(
        palette = grey.colors
        , na.color = "transparent"
      )
      , pixelValuesToColorFn = myCustomJSFunc
    )

}


  # Wed Nov 04 01:03:17 2020 --diy----------------------------
# mygeotif <- '001.tif'
'https://gitee.com/youmigo/bate/blob/master/img.file/GAIA_1985_2018_70_030.tif' %>%
  download.file(destfile = '001.tif')


  myCustomJSFunc = htmlwidgets::JS(
    "
      pixelValuesToColorFn = (raster, colorOptions) => {
        const cols = colorOptions.palette;
        var scale = chroma.scale(cols);

        if (colorOptions.breaks !== null) {
          scale = scale.classes(colorOptions.breaks);
        }
        var pixelFunc = values => {
          let clr = scale.domain([raster.mins, raster.maxs]);
          if (isNaN(values)) return colorOptions.naColor;
          return clr(values).hex();
        };
        return pixelFunc;
      };
    "
  )

  leaflet() %>%
    addTiles() %>%
    addGeotiff(
      file = mygeotif
      , opacity = 0.6 #  透明度
      , colorOptions = colorOptions(
        palette = grey.colors
        , na.color = "transparent"
      )
      , pixelValuesToColorFn = myCustomJSFunc
    )
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。