From 9db069dd05ed98a927a70e169b309e37be44d2e6 Mon Sep 17 00:00:00 2001 From: MadWasp79 Date: Tue, 30 Jun 2020 12:03:51 +0300 Subject: [PATCH 1/2] fix for incomplete roads reading --- .../osmand/binary/BinaryMapIndexReader.java | 10 ++++--- .../BinaryMapTransportReaderAdapter.java | 17 +++++++---- .../osmand/router/TransportRoutePlanner.java | 1 - .../router/TransportStopsRouteReader.java | 29 +++++++++++-------- 4 files changed, 34 insertions(+), 23 deletions(-) diff --git a/OsmAnd-java/src/main/java/net/osmand/binary/BinaryMapIndexReader.java b/OsmAnd-java/src/main/java/net/osmand/binary/BinaryMapIndexReader.java index 86fce4f208..3e3b92a41e 100644 --- a/OsmAnd-java/src/main/java/net/osmand/binary/BinaryMapIndexReader.java +++ b/OsmAnd-java/src/main/java/net/osmand/binary/BinaryMapIndexReader.java @@ -111,7 +111,7 @@ public class BinaryMapIndexReader { /*private*/ List transportIndexes = new ArrayList(); /*private*/ List routingIndexes = new ArrayList(); /*private*/ List indexes = new ArrayList(); - TLongObjectHashMap incompleteTransportRoutes = null; + Map> incompleteTransportRoutes = null; protected CodedInputStream codedIS; @@ -2655,15 +2655,17 @@ public class BinaryMapIndexReader { } } - public TLongObjectHashMap getIncompleteTransportRoutes() throws InvalidProtocolBufferException, IOException { + public Map> getIncompleteTransportRoutes() throws InvalidProtocolBufferException, IOException { if (incompleteTransportRoutes == null) { - incompleteTransportRoutes = new TLongObjectHashMap<>(); + incompleteTransportRoutes = new HashMap>(); for (TransportIndex ti : transportIndexes) { if (ti.incompleteRoutesLength > 0) { + TLongObjectHashMap indexIncompleteRoutes = new TLongObjectHashMap(); codedIS.seek(ti.incompleteRoutesOffset); int oldLimit = codedIS.pushLimit(ti.incompleteRoutesLength); - transportAdapter.readIncompleteRoutesList(incompleteTransportRoutes); + transportAdapter.readIncompleteRoutesList(indexIncompleteRoutes, ti.filePointer); codedIS.popLimit(oldLimit); + incompleteTransportRoutes.put(ti, indexIncompleteRoutes); } } } diff --git a/OsmAnd-java/src/main/java/net/osmand/binary/BinaryMapTransportReaderAdapter.java b/OsmAnd-java/src/main/java/net/osmand/binary/BinaryMapTransportReaderAdapter.java index 358e908dbd..1f7a4ad6d5 100644 --- a/OsmAnd-java/src/main/java/net/osmand/binary/BinaryMapTransportReaderAdapter.java +++ b/OsmAnd-java/src/main/java/net/osmand/binary/BinaryMapTransportReaderAdapter.java @@ -11,6 +11,7 @@ import com.google.protobuf.WireFormat; import gnu.trove.map.hash.TIntObjectHashMap; import gnu.trove.map.hash.TLongObjectHashMap; import net.osmand.binary.BinaryMapIndexReader.SearchRequest; +import net.osmand.data.IncompleteTransportRoute; import net.osmand.data.TransportSchedule; import net.osmand.data.TransportStop; import net.osmand.data.TransportStopExit; @@ -71,7 +72,6 @@ public class BinaryMapTransportReaderAdapter { return bottom; } - IndexStringTable stringTable = null; } @@ -251,8 +251,9 @@ public class BinaryMapTransportReaderAdapter { return ((char) i)+""; } - public void readIncompleteRoutesList(TLongObjectHashMap incompleteRoutes) throws IOException { + public void readIncompleteRoutesList(TLongObjectHashMap incompleteRoutes, int transportIndexStart) throws IOException { boolean end = false; + int offset = codedIS.getTotalBytesRead(); while (!end) { int t = codedIS.readTag(); int tag = WireFormat.getTagFieldNumber(t); @@ -263,7 +264,7 @@ public class BinaryMapTransportReaderAdapter { case OsmandOdb.IncompleteTransportRoutes.ROUTES_FIELD_NUMBER: int l = codedIS.readRawVarint32(); int olds = codedIS.pushLimit(l); - net.osmand.data.IncompleteTransportRoute ir = readIncompleteRoute(); + net.osmand.data.IncompleteTransportRoute ir = readIncompleteRoute(offset, transportIndexStart); net.osmand.data.IncompleteTransportRoute itr = incompleteRoutes.get(ir.getRouteId()); if(itr != null) { itr.setNextLinkedRoute(ir); @@ -281,7 +282,7 @@ public class BinaryMapTransportReaderAdapter { } - public net.osmand.data.IncompleteTransportRoute readIncompleteRoute() throws IOException { + public net.osmand.data.IncompleteTransportRoute readIncompleteRoute(int readOffset, int transportIndexStart) throws IOException { net.osmand.data.IncompleteTransportRoute dataObject = new net.osmand.data.IncompleteTransportRoute(); boolean end = false; while(!end){ @@ -295,7 +296,12 @@ public class BinaryMapTransportReaderAdapter { dataObject.setRouteId(codedIS.readUInt64()); break; case OsmandOdb.IncompleteTransportRoute.ROUTEREF_FIELD_NUMBER : - dataObject.setRouteOffset(codedIS.readRawVarint32()); + int delta = codedIS.readRawVarint32(); + if (delta > transportIndexStart) { + dataObject.setRouteOffset(delta); + } else { + dataObject.setRouteOffset(readOffset - delta); + } break; case OsmandOdb.IncompleteTransportRoute.OPERATOR_FIELD_NUMBER : skipUnknownField(t); @@ -318,7 +324,6 @@ public class BinaryMapTransportReaderAdapter { break; } } - return dataObject; } diff --git a/OsmAnd-java/src/main/java/net/osmand/router/TransportRoutePlanner.java b/OsmAnd-java/src/main/java/net/osmand/router/TransportRoutePlanner.java index c9d471a497..c94831d8f4 100644 --- a/OsmAnd-java/src/main/java/net/osmand/router/TransportRoutePlanner.java +++ b/OsmAnd-java/src/main/java/net/osmand/router/TransportRoutePlanner.java @@ -865,7 +865,6 @@ public class TransportRoutePlanner { lst.add(segment); } } else { - // MapUtils.getDistance(s.getLocation(), route.getForwardStops().get(158).getLocation()); System.err.println(String.format("Routing error: missing stop '%s' in route '%s' id: %d", s.toString(), route.getRef(), route.getId() / 2)); } diff --git a/OsmAnd-java/src/main/java/net/osmand/router/TransportStopsRouteReader.java b/OsmAnd-java/src/main/java/net/osmand/router/TransportStopsRouteReader.java index d077335d0b..fd4b88e393 100644 --- a/OsmAnd-java/src/main/java/net/osmand/router/TransportStopsRouteReader.java +++ b/OsmAnd-java/src/main/java/net/osmand/router/TransportStopsRouteReader.java @@ -8,6 +8,7 @@ import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; +import java.util.Map.Entry; import gnu.trove.iterator.TIntObjectIterator; import gnu.trove.list.array.TIntArrayList; @@ -15,6 +16,7 @@ import gnu.trove.map.hash.TIntObjectHashMap; import gnu.trove.map.hash.TLongObjectHashMap; import net.osmand.binary.BinaryMapIndexReader; import net.osmand.binary.BinaryMapIndexReader.SearchRequest; +import net.osmand.binary.BinaryMapTransportReaderAdapter.TransportIndex; import net.osmand.data.IncompleteTransportRoute; import net.osmand.data.LatLon; import net.osmand.data.TransportRoute; @@ -43,7 +45,6 @@ public class TransportStopsRouteReader { List stops = r.searchTransportIndex(sr); TIntObjectHashMap routesToLoad = mergeTransportStops(r, loadedTransportStops, stops); loadRoutes(r, routesToLoad); - for (TransportStop stop : stops) { // skip missing stops if (stop.isMissingStop()) { @@ -388,18 +389,22 @@ public class TransportStopsRouteReader { List allRoutes = null; for (BinaryMapIndexReader bmir : routesFilesCache.keySet()) { // here we could limit routeMap indexes by only certain bbox around start / end (check comment on field) - IncompleteTransportRoute ptr = bmir.getIncompleteTransportRoutes().get(baseRoute.getId()); - if (ptr != null) { - TIntArrayList lst = new TIntArrayList(); - while (ptr != null) { - lst.add(ptr.getRouteOffset()); - ptr = ptr.getNextLinkedRoute(); - } - if (lst.size() > 0) { - if (allRoutes == null) { - allRoutes = new ArrayList(); + if (!bmir.getIncompleteTransportRoutes().isEmpty()) { + for (Entry> entry : bmir.getIncompleteTransportRoutes().entrySet()) { + IncompleteTransportRoute ptr = entry.getValue().get(baseRoute.getId()); + if (ptr != null) { + TIntArrayList lst = new TIntArrayList(); + while (ptr != null) { + lst.add(ptr.getRouteOffset()); + ptr = ptr.getNextLinkedRoute(); + } + if (lst.size() > 0) { + if (allRoutes == null) { + allRoutes = new ArrayList(); + } + allRoutes.addAll(bmir.getTransportRoutes(lst.toArray()).valueCollection()); + } } - allRoutes.addAll(bmir.getTransportRoutes(lst.toArray()).valueCollection()); } } } From 86b08b7568ecfd4c0316b3af8540229a2fb9fd7a Mon Sep 17 00:00:00 2001 From: MadWasp79 Date: Wed, 1 Jul 2020 13:29:56 +0300 Subject: [PATCH 2/2] fix cache and incomplete route offset --- .../osmand/binary/BinaryMapIndexReader.java | 12 +++---- .../BinaryMapTransportReaderAdapter.java | 7 ++-- .../osmand/data/IncompleteTransportRoute.java | 6 +++- .../router/TransportStopsRouteReader.java | 33 +++++++------------ 4 files changed, 24 insertions(+), 34 deletions(-) diff --git a/OsmAnd-java/src/main/java/net/osmand/binary/BinaryMapIndexReader.java b/OsmAnd-java/src/main/java/net/osmand/binary/BinaryMapIndexReader.java index 3e3b92a41e..c8f84796b9 100644 --- a/OsmAnd-java/src/main/java/net/osmand/binary/BinaryMapIndexReader.java +++ b/OsmAnd-java/src/main/java/net/osmand/binary/BinaryMapIndexReader.java @@ -111,7 +111,7 @@ public class BinaryMapIndexReader { /*private*/ List transportIndexes = new ArrayList(); /*private*/ List routingIndexes = new ArrayList(); /*private*/ List indexes = new ArrayList(); - Map> incompleteTransportRoutes = null; + TLongObjectHashMap incompleteTransportRoutes = null; protected CodedInputStream codedIS; @@ -2655,22 +2655,20 @@ public class BinaryMapIndexReader { } } - public Map> getIncompleteTransportRoutes() throws InvalidProtocolBufferException, IOException { + + public TLongObjectHashMap getIncompleteTransportRoutes() throws InvalidProtocolBufferException, IOException { if (incompleteTransportRoutes == null) { - incompleteTransportRoutes = new HashMap>(); + incompleteTransportRoutes = new TLongObjectHashMap<>(); for (TransportIndex ti : transportIndexes) { if (ti.incompleteRoutesLength > 0) { - TLongObjectHashMap indexIncompleteRoutes = new TLongObjectHashMap(); codedIS.seek(ti.incompleteRoutesOffset); int oldLimit = codedIS.pushLimit(ti.incompleteRoutesLength); - transportAdapter.readIncompleteRoutesList(indexIncompleteRoutes, ti.filePointer); + transportAdapter.readIncompleteRoutesList(incompleteTransportRoutes, ti.filePointer); codedIS.popLimit(oldLimit); - incompleteTransportRoutes.put(ti, indexIncompleteRoutes); } } } return incompleteTransportRoutes; } - } diff --git a/OsmAnd-java/src/main/java/net/osmand/binary/BinaryMapTransportReaderAdapter.java b/OsmAnd-java/src/main/java/net/osmand/binary/BinaryMapTransportReaderAdapter.java index 1f7a4ad6d5..1e733e50f6 100644 --- a/OsmAnd-java/src/main/java/net/osmand/binary/BinaryMapTransportReaderAdapter.java +++ b/OsmAnd-java/src/main/java/net/osmand/binary/BinaryMapTransportReaderAdapter.java @@ -253,7 +253,6 @@ public class BinaryMapTransportReaderAdapter { public void readIncompleteRoutesList(TLongObjectHashMap incompleteRoutes, int transportIndexStart) throws IOException { boolean end = false; - int offset = codedIS.getTotalBytesRead(); while (!end) { int t = codedIS.readTag(); int tag = WireFormat.getTagFieldNumber(t); @@ -264,7 +263,7 @@ public class BinaryMapTransportReaderAdapter { case OsmandOdb.IncompleteTransportRoutes.ROUTES_FIELD_NUMBER: int l = codedIS.readRawVarint32(); int olds = codedIS.pushLimit(l); - net.osmand.data.IncompleteTransportRoute ir = readIncompleteRoute(offset, transportIndexStart); + net.osmand.data.IncompleteTransportRoute ir = readIncompleteRoute(transportIndexStart); net.osmand.data.IncompleteTransportRoute itr = incompleteRoutes.get(ir.getRouteId()); if(itr != null) { itr.setNextLinkedRoute(ir); @@ -282,7 +281,7 @@ public class BinaryMapTransportReaderAdapter { } - public net.osmand.data.IncompleteTransportRoute readIncompleteRoute(int readOffset, int transportIndexStart) throws IOException { + public net.osmand.data.IncompleteTransportRoute readIncompleteRoute(int transportIndexStart) throws IOException { net.osmand.data.IncompleteTransportRoute dataObject = new net.osmand.data.IncompleteTransportRoute(); boolean end = false; while(!end){ @@ -300,7 +299,7 @@ public class BinaryMapTransportReaderAdapter { if (delta > transportIndexStart) { dataObject.setRouteOffset(delta); } else { - dataObject.setRouteOffset(readOffset - delta); + dataObject.setRouteOffset(transportIndexStart + delta); } break; case OsmandOdb.IncompleteTransportRoute.OPERATOR_FIELD_NUMBER : diff --git a/OsmAnd-java/src/main/java/net/osmand/data/IncompleteTransportRoute.java b/OsmAnd-java/src/main/java/net/osmand/data/IncompleteTransportRoute.java index 84be533a5f..9518f1237d 100644 --- a/OsmAnd-java/src/main/java/net/osmand/data/IncompleteTransportRoute.java +++ b/OsmAnd-java/src/main/java/net/osmand/data/IncompleteTransportRoute.java @@ -13,7 +13,11 @@ public class IncompleteTransportRoute { } public void setNextLinkedRoute(IncompleteTransportRoute nextLinkedRoute) { - this.nextLinkedRoute = nextLinkedRoute; + if (this.nextLinkedRoute == null) { + this.nextLinkedRoute = nextLinkedRoute; + } else { + this.nextLinkedRoute.setNextLinkedRoute(nextLinkedRoute); + } } public long getRouteId() { diff --git a/OsmAnd-java/src/main/java/net/osmand/router/TransportStopsRouteReader.java b/OsmAnd-java/src/main/java/net/osmand/router/TransportStopsRouteReader.java index fd4b88e393..7428196332 100644 --- a/OsmAnd-java/src/main/java/net/osmand/router/TransportStopsRouteReader.java +++ b/OsmAnd-java/src/main/java/net/osmand/router/TransportStopsRouteReader.java @@ -389,32 +389,21 @@ public class TransportStopsRouteReader { List allRoutes = null; for (BinaryMapIndexReader bmir : routesFilesCache.keySet()) { // here we could limit routeMap indexes by only certain bbox around start / end (check comment on field) - if (!bmir.getIncompleteTransportRoutes().isEmpty()) { - for (Entry> entry : bmir.getIncompleteTransportRoutes().entrySet()) { - IncompleteTransportRoute ptr = entry.getValue().get(baseRoute.getId()); - if (ptr != null) { - TIntArrayList lst = new TIntArrayList(); - while (ptr != null) { - lst.add(ptr.getRouteOffset()); - ptr = ptr.getNextLinkedRoute(); - } - if (lst.size() > 0) { - if (allRoutes == null) { - allRoutes = new ArrayList(); - } - allRoutes.addAll(bmir.getTransportRoutes(lst.toArray()).valueCollection()); - } + IncompleteTransportRoute ptr = bmir.getIncompleteTransportRoutes().get(baseRoute.getId()); + if (ptr != null) { + TIntArrayList lst = new TIntArrayList(); + while (ptr != null) { + lst.add(ptr.getRouteOffset()); + ptr = ptr.getNextLinkedRoute(); + } + if (lst.size() > 0) { + if (allRoutes == null) { + allRoutes = new ArrayList(); } + allRoutes.addAll(bmir.getTransportRoutes(lst.toArray()).valueCollection()); } } } return allRoutes; } - - - - - - - }