Skip to content

Instantly share code, notes, and snippets.

@h-a-graham
Created October 15, 2021 09:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save h-a-graham/4ef9673ced676936a4eea311247e3107 to your computer and use it in GitHub Desktop.
Save h-a-graham/4ef9673ced676936a4eea311247e3107 to your computer and use it in GitHub Desktop.
#anglr questions
# example...
library(gdalio)
library(rayshader)
library(topography)
library(gdalwebsrv)
library(anglr)
source(system.file("raster_format/raster_format.codeR", package = "gdalio", mustWork = TRUE))
#functions...
rotate_ <- function(x) t(apply(x, 2, rev))
gdalio_rayshader_matrix <- function(dsn, resample='CubicSpline', ...) {
v <- gdalio_data(dsn, resample=resample, ...)
g <- gdalio_get_default_grid()
m <- matrix(v[[1]], g$dimension[1])[,g$dimension[2]:1, drop = F]
rotate_(rotate_(m))|>
apply(2,rev)
}
gdalio_rayshader_array <- function(dsn, alpha=1,...) {
v <- gdalio_data(dsn, ...)
g <- gdalio_get_default_grid()
matrix_thing <- function(.v){
m <- matrix(as.numeric(.v), g$dimension[1])#[,g$dimension[2]:1, drop = F]
rotate_(m)
}
v2 <- lapply(v, matrix_thing)
a <- matrix(NA, g$dimension[2],g$dimension[1])
aa <- array(c(unlist(v2, use.names = FALSE), a), c(g$dimension[2], g$dimension[1], 4))[,g$dimension[1]:1, , drop = FALSE]
aa <- aa%>%
scales::rescale(.,to=c(0,1))
aa[,,4] <- alpha
return(aa)
}
# Various gdalio grid setting options...
gdalio_set_default_grid(list(extent=c(-13608804, -13594804, 5805575, 5819575 ),
dimension=c(1400, 1400),
projection=c("+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +wktext +no_defs")))
# Get the data
topo_mat <- gdalio_rayshader_matrix(topography_source('aws'))
drape_array <- gdalio_rayshader_array(server_file("wms_virtualearth"),
alpha=0.7, bands=1:3)
# Enhance texture with rayshader
texture <- topo_mat %>%
sphere_shade(texture='imhof4')%>%
add_overlay(., drape_array,rescale_original=T) %>%
add_shadow(ray_shade(topo_mat, zscale=5, sunangle=165, multicore = T), 0.1)
# plot with rayshader to see what we're going for...
plot_3d(texture, topo_mat, zscale = 5)
# anglr works nice for the matrix...
.mesh1 <- anglr::as.mesh3d(topo_mat)
anglr::plot3d(.mesh1)
#### QUESTION... What is the right way to map texture using anglr?
# here I attempt to use an RGB raster and a png file... neither work...
texture_rgb <- gdalio_raster(gdalwebsrv::server_file('wms_virtualearth'),
band_output_type='Int32', bands=1:3)
# raster::plotRGB(texture_rgb) # works nice...
temp_tex <- tempfile(fileext = '.png')
png::writePNG(texture, 'dev_tests/img.png') # also works nicely. but numbers not 0-255 - rescaling screws things up and still won't plot with anglr.
# What's the write approach for building the mesh with texture?
.mesh2 <- anglr::as.mesh3d(topo_mat, image_texture=texture_rgb) # ERROR
.mesh3 <- anglr::as.mesh3d(topo_mat, image_texture=temp_tex)
# anglr::plot3d(.mesh2)
@mdsumner
Copy link

here's the answer, you need to use raster objects in both cases, else there's no reference between the matrix and the image - there are many ways to do this, and we could harvest the metadata that rayshader keeps as attributes, or allow the input matrix to share the textures metadata etc. etc. - but, it's all written so you can have a raster elevation and a raster image, in whatever projection they are in, and the textures are mapped, so modifying your code:

topo_raster <- gdalio_raster(topography_source('aws'))

texture_rgb <- gdalio_raster(gdalwebsrv::server_file('wms_virtualearth'),
                             band_output_type='Int32', bands=1:3)

.mesh2 <- anglr::as.mesh3d(topo_raster, image_texture=texture_rgb) 
anglr::plot3d(.mesh2)

@mdsumner
Copy link

mdsumner commented Oct 15, 2021

full example

#anglr questions
# example...
library(gdalio)
library(rayshader)
library(topography)
library(gdalwebsrv)
library(anglr)
source(system.file("raster_format/raster_format.codeR", package = "gdalio", mustWork = TRUE))


# Various gdalio grid setting options...
gdalio_set_default_grid(list(extent=c(-13608804, -13594804,   5805575,   5819575 ),
                             dimension=c(1400, 1400) * 2,
                             projection=c("+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +wktext  +no_defs")))

topo_raster <- gdalio_raster(topography_source('aws'), resample = "bilinear")

texture_rgb <- gdalio_raster(gdalwebsrv::server_file('wms_virtualearth'),
                             band_output_type='Int32', bands=1:3)

.mesh2 <- anglr::as.mesh3d(topo_raster, image_texture=texture_rgb) 
anglr::plot3d(.mesh2)
rgl::aspect3d(1, 1, .2)

(note resample in AWS source, looks better)

image

@h-a-graham
Copy link
Author

That's great, thank you so much!

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