mapboxer icon indicating copy to clipboard operation
mapboxer copied to clipboard

How to supply a Mapbox source with building heights?

Open asadow opened this issue 9 months ago • 0 comments

I'm trying to mimic the Mapbox 3d buildings example in R but am having trouble.

What is wrong with my source below?

library(osmdata)
library(mapboxer)
library(tidyverse)

q <- opq(bbox = c(-80.24021, 43.52522, -80.21911, 43.54))
osmdata_uog <- q |>
  add_osm_feature(key = "building") |>
  osmdata_sf()

uog_polygons <- osmdata_uog$osm_polygons |> 
  mutate(name = str_to_upper(name))

my_source <- uog_polygons |> 
  mutate(height = 300) |> 
  as_mapbox_source()

my_style <- list(
  'id' = 'add-3d-buildings',
  'source' = my_source,
  # 'source-layer' = 'building',
  'filter' = list('==', 'extrude', 'true'),
  'type' = 'fill-extrusion',
  'minzoom' = 15,
  'paint' = list(
    'fill-extrusion-color' = '#aaa',
    
    # // Use an 'interpolate' expression to
    # // add a smooth transition effect to
    # // the buildings as the user zooms in.
    'fill-extrusion-height' = list(
      'interpolate',
      list('linear'),
      list('zoom'),
      15,
      0,
      15.05,
      list('get', 'height')
    ),
    ## min_height 0 can be added to source
    # 'fill-extrusion-base' = list(
    #   'interpolate',
    #   list('linear'),
    #   list('zoom'),
    #   15,
    #   0,
    #   15.05,
    #   list('get', 'min_height')
    # ),
    'fill-extrusion-opacity' = 0.6
  )
)

map <- mapboxer(
  center = c(-80.22655754542174, 43.53204413393032),
  zoom = 15,
  pitch = 45,
  bearing = -17.6,
  container = 'map',
  antialias = TRUE
) %>%
  add_layer(my_style)

if (interactive()) map

asadow avatar May 14 '24 18:05 asadow