tylermorganwall / rayshader

R Package for 2D and 3D mapping and data visualization
https://www.rayshader.com/
2.05k stars 211 forks source link

render_label with lat long not working #271

Open cvpalms opened 1 year ago

cvpalms commented 1 year ago

``Describe the bug After creating a 3d plot using data in a non lat-long based CRS, I cannot get the render_label function to place labels when using lat/long as inputs for the label location, along with the extent of the input dataset. I have tried several variations for the zscale and altitude values just in case the label was appearing way above or below the rendered scene, but no luck there. I was assuming that rander_label function will translate the lat/long values to the native CRS of the input raster or vector GIS layer

Session Info attached base packages: [1] stats graphics grDevices utils datasets methods base

other attached packages: [1] rgl_1.0.1 colorspace_2.1-0 MetBrewer_0.2.0 fasterize_1.0.4 sf_1.0-9 rayshader_0.34.8
[7] elevatr_0.4.3.9999 raster_3.6-14 sp_1.6-0

loaded via a namespace (and not attached): [1] progress_1.2.2 tidyselect_1.2.0 terra_1.7-3 xfun_0.37 purrr_1.0.1 lattice_0.20-45
[7] vctrs_0.5.2 generics_0.1.3 htmltools_0.5.4 base64enc_0.1-3 utf8_1.2.3 rlang_1.0.6
[13] e1071_1.7-13 pillar_1.8.1 glue_1.6.2 DBI_1.1.3 foreach_1.5.2 lifecycle_1.0.3
[19] munsell_0.5.0 gtable_0.3.1 progressr_0.13.0 htmlwidgets_1.6.1 codetools_0.2-18 knitr_1.42
[25] fastmap_1.1.0 doParallel_1.0.17 parallel_4.2.2 class_7.3-20 fansi_1.0.4 Rcpp_1.0.10
[31] KernSmooth_2.23-20 scales_1.2.1 classInt_0.4-8 magick_2.7.3 jsonlite_1.8.4 ggplot2_3.4.1
[37] hms_1.1.2 png_0.1-8 digest_0.6.31 dplyr_1.1.0 grid_4.2.2 rgdal_1.6-4
[43] cli_3.6.0 tools_4.2.2 magrittr_2.0.3 proxy_0.4-27 tibble_3.1.8 crayon_1.5.2
[49] pkgconfig_2.0.3 ellipsis_0.3.2 prettyunits_1.1.1 rstudioapi_0.14 iterators_1.0.14 R6_2.5.1
[55] units_0.8-1 compiler_4.2.2

Reproducible Example library(rayshader) library(sf) library(raster) library(fasterize) library(MetBrewer) library(colorspace) library(rgl)

Step 1: Download the geopackage GIS layer used as input to the test script

#URL: https://data.humdata.org/dataset/kontur-population-switzerland
#Unzip the geopackage file, and place it in the folder named "data" in your R project
# For context, these are hexagons with population counts at 400 meter resolution for all of Switzerland

Step 2: Read in a GIS layer of polygons with population counts

Note that this geopackage layer is in coordinate system 3857 (WGS 84 Pseudo-Mercator where the units are meters instead of lat/long degrees)

in_sf = read_sf("data/kontur_population_CH_20220630.gpkg")

Step 3: Convert the GIS layer to a raster, with a cell size of 400 meters

r = raster(in_sf, res = 400) rast <- fasterize(in_sf, r, field = "population", fun="max")

Step 4: Convert the raster to a matrix for 3d plot

elmat = raster_to_matrix(rast)

Step 5: Setup colorramp for raster rendering by population

colors = met.brewer("OKeeffe2") texture = grDevices::colorRampPalette(colors)(256) swatchplot(texture)

Step 6: Render the 3d scene

Step 6a: Close RGL window if it's already open

if (length(rgl.dev.list()) > 0) { rgl.close() }

Step 6b: Create 3d Plot

elmat |> height_shade(texture) |> plot_3d(heightmap = elmat ,zscale = 400 #Each cell in the raster is 400 meters on each side )

Below is the step where the issue occurs. The label will not render. It should appear floating over the dark red patch on the far left side of the scene

Question: Does the render_label function support scenarios where lat/long values are provided for the label location,

#but the extent input has a CRS that is meter based instead of lat/long based?

Step 7: Render a label for Geneva using lat long. Raster used for extent input uses CRS 3857 (meters)

render_label(elmat, text = "Geneva", lat = 46.204391, long = 6.143158, extent = rast, zscale = 400, altitude = 15000, clear_previous = TRUE)