London Cycle Hire Map - now with live data

We wrote last week about the launch of Barclays Cycle Hire, London’s first public bike rental scheme, and produced a map using data published by the London Datastore, along with some ideas on usability compared with the official map of the docking stations.

The achillesĀ heel of this effort, of course, was the lack of real time location and availability data. While an official API is no doubt on the way, Adrian Short has just published his unofficial Boris Bikes API that loads the official map and then scrapes its status data. We’ve used Adrian’s clever parsing of the official map page to drop live data into a second version of our map, which you can see below. The new map still uses v2 of the Google Maps API, so it won’t play too nicely on iPhones and Androids, but adding the live data’s a useful step. Let us know what you think…

Source Code

The geo.me platform is written in Rails, so we added a controller method that downloads the official map page, uses Adrian’s regex to parse the data, then returns the data in JSON format. Here it is:

def boris_cycle_data
	# call the official map and parse the live availability date
	# inspired by http://github.com/adrianshort/borisapi
	uri = URI.parse("https://web.barclayscyclehire.tfl.gov.uk/maps")
	http = Net::HTTP.new(uri.host, uri.port)
	http.use_ssl = true
	http.verify_mode = OpenSSL::SSL::VERIFY_NONE
	request = Net::HTTP::Get.new(uri.request_uri)
	response = http.request(request)
	contents = response.body

	# Adrian's part...
	regexp = /\{id:"(\d+)".+?name:"(.+?)".+?lat:"(.+?)".+?long:"(.+?)".+?nbBikes:"(\d+)".+?nbEmptyDocks:"(\d+)".+?installed:"(.+?)".+?locked:"(.+?)".+?temporary:"(.+?)"\}/
	hour = /var hour='(\d\d:\d\d)'/.match(contents)
	matches = contents.scan(regexp)
	timestamp = hour[0].split('\'').second

	# now process the data, building an array of returned JSON records...
	hirelocations = Array.new
	for station in matches

		# collect attributes from scraped data
		id = station[0].to_s
		location = station[1]
		lat = station[2].to_f
		lng = station[3].to_f
		bikes_available = station[4].to_i
		capacity = bikes_available + station[5].to_i

		# let's only list operational sites
		if station[6] == "true"
			hirelocations.push({
				:id => id,
				:location => location,
				:lat => lat,
				:lng => lng,
				:bikes_available => bikes_available,
				:capacity => capacity,
				:timestamp => timestamp
			})
		end

	end

	# create our returned JSON data
	result={ :data=>hirelocations }
	render :text=>result.to_json
end

5 comments to London Cycle Hire Map – now with live data

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>