title: GTFS/NextBus/Google Maps - transit distance traveled
body: <p>I am trying to get the distance traveled on a transit route -- particularly San Francisco MUNI, but the standards <a href="http://www.google.com/url?sa=t&amp;rct=j&amp;q=&amp;esrc=s&amp;source=web&amp;cd=1&amp;cad=rja&amp;ved=0CCIQFjAA&amp;url=http%3A%2F%2Fwww.nextbus.com%2FxmlFeedDocs%2FNextBusXMLFeed.pdf&amp;ei=wGE9UPWrIcWqigLr1YHwCw&amp;usg=AFQjCNGCmOEwCi7sz4VOAdTWCSjoWm4r7Q" rel="nofollow">NextBus</a>, <a href="https://developers.google.com/transit/gtfs/reference#shapes_fields" rel="nofollow">GTFS, and <a href="https://developers.google.com/maps/documentation/directions/" rel="nofollow">Google Maps API</a> appear to be universal. I'm comfortable using any of these APIs, I'm just not sure how to go about this problem.  </p>
  
  <p><b>The easy way - ask Google Maps</b> (this using webservices, but there is also the javascript API):</p>
  
  <p><a href="http://maps.googleapis.com/maps/api/directions/json?origin=37.7954199,-122.397&amp;destination=37.7873299,-122.44691&amp;sensor=false&amp;mode=transit&amp;departure_time=1348109609&amp;alternatives=true" rel="nofollow">http://maps.googleapis.com/maps/api/directions/json?origin=37.7954199,-122.397&amp;destination=37.7873299,-122.44691&amp;sensor=false&amp;mode=transit&amp;departure_time=1348109609&amp;alternatives=true</a></p>
  
  <p>this JSON includes distance traveled, but there are two issues:</p>
  
  <ol>
  <li>Google does not allow you to use this data unless you're displaying a map, which I don't want to do</li>
  <li>I would need to ensure that the distance returned is for the correct route/line, since it can/will give multiple routing options. This is probably doable but would require more logic. </li>
  </ol>
  
  <p><b>EDIT:</b> using <code>alternatives=true</code> (or <code>provideRouteAlternatives: true</code> using the javascript API) only returns a maximum of 3 routes, which here in SF often doesn't include the route I'm looking for (other transit agencies, multiple lines on the same route, etc). So this isn't such a great option. </p>
  
  <p><b>NextBus:</b></p>
  
  <p>example route config:</p>
  
  <p><a href="http://webservices.nextbus.com/service/publicXMLFeed?command=routeConfig&amp;a=sf-muni&amp;r=1" rel="nofollow">http://webservices.nextbus.com/service/publicXMLFeed?command=routeConfig&amp;a=sf-muni&amp;r=1</a></p>
  
  <p>The coordinates for each stop are given, but connecting the dots on those is not the same as the route taken -- it will cut corners, etc, and I need this to be accurate. The actual route taken is given under <code>&lt;path&gt;/&lt;point&gt;</code>, but I don't see any obvious correlation between stop and path coordinates. Plus, NextBus says in their documentation</a> (p.10 near the bottom) that you should NOT connect points between <code>&lt;path&gt;</code> segments, they're only meant for drawing on a map and can overlap. </p>
  
  <p><b>GTFS:</b></p>
  
  <p>The GTFS data also separates stop and "shape" coordinates (like NextBus paths). Unfortunately, the coordinates are slightly different for the same stops between NextBus and GTFS (rounding), though the stop ID/tags are the same. Also, the data files are in the megabytes, and I need to use this for a mobile app. I suppose I could put all the data in a database and query that, but that still leaves figuring out how to correlate the stops with the shape. The "shapes_distance_traveled" column in the shapes.txt file is especially promising. MUNI chooses to leave the optional "shapes_distance_traveled" field out of stop_times.txt, though. </p>
  
  <p>Any advice would be appreciated, I understand this seems like an epic task to get a simple value. Maybe I'll just throw a map in to legitimately use the distance :)</p>