<?xml version="1.0" encoding="UTF-8"?>
<posts type="array">
  <post>
    <author>Simon Schoeters</author>
    <content>&lt;p&gt;In one of my &lt;a href="/blog/generated-maps" title="Generated maps with Ruby on Rails"&gt;previous posts&lt;/a&gt; I showed how to dynamically generate a map from a set of coordinates in Ruby on Rails (like the map on top of this page). This is nice but still required me to manually update my location each time... not very useable. I have a &lt;a href="http://www.dopplr.com/" title="An online tool for frequent business travellers" class="ext"&gt;Dopplr&lt;/a&gt; account where I enter all my future trips so it feels stupid to enter the same information again on my own website. We can do better than that!&lt;/p&gt;

&lt;p&gt;Here is the idea: a script that checks my travel plans on Dopplr for to following day and creates a new map of that location if needed. Also: a perfect excuse to have a look at the Rake build language which I had never used.&lt;/p&gt;

&lt;h3&gt;Dopplr &lt;abbr title="Application Programming Interface"&gt;API&lt;/abbr&gt;&lt;/h3&gt;

&lt;p&gt;The &lt;a href="http://dopplr.pbwiki.com/" title="Dopplr Developer Wiki" class="ext"&gt;Dopplr &lt;abbr title="Application Programming Interface"&gt;API&lt;/abbr&gt;&lt;/a&gt; includes a Ruby client which makes our life easier, download the &amp;lsquo;Improved Ruby Client&amp;rsquo; in your &lt;samp&gt;&amp;lt;yourapp&amp;gt;/lib&lt;/samp&gt; folder so we can use it later on.&lt;/p&gt;

&lt;h3&gt;Tokens and sessions&lt;/h3&gt;

&lt;p&gt;You will need a session key to use the Dopplr &lt;abbr title="Application Programming Interface"&gt;API&lt;/abbr&gt;, this key replaces your username and password (read &amp;lsquo;&lt;a href="http://adactio.com/journal/1357" class="ext" title="The password anti-pattern @ Adactio"&gt;The password anti-pattern&lt;/a&gt;&amp;rsquo; if you want to know why). Here is how to get one:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Go to &lt;samp&gt;https://www.dopplr.com/api/AuthSubRequest?scope=http://www.dopplr.com&amp;amp;next=&lt;strong&gt;&amp;lt;your_url&amp;gt;&lt;/strong&gt;&amp;amp;session=1&lt;/samp&gt;. Replace &lt;samp&gt;&amp;lt;your_url&amp;gt;&lt;/samp&gt; with your website's URL.&lt;/li&gt;
&lt;li&gt;Once approved you will be redirected to your URL with a &lt;samp&gt;token=&amp;lt;token_hash&amp;gt;&lt;/samp&gt; parameter, copy the token value.&lt;/li&gt;
&lt;li&gt;Start your Rails development console to convert the token to a session hash with &lt;samp&gt;./script/console&lt;/samp&gt;.&lt;/li&gt;
&lt;li&gt;Upgrade the token to a session:&lt;br/&gt;
&lt;samp&gt;&amp;gt;&amp;gt; d = Dopplr.new&lt;br/&gt;
&amp;gt;&amp;gt; d.set_token("&lt;strong&gt;&amp;lt;token_hash&amp;gt;&lt;/strong&gt;")&lt;br/&gt;
&amp;gt;&amp;gt; d.upgrade_to_session&lt;/samp&gt;&lt;br/&gt;&lt;/li&gt;
&lt;li&gt;Copy the result - this is your session key - and keep it somewhere safe.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;Rake task&lt;/h3&gt;

&lt;p&gt;I wanted something outside this website that could run automatically, even when the website wasn't opened. Rake offers you a way to do this: you can build and run scripts independently from the rest of the application while using the same environment as the application.&lt;/p&gt;

&lt;p&gt;Here is a simplified version of the task I created. Copy this and save it in your &lt;samp&gt;&amp;lt;yourapp&amp;gt;/lib/tasks&lt;/samp&gt; folder as &lt;samp&gt;map.rake&lt;/samp&gt;.&lt;/p&gt;

&lt;p&gt;&lt;samp&gt;namespace :map do&lt;br /&gt;&lt;br /&gt;
desc "Overwrites the map with my Dopplr location on a given date"&lt;br /&gt;
task :date =&gt; :environment do&lt;br /&gt;
c = define_coordinates(ENV['date'])&lt;br /&gt;
create_map(c['lat'], c['long'], 1200, 100, 0.3, 8)&lt;br /&gt;
end&lt;br /&gt;&lt;br /&gt;
desc "Updates the map.png with a map of my current Dopplr location"&lt;br /&gt;
task :update =&gt; :environment do&lt;br /&gt;
today = Time.now&lt;br /&gt;
c = define_coordinates(today.strftime("%Y%m%d"))&lt;br /&gt;
create_map(c['lat'], c['long'], 1200, 100, 0.3, 8)&lt;br /&gt;
end&lt;br /&gt;&lt;br /&gt;
# Extract the coordinates from the Dopplr API&lt;br /&gt;
def define_coordinates(date)&lt;br /&gt;
d = Dopplr.new&lt;br /&gt;
d.set_token(&lt;strong&gt;&amp;lt;session_key&amp;gt;&lt;/strong&gt;)&lt;br /&gt;
loc = d.location_on_date(:date =&gt; date)['location']&lt;br /&gt;
if loc.has_key?('trip')&lt;br /&gt;
{"lat" =&gt; loc['trip']['city']['latitude'], "long" =&gt; loc['trip']['city']['longitude']}&lt;br /&gt;
else&lt;br /&gt;
{"lat" =&gt; loc['home']['latitude'], "long" =&gt; loc['home']['longitude']}&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;&lt;br /&gt;
# Create a new OpenStreetMap with the given coordinates&lt;br /&gt;
def create_map(latitude, longitude, width = 500, height = 500, opacity = 1, zoom = 7)&lt;br /&gt;
[...] see the &lt;a href="/blog/generated-maps" title="Generated maps with Ruby on Rails"&gt;other&lt;/a&gt; post [...]&lt;br /&gt;
end&lt;br /&gt;&lt;br /&gt;
end&lt;/samp&gt;&lt;/p&gt;

&lt;p&gt;Now, check your taks with &lt;samp&gt;rake -T&lt;/samp&gt;. You'll get an overview of all the Rake tasks for your app and there should be a &amp;lsquo;map:date&amp;rsquo; and &amp;lsquo;map:update&amp;rsquo; one. This means you can run these scripts interactively now:&lt;/p&gt;

&lt;dl&gt;
	&lt;dt&gt;&lt;samp&gt;rake map:date date=20080101&lt;/samp&gt;&lt;/dt&gt;
	&lt;dd&gt;Will create/overwrite the map.png file in the /public/image folder with a map of you Dopplr location on January 1&lt;sup&gt;st&lt;/sup&gt;, 2008.&lt;/dd&gt;
	&lt;dt&gt;&lt;samp&gt;rake map:update&lt;/samp&gt;&lt;/dt&gt;
	&lt;dd&gt;Will also create a map but with your current Dopplr location.&lt;/dd&gt;
&lt;/dl&gt;

&lt;h3&gt;Cron&lt;/h3&gt;

&lt;p&gt;Almost done. We can now create a new map whenever we want on any given date. Let's run this script each night at 4 in the morning. Add the following line to your crontab:&lt;/p&gt;

&lt;p&gt;&lt;samp&gt;0 4 * * * cd &lt;strong&gt;&amp;lt;app_path&amp;gt;&lt;/strong&gt; &amp;amp;&amp;amp; /usr/local/bin/rake map:update RAILS_ENV=production&lt;/samp&gt;&lt;/p&gt;

&lt;p&gt;That's it. The script will now look for your current location in your Dopplr account and create a map based on that location.&lt;/p&gt;</content>
    <created-at type="datetime">2008-10-28T18:06:58+01:00</created-at>
    <id type="integer">40</id>
    <location-id type="integer">8</location-id>
    <permalink>dopplr-to-openstreetmap</permalink>
    <title>From Dopplr to OpenStreetMap</title>
    <updated-at type="datetime">2008-10-28T18:13:36+01:00</updated-at>
  </post>
</posts>
