sp_to_geojson: Convert a spatial data object to a GeoJSON string

View source: R/sp_to_geojson.R

sp_to_geojsonR Documentation

Convert a spatial data object to a GeoJSON string

Description

Convert an R spatial polygons object to a GeoJSON string that can be used in geojson_layer. The spatial classes are provided by the sp package.

Usage

sp_to_geojson(sp)

Arguments

sp

A SpatialPolygonsDataFrame.

Value

A string containing unparsed GeoJSON.

Note

This function will write a GeoJSON file to the temporary directory created by R, but this file will be deleted when the R session ends.

Examples

if(require(USAboundaries)) {
  # Boundaries of the United States of America in 1800
  us <- us_boundaries(as.Date("1800-01-01"))
  us_geojson <- sp_to_geojson(us)

  cartographer(region = "United States") %>%
    geojson_layer(data = us_geojson, clickable = TRUE, label = "US 1800")
}

ropensci/cartographer documentation built on May 18, 2022, 9:49 a.m.