tylermorganwall / rayshader

R Package for 2D and 3D mapping and data visualization

Home Page:https://www.rayshader.com/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

render_multipolygonz error

SbastianGarzon opened this issue · comments

Hello,
I'm getting the following error while using the render_multipolygonz function with the 3D BAG-TU Delft models (https://3dbag.nl/en/).
I managed to reproduce your example, but it is not working using other datasets.

Here is a reproducible error:
(You need to download first the 3dbag tile:https://3dbag.nl/en/download?tid=5907)

#Use legacy sf behavior
library(sf)
library(dplyr)
library(rayrender)
library(elevatr)
library(rayshader)
library(geojsonsf)
library(raster)
`
sf_use_s2(FALSE)

#Load the 3D tudelfted (from : https://3dbag.nl/en/download?tid=5907)
st_read("3dbag_v210908_fd2cee53_5907.gpkg") -> delft_buildings

# Important building
tu_delft_architecture =  c(4.37027,52.00583)

# Transform coordinates to coordinate reference system 

tu_delft_architecture |> 
  st_point() |> 
  st_sfc(crs = 4326) |> 
  st_transform(st_crs(delft_buildings)) ->
  tu_d_point

# From Polygon to MULTIPOLYGON Z

delft_buildings |> 
  st_cast("MULTIPOLYGON")|> 
  st_zm(drop=FALSE,what = "Z") ->
  tudelft_campus

# Define centroid

centroid <- st_centroid(st_union(tudelft_campus))

# Download raster

elevation_data = elevatr::get_elev_raster(locations = st_buffer(tu_d_point, 
                                                                dist=units::as_units(500,"m")),z=14)

#Get the bounding box for the scene
scene_bbox = st_bbox(tudelft_campus)

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

# Raster to matrix
tud_elevation_matrix = raster_to_matrix(cropped_data)

# Render !

tud_elevation_matrix %>% sphere_shade(sunangle = 45, texture = "desert") %>%
  add_shadow(ray_shade(tud_elevation_matrix, zscale = 1), 0.5) %>%
  add_shadow(ambient_shade(tud_elevation_matrix), 0) %>% plot_3d(tud_elevation_matrix,windowsize=c(1200,800))

render_multipolygonz(tudelft_campus,
                     extent = extent(cropped_data),
                     clear_previous = TRUE, zscale = 4,
                     color = "grey80",
                     heightmap = tud_elevation_matrix)

Result:

Error in rep(0, nrow(mesh$shapes[[1]]$indices)) : invalid 'times' argument
Traceback:

3. rayvertex::set_material(tempobj, diffuse = color[k])
2. render_obj(filename = obj_temp, extent = extent, obj_zscale = obj_zscale, clear_previous = FALSE, zscale = zscale, color = color, offset = offset, swap_yz = swap_yz, heightmap = heightmap, baseshape = baseshape, rgl_tag = rgl_tag, ...)
1. render_multipolygonz(tudelft_campus, extent = extent(cropped_data), clear_previous = TRUE, zscale = 4, color = "grey80", heightmap = tud_elevation_matrix)