Working with GeoJSON & TopoJSON

If your data is in GeoJSON or TopoJSON format, you can either convert it into sp classes using rgdal::readOGR or geojsonio (docs) and then use the marker and shape functions to add them; or use the addGeoJSON() and addTopoJSON() functions, for more convenient but less expressive handling.

The addGeoJSON() and addTopoJSON() functions accept GeoJSON data in either parsed (nested lists) or stringified (single-element character vector) format.

Note that for larger JSON data, using parsed is significantly slower than using stringified, because parsed data must go through a JSON encoding step.

A simple example using stringified data:

topoData <- readLines("json/us-10m.json") %>% paste(collapse = "\n")

leaflet() %>% setView(lng = -98.583, lat = 39.833, zoom = 3) %>%
  addTiles() %>%
  addTopoJSON(topoData, weight = 1, color = "#444444", fill = FALSE)

Styling

You can modify the style of GeoJSON/TopoJSON features in a few ways. (Currently only paths and shapes can be styled; marker styling is not supported.)

One way to apply styles to all of the features is to use the arguments directly on the addGeoJSON/addTopoJSON functions. Another way is to encode styling information directly in the JSON object/string by putting a style object directly underneath the top level JSON object.

You can provide feature-specific styles by annotating each feature’s property object with a style: {...} object.

See Leaflet’s path options for available style properties.

The feature-specific styles have the highest precedence, then the top-level style object, and finally the style-related arguments passed to the function.

The previous example demonstrated the style arguments. Below is a more involved example that sets both global styles and per-feature styles directly into the JSON object.

library(jsonlite)

# From http://data.okfn.org/data/datasets/geo-boundaries-world-110m
geojson <- readLines("json/countries.geojson", warn = FALSE) %>%
  paste(collapse = "\n") %>%
  fromJSON(simplifyVector = FALSE)

# Default styles for all features
geojson$style = list(
  weight = 1,
  color = "#555555",
  opacity = 1,
  fillOpacity = 0.8
)

# Gather GDP estimate from all countries
gdp_md_est <- sapply(geojson$features, function(feat) {
  feat$properties$gdp_md_est
})
# Gather population estimate from all countries
pop_est <- sapply(geojson$features, function(feat) {
  max(1, feat$properties$pop_est)
})

# Color by per-capita GDP using quantiles
pal <- colorQuantile("Greens", gdp_md_est / pop_est)
# Add a properties$style list to each feature
geojson$features <- lapply(geojson$features, function(feat) {
  feat$properties$style <- list(
    fillColor = pal(
      feat$properties$gdp_md_est / max(1, feat$properties$pop_est)
    )
  )
  feat
})

# Add the now-styled GeoJSON object to the map
leaflet() %>% addGeoJSON(geojson)