From 49eae8e389dfec51399e99e7e8bbd451f097284f Mon Sep 17 00:00:00 2001 From: Christian Schwarz Date: Thu, 5 Jun 2014 20:30:12 +0200 Subject: [PATCH] Switched to a custom country code service --- data/address-formats.json | 4 ++-- index.html | 1 + js/id/geo.js | 4 ++-- js/id/services/countrycode.js | 37 +++++++++++++++++++++++++++++++++++ js/id/ui/preset/address.js | 16 +++------------ test/index.html | 1 + test/spec/geo.js | 4 ++-- 7 files changed, 48 insertions(+), 19 deletions(-) create mode 100644 js/id/services/countrycode.js diff --git a/data/address-formats.json b/data/address-formats.json index 5627800b3..7de137b8e 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/index.html b/index.html index 09f51eca1..7cfc9e091 100644 --- a/index.html +++ b/index.html @@ -40,6 +40,7 @@ + diff --git a/js/id/geo.js b/js/id/geo.js index aeceae3ea..36ca5fb3a 100644 --- a/js/id/geo.js +++ b/js/id/geo.js @@ -137,8 +137,8 @@ iD.geo.pathLength = function(path) { }; iD.geo.pointInFeature = function(point, feature) { - if (feature.properties && feature.properties.bounds) { - var bounds = feature.properties.bounds; + if (feature.bounds) { + var bounds = feature.bounds; if (point[0] < bounds[0][0] || point[0] > bounds[1][0] || point[1] < bounds[0][1] || point[1] > bounds[1][1]) return false; diff --git a/js/id/services/countrycode.js b/js/id/services/countrycode.js new file mode 100644 index 000000000..16a8ebf71 --- /dev/null +++ b/js/id/services/countrycode.js @@ -0,0 +1,37 @@ +iD.countryCode = function() { + var countryCode = {}, + endpoint = 'http://countrycode.refactory.at/?'; + + if (!iD.countryCode.cache) { + iD.countryCode.cache = []; + } + + var cache = iD.countryCode.cache; + + countryCode.search = function(location, callback) { + var country = _.find(cache, function (country) { + return iD.geo.pointInFeature(location, country); + }); + + if (country) + return callback(null, country); + + d3.json(endpoint + + iD.util.qsString({ + lat: location[1], + lon: location[0], + geometry: 1 + }), function(err, country) { + if (err) + return callback(err); + else if (country && country.error) + return callback(country.error); + + cache.push(country); + + callback(null, country); + }); + }; + + return countryCode; +}; diff --git a/js/id/ui/preset/address.js b/js/id/ui/preset/address.js index 172f36de8..e8456c4f3 100644 --- a/js/id/ui/preset/address.js +++ b/js/id/ui/preset/address.js @@ -93,26 +93,16 @@ iD.ui.preset.address = function(field, context) { var wrap = selection.selectAll('.preset-input-wrap').data([0]), center = entity.extent(context.graph()).center(), countryCode, - //country, addressFormat; - /* - country = _.find(iD.data.countries.features, function(f) { - return iD.geo.pointInFeature(center, f); - }); - - if (country) - countryCode = country.properties.countryCode; - */ - // Enter var enter = wrap.enter().append('div') .attr('class', 'preset-input-wrap'); - d3.json('http://nominatim.openstreetmap.org/reverse?format=json&lon=' + center[0] + '&lat=' + center[1], function (err, result) { - if (result && result.address && result.address.country_code) - countryCode = result.address.country_code; + iD.countryCode().search(center, function (err, result) { + if (result) + countryCode = result.countryCode; addressFormat = _.find(iD.data.addressFormats, function (a) { return a && a.countryCodes && _.contains(a.countryCodes, countryCode); diff --git a/test/index.html b/test/index.html index 213836440..5e98e6806 100644 --- a/test/index.html +++ b/test/index.html @@ -38,6 +38,7 @@ + diff --git a/test/spec/geo.js b/test/spec/geo.js index 78708aff1..38b2514c4 100644 --- a/test/spec/geo.js +++ b/test/spec/geo.js @@ -241,12 +241,12 @@ describe('iD.geo', function() { expect(iD.geo.pointInFeature(point, feature)).to.be.true; }); it('point should be in a polygon feature with bounds', function() { - var feature = { geometry: { type: 'Polygon', coordinates: [[[0, 0], [0, 1], [1, 1], [1, 0], [0, 0]]] }, properties: { bounds: [[0, 0], [1, 1]] } }; + var feature = { geometry: { type: 'Polygon', coordinates: [[[0, 0], [0, 1], [1, 1], [1, 0], [0, 0]]] }, bounds: [[0, 0], [1, 1]] }; var point = [0.5, 0.5]; expect(iD.geo.pointInFeature(point, feature)).to.be.true; }); it('point should not be in a polygon feature with bounds', function() { - var feature = { geometry: { type: 'Polygon', coordinates: [[[0, 0], [0, 1], [1, 1], [1, 0], [0, 0]]] }, properties: { bounds: [[0, 0], [1, 1]] } }; + var feature = { geometry: { type: 'Polygon', coordinates: [[[0, 0], [0, 1], [1, 1], [1, 0], [0, 0]]] }, bounds: [[0, 0], [1, 1]] }; var point = [0.5, 1.5]; expect(iD.geo.pointInFeature(point, feature)).to.be.false; });