diff --git a/DataExtractionOSM/.classpath b/DataExtractionOSM/.classpath index 40749a6c55..3e80609632 100644 --- a/DataExtractionOSM/.classpath +++ b/DataExtractionOSM/.classpath @@ -13,5 +13,6 @@ + diff --git a/DataExtractionOSM/OsmExtractionUI.launch b/DataExtractionOSM/OsmExtractionUI.launch deleted file mode 100644 index d40ec671ff..0000000000 --- a/DataExtractionOSM/OsmExtractionUI.launch +++ /dev/null @@ -1,15 +0,0 @@ - - - - - - - - - - - - - - - diff --git a/DataExtractionOSM/lib/junit-4.10.jar b/DataExtractionOSM/lib/junit-4.10.jar new file mode 100644 index 0000000000..bf5c0b9c6a Binary files /dev/null and b/DataExtractionOSM/lib/junit-4.10.jar differ diff --git a/DataExtractionOSM/src/net/osmand/router/BinaryRoutePlanner.java b/DataExtractionOSM/src/net/osmand/router/BinaryRoutePlanner.java index 976102a0b2..a133a3cf3c 100644 --- a/DataExtractionOSM/src/net/osmand/router/BinaryRoutePlanner.java +++ b/DataExtractionOSM/src/net/osmand/router/BinaryRoutePlanner.java @@ -364,6 +364,12 @@ public class BinaryRoutePlanner { if(ctx.isUseDynamicRoadPrioritising() && next != null){ double priority = ctx.getRouter().getFutureRoadPriority(next.road); result /= priority; + int dist = ctx.getDynamicRoadPriorityDistance(); + // only firts 500 m count by dynamic priority + if(distToFinalPoint > dist && dist != 0){ + result = (distToFinalPoint - dist) / ctx.getRouter().getMaxDefaultSpeed() + + dist / (ctx.getRouter().getMaxDefaultSpeed() * priority); + } } return result; } @@ -815,9 +821,9 @@ public class BinaryRoutePlanner { */ private List prepareResult(RoutingContext ctx, RouteSegment start, RouteSegment end, boolean leftside) { List result = new ArrayList(); - + RouteSegment segment = ctx.finalReverseRoute; - int parentSegmentStart = ctx.finalReverseEndSegment; + int parentSegmentStart = ctx.finalReverseEndSegment; while (segment != null) { RouteSegmentResult res = new RouteSegmentResult(segment.road, parentSegmentStart, segment.segmentStart); parentSegmentStart = segment.parentSegmentEnd; @@ -825,7 +831,7 @@ public class BinaryRoutePlanner { result.add(res); } Collections.reverse(result); - + segment = ctx.finalDirectRoute; int parentSegmentEnd = ctx.finalDirectEndSegment; while (segment != null) { @@ -835,8 +841,7 @@ public class BinaryRoutePlanner { result.add(res); } Collections.reverse(result); - - + // calculate time float completeTime = 0; float completeDistance = 0; @@ -864,7 +869,7 @@ public class BinaryRoutePlanner { rr.setSegmentTime((float) distOnRoadToPass); rr.setSegmentSpeed((float) speed); rr.setDistance((float) distance); - + completeTime += distOnRoadToPass; completeDistance += distance; } @@ -879,60 +884,59 @@ public class BinaryRoutePlanner { if (t != null || i == result.size()) { if (toUpdate >= 0) { String turn = result.get(toUpdate).getTurnType().toString(); - if(result.get(toUpdate).getTurnType().getLanes() != null) { + if (result.get(toUpdate).getTurnType().getLanes() != null) { turn += Arrays.toString(result.get(toUpdate).getTurnType().getLanes()); } - result.get(toUpdate).setDescription( turn - + String.format(" and go %.2f meters", dist)); + result.get(toUpdate).setDescription(turn + String.format(" and go %.2f meters", dist)); } toUpdate = i; dist = 0; } - if ( i < result.size()) { + if (i < result.size()) { dist += result.get(i).getDistance(); } } + + println("ROUTE : "); + double startLat = MapUtils.get31LatitudeY(start.road.getPoint31YTile(start.segmentStart)); + double startLon = MapUtils.get31LongitudeX(start.road.getPoint31XTile(start.segmentStart)); + double endLat = MapUtils.get31LatitudeY(end.road.getPoint31YTile(end.segmentStart)); + double endLon = MapUtils.get31LongitudeX(end.road.getPoint31XTile(end.segmentStart)); + StringBuilder add = new StringBuilder(); + add.append("loadedTiles = \"").append(ctx.loadedTiles).append("\" "); + add.append("visitedSegments = \"").append(ctx.visitedSegments).append("\" "); + add.append("complete_distance = \"").append(completeDistance).append("\" "); + println(MessageFormat.format("", startLat + + "", startLon + "", endLat + "", endLon + "", completeTime + "", ctx.config.routerName, add.toString())); if (PRINT_TO_CONSOLE_ROUTE_INFORMATION_TO_TEST) { - println("ROUTE : "); - double startLat = MapUtils.get31LatitudeY(start.road.getPoint31YTile(start.segmentStart)); - double startLon = MapUtils.get31LongitudeX(start.road.getPoint31XTile(start.segmentStart)); - double endLat = MapUtils.get31LatitudeY(end.road.getPoint31YTile(end.segmentStart)); - double endLon = MapUtils.get31LongitudeX(end.road.getPoint31XTile(end.segmentStart)); - StringBuilder add = new StringBuilder(); - add.append("loadedTiles = \"").append(ctx.loadedTiles).append("\" "); - add.append("visitedSegments = \"").append(ctx.visitedSegments).append("\" "); - add.append("complete_distance = \"").append(completeDistance).append("\" "); - println(MessageFormat.format("", - startLat + "", startLon + "", endLat + "", endLon + "", completeTime + "", ctx.config.routerName, add.toString())); for (RouteSegmentResult res : result) { String name = res.getObject().getName(); String ref = res.getObject().getRef(); - if(name == null) { + if (name == null) { name = ""; } if (ref != null) { - name += " (" + ref +") "; + name += " (" + ref + ") "; } StringBuilder additional = new StringBuilder(); additional.append("time = \"").append(res.getSegmentTime()).append("\" "); additional.append("name = \"").append(name).append("\" "); additional.append("distance = \"").append(res.getDistance()).append("\" "); - if(res.getTurnType() != null) { + if (res.getTurnType() != null) { additional.append("turn = \"").append(res.getTurnType()).append("\" "); - if(res.getTurnType().getLanes() != null) { + if (res.getTurnType().getLanes() != null) { additional.append("lanes = \"").append(Arrays.toString(res.getTurnType().getLanes())).append("\" "); } } additional.append("start_bearing = \"").append(res.getBearingBegin()).append("\" "); additional.append("end_bearing = \"").append(res.getBearingEnd()).append("\" "); additional.append("description = \"").append(res.getDescription()).append("\" "); - println(MessageFormat.format( - "\t", - (res.getObject().getId()) + "", res.getStartPointIndex() + "", res.getEndPointIndex() + "", additional.toString())); + println(MessageFormat.format("\t", (res.getObject().getId()) + "", + res.getStartPointIndex() + "", res.getEndPointIndex() + "", additional.toString())); } - println(""); } + println(""); return result; } diff --git a/DataExtractionOSM/src/net/osmand/router/RoutingConfiguration.java b/DataExtractionOSM/src/net/osmand/router/RoutingConfiguration.java index 6e5fdf5dd2..c33e646a54 100644 --- a/DataExtractionOSM/src/net/osmand/router/RoutingConfiguration.java +++ b/DataExtractionOSM/src/net/osmand/router/RoutingConfiguration.java @@ -27,6 +27,7 @@ public class RoutingConfiguration { // 1.2 Dynamic road prioritizing (heuristic) public boolean useDynamicRoadPrioritising = true; + public int dynamicRoadPriorityDistance = 0; // 1.3 Relaxing strategy public boolean useRelaxingStrategy = true; @@ -44,6 +45,7 @@ public class RoutingConfiguration { // 1.6 Used to calculate route in movement public Double initialDirection; + public static class Builder { // Design time storage private String defaultRouter = ""; @@ -67,6 +69,7 @@ public class RoutingConfiguration { i.useDynamicRoadPrioritising = parseSilentBoolean(getAttribute(router, "useDynamicRoadPrioritising"), i.useDynamicRoadPrioritising); i.useRelaxingStrategy = parseSilentBoolean(getAttribute(router, "useRelaxingStrategy"), i.useRelaxingStrategy); + i.dynamicRoadPriorityDistance = parseSilentInt(getAttribute(router, "dynamicRoadPriorityDistance"), i.dynamicRoadPriorityDistance); i.ITERATIONS_TO_RELAX_NODES = parseSilentInt(getAttribute(router, "iterationsToRelaxRoutes"), i.ITERATIONS_TO_RELAX_NODES); i.RELAX_NODES_IF_START_DIST_COEF = parseSilentDouble(getAttribute(router, "relaxNodesIfStartDistSmallCoeff"), i.RELAX_NODES_IF_START_DIST_COEF); i.planRoadDirection = parseSilentInt(getAttribute(router, "planRoadDirection"), i.planRoadDirection); diff --git a/DataExtractionOSM/src/net/osmand/router/RoutingContext.java b/DataExtractionOSM/src/net/osmand/router/RoutingContext.java index 48e9fa6971..0f818d8726 100644 --- a/DataExtractionOSM/src/net/osmand/router/RoutingContext.java +++ b/DataExtractionOSM/src/net/osmand/router/RoutingContext.java @@ -106,6 +106,10 @@ public class RoutingContext { public boolean isUseDynamicRoadPrioritising() { return config.useDynamicRoadPrioritising; } + + public int getDynamicRoadPriorityDistance() { + return config.dynamicRoadPriorityDistance; + } public boolean isUseRelaxingStrategy() { return config.useRelaxingStrategy; diff --git a/DataExtractionOSM/src/net/osmand/router/routing.xml b/DataExtractionOSM/src/net/osmand/router/routing.xml index 4c3728a460..97749d5cf3 100644 --- a/DataExtractionOSM/src/net/osmand/router/routing.xml +++ b/DataExtractionOSM/src/net/osmand/router/routing.xml @@ -10,7 +10,8 @@ - + + diff --git a/DataExtractionOSM/src/net/osmand/router/test/JUnitRouteTest.java b/DataExtractionOSM/src/net/osmand/router/test/JUnitRouteTest.java new file mode 100644 index 0000000000..0ff6b697aa --- /dev/null +++ b/DataExtractionOSM/src/net/osmand/router/test/JUnitRouteTest.java @@ -0,0 +1,56 @@ +package net.osmand.router.test; + +import java.io.File; +import java.io.IOException; +import java.io.RandomAccessFile; +import java.util.ArrayList; +import java.util.List; + +import javax.xml.parsers.ParserConfigurationException; + +import net.osmand.binary.BinaryMapIndexReader; +import net.osmand.router.BinaryRoutePlanner; +import net.osmand.router.RoutingConfiguration; +import net.osmand.swing.DataExtractionSettings; + +import org.junit.Before; +import org.junit.Test; +import org.xml.sax.SAXException; + + +public class JUnitRouteTest { + + static BinaryMapIndexReader[] rs; + @Before + public void setupFiles() throws IOException { + if(rs != null){ + return; + } + BinaryRoutePlanner.PRINT_TO_CONSOLE_ROUTE_INFORMATION_TO_TEST = false; + + List files = new ArrayList(); + for (File f : new File(DataExtractionSettings.getSettings().getBinaryFilesDir()).listFiles()) { + if (f.getName().endsWith(".obf")) { + files.add(f); + } + } + rs = new BinaryMapIndexReader[files.size()]; + int it = 0; + for (File f : files) { + RandomAccessFile raf = new RandomAccessFile(f, "r"); //$NON-NLS-1$ //$NON-NLS-2$ + rs[it++] = new BinaryMapIndexReader(raf, false); + } + } + + + @Test + public void runNL() throws SAXException, IOException, ParserConfigurationException { + RouterTestsSuite.test(getClass().getResourceAsStream("nl.test.xml"), rs, RoutingConfiguration.getDefault()); + } + + @Test + public void runNL2() throws SAXException, IOException, ParserConfigurationException { + RouterTestsSuite.test(getClass().getResourceAsStream("nl2.test.xml"), rs, RoutingConfiguration.getDefault()); + } + +} diff --git a/DataExtractionOSM/src/net/osmand/router/test/RouterTestsSuite.java b/DataExtractionOSM/src/net/osmand/router/test/RouterTestsSuite.java index ebba8b667d..731ac59d90 100644 --- a/DataExtractionOSM/src/net/osmand/router/test/RouterTestsSuite.java +++ b/DataExtractionOSM/src/net/osmand/router/test/RouterTestsSuite.java @@ -14,7 +14,6 @@ import javax.xml.parsers.ParserConfigurationException; import net.osmand.binary.BinaryMapIndexReader; import net.osmand.router.BinaryRoutePlanner; import net.osmand.router.BinaryRoutePlanner.RouteSegment; -import net.osmand.router.RoutingConfiguration.Builder; import net.osmand.router.RouteSegmentResult; import net.osmand.router.RoutingConfiguration; import net.osmand.router.RoutingContext; @@ -27,16 +26,67 @@ import org.xml.sax.InputSource; import org.xml.sax.SAXException; public class RouterTestsSuite { - // TODO add one international test - // POLAND/BELARUS (problems with way connection) The best at 100% -// Start lat=52.115756035004786 lon=23.56539487838745 -// END lat=52.03710226357107 lon=23.47106695175171 -// id=32032589 start=8 end=9 -// id=32032656 start=0 end=18 -// id=32031919 start=1 end=0 + + private static class Parameters { + public File obfDir; + public List tests = new ArrayList(); + public RoutingConfiguration.Builder configBuilder; + + public static Parameters init(String[] args) throws SAXException, IOException { + Parameters p = new Parameters(); + String routingXmlFile = null; + String obfDirectory = null; + BinaryRoutePlanner.PRINT_TO_CONSOLE_ROUTE_INFORMATION_TO_TEST = false; + for (String a : args) { + if (a.startsWith("-routingXmlPath=")) { + routingXmlFile = a.substring("-routingXmlPath=".length()); + } else if (a.startsWith("-verbose")) { + BinaryRoutePlanner.PRINT_TO_CONSOLE_ROUTE_INFORMATION_TO_TEST = true; + } else if (a.startsWith("-obfDir=")) { + obfDirectory = a.substring("-obfDir=".length()); + } else if (a.startsWith("-testDir=")) { + String testDirectory = a.substring("-testDir=".length()); + for(File f : new File(testDirectory).listFiles()) { + if(f.getName().endsWith(".test.xml")){ + p.tests.add(f); + } + } + } else if(!a.startsWith("-")){ + p.tests.add(new File(a)); + } + } + + if (obfDirectory == null) { + obfDirectory = DataExtractionSettings.getSettings().getBinaryFilesDir(); + } + if (obfDirectory != null && obfDirectory.length() > 0) { + p.obfDir = new File(obfDirectory); + } + + if (routingXmlFile == null) { + routingXmlFile = DataExtractionSettings.getSettings().getRoutingXmlPath(); + } + if (routingXmlFile.equals("routing.xml")) { + p.configBuilder = RoutingConfiguration.getDefault(); + } else { + p.configBuilder = RoutingConfiguration.parseFromInputStream(new FileInputStream(routingXmlFile)); + } + + return p; + } + } + public static void main(String[] args) throws IOException, SAXException, ParserConfigurationException { + Parameters params = Parameters.init(args); + if(params.tests.isEmpty() || params.obfDir == null) { + println("Run router tests is console utility to test route calculation for osmand."); + println("\nUsage for run tests : runTestsSuite [-routingXmlPath=PATH] [-verbose] [-obfDir=PATH] [-testDir=PATH] {individualTestPath}"); + return; + } List files = new ArrayList(); - for (File f : new File(DataExtractionSettings.getSettings().getBinaryFilesDir()).listFiles()) { + + + for (File f : params.obfDir.listFiles()) { if (f.getName().endsWith(".obf")) { files.add(f); } @@ -49,8 +99,9 @@ public class RouterTestsSuite { } boolean allSuccess = true; - for(String a : args) { - allSuccess &= test(a, rs); + + for(File f : params.tests) { + allSuccess &= test(new FileInputStream(f), rs, params.configBuilder); } if (allSuccess) { System.out.println("All is successfull"); @@ -59,17 +110,21 @@ public class RouterTestsSuite { } - private static boolean test(String file, BinaryMapIndexReader[] rs) throws SAXException, IOException, ParserConfigurationException { - InputStream resource = new FileInputStream(file); + private static void println(String string) { + System.out.println(string); + } + + + public static boolean test(InputStream resource, BinaryMapIndexReader[] rs, RoutingConfiguration.Builder config) throws SAXException, IOException, ParserConfigurationException { Document testSuite = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new InputSource(resource)); NodeList tests = testSuite.getElementsByTagName("test"); - - for(int i=0; i < tests.getLength(); i++){ + + for (int i = 0; i < tests.getLength(); i++) { Element e = (Element) tests.item(i); BinaryRoutePlanner router = new BinaryRoutePlanner(null, rs); - testRoute(e, router); + testRoute(e, router, config); } - + return true; } @@ -79,14 +134,18 @@ public class RouterTestsSuite { } return Float.parseFloat(e.getAttribute(attr)); } - private static boolean isIn(float expected, float value, float percent){ + private static boolean isInOrLess(float expected, float value, float percent){ if(Math.abs(value/expected - 1) < percent / 100){ return true; } + if(value < expected) { + System.err.println("Test could be adjusted value " + value + " is much less then expected " + expected); + return true; + } return false; } - private static void testRoute(Element testCase, BinaryRoutePlanner planner) throws IOException, SAXException { + private static void testRoute(Element testCase, BinaryRoutePlanner planner, RoutingConfiguration.Builder config) throws IOException, SAXException { String vehicle = testCase.getAttribute("vehicle"); int loadedTiles = (int) parseFloat(testCase, "loadedTiles"); int visitedSegments = (int) parseFloat(testCase, "visitedSegments"); @@ -98,14 +157,7 @@ public class RouterTestsSuite { System.err.println("\n\n!! Skipped test case '" + testDescription + "' because 'best_percent' attribute is not specified \n\n" ); return; } - String xmlPath = DataExtractionSettings.getSettings().getRoutingXmlPath(); - Builder builder; - if(xmlPath.equals("routing.xml")){ - builder = RoutingConfiguration.getDefault() ; - } else{ - builder = RoutingConfiguration.parseFromInputStream(new FileInputStream(xmlPath)); - } - RoutingContext ctx = new RoutingContext(builder.build(vehicle, true)); + RoutingContext ctx = new RoutingContext(config.build(vehicle, true)); String skip = testCase.getAttribute("skip_comment"); if (skip != null && skip.length() > 0) { System.err.println("\n\n!! Skipped test case '" + testDescription + "' because '" + skip + "'\n\n" ); @@ -132,16 +184,16 @@ public class RouterTestsSuite { completeTime += route.get(i).getSegmentTime(); completeDistance += route.get(i).getDistance(); } - if(complete_time > 0 && !isIn(complete_time, completeTime, percent)) { + if(complete_time > 0 && !isInOrLess(complete_time, completeTime, percent)) { throw new IllegalArgumentException(String.format("Complete time (expected) %s != %s (original) : %s", complete_time, completeTime, testDescription)); } - if(complete_distance > 0 && !isIn(complete_distance, completeDistance, percent)) { + if(complete_distance > 0 && !isInOrLess(complete_distance, completeDistance, percent)) { throw new IllegalArgumentException(String.format("Complete distance (expected) %s != %s (original) : %s", complete_distance, completeDistance, testDescription)); } - if(visitedSegments > 0 && !isIn(visitedSegments, ctx.visitedSegments, percent)) { + if(visitedSegments > 0 && !isInOrLess(visitedSegments, ctx.visitedSegments, percent)) { throw new IllegalArgumentException(String.format("Visited segments (expected) %s != %s (original) : %s", visitedSegments, ctx.visitedSegments, testDescription)); } - if(loadedTiles > 0 && !isIn(loadedTiles, ctx.loadedTiles, percent)) { + if(loadedTiles > 0 && !isInOrLess(loadedTiles, ctx.loadedTiles, percent)) { throw new IllegalArgumentException(String.format("Loaded tiles (expected) %s != %s (original) : %s", loadedTiles, ctx.loadedTiles, testDescription)); } @@ -155,7 +207,7 @@ public class RouterTestsSuite { } - private static NodeList compareBySegment(Element testCase, String testDescription, List route) { + protected static NodeList compareBySegment(Element testCase, String testDescription, List route) { NodeList segments = testCase.getElementsByTagName("segment"); int i = 0; while (i < segments.getLength() && i < route.size()) { diff --git a/DataExtractionOSM/src/net/osmand/router/test/nl_test.xml b/DataExtractionOSM/src/net/osmand/router/test/nl.test.xml similarity index 99% rename from DataExtractionOSM/src/net/osmand/router/test/nl_test.xml rename to DataExtractionOSM/src/net/osmand/router/test/nl.test.xml index 9d9ab672e5..05b31af00e 100644 --- a/DataExtractionOSM/src/net/osmand/router/test/nl_test.xml +++ b/DataExtractionOSM/src/net/osmand/router/test/nl.test.xml @@ -1,6 +1,6 @@ - diff --git a/DataExtractionOSM/src/net/osmand/router/test/nl2.test.xml b/DataExtractionOSM/src/net/osmand/router/test/nl2.test.xml new file mode 100644 index 0000000000..2522d5d009 --- /dev/null +++ b/DataExtractionOSM/src/net/osmand/router/test/nl2.test.xml @@ -0,0 +1,243 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file