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 approximation GPX route by Network #485

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@


import static net.osmand.router.RoutingConfiguration.DEFAULT_NATIVE_MEMORY_LIMIT;
import static net.osmand.swing.DataExtractionSettings.getSettings;

import java.awt.Color;
import java.awt.Component;
Expand Down Expand Up @@ -44,6 +45,7 @@
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import net.osmand.router.network.NetworkRouteGpxApproximator;
import org.apache.commons.logging.Log;
import org.json.JSONException;
import org.json.JSONObject;
Expand Down Expand Up @@ -143,21 +145,21 @@ public void destroyLayer() {

public void setStart(LatLon start) {
startRoute = start;
DataExtractionSettings.getSettings().saveStartLocation(start.getLatitude(), start.getLongitude());
getSettings().saveStartLocation(start.getLatitude(), start.getLongitude());
map.repaint();
}

public void setEnd(LatLon end) {
endRoute = end;
DataExtractionSettings.getSettings().saveEndLocation(end.getLatitude(), end.getLongitude());
getSettings().saveEndLocation(end.getLatitude(), end.getLongitude());
map.repaint();
}

@Override
public void initLayer(MapPanel map) {
this.map = map;
startRoute = DataExtractionSettings.getSettings().getStartLocation();
endRoute = DataExtractionSettings.getSettings().getEndLocation();
startRoute = getSettings().getStartLocation();
endRoute = getSettings().getEndLocation();

nextTurn = new JButton(">>"); //$NON-NLS-1$
nextTurn.addActionListener(new ActionListener(){
Expand Down Expand Up @@ -317,16 +319,20 @@ public void actionPerformed(ActionEvent e) {

@Override
public void actionPerformed(ActionEvent e) {
if (selectedGPXFile.hasTrkPt()) {
TrkSegment trkSegment = selectedGPXFile.tracks.get(0).segments.get(0);
startRoute = toLatLon(trkSegment.points.get(0));
endRoute = toLatLon(trkSegment.points.get(trkSegment.points.size() - 1));
List<LatLon> polyline = new ArrayList<LatLon>(trkSegment.points.size());
for (WptPt p : trkSegment.points) {
polyline.add(toLatLon(p));
}
calcRouteGpx(polyline);
}
calcRouteGpxAction(false);
}

};
directions.add(recalculate);
}

if (selectedGPXFile != null) {
Action recalculate = new AbstractAction("Calculate GPX route (Network)") {
private static final long serialVersionUID = 4221853329749387476L;

@Override
public void actionPerformed(ActionEvent e) {
calcRouteGpxAction(true);
}

};
Expand Down Expand Up @@ -388,7 +394,7 @@ public void actionPerformed(ActionEvent e) {
// @Override
// public void run() {
JFileChooser fileChooser = new JFileChooser(
DataExtractionSettings.getSettings().getDefaultWorkingDir());
getSettings().getDefaultWorkingDir());
if (fileChooser.showOpenDialog(map) == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
Gson gson = new Gson();
Expand Down Expand Up @@ -467,7 +473,7 @@ public void actionPerformed(ActionEvent e) {
RouteExporter exporter = new RouteExporter(name, previousRoute, locations, null);
GPXFile gpxFile = exporter.exportRoute();
JFileChooser fileChooser = new JFileChooser(
DataExtractionSettings.getSettings().getDefaultWorkingDir());
getSettings().getDefaultWorkingDir());
if (fileChooser.showSaveDialog(map) == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
GPXUtilities.writeGpxFile(file, gpxFile);
Expand Down Expand Up @@ -529,7 +535,7 @@ public void actionPerformed(ActionEvent e) {
GPXFile res = calculateAltitude(selectedGPXFile, missingFile);
if (res == null || missingFile[0] != null) {
String msg = missingFile[0] != null ? "Missing in 'srtm' folder: " + missingFile[0].getName()
: ("Missing 'srtm' folder: " + DataExtractionSettings.getSettings().getBinaryFilesDir() + "/srtm");
: ("Missing 'srtm' folder: " + getSettings().getBinaryFilesDir() + "/srtm");
JOptionPane.showMessageDialog(OsmExtractionUI.MAIN_APP.getFrame(), msg, "Missing srtm data",
JOptionPane.INFORMATION_MESSAGE);
} else {
Expand Down Expand Up @@ -582,6 +588,19 @@ public void actionPerformed(ActionEvent e) {

}

private void calcRouteGpxAction(boolean byNetwork) {
if (selectedGPXFile.hasTrkPt()) {
TrkSegment trkSegment = selectedGPXFile.tracks.get(0).segments.get(0);
startRoute = toLatLon(trkSegment.points.get(0));
endRoute = toLatLon(trkSegment.points.get(trkSegment.points.size() - 1));
List<LatLon> polyline = new ArrayList<>(trkSegment.points.size());
for (WptPt p : trkSegment.points) {
polyline.add(toLatLon(p));
}
calcRouteGpx(polyline, byNetwork);
}
}


protected void displayTrackInfo(GPXFile gpxFile, String header) {
GPXTrackAnalysis analysis = selectedGPXFile.getAnalysis(gpxFile.modifiedTime);
Expand Down Expand Up @@ -620,7 +639,7 @@ private int showOptionColorSchemeDialog(JMenu frame) {


protected GPXFile calculateAltitude(GPXFile gpxFile, File[] missingFile) {
File srtmFolder = new File(DataExtractionSettings.getSettings().getBinaryFilesDir(), "srtm");
File srtmFolder = new File(getSettings().getBinaryFilesDir(), "srtm");
if (!srtmFolder.exists()) {
return null;
}
Expand Down Expand Up @@ -766,21 +785,24 @@ private LatLon toLatLon(WptPt wptPt) {
return new LatLon(wptPt.lat, wptPt.lon);
}

private void calcRouteGpx(List<LatLon> polyline) {
private void calcRouteGpx(List<LatLon> polyline, boolean byNetwork) {
new Thread() {
@Override
public void run() {
List<Entity> entities = selfRoute(startRoute, endRoute, polyline, true, null, RouteCalculationMode.NORMAL);
if (entities != null) {
DataTileManager<Entity> points = new DataTileManager<Entity>(11);
for (Entity w : entities) {
LatLon n = w.getLatLon();
points.registerObject(n.getLatitude(), n.getLongitude(), w);
List<Entity> entities;
if(byNetwork){
try {
NetworkRouteGpxApproximator gpxApproximator =
new NetworkRouteGpxApproximator(getSettings().getObfReaders(), true);
gpxApproximator.setGpxFile(selectedGPXFile);
entities = gpxApproximator.approximate();
} catch (IOException e) {
throw new RuntimeException(e);
}
map.setPoints(points);
map.fillPopupActions();
}else {
entities = selfRoute(startRoute, endRoute, polyline, true, null, RouteCalculationMode.NORMAL);
}

outputPoints(entities);
if (selectedGPXFile != null) {
JOptionPane.showMessageDialog(OsmExtractionUI.MAIN_APP.getFrame(), "Check validity of GPX File",
"GPX route correction", JOptionPane.INFORMATION_MESSAGE);
Expand All @@ -789,25 +811,28 @@ public void run() {
}.start();
}


private void calcRoute(final RouteCalculationMode m) {
new Thread() {
@Override
public void run() {
List<Entity> res = selfRoute(startRoute, endRoute, intermediates, false, previousRoute, m);
if (res != null) {
DataTileManager<Entity> points = new DataTileManager<Entity>(11);
for (Entity w : res) {
LatLon n = w.getLatLon();
points.registerObject(n.getLatitude(), n.getLongitude(), w);
}
map.setPoints(points);
map.fillPopupActions();
}
outputPoints(res);
}
}.start();
}

private void outputPoints(List<Entity> entities) {
if (entities != null) {
DataTileManager<Entity> points = new DataTileManager<Entity>(11);
for (Entity w : entities) {
LatLon n = w.getLatLon();
points.registerObject(n.getLatitude(), n.getLongitude(), w);
}
map.setPoints(points);
map.fillPopupActions();
}
}

public static List<Way> route_YOURS(LatLon start, LatLon end){
List<Way> res = new ArrayList<Way>();
long time = System.currentTimeMillis();
Expand Down Expand Up @@ -1043,7 +1068,7 @@ public static List<Way> route_OSRM(LatLon start, LatLon end){
if (start != null && end != null) {
try {
StringBuilder uri = new StringBuilder();
uri.append(DataExtractionSettings.getSettings().getOsrmServerAddress());
uri.append(getSettings().getOsrmServerAddress());
uri.append("/viaroute?");
uri.append("&loc=").append(start.getLatitude()).append(",").append(start.getLongitude());
uri.append("&loc=").append(end.getLatitude()).append(",").append(end.getLongitude());
Expand Down Expand Up @@ -1104,7 +1129,7 @@ public List<Entity> selfRoute(LatLon start, LatLon end, List<LatLon> intermediat
long time = System.currentTimeMillis();


final boolean animateRoutingCalculation = DataExtractionSettings.getSettings().isAnimateRouting();
final boolean animateRoutingCalculation = getSettings().isAnimateRouting();
if(animateRoutingCalculation) {
nextTurn.setVisible(true);
playPauseButton.setVisible(true);
Expand All @@ -1117,13 +1142,13 @@ public List<Entity> selfRoute(LatLon start, LatLon end, List<LatLon> intermediat
System.out.println("Self made route from " + start + " to " + end);
if (start != null && end != null) {
try {
BinaryMapIndexReader[] files = DataExtractionSettings.getSettings().getObfReaders();
BinaryMapIndexReader[] files = getSettings().getObfReaders();
if (files.length == 0) {
JOptionPane.showMessageDialog(OsmExtractionUI.MAIN_APP.getFrame(),
"Please specify obf file in settings", "Obf file not found", JOptionPane.ERROR_MESSAGE);
return null;
}
String m = DataExtractionSettings.getSettings().getRouteMode();
String m = getSettings().getRouteMode();
String[] props = m.split("\\,");
RoutePlannerFrontEnd router = new RoutePlannerFrontEnd();

Expand All @@ -1136,7 +1161,7 @@ public List<Entity> selfRoute(LatLon start, LatLon end, List<LatLon> intermediat
}
}
RoutingMemoryLimits memoryLimit = new RoutingMemoryLimits(2000, DEFAULT_NATIVE_MEMORY_LIMIT * 10);
RoutingConfiguration config = DataExtractionSettings.getSettings().getRoutingConfig().
RoutingConfiguration config = getSettings().getRoutingConfig().
// addImpassableRoad(6859437l).
// addImpassableRoad(46859655089l).
setDirectionPoints(directionPointsFile)
Expand Down Expand Up @@ -1165,7 +1190,7 @@ public List<Entity> selfRoute(LatLon start, LatLon end, List<LatLon> intermediat
config.routeCalculationTime = System.currentTimeMillis();

final RoutingContext ctx = router.buildRoutingContext(config,
DataExtractionSettings.getSettings().useNativeRouting()
getSettings().useNativeRouting()
? NativeSwingRendering.getDefaultFromSettings()
: null,
files, rm);
Expand Down