Posty

Simple geolocation gem that stores the database locally.

Getting started

gem install posty

To download mySociety’s mirror of the Code-Point Open in WGS84 format, and create a database from it, and store that inside the gem (you may not need the sudo):

sudo posty-init -g

This takes about 8 minutes for me, and leaves me with a database of approximately 157M.

If you don’t want the database being stored in your gem, run something like this instead:

posty-init -d database.sqlite3

Then, in Ruby:

require 'posty'
posty = Posty.new
postcode = posty.postcode('SE1 1EN') # space is optional, as is capitalisation

p postcode.postcode
p [postcode.latitude, postcode.longitude]
p postcode.county

If the database isn’t in the gem, you’ll need to tell it where to find it:

posty = Posty.new('database.sqlite3')

To make things even easier, there’s a class method too (which accepts the database as the second parameter):

Posty.postcode('SE1 1EN')

Notes

The table contains the following columns, extracted from the WGS84 edition of Code-Point Open:

  • postcode

  • quality

  • latitude

  • longitude

  • country

  • nhs_region

  • nhs_health_authority

  • county

  • district

  • ward

It does not contain eastings and northings.