Skip to content
This repository has been archived by the owner on Mar 15, 2024. It is now read-only.

Commit

Permalink
Merge pull request #105 from mapbox/matches-to-wikidata
Browse files Browse the repository at this point in the history
OSM name of a feature matches to Wikidata name
  • Loading branch information
bkowshik authored Mar 17, 2017
2 parents ade863e + b9fdfdc commit 94dd19a
Show file tree
Hide file tree
Showing 2 changed files with 158 additions and 0 deletions.
62 changes: 62 additions & 0 deletions comparators/name-matches-to-wikidata.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
'use strict';

var request = require('request');

module.exports = nameMatchesToWikidata;

function getWikidataName(feature, id) {
if (feature.hasOwnProperty('entities') &&
feature['entities'].hasOwnProperty(id) &&
feature['entities'][id].hasOwnProperty(['labels']) &&
feature['entities'][id]['labels'].hasOwnProperty('en'))
return feature['entities'][id]['labels']['en']['value'];
else
return undefined;
}

function getWikidataAliasNames(feature, id) {
var names = [];
if (feature.hasOwnProperty('entities') &&
feature['entities'].hasOwnProperty(id) &&
feature['entities'][id].hasOwnProperty(['aliases']) &&
feature['entities'][id]['aliases'].hasOwnProperty('en')) {
var aliases = feature['entities'][id]['aliases']['en'];
for (var i = 0; i < aliases.length; i++) {
names.push(aliases[i]['value']);
}
}
return names;
}

function nameMatchesToWikidata(newVersion, oldVersion, callback) {
var result = {};

if (!newVersion) return callback(null, result);

// Check if feature is newly created.
if (newVersion.properties['osm:version'] !== 1) {
if (!oldVersion || (newVersion.properties['name'] === oldVersion.properties['name'])) return callback(null, result);
}

if (newVersion.properties.hasOwnProperty('wikidata') && newVersion.properties.hasOwnProperty('name')) {

var osmName = newVersion.properties['name'];
var wikidataID = newVersion.properties['wikidata'];
var url = 'https://www.wikidata.org/w/api.php?action=wbgetentities&ids=' + wikidataID + '&format=json';

request(url, function (error, response, body) {
if (!error && response && (response.statusCode === 200)) {
var wikidataFeature = JSON.parse(body);
var wikidataName = getWikidataName(wikidataFeature, wikidataID);
var wikidataAliasNames = getWikidataAliasNames(wikidataFeature, wikidataID);

if ((osmName !== wikidataName) && (wikidataAliasNames.indexOf(osmName) === -1)) return callback(null, {
'result:name_matches_to_wikidata': false
});
}
return callback(null, result);
});
} else {
return callback(null, result);
}
}
96 changes: 96 additions & 0 deletions tests/fixtures/name-matches-to-wikidata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
{
"compareFunction": "name-matches-to-wikidata",
"fixtures": [
{
"description": "Test OSM name different from Wikidata name",
"expectedResult": {
"result:name_matches_to_wikidata": false
},
"newVersion": {
"type": "Feature",
"properties": {
"osm:id": 427818536,
"osm:type": "way",
"name": "Central Park is now something else",
"wikidata": "Q160409",
"osm:version": 1
},
"geometry": null
},
"oldVersion": null
},
{
"description": "Test OSM name matches with aliases on Wikidata",
"expectedResult": {},
"newVersion": {
"type": "Feature",
"properties": {
"osm:id": 3401391999,
"osm:type": "node",
"name": "Bengaluru",
"wikidata": "Q1355"
},
"geometry": null
},
"oldVersion": null
},
{
"description": "Test feature with no Wikidata tag",
"expectedResult": {},
"newVersion": {
"type": "Feature",
"properties": {
"osm:id": 3401391999,
"osm:type": "node",
"name": "Bengaluru"
},
"geometry": null
},
"oldVersion": null
},
{
"description": "Test feature with Wikidata tag and not a name modification",
"expectedResult": {},
"newVersion": {
"type": "Feature",
"properties": {
"osm:id": 3401391999,
"osm:type": "node",
"name": "Bengaluru is somethong else",
"osm:version": 2,
"wikidata": "Q1355"
},
"geometry": null
},
"oldVersion": {
"type": "Feature",
"properties": {
"osm:id": 3401391999,
"osm:type": "node",
"name": "Bengaluru is somethong else",
"osm:version": 1,
"wikidata": "Q1355"
},
"geometry": null
}
},
{
"description": "Test new feature with Wikidata",
"expectedResult": {
"result:name_matches_to_wikidata": false
},
"newVersion": {
"type": "Feature",
"properties": {
"osm:id": 3401391999,
"osm:type": "node",
"name": "Bengaluru is something else",
"osm:version": 1,
"wikidata": "Q1355"
},
"geometry": null
},
"oldVersion": null
}
]
}

0 comments on commit 94dd19a

Please sign in to comment.