tylermorganwall / tree3d

https://tylermorganwall.github.io/tree3d/
Other
20 stars 1 forks source link

Example from README errors when trying to render buildings #2

Closed elipousson closed 3 months ago

elipousson commented 3 months ago

It is surely possible that this is an issue with my setup instead of with the package but, just in case, I wanted to report the potential issue.

Here is a reprex with the code copied directly from the first two code blocks in the README:

library(tree3d)
library(sf)
#> Linking to GEOS 3.11.0, GDAL 3.5.3, PROJ 9.1.0; sf_use_s2() is TRUE
library(terra)
#> terra 1.7.78
library(rayshader)
#> 
#> Attaching package: 'rayshader'
#> The following object is masked from 'package:tree3d':
#> 
#>     run_documentation

# Download the sample data
temp_download = tempfile(fileext = ".zip")
download.file("https://github.com/tylermorganwall/tree3d/raw/main/inst/extdata/tree3d_sandbox_data.zip", temp_download)

# Unzip and get directory
unzip(temp_download, exdir = tempdir())
zipdir = paste0(tempdir(), .Platform$file.sep, 
                "sandbox_data", .Platform$file.sep)

# Load all files
poly_boundary = sf::st_read(paste0(zipdir, "Area_Boundary_subset.shp"), quiet = TRUE)
baseline_tree = sf::st_read(paste0(zipdir, "Baseline_Trees_Z_clip.shp"), quiet = TRUE)
building_baseline = sf::st_read(paste0(zipdir, "Buildings_Baseline_2020_Z_clip.shp"), quiet = TRUE)
parcels = sf::st_read(paste0(zipdir, "Parcels_2020_clip.shp"), quiet = TRUE)

dem = terra::rast(paste0(zipdir, "DEM_2013_subset_zero.tif"))

# Inspect the tree data
head(baseline_tree)
#> Simple feature collection with 6 features and 8 fields
#> Geometry type: POINT
#> Dimension:     XYZ
#> Bounding box:  xmin: 493801.7 ymin: 5452079 xmax: 493951.7 ymax: 5452181
#> z_range:       zmin: 7.19 zmax: 16.25
#> Projected CRS: NAD83 / UTM zone 10N
#>             Species Height      Type Location MatureSize Diameter  CrownType
#> 1 Prunus cerasifera   7.19 Deciduous   Street      Small      4.6 spreading1
#> 2  Acer x freemanii  16.25 Deciduous   Street      Large     12.1       oval
#> 3  Acer x freemanii  16.22 Deciduous   Street      Large      7.5       oval
#> 4    Fraxinus ornus   8.09 Deciduous   Street     Medium      4.1       oval
#> 5  Parrotia persica   9.20 Deciduous   Street     Medium      4.1 spreading1
#> 6       Sorbus aria   9.67 Deciduous   Street     Medium      5.3       oval
#>   Color                       geometry
#> 1  <NA> POINT Z (493937.4 5452179 7...
#> 2  <NA> POINT Z (493951.7 5452079 1...
#> 3  <NA> POINT Z (493831.7 5452181 1...
#> 4  <NA> POINT Z (493801.7 5452167 8...
#> 5  <NA> POINT Z (493845.4 5452181 9.2)
#> 6  <NA> POINT Z (493898.5 5452165 9...

#Use rayshader to convert the DEM to a matrix and generate a 3D mesh
dem_mat = raster_to_matrix(dem)
dem_mat |>
  constant_shade("#6dbd83") |>
  add_overlay(generate_polygon_overlay(poly_boundary, extent = ext(dem),
                                       heightmap = dem_mat,
                                       palette = "#bcebc9")) |>
  add_overlay(generate_polygon_overlay(parcels, extent = ext(dem),
                                       heightmap = dem_mat,
                                       palette = "#afe0bd")) |>
  plot_3d(dem_mat, background = "lightblue", windowsize = 800)

#Render the buildings as extruded polygons
render_polygons(building_baseline, 
                extent = ext(dem), 
                data_column_top = "height_ag", 
                clear_previous = TRUE, 
                color = "white", 
                heightmap = dem_mat)
#> Error in t.default(mesh$vertices): argument is not a matrix
render_camera(theta = 44, phi = 36, zoom = 0.7, fov = 50, shift_vertical = -30)
render_snapshot()

Created on 2024-06-18 with reprex v2.1.0

tylermorganwall commented 3 months ago

You just need to update to the latest version of rayshader (this was fixed in this following commit https://github.com/tylermorganwall/rayshader/commit/53498da030290b6e16368fc1c8c605e3b5eb5de5):

install.packages('rayshader', repos = c('https://tylermorganwall.r-universe.dev', 'https://cloud.r-project.org'))