diff --git a/data/address-formats.json b/data/address-formats.json index 7de137b8e..5627800b3 100644 --- a/data/address-formats.json +++ b/data/address-formats.json @@ -3,11 +3,11 @@ "format": [["number", "street"], ["city", "postcode"]] }, { - "countryCodes": ["GB"], + "countryCodes": ["gb"], "format": [["housename"], ["number", "street"], ["city", "postcode"]] }, { - "countryCodes": ["AT", "CH", "DE"], + "countryCodes": ["at", "ch", "de"], "format": [["street", "number"], ["postcode", "city"]] } ] diff --git a/js/id/services/countrycode.js b/js/id/services/countrycode.js index 386048075..61704e9e1 100644 --- a/js/id/services/countrycode.js +++ b/js/id/services/countrycode.js @@ -1,35 +1,36 @@ iD.countryCode = function() { var countryCode = {}, - endpoint = 'http://countrycode.refactory.at/api/1/?'; + endpoint = 'http://nominatim.openstreetmap.org/reverse?'; if (!iD.countryCode.cache) { - iD.countryCode.cache = []; + iD.countryCode.cache = rbush(); } var cache = iD.countryCode.cache; countryCode.search = function(location, callback) { - var country = _.find(cache, function (country) { - return iD.geo.pointInFeature(location, country); - }); + var countryCodes = cache.search([location[0], location[1], location[0], location[1]]); - if (country) - return callback(null, country); + if (countryCodes.length > 0) + return callback(null, countryCodes[0][4]); d3.json(endpoint + iD.util.qsString({ + format: 'json', + addressdetails: 1, lat: location[1], - lon: location[0], - geometry: 1 - }), function(err, country) { + lon: location[0] + }), function(err, result) { if (err) return callback(err); - else if (country && country.error) - return callback(country.error); + else if (result && result.error) + return callback(result.error); - cache.push(country); + var extent = iD.geo.Extent(location).padByMeters(1000); - callback(null, country); + cache.insert([extent[0][0], extent[0][1], extent[1][0], extent[1][1], result.address.country_code]); + + callback(null, result.address.country_code); }); }; diff --git a/js/id/ui/preset/address.js b/js/id/ui/preset/address.js index e8456c4f3..b0e917bad 100644 --- a/js/id/ui/preset/address.js +++ b/js/id/ui/preset/address.js @@ -92,7 +92,6 @@ iD.ui.preset.address = function(field, context) { function address(selection) { var wrap = selection.selectAll('.preset-input-wrap').data([0]), center = entity.extent(context.graph()).center(), - countryCode, addressFormat; // Enter @@ -100,10 +99,7 @@ iD.ui.preset.address = function(field, context) { var enter = wrap.enter().append('div') .attr('class', 'preset-input-wrap'); - iD.countryCode().search(center, function (err, result) { - if (result) - countryCode = result.countryCode; - + iD.countryCode().search(center, function (err, countryCode) { addressFormat = _.find(iD.data.addressFormats, function (a) { return a && a.countryCodes && _.contains(a.countryCodes, countryCode); }) || _.first(iD.data.addressFormats);