Switched to a custom country code service

This commit is contained in:
Christian Schwarz
2014-06-05 20:30:12 +02:00
parent 5a51a6c8c2
commit 49eae8e389
7 changed files with 48 additions and 19 deletions

View File

@@ -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"]]
}
]

View File

@@ -40,6 +40,7 @@
<script src='js/id/util/session_mutex.js'></script>
<script src='js/id/util/suggest_names.js'></script>
<script src='js/id/services/countrycode.js'></script>
<script src='js/id/services/taginfo.js'></script>
<script src='js/id/services/wikipedia.js'></script>

View File

@@ -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;

View File

@@ -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;
};

View File

@@ -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);

View File

@@ -38,6 +38,7 @@
<script src='../js/id/id.js'></script>
<script src='../js/id/util.js'></script>
<script src='../js/id/services/countrycode.js'></script>
<script src='../js/id/services/taginfo.js'></script>
<script src='../js/id/services/wikipedia.js'></script>

View File

@@ -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;
});