eCourier RSS Feed eCourier News RSS Feed

At eCourier we move fast. Keep up to date with the latest eCourier news by subscribing to our RSS feed.
(What's this?)

In the news

eCourier News Stop the press and read some happy articles all about yours truly.
Technology Overview Aiba API

Direction analysis

In this example we'll learn how to plot a diagram showing what directions couriers travelled in:


This example plots a crude radar chart of the direction couriers travel in. North is up, East is right etc as in a compass. The goal isn't perfect information visualisation but to provide a jumping point for you to perfect it.

You may expect that in a loosely gridded city like London, couriers may travel along those grid lines. The above graphic suggests this may be the case but is by no means conclusive - it's taken from one hour of data. This example assumes basic knowledge of the UNIX environment and ruby. Let's see how it works:
#!/usr/bin/ruby

require 'xml/libxml'
require 'RMagick'
Start the ruby interpreter and load in a XML library (libxml) to parse the data and the ruby image magick library to draw the image.
xml = `curl -s 'http://api.ecourier.co.uk/xmlapi/0.1/API_KEY/PASSWORD/courier/time_range/2006-08-22%2010:00/2006-08-22%2012:00'`

p = XML::Parser.new
p.string = xml
doc = p.parse
Use curl to load some XML data in to a string. Load this an XML object and parse it so that we can iterate over the data.
buckets = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]

doc.find('courier').each do |courier|
  i = (courier['heading'].to_f % 24).to_i
  buckets[i] = buckets[i] + 1
end
Go through all the data and put the direction of the courier in to a bucket 15 degrees wide (24 buckets on a circle).
biggest = 0

24.times do |i|
  biggest = buckets[i] if buckets[i] > biggest
end
Figure out what the biggest bucket size is so that we can scale all results by this later
canvas = Magick::Image.new(400,400)
gc = Magick::Draw.new
gc.stroke('black')
gc.stroke_width(3)
Create a new image 400x400 pixels across. Make a graphics context to draw on with a black pen 3 pixels wide
24.times do |i|
  buckets[i] = buckets[i].to_f / biggest
  angle = ((i * 15) + 7.5)
  x = buckets[i] * 200 * Math.sin(angle * 3.1415 / 180)
  y = buckets[i] * 200 * Math.cos(angle * 3.1415 / 180)
  gc.line(200,200, 200 + x, 200 + y)
end
For each bucket: Scale the bucket size, find the angle to draw the line, find the end point of the line. Finally draw a line from the center out to that end point.
gc.draw(canvas)
canvas.write('courier_directions.jpg')
Draw the context on to the image and save it with the given filename.