R to GeoJSON

June 30, 2013

(This article was first published on Recology - R, and kindly contributed to R-bloggers)

UPDATE As you can see in Patrick’s comment below you can convert to GeoJSON format files with rgdal as an alternative to calling the Ogre web API described below. See here for example code for converting to GeoJSON with rgdal.

GitHub recently introduced the ability to render GeoJSON files on their site as maps here, and recently introduced here support for TopoJSON, an extension of GeoJSON can be up to 80% smaller than GeoJSON, support for other file extensions (.topojson and .json), and you can embed the maps on other sites (so awesome). The underlying maps used on GitHub are Openstreet Maps.

A recent blog post showed how to convert .shp or .kml files to GeoJSON to then upload to GitHub here. The approach used Ruby on the command line locally to convert the geospatial files to GeoJSON.

Can we do this in R? Perhaps others have already done this, but there’s more than one way to do anything, no?

I’m not aware of a converter to GeoJSON within R, but there is a web service that can do this, called Ogre. The service lets you POST a file, which then converts to GeoJSON and gives it back to you. Ogre accepts many different file formats: BNA, CSV, DGN, DXF, zipped shapefiles, GeoConcept, GeoJSON, GeoRSS, GML, GMT, KML, MapInfo, and VRT.

We can use the Ogre API to upload a local geospatial file of various formats and get the GeoJSON back, then put it up on GitHub, and they render the map for you. Sweetness.

So here’s the protocol.

1. Load httr. What is httr? For those not in the know it is a simpler wrapper around RCurl, a curl interface for R.

# install.packages('httr')

2. Here is a function to convert your geospatial files to GeoJSON (with roxygen docs).

togeojson <- function(file, writepath = "~") {
    url <- "http://ogre.adc4gis.com/convert"
    tt <- POST(url, body = list(upload = upload_file(file)))
    out <- content(tt, as = "text")
    fileConn <- file(writepath)
    writeLines(out, fileConn)

3. Convert a file to GeoJSON


In the first line I specify the location of the file on my machine. In the second line the function togeojson reads in the file, sends the file to the API endpoint http://ogre.adc4gis.com/convert, collects the returned GeoJSON object, and saves the GeoJSON to a file that you specify. Here we are converting a KML file with point occurrences (data collected from USGS’s BISON service).

file <- "~/github/sac/rgeojson/acer_spicatum.kml"
togeojson(file, "~/github/sac/rgeojson/acer_spicatum.geojson")


Here, we are converting a zip file containing shape files for Pinus contorta (data collected from the USGS here.

file <- "~/github/sac/rgeojson/pinucont.zip"
togeojson(file, "~/github/sac/rgeojson/pinus.geojson")

4. Then commit and push to GitHub. And this is what they look like on GitHub

Acer spicatum distribution (points)

Pinus contorta distribution (polygons)

If you want, you can clone a repo from my account. Then do the below. (of course, you must have git installed, and have a GitHub account…)

First, fork my rgeojson repo here to your GitHub account.

Second, in your terminal/command line…

git clone https://github.com/<yourgithubusername>/rgeojson.git
cd rgeojson

Third, in R specify the location of either the KML file or the zipped shape files, then call togeojson function to convert the KML file to a GeoJSON file (which should output a file acer_spicatum.geojson)

file <- "/path/to/acer_spicatum.kml"
togeojson(file, "~/path/to/write/to/acer_spicatum.geojson")

Fourth, back in the terminal…

git add acer_spicatum.geojson
git commit -a -m 'some cool commit message'
git push

Fifth, go to your rgeojson repo on GitHub and click on the acer_spicatum.geojson file, and the map should render.

Look for this functionality to come to the rbison and rgbif R packages soon. Why is that cool? Think of the workflow: Query for species occurrence data in the BISON or GBIF databases, convert the results to a GeoJSON file, push to GitHub, and you have an awesome interactive map on the web. Not too bad eh.

To leave a comment for the author, please follow the link and comment on their blog: Recology - R.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.


Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)