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

Use empty/solid color basemap #28

Closed CIOData closed 4 months ago

CIOData commented 4 months 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 4 months 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 4 months 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 2 months 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.