From 3595166598cc717236a0822b5b819a85738e2bb8 Mon Sep 17 00:00:00 2001 From: RZR-UA Date: Fri, 25 Oct 2024 13:02:22 +0200 Subject: [PATCH] Refactor searchOsmcPropertiesByFinalTags into getShieldTagsFromOsmcTags --- .../render/FindByRenderingTypesRules.java | 85 ------------------- .../osmand/routes/RouteRelationExtractor.java | 36 +++++++- 2 files changed, 34 insertions(+), 87 deletions(-) diff --git a/java-tools/OsmAndMapCreatorUtilities/src/main/java/net/osmand/render/FindByRenderingTypesRules.java b/java-tools/OsmAndMapCreatorUtilities/src/main/java/net/osmand/render/FindByRenderingTypesRules.java index 04fa236f6..021cc7b85 100644 --- a/java-tools/OsmAndMapCreatorUtilities/src/main/java/net/osmand/render/FindByRenderingTypesRules.java +++ b/java-tools/OsmAndMapCreatorUtilities/src/main/java/net/osmand/render/FindByRenderingTypesRules.java @@ -19,15 +19,6 @@ public class FindByRenderingTypesRules { public final MapRenderingTypesEncoder renderingTypes; private final RenderingRuleSearchRequest searchRequest; - private static final Map suffixOsmcTagsToResult = Map.of( - "icon", "shield_fg", - "icon_2", "shield_fg_2", - "textShield", "shield_bg", - "_osmc_waycolor", "color", - "_osmc_text", "shield_text", - "_osmc_textcolor", "shield_textcolor" - ); - public FindByRenderingTypesRules() { this(new String[]{"default.render.xml"}, null); } @@ -111,82 +102,6 @@ private static Map readRenderingConstantsFromIS(InputStream is) return renderingConstants; } - @Nullable - public Map searchOsmcPropertiesByFinalTags(@Nonnull Map tags) { - final String FIRST = "_1", ROUTE_PREFIX = "route_", OSMC_TEXT_SUFFIX = "_1_osmc_text"; - - String routeTypeTag = null; - for (String key : tags.keySet()) { - if (key.startsWith(ROUTE_PREFIX) && key.endsWith(FIRST)) { - routeTypeTag = key.replace(FIRST, ""); - } - } - - String routeNameTag = routeTypeTag != null ? routeTypeTag + OSMC_TEXT_SUFFIX : null; - - Map searchTags = new LinkedHashMap<>(); - if (routeTypeTag != null) { - searchTags.put(routeTypeTag, tags.get(routeTypeTag + FIRST)); - } - searchTags.putAll(tags); - - for (Map.Entry entry : searchTags.entrySet()) { - final String tag = entry.getKey(); - final String value = entry.getValue(); - - searchRequest.clearState(); - searchRequest.setStringFilter(renderingRules.PROPS.R_TAG, tag); - searchRequest.setStringFilter(renderingRules.PROPS.R_VALUE, value); - if (routeNameTag != null) { - searchRequest.setStringFilter(renderingRules.PROPS.R_NAME_TAG, routeNameTag); - } - searchRequest.search(RenderingRulesStorage.TEXT_RULES); - - RenderingRuleProperty[] props = { - renderingRules.PROPS.R_ICON, - renderingRules.PROPS.R_ICON_2, - renderingRules.PROPS.R_TEXT_SHIELD - }; - - Map result = new HashMap<>(); - for (RenderingRuleProperty p : props) { - String key = p.getAttrName(); - String val = searchRequest.getStringPropertyValue(p); - String validated = substituteAndValidate(val, searchTags); - if (validated != null) { - String convertedKey = suffixOsmcTagsToResult.get(key); - result.put(convertedKey == null ? key : convertedKey, validated); - } - } - - if (!result.isEmpty()) { - for (String key : searchTags.keySet()) { - for (String suffix : suffixOsmcTagsToResult.keySet()) { - if (key.endsWith(suffix)) { - result.put(suffixOsmcTagsToResult.get(suffix), searchTags.get(key)); - } - } - } - return result; - } - } - - return null; - } - - @Nullable - private String substituteAndValidate(@Nullable String in, @Nonnull Map tags) { - if (in != null && in.contains("?")) { - for (String key : tags.keySet()) { - if (in.contains("?" + key + "?")) { - return in.replace("?" + key + "?", tags.get(key)); - } - } - return null; // invalid - } - return in; - } - @Nullable public String searchGpxIconByNode(@Nonnull Node node) { return searchBestPropertyByOsmTags(node.getTags(), diff --git a/java-tools/OsmAndMapCreatorUtilities/src/main/java/net/osmand/routes/RouteRelationExtractor.java b/java-tools/OsmAndMapCreatorUtilities/src/main/java/net/osmand/routes/RouteRelationExtractor.java index f840af5a2..a805fbeee 100644 --- a/java-tools/OsmAndMapCreatorUtilities/src/main/java/net/osmand/routes/RouteRelationExtractor.java +++ b/java-tools/OsmAndMapCreatorUtilities/src/main/java/net/osmand/routes/RouteRelationExtractor.java @@ -347,8 +347,8 @@ private void saveGpx(Entity relation, Map children, File resul waysToJoin.add(way); transformer.addPropogatedTags(finder.renderingTypes, MapRenderingTypesEncoder.EntityConvertApplyType.MAP, way, way.getModifiableTags()); - Map props = finder.searchOsmcPropertiesByFinalTags(way.getTags()); - if (props != null) { + Map props = getShieldTagsFromOsmcTags(way.getTags()); + if (!Algorithms.isEmpty(props)) { if (props.containsKey("color")) { // color is forced by osmc_waycolor metadataExtensions.remove("colour"); @@ -582,4 +582,36 @@ public void loadEntityRelation(Relation e, List parentRelations) throws } } } + + private static final Map osmcTagsToShieldProps = Map.of( + "osmc_text", "shield_text", + "osmc_background", "shield_bg", + "osmc_foreground", "shield_fg", + "osmc_foreground2", "shield_fg_2", + "osmc_textcolor", "shield_textcolor", + "osmc_waycolor", "color" // waycolor is a part of osmc:symbol and must be applied to whole way + ); + + private static final String OSMC_ICON_PREFIX = "osmc_"; + private static final String OSMC_ICON_BG_SUFFIX = "_bg"; + private static final Set shieldBgIcons = Set.of("shield_bg"); + private static final Set shieldFgIcons = Set.of("shield_fg", "sheld_fg_2"); + + @Nonnull + public static Map getShieldTagsFromOsmcTags(@Nonnull Map tags) { + Map result = new HashMap<>(); + for (String tag : tags.keySet()) { + for (String match : osmcTagsToShieldProps.keySet()) { + if (tag.endsWith(match)) { + final String key = osmcTagsToShieldProps.get(match); + final String prefix = + (shieldBgIcons.contains(key) || shieldFgIcons.contains(key)) ? OSMC_ICON_PREFIX : ""; + final String suffix = shieldBgIcons.contains(key) ? OSMC_ICON_BG_SUFFIX : ""; + final String val = prefix + tags.get(tag) + suffix; + result.putIfAbsent(key, val); // prefer 1st + } + } + } + return result; + } }