use of net.osmand.data.DataTileManager in project OsmAnd-tools by osmandapp.
the class MapRouterLayer method selfRoute.
public List<Way> selfRoute(LatLon start, LatLon end, List<LatLon> intermediates, List<RouteSegmentResult> previousRoute, RouteCalculationMode rm) {
List<Way> res = new ArrayList<Way>();
long time = System.currentTimeMillis();
List<File> files = new ArrayList<File>();
for (File f : Algorithms.getSortedFilesVersions(new File(DataExtractionSettings.getSettings().getBinaryFilesDir()))) {
if (f.getName().endsWith(".obf")) {
files.add(f);
}
}
final boolean animateRoutingCalculation = DataExtractionSettings.getSettings().isAnimateRouting();
if (animateRoutingCalculation) {
nextTurn.setVisible(true);
playPauseButton.setVisible(true);
stopButton.setVisible(true);
pause = true;
playPauseButton.setText("Play");
}
stop = false;
if (files.isEmpty()) {
JOptionPane.showMessageDialog(OsmExtractionUI.MAIN_APP.getFrame(), "Please specify obf file in settings", "Obf file not found", JOptionPane.ERROR_MESSAGE);
return null;
}
System.out.println("Self made route from " + start + " to " + end);
if (start != null && end != null) {
try {
BinaryMapIndexReader[] rs = new BinaryMapIndexReader[files.size()];
int it = 0;
for (File f : files) {
// $NON-NLS-1$ //$NON-NLS-2$
RandomAccessFile raf = new RandomAccessFile(f, "r");
rs[it++] = new BinaryMapIndexReader(raf, f);
}
String m = DataExtractionSettings.getSettings().getRouteMode();
String[] props = m.split("\\,");
RoutePlannerFrontEnd router = new RoutePlannerFrontEnd(USE_OLD_ROUTING);
Map<String, String> paramsR = new LinkedHashMap<String, String>();
for (String p : props) {
if (p.contains("=")) {
paramsR.put(p.split("=")[0], p.split("=")[1]);
} else {
paramsR.put(p, "true");
}
}
RoutingConfiguration config = DataExtractionSettings.getSettings().getRoutingConfig().build(props[0], /*RoutingConfiguration.DEFAULT_MEMORY_LIMIT*/
1000, paramsR);
PrecalculatedRouteDirection precalculatedRouteDirection = null;
// Test gpx precalculation
// LatLon[] lts = parseGPXDocument("/home/victor/projects/osmand/temp/esya.gpx");
// start = lts[0];
// end = lts[lts.length - 1];
// System.out.println("Start " + start + " end " + end);
// precalculatedRouteDirection = PrecalculatedRouteDirection.build(lts, config.router.getMaxDefaultSpeed());
// precalculatedRouteDirection.setFollowNext(true);
// config.planRoadDirection = 1;
// Test initial direction
// config.initialDirection = 90d / 180d * Math.PI; // EAST
// config.initialDirection = 180d / 180d * Math.PI; // SOUTH
// config.initialDirection = -90d / 180d * Math.PI; // WEST
// config.initialDirection = 0 / 180d * Math.PI; // NORTH
// config.NUMBER_OF_DESIRABLE_TILES_IN_MEMORY = 300;
// config.ZOOM_TO_LOAD_TILES = 14;
final RoutingContext ctx = router.buildRoutingContext(config, DataExtractionSettings.getSettings().useNativeRouting() ? NativeSwingRendering.getDefaultFromSettings() : null, rs, rm);
ctx.leftSideNavigation = false;
ctx.previouslyCalculatedRoute = previousRoute;
log.info("Use " + config.routerName + "mode for routing");
final DataTileManager<Entity> points = new DataTileManager<Entity>(11);
map.setPoints(points);
ctx.setVisitor(createSegmentVisitor(animateRoutingCalculation, points));
// Choose native or not native
long nt = System.nanoTime();
startProgressThread(ctx);
try {
List<RouteSegmentResult> searchRoute = router.searchRoute(ctx, start, end, intermediates, precalculatedRouteDirection);
throwExceptionIfRouteNotFound(ctx, searchRoute);
System.out.println("External native time " + (System.nanoTime() - nt) / 1.0e9f);
if (animateRoutingCalculation) {
playPauseButton.setVisible(false);
nextTurn.setText("FINISH");
waitNextPress();
nextTurn.setText(">>");
}
this.previousRoute = searchRoute;
calculateResult(res, searchRoute);
} finally {
ctx.calculationProgress.isCancelled = true;
}
} catch (Exception e) {
ExceptionHandler.handle(e);
} finally {
playPauseButton.setVisible(false);
nextTurn.setVisible(false);
stopButton.setVisible(false);
if (map.getPoints() != null) {
map.getPoints().clear();
}
}
System.out.println("Finding self routes " + res.size() + " " + (System.currentTimeMillis() - time) + " ms");
}
return res;
}
use of net.osmand.data.DataTileManager in project OsmAnd-tools by osmandapp.
the class MapRouterLayer method createSegmentVisitor.
private RouteSegmentVisitor createSegmentVisitor(final boolean animateRoutingCalculation, final DataTileManager<Entity> points) {
return new RouteSegmentVisitor() {
private List<RouteSegment> cache = new ArrayList<RouteSegment>();
private List<RouteSegment> pollCache = new ArrayList<RouteSegment>();
private List<Integer> cacheInt = new ArrayList<Integer>();
@Override
public void visitSegment(RouteSegment s, int endSegment, boolean poll) {
if (stop) {
throw new RuntimeException("Interrupted");
}
if (!animateRoutingCalculation) {
return;
}
if (!poll && pause) {
pollCache.add(s);
return;
}
cache.add(s);
cacheInt.add(endSegment);
if (cache.size() < steps) {
return;
}
if (pause) {
registerObjects(points, poll, pollCache, null);
pollCache.clear();
}
registerObjects(points, !poll, cache, cacheInt);
cache.clear();
cacheInt.clear();
redraw();
if (pause) {
waitNextPress();
}
}
private void registerObjects(final DataTileManager<Entity> points, boolean white, List<RouteSegment> registerCache, List<Integer> cacheInt) {
for (int l = 0; l < registerCache.size(); l++) {
RouteSegment segment = registerCache.get(l);
Way way = new Way(-1);
way.putTag(OSMTagKey.NAME.getValue(), segment.getTestName());
if (white) {
way.putTag("color", "white");
}
int from = cacheInt != null ? segment.getSegmentStart() : segment.getSegmentStart() - 2;
int to = cacheInt != null ? cacheInt.get(l) : segment.getSegmentStart() + 2;
if (from > to) {
int x = from;
from = to;
to = x;
}
for (int i = from; i <= to; i++) {
if (i >= 0 && i < segment.getRoad().getPointsLength()) {
net.osmand.osm.edit.Node n = createNode(segment, i);
way.addNode(n);
}
}
LatLon n = way.getLatLon();
points.registerObject(n.getLatitude(), n.getLongitude(), way);
}
}
};
}
use of net.osmand.data.DataTileManager in project OsmAnd-tools by osmandapp.
the class MapRouterLayer method fillPopupMenuWithActions.
public void fillPopupMenuWithActions(JPopupMenu menu) {
Action start = new AbstractAction("Mark start point") {
private static final long serialVersionUID = 507156107455281238L;
@Override
public void actionPerformed(ActionEvent e) {
Point popupMenuPoint = map.getPopupMenuPoint();
double fy = (popupMenuPoint.y - map.getCenterPointY()) / map.getTileSize();
double fx = (popupMenuPoint.x - map.getCenterPointX()) / map.getTileSize();
double latitude = MapUtils.getLatitudeFromTile(map.getZoom(), map.getYTile() + fy);
double longitude = MapUtils.getLongitudeFromTile(map.getZoom(), map.getXTile() + fx);
setStart(new LatLon(latitude, longitude));
}
};
menu.add(start);
Action end = new AbstractAction("Mark end point") {
private static final long serialVersionUID = 4446789424902471319L;
@Override
public void actionPerformed(ActionEvent e) {
Point popupMenuPoint = map.getPopupMenuPoint();
double fy = (popupMenuPoint.y - map.getCenterPointY()) / map.getTileSize();
double fx = (popupMenuPoint.x - map.getCenterPointX()) / map.getTileSize();
double latitude = MapUtils.getLatitudeFromTile(map.getZoom(), map.getYTile() + fy);
double longitude = MapUtils.getLongitudeFromTile(map.getZoom(), map.getXTile() + fx);
setEnd(new LatLon(latitude, longitude));
}
};
menu.add(end);
Action selfRoute = new AbstractAction("Calculate OsmAnd route") {
private static final long serialVersionUID = 507156107455281238L;
@Override
public void actionPerformed(ActionEvent e) {
previousRoute = null;
calcRoute(RouteCalculationMode.NORMAL);
}
};
menu.add(selfRoute);
Action selfBaseRoute = new AbstractAction("Calculate OsmAnd base route") {
private static final long serialVersionUID = 8049785829806139142L;
@Override
public void actionPerformed(ActionEvent e) {
previousRoute = null;
calcRoute(RouteCalculationMode.BASE);
}
};
menu.add(selfBaseRoute);
Action complexRoute = new AbstractAction("Calculate OsmAnd complex route") {
private static final long serialVersionUID = 8049785829806139142L;
@Override
public void actionPerformed(ActionEvent e) {
previousRoute = null;
calcRoute(RouteCalculationMode.COMPLEX);
}
};
menu.add(complexRoute);
Action recalculate = new AbstractAction("Recalculate OsmAnd route") {
private static final long serialVersionUID = 507156107455281238L;
@Override
public boolean isEnabled() {
// return previousRoute != null;
return true;
}
@Override
public void actionPerformed(ActionEvent e) {
calcRoute(RouteCalculationMode.NORMAL);
}
};
menu.add(recalculate);
Action route_YOURS = new AbstractAction("Calculate YOURS route") {
private static final long serialVersionUID = 507156107455281238L;
@Override
public void actionPerformed(ActionEvent e) {
new Thread() {
@Override
public void run() {
List<Way> ways = route_YOURS(startRoute, endRoute);
DataTileManager<Way> points = new DataTileManager<Way>(11);
for (Way w : ways) {
LatLon n = w.getLatLon();
points.registerObject(n.getLatitude(), n.getLongitude(), w);
}
map.setPoints(points);
}
}.start();
}
};
menu.add(route_YOURS);
Action loadGPXFile = new AbstractAction("Load GPX file...") {
private static final long serialVersionUID = 507156107455281238L;
@Override
public void actionPerformed(ActionEvent e) {
new Thread() {
@Override
public void run() {
JFileChooser fileChooser = new JFileChooser();
if (fileChooser.showOpenDialog(map) == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
DataTileManager<Way> points = new DataTileManager<Way>(11);
List<Way> ways = parseGPX(file);
for (Way w : ways) {
LatLon n = w.getLatLon();
points.registerObject(n.getLatitude(), n.getLongitude(), w);
}
// load from file
map.setPoints(points);
}
}
}.start();
}
};
menu.add(loadGPXFile);
Action swapLocations = new AbstractAction("Swap locations") {
private static final long serialVersionUID = 507156107455281238L;
@Override
public void actionPerformed(ActionEvent e) {
LatLon l = endRoute;
endRoute = startRoute;
startRoute = l;
map.repaint();
}
};
menu.add(swapLocations);
Action addIntermediate = new AbstractAction("Add transit point") {
@Override
public void actionPerformed(ActionEvent e) {
Point popupMenuPoint = map.getPopupMenuPoint();
double fy = (popupMenuPoint.y - map.getCenterPointY()) / map.getTileSize();
double fx = (popupMenuPoint.x - map.getCenterPointX()) / map.getTileSize();
double latitude = MapUtils.getLatitudeFromTile(map.getZoom(), map.getYTile() + fy);
double longitude = MapUtils.getLongitudeFromTile(map.getZoom(), map.getXTile() + fx);
intermediates.add(new LatLon(latitude, longitude));
map.repaint();
}
};
menu.add(addIntermediate);
Action remove = new AbstractAction("Remove transit point") {
@Override
public void actionPerformed(ActionEvent e) {
if (intermediates.size() > 0) {
intermediates.remove(0);
}
map.repaint();
}
};
menu.add(remove);
}
use of net.osmand.data.DataTileManager in project OsmAnd-tools by osmandapp.
the class MapAddressLayer method showCurrentCityActions.
private void showCurrentCityActions() {
Point popupMenuPoint = map.getPopupMenuPoint();
double fy = (popupMenuPoint.y - map.getCenterPointY()) / map.getTileSize();
double fx = (popupMenuPoint.x - map.getCenterPointX()) / map.getTileSize();
final double latitude = MapUtils.getLatitudeFromTile(map.getZoom(), map.getYTile() + fy);
final double longitude = MapUtils.getLongitudeFromTile(map.getZoom(), map.getXTile() + fx);
new Thread(new Runnable() {
@Override
public void run() {
try {
DataTileManager<Entity> points = new DataTileManager<Entity>(15);
List<Entity> os = searchAddress(latitude, longitude, points);
for (Entity w : os) {
LatLon n = w.getLatLon();
points.registerObject(n.getLatitude(), n.getLongitude(), w);
}
map.setPoints(points);
map.repaint();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
}
use of net.osmand.data.DataTileManager in project OsmAnd-tools by osmandapp.
the class MapAddressLayer method whereAmI.
private void whereAmI() {
Point popupMenuPoint = map.getPopupMenuPoint();
double fy = (popupMenuPoint.y - map.getCenterPointY()) / map.getTileSize();
double fx = (popupMenuPoint.x - map.getCenterPointX()) / map.getTileSize();
final double latitude = MapUtils.getLatitudeFromTile(map.getZoom(), map.getYTile() + fy);
final double longitude = MapUtils.getLongitudeFromTile(map.getZoom(), map.getXTile() + fx);
new Thread(new Runnable() {
@Override
public void run() {
try {
DataTileManager<Entity> points = new DataTileManager<Entity>(15);
List<Entity> os = whereAmI(latitude, longitude, points);
for (Entity w : os) {
LatLon n = w.getLatLon();
points.registerObject(n.getLatitude(), n.getLongitude(), w);
}
map.setPoints(points);
map.repaint();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
}
Aggregations