hypertidy/ceramic

wrap for ggplot2 - RGB example

mdsumner opened this issue · 4 comments

library(ceramic)
## a point in longlat, and a buffer in metres
pt <- cbind(136, -34)
im <- cc_location(pt, buffer = c(1e6, 5e5), type = "mapbox.satellite")
#im <- aggregate(im,fact = 24, method = "ngb")
library(ggplot2)
library(raster)
#> Loading required package: sp
tab <- as.data.frame(im, xy = TRUE)
names(tab) <- c("x", "y", "red", "green", "blue")
tab$hex <- rgb(tab$red, tab$green, tab$blue, maxColorValue = 255)
ggplot(tab, aes(x, y, fill = hex)) + geom_raster() + coord_equal()  +   scale_fill_identity()

Created on 2019-02-16 by the reprex package (v0.2.1)

And now with reprojection, and dropping missing values

library(ceramic)
## a point in longlat, and a buffer in metres
pt <- cbind(136, -34)
im <- cc_location(pt, buffer = c(1e6, 5e5), type = "mapbox.satellite")
library(ggplot2)
library(raster)
#> Loading required package: sp

## when we reproject, we'll get missing values (probably so later ...)
im <- projectRaster(im, crs = "+proj=laea +lat_0=0 +lon_0=100", method = "ngb")  ## nearest neighbour, not bilinear
tab <- as.data.frame(im, xy = TRUE)
names(tab) <- c("x", "y", "red", "green", "blue")
tab <- dplyr::filter(tab, !is.na(red))

## ... when we have missing values, we should drop them or rgb() will error
tab$hex <- rgb(tab$red, tab$green, tab$blue, maxColorValue = 255)

ggplot(tab, aes(x, y, fill = hex)) + geom_raster() + coord_equal() + scale_fill_identity()

Created on 2019-02-16 by the reprex package (v0.2.1)

DC https://twitter.com/shermstats/status/1096613148184985600

library(ceramic) # remotes::install_github('hypertidy/ceramic')


## -77.041798, 38.895435, -77.032022, 38.901376
ex <- c(xmin = -77.041798, xmax = -77.032022, ymin = 38.895435, ymax = 38.901376 )
span <- geosphere::distGeo(ex[c(1, 3)], ex[c(2, 4)])
## a point in longlat, and a buffer in metres
im <- cc_location(cbind(mean(ex[1:2]), mean(ex[3:4])), buffer = span/2)
library(ggplot2)
library(raster)
#> Loading required package: sp

tab <- as.data.frame(im, xy = TRUE)
names(tab) <- c("x", "y", "red", "green", "blue")
tab <- dplyr::filter(tab, !is.na(red))
## ... when we have missing values, we should drop them or rgb() will error
tab$hex <- rgb(tab$red, tab$green, tab$blue, maxColorValue = 255)

ggplot(tab, aes(x, y, fill = I(hex))) + geom_raster() + coord_equal()

Created on 2019-02-16 by the reprex package (v0.2.1)

In terms of a task, make this an option to read tiles and return what geom_raster needs

See plusraster for a easy wrapper for this using annotation, anglr for the general issue of plotting raster meshes with grid vectorization