Class: Geogov::Mapit
- Inherits:
-
Object
show all
- Defined in:
- lib/geogov/providers/mapit.rb
Defined Under Namespace
Classes: Method
Instance Method Summary
collapse
Constructor Details
#initialize(default_url = "http://mapit.mysociety.org") ⇒ Mapit
Returns a new instance of Mapit.
32
33
34
|
# File 'lib/geogov/providers/mapit.rb', line 32
def initialize(default_url = "http://mapit.mysociety.org")
@base = default_url
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
117
118
119
120
121
122
123
|
# File 'lib/geogov/providers/mapit.rb', line 117
def method_missing(method, *args, &block)
if valid_mapit_methods.include?(method)
Mapit::Method.new(method.to_s, args).call(@base)
else
super(method, *args, &block)
end
end
|
Instance Method Details
#areas_for_stack_from_coords(lat, lon) ⇒ Object
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
# File 'lib/geogov/providers/mapit.rb', line 65
def areas_for_stack_from_coords(lat, lon)
query = self.point("4326", [lon, lat])
results = {:point => {'lat' => lat, 'lon' => lon}}
query.each do |id, area_info|
level = translate_area_type_to_shortcut(area_info['type'])
if level
level = level.downcase.to_sym
results[level] = [] unless results[level]
level_info = area_info.select { |k, v| ["name", "id", "type"].include?(k) }
level_info['ons'] = area_info['codes']['ons'] if area_info['codes'] && area_info['codes']['ons']
results[level] << level_info
results[:nation] = area_info['country_name'] if results[:nation].nil?
end
end
return results
end
|
#areas_for_stack_from_postcode(postcode) ⇒ Object
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
# File 'lib/geogov/providers/mapit.rb', line 82
def areas_for_stack_from_postcode(postcode)
query = self.postcode(postcode)
results = {}
if query && query['shortcuts'] && query['areas']
query['shortcuts'].each do |typ, i|
if i.is_a? Hash
ids = i.values()
else
ids = [i]
end
ids.each do |id|
area_info = query['areas'][id.to_s]
level = typ.downcase.to_sym
results[level] = [] unless results[level]
level_info = area_info.select { |k, v| ["name", "id", "type"].include?(k) }
level_info['ons'] = area_info['codes']['ons'] if area_info['codes'] && area_info['codes']['ons']
results[level] << level_info
results[:nation] = area_info['country_name'] if results[:nation].nil?
end
end
lat, lon = query['wgs84_lat'], query['wgs84_lon']
results[:point] = {'lat' => lat, 'lon' => lon}
end
return results
end
|
#centre_of_district(district_postcode) ⇒ Object
109
110
111
112
113
114
115
|
# File 'lib/geogov/providers/mapit.rb', line 109
def centre_of_district(district_postcode)
query = self.postcode("partial", district_postcode)
if query
lat, lon = query['wgs84_lat'], query['wgs84_lon']
return {'lat' => lat, 'lon' => lon}
end
end
|
#lat_lon_from_postcode(postcode) ⇒ Object
44
45
46
47
|
# File 'lib/geogov/providers/mapit.rb', line 44
def lat_lon_from_postcode(postcode)
areas = areas_for_stack_from_postcode(postcode)
areas[:point]
end
|
#respond_to?(sym) ⇒ Boolean
40
41
42
|
# File 'lib/geogov/providers/mapit.rb', line 40
def respond_to?(sym)
valid_mapit_methods.include?(sym) || super(sym)
end
|
#translate_area_type_to_shortcut(area_type) ⇒ Object
Borrowed heavily from mapit’s pylib/postcodes/views.py with some amendments based on pylib/mapit/areas/models.py
51
52
53
54
55
56
57
58
59
60
61
62
63
|
# File 'lib/geogov/providers/mapit.rb', line 51
def translate_area_type_to_shortcut(area_type)
if ['COP', 'LBW', 'LGE', 'MTW', 'UTE', 'UTW', 'DIW'].include?(area_type)
return 'ward'
elsif ['CTY', 'CED'].include?(area_type)
git
return 'council' elsif ['DIS', 'LBO'].include?(area_type)
return 'council' elsif area_type == 'WMC' return 'WMC'
end
end
|
#valid_mapit_methods ⇒ Object
36
37
38
|
# File 'lib/geogov/providers/mapit.rb', line 36
def valid_mapit_methods
[:postcode, :areas, :area, :point, :generations]
end
|