Skip to content

Commit

Permalink
Refactor searchOsmcPropertiesByFinalTags into getShieldTagsFromOsmcTags
Browse files Browse the repository at this point in the history
  • Loading branch information
RZR-UA committed Oct 25, 2024
1 parent 44b3811 commit 8653b5b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,6 @@ public class FindByRenderingTypesRules {
public final MapRenderingTypesEncoder renderingTypes;
private final RenderingRuleSearchRequest searchRequest;

private static final Map<String, String> 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);
}
Expand Down Expand Up @@ -111,82 +102,6 @@ private static Map<String, String> readRenderingConstantsFromIS(InputStream is)
return renderingConstants;
}

@Nullable
public Map<String, String> searchOsmcPropertiesByFinalTags(@Nonnull Map<String, String> 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<String, String> searchTags = new LinkedHashMap<>();
if (routeTypeTag != null) {
searchTags.put(routeTypeTag, tags.get(routeTypeTag + FIRST));
}
searchTags.putAll(tags);

for (Map.Entry<String, String> 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<String, String> 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<String, String> 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(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -347,8 +347,8 @@ private void saveGpx(Entity relation, Map<EntityId, Entity> children, File resul
waysToJoin.add(way);
transformer.addPropogatedTags(finder.renderingTypes,
MapRenderingTypesEncoder.EntityConvertApplyType.MAP, way, way.getModifiableTags());
Map<String, String> props = finder.searchOsmcPropertiesByFinalTags(way.getTags());
if (props != null) {
Map<String, String> props = getShieldTagsFromOsmcTags(way.getTags());
if (!Algorithms.isEmpty(props)) {
if (props.containsKey("color")) {
// color is forced by osmc_waycolor
metadataExtensions.remove("colour");
Expand Down Expand Up @@ -582,4 +582,36 @@ public void loadEntityRelation(Relation e, List<Entity> parentRelations) throws
}
}
}

private static final Map<String, String> osmcTagsToShieldProps = Map.of(
"osmc_waycolor", "color",
"osmc_text", "shield_text",
"osmc_background", "shield_bg",
"osmc_foreground", "shield_fg",
"osmc_foreground2", "shield_fg_2",
"osmc_textcolor", "shield_textcolor"
);

private static final String OSMC_ICON_PREFIX = "osmc_";
private static final String OSMC_ICON_BG_SUFFIX = "_bg";
private static final Set<String> shieldBgIcons = Set.of("shield_bg");
private static final Set<String> shieldFgIcons = Set.of("shield_fg", "sheld_fg_2");

@Nonnull
public static Map<String, String> getShieldTagsFromOsmcTags(@Nonnull Map<String, String> tags) {
Map<String, String> 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);
}
}
}
return result;
}
}

0 comments on commit 8653b5b

Please sign in to comment.