{"id":73427,"date":"2013-06-30T01:00:00","date_gmt":"2013-06-30T07:00:00","guid":{"rendered":"http:\/\/www.r-bloggers.com\/?guid=a5837415691ee3a3ccfb375b493b11de"},"modified":"2013-06-30T01:00:00","modified_gmt":"2013-06-30T07:00:00","slug":"r-to-geojson","status":"publish","type":"post","link":"https:\/\/www.r-bloggers.com\/2013\/06\/r-to-geojson\/","title":{"rendered":"R to GeoJSON"},"content":{"rendered":"<!-- \r\n<div style=\"min-height: 30px;\">\r\n[social4i size=\"small\" align=\"align-left\"]\r\n<\/div>\r\n-->\r\n\r\n<div style=\"border: 1px solid; background: none repeat scroll 0 0 #EDEDED; margin: 1px; font-size: 12px;\">\r\n[This article was first published on  <strong><a href=\"http:\/\/schamberlain.github.io\/2013\/06\/geojson\/\"> Recology - R<\/a><\/strong>, and kindly contributed to <a href=\"https:\/\/www.r-bloggers.com\/\" rel=\"nofollow\">R-bloggers<\/a>].  (You can report issue about the content on this page <a href=\"https:\/\/www.r-bloggers.com\/contact-us\/\">here<\/a>)\r\n<hr>Want to share your content on R-bloggers?<a href=\"https:\/\/www.r-bloggers.com\/add-your-blog\/\" rel=\"nofollow\"> click here<\/a> if you have a blog, or <a href=\"http:\/\/r-posts.com\/\" rel=\"nofollow\"> here<\/a> if you don't.\r\n<\/div>\n<p>GitHub recently introduced the ability to render <a href=\"http:\/\/en.wikipedia.org\/wiki\/GeoJSON\" rel=\"nofollow\" target=\"_blank\">GeoJSON<\/a> files on their site as maps <a href=\"https:\/\/github.com\/blog\/1528-there-s-a-map-for-that\" rel=\"nofollow\" target=\"_blank\">here<\/a>, and recently introduced <a href=\"https:\/\/github.com\/blog\/1541-geojson-rendering-improvements\" rel=\"nofollow\" target=\"_blank\">here<\/a> support for <a href=\"https:\/\/github.com\/mbostock\/topojson\" rel=\"nofollow\" target=\"_blank\">TopoJSON<\/a>, an extension of GeoJSON can be up to 80% smaller than GeoJSON, support for other file extensions (<code>.topojson<\/code> and <code>.json<\/code>), and you can embed the maps on other sites (so awesome). The underlying maps used on GitHub are <a href=\"http:\/\/www.openstreetmap.org\/\" rel=\"nofollow\" target=\"_blank\">Openstreet Maps<\/a>.<\/p>\n\n<p>A recent blog post showed how to convert .shp or .kml files to GeoJSON to then upload to GitHub <a href=\"http:\/\/ben.balter.com\/2013\/06\/26\/how-to-convert-shapefiles-to-geojson-for-use-on-github\/\" rel=\"nofollow\" target=\"_blank\">here<\/a>. The approach used Ruby on the command line locally to convert the geospatial files to GeoJSON.<\/p>\n\n<p>Can we do this in R? Perhaps others have already done this, but there&#8217;s more than one way to do anything, no?<\/p>\n\n<p>I&#8217;m not aware of a converter to GeoJSON within R, but there is a web service that can do this, called <a href=\"http:\/\/ogre.adc4gis.com\/\" rel=\"nofollow\" target=\"_blank\">Ogre<\/a>. The service lets you <code>POST<\/code> 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.<\/p>\n\n<p>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.<\/p>\n\n<p>So here&#8217;s the protocol.<\/p>\n\n<hr \/>\n\n<h3>1. Load httr. What is httr? For those not in the know it is a simpler wrapper around RCurl, a curl interface for R.<\/h3>\n\n<pre># install.packages(&#39;httr&#39;)\nlibrary(httr)\n<\/pre>\n\n\n<h3>2. Here is a function to convert your geospatial files to GeoJSON (with roxygen docs).<\/h3>\n\n<pre>togeojson &lt;- function(file, writepath = &quot;~&quot;) {\n    url &lt;- &quot;http:\/\/ogre.adc4gis.com\/convert&quot;\n    tt &lt;- POST(url, body = list(upload = upload_file(file)))\n    out &lt;- content(tt, as = &quot;text&quot;)\n    fileConn &lt;- file(writepath)\n    writeLines(out, fileConn)\n    close(fileConn)\n}\n<\/pre>\n\n\n<h3>3. Convert a file to GeoJSON<\/h3>\n\n<p><strong>KML<\/strong><\/p>\n\n<p>In the first line I specify the location of the file on my machine. In the second line the function <code>togeojson<\/code> reads in the file, sends the file to the API endpoint <em>http:\/\/ogre.adc4gis.com\/convert<\/em>, 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 <a href=\"http:\/\/bison.usgs.ornl.gov\/\" rel=\"nofollow\" target=\"_blank\">USGS's BISON service<\/a>).<\/p>\n\n<pre>file &lt;- &quot;~\/github\/sac\/rgeojson\/acer_spicatum.kml&quot;\ntogeojson(file, &quot;~\/github\/sac\/rgeojson\/acer_spicatum.geojson&quot;)\n<\/pre>\n\n\n<p><strong>Shapefiles<\/strong><\/p>\n\n<p>Here, we are converting a zip file containing shape files for <em>Pinus contorta<\/em> (data collected from the USGS <a href=\"http:\/\/esp.cr.usgs.gov\/data\/little\/\" rel=\"nofollow\" target=\"_blank\">here<\/a>.<\/p>\n\n<pre>file &lt;- &quot;~\/github\/sac\/rgeojson\/pinucont.zip&quot;\ntogeojson(file, &quot;~\/github\/sac\/rgeojson\/pinus.geojson&quot;)\n<\/pre>\n\n\n<h3>4. Then commit and push to GitHub. And this is what they look like on GitHub<\/h3>\n\n<p><em>Acer spicatum<\/em> distribution (points)<\/p>\n\n<!-- ![](\/img\/acer_spiacdtum_dist.png) -->\n\n\n<script src=\"https:\/\/embed.github.com\/view\/geojson\/SChamberlain\/rgeojson\/output\/acer_spicatum.geojson\"><\/script>\n\n\n<p><em>Pinus contorta<\/em> distribution (polygons)<\/p>\n\n<!-- ![](\/img\/pinus_dist.png) -->\n\n\n<script src=\"https:\/\/embed.github.com\/view\/geojson\/SChamberlain\/rgeojson\/output\/pinus.geojson\"><\/script>\n\n\n<hr \/>\n\n<p>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...)<\/p>\n\n<p>First, fork my <code>rgeojson<\/code> repo <a href=\"https:\/\/github.com\/SChamberlain\/rgeojson\" rel=\"nofollow\" target=\"_blank\">here<\/a> to your GitHub account.<\/p>\n\n<p>Second, in your terminal\/command line...<\/p>\n\n<pre>git clone https:\/\/github.com\/&lt;yourgithubusername&gt;\/rgeojson.git\ncd rgeojson\n<\/pre>\n\n\n<p>Third, in R specify the location of either the KML file or the zipped shape files, then call <code>togeojson<\/code> function to convert the KML file to a GeoJSON file (which should output a file <em>acer_spicatum.geojson<\/em>)<\/p>\n\n<pre>file &lt;- &quot;\/path\/to\/acer_spicatum.kml&quot;\ntogeojson(file, &quot;~\/path\/to\/write\/to\/acer_spicatum.geojson&quot;)\n<\/pre>\n\n\n<p>Fourth, back in the terminal...<\/p>\n\n<pre>git add acer_spicatum.geojson\ngit commit -a -m &#39;some cool commit message&#39;\ngit push\n<\/pre>\n\n\n<p>Fifth, go to your <em>rgeojson<\/em> repo on GitHub and click on the <em>acer_spicatum.geojson<\/em> file, and the map should render.<\/p>\n\n<hr \/>\n\n<p>Look for this functionality to come to the <a href=\"https:\/\/github.com\/ropensci\/rbison\" rel=\"nofollow\" target=\"_blank\">rbison<\/a> and <a href=\"https:\/\/github.com\/ropensci\/rgbif\" rel=\"nofollow\" target=\"_blank\">rgbif<\/a> 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.<\/p>\n\n<div style=\"border: 1px solid; background: none repeat scroll 0 0 #EDEDED; margin: 1px; font-size: 13px;\">\r\n<div style=\"text-align: center;\">To <strong>leave a comment<\/strong> for the author, please follow the link and comment on their blog: <strong><a href=\"http:\/\/schamberlain.github.io\/2013\/06\/geojson\/\"> Recology - R<\/a><\/strong>.<\/div>\r\n<hr \/>\r\n<a href=\"https:\/\/www.r-bloggers.com\/\" rel=\"nofollow\">R-bloggers.com<\/a> offers <strong><a href=\"https:\/\/feedburner.google.com\/fb\/a\/mailverify?uri=RBloggers\" rel=\"nofollow\">daily e-mail updates<\/a><\/strong> about <a title=\"The R Project for Statistical Computing\" href=\"https:\/\/www.r-project.org\/\" rel=\"nofollow\">R<\/a> news and tutorials about <a title=\"R tutorials\" href=\"https:\/\/www.r-bloggers.com\/how-to-learn-r-2\/\" rel=\"nofollow\">learning R<\/a> and many other topics. <a title=\"Data science jobs\" href=\"https:\/\/www.r-users.com\/\" rel=\"nofollow\">Click here if you're looking to post or find an R\/data-science job<\/a>.\r\n\r\n<hr>Want to share your content on R-bloggers?<a href=\"https:\/\/www.r-bloggers.com\/add-your-blog\/\" rel=\"nofollow\"> click here<\/a> if you have a blog, or <a href=\"http:\/\/r-posts.com\/\" rel=\"nofollow\"> here<\/a> if you don't.\r\n<\/div>","protected":false},"excerpt":{"rendered":"<p>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 (&#8230;<\/p>\n","protected":false},"author":416,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[4],"tags":[],"aioseo_notices":[],"jetpack-related-posts":[{"id":75244,"url":"https:\/\/www.r-bloggers.com\/2014\/01\/how-to-create-a-geojson-in-r\/","url_meta":{"origin":73427,"position":0},"title":"How to create a geojson in R","date":"January 22, 2014","format":false,"excerpt":"This is a short but sweet post on how to create a geojson file in R .\u00a0\u00a0Remember that you can render geojson files in github. \u00a0 The post How to create a geojson in R appeared first on Jose Gonzalez.","rel":"nofollow","context":"In \"R bloggers\"","img":{"src":"","width":0,"height":0},"classes":[]},{"id":75350,"url":"https:\/\/www.r-bloggers.com\/2013\/10\/interactive-maps-with-polygons-using-r-geojson-and-github\/","url_meta":{"origin":73427,"position":1},"title":"Interactive maps with polygons using R, Geojson, and Github","date":"October 22, 2013","format":false,"excerpt":"Previously on this blog we have discussed making geojson maps and uploading to Github for interactive visualization with USGS BISON data, and with GBIF data, and on my own personal blog. This is done using a file format called geojson, a file format based on JSON (JavaScript Object Notation) in\u2026","rel":"nofollow","context":"In \"R bloggers\"","img":{"src":"","width":0,"height":0},"classes":[]},{"id":74160,"url":"https:\/\/www.r-bloggers.com\/2013\/09\/r-geojson-and-github\/","url_meta":{"origin":73427,"position":2},"title":"R, GeoJSON and GitHub","date":"September 20, 2013","format":false,"excerpt":"GeoJSON is an open computer file format for encoding collections of simple geographical features along with their non-spatial attributes using \u2026Continuar leyendo \u00bb","rel":"nofollow","context":"In \"R bloggers\"","img":{"src":"","width":0,"height":0},"classes":[]}],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.r-bloggers.com\/wp-json\/wp\/v2\/posts\/73427"}],"collection":[{"href":"https:\/\/www.r-bloggers.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.r-bloggers.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.r-bloggers.com\/wp-json\/wp\/v2\/users\/416"}],"replies":[{"embeddable":true,"href":"https:\/\/www.r-bloggers.com\/wp-json\/wp\/v2\/comments?post=73427"}],"version-history":[{"count":0,"href":"https:\/\/www.r-bloggers.com\/wp-json\/wp\/v2\/posts\/73427\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.r-bloggers.com\/wp-json\/wp\/v2\/media?parent=73427"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.r-bloggers.com\/wp-json\/wp\/v2\/categories?post=73427"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.r-bloggers.com\/wp-json\/wp\/v2\/tags?post=73427"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}