<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>a work on process &#187; bus routes</title>
	<atom:link href="http://jystewart.net/process/tag/bus-routes/feed/" rel="self" type="application/rss+xml" />
	<link>http://jystewart.net/process</link>
	<description>notes from another web developer</description>
	<lastBuildDate>Tue, 27 Jul 2010 09:53:43 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Corrected bus routes on Rails</title>
		<link>http://jystewart.net/process/2006/09/corrected-bus-routes-on-rails/</link>
		<comments>http://jystewart.net/process/2006/09/corrected-bus-routes-on-rails/#comments</comments>
		<pubDate>Tue, 26 Sep 2006 13:08:03 +0000</pubDate>
		<dc:creator>James Stewart</dc:creator>
				<category><![CDATA[Commentary]]></category>
		<category><![CDATA[bus routes]]></category>
		<category><![CDATA[hpricot]]></category>
		<category><![CDATA[REST]]></category>
		<category><![CDATA[ruby on rails]]></category>
		<category><![CDATA[screen scraping]]></category>
		<category><![CDATA[the rapid]]></category>

		<guid isPermaLink="false">http://jystewart.net/process/archives/2006/09/corrected-bus-routes-on-rails/</guid>
		<description><![CDATA[In the process of building my bus route app, I realised that half the data for bus stops is missing. While the site&#8217;s developers have done a good job of providing clear data on half the stops, if you want to see stops going in the other direction, you have to use a drop-down box <a href="http://jystewart.net/process/2006/09/corrected-bus-routes-on-rails/" class="more-link">More &#62;</a>]]></description>
			<content:encoded><![CDATA[<p>In the process of building my bus route app, I realised that half the data for bus stops is missing. While the site&#8217;s developers have done a good job of providing clear data on half the stops, if you want to see stops going in the other direction, you have to use a drop-down box that triggers an AJAX request and repopulates the table.</p>
<p>A little digging shows that the call is to:</p>
<p>http://www.ridetherapid.org/includes/ajax_return.php?mode=routestops&#038;direction={direction}>&#038;routeID={routeid}</p>
<p>which returns an HTML table with the relevant stop data. So in a sense, there are permalinks for each set of stops, but it&#8217;d be nice if they were more clearly advertised, particularly since the site as is won&#8217;t work for those without javascript switched on.</p>
<p>The other gotcha is that it seems the internal IDs for some routes don&#8217;t match their route numbers. If you try and retrieve the westbound stops for Route #14 the call is actually to:</p>
<p><a href="http://www.ridetherapid.org/includes/ajax_return.php?mode=routestops&#038;direction=W&#038;routeID=13">http://www.ridetherapid.org/includes/ajax_return.php?mode=routestops&#038;direction=W&#038;routeID=13</a></p>
<p>and when you make requests for route 13, the routeID passed is 14. The same disparity continues, suggesting that they&#8217;ve (sensibly) added primary keys to their database other than the route number. It turns out that ID is embedded in the markup within a comment showing the direction and the ID. For Route #50 that is:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&amp;lt;div id=&quot;stopListWrapper&quot;&gt;
&amp;lt;!-- E -&gt; 19 --&gt;
&amp;lt;div id=&quot;stopList&quot;&gt;
...
&amp;lt;/div&gt;
&amp;lt;/div&gt;</pre></div></div>

<p>Since the document is already being parsed using <a href="http://code.whytheluckystiff.net/hpricot/">hpricot</a>, we can get that with:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">internal_route_id = doc.<span style="color:#9900CC;">at</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;div#stopListWrapper&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">children</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">to_s</span>.<span style="color:#9900CC;">match</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">/</span>\<span style="color:#006600; font-weight:bold;">-</span>\<span style="color:#006600; font-weight:bold;">&gt;</span> <span style="color:#006600; font-weight:bold;">&#40;</span>\d<span style="color:#006600; font-weight:bold;">+</span><span style="color:#006600; font-weight:bold;">&#41;</span> \<span style="color:#006600; font-weight:bold;">-</span>\<span style="color:#006600; font-weight:bold;">-</span>\<span style="color:#006600; font-weight:bold;">&gt;/</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span></pre></div></div>

<p>(get the div, note that the comment is the second child, and get the data with a regular expression)</p>
<p>I&#8217;ve updated my scraper and the service to grab data based on the correct IDs. The HTML views will follow suit shortly.</p>
]]></content:encoded>
			<wfw:commentRss>http://jystewart.net/process/2006/09/corrected-bus-routes-on-rails/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Bus routes on Rails</title>
		<link>http://jystewart.net/process/2006/09/bus-routes-on-rails/</link>
		<comments>http://jystewart.net/process/2006/09/bus-routes-on-rails/#comments</comments>
		<pubDate>Mon, 25 Sep 2006 22:51:59 +0000</pubDate>
		<dc:creator>James Stewart</dc:creator>
				<category><![CDATA[Notes]]></category>
		<category><![CDATA[activeresource]]></category>
		<category><![CDATA[bus routes]]></category>
		<category><![CDATA[nested routes]]></category>
		<category><![CDATA[REST]]></category>
		<category><![CDATA[ruby on rails]]></category>
		<category><![CDATA[screen scraping]]></category>
		<category><![CDATA[the rapid]]></category>
		<category><![CDATA[Web Services]]></category>

		<guid isPermaLink="false">http://jystewart.net/process/archives/2006/09/bus-routes-on-rails/</guid>
		<description><![CDATA[Following on from my previous entry about scraping bus route data from The Rapid&#8217;s website, and to begin to demonstrate the possibilities it opens up, I&#8217;ve set up a simple web service to provide route and stop data. It&#8217;s based on the new REST style from Edge Rails, and routes are scoped by city to <a href="http://jystewart.net/process/2006/09/bus-routes-on-rails/" class="more-link">More &#62;</a>]]></description>
			<content:encoded><![CDATA[<p>Following on from <a href="http://jystewart.net/process/archives/2006/09/scraping-grand-rapids-bus-routes/">my previous entry</a> about scraping bus route data from The Rapid&#8217;s website, and to begin to demonstrate the possibilities it opens up, I&#8217;ve set up a simple web service to provide route and stop data. It&#8217;s based on the new REST style from Edge Rails, and routes are scoped by city to allow for future expansion. To get data on Route 1, GET:</p>
<p><a href="http://projects.jystewart.net/buses/cities/1/routes/1">http://projects.jystewart.net/buses/cities/1/routes/1</a></p>
<p>To get a list of the stops within 1.5 miles of a given longitude and latitude, GET:</p>
<p>http://projects.jystewart.net/buses/cities/1/stops/?longitude=X&#038;latitude=Y&#038;distance=1.5</p>
<p>Using Edge Rails, setting up the application was remarkably simple. Three models, three controllers, appropriate use of respond_to blocks, and the right entries in config/routes.rb:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">map.<span style="color:#9900CC;">resources</span> <span style="color:#ff3333; font-weight:bold;">:cities</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>cities<span style="color:#006600; font-weight:bold;">|</span> 
  cities.<span style="color:#9900CC;">resources</span> <span style="color:#ff3333; font-weight:bold;">:stops</span>
  cities.<span style="color:#9900CC;">resources</span> <span style="color:#ff3333; font-weight:bold;">:routes</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>This was the first time I&#8217;ve used nested routes so it took a few minutes to work out the correct syntax for the link_to calls. When using nested routes like those above, you must declare first the ID of the city and then the ID of the stop or route, eg:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#006600; font-weight:bold;">%</span>= link_to <span style="color:#996600;">'My Route'</span>, route_url<span style="color:#006600; font-weight:bold;">&#40;</span>city, route<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">%&gt;</span></pre></div></div>

<p>I&#8217;m not making any guarantees about the long term availability of the service, but if anyone wants to make use of it, let me know and we can probably work something out. I&#8217;ll probably be making use of it myself.</p>
]]></content:encoded>
			<wfw:commentRss>http://jystewart.net/process/2006/09/bus-routes-on-rails/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Scraping Grand Rapids bus routes</title>
		<link>http://jystewart.net/process/2006/09/scraping-grand-rapids-bus-routes/</link>
		<comments>http://jystewart.net/process/2006/09/scraping-grand-rapids-bus-routes/#comments</comments>
		<pubDate>Mon, 25 Sep 2006 11:49:11 +0000</pubDate>
		<dc:creator>James Stewart</dc:creator>
				<category><![CDATA[Commentary]]></category>
		<category><![CDATA[bus routes]]></category>
		<category><![CDATA[Grand Rapids]]></category>
		<category><![CDATA[hpricot]]></category>
		<category><![CDATA[open data]]></category>
		<category><![CDATA[ruby on rails]]></category>
		<category><![CDATA[screen scraping]]></category>
		<category><![CDATA[the rapid]]></category>

		<guid isPermaLink="false">http://jystewart.net/process/archives/2006/09/scraping-grand-rapids-bus-routes/</guid>
		<description><![CDATA[The Rapid, the bus service for Grand Rapids and surrounding areas, recently redesigned their website. The redesign was long overdue and the result certainly looks a lot cleaner, if still far from inspiring. They&#8217;ve added a flash-based map showing their routes (though it could do with being a little larger on the page) and added <a href="http://jystewart.net/process/2006/09/scraping-grand-rapids-bus-routes/" class="more-link">More &#62;</a>]]></description>
			<content:encoded><![CDATA[<p>The Rapid, the bus service for Grand Rapids and surrounding areas, recently redesigned their website. The redesign was long overdue and the result certainly looks a lot cleaner, if still far from inspiring. They&#8217;ve added <a href="http://www.ridetherapid.org/ride/routes/">a flash-based map</a> showing their routes (though it could do with being a little larger on the page) and added PDF maps of each route (eg. <a href="http://www.ridetherapid.org/fileDisplay/index.php?routeFile=1158161765_route6.pdf)">this one for Route 6</a>). Unfortunately as yet there&#8217;s no tool for working out routes, but that&#8217;s not a big surprise.</p>
<p>My favourite features, however, are not any of those mentioned above but the fact that each route now has a clean URL (eg. <a href="http://www.ridetherapid.org/ride/routes/6/">http://www.ridetherapid.org/ride/routes/6/</a>) and a link to google maps for each stop, thereby exposing the coordinates of all the stops. With those two components in place, it becomes very easy to pull out the route data and begin to apply it to other uses. A ruby script (using _why&#8217;s excellent <a href="http://code.whytheluckystiff.net/hpricot/">hpricot</a>) to do just that would be:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;">#!/usr/bin/env ruby</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'rubygems'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'open-uri'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'hpricot'</span>
&nbsp;
routes = <span style="color:#006600; font-weight:bold;">&#40;</span>1..15<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">to_a</span>.<span style="color:#9900CC;">concat</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">24</span>,<span style="color:#006666;">28</span>,<span style="color:#006666;">37</span>,<span style="color:#006666;">44</span>,<span style="color:#006666;">49</span>,<span style="color:#006666;">50</span>,<span style="color:#006666;">51</span><span style="color:#006600; font-weight:bold;">&#93;</span>
&nbsp;
routes.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>route<span style="color:#006600; font-weight:bold;">|</span>
  <span style="color:#9966CC; font-weight:bold;">begin</span>
    route_uri = <span style="color:#996600;">&quot;http://www.ridetherapid.org/ride/routes/#{route}/stops/&quot;</span>
    doc = Hpricot<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0066; font-weight:bold;">open</span><span style="color:#006600; font-weight:bold;">&#40;</span>route_uri<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    title = doc.<span style="color:#9900CC;">at</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;h1&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">children</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">to_s</span>.<span style="color:#9900CC;">strip</span>
    <span style="color:#CC0066; font-weight:bold;">puts</span> title
    stopList = doc.<span style="color:#9900CC;">search</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;div#stopList table tr&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#006600; font-weight:bold;">&#40;</span>1..<span style="color:#9900CC;">stopList</span>.<span style="color:#9900CC;">size</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>row<span style="color:#006600; font-weight:bold;">|</span>
      <span style="color:#9966CC; font-weight:bold;">unless</span> stopList<span style="color:#006600; font-weight:bold;">&#91;</span>row<span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#0000FF; font-weight:bold;">nil</span>?
        uri = stopList<span style="color:#006600; font-weight:bold;">&#91;</span>row<span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">at</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;a&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">attributes</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'href'</span><span style="color:#006600; font-weight:bold;">&#93;</span>
        name = stopList<span style="color:#006600; font-weight:bold;">&#91;</span>row<span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">at</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;a&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">children</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">to_s</span>
        coords = uri.<span style="color:#9900CC;">match</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">/</span>q=<span style="color:#006600; font-weight:bold;">&#40;</span>\d<span style="color:#006600; font-weight:bold;">+</span>\.\d<span style="color:#006600; font-weight:bold;">+</span><span style="color:#006600; font-weight:bold;">&#41;</span>,\<span style="color:#006600; font-weight:bold;">+</span><span style="color:#006600; font-weight:bold;">&#40;</span>.<span style="color:#006600; font-weight:bold;">*</span>?<span style="color:#006600; font-weight:bold;">&#41;</span>\<span style="color:#006600; font-weight:bold;">&amp;/</span><span style="color:#006600; font-weight:bold;">&#41;</span>
        <span style="color:#9966CC; font-weight:bold;">if</span> coords.<span style="color:#9966CC; font-weight:bold;">class</span> == <span style="color:#CC00FF; font-weight:bold;">MatchData</span>
          latitude = coords<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span>
          longitude = coords<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">2</span><span style="color:#006600; font-weight:bold;">&#93;</span>
          <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;#{name} on route #{route} is at #{latitude}, #{longitude}&quot;</span>
        <span style="color:#9966CC; font-weight:bold;">end</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">rescue</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> err
    <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Problem retrieving #{route_uri}&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>With this data available, it immediately becomes possible for local people and organizations to make use of it in a variety of ways&#8211;businesses could easily show the nearest bus stops to their locations, listing services can help visitors plan their routes, and those of us who aren&#8217;t fans of the flash-map could use other services to build alternatives.</p>
]]></content:encoded>
			<wfw:commentRss>http://jystewart.net/process/2006/09/scraping-grand-rapids-bus-routes/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
