From e63e9b686543977081be5c0f541076ba68f4e6c8 Mon Sep 17 00:00:00 2001 From: Victor Shcherb Date: Thu, 9 Jul 2020 19:34:49 +0200 Subject: [PATCH] Fix #9293 --- .../net/osmand/binary/GeocodingUtilities.java | 64 ++++++++++++++++--- .../osmand/router/RoutePlannerFrontEnd.java | 11 +++- .../net/osmand/router/RoutingContext.java | 7 ++ .../osmand/plus/CurrentPositionHelper.java | 6 +- .../sample1/CurrentPositionHelper.java | 6 +- 5 files changed, 79 insertions(+), 15 deletions(-) diff --git a/OsmAnd-java/src/main/java/net/osmand/binary/GeocodingUtilities.java b/OsmAnd-java/src/main/java/net/osmand/binary/GeocodingUtilities.java index 1699d749d9..77357bac9b 100644 --- a/OsmAnd-java/src/main/java/net/osmand/binary/GeocodingUtilities.java +++ b/OsmAnd-java/src/main/java/net/osmand/binary/GeocodingUtilities.java @@ -4,6 +4,7 @@ import net.osmand.PlatformUtil; import net.osmand.ResultMatcher; import net.osmand.CollatorStringMatcher.StringMatcherMode; import net.osmand.binary.BinaryMapIndexReader.SearchRequest; +import net.osmand.binary.BinaryMapRouteReaderAdapter.RouteRegion; import net.osmand.data.Building; import net.osmand.data.City; import net.osmand.data.LatLon; @@ -23,9 +24,11 @@ import java.text.Collator; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; +import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.List; +import java.util.Map; import java.util.Set; import gnu.trove.set.hash.TLongHashSet; @@ -40,7 +43,7 @@ public class GeocodingUtilities { public static final float STOP_SEARCHING_STREET_WITH_MULTIPLIER_RADIUS = 250; public static final float STOP_SEARCHING_STREET_WITHOUT_MULTIPLIER_RADIUS = 400; - public static final int DISTANCE_STREET_NAME_PROXIMITY_BY_NAME = 15000; + public static final int DISTANCE_STREET_NAME_PROXIMITY_BY_NAME = 45000; public static final float DISTANCE_STREET_FROM_CLOSEST_WITH_SAME_NAME = 1000; public static final float THRESHOLD_MULTIPLIER_SKIP_BUILDINGS_AFTER = 1.5f; @@ -142,15 +145,12 @@ public class GeocodingUtilities { RoutePlannerFrontEnd rp = new RoutePlannerFrontEnd(); List lst = new ArrayList(); List listR = new ArrayList(); - rp.findRouteSegment(lat, lon, ctx, listR); + // we allow duplications to search in both files for boundary regions + rp.findRouteSegment(lat, lon, ctx, listR, false, true); double distSquare = 0; - TLongHashSet set = new TLongHashSet(); - Set streetNames = new HashSet(); + Map> streetNames = new HashMap<>(); for (RouteSegmentPoint p : listR) { RouteDataObject road = p.getRoad(); - if (!set.add(road.getId())) { - continue; - } // System.out.println(road.toString() + " " + Math.sqrt(p.distSquare)); String name = Algorithms.isEmpty(road.getName()) ? road.getRef("", false, true) : road.getName(); if (allowEmptyNames || !Algorithms.isEmpty(name)) { @@ -164,7 +164,13 @@ public class GeocodingUtilities { sr.connectionPoint = new LatLon(MapUtils.get31LatitudeY(p.preciseY), MapUtils.get31LongitudeX(p.preciseX)); sr.regionFP = road.region.getFilePointer(); sr.regionLen = road.region.getLength(); - if (streetNames.add(sr.streetName)) { + List plst = streetNames.get(sr.streetName); + if (plst == null) { + plst = new ArrayList(); + streetNames.put(sr.streetName, plst); + } + if (!plst.contains(road.region)) { + plst.add(road.region); lst.add(sr); } } @@ -308,6 +314,48 @@ public class GeocodingUtilities { return res; } + public void filterDuplicateRegionResults(final List res) { + Collections.sort(res, DISTANCE_COMPARATOR); + // filter duplicate city results (when building is in both regions on boundary) + for (int i = 0; i < res.size() - 1;) { + int cmp = cmpResult(res.get(i), res.get(i + 1)); + if (cmp > 0) { + res.remove(i); + } else if (cmp < 0) { + res.remove(i + 1); + } else { + // nothing to delete + i++; + } + } + } + + private int cmpResult(GeocodingResult gr1, GeocodingResult gr2) { + boolean eqStreet = Algorithms.stringsEqual(gr1.streetName, gr2.streetName); + if (eqStreet) { + boolean sameObj = false; + if (gr1.building != null && gr2.building != null) { + if (Algorithms.stringsEqual(gr1.building.getName(), gr2.building.getName())) { + // same building + sameObj = true; + } + } else if (gr1.building == null && gr2.building == null) { + // same street + sameObj = true; + } + if (sameObj) { + double cityDist1 = MapUtils.getDistance(gr1.searchPoint, gr1.city.getLocation()); + double cityDist2 = MapUtils.getDistance(gr2.searchPoint, gr2.city.getLocation()); + if (cityDist1 < cityDist2) { + return -1; + } else { + return 1; + } + } + } + return 0; + } + private List loadStreetBuildings(final GeocodingResult road, BinaryMapIndexReader reader, GeocodingResult street) throws IOException { final List streetBuildings = new ArrayList(); diff --git a/OsmAnd-java/src/main/java/net/osmand/router/RoutePlannerFrontEnd.java b/OsmAnd-java/src/main/java/net/osmand/router/RoutePlannerFrontEnd.java index 108e00f2c9..7a56f82591 100644 --- a/OsmAnd-java/src/main/java/net/osmand/router/RoutePlannerFrontEnd.java +++ b/OsmAnd-java/src/main/java/net/osmand/router/RoutePlannerFrontEnd.java @@ -117,15 +117,20 @@ public class RoutePlannerFrontEnd { } public RouteSegmentPoint findRouteSegment(double lat, double lon, RoutingContext ctx, List list, boolean transportStop) throws IOException { + return findRouteSegment(lat, lon, ctx, list, false, false); + } + + public RouteSegmentPoint findRouteSegment(double lat, double lon, RoutingContext ctx, List list, boolean transportStop, + boolean allowDuplications) throws IOException { int px = MapUtils.get31TileNumberX(lon); int py = MapUtils.get31TileNumberY(lat); ArrayList dataObjects = new ArrayList(); - ctx.loadTileData(px, py, 17, dataObjects); + ctx.loadTileData(px, py, 17, dataObjects, allowDuplications); if (dataObjects.isEmpty()) { - ctx.loadTileData(px, py, 15, dataObjects); + ctx.loadTileData(px, py, 15, dataObjects, allowDuplications); } if (dataObjects.isEmpty()) { - ctx.loadTileData(px, py, 14, dataObjects); + ctx.loadTileData(px, py, 14, dataObjects, allowDuplications); } if (list == null) { list = new ArrayList(); diff --git a/OsmAnd-java/src/main/java/net/osmand/router/RoutingContext.java b/OsmAnd-java/src/main/java/net/osmand/router/RoutingContext.java index e3c233b4d4..75a4295b5a 100644 --- a/OsmAnd-java/src/main/java/net/osmand/router/RoutingContext.java +++ b/OsmAnd-java/src/main/java/net/osmand/router/RoutingContext.java @@ -412,6 +412,10 @@ public class RoutingContext { } public void loadTileData(int x31, int y31, int zoomAround, final List toFillIn) { + loadTileData(x31, y31, zoomAround, toFillIn, false); + } + + public void loadTileData(int x31, int y31, int zoomAround, final List toFillIn, boolean allowDuplications) { int t = config.ZOOM_TO_LOAD_TILES - zoomAround; int coordinatesShift = (1 << (31 - config.ZOOM_TO_LOAD_TILES)); if(t <= 0) { @@ -432,6 +436,9 @@ public class RoutingContext { TLongObjectHashMap excludeDuplications = new TLongObjectHashMap(); while (it.hasNext()) { getAllObjects(it.next(), toFillIn, excludeDuplications); + if (allowDuplications) { + excludeDuplications.clear(); + } } timeToFindInitialSegments += (System.nanoTime() - now); } diff --git a/OsmAnd/src/net/osmand/plus/CurrentPositionHelper.java b/OsmAnd/src/net/osmand/plus/CurrentPositionHelper.java index 5fce920b91..a50a345aa3 100644 --- a/OsmAnd/src/net/osmand/plus/CurrentPositionHelper.java +++ b/OsmAnd/src/net/osmand/plus/CurrentPositionHelper.java @@ -237,6 +237,7 @@ public class CurrentPositionHelper { List complete = new ArrayList<>(); double minBuildingDistance = 0; if (res != null) { + GeocodingUtilities gu = new GeocodingUtilities(); for (GeocodingResult r : res) { BinaryMapIndexReader foundRepo = null; List rts = usedReaders; @@ -259,7 +260,7 @@ public class CurrentPositionHelper { } else if (foundRepo != null) { List justified = null; try { - justified = new GeocodingUtilities().justifyReverseGeocodingSearch(r, foundRepo, + justified = gu.justifyReverseGeocodingSearch(r, foundRepo, minBuildingDistance, result); } catch (IOException e) { log.error("Exception happened during reverse geocoding", e); @@ -277,6 +278,7 @@ public class CurrentPositionHelper { complete.add(r); } } + gu.filterDuplicateRegionResults(complete); } if (result.isCancelled()) { @@ -287,7 +289,7 @@ public class CurrentPositionHelper { }); return; } - Collections.sort(complete, GeocodingUtilities.DISTANCE_COMPARATOR); +// Collections.sort(complete, GeocodingUtilities.DISTANCE_COMPARATOR); // for(GeocodingResult rt : complete) { // System.out.println(rt.toString()); // } diff --git a/OsmAndCore-sample/src/net/osmand/core/samples/android/sample1/CurrentPositionHelper.java b/OsmAndCore-sample/src/net/osmand/core/samples/android/sample1/CurrentPositionHelper.java index 351f1891d6..e490925754 100644 --- a/OsmAndCore-sample/src/net/osmand/core/samples/android/sample1/CurrentPositionHelper.java +++ b/OsmAndCore-sample/src/net/osmand/core/samples/android/sample1/CurrentPositionHelper.java @@ -191,6 +191,7 @@ public class CurrentPositionHelper { private void justifyResult(List res, final ResultMatcher result) { List complete = new ArrayList<>(); double minBuildingDistance = 0; + GeocodingUtilities gu = new GeocodingUtilities(); if (res != null) { for (GeocodingResult r : res) { BinaryMapIndexReader foundRepo = null; @@ -208,7 +209,7 @@ public class CurrentPositionHelper { } else if (foundRepo != null) { List justified = null; try { - justified = new GeocodingUtilities().justifyReverseGeocodingSearch(r, foundRepo, + justified = gu.justifyReverseGeocodingSearch(r, foundRepo, minBuildingDistance, result); } catch (IOException e) { log.error("Exception happened during reverse geocoding", e); @@ -227,6 +228,7 @@ public class CurrentPositionHelper { complete.add(r); } } + gu.filterDuplicateRegionResults(complete); } if (result.isCancelled()) { @@ -237,7 +239,7 @@ public class CurrentPositionHelper { }); return; } - Collections.sort(complete, GeocodingUtilities.DISTANCE_COMPARATOR); + // Collections.sort(complete, GeocodingUtilities.DISTANCE_COMPARATOR); // for(GeocodingResult rt : complete) { // System.out.println(rt.toString()); // }