## a coarse stab at using triangulations to provide image textures with sf
library(sf)
## Linking to GEOS 3.5.0, GDAL 2.1.1, proj.4 4.9.3
u <- "https://jeroenooms.github.io/images/frink.png"
f <- basename(u)
if (!file.exists(f)) download.file(u, f, mode = "wb")
library(raster)
## Loading required package: sp
r <- brick(f)
## devtools::install_github("mdsumner/sfdct")
library(sfdct)
data("antarctica", package = "sfdct")
## at minimum a(rea) = 1e10 it's a bit too coarse ..
tri <- ct_triangulate(antarctica[1L, ], a = 1e9, D = TRUE)
## explode to individual triangles
gtri <- st_set_crs(do.call(st_sfc, lapply(st_geometry(tri), function(x) unclass(x))), st_crs(tri))
xypoints <- do.call(rbind, lapply(gtri, function(tr) apply(tr[[1]], 2, mean)))
extent(r) <- extent(xypoints)
idx <- cellFromXY(r, xypoints)
## plot the two data sets to ensure they do overlap
plotRGB(r)
plot(antarctica[1, ], add = T)

## extract the raw image values per geometric element
## and plot those
vals <- extract(r, idx)
vals[is.na(vals)] <- 0
cols <- rgb(vals[, 1], vals[, 2], vals[, 3], maxColorValue = 255)
plot(gtri, col = cols, border = NA)
