Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add basic controls for the routing helper reimplementation #25

Draft
wants to merge 17 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
023ec80
Start creating the routing helper rewrite, at the moment just a bit o…
floscher Aug 24, 2020
08c2706
Automatically open routing helper when selecting a route relation, ma…
floscher Nov 15, 2020
928c168
Add really basic transport mode detection to routing help, currently …
floscher Nov 15, 2020
0aa7bb6
I completed the BusTransportMode, so it works for all kinds of buses.
PolyglotOpenstreetmap Nov 15, 2020
2395f15
I created a BicycleTransportMode
PolyglotOpenstreetmap Nov 15, 2020
3d3cc48
I created a PedestrianTransportMode and a HorseTransportMode. I also …
PolyglotOpenstreetmap Nov 15, 2020
bc77476
Make the suitable ways in transport modes a static field
PolyglotOpenstreetmap Nov 16, 2020
ff36ed1
Rename routing helper to route explorer
floscher Nov 16, 2020
f23c46e
I factored out the code duplication. It made the code less readable t…
PolyglotOpenstreetmap Nov 16, 2020
92fc292
created a test for the BusTransportMode, canTurn still needs to be added
PolyglotOpenstreetmap Nov 20, 2020
8e57521
fixed the tests and the implementation of canTraverseWay
PolyglotOpenstreetmap Nov 20, 2020
1d15ad9
made the BusTransportModeTest somewhat more extensive for testCanTrav…
PolyglotOpenstreetmap Nov 20, 2020
d3ec984
I tried to add tests for canTurn, but I'm stuck on line 46 of Abstrac…
PolyglotOpenstreetmap Nov 20, 2020
cab5fd5
All the tests are passing. Prohibiting turn restrictions "no_" are te…
PolyglotOpenstreetmap Nov 21, 2020
58f97e5
I added tests for the mandatory types, but the result with only asser…
PolyglotOpenstreetmap Nov 21, 2020
5055bc5
Oops, fix copy/paste error
PolyglotOpenstreetmap Nov 21, 2020
56b6c9b
Turn restrictions seem to be a can of worms...
PolyglotOpenstreetmap Nov 21, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ plugin.canloadatruntime=true
plugin.icon=images/bus.svg

# Minimum JOSM version (any numeric version)
plugin.main.version=15238
plugin.main.version=15418
# JOSM version to compile against (any numeric version available for download, or the special values "tested" or "latest")
plugin.compile.version=17084
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import org.openstreetmap.josm.plugins.pt_assistant.actions.SortPTRouteMembersAction;
import org.openstreetmap.josm.plugins.pt_assistant.actions.SortPTRouteMembersMenuBar;
import org.openstreetmap.josm.plugins.pt_assistant.actions.SplitRoundaboutAction;
import org.openstreetmap.josm.plugins.pt_assistant.routeexplorer.RouteExplorer;
import org.openstreetmap.josm.plugins.pt_assistant.data.PTRouteSegment;
import org.openstreetmap.josm.plugins.pt_assistant.gui.PTAssistantLayerManager;
import org.openstreetmap.josm.plugins.pt_assistant.validation.BicycleFootRouteValidatorTest;
Expand Down Expand Up @@ -79,6 +80,7 @@ public PTAssistantPlugin(PluginInformation info) {
.addMenu("File", trc("menu", "Public Transport"), KeyEvent.VK_P, 5, ht("/Menu/Public Transport"));
addToMenu(PublicTransportMenu);

SelectionEventManager.getInstance().addSelectionListener(new RouteExplorer());
SelectionEventManager.getInstance().addSelectionListener(PTAssistantLayerManager.PTLM);
KeyboardFocusManager.getCurrentKeyboardFocusManager().addPropertyChangeListener(PTAssistantLayerManager.PTLM);
initialiseWizard();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
// License: GPL. For details, see LICENSE file.
package org.openstreetmap.josm.plugins.pt_assistant.routeexplorer;

import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.stream.Collectors;

import javax.swing.JOptionPane;

import com.drew.lang.annotations.NotNull;
import org.openstreetmap.josm.data.osm.AbstractPrimitive;
import org.openstreetmap.josm.data.osm.DataSelectionListener;
import org.openstreetmap.josm.data.osm.Relation;
import org.openstreetmap.josm.data.osm.RelationMember;
import org.openstreetmap.josm.gui.MainApplication;
import org.openstreetmap.josm.gui.MapFrame;
import org.openstreetmap.josm.gui.Notification;
import org.openstreetmap.josm.gui.dialogs.relation.DownloadRelationMemberTask;
import org.openstreetmap.josm.plugins.pt_assistant.routeexplorer.transportmode.ITransportMode;
import org.openstreetmap.josm.plugins.pt_assistant.utils.DialogUtils;
import org.openstreetmap.josm.plugins.pt_assistant.utils.RouteUtils;
import org.openstreetmap.josm.plugins.pt_assistant.utils.WayUtils;
import org.openstreetmap.josm.tools.I18n;

public class RouteExplorer implements DataSelectionListener {
@NotNull
private Optional<ITransportMode> activeTransportMode = Optional.empty();

private final RouteExplorerPanel routingHelperPanel = new RouteExplorerPanel(this);

@NotNull
private Optional<Relation> currentRelation = Optional.empty();

@NotNull
private Optional<RelationMember> currentMember = Optional.empty();

@Override
public void selectionChanged(final SelectionChangeEvent event) {
final MapFrame mapframe = MainApplication.getMap();
if (mapframe != null) {
final Optional<Relation> singleRelationSelection = Optional.of(event.getSelection())
.filter(selection -> selection.size() == 1)
.map(selection -> selection.iterator().next())
.map(selectedPrimitive -> selectedPrimitive instanceof Relation ? (Relation) selectedPrimitive : null)
.filter(RouteUtils::isRoute);
this.currentRelation = singleRelationSelection;
this.activeTransportMode = currentRelation.flatMap(relation -> ITransportMode.TRANSPORT_MODES.stream().filter(it -> it.canBeUsedForRelation(relation)).findFirst());
if (singleRelationSelection.isPresent()) {
routingHelperPanel.onRelationChange(singleRelationSelection.get(), activeTransportMode);
if (mapframe.getTopPanel(RouteExplorerPanel.class) == null) {
mapframe.addTopPanel(routingHelperPanel);
}
} else {
mapframe.removeTopPanel(RouteExplorerPanel.class);
}
}
}

public void goToFirstWay() {
final Optional<Relation> currentRelation = this.currentRelation;
final long missingMembersCount = currentRelation
.map(it ->
it.getMembers().stream()
.filter(member -> member.getMember().isIncomplete())
.count()
)
.orElse(0L);
if (missingMembersCount > 0) {
if (
DialogUtils.showYesNoQuestion(
routingHelperPanel,
I18n.tr("Relation is incomplete"),
I18n.trn(
"The relations has {0} missing member. Would you like to download the missing member now?",
"The relations has {0} missing members. Would you like to download the missing members now?",
missingMembersCount,
missingMembersCount
)
)
) {
final Future<?> f = MainApplication.worker.submit(new DownloadRelationMemberTask(
currentRelation.get(),
currentRelation.get().getMembers().stream()
.map(RelationMember::getMember)
.filter(AbstractPrimitive::isIncomplete)
.collect(Collectors.toSet()),
MainApplication.getLayerManager().getActiveDataLayer()
));
new Thread(() -> {
try {
f.get();

// try again, now the missingMembersCount should be 0, so we should go to the else-branch this time
goToFirstWay();
} catch (CancellationException | InterruptedException | ExecutionException e) {
JOptionPane.showMessageDialog(
routingHelperPanel,
I18n.tr("The download of missing members has failed!"),
I18n.tr("Download failed"),
JOptionPane.ERROR_MESSAGE
);
}

}).start();
}
} else {
final List<RelationMember> wayMembers = currentRelation.map(relation -> relation.getMembers().stream().filter(RouteUtils::isRouteWayMember).collect(Collectors.toList())).orElse(Collections.emptyList());
this.currentMember = wayMembers.stream().findFirst();
if (wayMembers.isEmpty()) {
JOptionPane.showMessageDialog(routingHelperPanel, "No way found to traverse", "Could not find a way to traverse", JOptionPane.ERROR_MESSAGE);
} else {
routingHelperPanel.onCurrentWayChange(
currentRelation.get(),
wayMembers.get(0),
RouteExplorerPanel.ConnectionType.END,
wayMembers.size() == 1
? RouteExplorerPanel.ConnectionType.END
: (
WayUtils.isTouchingOtherWay(wayMembers.get(0).getWay(), wayMembers.get(1).getWay())
? RouteExplorerPanel.ConnectionType.CONNECTED
: RouteExplorerPanel.ConnectionType.NOT_CONNECTED
),
0,
wayMembers.size()
);
}
}
}

public void goToPreviousGap() {
JOptionPane.showMessageDialog(routingHelperPanel, "Not implemented yet", "Not implemented", JOptionPane.ERROR_MESSAGE);
}

public void goToPreviousWay() {
goNWaysForward(-1);
}

public void goToNextWay() {
goNWaysForward(1);
}

private void goNWaysForward(final int n) {
currentRelation.ifPresent(relation ->
currentMember.ifPresent(member -> {
final List<RelationMember> wayMembers = relation.getMembers().stream().filter(RouteUtils::isRouteWayMember).collect(Collectors.toList());
final int targetIndex = wayMembers.indexOf(member) + n;
if (targetIndex < 0 || targetIndex >= wayMembers.size() - 1) {
new Notification(I18n.tr("You reached the end of the route")).setIcon(JOptionPane.INFORMATION_MESSAGE).setDuration(Notification.TIME_SHORT).show();
} else {
currentMember = Optional.of(wayMembers.get(targetIndex));
routingHelperPanel.onCurrentWayChange(
relation,
wayMembers.get(targetIndex),
targetIndex <= 0 ? RouteExplorerPanel.ConnectionType.END : (
WayUtils.isTouchingOtherWay(wayMembers.get(targetIndex).getWay(), wayMembers.get(targetIndex - 1).getWay())
? RouteExplorerPanel.ConnectionType.CONNECTED
: RouteExplorerPanel.ConnectionType.NOT_CONNECTED
),
targetIndex >= wayMembers.size() - 1 ? RouteExplorerPanel.ConnectionType.END : (
WayUtils.isTouchingOtherWay(wayMembers.get(targetIndex).getWay(), wayMembers.get(targetIndex + 1).getWay())
? RouteExplorerPanel.ConnectionType.CONNECTED
: RouteExplorerPanel.ConnectionType.NOT_CONNECTED
),
targetIndex,
wayMembers.size()
);
}
})
);
}

public void goToNextGap() {
JOptionPane.showMessageDialog(routingHelperPanel, "Not implemented yet", "Not implemented", JOptionPane.ERROR_MESSAGE);
}
}
Loading