Skip to content

Instantly share code, notes, and snippets.

@mdsumner
Created June 25, 2015 06:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mdsumner/a3d2277bae7cd8082302 to your computer and use it in GitHub Desktop.
Save mdsumner/a3d2277bae7cd8082302 to your computer and use it in GitHub Desktop.
Heatmap on a globe Lesson #1
A recreation of http://gis.stackexchange.com/questions/152094/heatmap-on-globe-distortion-problems
```{r}
library(raster)
library(geosphere)
library(MASS)
r <- raster(extent(-180, 180, -90, 90), nrow = 256, ncol = 256)
nn <- 300
#x <- randomCoordinates(nn)
x <- cbind(runif(nn, -180, 180), runif(nn, -90, 90))
kde <- raster(kde2d(x[,1], x[,2], h = c(bandwidth.nrd(x[,1]), bandwidth.nrd(x[,2]))/5, n = nrow(r), lims = c(xmin(r), xmax(r), ymin(r), ymax(r))))
library(rgl)
library(rglgris) ## devtools::install_github("mdsumner/rglgris")
## build a quadmesh from raster
b <- bgl(r)
## sphere
b$vb[1:3,] <- t(llh2xyz(t(b$vb[1:3,])))
scl <- function(x) (x - min(x))/diff(range(x))
shade3d(b, col = rep(terrain.colors(25)[scl(values(kde)) * 24 + 1], each = 4))
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment