Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save alfcrisci/8a43b73bf73d9d965fa7c3e801ac80ac to your computer and use it in GitHub Desktop.
Save alfcrisci/8a43b73bf73d9d965fa7c3e801ac80ac to your computer and use it in GitHub Desktop.
R script to visualize GeoTIFFs in a Leaflet Map. Generates an HTML file containing this map
# dependencies on fedora23:
# gdal-devel geos-devel proj-devel proj-nad proj-epsg
library(htmlwidgets)
library(raster)
library(leaflet)
# PATHS TO INPUT / OUTPUT FILES
projectPath = "/home/kreis/git/geotiff/"
#imgPath = paste(projectPath,"data/cea.tif", sep = "")
#imgPath = paste(projectPath,"data/o41078a1.tif", sep = "") # bigger than standard max size (15431804 bytes is greater than maximum 4194304 bytes)
imgPath = paste(projectPath,"data/SP27GTIF.TIF", sep = "")
outPath = paste(projectPath, "leaflethtmlgen.html", sep="")
# load raster image file
r <- raster(imgPath)
# reproject the image, if necessary
#crs(r) <- sp::CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs")
# color palette, which is interpolated ?
pal <- colorNumeric(c("#000000", "#666666", "#FFFFFF"), values(r),
na.color = "transparent")
# create the leaflet widget
m <- leaflet() %>%
addTiles() %>%
addRasterImage(r, colors=pal, opacity = 0.9, maxBytes = 123123123) %>%
addLegend(pal = pal, values = values(r), title = "Test")
# save the generated widget to html
# contains the leaflet widget AND the image.
saveWidget(m, file = outPath, selfcontained = FALSE, libdir = 'leafletwidget_libs')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment