From 33dfd0f039e1de36cdd90cd225853ea3e7723cdf Mon Sep 17 00:00:00 2001 From: RZR-UA Date: Wed, 4 Dec 2024 19:46:56 +0100 Subject: [PATCH] Replace osm_*_present tags with hasOsmRouteId() --- .../main/java/net/osmand/data/Amenity.java | 6 +++ .../plus/wikivoyage/data/TravelArticle.java | 5 +++ .../plus/wikivoyage/data/TravelObfHelper.java | 40 +++++++++---------- 3 files changed, 31 insertions(+), 20 deletions(-) diff --git a/OsmAnd-java/src/main/java/net/osmand/data/Amenity.java b/OsmAnd-java/src/main/java/net/osmand/data/Amenity.java index 438ba0d2034..22ed3196b22 100644 --- a/OsmAnd-java/src/main/java/net/osmand/data/Amenity.java +++ b/OsmAnd-java/src/main/java/net/osmand/data/Amenity.java @@ -53,6 +53,7 @@ public class Amenity extends MapObject { public static final String IS_AGGR_PART = "is_aggr_part"; public static final String CONTENT_JSON = "content_json"; public static final String ROUTE_ID = "route_id"; + public static final String ROUTE_ID_OSM_PREFIX = "OSM"; public static final String ROUTE_SOURCE = "route_source"; public static final String ROUTE_NAME = "route_name"; public static final String COLOR = "color"; @@ -452,6 +453,11 @@ public String getRouteId() { return getAdditionalInfo(ROUTE_ID); } + public boolean hasOsmRouteId() { + String routeId = getRouteId(); + return routeId != null && routeId.startsWith(ROUTE_ID_OSM_PREFIX); + } + public String getGpxFileName(String lang) { final String gpxFileName = lang != null ? getName(lang) : getEnName(true); if (!Algorithms.isEmpty(gpxFileName)) { diff --git a/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelArticle.java b/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelArticle.java index 66e06b3f47b..218a591c8a3 100644 --- a/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelArticle.java +++ b/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelArticle.java @@ -127,6 +127,11 @@ public String getRouteId() { return routeId; } + public boolean hasOsmRouteId() { + String routeId = getRouteId(); + return routeId != null && routeId.startsWith(Amenity.ROUTE_ID_OSM_PREFIX); + } + @NonNull public String getGpxFileName() { String gpxFileName = !Algorithms.isEmpty(title) ? title : routeId; diff --git a/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelObfHelper.java b/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelObfHelper.java index f65fe98d8e2..7d3dcc05bfd 100644 --- a/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelObfHelper.java +++ b/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelObfHelper.java @@ -123,16 +123,16 @@ public class TravelObfHelper implements TravelHelper { private final List> foundAmenities = new ArrayList<>(); public volatile int requestNumber = 0; + // Keep important tags by prefix. Note: name, ref, type, and route tags are processed in a special way. + private static final Set saveAsIsAmenityGpxTags = Set.of( + "route_id", "flexible_line_width", "translucent_line_colors", "shield_" + ); // Do not clutter GPX with tags that are always generated. - private static final Set avoidAmenityGpxTags = Set.of( + private static final Set doNotSaveAmenityGpxTags = Set.of( "date", "distance", "route_name", "route_radius", "avg_ele", "min_ele", "max_ele", "start_ele", "ele_graph", "diff_ele_up", "diff_ele_down", "avg_speed", "min_speed", "max_speed", "time_moving", "time_moving_no_gaps", "time_span", "time_span_no_gaps" ); - // Keep important tags by prefix. Note: name, ref, type, and route tags are processed in a special way. - private static final Set keepAsIsAmenityGpxTags = Set.of( - "route_id", "flexible_line_width", "translucent_line_colors", "shield_" - ); public TravelObfHelper(OsmandApplication app) { this.app = app; @@ -1169,15 +1169,19 @@ public boolean publish(Amenity amenity) { if (tag.startsWith(OBF_GPX_EXTENSION_TAG_PREFIX)) { String gpxTag = tag.replaceFirst(OBF_GPX_EXTENSION_TAG_PREFIX, ""); gpxFileExtensions.put(gpxTag, value); - } else if (!avoidAmenityGpxTags.contains(tag)) { - String gpxTag = OSM_PREFIX + tag; - for (String prefix : keepAsIsAmenityGpxTags) { + } else if (!doNotSaveAmenityGpxTags.contains(tag)) { + boolean saveAsIs = false; + for (String prefix : saveAsIsAmenityGpxTags) { if (tag.startsWith(prefix)) { - gpxTag = tag; + saveAsIs = true; break; } } - gpxFileExtensions.put(gpxTag, value); + if (saveAsIs) { + gpxFileExtensions.put(tag, value); + } else if (amenity.hasOsmRouteId()) { + gpxFileExtensions.put(OSM_PREFIX + tag, value); + } } } } @@ -1205,8 +1209,10 @@ private void reconstructGpxTagsFromAmenityType(Amenity amenity, Map re if (article instanceof TravelGpx) { gpxFile = new GpxFile(Version.getFullVersion(app)); gpxFile.getMetadata().setName(Objects.requireNonNullElse(article.title, article.routeId)); // path is name - if (!Algorithms.isEmpty(article.title)) { + if (!Algorithms.isEmpty(article.title) && article.hasOsmRouteId()) { gpxFileExtensions.putIfAbsent(OSM_PREFIX + "name", article.title); } if (!Algorithms.isEmpty(article.description)) { gpxFile.getMetadata().setDesc(article.description); } - final String[] cleanupByPresenceTags = { "ref", "name", "description" }; // osm_ref_present, etc - for (String tag : cleanupByPresenceTags) { - if (!gpxFileExtensions.containsKey("osm_" + tag + "_present")) { - gpxFileExtensions.remove(OSM_PREFIX + tag); - } - } } else { String description = article.getDescription(); String title = FileUtils.isValidFileName(description) ? description : article.getTitle(); @@ -1307,7 +1307,7 @@ private synchronized GpxFile buildGpxFile(@NonNull List re if (gpxFileExtensions.containsKey(GpxUtilities.ACTIVITY_TYPE)) { gpxFile.getMetadata().getExtensionsToWrite() .put(GpxUtilities.ACTIVITY_TYPE, gpxFileExtensions.get(GpxUtilities.ACTIVITY_TYPE)); - gpxFileExtensions.remove(GpxUtilities.ACTIVITY_TYPE); // activities live in the metadata + gpxFileExtensions.remove(GpxUtilities.ACTIVITY_TYPE); // move activity to metadata } gpxFile.getExtensionsToWrite().putAll(gpxFileExtensions); // finally }