walkerke / mapgl

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

Use empty/solid color basemap #28

Closed CIOData closed 1 month ago

CIOData commented 1 month ago

I am trying to create a map using shifted geometry for the US (i.e. Alaska and Hawaii moved), and need to have an empty/solid color basemap option for displaying it. I can't seem to find an option for doing this in mapboxgl() or maplibre().

walkerke commented 1 month ago

It's kind of a wonky solution, based on this: https://gis.stackexchange.com/questions/398348/mapbox-map-with-no-style-only-self-hosted-vector-data

I'd be open to it as a feature request! Note that the Albers projection is only available in Mapbox, not MapLibre.

library(mapgl)
library(tigris)
library(jsonlite)

js_object <- list(
  version = 8,
  sources = structure(list(), .Names = character(0)),
  layers = list()
)

json_output <- toJSON(js_object, auto_unbox = TRUE, pretty = TRUE)

us_states <- states(cb = TRUE, resolution = "20m") |> 
  shift_geometry()

mapboxgl(style = json_output,
         projection = "albers",
         center = c(-98.8, 37.68),
         zoom = 3.4) |>
  add_fill_layer(
    id = "states",
    source = us_states,
    fill_color = "navy",
    fill_opacity = 0.7,
    tooltip = "NAME"
  )

image

walkerke commented 1 month ago

You can also author a custom blank style with a background in Mapbox Studio, then use that. Here I've got a style with a light grey background I set up in Studio.

style_url <- "mapbox://styles/kwalkertcu/clz2wwap502f301pafh8ad1zv/draft"

mapboxgl(style = style_url,
         projection = "albers",
         center = c(-98.8, 37.68),
         zoom = 3.4) |>
  add_fill_layer(
    id = "states",
    source = us_states,
    fill_color = "navy",
    fill_opacity = 0.7,
    tooltip = "NAME"
  )

image

CIOData commented 4 days ago

This was helpful, but I've run into a funny issue. When I try to use this solution with add_fill_extrusion_layer() I get a half copy of the map off in the distance at certain zoom levels.

image

This does not happen when I use just add_fill_layer().

image

Any thoughts on what might be happening? I can't seem to figure it out.