From f2c6e08fb337c8e8f505d7fd3c7e02fe6586bdf8 Mon Sep 17 00:00:00 2001 From: Victor Shcherb Date: Sun, 4 Aug 2019 01:37:10 +0200 Subject: [PATCH] Improve cahcing for osmand regions --- .../java/net/osmand/map/OsmandRegions.java | 68 +++++++------------ .../plus/views/DownloadedRegionsLayer.java | 2 +- 2 files changed, 24 insertions(+), 46 deletions(-) diff --git a/OsmAnd-java/src/main/java/net/osmand/map/OsmandRegions.java b/OsmAnd-java/src/main/java/net/osmand/map/OsmandRegions.java index c49a6fe17c..76ead632c9 100644 --- a/OsmAnd-java/src/main/java/net/osmand/map/OsmandRegions.java +++ b/OsmAnd-java/src/main/java/net/osmand/map/OsmandRegions.java @@ -268,18 +268,19 @@ public class OsmandRegions { return Math.abs(area); } - private List getCountries(int tile31x, int tile31y) { - HashSet set = new HashSet(quadTree.queryInBox(new QuadRect(tile31x, tile31y, tile31x, tile31y), + private List getCountries(int lx, int rx, int ty, int by) throws IOException { + HashSet set = new HashSet(quadTree.queryInBox(new QuadRect(lx, ty, rx, by), new ArrayList())); List result = new ArrayList(); Iterator it = set.iterator(); - + int mx = lx / 2 + rx / 2; + int my = ty / 2 + by / 2; while (it.hasNext()) { String cname = it.next(); BinaryMapDataObject container = null; int count = 0; for (BinaryMapDataObject bo : countriesByDownloadName.get(cname)) { - if (contain(bo, tile31x, tile31y)) { + if (contain(bo, mx, my)) { count++; container = bo; break; @@ -314,51 +315,26 @@ public class OsmandRegions { } + public List query(int lx, int rx, int ty, int by) throws IOException { + if (quadTree != null) { + return getCountries(lx, rx, ty, by); + } + return queryBboxNoInit(lx, rx, ty, by); + } + + public List query(final int tile31x, final int tile31y) throws IOException { if (quadTree != null) { - return getCountries(tile31x, tile31y); + return getCountries(tile31x, tile31x, tile31y, tile31y); } - return queryNoInit(tile31x, tile31y); + return queryBboxNoInit(tile31x, tile31x, tile31y, tile31y); } - private synchronized List queryNoInit(final int tile31x, final int tile31y) throws IOException { - final List result = new ArrayList(); - BinaryMapIndexReader.SearchRequest sr = BinaryMapIndexReader.buildSearchRequest(tile31x, tile31x, tile31y, tile31y, - 5, new BinaryMapIndexReader.SearchFilter() { - @Override - public boolean accept(TIntArrayList types, BinaryMapIndexReader.MapIndex index) { - return true; - } - }, new ResultMatcher() { - - - @Override - public boolean publish(BinaryMapDataObject object) { - if (object.getPointsLength() < 1) { - return false; - } - initTypes(object); - if (contain(object, tile31x, tile31y)) { - result.add(object); - } - return false; - } - - @Override - public boolean isCancelled() { - return false; - } - } - ); - if (reader != null) { - reader.searchMapIndex(sr); - } - return result; - } - - - public synchronized List queryBbox(int lx, int rx, int ty, int by) throws IOException { + + private synchronized List queryBboxNoInit(int lx, int rx, int ty, int by) throws IOException { final List result = new ArrayList(); + int mx = lx / 2 + rx / 2; + int my = ty / 2 + by / 2; BinaryMapIndexReader.SearchRequest sr = BinaryMapIndexReader.buildSearchRequest(lx, rx, ty, by, 5, new BinaryMapIndexReader.SearchFilter() { @Override @@ -373,7 +349,9 @@ public class OsmandRegions { return false; } initTypes(object); - result.add(object); + if (contain(object, mx, my)) { + result.add(object); + } return false; } @@ -747,7 +725,7 @@ public class OsmandRegions { List mapDataObjects; try { - mapDataObjects = queryBbox(point31x, point31x, point31y, point31y); + mapDataObjects = queryBboxNoInit(point31x, point31x, point31y, point31y); } catch (IOException e) { throw new IOException("Error while calling queryBbox"); } diff --git a/OsmAnd/src/net/osmand/plus/views/DownloadedRegionsLayer.java b/OsmAnd/src/net/osmand/plus/views/DownloadedRegionsLayer.java index 1451524c41..f42e996b36 100644 --- a/OsmAnd/src/net/osmand/plus/views/DownloadedRegionsLayer.java +++ b/OsmAnd/src/net/osmand/plus/views/DownloadedRegionsLayer.java @@ -281,7 +281,7 @@ public class DownloadedRegionsLayer extends OsmandMapLayer implements IContextMe int bottom = MapUtils.get31TileNumberY(rb.getLatitude()); try { - result = osmandRegions.queryBbox(left, right, top, bottom); + result = osmandRegions.query(left, right, top, bottom); } catch (IOException e) { return result; }