public static void assertPList(PointList expected, PointList list) { assertEquals("size of point lists is not equal", expected.getSize(), list.getSize()); for (int i = 0; i < expected.getSize(); i++) { assertEquals(expected.getLatitude(i), list.getLatitude(i), 1e-4); assertEquals(expected.getLongitude(i), list.getLongitude(i), 1e-4); } }
public void add(PointList points) { int newSize = size + points.getSize(); incCap(newSize); for (int i = 0; i < points.getSize(); i++) { int tmp = size + i; latitudes[tmp] = points.getLatitude(i); longitudes[tmp] = points.getLongitude(i); if (is3D) elevations[tmp] = points.getElevation(i); } size = newSize; }
public void suggest(File locationFile, int locationListSize, File outputFile, int hubSize) { // WARNING: this code is VERY DIRTY. // It's JUST good enough to generate the hubs for Belgium once (because we only need to // determine them once). // Further research to generate good hubs is needed. List<AirLocation> locationList = readAirLocationFile(locationFile); if (locationListSize > locationList.size()) { throw new IllegalArgumentException( "The locationListSize (" + locationListSize + ") is larger than the locationList size (" + locationList.size() + ")."); } locationList = subselectLocationList(locationListSize, locationList); Map<Point, Point> fromPointMap = new LinkedHashMap<Point, Point>(locationListSize * 10); Map<Point, Point> toPointMap = new LinkedHashMap<Point, Point>(locationListSize * 10); int rowIndex = 0; double maxAirDistance = 0.0; for (AirLocation fromAirLocation : locationList) { for (AirLocation toAirLocation : locationList) { double airDistance = fromAirLocation.getAirDistanceDouble(toAirLocation); if (airDistance > maxAirDistance) { maxAirDistance = airDistance; } } } double airDistanceThreshold = maxAirDistance / 10.0; for (AirLocation fromAirLocation : locationList) { for (AirLocation toAirLocation : locationList) { double distance; if (fromAirLocation != toAirLocation) { GHRequest request = new GHRequest( fromAirLocation.getLatitude(), fromAirLocation.getLongitude(), toAirLocation.getLatitude(), toAirLocation.getLongitude()) .setVehicle("car"); GHResponse response = graphHopper.route(request); if (response.hasErrors()) { throw new IllegalStateException( "GraphHopper gave " + response.getErrors().size() + " errors. First error chained.", response.getErrors().get(0)); } // Distance should be in km, not meter distance = response.getDistance() / 1000.0; if (distance == 0.0) { throw new IllegalArgumentException( "The fromAirLocation (" + fromAirLocation + ") and toAirLocation (" + toAirLocation + ") are the same."); } PointList ghPointList = response.getPoints(); PointPart previousFromPointPart = null; PointPart previousToPointPart = null; double previousLatitude = Double.NaN; double previousLongitude = Double.NaN; for (int i = 0; i < ghPointList.size(); i++) { double latitude = ghPointList.getLatitude(i); double longitude = ghPointList.getLongitude(i); if (latitude == previousLatitude && longitude == previousLongitude) { continue; } if (calcAirDistance( latitude, longitude, fromAirLocation.getLatitude(), fromAirLocation.getLongitude()) < airDistanceThreshold) { Point fromPoint = new Point(latitude, longitude); Point oldFromPoint = fromPointMap.get(fromPoint); if (oldFromPoint == null) { // Initialize fromPoint instance fromPoint.pointPartMap = new LinkedHashMap<AirLocation, PointPart>(); fromPointMap.put(fromPoint, fromPoint); } else { // Reuse existing fromPoint instance fromPoint = oldFromPoint; } PointPart fromPointPart = fromPoint.pointPartMap.get(fromAirLocation); if (fromPointPart == null) { fromPointPart = new PointPart(fromPoint, fromAirLocation); fromPoint.pointPartMap.put(fromAirLocation, fromPointPart); fromPointPart.previousPart = previousFromPointPart; } fromPointPart.count++; previousFromPointPart = fromPointPart; } if (calcAirDistance( latitude, longitude, toAirLocation.getLatitude(), toAirLocation.getLongitude()) < airDistanceThreshold) { Point toPoint = new Point(latitude, longitude); Point oldToPoint = toPointMap.get(toPoint); if (oldToPoint == null) { // Initialize toPoint instance toPoint.pointPartMap = new LinkedHashMap<AirLocation, PointPart>(); toPointMap.put(toPoint, toPoint); } else { // Reuse existing toPoint instance toPoint = oldToPoint; } // Basically do the same as fromPointPart, but while traversing in the other direction PointPart toPointPart = toPoint.pointPartMap.get(toAirLocation); boolean newToPointPart = false; if (toPointPart == null) { toPointPart = new PointPart(toPoint, toAirLocation); toPoint.pointPartMap.put(toAirLocation, toPointPart); newToPointPart = true; } if (previousToPointPart != null) { previousToPointPart.previousPart = toPointPart; } toPointPart.count++; if (newToPointPart) { previousToPointPart = toPointPart; } else { previousToPointPart = null; } } previousLatitude = latitude; previousLongitude = longitude; } } } logger.debug(" Finished routes for rowIndex {}/{}", rowIndex, locationList.size()); rowIndex++; } Set<Point> hubPointList = new LinkedHashSet<Point>(20); extractFromHubs(new ArrayList<Point>(fromPointMap.values()), hubPointList, (hubSize + 1) / 2); extractFromHubs(new ArrayList<Point>(toPointMap.values()), hubPointList, hubSize / 2); logger.info("Writing hubs..."); BufferedWriter vrpWriter = null; try { vrpWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outputFile), "UTF-8")); vrpWriter.write("HUB_COORD_SECTION\n"); int id = 0; for (Point point : hubPointList) { vrpWriter.write("" + id + " " + point.latitude + " " + point.longitude + " " + id + "\n"); id++; } vrpWriter.write("\n\nGOOGLE MAPS\n"); for (Point point : hubPointList) { vrpWriter.write("" + id + "\t0\t" + point.latitude + "," + point.longitude + "\n"); id++; } } catch (IOException e) { throw new IllegalArgumentException( "Could not read the locationFile (" + locationFile.getName() + ") or write the vrpOutputFile (" + outputFile.getName() + ").", e); } finally { IOUtils.closeQuietly(vrpWriter); } // Throw in google docs spreadsheet and use add-on Mapping Sheets to visualize. }