diff --git a/comparators/added_place.js b/comparators/added_place.js deleted file mode 100644 index 5f7c36d..0000000 --- a/comparators/added_place.js +++ /dev/null @@ -1,30 +0,0 @@ -'use strict'; - -module.exports = addedPlace; - -function addedPlace(newVersion, oldVersion) { - if (newVersion.deleted) { - return false; - } - if (oldVersion) { - if ( - 'place' in newVersion.properties && !('place' in oldVersion.properties) - ) { - if ( - newVersion.properties['place'] === 'city' || - newVersion.properties['place'] === 'town' || - newVersion.properties['place'] === 'country' - ) { - return {'result:added_place': true}; - } - } - } else if ( - 'place' in newVersion.properties && - (newVersion.properties['place'] === 'city' || - newVersion.properties['place'] === 'town' || - newVersion.properties['place'] === 'country') - ) { - return {'result:added_place': true}; - } - return false; -} diff --git a/comparators/important_place.js b/comparators/important_place.js new file mode 100644 index 0000000..ef9b634 --- /dev/null +++ b/comparators/important_place.js @@ -0,0 +1,26 @@ +'use strict'; +const welltagged = require('../lib/welltagged'); +module.exports = importantPlace; +module.exports.checkPlaceType = checkPlaceType; + +function importantPlace(newVersion, oldVersion) { + const newType = checkPlaceType(newVersion); + const oldType = checkPlaceType(oldVersion); + return oldType || newType; +} + +function checkPlaceType(feature) { + if (!feature) return false; + if (feature && feature.geometry && feature.geometry.type !== 'Point') { + return false; + } + const tags = feature.properties; + if (tags && tags.place) { + if (tags.place === 'country') return tags.place; + if (tags.place === 'city') return tags.place; + if (tags.place === 'town' && welltagged.hasWikiTags(tags)) { + return 'town'; + } + } + return false; +} diff --git a/comparators/place_created.js b/comparators/place_created.js new file mode 100644 index 0000000..7712408 --- /dev/null +++ b/comparators/place_created.js @@ -0,0 +1,22 @@ +'use strict'; +const importantPlace = require('./important_place'); + +module.exports = createPlace; + +/** + * Detects whether a feature was or is a park + @param {geojson} newVersion - New version of feature + @param {geojson} oldVersion - Previous version of feature + @returns {object|false} returns the detected incident or false + */ +function createPlace(newVersion, oldVersion) { + const isNew = newVersion.properties['osm:version'] === 1 || !oldVersion; + const isImportant = importantPlace(newVersion, oldVersion); + + if (isNew && isImportant) { + return { + message: `Important place=${newVersion.properties.place} created` + }; + } + return false; +} diff --git a/comparators/place_deleted.js b/comparators/place_deleted.js new file mode 100644 index 0000000..1583b98 --- /dev/null +++ b/comparators/place_deleted.js @@ -0,0 +1,22 @@ +'use strict'; +const importantPlace = require('./important_place'); + +module.exports = deletePlace; + +/** + * Detects whether a feature was or is a park + @param {geojson} newVersion - New version of feature + @param {geojson} oldVersion - Previous version of feature + @returns {object|false} returns the detected incident or false + */ +function deletePlace(newVersion, oldVersion) { + const isDeleted = !!newVersion.deleted; + const isImportant = importantPlace(newVersion, oldVersion); + + if (isDeleted && isImportant) { + return { + message: `Important place=${newVersion.properties.place} deleted` + }; + } + return false; +} diff --git a/index.js b/index.js index 1be3452..db802d8 100755 --- a/index.js +++ b/index.js @@ -32,6 +32,8 @@ module.exports = { require('./comparators/modified_place_wikidata') ), modified_monument: wrapsync(require('./comparators/modified_monument')), + place_created: wrapsync(require('./comparators/place_created')), + place_deleted: wrapsync(require('./comparators/place_deleted')), invalid_tag_combination: require('./comparators/invalid_tag_combination'), water_feature_by_new_user: require('./comparators/water_feature_by_new_user'), common_tag_values: wrapsync(require('./comparators/common_tag_values.js')), diff --git a/lib/welltagged.js b/lib/welltagged.js new file mode 100644 index 0000000..4356994 --- /dev/null +++ b/lib/welltagged.js @@ -0,0 +1,60 @@ +'use strict'; +const namePattern = /^(int_|loc_|nat_|official_|reg_|short_|sorting_)?name(:.*)?$/; +module.exports = wellMaintained; +module.exports.hasTranslations = hasTranslations; +module.exports.hasWikiTags = hasWikiTags; +module.exports.hasPlaceMetadata = hasPlaceMetadata; + +function hasWikiTags(tags) { + return tags && (tags.wikipedia || tags.wikidata); +} + +function hasTranslations(tags) { + const translations = Object.keys(tags) + .map(key => namePattern.test(key)) + .filter(k => !!k).length; + return translations > 1; +} + +function hasAddr(tags) { + const addrs = Object.keys(tags) + .map(key => /^addr/.test(key)) + .filter(k => !!k).length; + return addrs > 0; +} + +function hasRefs(tags) { + const refs = Object.keys(tags) + .map(key => /^ref/.test(key)) + .filter(k => !!k).length; + return refs > 0; +} + +function hasContact(tags) { + return tags.website || + tags.phone || + tags['contact:phone'] || + tags['contact:website']; +} + +function hasPlaceMetadata(tags) { + return tags.population || tags.ele || tags.postal_code || tags.is_in; +} + +function hasMetadataTags(tags) { + return hasAddr(tags) || + hasContact(tags) || + hasRefs(tags) || + hasTranslations(tags) || + hasWikiTags(tags) || + hasPlaceMetadata(tags); +} + +function wellMaintained(newVersion, oldVersion) { + if (hasMetadataTags(newVersion.properties)) { + return true; + } else if (oldVersion && hasMetadataTags(oldVersion.properties)) { + return true; + } + return false; +} diff --git a/tests/fixtures/added_place.json b/tests/fixtures/added_place.json deleted file mode 100644 index 4f091a1..0000000 --- a/tests/fixtures/added_place.json +++ /dev/null @@ -1,112 +0,0 @@ -{ - "compareFunction": "added_place", - "fixtures": [ - { - "description": "No old version, new version", - "newVersion": {"deleted": true}, - "oldVersion": null, - "expectedResult": false - }, - { - "description": "Checks for added place tag", - "newVersion": { - "type": "Feature", - "properties": { - "osm:id": 1234, - "osm:type": "node", - "osm:uid": 123, - "osm:changeset": 123, - "place": "city" - }, - "geometry": { - "type": "Point", - "coordinates": [ - 10, - 10 - ] - } - }, - "oldVersion": null, - "expectedResult": { - "result:added_place": true - } - }, - { - "description": "Checks for added place tag", - "newVersion": { - "type": "Feature", - "properties": { - "osm:id": 1234, - "osm:type": "node", - "osm:uid": 123, - "osm:changeset": 123, - "place": "city" - }, - "geometry": { - "type": "Point", - "coordinates": [ - 10, - 10 - ] - } - }, - "oldVersion": { - "type": "Feature", - "properties": { - "osm:id": 1234, - "osm:type": "node", - "osm:uid": 124, - "osm:changeset": 124 - }, - "geometry": { - "type": "Point", - "coordinates": [ - 11, - 11 - ] - } - }, - "expectedResult": { - "result:added_place": true - } - }, - { - "description": "Place in old and new version", - "newVersion": { - "type": "Feature", - "properties": { - "osm:id": 1234, - "osm:type": "node", - "osm:uid": 123, - "osm:changeset": 123, - "place": "city" - }, - "geometry": { - "type": "Point", - "coordinates": [ - 10, - 10 - ] - } - }, - "oldVersion": { - "type": "Feature", - "properties": { - "osm:id": 1234, - "osm:type": "node", - "osm:uid": 124, - "osm:changeset": 124, - "place": "city" - }, - "geometry": { - "type": "Point", - "coordinates": [ - 11, - 11 - ] - } - }, - "expectedResult": false - } - ] -} diff --git a/tests/fixtures/place_created.json b/tests/fixtures/place_created.json new file mode 100644 index 0000000..c5c3449 --- /dev/null +++ b/tests/fixtures/place_created.json @@ -0,0 +1,47 @@ +{ + "compareFunction": "place_created", + "fixtures": [ + { + "description": "Italy created", + "newVersion": { + "geometry": { + "type": "Point", + "coordinates": [ + 0, + 0 + ] + }, + "properties": { + "osm:version": 1, + "osm:type": "node", + "place": "country", + "name": "Italia" + } + }, + "expectedResult": { + "message": "Important place=country created" + } + }, + { + "description": "London created", + "newVersion": { + "geometry": { + "type": "Point", + "coordinates": [ + 0, + 0 + ] + }, + "properties": { + "osm:version": 1, + "osm:type": "node", + "place": "city", + "name": "London" + } + }, + "expectedResult": { + "message": "Important place=city created" + } + } + ] +} diff --git a/tests/fixtures/place_deleted.json b/tests/fixtures/place_deleted.json new file mode 100644 index 0000000..e97ac32 --- /dev/null +++ b/tests/fixtures/place_deleted.json @@ -0,0 +1,31 @@ +{ + "compareFunction": "place_deleted", + "fixtures": [ + { + "description": "London deleted", + "oldVersion": { + "properties": { + "osm:version": 1, + "osm:type": "node", + "place": "city", + "name": "London" + } + }, + "newVersion": { + "properties": { + "osm:version": 2, + "osm:type": "node", + "place": "city", + "name": "London" + }, + "geometry": { + "type": "Point" + }, + "deleted": true + }, + "expectedResult": { + "message": "Important place=city deleted" + } + } + ] +}