Point list to quadtree

This commit is contained in:
MadWasp79 2019-12-12 19:25:21 +02:00
parent 41b4e81fa0
commit 53268f462a
5 changed files with 89 additions and 23 deletions

View file

@ -183,8 +183,13 @@ public class GeocodingUtilities {
}
}
Collections.sort(lst, GeocodingUtilities.DISTANCE_COMPARATOR);
if (lst.size() > 0) {
result.put(lst.get(0).point.getRoad().id, point);
// log.debug(String.format("Road %s", lst.get(0).point.getRoad()));
}
}
log.debug(String.format("Time to load %d, time to findInitialSegment %d, timeToLoadHeaders %d, tiles loaded %d",
ctx.timeToLoad,ctx.timeToFindInitialSegments, ctx.timeToLoadHeaders, ctx.loadedTiles));
return result;
}

View file

@ -92,10 +92,10 @@ public class RoutingContext {
public int memoryOverhead = 0;
long timeNanoToCalcDeviation = 0;
long timeToLoad = 0;
long timeToLoadHeaders = 0;
long timeToFindInitialSegments = 0;
public long timeNanoToCalcDeviation = 0;
public long timeToLoad = 0;
public long timeToLoadHeaders = 0;
public long timeToFindInitialSegments = 0;
public long timeToCalculate = 0;
int distinctLoadedTiles = 0;
@ -289,6 +289,7 @@ public class RoutingContext {
public void loadSubregionTile(final RoutingSubregionTile ts, boolean loadObjectsInMemory, List<RouteDataObject> toLoad, TLongHashSet excludeNotAllowed) {
boolean wasUnloaded = ts.isUnloaded();
int ucount = ts.getUnloadCont();
if (nativeLib == null) {
long now = System.nanoTime();
try {
@ -321,7 +322,7 @@ public class RoutingContext {
} catch (IOException e) {
throw new RuntimeException("Loading data exception", e);
}
// log.debug(String.format("Load subregion: %s", ts.subregion.filePointer));
timeToLoad += (System.nanoTime() - now);
} else {

View file

@ -188,9 +188,9 @@ public class CurrentPositionHelper {
boolean allowEmptyNames,
@Nullable ApplicationMode appMode) {
List<BinaryMapReaderResource> checkReaders = new ArrayList<>();
List<BinaryMapReaderResource> checkReaders = usedReaders;
for (Location loc : points) {
checkReaders.addAll(checkReaders(loc.getLatitude(), loc.getLongitude(), usedReaders));
checkReaders = checkReaders(loc.getLatitude(), loc.getLongitude(), checkReaders);
}
if (appMode == null) {
@ -230,11 +230,11 @@ public class CurrentPositionHelper {
for (BinaryMapReaderResource rep : checkReaders) {
rs[i++] = rep.getReader(BinaryMapReaderResourceType.STREET_LOOKUP);
}
RoutingConfiguration cfg = app.getRoutingConfig().build(p, 10,
RoutingConfiguration cfg = app.getRoutingConfig().build(p, 100,
new HashMap<String, String>());
cfg.routeCalculationTime = System.currentTimeMillis();
ctx = new RoutePlannerFrontEnd().buildRoutingContext(cfg, null, rs);
RoutingConfiguration defCfg = app.getRoutingConfig().build("geocoding", 10,
RoutingConfiguration defCfg = app.getRoutingConfig().build("geocoding", 100,
new HashMap<String, String>());
defCtx = new RoutePlannerFrontEnd().buildRoutingContext(defCfg, null, rs);
} else {

View file

@ -979,8 +979,8 @@ public class MapActivityActions implements DialogProvider {
@Override
public boolean onContextMenuClick(ArrayAdapter<ContextMenuItem> adapter, int itemId, int pos, boolean isChecked, int[] viewCoordinates) {
app.logEvent("drawer_import_avoid_roads");
// app.getAvoidRoadsHelper().testRun();
app.getAvoidRoadsHelper().testRunDownload();
app.getAvoidRoadsHelper().testRun();
// app.getAvoidRoadsHelper().testRunDownload();
return true;
}
}).createItem());

View file

@ -11,6 +11,9 @@ import net.osmand.IndexConstants;
import net.osmand.Location;
import net.osmand.PlatformUtil;
import net.osmand.ResultMatcher;
import net.osmand.binary.RouteDataObject;
import net.osmand.data.QuadRect;
import net.osmand.data.QuadTree;
import net.osmand.osm.io.NetworkUtils;
import net.osmand.plus.ApplicationMode;
import net.osmand.plus.OsmandApplication;
@ -27,6 +30,7 @@ import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
@ -64,6 +68,7 @@ public class AvoidRoadsHelper {
completionCallback = new CompletionCallback() {
@Override
public void onRDOSearchComplete() {
if (saveResultToFile) {
File out = new File (app.getAppPath(IndexConstants.AVOID_ROADS_DIR).getAbsolutePath() + "/"
+ "processed_ids.json");
@ -80,11 +85,15 @@ public class AvoidRoadsHelper {
}
public void testRun() {
File in = new File(app.getAppPath(IndexConstants.AVOID_ROADS_DIR).getAbsolutePath() + "/" + inputFileName);
LOG.debug(String.format("Input json: %s", in.getAbsolutePath()));
if (in.exists()) {
loadJson(in.getAbsolutePath(), FROM_STORAGE);
}
// File in = new File(app.getAppPath(IndexConstants.AVOID_ROADS_DIR).getAbsolutePath() + "/" + inputFileName);
// LOG.debug(String.format("Input json: %s", in.getAbsolutePath()));
// if (in.exists()) {
// loadJson(in.getAbsolutePath(), FROM_STORAGE);
// }
//String url = "https://gist.githubusercontent.com/MadWasp79/1238d8878792572e343eb2e296c3c7f5/raw/494f872425993797c3a3bc79a4ec82039db6ee46/point_100.json";
String url = "https://gist.githubusercontent.com/MadWasp79/45f362ea48e9e8edd1593113593993c5/raw/6e817fb3bc7eaeaa3eda24847fde4855eb22485d/points_500.json";
LOG.debug(String.format("Loading json from url: %s", url));
loadJson(url, FROM_URL);
}
public void testRunDownload() {
@ -94,11 +103,40 @@ public class AvoidRoadsHelper {
}
public void convertPointsToRDO(List<Location> parsedPoints) {
public void convertPointsToRDO(final List<Location> parsedPoints) {
final Map<Long, Location> avoidedRoads = new HashMap<>();
final long timeStart = System.currentTimeMillis();
final int[] count = {0};
this.roadsToAvoid.clear();
// for (final Location point : parsedPoints) {
//
// app.getLocationProvider().getRouteSegment(point, appMode, false, new ResultMatcher<RouteDataObject>() {
// @Override
// public boolean publish(RouteDataObject result) {
// count[0]++;
// if (result == null) {
// LOG.error("Error! Find no result for point []");
// } else {
// avoidedRoads.put(result.id, point);
// }
// if (count[0] == parsedPoints.size()) {
// completionCallback.onRDOSearchComplete();
// }
// if (count[0]%10 == 0) {
// app.showShortToastMessage(String.format("Found %d roads", count[0]));
// }
// return true;
// }
//
// @Override
// public boolean isCancelled() {
// return false;
// }
// });
// }
app.getLocationProvider().getMultipleRouteSegmentsIds(parsedPoints, appMode, false, new ResultMatcher<Map<Long, Location>>() {
app.getLocationProvider().getMultipleRouteSegmentsIds(parsedPoints, appMode, false,
new ResultMatcher<Map<Long, Location>>() {
@Override
public boolean publish(Map<Long, Location> result) {
@ -139,7 +177,7 @@ public class AvoidRoadsHelper {
parsePointsFromJson(is, result);
return result;
} catch (Exception e) {
LOG.error("Error reading json url!");
LOG.error("Error reading json !");
} finally {
if (is != null) {
try {
@ -195,12 +233,34 @@ public class AvoidRoadsHelper {
private void parsePointsFromJson(InputStream is, List<Location> result) {
Gson gson = new Gson();
GeoJSON geoJSON = gson.fromJson(new BufferedReader(new InputStreamReader(is)), GeoJSON.class);
double minlat = 0 , maxlat = 0, minlon = 0, maxlon= 0;
boolean first = true;
for (Point o : geoJSON.points) {
Location ll = new Location("geoJSON");
ll.setLatitude(o.geo.coordinates.get(1));
ll.setLongitude(o.geo.coordinates.get(0));
double lat = o.geo.coordinates.get(1);
double lon = o.geo.coordinates.get(0);
if(first) {
minlat = maxlat = lat;
minlon = maxlon = lon;
first = false;
} else {
minlat = Math.min(minlat, lat);
minlon = Math.min(minlon, lon);
maxlat = Math.max(maxlat, lat);
maxlon = Math.max(maxlon, lon);
}
ll.setLatitude(lat);
ll.setLongitude(lon);
result.add(ll);
}
QuadRect qr = new QuadRect(minlon, minlat, maxlon, maxlat);
QuadTree<Location> qt = new QuadTree<Location>(qr, 8, 0.55f);
for(Location l : result) {
qt.insert(l, (float)l.getLongitude(), (float) l.getLatitude());
}
qt.queryInBox(qr, result);
app.showShortToastMessage(String.format("Loaded and parsed %d avoid points from JSON. Starting segment search.", result.size()));
LOG.debug(String.format("Points parsed: %d", result.size()));
}