Skip to content

Commit

Permalink
Merge pull request #1 from theodi/master
Browse files Browse the repository at this point in the history
Add support for other administrative areas
  • Loading branch information
craigw committed Sep 17, 2013
2 parents 1060a52 + 3b99558 commit 4c1b151
Show file tree
Hide file tree
Showing 21 changed files with 4,775 additions and 17 deletions.
3 changes: 3 additions & 0 deletions Gemfile
@@ -1,3 +1,6 @@
#ruby=1.9.3-p374
#ruby-gemset=mapit

source 'https://rubygems.org'

# Specify your gem's dependencies in my_society-map_it.gemspec
Expand Down
8 changes: 7 additions & 1 deletion README.md
Expand Up @@ -22,9 +22,15 @@ Or install it yourself as:

require 'my_society/map_it'
p = MySociety::MapIt::Postcode.new 'SE1 1EY'
p.two_tier? # => false
la = p.local_authority
la.name # => "Southwark Borough Council"


p = MySociety::MapIt::Postcode.new 'B46 3LD'
p.two_tier? # => true
la = p.local_authority
la[:district].name # => "North Warwickshire Borough Council"
la[:county].name # => "Warwickshire County Council"

## Contributing

Expand Down
9 changes: 7 additions & 2 deletions Rakefile
@@ -1,2 +1,7 @@
#!/usr/bin/env rake
require "bundler/gem_tasks"
$:.unshift File.join( File.dirname(__FILE__), "lib")

require 'rspec/core/rake_task'

RSpec::Core::RakeTask.new(:spec)

task :default => :spec
107 changes: 93 additions & 14 deletions lib/my_society/map_it.rb
Expand Up @@ -18,23 +18,84 @@ def to_hash
end

module LocalAuthorityFinder
LOCAL_AUTHORITY_TYPE_CODES = %w(
DIS MTD UTA LBO CTY LGD COI
).map { |c| c.freeze }.freeze
DISTRICT_TYPE_CODES = %w(
DIS LGD
).freeze

UNITARY_TYPE_CODES = %w(
UTA MTD LBO COI
).freeze

COUNTY_TYPE_CODES = %w(
CTY
).freeze

COUNTY_WARD_TYPE_CODES = %w(
CED
)

PARISH_TYPE_CODES = %w(
CPC COP
)

WARD_TYPE_CODES = %w(
DIW LBW LGW MTW UTW
)

def district
detect(DISTRICT_TYPE_CODES)
end

def parish
detect(PARISH_TYPE_CODES)
end

def county
return if district.nil?
detect(COUNTY_TYPE_CODES)
end

def county_ward
return if county.nil?
detect(COUNTY_WARD_TYPE_CODES)
end

def unitary
detect(UNITARY_TYPE_CODES)
end

def ward
detect(WARD_TYPE_CODES)
end

def local_authority
local_authority_info = to_point.to_hash.values.sort_by { |a|
LOCAL_AUTHORITY_TYPE_CODES.index(a['type']) || -1
}.detect do |la|
LOCAL_AUTHORITY_TYPE_CODES.include? la['type']
end

return if local_authority_info.nil?

LocalAuthority.new local_authority_info
if district.nil?
unitary
else
{
:district => district,
:county => county
}
end
rescue
nil
end

def detect(type)
la = to_point.to_hash.values.detect do |la|
type.include? la['type']
end

return if la.nil?

LocalAuthority.new la
rescue
nil
end

def two_tier?
local_authority.kind_of? Hash
end
end

class LocalAuthority
Expand All @@ -58,6 +119,14 @@ def name
def id
attributes['id']
end

def snac
attributes['codes']['ons'] rescue nil
end

def gss
attributes['codes']['gss'] rescue nil
end

def uri
[ MySociety::MapIt.base_url, 'area', id ].join '/'
Expand Down Expand Up @@ -88,7 +157,7 @@ def initialize x, y, coordinate_system = SYSTEM_WGS84
end

def uri
"#{MySociety::MapIt.base_url}/point/#{coordinate_system}/#{x},#{y}"
"#{MySociety::MapIt.base_url}/point/#{coordinate_system}/#{y},#{x}"
end

def to_point
Expand Down Expand Up @@ -117,7 +186,17 @@ def uri

def to_point
h = to_hash.dup
Point.new h['wgs84_lon'], h['wgs84_lat'], Point::SYSTEM_WGS84
Point.new h['wgs84_lat'], h['wgs84_lon'], Point::SYSTEM_WGS84
end

def easting_northing
h = to_hash.dup
if h['coordsyst'] == "G"
coordsyst = Point::SYSTEM_BRITISH_NATIONAL_GRID
else h['coordsyst'] == "I"
coordsyst = Point::SYSTEM_IRISH_NATIONAL_GRID
end
Point.new h['northing'], h['easting'], coordsyst
end
end
end
Expand Down
5 changes: 5 additions & 0 deletions my_society-map_it.gemspec
Expand Up @@ -14,4 +14,9 @@ Gem::Specification.new do |gem|
gem.name = "my_society-map_it"
gem.require_paths = ["lib"]
gem.version = MySociety::MapIt::VERSION

gem.add_development_dependency "rspec"
gem.add_development_dependency "simplecov-rcov"
gem.add_development_dependency "fakeweb"
gem.add_development_dependency "vcr"
end

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 4c1b151

Please sign in to comment.