Class: Geogov::Mapit

Inherits:
Object
  • 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



128
129
130
131
132
133
134
# File 'lib/geogov/providers/mapit.rb', line 128

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



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/geogov/providers/mapit.rb', line 63

def areas_for_stack_from_coords(lat, lon)
  query = self.point("4326", [lon, lat])
  results = {:point => {'lat' => lat, 'lon' => lon}}
  councils = { }
  
  query.each do |id, area_info|
    type = area_info['type'].upcase.to_sym
    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
    
    councils[type] = { 'name' => area_info['name'], 'type' => area_info['type'], 'id' => area_info['id'] }
  end

  return councils.merge results 
end

#areas_for_stack_from_postcode(postcode) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/geogov/providers/mapit.rb', line 87

def areas_for_stack_from_postcode(postcode)
  query = self.postcode(postcode)
  results = {}

  if query && query['areas']

    query['areas'].each do |i, area|
      type = area['type'].to_sym
      results[type] = area
    end 

    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



120
121
122
123
124
125
126
# File 'lib/geogov/providers/mapit.rb', line 120

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] or raise UnrecognizedLocationError
end

#respond_to?(sym) ⇒ Boolean

Returns:

  • (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
# 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', 'MTD', 'UTA', 'DIS', 'LBO', 'LGD'].include?(area_type)
    return 'council'
  elsif area_type == 'EUR'
    return 'region'
  elsif area_type == 'WMC' # XXX Also maybe 'EUR', 'NIE', 'SPC', 'SPE', 'WAC', 'WAE', 'OLF', 'OLG', 'OMF', 'OMG')
    return 'WMC'
  end
end

#valid_mapit_methodsObject



36
37
38
# File 'lib/geogov/providers/mapit.rb', line 36

def valid_mapit_methods
  [:postcode, :areas, :area, :point, :generations]
end