Skip to content

Instantly share code, notes, and snippets.

@DavZim
Created July 8, 2020 08:11
Show Gist options
  • Save DavZim/414e30fb09b2e1d06e22464b83249cc6 to your computer and use it in GitHub Desktop.
Save DavZim/414e30fb09b2e1d06e22464b83249cc6 to your computer and use it in GitHub Desktop.
library(sf)
#> Linking to GEOS 3.8.0, GDAL 3.1.2, PROJ 7.0.0
library(leaflet)
library(leafem)

url <- "https://raw.githubusercontent.com/bjornharrtell/flatgeobuf/3.0.1/test/data/UScounties.fgb"
map <- st_read(url)
#> Reading layer `UScounties' from data source `https://raw.githubusercontent.com/bjornharrtell/flatgeobuf/3.0.1/test/data/UScounties.fgb' using driver `FlatGeobuf'
#> Simple feature collection with 3221 features and 6 fields
#> geometry type:  MULTIPOLYGON
#> dimension:      XY
#> bbox:           xmin: -179.1473 ymin: 17.88481 xmax: 179.7785 ymax: 71.35256
#> geographic CRS: NAD83

map$color <- colorFactor("viridis", unique(map$STATE))(map$STATE)
map
#> Simple feature collection with 3221 features and 7 fields
#> geometry type:  MULTIPOLYGON
#> dimension:      XY
#> bbox:           xmin: -179.1473 ymin: 17.88481 xmax: 179.7785 ymax: 71.35256
#> geographic CRS: NAD83
#> First 10 features:
#>    STATE_FIPS COUNTY_FIP  FIPS STATE        NAME   LSAD
#> 1          23        009 23009    ME     Hancock County
#> 2          33        007 33007    NH        Coos County
#> 3          50        009 50009    VT       Essex County
#> 4          50        019 50019    VT     Orleans County
#> 5          23        007 23007    ME    Franklin County
#> 6          23        029 23029    ME  Washington County
#> 7          23        019 23019    ME   Penobscot County
#> 8          23        021 23021    ME Piscataquis County
#> 9          23        025 23025    ME    Somerset County
#> 10         50        013 50013    VT  Grand Isle County
#>                          geometry   color
#> 1  MULTIPOLYGON (((-68.53108 4... #297B8E
#> 2  MULTIPOLYGON (((-71.05975 4... #21A585
#> 3  MULTIPOLYGON (((-71.49463 4... #CAE11F
#> 4  MULTIPOLYGON (((-72.14193 4... #CAE11F
#> 5  MULTIPOLYGON (((-70.83471 4... #297B8E
#> 6  MULTIPOLYGON (((-67.50038 4... #297B8E
#> 7  MULTIPOLYGON (((-68.03364 4... #297B8E
#> 8  MULTIPOLYGON (((-68.82681 4... #297B8E
#> 9  MULTIPOLYGON (((-70.02336 4... #297B8E
#> 10 MULTIPOLYGON (((-73.34312 4... #CAE11F

st_write(map, "local_map.fgb", append = FALSE)
#> Writing layer `local_map' to data source `local_map.fgb' using driver `FlatGeobuf'
#> Writing 3221 features with 7 fields and geometry type Multi Polygon.

leaflet() %>%
  addTiles() %>%
  leafem:::addFgb(
    file = "local_map.fgb", group = "counties",
    label = "NAME", popup = TRUE,
    fillColor = map$color, fillOpacity = 0.6,
    color = "black", weight = 1) %>%
  addLayersControl(overlayGroups = c("counties")) %>%
  setView(lng = -105.644, lat = 51.618, zoom = 3)
image <- imager::load.image("screenshot.png")
plot(image)

Created on 2020-07-08 by the reprex package (v0.3.0)

@DavZim
Copy link
Author

DavZim commented Jul 8, 2020

local_map.fgb can be found separately here: https://github.com/DavZim/misc/blob/master/local_map.fgb

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment