geogov 0.0.7 → 0.0.8
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/geogov.rb +5 -18
- data/lib/geogov/geo_stack.rb +12 -53
- data/lib/geogov/providers/mapit.rb +8 -19
- data/test/geogov_test.rb +1 -48
- metadata +31 -44
data/lib/geogov.rb
CHANGED
@@ -20,15 +20,15 @@ module Geogov
|
|
20
20
|
raise ArgumentError.new("#{instance.class} doesn't respond to #{method}")
|
21
21
|
end
|
22
22
|
|
23
|
-
|
23
|
+
caching_instance = SimpleCache.new(instance)
|
24
24
|
@@methods ||= {}
|
25
|
-
@@methods[method] =
|
25
|
+
@@methods[method] = caching_instance
|
26
26
|
|
27
27
|
unless self.methods().include?(method)
|
28
28
|
dispatcher = <<-EOS
|
29
|
-
def #{method}(*args, &block)
|
30
|
-
@@methods[:#{method}].__send__(#{method.inspect}, *args, &block)
|
31
|
-
end
|
29
|
+
def #{method}(*args, &block)
|
30
|
+
@@methods[:#{method}].__send__(#{method.inspect}, *args, &block)
|
31
|
+
end
|
32
32
|
EOS
|
33
33
|
module_eval(dispatcher)
|
34
34
|
end
|
@@ -38,16 +38,6 @@ module Geogov
|
|
38
38
|
yield self
|
39
39
|
end
|
40
40
|
|
41
|
-
def lat_lon_from_postcode(postcode)
|
42
|
-
result = Mapit.new().lat_lon_from_postcode(postcode)
|
43
|
-
|
44
|
-
if result.nil?
|
45
|
-
raise UnrecognizedLocationError
|
46
|
-
end
|
47
|
-
|
48
|
-
result
|
49
|
-
end
|
50
|
-
|
51
41
|
provider_for :nearest_place_name, DracosGazetteer.new()
|
52
42
|
|
53
43
|
provider_for :lat_lon_to_country, Geonames.new()
|
@@ -65,6 +55,3 @@ module Geogov
|
|
65
55
|
extend self
|
66
56
|
|
67
57
|
end
|
68
|
-
|
69
|
-
|
70
|
-
|
data/lib/geogov/geo_stack.rb
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
module Geogov
|
2
2
|
class GeoStack
|
3
3
|
|
4
|
-
attr_accessor :ward, :council, :nation, :country, :
|
5
|
-
attr_reader :postcode
|
4
|
+
attr_accessor :ward, :council, :nation, :country, :wmc, :lat, :lon, :friendly_name
|
5
|
+
attr_reader :postcode, :fuzzy_point
|
6
6
|
|
7
7
|
def initialize(&block)
|
8
8
|
if block_given?
|
@@ -58,10 +58,13 @@ module Geogov
|
|
58
58
|
{
|
59
59
|
:fuzzy_point => self.fuzzy_point.to_hash,
|
60
60
|
:postcode => self.postcode,
|
61
|
-
:council => self.council,
|
62
61
|
:ward => self.ward,
|
62
|
+
:council => self.council,
|
63
|
+
:nation => self.nation,
|
64
|
+
:country => self.country,
|
65
|
+
:wmc => self.wmc,
|
63
66
|
:friendly_name => self.friendly_name
|
64
|
-
}
|
67
|
+
}.select {|k,v| !(v.nil?) }
|
65
68
|
end
|
66
69
|
|
67
70
|
def update(hash)
|
@@ -76,52 +79,6 @@ module Geogov
|
|
76
79
|
empty.fuzzy_point = empty.calculate_fuzzy_point
|
77
80
|
end
|
78
81
|
end
|
79
|
-
|
80
|
-
def friendly_name
|
81
|
-
@friendly_name ||= build_locality
|
82
|
-
end
|
83
|
-
|
84
|
-
def has_authority?( type )
|
85
|
-
get_authority(type) ? true : false
|
86
|
-
end
|
87
|
-
|
88
|
-
def get_authority( type )
|
89
|
-
return false if self.authorities[type.upcase.to_sym] == true
|
90
|
-
self.authorities.nil? or self.authorities[type.upcase.to_sym].nil? ? false : self.authorities[type.upcase.to_sym]
|
91
|
-
end
|
92
|
-
|
93
|
-
def formatted_authority_name( type )
|
94
|
-
return false unless has_authority?(type)
|
95
|
-
name = get_authority(type)['name'].dup
|
96
|
-
|
97
|
-
name.sub!(/ *((District Council|Borough Council|Community|County Council|City Council|Council) ?)+/,'')
|
98
|
-
name.sub!(/ (North|East|South|West|Central)$/,'')
|
99
|
-
name.sub!(/Mid /,'')
|
100
|
-
|
101
|
-
name
|
102
|
-
end
|
103
|
-
|
104
|
-
def build_locality
|
105
|
-
return false unless self.authorities
|
106
|
-
|
107
|
-
case
|
108
|
-
when has_authority?('DIS') && has_authority?('CTY')
|
109
|
-
locality = ['DIS','CTY']
|
110
|
-
when has_authority?('LBO')
|
111
|
-
locality = ['LBO','London']
|
112
|
-
when has_authority?('UTA') && has_authority?('CPC') # for cornwall civil parishes
|
113
|
-
locality = ['CPC','UTA']
|
114
|
-
when has_authority?('UTA') && has_authority?('UTE')
|
115
|
-
locality = ['UTE','UTA']
|
116
|
-
when has_authority?('UTA') && has_authority?('UTW')
|
117
|
-
locality = ['UTW','UTA']
|
118
|
-
when has_authority?('MTW') && has_authority?('MTD')
|
119
|
-
locality = ['MTW','MTD']
|
120
|
-
else
|
121
|
-
return false
|
122
|
-
end
|
123
|
-
locality.map {|t| formatted_authority_name(t) || t }.uniq.join(', ')
|
124
|
-
end
|
125
82
|
|
126
83
|
def has_valid_lat_lon(hash)
|
127
84
|
return (hash['lon'] and hash['lat'] and hash['lon'] != "" and hash['lat'] != "")
|
@@ -133,12 +90,16 @@ module Geogov
|
|
133
90
|
fields = Geogov.areas_for_stack_from_postcode(postcode)
|
134
91
|
if fields
|
135
92
|
lat_lon = fields[:point]
|
93
|
+
if lat_lon
|
94
|
+
self.friendly_name = Geogov.nearest_place_name(lat_lon['lat'],lat_lon['lon'])
|
95
|
+
end
|
136
96
|
set_fields(fields.select {|k,v| k != :point})
|
137
97
|
end
|
138
98
|
end
|
139
99
|
end
|
140
100
|
|
141
101
|
def fetch_missing_fields_for_coords(lat, lon)
|
102
|
+
self.friendly_name = Geogov.nearest_place_name(lat, lon)
|
142
103
|
fields = Geogov.areas_for_stack_from_coords(lat, lon)
|
143
104
|
if ['England', 'Scotland', 'Northern Ireland', 'Wales'].include?(fields[:nation])
|
144
105
|
self.country = 'UK'
|
@@ -154,9 +115,7 @@ module Geogov
|
|
154
115
|
self.send(setter,value)
|
155
116
|
end
|
156
117
|
else
|
157
|
-
|
158
|
-
self.authorities[geo] = value
|
159
|
-
# raise ArgumentError, "geo type '#{geo}' is not a valid geo type"
|
118
|
+
raise ArgumentError, "geo type '#{geo}' is not a valid geo type"
|
160
119
|
end
|
161
120
|
end
|
162
121
|
self
|
@@ -51,10 +51,12 @@ module Geogov
|
|
51
51
|
def translate_area_type_to_shortcut(area_type)
|
52
52
|
if ['COP', 'LBW', 'LGE', 'MTW', 'UTE', 'UTW', 'DIW'].include?(area_type)
|
53
53
|
return 'ward'
|
54
|
-
elsif ['CTY', 'CED'
|
55
|
-
|
56
|
-
|
57
|
-
return '
|
54
|
+
elsif ['CTY', 'CED'].include?(area_type)
|
55
|
+
|
56
|
+
git
|
57
|
+
return 'council' # county
|
58
|
+
elsif ['DIS', 'LBO'].include?(area_type)
|
59
|
+
return 'council' # district
|
58
60
|
elsif area_type == 'WMC' # XXX Also maybe 'EUR', 'NIE', 'SPC', 'SPE', 'WAC', 'WAE', 'OLF', 'OLG', 'OMF', 'OMG')
|
59
61
|
return 'WMC'
|
60
62
|
end
|
@@ -63,12 +65,8 @@ module Geogov
|
|
63
65
|
def areas_for_stack_from_coords(lat, lon)
|
64
66
|
query = self.point("4326", [lon, lat])
|
65
67
|
results = {:point => {'lat' => lat, 'lon' => lon}}
|
66
|
-
councils = { }
|
67
|
-
|
68
68
|
query.each do |id, area_info|
|
69
|
-
type = area_info['type'].upcase.to_sym
|
70
69
|
level = translate_area_type_to_shortcut(area_info['type'])
|
71
|
-
|
72
70
|
if level
|
73
71
|
level = level.downcase.to_sym
|
74
72
|
results[level] = [] unless results[level]
|
@@ -77,24 +75,15 @@ module Geogov
|
|
77
75
|
results[level] << level_info
|
78
76
|
results[:nation] = area_info['country_name'] if results[:nation].nil?
|
79
77
|
end
|
80
|
-
|
81
|
-
councils[type] = { 'name' => area_info['name'], 'type' => area_info['type'], 'id' => area_info['id'] }
|
82
78
|
end
|
83
|
-
|
84
|
-
return councils.merge results
|
79
|
+
return results
|
85
80
|
end
|
86
81
|
|
87
82
|
def areas_for_stack_from_postcode(postcode)
|
88
83
|
query = self.postcode(postcode)
|
89
84
|
results = {}
|
90
85
|
|
91
|
-
if query && query['areas']
|
92
|
-
|
93
|
-
query['areas'].each do |i, area|
|
94
|
-
type = area['type'].to_sym
|
95
|
-
results[type] = area
|
96
|
-
end
|
97
|
-
|
86
|
+
if query && query['shortcuts'] && query['areas']
|
98
87
|
query['shortcuts'].each do |typ, i|
|
99
88
|
if i.is_a? Hash
|
100
89
|
ids = i.values()
|
data/test/geogov_test.rb
CHANGED
@@ -3,6 +3,7 @@ require 'test_helper'
|
|
3
3
|
class GovspeakTest < Test::Unit::TestCase
|
4
4
|
|
5
5
|
test "IP-located stack should have country" do
|
6
|
+
|
6
7
|
Geogov.configure do |g|
|
7
8
|
g.provider_for :centre_of_country, stub(:centre_of_country => {"lat"=>37, "lon"=>-96})
|
8
9
|
g.provider_for :remote_location, stub(:remote_location => {'country' => 'US'})
|
@@ -73,52 +74,4 @@ class GovspeakTest < Test::Unit::TestCase
|
|
73
74
|
assert_equal :country, stack.fuzzy_point.accuracy
|
74
75
|
end
|
75
76
|
|
76
|
-
test "stack with postcode returns correct locality" do
|
77
|
-
stack = Geogov::GeoStack.new
|
78
|
-
stack = stack.update( 'postcode' => "SW1A 1AA" )
|
79
|
-
|
80
|
-
assert_equal "Westminster, London", stack.friendly_name
|
81
|
-
end
|
82
|
-
|
83
|
-
test "stack with coordinates returns correct locality" do
|
84
|
-
stack = Geogov::GeoStack.new
|
85
|
-
stack = stack.update( 'lat' => "51.501009", "lon" => "-0.1415870" )
|
86
|
-
|
87
|
-
assert_equal "Westminster, London", stack.friendly_name
|
88
|
-
end
|
89
|
-
|
90
|
-
test "stack with postcode returns authorities" do
|
91
|
-
stack = Geogov::GeoStack.new
|
92
|
-
stack = stack.update( 'postcode' => "SW1A 1AA" )
|
93
|
-
|
94
|
-
assert stack.authorities.any?
|
95
|
-
end
|
96
|
-
|
97
|
-
test "stack with coordinates returns authorities" do
|
98
|
-
stack = Geogov::GeoStack.new
|
99
|
-
stack = stack.update( 'lat' => "51.501009", "lon" => "-0.1415870" )
|
100
|
-
|
101
|
-
assert stack.authorities.any?
|
102
|
-
end
|
103
|
-
|
104
|
-
test "stack with postcode returns correct locality, ward and council" do
|
105
|
-
stack = Geogov::GeoStack.new
|
106
|
-
stack = stack.update( 'postcode' => "SW1A 1AA" )
|
107
|
-
|
108
|
-
assert_equal "Westminster, London", stack.friendly_name
|
109
|
-
assert stack.ward and stack.ward.any?
|
110
|
-
assert stack.council and stack.council.any?
|
111
|
-
assert_equal "Westminster City Council", stack.council.first['name']
|
112
|
-
end
|
113
|
-
|
114
|
-
test "stack with coordinates returns correct locality, ward and council" do
|
115
|
-
stack = Geogov::GeoStack.new
|
116
|
-
stack = stack.update( 'lat' => "51.501009", "lon" => "-0.1415870" )
|
117
|
-
|
118
|
-
assert_equal "Westminster, London", stack.friendly_name
|
119
|
-
assert stack.ward and stack.ward.any?
|
120
|
-
assert stack.council and stack.council.any?
|
121
|
-
assert_equal "Westminster City Council", stack.council.first['name']
|
122
|
-
end
|
123
|
-
|
124
77
|
end
|
metadata
CHANGED
@@ -1,51 +1,47 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: geogov
|
3
|
-
version: !ruby/object:Gem::Version
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.8
|
4
5
|
prerelease:
|
5
|
-
version: 0.0.7
|
6
6
|
platform: ruby
|
7
|
-
authors:
|
7
|
+
authors:
|
8
8
|
- Ben Griffiths
|
9
9
|
- James Stewart
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
- !ruby/object:Gem::Dependency
|
13
|
+
date: 2012-01-30 00:00:00.000000000Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
17
16
|
name: rake
|
18
|
-
requirement: &
|
17
|
+
requirement: &70356750291540 !ruby/object:Gem::Requirement
|
19
18
|
none: false
|
20
|
-
requirements:
|
19
|
+
requirements:
|
21
20
|
- - ~>
|
22
|
-
- !ruby/object:Gem::Version
|
21
|
+
- !ruby/object:Gem::Version
|
23
22
|
version: 0.9.0
|
24
23
|
type: :development
|
25
24
|
prerelease: false
|
26
|
-
version_requirements: *
|
27
|
-
- !ruby/object:Gem::Dependency
|
25
|
+
version_requirements: *70356750291540
|
26
|
+
- !ruby/object:Gem::Dependency
|
28
27
|
name: mocha
|
29
|
-
requirement: &
|
28
|
+
requirement: &70356750291060 !ruby/object:Gem::Requirement
|
30
29
|
none: false
|
31
|
-
requirements:
|
30
|
+
requirements:
|
32
31
|
- - ~>
|
33
|
-
- !ruby/object:Gem::Version
|
32
|
+
- !ruby/object:Gem::Version
|
34
33
|
version: 0.9.0
|
35
34
|
type: :development
|
36
35
|
prerelease: false
|
37
|
-
version_requirements: *
|
36
|
+
version_requirements: *70356750291060
|
38
37
|
description: Geolocation and utilities for UK Government single domain
|
39
|
-
email:
|
38
|
+
email:
|
40
39
|
- ben@alphagov.co.uk
|
41
40
|
- jystewart@gmail.com
|
42
41
|
executables: []
|
43
|
-
|
44
42
|
extensions: []
|
45
|
-
|
46
43
|
extra_rdoc_files: []
|
47
|
-
|
48
|
-
files:
|
44
|
+
files:
|
49
45
|
- lib/geogov/fuzzy_point.rb
|
50
46
|
- lib/geogov/geo_stack.rb
|
51
47
|
- lib/geogov/providers/dracos_gazetteer.rb
|
@@ -62,37 +58,28 @@ files:
|
|
62
58
|
- test/test_helper.rb
|
63
59
|
homepage: http://github.com/alphagov/geogov
|
64
60
|
licenses: []
|
65
|
-
|
66
61
|
post_install_message:
|
67
62
|
rdoc_options: []
|
68
|
-
|
69
|
-
require_paths:
|
63
|
+
require_paths:
|
70
64
|
- lib
|
71
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
65
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
72
66
|
none: false
|
73
|
-
requirements:
|
74
|
-
- -
|
75
|
-
- !ruby/object:Gem::Version
|
76
|
-
|
77
|
-
|
78
|
-
- 0
|
79
|
-
version: "0"
|
80
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
67
|
+
requirements:
|
68
|
+
- - ! '>='
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: '0'
|
71
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
81
72
|
none: false
|
82
|
-
requirements:
|
83
|
-
- -
|
84
|
-
- !ruby/object:Gem::Version
|
85
|
-
|
86
|
-
segments:
|
87
|
-
- 0
|
88
|
-
version: "0"
|
73
|
+
requirements:
|
74
|
+
- - ! '>='
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
89
77
|
requirements: []
|
90
|
-
|
91
78
|
rubyforge_project:
|
92
|
-
rubygems_version: 1.8.
|
79
|
+
rubygems_version: 1.8.10
|
93
80
|
signing_key:
|
94
81
|
specification_version: 3
|
95
82
|
summary: Geolocation and utilities for UK Government single domain
|
96
|
-
test_files:
|
83
|
+
test_files:
|
97
84
|
- test/geogov_test.rb
|
98
85
|
- test/test_helper.rb
|