Posty – our new gem for geolocation in Ruby

Just last week Ordnance Survey released a tonne of data. This is of course a wonderful thing for everybody, and software making use of this data has only started appearing in the past week. This morning, in fact, we noticed a gem, pat by Stef Lewandowski which uses Pezholio/Stuart Harrison’s very useful UK Postcodes.

We took a look at it, and decided to make a gem that uses a local database (usually a bit faster than using a Web-based API), but which is similarly easy to use.

Three hours later, we present to you posty!

The data it provides from CodePoint Open is:

  • postcode
  • quality
  • latitude
  • longitude
  • country
  • nhs_region
  • nhs_health_authority
  • county
  • district
  • ward

In all these cases, rather than proving the name of the county/district/ward/etc, it only provides the identifier used in the data set. This is a case where UK Postcodes would be a better option.

To install it,

sudo gem install posty

To set up the database (which can be downloaded automatically from mySociety, or you can download the latest version yourself and use the -c option),

sudo posty-init -g

This should take about 8 minutes to load all the CSV into the database.

To use it,

require 'posty'
Posty.postcode('SE1 1EN')

If you’re going to be using it frequently from the same process, you should probably use the following instead,

require 'posty'
posty = Posty.new
postcode = posty.postcode('SE1 1EN')
postcode = posty.postcode('SE1 7PB')

The magick going on here is that the database is stored in the gem (in lib/posty/codepointopen.sqlite3), and then it’s available without having to connect manually.

Have a fiddle with posty and tell us what you think.