Finish algorith to load for transport router

This commit is contained in:
Victor Shcherb 2019-07-29 16:13:55 +02:00
parent ac7cfffdd4
commit 5827eff2bc
2 changed files with 93 additions and 59 deletions

View file

@ -32,6 +32,13 @@ public class TransportStop extends MapObject {
public void setRoutes(List<TransportRoute> routes) { public void setRoutes(List<TransportRoute> routes) {
this.routes = routes; this.routes = routes;
} }
public void addRoute(TransportRoute rt) {
if (this.routes == null) {
this.routes = new ArrayList<TransportRoute>();
}
this.routes.add(rt);
}
public int[] getReferencesToRoutes() { public int[] getReferencesToRoutes() {
return referencesToRoutes; return referencesToRoutes;

View file

@ -9,12 +9,14 @@ import net.osmand.data.TransportSchedule;
import net.osmand.data.TransportStop; import net.osmand.data.TransportStop;
import net.osmand.osm.edit.Node; import net.osmand.osm.edit.Node;
import net.osmand.osm.edit.Way; import net.osmand.osm.edit.Way;
import net.osmand.util.Algorithms;
import net.osmand.util.MapUtils; import net.osmand.util.MapUtils;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
import java.util.Iterator;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -765,82 +767,62 @@ public class TransportRoutePlanner {
int pz = (31 - cfg.ZOOM_TO_LOAD_TILES); int pz = (31 - cfg.ZOOM_TO_LOAD_TILES);
SearchRequest<TransportStop> sr = BinaryMapIndexReader.buildSearchTransportRequest(x << pz, (x + 1) << pz, SearchRequest<TransportStop> sr = BinaryMapIndexReader.buildSearchTransportRequest(x << pz, (x + 1) << pz,
y << pz, (y + 1) << pz, -1, null); y << pz, (y + 1) << pz, -1, null);
// could be global?
// could be global ?
TLongObjectHashMap<TransportStop> loadedTransportStops = new TLongObjectHashMap<TransportStop>(); TLongObjectHashMap<TransportStop> loadedTransportStops = new TLongObjectHashMap<TransportStop>();
TIntArrayList localFileRoutesToLoad = new TIntArrayList();
// ?
List<TransportStop> existingStops = new ArrayList<>();
// local
TLongObjectHashMap<TLongHashSet> stopsRoutes = new TLongObjectHashMap<>();
TIntArrayList allStopReferencesForTile = new TIntArrayList();
TIntArrayList stopReferences = new TIntArrayList();
for (BinaryMapIndexReader r : routeMap.keySet()) { for (BinaryMapIndexReader r : routeMap.keySet()) {
sr.clearSearchResults(); sr.clearSearchResults();
allStopReferencesForTile.clear();
List<TransportStop> stops = r.searchTransportIndex(sr); List<TransportStop> stops = r.searchTransportIndex(sr);
for (TransportStop stop : stops) {
long stopId = stop.getId(); localFileRoutesToLoad.clear();
TransportStop mainStop = loadedTransportStops.get(stopId); prepareRoutesToLoad(loadedTransportStops, stops, localFileRoutesToLoad);
if (mainStop == null) {
mainStop = stop;
loadedTransportStops.put(stopId, stop);
}
if (!mainStop.isDeleted()) {
stopReferences.clear();
stopReferences.addAll(stop.getReferencesToRoutes());
long[] routesIds = stop.getRoutesIds();
if (routesIds != null) {
TLongHashSet routes = stopsRoutes.get(stopId);
if (routes == null) {
routes = new TLongHashSet(routesIds);
stopsRoutes.put(stopId, routes);
} else {
for (int i = 0; i < routesIds.length; i++) {
long routeId = routesIds[i];
if (routes.contains(routeId)) {
stopReferences.removeAt(i);
} else {
routes.add(routeId);
}
}
}
}
if (stopReferences.size() > 0) {
allStopReferencesForTile.addAll(stopReferences);
} else {
existingStops.add(stop);
}
} else {
existingStops.add(stop);
}
}
if (existingStops.size() > 0) {
stops.removeAll(existingStops);
}
// load routes and create transport segments // load routes and create transport segments
if (allStopReferencesForTile.size() > 0) { TIntObjectHashMap<TransportRoute> localFileRoutes = new TIntObjectHashMap<TransportRoute>();
allStopReferencesForTile.sort(); if (localFileRoutesToLoad.size() > 0) {
localFileRoutesToLoad.sort();
TIntArrayList referencesToLoad = new TIntArrayList(); TIntArrayList referencesToLoad = new TIntArrayList();
TIntObjectHashMap<TransportRoute> loadedRoutes = routeMap.get(r); TIntObjectHashMap<TransportRoute> loadedRoutes = routeMap.get(r);
TIntObjectHashMap<TransportRoute> routes = new TIntObjectHashMap<TransportRoute>(); TIntIterator it = localFileRoutesToLoad.iterator();
TIntIterator it = allStopReferencesForTile.iterator(); int p = localFileRoutesToLoad.get(0) + 1; // different
int p = allStopReferencesForTile.get(0) + 1; // different
while (it.hasNext()) { while (it.hasNext()) {
int nxt = it.next(); int nxt = it.next();
if (p != nxt) { if (p != nxt) {
if (loadedRoutes.contains(nxt)) { if (loadedRoutes.contains(nxt)) {
routes.put(nxt, loadedRoutes.get(nxt)); localFileRoutes.put(nxt, loadedRoutes.get(nxt));
} else { } else {
referencesToLoad.add(nxt); referencesToLoad.add(nxt);
} }
} }
} }
r.loadTransportRoutes(referencesToLoad.toArray(), routes); r.loadTransportRoutes(referencesToLoad.toArray(), localFileRoutes);
loadedRoutes.putAll(routes); loadedRoutes.putAll(localFileRoutes);
}
for (TransportStop stop : stops) {
long stopId = stop.getId();
TransportStop multifileStop = loadedTransportStops.get(stopId);
int[] rrs = stop.getReferencesToRoutes();
if (multifileStop == stop) {
// clear up so it won't be used as it is multi file stop
stop.setReferencesToRoutes(null);
}
if(rrs != null && !multifileStop.isDeleted()) {
for(int i = 0; i < rrs.length; i++) {
TransportRoute route = localFileRoutes.get(rrs[i]);
if(route == null) {
System.err.println(String.format("Something went wrong by loading route %d for stop %s", rrs[i], stop));
} else {
// duplicates won't be added
multifileStop.addRouteId(route.getId());
multifileStop.addRoute(route);
}
}
}
} }
} }
@ -850,6 +832,51 @@ public class TransportRoutePlanner {
return lst; return lst;
} }
private List<TransportStop> prepareRoutesToLoad(TLongObjectHashMap<TransportStop> loadedTransportStops,
List<TransportStop> stops, TIntArrayList resRoutesToLoad) {
Iterator<TransportStop> it = stops.iterator();
while (it.hasNext()) {
TransportStop stop = it.next();
long stopId = stop.getId();
TransportStop multifileStop = loadedTransportStops.get(stopId);
long[] routesIds = stop.getRoutesIds();
long[] delRIds = stop.getDeletedRoutesIds();
if (multifileStop == null) {
loadedTransportStops.put(stopId, stop);
if(!stop.isDeleted()) {
resRoutesToLoad.addAll(stop.getReferencesToRoutes());
}
} else if(multifileStop.isDeleted()){
// stop has noting to load, so not needed
it.remove();
} else {
if (delRIds != null) {
for (long deletedRouteId : delRIds) {
multifileStop.addDeletedRouteId(deletedRouteId);
}
}
if (routesIds != null && routesIds.length > 0) {
int[] refs = stop.getReferencesToRoutes();
for (int i = 0; i < routesIds.length; i++) {
long routeId = routesIds[i];
if (!multifileStop.hasRoute(routeId) && !multifileStop.isRouteDeleted(routeId)) {
resRoutesToLoad.add(refs[i]);
}
}
} else {
if (stop.hasReferencesToRoutes()) {
// old format
resRoutesToLoad.addAll(stop.getReferencesToRoutes());
} else {
// stop has noting to load, so not needed
it.remove();
}
}
}
}
return stops;
}
private void loadTransportSegments(TransportStop[] stops, List<TransportRouteSegment> lst) throws IOException { private void loadTransportSegments(TransportStop[] stops, List<TransportRouteSegment> lst) throws IOException {
for(TransportStop s : stops) { for(TransportStop s : stops) {
if (s.isDeleted()) { if (s.isDeleted()) {