From e9798b248d356b5c9456c0ad8dcad897da66003f Mon Sep 17 00:00:00 2001 From: GaidamakUA Date: Mon, 11 Jan 2016 14:54:26 +0200 Subject: [PATCH] Impassable roads layer. Icons showing. --- .../src/net/osmand/router/GeneralRouter.java | 16 ++-- .../osmand/router/RoutingConfiguration.java | 40 +++++----- .../plus/activities/MapActivityLayers.java | 13 +++- .../plus/helpers/AvoidSpecificRoads.java | 4 +- .../plus/views/ImpassableRoadsLayer.java | 74 +++++++++++++++++++ 5 files changed, 115 insertions(+), 32 deletions(-) create mode 100644 OsmAnd/src/net/osmand/plus/views/ImpassableRoadsLayer.java diff --git a/OsmAnd-java/src/net/osmand/router/GeneralRouter.java b/OsmAnd-java/src/net/osmand/router/GeneralRouter.java index c5354b9b5a..5719696e91 100644 --- a/OsmAnd-java/src/net/osmand/router/GeneralRouter.java +++ b/OsmAnd-java/src/net/osmand/router/GeneralRouter.java @@ -1,6 +1,11 @@ package net.osmand.router; -import gnu.trove.set.hash.TLongHashSet; +import net.osmand.binary.BinaryMapRouteReaderAdapter.RouteRegion; +import net.osmand.binary.BinaryMapRouteReaderAdapter.RouteTypeRule; +import net.osmand.binary.RouteDataObject; +import net.osmand.router.BinaryRoutePlanner.RouteSegment; +import net.osmand.util.Algorithms; +import net.osmand.util.MapUtils; import java.io.PrintStream; import java.util.ArrayList; @@ -14,12 +19,7 @@ import java.util.Map; import java.util.Map.Entry; import java.util.Set; -import net.osmand.binary.BinaryMapRouteReaderAdapter.RouteRegion; -import net.osmand.binary.BinaryMapRouteReaderAdapter.RouteTypeRule; -import net.osmand.binary.RouteDataObject; -import net.osmand.router.BinaryRoutePlanner.RouteSegment; -import net.osmand.util.Algorithms; -import net.osmand.util.MapUtils; +import gnu.trove.set.hash.TLongHashSet; public class GeneralRouter implements VehicleRouter { @@ -875,7 +875,7 @@ public class GeneralRouter implements VehicleRouter { } - public void addImpassableRoads(TLongHashSet impassableRoads) { + public void addImpassableRoads(Set impassableRoads) { if (impassableRoads != null && !impassableRoads.isEmpty()) { if (this.impassableRoads == null) { this.impassableRoads = new TLongHashSet(); diff --git a/OsmAnd-java/src/net/osmand/router/RoutingConfiguration.java b/OsmAnd-java/src/net/osmand/router/RoutingConfiguration.java index 758ec8a302..94ec5bb155 100644 --- a/OsmAnd-java/src/net/osmand/router/RoutingConfiguration.java +++ b/OsmAnd-java/src/net/osmand/router/RoutingConfiguration.java @@ -1,15 +1,6 @@ package net.osmand.router; -import gnu.trove.set.hash.TLongHashSet; - -import java.io.IOException; -import java.io.InputStream; -import java.util.ArrayList; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; -import java.util.Stack; - +import net.osmand.Location; import net.osmand.PlatformUtil; import net.osmand.binary.RouteDataObject; import net.osmand.router.GeneralRouter.GeneralRouterProfile; @@ -20,6 +11,15 @@ import net.osmand.util.Algorithms; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserException; +import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Stack; + public class RoutingConfiguration { public static final int DEFAULT_MEMORY_LIMIT = 30; @@ -55,12 +55,12 @@ public class RoutingConfiguration { private String defaultRouter = ""; private Map routers = new LinkedHashMap(); private Map attributes = new LinkedHashMap(); - private TLongHashSet impassableRoadIds = new TLongHashSet(); + private HashMap impassableRoadLocations = new HashMap<>(); private List impassableRoads = new ArrayList(); // Example // { -// impassableRoadIds.add(23000069L); +// impassableRoadLocations.add(23000069L); // } public RoutingConfiguration build(String router, int memoryLimitMB) { @@ -87,7 +87,7 @@ public class RoutingConfiguration { i.initialDirection = direction; i.recalculateDistance = parseSilentFloat(getAttribute(i.router, "recalculateDistanceHelp"), i.recalculateDistance) ; i.heuristicCoefficient = parseSilentFloat(getAttribute(i.router, "heuristicCoefficient"), i.heuristicCoefficient); - i.router.addImpassableRoads(impassableRoadIds); + i.router.addImpassableRoads(impassableRoadLocations.keySet()); i.ZOOM_TO_LOAD_TILES = parseSilentInt(getAttribute(i.router, "zoomToLoadTiles"), i.ZOOM_TO_LOAD_TILES); int desirable = parseSilentInt(getAttribute(i.router, "memoryLimitInMB"), 0); if(desirable != 0) { @@ -109,14 +109,14 @@ public class RoutingConfiguration { return impassableRoads; } - public TLongHashSet getImpassableRoadIds() { - return impassableRoadIds; + public Map getImpassableRoadLocations() { + return impassableRoadLocations; } - public void addImpassableRoad(RouteDataObject r) { - if (!impassableRoadIds.contains(r.id)){ - impassableRoadIds.add(r.id); - impassableRoads.add(r); + public void addImpassableRoad(RouteDataObject route, Location location) { + if (!impassableRoadLocations.containsKey(route.id)){ + impassableRoadLocations.put(route.id, location); + impassableRoads.add(route); } } @@ -139,7 +139,7 @@ public class RoutingConfiguration { } public void removeImpassableRoad(RouteDataObject obj) { - impassableRoadIds.remove(obj.id); + impassableRoadLocations.remove(obj.id); impassableRoads.remove(obj); } diff --git a/OsmAnd/src/net/osmand/plus/activities/MapActivityLayers.java b/OsmAnd/src/net/osmand/plus/activities/MapActivityLayers.java index 461b4d2dcf..5c2c36fbab 100644 --- a/OsmAnd/src/net/osmand/plus/activities/MapActivityLayers.java +++ b/OsmAnd/src/net/osmand/plus/activities/MapActivityLayers.java @@ -35,6 +35,7 @@ import net.osmand.plus.views.ContextMenuLayer; import net.osmand.plus.views.DownloadedRegionsLayer; import net.osmand.plus.views.FavoritesLayer; import net.osmand.plus.views.GPXLayer; +import net.osmand.plus.views.ImpassableRoadsLayer; import net.osmand.plus.views.MapControlsLayer; import net.osmand.plus.views.MapInfoLayer; import net.osmand.plus.views.MapTextLayer; @@ -71,6 +72,7 @@ public class MapActivityLayers { private TransportInfoLayer transportInfoLayer; private PointLocationLayer locationLayer; private PointNavigationLayer navigationLayer; + private ImpassableRoadsLayer impassableRoadsLayer; private MapInfoLayer mapInfoLayer; private MapTextLayer mapTextLayer; private ContextMenuLayer contextMenuLayer; @@ -79,7 +81,7 @@ public class MapActivityLayers { private MapWidgetRegistry mapWidgetRegistry; private StateChangedListener transparencyListener; - + public MapActivityLayers(MapActivity activity) { this.activity = activity; this.mapWidgetRegistry = new MapWidgetRegistry(activity.getMyApplication().getSettings()); @@ -141,6 +143,9 @@ public class MapActivityLayers { // 7. point navigation layer navigationLayer = new PointNavigationLayer(activity); mapView.addLayer(navigationLayer, 7); + // 7.5 Impassible roads + impassableRoadsLayer = new ImpassableRoadsLayer(activity); + mapView.addLayer(impassableRoadsLayer, 7.5f); // 8. context menu layer contextMenuLayer = new ContextMenuLayer(activity); mapView.addLayer(contextMenuLayer, 8); @@ -421,7 +426,11 @@ public class MapActivityLayers { public PointNavigationLayer getNavigationLayer() { return navigationLayer; } - + + public ImpassableRoadsLayer getImpassableRoadsLayer() { + return impassableRoadsLayer; + } + public GPXLayer getGpxLayer() { return gpxLayer; } diff --git a/OsmAnd/src/net/osmand/plus/helpers/AvoidSpecificRoads.java b/OsmAnd/src/net/osmand/plus/helpers/AvoidSpecificRoads.java index 98592988bf..23cf7fd2ed 100644 --- a/OsmAnd/src/net/osmand/plus/helpers/AvoidSpecificRoads.java +++ b/OsmAnd/src/net/osmand/plus/helpers/AvoidSpecificRoads.java @@ -144,7 +144,7 @@ public class AvoidSpecificRoads { }); } private void findRoad(final MapActivity activity, final LatLon loc) { - Location ll = new Location(""); + final Location ll = new Location(""); ll.setLatitude(loc.getLatitude()); ll.setLongitude(loc.getLongitude()); app.getLocationProvider().getRouteSegment(ll, new ResultMatcher() { @@ -154,7 +154,7 @@ public class AvoidSpecificRoads { if(object == null) { Toast.makeText(activity, R.string.error_avoid_specific_road, Toast.LENGTH_LONG).show(); } else { - getBuilder().addImpassableRoad(object); + getBuilder().addImpassableRoad(object, ll); RoutingHelper rh = app.getRoutingHelper(); if(rh.isRouteCalculated() || rh.isRouteBeingCalculated()) { rh.recalculateRouteDueToSettingsChange(); diff --git a/OsmAnd/src/net/osmand/plus/views/ImpassableRoadsLayer.java b/OsmAnd/src/net/osmand/plus/views/ImpassableRoadsLayer.java new file mode 100644 index 0000000000..aae75e9a8a --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/views/ImpassableRoadsLayer.java @@ -0,0 +1,74 @@ +package net.osmand.plus.views; + +import android.graphics.Bitmap; +import android.graphics.BitmapFactory; +import android.graphics.Canvas; +import android.graphics.Paint; + +import net.osmand.Location; +import net.osmand.data.RotatedTileBox; +import net.osmand.plus.R; +import net.osmand.plus.activities.MapActivity; + +import java.util.Map; + +/** + * Created by Denis on + * 20.03.2015. + */ +public class ImpassableRoadsLayer extends OsmandMapLayer { + + private static final int startZoom = 10; + private final MapActivity activity; + private Bitmap roadWorkIcon; + private OsmandMapTileView view; + private Paint paint; + private Map missingRoads; + + public ImpassableRoadsLayer(MapActivity activity) { + this.activity = activity; + } + + @Override + public void initLayer(OsmandMapTileView view) { + this.view = view; + roadWorkIcon = BitmapFactory.decodeResource(view.getResources(), R.drawable.ic_action_road_works_dark); + paint = new Paint(); + } + + @Override + public void onDraw(Canvas canvas, RotatedTileBox tileBox, DrawSettings settings) { + + } + + @Override + public void onPrepareBufferImage(Canvas canvas, RotatedTileBox tileBox, DrawSettings settings) { + if (tileBox.getZoom() >= startZoom) { + for (long id : getMissingRoads().keySet()) { + Location location = getMissingRoads().get(id); + float x = tileBox.getPixXFromLatLon(location.getLatitude(), location.getLongitude()); + float y = tileBox.getPixYFromLatLon(location.getLatitude(), location.getLongitude()); + float left = x - roadWorkIcon.getWidth() / 2; + float top = y - roadWorkIcon.getHeight() / 2; + canvas.drawBitmap(roadWorkIcon, left, top, paint); + } + } + } + + public Map getMissingRoads() { + if(missingRoads == null) { + missingRoads = activity.getMyApplication().getDefaultRoutingConfig().getImpassableRoadLocations(); + } + return missingRoads; + } + + @Override + public void destroyLayer() { + + } + + @Override + public boolean drawInScreenPixels() { + return true; + } +}