Skip to content

Instantly share code, notes, and snippets.

@benfasoli
Last active February 22, 2021 22:36
Show Gist options
  • Save benfasoli/b24caa9aee8c78593dd337412fcdba21 to your computer and use it in GitHub Desktop.
Save benfasoli/b24caa9aee8c78593dd337412fcdba21 to your computer and use it in GitHub Desktop.
Produce interactive visualization of time integrated footprint
#!/usr/bin/env Rscript
library(htmlwidgets)
library(leaflet)
library(raster)
library(viridis)
path <- 'example-footprint.nc'
footprint <- brick(path)
footprint
# class : RasterBrick
# dimensions : 300, 300, 90000, 6 (nrow, ncol, ncell, nlayers)
# resolution : 0.01, 0.01 (x, y)
# extent : -114, -111, 39, 42 (xmin, xmax, ymin, ymax)
# crs : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0
# source : /Users/benfasoli/Desktop/stilt/example-footprint.nc
# names : X1449684000, X1449687600, X1449691200, X1449694800, X1449698400, X1449702000
# z-value : 1449684000, 1449687600, 1449691200, 1449694800, 1449698400, 1449702000
# varname : foot
footprint_timeint <- sum(footprint)
# Set zero footprint values to NA to hide from displaying
mask <- footprint_timeint == 0
footprint_timeint[mask] <- NA
# Uncomment to display entire domain
# footprint_timeint[mask] <- quantile(values(footprint_timeint), 0.01, na.rm = T)
# Log10 transform for visualizations
footprint_timeint_log10 <- log10(footprint_timeint)
footprint_timeint_log10
# class : RasterLayer
# dimensions : 46, 60, 2760 (nrow, ncol, ncell)
# resolution : 0.3, 0.3 (x, y)
# extent : -123, -105, 33, 46.8 (xmin, xmax, ymin, ymax)
# crs : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0
# source : memory
# names : layer
# values : -8.396288, 0.1152478 (min, max)
# Leaflet legends default to what would normally be considered upside-down,
# where smallest value is at top and largest at bottom. Reverse the order and
# add back in the negative sign in the addLegend() call
footprint_timeint_log10 <- -footprint_timeint_log10
colors <- colorNumeric(viridis::viridis(256),
domain = values(footprint_timeint_log10),
na.color = '#00000000',
reverse = T)
map <- leaflet() %>%
addProviderTiles('CartoDB.Positron') %>%
addRasterImage(footprint_timeint_log10, opacity = 0.5, colors = colors) %>%
addLegend(position = 'bottomright',
pal = colors,
values = values(footprint_timeint_log10),
title = 'log10',
labFormat = labelFormat(prefix = '-'))
saveWidget(map, 'example-map.html')
map
@benfasoli
Copy link
Author

_example-map

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment