Skip to content

Commit

Permalink
Make map feature collection hardcoded
Browse files Browse the repository at this point in the history
  • Loading branch information
Robbendebiene committed Sep 1, 2023
1 parent 28250ec commit 703257e
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 72 deletions.
72 changes: 0 additions & 72 deletions lib/models/map_feature_collection.dart

This file was deleted.

63 changes: 63 additions & 0 deletions lib/models/map_features/map_feature_collection.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import 'dart:collection';

import 'package:flutter/material.dart';

import '/models/osm_element_type.dart';
import '/models/element_conditions/element_condition.dart';
import '/models/element_conditions/sub_condition_matcher.dart';
import '/models/element_conditions/tag_value_matcher.dart';
import '/models/element_variants/base_element.dart';
import 'map_feature_definition.dart';
import 'map_feature_representation.dart';


abstract final class MapFeatures {

/// Compare the elements of this Map Feature Collection with given OSM element
/// and return the best matching Map Feature Template.
///
/// If not matching [MapFeatureDefinition] can be found a dummy [MapFeatureRepresentation] is returned.
static MapFeatureRepresentation getForElement (ProcessedElement osmElement) {
MapFeatureDefinition? bestMatch;
int score = 0;

for (final MapFeatureDefinition mapFeature in _list) {
final matchingCondition = mapFeature.matchesBy(osmElement);
if (matchingCondition != null) {
// Check if the newly matched map feature has more matching tags than the previously matched map feature
final newScore = _calcConditionScore(matchingCondition);
if (newScore > score) {
score = newScore;
bestMatch = mapFeature;
}
}
}

if (bestMatch != null) {
return bestMatch.resolve(osmElement);
}
else {
// construct dummy MapFeatureRepresentation
return MapFeatureRepresentation.fromElement(element: osmElement);
}
}

/// Calculate a simple score for a condition in order to prioritize one map feature over another.
/// Currently this only counts the number of tags the main tag condition has.
static int _calcConditionScore(ElementCondition condition) {
return condition.characteristics.fold<int>(0, (value, cond) {
if (cond is TagsSubCondition) return value + cond.characteristics.length;
return value;
});
}

static final UnmodifiableListView<MapFeatureDefinition> definitions = UnmodifiableListView(_list);

/// Defines all map features with their names, icons and conditions.
// TODO: make whole list const one day when https://github.com/dart-lang/language/issues/1048 is implemented
static final _list = <MapFeatureDefinition>[

];
}

0 comments on commit 703257e

Please sign in to comment.