Skip to content

Commit

Permalink
Add isUniqueClickableWay(), refactor previous code
Browse files Browse the repository at this point in the history
  • Loading branch information
RZR-UA committed Jan 14, 2025
1 parent ef1731f commit 9923629
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 23 deletions.
8 changes: 8 additions & 0 deletions OsmAnd/src/net/osmand/plus/track/clickable/ClickableWay.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ public ClickableWay(@NonNull GpxFile gpxFile, long osmId, @Nullable String name,
this.selectedGpxPoint = new SelectedGpxPoint(null, wpt);
}

public long getOsmId() {
return osmId;
}

public GpxFile getGpxFile() {
return gpxFile;
}
Expand All @@ -34,6 +38,10 @@ public SelectedGpxPoint getSelectedGpxPoint() {
return selectedGpxPoint;
}

public String getGpxFileName() {
return Algorithms.sanitizeFileName(getWayName());
}

public String getWayName() {
return Algorithms.isEmpty(name) ? Long.toString(osmId) : name;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import net.osmand.shared.gpx.GpxFile;
import net.osmand.shared.gpx.GpxTrackAnalysis;
import net.osmand.shared.gpx.primitives.WptPt;
import net.osmand.util.Algorithms;

import java.io.File;
import java.util.List;
Expand All @@ -40,8 +39,8 @@ public boolean showMenuAction(@Nullable Object object) {
GpxFile gpxFile = that.getGpxFile();
GpxTrackAnalysis analysis = gpxFile.getAnalysis(0);
WptPt selectedPoint = that.getSelectedGpxPoint().getSelectedPoint();
String safeFileName = Algorithms.sanitizeFileName(that.getWayName());
File file = new File(FileUtils.getTempDir(app), safeFileName + GPX_FILE_EXT);
String safeFileName = that.getGpxFileName() + GPX_FILE_EXT;
File file = new File(FileUtils.getTempDir(app), safeFileName);
GpxUiHelper.saveAndOpenGpx(mapActivity, file, gpxFile, selectedPoint, analysis, null, true);
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import static net.osmand.data.MapObject.AMENITY_ID_RIGHT_SHIFT;

// THINK use similar icon="piste_high_difficulty" for no-name pistes
// THINK auto-reverse Way (assume downhill OR detect start by minDist to currentLocation)

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

Expand Down Expand Up @@ -137,16 +140,14 @@ private ClickableWay searchClickableWay(LatLon searchLatLon, int searchRadius,

String color = getGpxColorByTags(tags);
if (color != null) {
System.err.printf("XXX color (%s)\n", color);
gpxFile.setColor(color);
}

// TODO check unique gpx
// TODO cache <id, GpxFile>
// TODO close previous on open new
// TODO fetch elevation data from routing-section

// TODO fetch elevation data from routing-section - RouteSectionReader.java

// TODO calc distance stats, elevation stats, etc (automatically if data exists)
// THINK auto-reverse Way (assume downhill OR detect start by minDist to currentLocation)

return new ClickableWay(gpxFile, osmId, name, searchLatLon);
}
Expand Down
47 changes: 32 additions & 15 deletions OsmAnd/src/net/osmand/plus/views/layers/MapSelectionHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ private Amenity getAmenity(LatLon latLon, ObfMapObject obfMapObject, Map<String,

private boolean addTravelGpx(@NonNull MapSelectionResult result, @Nullable String routeId, @Nullable String ref) {
TravelGpx travelGpx = app.getTravelHelper().searchGpx(result.pointLatLon, routeId, ref);
if (travelGpx != null && isUniqueGpx(result.selectedObjects, travelGpx)) {
if (travelGpx != null && isUniqueTravelGpx(result.selectedObjects, travelGpx)) {
WptPt selectedPoint = new WptPt();
selectedPoint.setLat(result.pointLatLon.getLatitude());
selectedPoint.setLon(result.pointLatLon.getLongitude());
Expand All @@ -492,7 +492,7 @@ private boolean addTravelGpx(@NonNull MapSelectionResult result, @Nullable Strin

private boolean addClickableWayV1(@NonNull MapSelectionResult result, @NonNull RenderedObject renderedObject) {
ClickableWay clickableWay = clickableWayLoader.searchClickableWayV1(result.pointLatLon, renderedObject);
if (clickableWay != null) {
if (clickableWay != null && isUniqueClickableWay(result.selectedObjects, clickableWay)) {
result.selectedObjects.put(clickableWay, clickableWayLoader.getContextMenuProvider());
return true;
}
Expand All @@ -502,16 +502,39 @@ private boolean addClickableWayV1(@NonNull MapSelectionResult result, @NonNull R
private boolean addClickableWayV2(@NonNull MapSelectionResult result, @NonNull ObfMapObject obfMapObject,
@NonNull Map<String, String> tags) {
ClickableWay clickableWay = clickableWayLoader.searchClickableWayV2(result.pointLatLon, obfMapObject, tags);
if (clickableWay != null) {
if (clickableWay != null && isUniqueClickableWay(result.selectedObjects, clickableWay)) {
result.selectedObjects.put(clickableWay, clickableWayLoader.getContextMenuProvider());
return true;
}
return false;
}

private boolean isUniqueGpx(@NonNull Map<Object, IContextMenuProvider> selectedObjects,
@NonNull TravelGpx travelGpx) {
String travelGpxFileName = travelGpx.getGpxFileName() + GPX_FILE_EXT;
private boolean isUniqueGpxFileName(Map<Object, IContextMenuProvider> selectedObjects, String gpxFileName) {
for (Map.Entry<Object, IContextMenuProvider> entry : selectedObjects.entrySet()) {
if (entry.getKey() instanceof SelectedGpxPoint && entry.getValue() instanceof GPXLayer) {
SelectedGpxPoint selectedGpxPoint = (SelectedGpxPoint) entry.getKey();
if (selectedGpxPoint.getSelectedGpxFile().getGpxFile().getPath().endsWith(gpxFileName)) {
return false;
}
}
}
return true;
}

private boolean isUniqueClickableWay(@NonNull Map<Object, IContextMenuProvider> selectedObjects,
@NonNull ClickableWay clickableWay) {
for (Object object : selectedObjects.keySet()) {
if (object instanceof ClickableWay that) {
if (clickableWay.getOsmId() == that.getOsmId()) {
return false;
}
}
}
return isUniqueGpxFileName(selectedObjects, clickableWay.getGpxFileName() + GPX_FILE_EXT);
}

private boolean isUniqueTravelGpx(@NonNull Map<Object, IContextMenuProvider> selectedObjects,
@NonNull TravelGpx travelGpx) {
for (Map.Entry<Object, IContextMenuProvider> entry : selectedObjects.entrySet()) {
if (entry.getKey() instanceof Pair && entry.getValue() instanceof GPXLayer
&& ((Pair<?, ?>) entry.getKey()).first instanceof TravelGpx) {
Expand All @@ -520,14 +543,8 @@ private boolean isUniqueGpx(@NonNull Map<Object, IContextMenuProvider> selectedO
return false;
}
}
if (entry.getKey() instanceof SelectedGpxPoint && entry.getValue() instanceof GPXLayer) {
SelectedGpxPoint selectedGpxPoint = (SelectedGpxPoint) entry.getKey();
if (selectedGpxPoint.getSelectedGpxFile().getGpxFile().getPath().endsWith(travelGpxFileName)) {
return false;
}
}
}
return true;
return isUniqueGpxFileName(selectedObjects, travelGpx.getGpxFileName() + GPX_FILE_EXT);
}

private void addOsmRoute(@NonNull MapSelectionResult result, @NonNull RotatedTileBox tileBox, @NonNull PointF point,
Expand Down Expand Up @@ -582,13 +599,13 @@ private void putRouteGpxToSelected(@NonNull Map<Object, IContextMenuProvider> se
log.error(e);
}
for (RouteKey routeKey : routes.keySet()) {
if (isUniqueRoute(selectedObjects.keySet(), routeKey)) {
if (isUniqueOsmRoute(selectedObjects.keySet(), routeKey)) {
selectedObjects.put(new Pair<>(routeKey, rect), provider);
}
}
}

private boolean isUniqueRoute(@NonNull Set<Object> set, @NonNull RouteKey tmpRouteKey) {
private boolean isUniqueOsmRoute(@NonNull Set<Object> set, @NonNull RouteKey tmpRouteKey) {
for (Object selectedObject : set) {
if (selectedObject instanceof Pair && ((Pair<?, ?>) selectedObject).first instanceof RouteKey) {
RouteKey routeKey = (RouteKey) ((Pair<?, ?>) selectedObject).first;
Expand Down

0 comments on commit 9923629

Please sign in to comment.