File: ban_data_gouv_fr.rb

package info (click to toggle)
ruby-geocoder 1.8.6-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 900 kB
  • sloc: ruby: 8,419; makefile: 3
file content (143 lines) | stat: -rw-r--r-- 4,243 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# encoding: utf-8

require 'geocoder/lookups/base'
require 'geocoder/results/ban_data_gouv_fr'

module Geocoder::Lookup
  class BanDataGouvFr < Base

    def name
      "Base Adresse Nationale Française"
    end

    def map_link_url(coordinates)
      "https://www.openstreetmap.org/#map=19/#{coordinates.join('/')}"
    end

    private # ---------------------------------------------------------------

    def base_query_url(query)
      method = query.reverse_geocode? ? "reverse" : "search"
      "#{protocol}://data.geopf.fr/geocodage/#{method}/?"
    end

    def any_result?(doc)
      doc['features'] and doc['features'].any?
    end

    def results(query)
      if doc = fetch_data(query) and any_result?(doc)
        [doc]
      else
        []
      end
    end

    #### PARAMS ####

    def query_url_params(query)
      query_ban_datagouv_fr_params(query).merge(super)
    end

    def query_ban_datagouv_fr_params(query)
      query.reverse_geocode? ? reverse_geocode_ban_fr_params(query) : search_geocode_ban_fr_params(query)
    end

    #### SEARCH GEOCODING PARAMS ####
    #
    #  :q             =>    required, full text search param)

    #  :limit         =>    force limit number of results returned by raw API
    #                       (default = 5) note : only first result is taken
    #                       in account in geocoder
    #
    #  :autocomplete  =>    pass 0 to disable autocomplete treatment of :q
    #                       (default = 1)
    #
    #  :lat           =>    force filter results around specific lat/lon
    #
    #  :lon           =>    force filter results around specific lat/lon
    #
    #  :type          =>    force filter the returned result type
    #                       (check results for a list of accepted types)
    #
    #  :postcode      =>    force filter results on a specific city post code
    #
    #  :citycode      =>    force filter results on a specific city UUID INSEE code
    #
    #  For up to date doc (in french only) : https://adresse.data.gouv.fr/api/
    #
    def search_geocode_ban_fr_params(query)
      params = {
        q: query.sanitized_text
      }
      unless (limit = query.options[:limit]).nil? || !limit_param_is_valid?(limit)
        params[:limit] = limit.to_i
      end
      unless (autocomplete = query.options[:autocomplete]).nil? || !autocomplete_param_is_valid?(autocomplete)
        params[:autocomplete] = autocomplete.to_s
      end
      unless (type = query.options[:type]).nil? || !type_param_is_valid?(type)
        params[:type] = type.downcase
      end
      unless (postcode = query.options[:postcode]).nil? || !code_param_is_valid?(postcode)
        params[:postcode] = postcode.to_s
      end
      unless (citycode = query.options[:citycode]).nil? || !code_param_is_valid?(citycode)
        params[:citycode] = citycode.to_s
      end
      unless (lat = query.options[:lat]).nil? || !latitude_is_valid?(lat)
        params[:lat] = lat
      end
      unless (lon = query.options[:lon]).nil? || !longitude_is_valid?(lon)
        params[:lon] = lon
      end
      params
    end

    #### REVERSE GEOCODING PARAMS ####
    #
    #  :lat           =>    required
    #
    #  :lon           =>    required
    #
    #  :type          =>    force returned results type
    #                       (check results for a list of accepted types)
    #
    def reverse_geocode_ban_fr_params(query)
      lat_lon = query.coordinates
      params = {
          lat: lat_lon.first,
          lon: lat_lon.last
      }
      unless (type = query.options[:type]).nil? || !type_param_is_valid?(type)
        params[:type] = type.downcase
      end
      params
    end

    def limit_param_is_valid?(param)
      param.to_i.positive?
    end

    def autocomplete_param_is_valid?(param)
      [0,1].include?(param.to_i)
    end

    def type_param_is_valid?(param)
      %w(housenumber street locality municipality).include?(param.downcase)
    end

    def code_param_is_valid?(param)
      (1..99999).include?(param.to_i)
    end

    def latitude_is_valid?(param)
      param.to_f <= 90 && param.to_f >= -90
    end

    def longitude_is_valid?(param)
      param.to_f <= 180 && param.to_f >= -180
    end
  end
end