From ed1a8104b7146fe7ef2c5c93939eeb2f3bc21368 Mon Sep 17 00:00:00 2001 From: Alexey Kulish Date: Fri, 4 Nov 2016 21:44:49 +0300 Subject: [PATCH] Fix #3186 --- .../src/net/osmand/map/OsmandRegions.java | 15 +++++++ .../plus/mapcontextmenu/MenuController.java | 45 +++++++++++++------ .../plus/views/DownloadedRegionsLayer.java | 12 ++++- 3 files changed, 58 insertions(+), 14 deletions(-) diff --git a/OsmAnd-java/src/net/osmand/map/OsmandRegions.java b/OsmAnd-java/src/net/osmand/map/OsmandRegions.java index 1c888d09c7..3ab8ea2e38 100644 --- a/OsmAnd-java/src/net/osmand/map/OsmandRegions.java +++ b/OsmAnd-java/src/net/osmand/map/OsmandRegions.java @@ -30,6 +30,7 @@ import net.osmand.binary.BinaryMapIndexReader.TagValuePair; import net.osmand.data.LatLon; import net.osmand.data.QuadRect; import net.osmand.data.QuadTree; +import net.osmand.osm.edit.Node; import net.osmand.util.Algorithms; import net.osmand.util.MapAlgorithms; import net.osmand.util.MapUtils; @@ -238,6 +239,20 @@ public class OsmandRegions { return false; } + public static double getArea(BinaryMapDataObject bo) { + double area = 0.0; + if (bo.getPointsLength() > 0) { + for (int i = 1; i < bo.getPointsLength(); i++) { + double ax = bo.getPoint31XTile(i - 1); + double bx = bo.getPoint31XTile(i); + double ay = bo.getPoint31YTile(i - 1); + double by = bo.getPoint31YTile(i); + area += (bx + ax) * (by - ay) / 1.631E10; + } + } + return Math.abs(area); + } + private List getCountries(int tile31x, int tile31y) { HashSet set = new HashSet(quadTree.queryInBox(new QuadRect(tile31x, tile31y, tile31x, tile31y), new ArrayList())); diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/MenuController.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/MenuController.java index 6e0fe6faa6..2d81e3fd06 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/MenuController.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/MenuController.java @@ -1,14 +1,16 @@ package net.osmand.plus.mapcontextmenu; -import java.io.IOException; -import java.util.Iterator; -import java.util.LinkedList; -import java.util.List; +import android.graphics.drawable.Drawable; +import android.support.annotation.NonNull; +import android.view.View; +import android.view.View.OnClickListener; +import android.widget.LinearLayout; import net.osmand.IndexConstants; -import net.osmand.NativeLibrary; import net.osmand.NativeLibrary.RenderedObject; import net.osmand.binary.BinaryMapDataObject; +import net.osmand.binary.BinaryMapIndexReader; +import net.osmand.binary.BinaryMapIndexReader.TagValuePair; import net.osmand.binary.RouteDataObject; import net.osmand.data.Amenity; import net.osmand.data.FavouritePoint; @@ -60,11 +62,11 @@ import net.osmand.plus.views.mapwidgets.MapInfoWidgetsFactory.TopToolbarControll import net.osmand.plus.views.mapwidgets.MapInfoWidgetsFactory.TopToolbarControllerType; import net.osmand.util.Algorithms; import net.osmand.util.MapUtils; -import android.graphics.drawable.Drawable; -import android.support.annotation.NonNull; -import android.view.View; -import android.view.View.OnClickListener; -import android.widget.LinearLayout; + +import java.io.IOException; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; public abstract class MenuController extends BaseMenuController { @@ -520,11 +522,22 @@ public abstract class MenuController extends BaseMenuController { Iterator it = mapDataObjects.iterator(); while (it.hasNext()) { BinaryMapDataObject o = it.next(); - if (!osmandRegions.contain(o, point31x, point31y)) { - it.remove(); + if (o.getTypes() != null) { + boolean isRegion = true; + for (int i = 0; i < o.getTypes().length; i++) { + TagValuePair tp = o.getMapIndex().decodeType(o.getTypes()[i]); + if ("boundary".equals(tp.value)) { + isRegion = false; + break; + } + } + if (!isRegion || !osmandRegions.contain(o, point31x, point31y)) { + it.remove(); + } } } String selectedFullName = ""; + double smallestArea = -1; for (BinaryMapDataObject o : mapDataObjects) { boolean downloaded = checkIfObjectDownloaded(rm, osmandRegions.getDownloadName(o)); if (downloaded) { @@ -532,7 +545,13 @@ public abstract class MenuController extends BaseMenuController { break; } else { String fullName = osmandRegions.getFullName(o); - if (fullName.length() > selectedFullName.length()) { + double area = OsmandRegions.getArea(o); + if (smallestArea == -1) { + smallestArea = area; + selectedFullName = fullName; + downloadMapDataObject = o; + } else if (area < smallestArea) { + smallestArea = area; selectedFullName = fullName; downloadMapDataObject = o; } diff --git a/OsmAnd/src/net/osmand/plus/views/DownloadedRegionsLayer.java b/OsmAnd/src/net/osmand/plus/views/DownloadedRegionsLayer.java index 0432047b57..402ddd540e 100644 --- a/OsmAnd/src/net/osmand/plus/views/DownloadedRegionsLayer.java +++ b/OsmAnd/src/net/osmand/plus/views/DownloadedRegionsLayer.java @@ -13,6 +13,8 @@ import android.util.DisplayMetrics; import android.view.WindowManager; import net.osmand.IndexConstants; import net.osmand.binary.BinaryMapDataObject; +import net.osmand.binary.BinaryMapIndexReader; +import net.osmand.binary.BinaryMapIndexReader.TagValuePair; import net.osmand.data.LatLon; import net.osmand.data.PointDescription; import net.osmand.data.RotatedTileBox; @@ -468,7 +470,15 @@ public class DownloadedRegionsLayer extends OsmandMapLayer implements IContextMe Iterator it = result.iterator(); while (it.hasNext()) { BinaryMapDataObject o = it.next(); - if (!osmandRegions.contain(o, point31x, point31y) ) { + boolean isRegion = true; + for (int i = 0; i < o.getTypes().length; i++) { + TagValuePair tp = o.getMapIndex().decodeType(o.getTypes()[i]); + if ("boundary".equals(tp.value)) { + isRegion = false; + break; + } + } + if (!isRegion || !osmandRegions.contain(o, point31x, point31y) ) { it.remove(); } }