tylermorganwall / rayshader

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

render_multipolygonz renders fuzzy and wrinkled buildings [BUG] #283

Closed milos-agathon closed 1 year ago

milos-agathon commented 1 year ago

Describe the bug Hi Tyler, when I run rayshader::render_multipolygonz the buildings in the scene end up wrinkled and fuzzy (see the image attached). Could you, please, help me iron them?

Session Info R version 4.1.0 (2021-05-18) Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows 10 x64 (build 22621)

Matrix products: default

locale: [1] LC_COLLATE=English_Netherlands.1252 LC_CTYPE=English_Netherlands.1252 [3] LC_MONETARY=English_Netherlands.1252 LC_NUMERIC=C [5] LC_TIME=English_Netherlands.1252

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

other attached packages: [1] raster_3.6-14 sp_1.6-0 rayshader_0.35.7 elevatr_0.4.3.9999 [5] rayrender_0.29.6 sf_1.0-12 forcats_0.5.1 stringr_1.5.0 [9] dplyr_1.0.7 purrr_0.3.4 readr_2.0.0 tidyr_1.1.3 [13] tibble_3.1.7 ggplot2_3.3.5 tidyverse_1.3.1

loaded via a namespace (and not attached): [1] httr_1.4.2 jsonlite_1.8.0 foreach_1.5.2 showtext_0.9-5 [5] modelr_0.1.8 assertthat_0.2.1 showtextdb_3.0 cellranger_1.1.0 [9] progress_1.2.2 Rttf2pt1_1.3.9 pillar_1.9.0 backports_1.2.1 [13] lattice_0.20-44 glue_1.6.2 extrafontdb_1.0 digest_0.6.29
[17] rvest_1.0.1 colorspace_2.0-3 htmltools_0.5.4 slippymath_0.3.1 [21] pkgconfig_2.0.3 broom_0.7.8 magick_2.7.3 haven_2.4.1 [25] sysfonts_0.8.8 scales_1.2.1 terra_1.6-53 tzdb_0.1.2 [29] proxy_0.4-26 generics_0.1.0 ellipsis_0.3.2 withr_2.5.0 [33] cli_3.2.0 magrittr_2.0.3 crayon_1.5.2 readxl_1.3.1 [37] fs_1.5.0 fansi_1.0.3 doParallel_1.0.17 xml2_1.3.2 [41] class_7.3-19 progressr_0.9.0 tools_4.1.0 prettyunits_1.1.1 [45] hms_1.1.2 lifecycle_1.0.3 munsell_0.5.0 reprex_2.0.0 [49] compiler_4.1.0 e1071_1.7-9 rlang_1.0.6 classInt_0.4-3
[53] units_0.8-0 grid_4.1.0 iterators_1.0.14 rstudioapi_0.13 [57] htmlwidgets_1.6.1 base64enc_0.1-3 gtable_0.3.1 codetools_0.2-18 [61] curl_4.3.2 DBI_1.1.2 rayimage_0.9.1 R6_2.5.1 [65] lubridate_1.7.10 rgdal_1.5-23 knitr_1.41 fastmap_1.1.0 [69] extrafont_0.17 utf8_1.2.2 KernSmooth_2.23-20 stringi_1.7.6 [73] parallel_4.1.0 Rcpp_1.0.8.3 png_0.1-8 vctrs_0.5.1 [77] rgl_1.1.19 rayvertex_0.4.16 dbplyr_2.1.1 tidyselect_1.2.0 [81] xfun_0.36

Reproducible Example

# Load all the libraries needed
# define libraries we need
libs <- c(
    "tidyverse", "sf",
    "rayrender", "elevatr",
    "rayshader", "raster"
)

# install missing libraries
installed_libraries <- libs %in% rownames(installed.packages())
if (any(installed_libraries == F)) {
    installed.packages(libs[!installed_libraries])
}

# load libraries
invisible(lapply(libs, library, character.only = T))

# Use legacy sf behavior
sf::sf_use_s2(F)

# Load the 3D The Hague dataset (from https://data.3dbag.nl/gpkg/v20230622/tiles/10/270/592/10-270-592.gpkg)
buildings <- sf::st_read(
    "https://data.3dbag.nl/gpkg/v20230622/tiles/10/270/592/10-270-592.gpkg",
    layer = "lod22_3d" # the most detailed layer; other layers: lod12_3d, lod13_3d
)

sf::st_geometry(buildings) <- "geometry"

# bounding box polygon
scene_bbox_poly <- sf::st_bbox(buildings) |>
    sf::st_as_sfc()

# elevation data from the bounding box
elevation_data <- elevatr::get_elev_raster(
    locations = scene_bbox_poly,
    clip = "bbox",
    z = 14
)

# Bounding box for the scene
scene_bbox <- sf::st_bbox(buildings)

# Crop the elevation data to that bounding box
cropped_data <- raster::crop(
    elevation_data, scene_bbox
)

# Raster data to a matrix
elevation_matrix <- rayshader::raster_to_matrix(
    cropped_data
)

# Remove negative elevation data
elevation_matrix[elevation_matrix < 0] <- 0

# Turn the elevation data into a 3D surface and plot using rayshader
elevation_matrix |>
    rayshader::height_shade() |>
    rayshader::add_shadow(
        rayshader::lamb_shade(
            elevation_matrix
        ),
        0
    ) |>
    rayshader::plot_3d(elevation_matrix,
        solid = F, zscale = 3,
        zoom = .85, phi = 45,
        theta = -30,
        windowsize = 800
    )

# Render the buildings

rgl::par3d(ignoreExtent = T)

rayshader::render_multipolygonz(
    buildings,
    extent = raster::extent(
        cropped_data
    ),
    obj_zscale = T,
    zscale = 2,
    color = "grey80",
    heightmap = elevation_matrix
)

rayshader::render_snapshot(
    filename = "binnenhof.png"
)

binnenhof

tylermorganwall commented 1 year ago

When I run this, I don't see an issue--I've made many updates recently so whatever caused this may have been fixed.

Try it again after updating all the rayverse packages:

install.packages('rayshader', repos = c('https://tylermorganwall.r-universe.dev', 'https://cloud.r-project.org'))
milos-agathon commented 1 year ago

Thanks! The update solve the issue