Fix transport context menu / routing

This commit is contained in:
max-klaus 2019-07-31 21:03:49 +03:00
parent 39845c3601
commit e0b6506a7c
8 changed files with 257 additions and 171 deletions

View file

@ -6,7 +6,9 @@ import net.osmand.util.MapUtils;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
public class TransportStop extends MapObject {
@ -20,6 +22,7 @@ public class TransportStop extends MapObject {
public int y31;
private List<TransportStopExit> exits;
private List<TransportRoute> routes = null;
private LinkedHashMap<String, int[]> referencesToRoutesMap;
private TransportStopAggregated transportStopAggregated;
@ -28,7 +31,20 @@ public class TransportStop extends MapObject {
public List<TransportRoute> getRoutes() {
return routes;
}
public LinkedHashMap<String, int[]> getReferencesToRoutesMap() {
return referencesToRoutesMap;
}
public void putReferencesToRoutes(String repositoryFileName, int[] referencesToRoutes) {
LinkedHashMap<String, int[]> referencesToRoutesMap = this.referencesToRoutesMap;
if (referencesToRoutesMap == null) {
referencesToRoutesMap = new LinkedHashMap<>();
this.referencesToRoutesMap = referencesToRoutesMap;
}
referencesToRoutesMap.put(repositoryFileName, referencesToRoutes);
}
public void setRoutes(List<TransportRoute> routes) {
this.routes = routes;
}

View file

@ -765,38 +765,14 @@ public class TransportRoutePlanner {
// could be global ?
TLongObjectHashMap<TransportStop> loadedTransportStops = new TLongObjectHashMap<TransportStop>();
TIntArrayList localFileRoutesToLoad = new TIntArrayList();
TIntObjectHashMap<TransportRoute> localFileRoutes = new TIntObjectHashMap<>();
for (BinaryMapIndexReader r : routeMap.keySet()) {
sr.clearSearchResults();
List<TransportStop> stops = r.searchTransportIndex(sr);
localFileRoutesToLoad.clear();
prepareRoutesToLoad(loadedTransportStops, stops, localFileRoutesToLoad);
// load routes and create transport segments
TIntObjectHashMap<TransportRoute> localFileRoutes = new TIntObjectHashMap<TransportRoute>();
if (localFileRoutesToLoad.size() > 0) {
localFileRoutesToLoad.sort();
TIntArrayList referencesToLoad = new TIntArrayList();
TIntObjectHashMap<TransportRoute> loadedRoutes = routeMap.get(r);
TIntIterator it = localFileRoutesToLoad.iterator();
int p = localFileRoutesToLoad.get(0) + 1; // different
while (it.hasNext()) {
int nxt = it.next();
if (p != nxt) {
if (loadedRoutes.contains(nxt)) {
localFileRoutes.put(nxt, loadedRoutes.get(nxt));
} else {
referencesToLoad.add(nxt);
}
}
}
r.loadTransportRoutes(referencesToLoad.toArray(), localFileRoutes);
loadedRoutes.putAll(localFileRoutes);
}
localFileRoutes.clear();
mergeTransportStops(r, loadedTransportStops, stops, localFileRoutes, routeMap.get(r));
for (TransportStop stop : stops) {
long stopId = stop.getId();
TransportStop multifileStop = loadedTransportStops.get(stopId);
@ -808,19 +784,18 @@ public class TransportRoutePlanner {
// add other routes
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 if(multifileStop == stop ||
(!multifileStop.hasRoute(route.getId()) &&
!multifileStop.isRouteDeleted(route.getId()))){
if (rrs != null && !multifileStop.isDeleted()) {
for (int rr : rrs) {
TransportRoute route = localFileRoutes.get(rr);
if (route == null) {
System.err.println(String.format("Something went wrong by loading route %d for stop %s", rr, stop));
} else if (multifileStop == stop ||
(!multifileStop.hasRoute(route.getId()) &&
!multifileStop.isRouteDeleted(route.getId()))) {
// duplicates won't be added
multifileStop.addRouteId(route.getId());
multifileStop.addRoute(route);
}
}
}
}
@ -832,21 +807,28 @@ public class TransportRoutePlanner {
return lst;
}
private List<TransportStop> prepareRoutesToLoad(TLongObjectHashMap<TransportStop> loadedTransportStops,
List<TransportStop> stops, TIntArrayList resRoutesToLoad) {
public static List<TransportStop> mergeTransportStops(BinaryMapIndexReader reader,
TLongObjectHashMap<TransportStop> loadedTransportStops,
List<TransportStop> stops,
TIntObjectHashMap<TransportRoute> localFileRoutes,
TIntObjectHashMap<TransportRoute> loadedRoutes) throws IOException {
TIntArrayList routesToLoad = new TIntArrayList();
TIntArrayList localRoutesToLoad = new TIntArrayList();
Iterator<TransportStop> it = stops.iterator();
while (it.hasNext()) {
TransportStop stop = it.next();
long stopId = stop.getId();
localRoutesToLoad.clear();
TransportStop multifileStop = loadedTransportStops.get(stopId);
long[] routesIds = stop.getRoutesIds();
long[] delRIds = stop.getDeletedRoutesIds();
if (multifileStop == null) {
loadedTransportStops.put(stopId, stop);
multifileStop = stop;
if(!stop.isDeleted()) {
resRoutesToLoad.addAll(stop.getReferencesToRoutes());
localRoutesToLoad.addAll(stop.getReferencesToRoutes());
}
} else if(multifileStop.isDeleted()){
} else if (multifileStop.isDeleted()){
// stop has noting to load, so not needed
it.remove();
} else {
@ -860,20 +842,45 @@ public class TransportRoutePlanner {
for (int i = 0; i < routesIds.length; i++) {
long routeId = routesIds[i];
if (!multifileStop.hasRoute(routeId) && !multifileStop.isRouteDeleted(routeId)) {
resRoutesToLoad.add(refs[i]);
localRoutesToLoad.add(refs[i]);
}
}
} else {
if (stop.hasReferencesToRoutes()) {
// old format
resRoutesToLoad.addAll(stop.getReferencesToRoutes());
localRoutesToLoad.addAll(stop.getReferencesToRoutes());
} else {
// stop has noting to load, so not needed
it.remove();
}
}
}
routesToLoad.addAll(localRoutesToLoad);
multifileStop.putReferencesToRoutes(reader.getFile().getName(), localRoutesToLoad.toArray());
}
// load routes
if (routesToLoad.size() > 0) {
routesToLoad.sort();
TIntArrayList referencesToLoad = new TIntArrayList();
TIntIterator itr = routesToLoad.iterator();
int p = routesToLoad.get(0) + 1; // different
while (itr.hasNext()) {
int nxt = itr.next();
if (p != nxt) {
if (localFileRoutes != null && loadedRoutes != null && loadedRoutes.contains(nxt)) {
localFileRoutes.put(nxt, loadedRoutes.get(nxt));
} else {
referencesToLoad.add(nxt);
}
}
}
if (localFileRoutes != null && loadedRoutes != null) {
reader.loadTransportRoutes(referencesToLoad.toArray(), localFileRoutes);
loadedRoutes.putAll(localFileRoutes);
}
}
return stops;
}
@ -937,7 +944,7 @@ public class TransportRoutePlanner {
}
}
}

View file

@ -4,6 +4,7 @@ import android.content.Context;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
@ -11,6 +12,7 @@ import android.widget.LinearLayout;
import android.widget.TextView;
import net.osmand.AndroidUtils;
import net.osmand.PlatformUtil;
import net.osmand.ResultMatcher;
import net.osmand.binary.BinaryMapIndexReader;
import net.osmand.data.Amenity;
@ -25,14 +27,18 @@ import net.osmand.plus.R;
import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.mapcontextmenu.MenuBuilder;
import net.osmand.plus.myplaces.FavoritesActivity;
import net.osmand.plus.routing.TransportRoutingHelper;
import net.osmand.plus.widgets.TextViewEx;
import net.osmand.util.Algorithms;
import net.osmand.util.MapUtils;
import java.io.IOException;
import java.util.List;
public class FavouritePointMenuBuilder extends MenuBuilder {
private static final org.apache.commons.logging.Log LOG = PlatformUtil.getLog(FavouritePointMenuBuilder.class);
private final FavouritePoint fav;
private Object originObject;
@ -199,26 +205,31 @@ public class FavouritePointMenuBuilder extends MenuBuilder {
}
private TransportStop findTransportStop(String nameStringEn, double lat, double lon) {
QuadRect rect = MapUtils.calculateLatLonBbox(lat, lon, 15);
List<TransportStop> res = app.getResourceManager().searchTransportSync(rect.top, rect.left,
rect.bottom, rect.right, new ResultMatcher<TransportStop>() {
List<TransportStop> res = null;
try {
res = app.getResourceManager().searchTransportSync(rect.top, rect.left,
rect.bottom, rect.right, new ResultMatcher<TransportStop>() {
@Override
public boolean publish(TransportStop object) {
return true;
}
@Override
public boolean publish(TransportStop object) {
return true;
}
@Override
public boolean isCancelled() {
return false;
}
});
for (TransportStop stop : res) {
String stringEn = stop.toStringEn();
if (stringEn.equals(nameStringEn)) {
return stop;
@Override
public boolean isCancelled() {
return false;
}
});
} catch (IOException e) {
LOG.error(e.getMessage(), e);
}
if (res != null) {
for (TransportStop stop : res) {
String stringEn = stop.toStringEn();
if (stringEn.equals(nameStringEn)) {
return stop;
}
}
}
return null;

View file

@ -22,14 +22,13 @@ import net.osmand.plus.transport.TransportStopType;
import net.osmand.util.Algorithms;
import net.osmand.util.MapUtils;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import gnu.trove.list.array.TLongArrayList;
public class TransportStopController extends MenuController {
public static final int SHOW_STOPS_RADIUS_METERS = 150;
@ -124,22 +123,16 @@ public class TransportStopController extends MenuController {
MapActivity mapActivity = getMapActivity();
if (mapActivity != null) {
OsmandApplication app = mapActivity.getMyApplication();
List<TransportIndexRepository> reps = app.getResourceManager().searchTransportRepositories(transportStop.getLocation().getLatitude(),
transportStop.getLocation().getLongitude());
boolean useEnglishNames = app.getSettings().usingEnglishNames();
if (transportStop.getTransportStopAggregated() == null) {
processTransportStopAggregated(app, transportStop);
}
for (TransportIndexRepository t : reps) {
if (t.acceptTransportStop(transportStop)) {
ArrayList<TransportStop> transportStopsSameExit = new ArrayList<TransportStop>(transportStop.getLocalTransportStops());
ArrayList<TransportStop> nearbyTransportStops = new ArrayList<TransportStop>(transportStop.getNearbyTransportStops());
ArrayList<TransportStop> transportStopsSameExit = new ArrayList<>(transportStop.getLocalTransportStops());
ArrayList<TransportStop> nearbyTransportStops = new ArrayList<>(transportStop.getNearbyTransportStops());
addTransportStopRoutes(app, transportStopsSameExit, routesOnTheSameExit, useEnglishNames);
addTransportStopRoutes(app, nearbyTransportStops, routesNearby, useEnglishNames);
addTransportStopRoutes(transportStopsSameExit, routesOnTheSameExit, useEnglishNames, t);
addTransportStopRoutes(nearbyTransportStops, routesNearby, useEnglishNames, t);
}
}
sortTransportStopRoutes(routesOnTheSameExit);
sortTransportStopRoutes(routesNearby);
}
@ -166,16 +159,16 @@ public class TransportStopController extends MenuController {
});
}
private void addTransportStopRoutes(List<TransportStop> stops, List<TransportStopRoute> routes, boolean useEnglishNames, TransportIndexRepository t) {
private void addTransportStopRoutes(OsmandApplication app, List<TransportStop> stops, List<TransportStopRoute> routes, boolean useEnglishNames) {
for (TransportStop tstop : stops) {
if (tstop.hasReferencesToRoutes()) {
addRoutes(routes, useEnglishNames, t, tstop, transportStop, (int) MapUtils.getDistance(tstop.getLocation(), transportStop.getLocation()));
addRoutes(app, routes, useEnglishNames, tstop, transportStop, (int) MapUtils.getDistance(tstop.getLocation(), transportStop.getLocation()));
}
}
}
private void addRoutes(List<TransportStopRoute> routes, boolean useEnglishNames, TransportIndexRepository t, TransportStop s, TransportStop refStop, int dist) {
Collection<TransportRoute> rts = t.getRouteForStop(s);
private void addRoutes(OsmandApplication app, List<TransportStopRoute> routes, boolean useEnglishNames, TransportStop s, TransportStop refStop, int dist) {
List<TransportRoute> rts = app.getResourceManager().getRoutesForStop(s);
if (rts != null) {
for (TransportRoute rs : rts) {
boolean routeAlreadyAdded = checkSameRoute(routes, rs);
@ -198,7 +191,7 @@ public class TransportStopController extends MenuController {
}
}
public static void sortTransportStops(@NonNull LatLon latLon, List<TransportStop> transportStops) {
private static void sortTransportStops(@NonNull LatLon latLon, List<TransportStop> transportStops) {
for (TransportStop transportStop : transportStops) {
transportStop.distance = (int) MapUtils.getDistance(latLon, transportStop.getLocation());
}
@ -211,26 +204,14 @@ public class TransportStopController extends MenuController {
});
}
@NonNull
public static List<TransportStop> findTransportStopsAt(OsmandApplication app, double latitude, double longitude, int radiusMeters) {
ArrayList<TransportStop> transportStops = new ArrayList<>();
List<TransportIndexRepository> reps = app.getResourceManager().searchTransportRepositories(latitude, longitude);
TLongArrayList addedTransportStops = new TLongArrayList();
for (TransportIndexRepository t : reps) {
ArrayList<TransportStop> stops = new ArrayList<>();
QuadRect ll = MapUtils.calculateLatLonBbox(latitude, longitude, radiusMeters);
t.searchTransportStops(ll.top, ll.left, ll.bottom, ll.right, -1, stops, null);
for (TransportStop transportStop : stops) {
if (!addedTransportStops.contains(transportStop.getId())) {
addedTransportStops.add(transportStop.getId());
if (!transportStop.isDeleted()) {
transportStops.add(transportStop);
}
}
}
@Nullable
private static List<TransportStop> findTransportStopsAt(OsmandApplication app, double latitude, double longitude, int radiusMeters) {
QuadRect ll = MapUtils.calculateLatLonBbox(latitude, longitude, radiusMeters);
try {
return app.getResourceManager().searchTransportSync(ll.top, ll.left, ll.bottom, ll.right, null);
} catch (IOException e) {
return null;
}
return transportStops;
}
@Nullable
@ -241,6 +222,9 @@ public class TransportStopController extends MenuController {
LatLon loc = amenity.getLocation();
int radiusMeters = isSubwayEntrance ? SHOW_SUBWAY_STOPS_FROM_ENTRANCES_RADIUS_METERS : SHOW_STOPS_RADIUS_METERS;
List<TransportStop> transportStops = findTransportStopsAt(app, loc.getLatitude(), loc.getLongitude(), radiusMeters);
if (transportStops == null) {
return null;
}
sortTransportStops(loc, transportStops);
if (isSubwayEntrance) {
@ -275,20 +259,21 @@ public class TransportStopController extends MenuController {
return null;
}
public static void processTransportStopAggregated(OsmandApplication app, TransportStop transportStop) {
private static void processTransportStopAggregated(OsmandApplication app, TransportStop transportStop) {
TransportStopAggregated stopAggregated = new TransportStopAggregated();
transportStop.setTransportStopAggregated(stopAggregated);
stopAggregated.addLocalTransportStop(transportStop);
LatLon loc = transportStop.getLocation();
List<TransportStop> transportStops = findTransportStopsAt(app, loc.getLatitude(), loc.getLongitude(), SHOW_STOPS_RADIUS_METERS);
for (TransportStop stop : transportStops) {
stopAggregated.addNearbyTransportStop(stop);
if (transportStops != null) {
for (TransportStop stop : transportStops) {
stopAggregated.addNearbyTransportStop(stop);
}
}
}
public static TransportStopAggregated processTransportStopsForAmenity(List<TransportStop> transportStops, Amenity amenity) {
private static TransportStopAggregated processTransportStopsForAmenity(List<TransportStop> transportStops, Amenity amenity) {
TransportStopAggregated stopAggregated = new TransportStopAggregated();
stopAggregated.setAmenity(amenity);
@ -311,7 +296,7 @@ public class TransportStopController extends MenuController {
return stopAggregated;
}
public static boolean checkSameRoute(List<TransportStopRoute> stopRoutes, TransportRoute route) {
private static boolean checkSameRoute(List<TransportStopRoute> stopRoutes, TransportRoute route) {
for (TransportStopRoute stopRoute : stopRoutes) {
if (stopRoute.route.compareRoute(route)) {
return true;

View file

@ -23,6 +23,7 @@ import net.osmand.binary.BinaryMapIndexReader.SearchPoiTypeFilter;
import net.osmand.binary.CachedOsmandIndexes;
import net.osmand.data.Amenity;
import net.osmand.data.RotatedTileBox;
import net.osmand.data.TransportRoute;
import net.osmand.data.TransportStop;
import net.osmand.map.ITileSource;
import net.osmand.map.MapTileDownloader;
@ -44,6 +45,7 @@ import net.osmand.plus.resources.AsyncLoadingThread.OnMapLoadedListener;
import net.osmand.plus.resources.AsyncLoadingThread.TileLoadDownloadRequest;
import net.osmand.plus.srtmplugin.SRTMPlugin;
import net.osmand.plus.views.OsmandMapLayer.DrawSettings;
import net.osmand.router.TransportRoutePlanner.TransportRoutingContext;
import net.osmand.util.Algorithms;
import net.osmand.util.MapUtils;
@ -60,15 +62,19 @@ import java.io.InputStream;
import java.io.RandomAccessFile;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
import gnu.trove.list.array.TLongArrayList;
import gnu.trove.iterator.TLongObjectIterator;
import gnu.trove.map.hash.TIntObjectHashMap;
import gnu.trove.map.hash.TLongObjectHashMap;
import static net.osmand.plus.download.DownloadOsmandIndexesHelper.assetMapping;
@ -442,7 +448,7 @@ public class ResourceManager {
Map<String, String> mapping = assetMapping(context.getAssets());
File appPath = context.getAppPath(null);
if (appPath.canWrite()) {
for (Map.Entry<String,String> entry : mapping.entrySet()) {
for (Entry<String,String> entry : mapping.entrySet()) {
File jsFile = new File(appPath, entry.getValue());
if (entry.getValue().contains("-tts") && entry.getValue()
.endsWith(IndexConstants.TTSVOICE_INDEX_EXT_JS)) {
@ -774,6 +780,20 @@ public class ResourceManager {
}
////////////////////////////////////////////// Working with amenities ////////////////////////////////////////////////
public List<AmenityIndexRepository> getAmenityRepositories() {
List<String> fileNames = new ArrayList<>(amenityRepositories.keySet());
Collections.sort(fileNames, Algorithms.getStringVersionComparator());
List<AmenityIndexRepository> res = new ArrayList<>();
for (String fileName : fileNames) {
AmenityIndexRepository r = amenityRepositories.get(fileName);
if (r != null) {
res.add(r);
}
}
return res;
}
public List<Amenity> searchAmenities(SearchPoiTypeFilter filter,
double topLatitude, double leftLongitude, double bottomLatitude, double rightLongitude, int zoom, final ResultMatcher<Amenity> matcher) {
final List<Amenity> amenities = new ArrayList<Amenity>();
@ -784,14 +804,11 @@ public class ResourceManager {
int left31 = MapUtils.get31TileNumberX(leftLongitude);
int bottom31 = MapUtils.get31TileNumberY(bottomLatitude);
int right31 = MapUtils.get31TileNumberX(rightLongitude);
List<String> fileNames = new ArrayList<>(amenityRepositories.keySet());
Collections.sort(fileNames, Algorithms.getStringVersionComparator());
for (String name : fileNames) {
for (AmenityIndexRepository index : getAmenityRepositories()) {
if (matcher != null && matcher.isCancelled()) {
searchAmenitiesInProgress = false;
break;
}
AmenityIndexRepository index = amenityRepositories.get(name);
if (index != null && index.checkContainsInt(top31, left31, bottom31, right31)) {
List<Amenity> r = index.searchAmenities(top31,
left31, bottom31, right31, zoom, filter, matcher);
@ -825,7 +842,7 @@ public class ResourceManager {
rightLongitude = Math.max(rightLongitude, l.getLongitude());
}
if (!filter.isEmpty()) {
for (AmenityIndexRepository index : amenityRepositories.values()) {
for (AmenityIndexRepository index : getAmenityRepositories()) {
if (index.checkContainsInt(
MapUtils.get31TileNumberY(topLatitude),
MapUtils.get31TileNumberX(leftLongitude),
@ -852,7 +869,7 @@ public class ResourceManager {
public boolean containsAmenityRepositoryToSearch(boolean searchByName){
for (AmenityIndexRepository index : amenityRepositories.values()) {
for (AmenityIndexRepository index : getAmenityRepositories()) {
if(searchByName){
if(index instanceof AmenityIndexRepositoryBinary){
return true;
@ -873,7 +890,7 @@ public class ResourceManager {
int top = MapUtils.get31TileNumberY(topLatitude);
int right = MapUtils.get31TileNumberX(rightLongitude);
int bottom = MapUtils.get31TileNumberY(bottomLatitude);
for (AmenityIndexRepository index : amenityRepositories.values()) {
for (AmenityIndexRepository index : getAmenityRepositories()) {
if (matcher != null && matcher.isCancelled()) {
break;
}
@ -909,7 +926,7 @@ public class ResourceManager {
public Map<PoiCategory, List<String>> searchAmenityCategoriesByName(String searchQuery, double lat, double lon) {
Map<PoiCategory, List<String>> map = new LinkedHashMap<PoiCategory, List<String>>();
for (AmenityIndexRepository index : amenityRepositories.values()) {
for (AmenityIndexRepository index : getAmenityRepositories()) {
if (index instanceof AmenityIndexRepositoryBinary) {
if (index.checkContains(lat, lon)) {
((AmenityIndexRepositoryBinary) index).searchAmenityCategoriesByName(searchQuery, map);
@ -934,14 +951,37 @@ public class ResourceManager {
}
public Collection<BinaryMapReaderResource> getFileReaders() {
return fileReaders.values();
List<String> fileNames = new ArrayList<>(fileReaders.keySet());
Collections.sort(fileNames, Algorithms.getStringVersionComparator());
List<BinaryMapReaderResource> res = new ArrayList<>();
for (String fileName : fileNames) {
BinaryMapReaderResource r = fileReaders.get(fileName);
if (r != null) {
res.add(r);
}
}
return res;
}
////////////////////////////////////////////// Working with transport ////////////////////////////////////////////////
public LinkedHashMap<String, TransportIndexRepository> getTransportRepositories() {
List<String> fileNames = new ArrayList<>(transportRepositories.keySet());
Collections.sort(fileNames, Algorithms.getStringVersionComparator());
LinkedHashMap<String, TransportIndexRepository> res = new LinkedHashMap<>();
for (String fileName : fileNames) {
TransportIndexRepository r = transportRepositories.get(fileName);
if (r != null) {
res.put(fileName, r);
}
}
return res;
}
public List<TransportIndexRepository> searchTransportRepositories(double latitude, double longitude) {
List<TransportIndexRepository> repos = new ArrayList<TransportIndexRepository>();
for (TransportIndexRepository index : transportRepositories.values()) {
List<TransportIndexRepository> repos = new ArrayList<>();
for (TransportIndexRepository index : getTransportRepositories().values()) {
if (index.isUseForPublicTransport() && index.checkContains(latitude,longitude)) {
repos.add(index);
}
@ -949,34 +989,48 @@ public class ResourceManager {
return repos;
}
public List<TransportStop> searchTransportSync(double topLat, double leftLon, double bottomLat, double rightLon, ResultMatcher<TransportStop> matcher) {
public List<TransportStop> searchTransportSync(double topLat, double leftLon, double bottomLat, double rightLon,
ResultMatcher<TransportStop> matcher) throws IOException {
List<TransportIndexRepository> repos = new ArrayList<>();
List<TransportStop> stops = new ArrayList<>();
for (TransportIndexRepository index : transportRepositories.values()) {
TLongObjectHashMap<TransportStop> loadedTransportStops = new TLongObjectHashMap<>();
for (TransportIndexRepository index : getTransportRepositories().values()) {
if (index.isUseForPublicTransport() && index.checkContains(topLat, leftLon, bottomLat, rightLon)) {
repos.add(index);
}
}
if (!repos.isEmpty()) {
TLongArrayList addedStops = new TLongArrayList();
for (int i = repos.size() - 1; i >= 0; i--) {
TransportIndexRepository r = repos.get(i);
List<TransportStop> repStops = new ArrayList<>();
r.searchTransportStops(topLat, leftLon, bottomLat, rightLon, -1, repStops, matcher);
for (TransportStop s : repStops) {
if (!addedStops.contains(s.getId())) {
addedStops.add(s.getId());
if (!s.isDeleted()) {
stops.add(s);
}
}
}
for (TransportIndexRepository r : repos) {
List<TransportStop> stops = new ArrayList<>();
r.searchTransportStops(topLat, leftLon, bottomLat, rightLon, -1, stops, matcher);
BinaryMapIndexReader reader = ((TransportIndexRepositoryBinary) r).getOpenFile();
TransportRoutingContext.mergeTransportStops(reader, loadedTransportStops, stops, null, null);
}
}
List<TransportStop> stops = new ArrayList<>();
for (TransportStop s : loadedTransportStops.valueCollection()) {
if (!s.isDeleted()) {
stops.add(s);
}
}
return stops;
}
public List<TransportRoute> getRoutesForStop(TransportStop stop) {
List<TransportRoute> routes = new ArrayList<>();
LinkedHashMap<String, TransportIndexRepository> repositories = getTransportRepositories();
LinkedHashMap<String, int[]> referencesToRoutes = stop.getReferencesToRoutesMap();
if (referencesToRoutes != null) {
for (Entry<String, int[]> refs : referencesToRoutes.entrySet()) {
TransportIndexRepository r = repositories.get(refs.getKey());
if (r != null) {
List<TransportRoute> rr = r.getRoutesForReferences(refs.getValue());
routes.addAll(rr);
}
}
}
return routes;
}
////////////////////////////////////////////// Working with map ////////////////////////////////////////////////
public boolean updateRenderedMapNeeded(RotatedTileBox rotatedTileBox, DrawSettings drawSettings) {
return renderer.updateMapIsNeeded(rotatedTileBox, drawSettings);
@ -1031,9 +1085,10 @@ public class ResourceManager {
public BinaryMapIndexReader[] getRoutingMapFiles() {
Collection<BinaryMapReaderResource> fileReaders = getFileReaders();
List<BinaryMapIndexReader> readers = new ArrayList<>(fileReaders.size());
for(BinaryMapReaderResource r : fileReaders.values()) {
if(r.isUseForRouting()) {
for (BinaryMapReaderResource r : fileReaders) {
if (r.isUseForRouting()) {
BinaryMapIndexReader reader = r.getReader(BinaryMapReaderResourceType.ROUTING);
if (reader != null) {
readers.add(reader);
@ -1044,9 +1099,10 @@ public class ResourceManager {
}
public BinaryMapIndexReader[] getTransportRoutingMapFiles() {
Collection<BinaryMapReaderResource> fileReaders = getFileReaders();
List<BinaryMapIndexReader> readers = new ArrayList<>(fileReaders.size());
for(BinaryMapReaderResource r : fileReaders.values()) {
if(r.isUseForPublicTransport()) {
for (BinaryMapReaderResource r : fileReaders) {
if (r.isUseForPublicTransport()) {
BinaryMapIndexReader reader = r.getReader(BinaryMapReaderResourceType.TRANSPORT_ROUTING);
if (reader != null) {
readers.add(reader);
@ -1057,9 +1113,10 @@ public class ResourceManager {
}
public BinaryMapIndexReader[] getQuickSearchFiles() {
Collection<BinaryMapReaderResource> fileReaders = getFileReaders();
List<BinaryMapIndexReader> readers = new ArrayList<>(fileReaders.size());
for(BinaryMapReaderResource r : fileReaders.values()) {
if(r.getShallowReader().containsPoiData() ||
for (BinaryMapReaderResource r : fileReaders) {
if (r.getShallowReader().containsPoiData() ||
r.getShallowReader().containsAddressData()) {
readers.add(r.getReader(BinaryMapReaderResourceType.QUICK_SEARCH));
}

View file

@ -17,7 +17,9 @@ public interface TransportIndexRepository {
public void searchTransportStops(double topLatitude, double leftLongitude, double bottomLatitude, double rightLongitude,
int limit, List<TransportStop> stops, ResultMatcher<TransportStop> matcher);
public List<TransportRoute> getRouteForStop(TransportStop stop);
public List<TransportRoute> getRoutesForStop(TransportStop stop);
public List<TransportRoute> getRoutesForReferences(int[] referencesToRoutes);
public boolean isUseForPublicTransport();
}

View file

@ -58,19 +58,23 @@ public class TransportIndexRepositoryBinary implements TransportIndexRepository
}
@Override
public synchronized List<TransportRoute> getRouteForStop(TransportStop stop){
try {
Collection<TransportRoute> res = getOpenFile().getTransportRoutes(stop.getReferencesToRoutes()).valueCollection();
public synchronized List<TransportRoute> getRoutesForStop(TransportStop stop) {
return getRoutesForReferences(stop.getReferencesToRoutes());
}
if(res != null){
List<TransportRoute> lst = new ArrayList<>(res);
@Override
public List<TransportRoute> getRoutesForReferences(int[] referencesToRoutes) {
try {
Collection<TransportRoute> res = getOpenFile().getTransportRoutes(referencesToRoutes).valueCollection();
if (res != null) {
List<TransportRoute> lst = new ArrayList<>(res);
Collections.sort(lst, new Comparator<TransportRoute>() {
@Override
public int compare(TransportRoute o1, TransportRoute o2) {
int i1 = Algorithms.extractFirstIntegerNumber(o1.getRef());
int i2 = Algorithms.extractFirstIntegerNumber(o2.getRef());
int r = Integer.compare(i1, i2);
if( r == 0){
int r = Algorithms.compare(i1, i2);
if (r == 0) {
r = Algorithms.compare(o1.getName(), o2.getName());
}
return r;
@ -83,7 +87,7 @@ public class TransportIndexRepositoryBinary implements TransportIndexRepository
}
return Collections.emptyList();
}
@Override
public boolean acceptTransportStop(TransportStop stop) {
return resource.getShallowReader().transportStopBelongsTo(stop);

View file

@ -28,6 +28,7 @@ import net.osmand.plus.render.RenderingIcons;
import net.osmand.plus.transport.TransportStopRoute;
import net.osmand.plus.transport.TransportStopType;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
@ -102,27 +103,30 @@ public class TransportStopsLayer extends OsmandMapLayer implements ContextMenuLa
if (latLonBounds == null) {
return new ArrayList<>();
}
List<TransportStop> res = view.getApplication().getResourceManager().searchTransportSync(latLonBounds.top, latLonBounds.left,
latLonBounds.bottom, latLonBounds.right, new ResultMatcher<TransportStop>() {
try {
List<TransportStop> res = view.getApplication().getResourceManager().searchTransportSync(latLonBounds.top, latLonBounds.left,
latLonBounds.bottom, latLonBounds.right, new ResultMatcher<TransportStop>() {
@Override
public boolean publish(TransportStop object) {
return true;
}
@Override
public boolean publish(TransportStop object) {
return true;
}
@Override
public boolean isCancelled() {
return isInterrupted();
}
});
Collections.sort(res, new Comparator<TransportStop>() {
@Override
public int compare(TransportStop lhs, TransportStop rhs) {
return lhs.getId() < rhs.getId() ? -1 : (lhs.getId().longValue() == rhs.getId().longValue() ? 0 : 1);
}
});
return res;
@Override
public boolean isCancelled() {
return isInterrupted();
}
});
Collections.sort(res, new Comparator<TransportStop>() {
@Override
public int compare(TransportStop lhs, TransportStop rhs) {
return lhs.getId() < rhs.getId() ? -1 : (lhs.getId().longValue() == rhs.getId().longValue() ? 0 : 1);
}
});
return res;
} catch (IOException e) {
return new ArrayList<>();
}
}
};
}