{"id":20,"date":"2011-10-09T01:04:45","date_gmt":"2011-10-09T01:04:45","guid":{"rendered":"http:\/\/blog.fellstat.com\/?p=20"},"modified":"2011-10-09T01:04:45","modified_gmt":"2011-10-09T01:04:45","slug":"open-street-maps","status":"publish","type":"post","link":"https:\/\/blog.fellstat.com\/?p=20","title":{"rendered":"Open Street maps"},"content":{"rendered":"<p>There have been some exciting developments in the <a href=\"http:\/\/www.deducer.org\" target=\"_blank\" rel=\"noopener\">Deducer<\/a> ecosystem over the summer which should go into CRAN release in the next few months. Today I&#8217;m going to give a quick sneak peek at an Open Street Map &#8211; R connection with\u00a0accompanying\u00a0GUI. This post will just show the non-GUI components.<\/p>\n<p>The first part of the project was to create a way to download and plot Open Street Map data from either Mapnik or Bing in a local R instance. Before we can do that however, we need to install DeducerSpatial.<\/p>\n<pre>install.packages(c(\"Deducer\",\"sp\",\"rgdal\",\"maptools\"))\ninstall.packages(\"UScensus2000\")\n\n#get development versions\ninstall.packages(c(\"JGR\",\"Deducer\"),,\"http:\/\/rforge.net\",type=\"source\")\ninstall.packages(\"DeducerSpatial\",,\"http:\/\/r-forge.r-project.org\",type=\"source\")<\/pre>\n<pre><\/pre>\n<p>Note that you will need rgdal. And you will need your development tools to install the development versions of JGR, Deducer and DeducerSpatial.<\/p>\n<p><strong>Plot an Open Street Map Image<\/strong><\/p>\n<p>We are going to take a look at the median age of households in the 2000 california census survey. First, lets see if we can get the open street map image for that area.<\/p>\n<p>&nbsp;<\/p>\n<pre>#load package\nlibrary(DeducerSpatial)\nlibrary(UScensus2000)\n\n#create an open street map image\nlat &lt;- c(43.834526782236814,30.334953881988564)\nlon &lt;- c(-131.0888671875  ,-107.8857421875)\nsouthwest &lt;- openmap(c(lat[1],lon[1]),c(lat[2],lon[2]),zoom=5,'osm')<\/pre>\n<pre>plot(southwest,raster=FALSE)<\/pre>\n<p><a href=\"http:\/\/blog.fellstat.com\/wp-content\/uploads\/2011\/10\/southwest.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-large wp-image-27\" title=\"southwest\" src=\"http:\/\/blog.fellstat.com\/wp-content\/uploads\/2011\/10\/southwest-1024x899.png\" alt=\"\" width=\"550\" height=\"482\" \/><\/a><\/p>\n<p>Note that plot has an\u00a0argument &#8216;raster&#8217; which determines if the image is plotted as a raster image. &#8216;zoom&#8217; controls the level of detail in the image. Some care needs to be taken in choosing the right level of zoom, as you can end up trying to pull street level images for the entire world if you are not careful.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Make a Choropleth Plot<\/strong><\/p>\n<p>Next, we can add a choropleth to the plot. We bring in the census data from the UScensus2000 package, and transform it to the mercator projection using spTransform.<\/p>\n<p>&nbsp;<\/p>\n<pre>#load in califonia data and transform coordinates to mercator\ndata(california.tract)\ncalifornia.tract &lt;- spTransform(california.tract,osm())\n\n#add median age choropleth\nchoro_plot(california.tract,dem = california.tract@data[,'med.age'],\n\tlegend.title = 'Median Age')<\/pre>\n<p><a href=\"http:\/\/blog.fellstat.com\/wp-content\/uploads\/2011\/10\/choro.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-large wp-image-29\" title=\"choro\" src=\"http:\/\/blog.fellstat.com\/wp-content\/uploads\/2011\/10\/choro-1024x899.png\" alt=\"\" width=\"550\" height=\"482\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p><strong>Use\u00a0Aerial\u00a0Imagery<\/strong><\/p>\n<p>We can also easily use bing\u00a0satellite\u00a0imagery instead.<\/p>\n<pre>southwest &lt;- openmap(c(lat[1],lon[1]),c(lat[2],lon[2]),5,'bing')\nplot(southwest,raster=FALSE)\nchoro_plot(california.tract,dem = california.tract@data[,'med.age'],alpha=.8,\n\tlegend.title = 'Median Age')<\/pre>\n<p>&nbsp;<\/p>\n<p><a href=\"http:\/\/blog.fellstat.com\/wp-content\/uploads\/2011\/10\/bing.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-large wp-image-30\" title=\"bing\" src=\"http:\/\/blog.fellstat.com\/wp-content\/uploads\/2011\/10\/bing-1024x930.png\" alt=\"\" width=\"550\" height=\"499\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>One other fun thing to note is that the image tiles are cached so they do not always need to be re-downloaded.<\/p>\n<p>&nbsp;<\/p>\n<pre>&gt; system.time(southwest &lt;- openmap(c(lat[1],lon[1]),c(lat[2],lon[2]),zoom=7,'bing'))\n   user  system elapsed\n  9.502   0.547  17.166\n&gt; system.time(southwest &lt;- openmap(c(lat[1],lon[1]),c(lat[2],lon[2]),zoom=7,'bing'))\n   user  system elapsed\n  9.030   0.463   9.169<\/pre>\n<p>&nbsp;<\/p>\n<p>Notice how the elapsed time in the second call is half that of the first one, this is due to\u00a0caching.<\/p>\n<p>This just scratches the surface of what is possible with the new package. You can plot any type of spatial data (points, lines, etc.) supported by the sp package so long as it is in the mercator projection. Also, there is a full featured GUI to help you load your data and make plots, but I&#8217;ll talk about that in a later post.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>There have been some exciting developments in the Deducer ecosystem over the summer which should go into CRAN release in the next few months. Today I&#8217;m going to give a quick sneak peek at an Open Street Map &#8211; R connection with\u00a0accompanying\u00a0GUI. This post will just show the non-GUI components. The first part of the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,6,1],"tags":[12,14],"class_list":["post-20","post","type-post","status-publish","format-standard","hentry","category-deducer","category-r","category-uncategorized","tag-deducer","tag-r"],"_links":{"self":[{"href":"https:\/\/blog.fellstat.com\/index.php?rest_route=\/wp\/v2\/posts\/20","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.fellstat.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.fellstat.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.fellstat.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.fellstat.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=20"}],"version-history":[{"count":0,"href":"https:\/\/blog.fellstat.com\/index.php?rest_route=\/wp\/v2\/posts\/20\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.fellstat.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=20"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.fellstat.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=20"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.fellstat.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=20"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}