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 (
.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.
2. Here is a function to convert your geospatial files to GeoJSON (with roxygen docs).
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).
Here, we are converting a zip file containing shape files for Pinus contorta (data collected from the USGS here.
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…
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)
Fourth, back in the terminal…
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.