diff --git a/README_API_GUIDE.md b/README_API_GUIDE.md index 9080a62ea..6a45111c3 100644 --- a/README_API_GUIDE.md +++ b/README_API_GUIDE.md @@ -487,6 +487,16 @@ Regional Street Address Lookups * **Documentation**: http://geodata.nationaalgeoregister.nl/ * **Terms of Service**: https://www.pdok.nl/over-pdok - The PDOK services are based on open data and are therefore freely available to everyone. +### pdok NL (`:pdok_nl`) + +* **API key**: none +* **Quota**: none +* **Region**: Netherlands +* **SSL support**: yes, required +* **Languages**: Dutch +* **Documentation**: https://api.pdok.nl/bzk/locatieserver/search/v3_1/ui/#/Locatieserver/free +* **Terms of Service**: https://www.pdok.nl/over-pdok - The PDOK services are based on open data and are therefore freely available to everyone. + ### Ordnance Survey OpenNames (`:uk_ordnance_survey_names`) * **API key**: required (sign up at https://developer.ordnancesurvey.co.uk/os-names-api) diff --git a/lib/geocoder/lookup.rb b/lib/geocoder/lookup.rb index 56b36319f..8e613aa55 100644 --- a/lib/geocoder/lookup.rb +++ b/lib/geocoder/lookup.rb @@ -48,6 +48,7 @@ def street_services :uk_ordnance_survey_names, :opencagedata, :pelias, + :pdok_nl, :pickpoint, :here, :baidu, diff --git a/lib/geocoder/lookups/pdok_nl.rb b/lib/geocoder/lookups/pdok_nl.rb new file mode 100644 index 000000000..180d1ed19 --- /dev/null +++ b/lib/geocoder/lookups/pdok_nl.rb @@ -0,0 +1,43 @@ +require 'geocoder/lookups/base' +require "geocoder/results/pdok_nl" + +module Geocoder::Lookup + class PdokNl < Base + + def name + 'pdok NL' + end + + def supported_protocols + [:https] + end + + private # --------------------------------------------------------------- + + def cache_key(query) + base_query_url(query) + hash_to_query(query_url_params(query)) + end + + def base_query_url(query) + "#{protocol}://api.pdok.nl/bzk/locatieserver/search/v3_1/free?" + end + + def valid_response?(response) + json = parse_json(response.body) + super(response) if json + end + + def results(query) + return [] unless doc = fetch_data(query) + return doc['response']['docs'] + end + + def query_url_params(query) + { + fl: '*', + q: query.text, + wt: 'json' + }.merge(super) + end + end +end diff --git a/lib/geocoder/results/pdok_nl.rb b/lib/geocoder/results/pdok_nl.rb new file mode 100644 index 000000000..8b90dc609 --- /dev/null +++ b/lib/geocoder/results/pdok_nl.rb @@ -0,0 +1,62 @@ +require 'geocoder/results/base' + +module Geocoder::Result + class PdokNl < Base + + def response_attributes + @data + end + + def coordinates + @data['centroide_ll'][6..-2].split(' ').map(&:to_f).reverse + end + + def formatted_address + @data['weergavenaam'] + end + + alias_method :address, :formatted_address + + def province + @data['provincienaam'] + end + + alias_method :state, :province + + def city + @data['woonplaatsnaam'] + end + + def district + @data['gemeentenaam'] + end + + def street + @data['straatnaam'] + end + + def street_number + @data['huis_nlt'] + end + + def address_components + @data + end + + def state_code + @data['provinciecode'] + end + + def postal_code + @data['postcode'] + end + + def country + "Netherlands" + end + + def country_code + "NL" + end + end +end diff --git a/test/fixtures/pdok_nl b/test/fixtures/pdok_nl new file mode 100644 index 000000000..387d2ef96 --- /dev/null +++ b/test/fixtures/pdok_nl @@ -0,0 +1,245 @@ +{ + "response": { + "numFound": 601698, + "start": 0, + "maxScore": 21.486404, + "numFoundExact": true, + "docs": [ + { + "bron": "BAG", + "woonplaatscode": "3594", + "type": "adres", + "woonplaatsnaam": "Amsterdam", + "wijkcode": "WK0363AD", + "huis_nlt": "147", + "openbareruimtetype": "Weg", + "buurtnaam": "Nieuwe Kerk e.o.", + "gemeentecode": "0363", + "rdf_seealso": "http://bag.basisregistraties.overheid.nl/bag/id/nummeraanduiding/0363200000218908", + "weergavenaam": "Nieuwezijds Voorburgwal 147, 1012RJ Amsterdam", + "straatnaam_verkort": "Nieuwezijds Voorburgwal", + "id": "adr-563f90756e46d1c554b4ab00ac61b932", + "gekoppeld_perceel": [ + "ASD04-F-2749" + ], + "gemeentenaam": "Amsterdam", + "buurtcode": "BU0363AD05", + "wijknaam": "Burgwallen-Nieuwe Zijde", + "identificatie": "0363010000758545-0363200000218908", + "openbareruimte_id": "0363300000004690", + "waterschapsnaam": "Hoogheemraadschap Amstel Gooi en Vecht", + "provinciecode": "PV27", + "postcode": "1012RJ", + "provincienaam": "Noord-Holland", + "centroide_ll": "POINT(4.89089949 52.37316398)", + "nummeraanduiding_id": "0363200000218908", + "waterschapscode": "11", + "adresseerbaarobject_id": "0363010000758545", + "huisnummer": 147, + "provincieafkorting": "NH", + "centroide_rd": "POINT(121202 487370)", + "straatnaam": "Nieuwezijds Voorburgwal", + "score": 21.486404 + }, + { + "bron": "BAG/NWB", + "woonplaatscode": "3594", + "type": "weg", + "woonplaatsnaam": "Amsterdam", + "nwb_id": "032937", + "openbareruimtetype": "Weg", + "gemeentecode": "0363", + "rdf_seealso": "http://bag.basisregistraties.overheid.nl/bag/id/openbare-ruimte/0363300000004690", + "weergavenaam": "Nieuwezijds Voorburgwal, Amsterdam", + "straatnaam_verkort": "Nieuwezijds Voorburgwal", + "id": "weg-11b9db917ce90372930af5e6a9188478", + "gemeentenaam": "Amsterdam", + "identificatie": "0363300000004690", + "openbareruimte_id": "0363300000004690", + "provinciecode": "PV27", + "provincienaam": "Noord-Holland", + "centroide_ll": "POINT(4.89091865 52.37381873)", + "provincieafkorting": "NH", + "centroide_rd": "POINT(121203.803 487442.84)", + "straatnaam": "Nieuwezijds Voorburgwal", + "score": 20.103004 + }, + { + "bron": "BAG", + "woonplaatscode": "3594", + "type": "postcode", + "woonplaatsnaam": "Amsterdam", + "openbareruimtetype": "Weg", + "gemeentecode": "0363", + "weergavenaam": "Nieuwezijds Voorburgwal, 1012RC Amsterdam", + "straatnaam_verkort": "Nieuwezijds Voorburgwal", + "id": "pcd-5f1195a7f9fe6a99b1571d7c15c64138", + "gemeentenaam": "Amsterdam", + "identificatie": "0363300000004690_1012RC_3594", + "openbareruimte_id": "0363300000004690", + "provinciecode": "PV27", + "postcode": "1012RC", + "provincienaam": "Noord-Holland", + "centroide_ll": "POINT(4.89522341 52.37696711)", + "provincieafkorting": "NH", + "centroide_rd": "POINT(121499.326 487791.141)", + "straatnaam": "Nieuwezijds Voorburgwal", + "score": 18.053778 + }, + { + "bron": "BAG", + "woonplaatscode": "3594", + "type": "postcode", + "woonplaatsnaam": "Amsterdam", + "openbareruimtetype": "Weg", + "gemeentecode": "0363", + "weergavenaam": "Nieuwezijds Voorburgwal, 1012RD Amsterdam", + "straatnaam_verkort": "Nieuwezijds Voorburgwal", + "id": "pcd-89dbb53c07add38657bf17abe6dd49c4", + "gemeentenaam": "Amsterdam", + "identificatie": "0363300000004690_1012RD_3594", + "openbareruimte_id": "0363300000004690", + "provinciecode": "PV27", + "postcode": "1012RD", + "provincienaam": "Noord-Holland", + "centroide_ll": "POINT(4.8938645 52.3761153)", + "provincieafkorting": "NH", + "centroide_rd": "POINT(121406.149 487696.995)", + "straatnaam": "Nieuwezijds Voorburgwal", + "score": 18.053778 + }, + { + "bron": "BAG", + "woonplaatscode": "3594", + "type": "postcode", + "woonplaatsnaam": "Amsterdam", + "openbareruimtetype": "Weg", + "gemeentecode": "0363", + "weergavenaam": "Nieuwezijds Voorburgwal, 1012RE Amsterdam", + "straatnaam_verkort": "Nieuwezijds Voorburgwal", + "id": "pcd-f303e0cf9b29083a2beb757476ce8948", + "gemeentenaam": "Amsterdam", + "identificatie": "0363300000004690_1012RE_3594", + "openbareruimte_id": "0363300000004690", + "provinciecode": "PV27", + "postcode": "1012RE", + "provincienaam": "Noord-Holland", + "centroide_ll": "POINT(4.89252497 52.37521114)", + "provincieafkorting": "NH", + "centroide_rd": "POINT(121314.248 487597.017)", + "straatnaam": "Nieuwezijds Voorburgwal", + "score": 18.053778 + }, + { + "bron": "BAG", + "woonplaatscode": "3594", + "type": "postcode", + "woonplaatsnaam": "Amsterdam", + "openbareruimtetype": "Weg", + "gemeentecode": "0363", + "weergavenaam": "Nieuwezijds Voorburgwal, 1012RG Amsterdam", + "straatnaam_verkort": "Nieuwezijds Voorburgwal", + "id": "pcd-e6324266fae865a1d66861a5118ff3af", + "gemeentenaam": "Amsterdam", + "identificatie": "0363300000004690_1012RG_3594", + "openbareruimte_id": "0363300000004690", + "provinciecode": "PV27", + "postcode": "1012RG", + "provincienaam": "Noord-Holland", + "centroide_ll": "POINT(4.89177932 52.37453055)", + "provincieafkorting": "NH", + "centroide_rd": "POINT(121262.955 487521.639)", + "straatnaam": "Nieuwezijds Voorburgwal", + "score": 18.053778 + }, + { + "bron": "BAG", + "woonplaatscode": "3594", + "type": "postcode", + "woonplaatsnaam": "Amsterdam", + "openbareruimtetype": "Weg", + "gemeentecode": "0363", + "weergavenaam": "Nieuwezijds Voorburgwal, 1012RH Amsterdam", + "straatnaam_verkort": "Nieuwezijds Voorburgwal", + "id": "pcd-57b324f3b15c70ac7a8641c10b318c6a", + "gemeentenaam": "Amsterdam", + "identificatie": "0363300000004690_1012RH_3594", + "openbareruimte_id": "0363300000004690", + "provinciecode": "PV27", + "postcode": "1012RH", + "provincienaam": "Noord-Holland", + "centroide_ll": "POINT(4.89161743 52.37435875)", + "provincieafkorting": "NH", + "centroide_rd": "POINT(121251.8 487502.6)", + "straatnaam": "Nieuwezijds Voorburgwal", + "score": 18.053778 + }, + { + "bron": "BAG", + "woonplaatscode": "3594", + "type": "postcode", + "woonplaatsnaam": "Amsterdam", + "openbareruimtetype": "Weg", + "gemeentecode": "0363", + "weergavenaam": "Nieuwezijds Voorburgwal, 1012RJ Amsterdam", + "straatnaam_verkort": "Nieuwezijds Voorburgwal", + "id": "pcd-def6647d79cbce522864726af4c2af74", + "gemeentenaam": "Amsterdam", + "identificatie": "0363300000004690_1012RJ_3594", + "openbareruimte_id": "0363300000004690", + "provinciecode": "PV27", + "postcode": "1012RJ", + "provincienaam": "Noord-Holland", + "centroide_ll": "POINT(4.89133064 52.3739109)", + "provincieafkorting": "NH", + "centroide_rd": "POINT(121231.93 487452.904)", + "straatnaam": "Nieuwezijds Voorburgwal", + "score": 18.053778 + }, + { + "bron": "BAG", + "woonplaatscode": "3594", + "type": "postcode", + "woonplaatsnaam": "Amsterdam", + "openbareruimtetype": "Weg", + "gemeentecode": "0363", + "weergavenaam": "Nieuwezijds Voorburgwal, 1012RK Amsterdam", + "straatnaam_verkort": "Nieuwezijds Voorburgwal", + "id": "pcd-1fbacacc892befcdaa4e46f475e5a16a", + "gemeentenaam": "Amsterdam", + "identificatie": "0363300000004690_1012RK_3594", + "openbareruimte_id": "0363300000004690", + "provinciecode": "PV27", + "postcode": "1012RK", + "provincienaam": "Noord-Holland", + "centroide_ll": "POINT(4.89097665 52.37221864)", + "provincieafkorting": "NH", + "centroide_rd": "POINT(121206.534 487264.783)", + "straatnaam": "Nieuwezijds Voorburgwal", + "score": 18.053778 + }, + { + "bron": "BAG", + "woonplaatscode": "3594", + "type": "postcode", + "woonplaatsnaam": "Amsterdam", + "openbareruimtetype": "Weg", + "gemeentecode": "0363", + "weergavenaam": "Nieuwezijds Voorburgwal, 1012RL Amsterdam", + "straatnaam_verkort": "Nieuwezijds Voorburgwal", + "id": "pcd-540b9459499a7a25fa74d39a80072570", + "gemeentenaam": "Amsterdam", + "identificatie": "0363300000004690_1012RL_3594", + "openbareruimte_id": "0363300000004690", + "provinciecode": "PV27", + "postcode": "1012RL", + "provincienaam": "Noord-Holland", + "centroide_ll": "POINT(4.8911854 52.37137729)", + "provincieafkorting": "NH", + "centroide_rd": "POINT(121220.109 487171.074)", + "straatnaam": "Nieuwezijds Voorburgwal", + "score": 18.053778 + } + ] + } +} \ No newline at end of file diff --git a/test/fixtures/pdok_nl_no_results b/test/fixtures/pdok_nl_no_results new file mode 100644 index 000000000..dea509759 --- /dev/null +++ b/test/fixtures/pdok_nl_no_results @@ -0,0 +1,8 @@ +{ + "response": { + "numFound": 0, + "start": 0, + "maxScore": 21.486404, + "numFoundExact": true, + "docs": [] +} diff --git a/test/test_helper.rb b/test/test_helper.rb index c0fb78382..526f9b326 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -353,7 +353,6 @@ def results query end end - require 'geocoder/lookups/baidu' class Baidu private @@ -370,6 +369,14 @@ def default_fixture_filename end end + require 'geocoder/lookups/pdok_nl' + class PdokNl + private + def default_fixture_filename + "pdok_nl" + end + end + require 'geocoder/lookups/baidu_ip' class BaiduIp private diff --git a/test/unit/lookups/pdok_nl_test.rb b/test/unit/lookups/pdok_nl_test.rb new file mode 100644 index 000000000..bb53adbcd --- /dev/null +++ b/test/unit/lookups/pdok_nl_test.rb @@ -0,0 +1,25 @@ +# encoding: utf-8 +require 'test_helper' + +class PdokNlTest < GeocoderTestCase + + def setup + super + Geocoder.configure(lookup: :pdok_nl) + end + + def test_result_components + result = Geocoder.search('Nieuwezijds Voorburgwal 147, Amsterdam').first + + assert_equal result.street, 'Nieuwezijds Voorburgwal' + assert_equal result.street_number, '147' + assert_equal result.city, 'Amsterdam' + assert_equal result.postal_code, '1012RJ' + assert_equal result.address, 'Nieuwezijds Voorburgwal 147, 1012RJ Amsterdam' + assert_equal result.province, 'Noord-Holland' + assert_equal result.province_code, 'PV27' + assert_equal result.country_code, 'NL' + assert_equal result.latitude, 52.37316398 + assert_equal result.longitude, 4.89089949 + end +end