Skip to content

Commit

Permalink
Merge pull request #311 from pelias/dependency-country-code
Browse files Browse the repository at this point in the history
Use 3-character country code for dependencies where possible
  • Loading branch information
orangejulius authored May 12, 2022
2 parents 791dd52 + 3c68485 commit c194aec
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 8 deletions.
33 changes: 25 additions & 8 deletions src/pip/components/extractFields.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,7 @@ module.exports.create = function(enableLocalizedNames) {
res.properties.Hierarchy = [ [ res.properties.Id ] ];
}

// use different abbreviation field for country
if (res.properties.Placetype === 'country') {
res.properties.Abbrev = wofData.properties['wof:country_alpha3'];
} else if(wofData.properties['wof:shortcode']) {
res.properties.Abbrev = wofData.properties['wof:shortcode'];
} else {
res.properties.Abbrev = wofData.properties['wof:abbreviation'];
}
res.properties.Abbrev = getAbbreviation(wofData);

return res;
});
Expand All @@ -68,6 +61,30 @@ function getName(wofData, enableLocalizedNames) {
return getDefaultName(wofData);
}

/*
* Return an abbreviation based on the first defined value across several fields
*/
function getAbbreviation(wofData) {
const props = wofData.properties;
const placetype = props['wof:placetype'];

// countries have additional properties to check that
// may contain 3-character codes
if (['country', 'dependency'].includes(placetype)) {
return props['wof:country_alpha3'] ||
props['qs:adm0_a3'] ||
props['ne:adm0_a3'] ||
//the following properties generally contain a 2-character codes
//instead of 3 character and are therefore not preferred
props['wof:shortcode'] ||
props['wof:abbreviation'];
} else {
return props['wof:shortcode'] ||
props['wof:abbreviation'];
}
}


/**
* Get the string value of the property or false if not found
*
Expand Down
42 changes: 42 additions & 0 deletions test/pip/components/extractFieldsTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,48 @@ tape('extractFields tests', function(test) {

});

test.test('dependency placetype should check additional fields for 3 character country code', function(t) {
var input = {
properties: {}
};
input.properties['wof:id'] = 20;
input.properties['wof:name'] = 'Dependency name';
input.properties['wof:country_alpha3'] = undefined; // this is not always available for dependencies
input.properties['qs:adm0_a3'] = 'GUM'; // Quattroshapes or Natural Earth generally do provide a 3 char code
input.properties['wof:placetype'] = 'dependency';
input.properties['wof:hierarchy'] = [
{
placetype1: 12,
placetype2: 34
}
];

var expected = {
properties: {
Id: 20,
Name: 'Dependency name',
Abbrev: 'GUM',
Placetype: 'dependency',
Hierarchy: [
[ 12, 34 ]
],
Centroid: {
lat: undefined,
lon: undefined
},
BoundingBox: undefined
},
geometry: undefined
};

var extractFields = require('../../../src/pip/components/extractFields').create();

test_stream([input], extractFields, function(err, actual) {
t.deepEqual(actual, [expected], 'should be equal');
t.end();
});
});

test.test('when available, wof:label should be used instead of wof:name', function(t) {
var input = {
properties: {}
Expand Down

0 comments on commit c194aec

Please sign in to comment.