Created
December 6, 2010 21:06
-
-
Save malclocke/730947 to your computer and use it in GitHub Desktop.
Get info on ChurChur Metro buses within a lat/long boundary
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'net/http' | |
require 'json' | |
require 'nokogiri' | |
require 'open-uri' | |
# Lat long boundaries | |
# | |
# Around the casino | |
#xmin=172.6299226284027 | |
#ymin=-43.52838892844327 | |
#xmax=172.6409089565277 | |
#ymax=-43.52533957646274 | |
# Home | |
xmin = 172.70836114883423 | |
ymin = -43.49851108428091 | |
xmax = 172.71934747695923 | |
ymax = -43.49546022227689 | |
params = { | |
'geometry' => "#{xmin},#{ymin},#{xmax},#{ymax}", | |
'geometryType' => 'esriGeometryEnvelope', | |
'inSR' => '4326', | |
'spatialRel' => 'esriSpatialRelIntersects', | |
'relationParam' => '', | |
'objectIds' => '', | |
'where' => '', | |
'time' => '', | |
'returnCountOnly' => 'false', | |
'returnIdsOnly' => 'false', | |
'returnGeometry' => 'true', | |
'maxAllowableOffset' => '', | |
'outSR' => '', | |
'outFields' => 'Name,PlatformTa,RoadName,PlatformNo,Routes,Lat,Long', | |
'f' => 'pjson' | |
} | |
# GET request for stop info: | |
# http://arcgis.ecan.govt.nz/ArcGIS/rest/services/Beta/Bus_Routes/MapServer/2/query?text=&geometry=172.6299226284027%2C-43.52838892844327%2C172.6409089565277%2C-43.52533957646274&geometryType=esriGeometryEnvelope&inSR=4326&spatialRel=esriSpatialRelIntersects&relationParam=&objectIds=&where=&time=&returnCountOnly=false&returnIdsOnly=false&returnGeometry=true&maxAllowableOffset=&outSR=&outFields=Name%2CPlatformTa%2CRoadName%2CPlatformNo%2CRoutes&f=pjson | |
res = Net::HTTP.post_form(URI.parse('http://arcgis.ecan.govt.nz/ArcGIS/rest/services/Beta/Bus_Routes/MapServer/2/query'), params) | |
json = JSON.parse(res.body) | |
#puts json.inspect | |
json['features'].each do |stop| | |
attr = stop['attributes'] | |
loc = stop['geometry'] | |
puts "#{attr['Name']} - #{attr['RoadName']} (#{attr['PlatformNo']} = #{attr['PlatformTa']})" | |
puts " #{attr['Routes']}" | |
puts " latlong=#{attr['Lat']},#{attr['Long']}" | |
# Get the real time arrival info | |
#res = Net::HTTP.get(URI.parse("http://rtt.metroinfo.org.nz/RTT/Public/RoutePositionET.aspx?MaxETRows=10&PlatformTag=#{attr['PlatformTa']}")) | |
#puts res | |
doc = Nokogiri::HTML(open("http://rtt.metroinfo.org.nz/RTT/Public/RoutePositionET.aspx?MaxETRows=10&PlatformTag=#{attr['PlatformTa']}")) | |
fmt = "%7s | %-40s | %3s" | |
puts fmt % ['Route', 'Destination', 'ETA'] | |
puts '--------+------------------------------------------+----' | |
doc.css('#divXML table.tableET tbody tr').each do |tr| | |
route,dest,eta = tr.css('td') | |
if route and dest and eta | |
puts fmt % [route.content,dest.content,eta.content] | |
end | |
end | |
puts | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment