Convert various data formats to GeoJSON or TopoJSON

This package is a utility to convert geographic data to GeoJSON and TopoJSON formats. Nothing else. We hope to do this one job very well, and handle all reasonable use cases.

Functions in this package are organized first around what you’re working with or want to get, GeoJSON or TopoJSON, then convert to or read from various formats:

Each of the above functions have methods for various objects/classes, including numeric, data.frame, list, SpatialPolygons, SpatialLines, SpatialPoints, etc.

Additional functions:

  • map_gist() - push up a GeoJSON or topojson file as a GitHub gist (renders as an interactive map)
  • map_leaf() - create a local interactive map using the leaflet package

*json Info


A note about installing rgeos - built on top of C libraries, and installation often causes trouble for Linux users because no binaries are provided on CRAN for those platforms. Other dependencies in geojsonio should install easily automatically when you install geojsonio.


Install GDAL on the command line first, e.g., using homebrew

brew install gdal

Then install rgeos

install.packages("rgeos", type = "source")


Get deps first

sudo apt-get install libgdal1-dev libgdal-dev libgeos-c1 libproj-dev

Note: if you have trouble installing rgeos, try installing libgeos++-dev

Then install rgeos

install.packages("rgeos", type = "source")

Install geojsonio

Stable version from CRAN


Or development version from GitHub



Combine objects

geo_list + geo_list

Note: geo_list is the output type from geojson_list(), it’s just a list with a class attached so we know it’s geojson :)

json + json

Write GeoJSON

geojson_write(us.cities[1:2, ], lat = 'lat', lon = 'long')
#> <geojson-file>
#>   Path:       myfile.geojson
#>   From class: data.frame

Read GeoJSON

file <- system.file("examples", "california.geojson", package = "geojsonio")
out <- geojson_read(file)
#> [1] "type"     "name"     "crs"      "features"
#> [1] "type"       "properties" "geometry"

Use case: Play with US states

Using data from

Get some geojson

res <- HttpClient$new('')$get('repos/glynnbird/usstatesgeojson/contents')
out <- jsonlite::fromJSON(res$parse("UTF-8"), FALSE)
st_names <- Filter(function(x) grepl("\\.geojson", x), sapply(out, "[[", "name"))
base <- ''
st_files <- paste0(base, st_names)

Make a faceted plot

st_use <- st_files[7:13]
geo <- lapply(st_use, geojson_read, what = "sp")
df <- ldply(setNames(lapply(geo, fortify), gsub("\\.geojson", "", st_names[7:13])))
ggplot(df, aes(long, lat, group = group)) +
  geom_polygon() +
  facet_wrap(, scales = "free")

plot of chunk unnamed-chunk-24

Okay, so the maps are not quite right (stretched to fit each panel), but you get the idea.