R to GeoJSON

[This article was first published on Recology - R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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.

<span class="c1"># install.packages('httr')</span>
library<span class="p">(</span>httr<span class="p">)</span>

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

togeojson <span class="o"><-</span> <span class="kr">function</span><span class="p">(</span>file<span class="p">,</span> writepath <span class="o">=</span> <span class="s">"~"</span><span class="p">)</span> <span class="p">{</span>
    url <span class="o"><-</span> <span class="s">"http://ogre.adc4gis.com/convert"</span>
    tt <span class="o"><-</span> POST<span class="p">(</span>url<span class="p">,</span> body <span class="o">=</span> list<span class="p">(</span>upload <span class="o">=</span> upload_file<span class="p">(</span>file<span class="p">)))</span>
    out <span class="o"><-</span> content<span class="p">(</span>tt<span class="p">,</span> as <span class="o">=</span> <span class="s">"text"</span><span class="p">)</span>
    fileConn <span class="o"><-</span> file<span class="p">(</span>writepath<span class="p">)</span>
    writeLines<span class="p">(</span>out<span class="p">,</span> fileConn<span class="p">)</span>
    close<span class="p">(</span>fileConn<span class="p">)</span>
<span class="p">}</span>

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 <span class="o"><-</span> <span class="s">"~/github/sac/rgeojson/acer_spicatum.kml"</span>
togeojson<span class="p">(</span>file<span class="p">,</span> <span class="s">"~/github/sac/rgeojson/acer_spicatum.geojson"</span><span class="p">)</span>


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

file <span class="o"><-</span> <span class="s">"~/github/sac/rgeojson/pinucont.zip"</span>
togeojson<span class="p">(</span>file<span class="p">,</span> <span class="s">"~/github/sac/rgeojson/pinus.geojson"</span><span class="p">)</span>

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
<span class="nb">cd </span>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 <span class="o"><-</span> <span class="s">"/path/to/acer_spicatum.kml"</span>
togeojson<span class="p">(</span>file<span class="p">,</span> <span class="s">"~/path/to/write/to/acer_spicatum.geojson"</span><span class="p">)</span>

Fourth, back in the terminal…

git add acer_spicatum.geojson
git commit -a -m <span class="s1">'some cool commit message'</span>
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 about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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)