walkerke / mapgl

R interface to Mapbox GL JS v3 and Maplibre GL JS
https://walker-data.com/mapgl
Other
91 stars 5 forks source link

compare does not inherit bounds argument #49

Closed atsyplenkov closed 1 month ago

atsyplenkov commented 1 month ago

Hi,

When one creates the compare slider widget with two maps that were created with the bounds argument, the compare widget ignores the bounds and drops center and zoom to default values. See the example below.

How it works now

library(mapgl)
library(sf)

# Load example dataset
nc <-
  sf::st_read(system.file("shape/nc.shp", package = "sf"))[1, ] |>
  sf::st_transform(crs = 4326)

# Option 1 — Widget bbox set up with bounds argument ---------------------
# Create map 1
m1 <-
  maplibre(bound = nc) |> # bbox is setup with bounds
    add_fill_layer(
  id = "nc_data",
  source = nc,
  fill_color = "blue",
  fill_opacity = 0.5
)

# Create map 2
m2 <-
  maplibre(bound = nc) |> # bbox is setup with bounds
  add_fill_layer(
    id = "nc_data",
    source = nc,
    fill_color = "red",
    fill_opacity = 0.5
  )

# The bound argument of maplibregl object is ignored
compare(m1, m2)

The workaround

# Option 2 — Widget bbox set up with center and zoom arguments -----------------
nc_centroid <-
  sf::st_centroid(nc) |>
  sf::st_coordinates()

m1 <-
  maplibre(
    center = c(nc_centroid[1], nc_centroid[2]),
    zoom = 10
  ) |>
  add_fill_layer(
    id = "nc_data",
    source = nc,
    fill_color = "blue",
    fill_opacity = 0.5
  )

m2 <-
  maplibre(
    center = c(nc_centroid[1], nc_centroid[2]),
    zoom = 10
  ) |>
  add_fill_layer(
    id = "nc_data",
    source = nc,
    fill_color = "red",
    fill_opacity = 0.5
  )

# Now the center argument of maplibregl object is used
compare(m1, m2)

Session Info

#> R version 4.4.1 (2024-06-14 ucrt)
#> Platform: x86_64-w64-mingw32/x64
#> Running under: Windows 10 x64 (build 19045)
#> 
#> Matrix products: default
#> 
#> 
#> locale:
#> [1] LC_COLLATE=English_United States.utf8 
#> [2] LC_CTYPE=English_United States.utf8   
#> [3] LC_MONETARY=English_United States.utf8
#> [4] LC_NUMERIC=C                          
#> [5] LC_TIME=English_United States.utf8    
#> 
#> time zone: Pacific/Auckland
#> tzcode source: internal
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#> [1] sf_1.0-17   mapgl_0.1.4
#> 
#> loaded via a namespace (and not attached):
#>  [1] terra_1.7-78       cli_3.6.3          knitr_1.48         rlang_1.1.4       
#>  [5] xfun_0.46          DBI_1.2.3          KernSmooth_2.23-24 glue_1.7.0        
#>  [9] htmltools_0.5.8.1  e1071_1.7-14       rmarkdown_2.27     grid_4.4.1        
#> [13] evaluate_0.24.0    classInt_0.4-10    fastmap_1.2.0      base64enc_0.1-3   
#> [17] yaml_2.3.10        lifecycle_1.0.4    compiler_4.4.1     codetools_0.2-20  
#> [21] fs_1.6.4           htmlwidgets_1.6.4  Rcpp_1.0.13        digest_0.6.36     
#> [25] class_7.3-22       reprex_2.1.1       magrittr_2.0.3     tools_4.4.1       
#> [29] withr_3.0.1        proxy_0.4-27       geojsonsf_2.0.3    units_0.8-5
atsyplenkov commented 1 month ago

I think this is because the center and zoom parameters are actually set to 0 when maplibregl was created and are not updated.

m1 <- maplibre(bound = nc)

m1$x
#> $style
#> [1] "https://basemaps.cartocdn.com/gl/voyager-gl-style/style.json"
#> 
#> $center
#> [1] 0 0
#> 
#> $zoom
#> [1] 0
#> 
#> $bearing
#> [1] 0
#> 
#> $pitch
#> [1] 0
#> 
#> $additional_params
#> $additional_params$bounds
#> [1] -81.74091  36.23444 -81.23971  36.58973

So maybe we can add some function to update center and zoom arguments here as well?

https://github.com/walkerke/mapgl/blob/796545570e64dc29c7126ec4d9198db038bb9e0e/R/maplibre.R#L32-L37

walkerke commented 1 month ago

This is now fixed; we are now honoring the additional_params on the JS side which gets us the exact same behavior as a regular map. I do really appreciate the PR, though!