This repository has been archived by the owner on Mar 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #105 from mapbox/matches-to-wikidata
OSM name of a feature matches to Wikidata name
- Loading branch information
Showing
2 changed files
with
158 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
] | ||
} |