Skip to content

Commit

Permalink
DT-6107 fix coarse admin labeling
Browse files Browse the repository at this point in the history
Neigbourhood should not be added to label, if the labeled item is
supposedly larger than the neigbourhood.
  • Loading branch information
vesameskanen committed Dec 28, 2023
1 parent 9d7a6a8 commit b9ff3ef
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
24 changes: 15 additions & 9 deletions helper/labelGenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,20 @@ var stringUtils = require('../helper/stringUtils');
var normalize = stringUtils.normalize;
var removeSpaces = stringUtils.removeSpaces;

const areaLikes = ['venue', 'neighbourhood', 'localadmin'];

module.exports = function( record, req ){
var schemaId = _.get(record, ['parent', 'country_a', 0 ], 'FIN');
var schema = getSchema(schemaId);

// in virtually all cases, this will be the `name` field
var labelParts = getInitialLabel(record);

// do not add neighbourhood to largish area-like items
const skip = record.category?.includes('populated area') ? 'neighbourhood' : null;
// iterate the schema
for (var field in schema) {
var valueFunction = schema[field];
var value = valueFunction(record, req);
var value = valueFunction(record, req, skip);
if(value) {
labelParts.push(value);
}
Expand All @@ -25,18 +28,21 @@ module.exports = function( record, req ){
labelParts = _.compact(labelParts);

// third, dedupe and join with a comma and return
return dedupeNameAndFirstLabelElement(labelParts).join(', ');

const candDropMinorAdmin = areaLikes.includes(record.layer);
return dedupeNameAndFirstLabelElement(labelParts, candDropMinorAdmin).join(', ');
};

function dedupeNameAndFirstLabelElement(labelParts) {
function dedupeNameAndFirstLabelElement(labelParts, dropMinorAdmin) {
// only dedupe if a result has more than a name (the first label part)
if (labelParts.length > 1) {
// first, dedupe the name and 1st label array elements
// this is used to ensure that the `name` and first admin hierarchy elements aren't repeated
// eg - `["Lancaster", "Lancaster", "PA", "United States"]` -> `["Lancaster", "PA", "United States"]`

if (labelParts[0].toLowerCase() === labelParts[1].toLowerCase()) {
// this is used to ensure that the `name` and first admin hierarchy elements aren't repeated
// eg - `["Lancaster", "Lancaster", "PA", "United States"]` -> `["Lancaster", "PA", "United States"]`
// also, do not add a minor admin to a name of coarser admin, e.g. helsinki, kaartinkaupunki, helsinki
if (
labelParts[0].toLowerCase() === labelParts[1].toLowerCase() ||
dropMinorAdmin && labelParts.length > 2 && labelParts[0].toLowerCase() === labelParts[2].toLowerCase()
) {
labelParts.splice(1, 1);
}
}
Expand Down
5 changes: 4 additions & 1 deletion helper/labelSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function baseVal(val) {

// find the first field of record that has a non-empty value that's not already in labelParts
function getFirstProperty(fields, matchType, targets) {
return function(record, req) {
return function(record, req, skip) {

if (targets && targets.indexOf(record.layer)===-1) {
return null; // not applicable to this kind of record
Expand All @@ -50,6 +50,9 @@ function getFirstProperty(fields, matchType, targets) {
var bestField;
for (var i = 0; i < fields.length; i++) {
var field = fields[i];
if (field === skip) {
continue;
}
var fieldValue;
if (Array.isArray(field)) { // chain multi parts
var parts = [];
Expand Down

0 comments on commit b9ff3ef

Please sign in to comment.