diff --git a/android/app/src/main/java/com/graphhopper/android/AndroidDownloader.java b/android/app/src/main/java/com/graphhopper/android/AndroidDownloader.java index e5fdcda78f7..eb1b1281479 100644 --- a/android/app/src/main/java/com/graphhopper/android/AndroidDownloader.java +++ b/android/app/src/main/java/com/graphhopper/android/AndroidDownloader.java @@ -2,10 +2,8 @@ import com.graphhopper.util.Downloader; -public class AndroidDownloader extends Downloader -{ - public AndroidDownloader() - { +public class AndroidDownloader extends Downloader { + public AndroidDownloader() { super("GraphHopper Android"); } } diff --git a/android/app/src/main/java/com/graphhopper/android/AndroidHelper.java b/android/app/src/main/java/com/graphhopper/android/AndroidHelper.java index c618ccc2dbf..c4add58ef0a 100644 --- a/android/app/src/main/java/com/graphhopper/android/AndroidHelper.java +++ b/android/app/src/main/java/com/graphhopper/android/AndroidHelper.java @@ -1,37 +1,31 @@ package com.graphhopper.android; +import android.content.Context; +import android.net.ConnectivityManager; +import android.net.NetworkInfo; + import java.io.BufferedReader; import java.io.IOException; import java.io.Reader; import java.util.ArrayList; import java.util.List; -import android.content.Context; -import android.net.ConnectivityManager; -import android.net.NetworkInfo; - -public class AndroidHelper -{ - public static List readFile( Reader simpleReader ) throws IOException - { +public class AndroidHelper { + public static List readFile(Reader simpleReader) throws IOException { BufferedReader reader = new BufferedReader(simpleReader); - try - { + try { List res = new ArrayList(); String line; - while ((line = reader.readLine()) != null) - { + while ((line = reader.readLine()) != null) { res.add(line); } return res; - } finally - { + } finally { reader.close(); } } - public static boolean isFastDownload( Context ctx ) - { + public static boolean isFastDownload(Context ctx) { ConnectivityManager mgrConn = (ConnectivityManager) ctx .getSystemService(Context.CONNECTIVITY_SERVICE); return mgrConn.getActiveNetworkInfo() != null @@ -41,11 +35,9 @@ public static boolean isFastDownload( Context ctx ) // || mgrTel.getNetworkType() == TelephonyManager.NETWORK_TYPE_UMTS) { } - public static String getFileName( String str ) - { + public static String getFileName(String str) { int index = str.lastIndexOf("/"); - if (index > 0) - { + if (index > 0) { return str.substring(index + 1); } return str; diff --git a/android/app/src/main/java/com/graphhopper/android/GHAsyncTask.java b/android/app/src/main/java/com/graphhopper/android/GHAsyncTask.java index e60f0d4663d..8ace22c386f 100644 --- a/android/app/src/main/java/com/graphhopper/android/GHAsyncTask.java +++ b/android/app/src/main/java/com/graphhopper/android/GHAsyncTask.java @@ -2,38 +2,30 @@ import android.os.AsyncTask; -public abstract class GHAsyncTask extends AsyncTask -{ +public abstract class GHAsyncTask extends AsyncTask { private Throwable error; - protected abstract C saveDoInBackground( A... params ) throws Exception; + protected abstract C saveDoInBackground(A... params) throws Exception; - protected C doInBackground( A... params ) - { - try - { + protected C doInBackground(A... params) { + try { return saveDoInBackground(params); - } catch (Throwable t) - { + } catch (Throwable t) { error = t; return null; } } - public boolean hasError() - { + public boolean hasError() { return error != null; } - public Throwable getError() - { + public Throwable getError() { return error; } - public String getErrorMessage() - { - if (hasError()) - { + public String getErrorMessage() { + if (hasError()) { return error.getMessage(); } return "No Error"; diff --git a/android/app/src/main/java/com/graphhopper/android/MainActivity.java b/android/app/src/main/java/com/graphhopper/android/MainActivity.java index a75b4c75881..29b48f29d40 100644 --- a/android/app/src/main/java/com/graphhopper/android/MainActivity.java +++ b/android/app/src/main/java/com/graphhopper/android/MainActivity.java @@ -17,20 +17,12 @@ import android.view.View; import android.view.View.OnClickListener; import android.view.Window; -import android.widget.ArrayAdapter; -import android.widget.Button; -import android.widget.EditText; -import android.widget.Spinner; -import android.widget.TextView; -import android.widget.Toast; -import com.graphhopper.PathWrapper; - +import android.widget.*; import com.graphhopper.GHRequest; import com.graphhopper.GHResponse; import com.graphhopper.GraphHopper; -import com.graphhopper.util.*; +import com.graphhopper.PathWrapper; import com.graphhopper.util.Parameters.*; - import org.mapsforge.core.graphics.Bitmap; import org.mapsforge.core.graphics.Paint; import org.mapsforge.core.graphics.Style; @@ -56,8 +48,8 @@ import java.util.Map; import java.util.TreeMap; -public class MainActivity extends Activity -{ +public class MainActivity extends Activity { + private static final int NEW_MENU_ID = Menu.FIRST + 1; private MapView mapView; private GraphHopper hopper; private LatLong start; @@ -75,43 +67,36 @@ public class MainActivity extends Activity private File mapsFolder; private TileCache tileCache; - protected boolean onMapTap(LatLong tapLatLong) - { + protected boolean onMapTap(LatLong tapLatLong) { if (!isReady()) return false; - if (shortestPathRunning) - { + if (shortestPathRunning) { logUser("Calculation still in progress"); return false; } Layers layers = mapView.getLayerManager().getLayers(); - if (start != null && end == null) - { + if (start != null && end == null) { end = tapLatLong; shortestPathRunning = true; Marker marker = createMarker(tapLatLong, R.drawable.flag_red); - if (marker != null) - { + if (marker != null) { layers.add(marker); } calcPath(start.latitude, start.longitude, end.latitude, end.longitude); - } else - { + } else { start = tapLatLong; end = null; // remove all layers but the first one, which is the map - while (layers.size() > 1) - { + while (layers.size() > 1) { layers.remove(1); } Marker marker = createMarker(start, R.drawable.flag_green); - if (marker != null) - { + if (marker != null) { layers.add(marker); } } @@ -119,8 +104,7 @@ protected boolean onMapTap(LatLong tapLatLong) } @Override - protected void onCreate( Bundle savedInstanceState ) - { + protected void onCreate(Bundle savedInstanceState) { requestWindowFeature(Window.FEATURE_NO_TITLE); super.onCreate(savedInstanceState); setContentView(R.layout.main); @@ -136,10 +120,8 @@ protected void onCreate( Bundle savedInstanceState ) final EditText input = new EditText(this); input.setText(currentArea); boolean greaterOrEqKitkat = Build.VERSION.SDK_INT >= 19; - if (greaterOrEqKitkat) - { - if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) - { + if (greaterOrEqKitkat) { + if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { logUser("GraphHopper is not usable without an external storage!"); return; } @@ -165,8 +147,7 @@ protected void onCreate( Bundle savedInstanceState ) } @Override - protected void onDestroy() - { + protected void onDestroy() { super.onDestroy(); if (hopper != null) hopper.close(); @@ -180,14 +161,12 @@ protected void onDestroy() AndroidGraphicFactory.clearResourceMemoryCache(); } - boolean isReady() - { + boolean isReady() { // only return true if already loaded if (hopper != null) return true; - if (prepareInProgress) - { + if (prepareInProgress) { logUser("Preparation still in progress"); return false; } @@ -195,28 +174,23 @@ boolean isReady() return false; } - private void initFiles( String area ) - { + private void initFiles(String area) { prepareInProgress = true; currentArea = area; downloadingFiles(); } - private void chooseAreaFromLocal() - { + private void chooseAreaFromLocal() { List nameList = new ArrayList<>(); - String[] files = mapsFolder.list(new FilenameFilter() - { + String[] files = mapsFolder.list(new FilenameFilter() { @Override - public boolean accept( File dir, String filename ) - { + public boolean accept(File dir, String filename) { return filename != null && (filename.endsWith(".ghz") || filename .endsWith("-gh")); } }); - for (String file : files) - { + for (String file : files) { nameList.add(file); } @@ -224,30 +198,23 @@ public boolean accept( File dir, String filename ) return; chooseArea(localButton, localSpinner, nameList, - new MySpinnerListener() - { + new MySpinnerListener() { @Override - public void onSelect( String selectedArea, String selectedFile ) - { + public void onSelect(String selectedArea, String selectedFile) { initFiles(selectedArea); } }); } - private void chooseAreaFromRemote() - { - new GHAsyncTask>() - { - protected List saveDoInBackground( Void... params ) - throws Exception - { + private void chooseAreaFromRemote() { + new GHAsyncTask>() { + protected List saveDoInBackground(Void... params) + throws Exception { String[] lines = new AndroidDownloader().downloadAsString(fileListURL, false).split("\n"); List res = new ArrayList<>(); - for (String str : lines) - { + for (String str : lines) { int index = str.indexOf("href=\""); - if (index >= 0) - { + if (index >= 0) { index += 6; int lastIndex = str.indexOf(".ghz", index); if (lastIndex >= 0) @@ -260,32 +227,25 @@ protected List saveDoInBackground( Void... params ) } @Override - protected void onPostExecute( List nameList ) - { - if (hasError()) - { + protected void onPostExecute(List nameList) { + if (hasError()) { getError().printStackTrace(); logUser("Are you connected to the internet? Problem while fetching remote area list: " + getErrorMessage()); return; - } else if (nameList == null || nameList.isEmpty()) - { + } else if (nameList == null || nameList.isEmpty()) { logUser("No maps created for your version!? " + fileListURL); return; } - MySpinnerListener spinnerListener = new MySpinnerListener() - { + MySpinnerListener spinnerListener = new MySpinnerListener() { @Override - public void onSelect( String selectedArea, String selectedFile ) - { + public void onSelect(String selectedArea, String selectedFile) { if (selectedFile == null || new File(mapsFolder, selectedArea + ".ghz").exists() - || new File(mapsFolder, selectedArea + "-gh").exists()) - { + || new File(mapsFolder, selectedArea + "-gh").exists()) { downloadURL = null; - } else - { + } else { downloadURL = selectedFile; } initFiles(selectedArea); @@ -297,12 +257,10 @@ public void onSelect( String selectedArea, String selectedFile ) }.execute(); } - private void chooseArea( Button button, final Spinner spinner, - List nameList, final MySpinnerListener myListener ) - { + private void chooseArea(Button button, final Spinner spinner, + List nameList, final MySpinnerListener myListener) { final Map nameToFullName = new TreeMap<>(); - for (String fullName : nameList) - { + for (String fullName : nameList) { String tmp = Helper.pruneFileEnd(fullName); if (tmp.endsWith("-gh")) tmp = tmp.substring(0, tmp.length() - 3); @@ -315,34 +273,23 @@ private void chooseArea( Button button, final Spinner spinner, ArrayAdapter spinnerArrayAdapter = new ArrayAdapter<>( this, android.R.layout.simple_spinner_dropdown_item, nameList); spinner.setAdapter(spinnerArrayAdapter); - button.setOnClickListener(new OnClickListener() - { + button.setOnClickListener(new OnClickListener() { @Override - public void onClick( View v ) - { + public void onClick(View v) { Object o = spinner.getSelectedItem(); - if (o != null && o.toString().length() > 0 && !nameToFullName.isEmpty()) - { + if (o != null && o.toString().length() > 0 && !nameToFullName.isEmpty()) { String area = o.toString(); myListener.onSelect(area, nameToFullName.get(area)); - } else - { + } else { myListener.onSelect(null, null); } } }); } - public interface MySpinnerListener - { - void onSelect( String selectedArea, String selectedFile ); - } - - void downloadingFiles() - { + void downloadingFiles() { final File areaFolder = new File(mapsFolder, currentArea + "-gh"); - if (downloadURL == null || areaFolder.exists()) - { + if (downloadURL == null || areaFolder.exists()) { loadMap(areaFolder); return; } @@ -354,63 +301,52 @@ void downloadingFiles() dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); dialog.show(); - new GHAsyncTask() - { - protected Object saveDoInBackground( Void... _ignore ) - throws Exception - { + new GHAsyncTask() { + protected Object saveDoInBackground(Void... _ignore) + throws Exception { String localFolder = Helper.pruneFileEnd(AndroidHelper.getFileName(downloadURL)); localFolder = new File(mapsFolder, localFolder + "-gh").getAbsolutePath(); log("downloading & unzipping " + downloadURL + " to " + localFolder); AndroidDownloader downloader = new AndroidDownloader(); downloader.setTimeout(30000); downloader.downloadAndUnzip(downloadURL, localFolder, - new ProgressListener() - { + new ProgressListener() { @Override - public void update( long val ) - { + public void update(long val) { publishProgress((int) val); } }); return null; } - protected void onProgressUpdate( Integer... values ) - { + protected void onProgressUpdate(Integer... values) { super.onProgressUpdate(values); dialog.setProgress(values[0]); } - protected void onPostExecute( Object _ignore ) - { + protected void onPostExecute(Object _ignore) { dialog.dismiss(); - if (hasError()) - { + if (hasError()) { String str = "An error happened while retrieving maps:" + getErrorMessage(); log(str, getError()); logUser(str); - } else - { + } else { loadMap(areaFolder); } } }.execute(); } - void loadMap( File areaFolder ) - { + void loadMap(File areaFolder) { logUser("loading map"); MapDataStore mapDataStore = new MapFile(new File(areaFolder, currentArea + ".map")); mapView.getLayerManager().getLayers().clear(); TileRendererLayer tileRendererLayer = new TileRendererLayer(tileCache, mapDataStore, - mapView.getModel().mapViewPosition, false, true, false, AndroidGraphicFactory.INSTANCE) - { + mapView.getModel().mapViewPosition, false, true, false, AndroidGraphicFactory.INSTANCE) { @Override - public boolean onLongPress(LatLong tapLatLong, Point layerXY, Point tapXY) - { + public boolean onLongPress(LatLong tapLatLong, Point layerXY, Point tapXY) { return onMapTap(tapLatLong); } }; @@ -423,13 +359,10 @@ public boolean onLongPress(LatLong tapLatLong, Point layerXY, Point tapXY) loadGraphStorage(); } - void loadGraphStorage() - { + void loadGraphStorage() { logUser("loading graph (" + Constants.VERSION + ") ... "); - new GHAsyncTask() - { - protected Path saveDoInBackground( Void... v ) throws Exception - { + new GHAsyncTask() { + protected Path saveDoInBackground(Void... v) throws Exception { GraphHopper tmpHopp = new GraphHopper().forMobile(); tmpHopp.load(new File(mapsFolder, currentArea).getAbsolutePath() + "-gh"); log("found graph " + tmpHopp.getGraphHopperStorage().toString() + ", nodes:" + tmpHopp.getGraphHopperStorage().getNodes()); @@ -437,14 +370,11 @@ protected Path saveDoInBackground( Void... v ) throws Exception return null; } - protected void onPostExecute( Path o ) - { - if (hasError()) - { + protected void onPostExecute(Path o) { + if (hasError()) { logUser("An error happened while creating graph:" + getErrorMessage()); - } else - { + } else { logUser("Finished loading graph. Press long to define where to start and end the route."); } @@ -453,27 +383,21 @@ protected void onPostExecute( Path o ) }.execute(); } - private void finishPrepare() - { + private void finishPrepare() { prepareInProgress = false; } - private Polyline createPolyline( PathWrapper response ) - { + private Polyline createPolyline(PathWrapper response) { Paint paintStroke = AndroidGraphicFactory.INSTANCE.createPaint(); paintStroke.setStyle(Style.STROKE); paintStroke.setColor(Color.argb(128, 0, 0xCC, 0x33)); - paintStroke.setDashPathEffect(new float[] - { - 25, 15 - }); + paintStroke.setDashPathEffect(new float[]{25, 15}); paintStroke.setStrokeWidth(8); Polyline line = new Polyline(paintStroke, AndroidGraphicFactory.INSTANCE); List geoPoints = line.getLatLongs(); PointList tmp = response.getPoints(); - for (int i = 0; i < response.getPoints().getSize(); i++) - { + for (int i = 0; i < response.getPoints().getSize(); i++) { geoPoints.add(new LatLong(tmp.getLatitude(i), tmp.getLongitude(i))); } @@ -481,24 +405,20 @@ private Polyline createPolyline( PathWrapper response ) } @SuppressWarnings("deprecation") - private Marker createMarker(LatLong p, int resource ) - { + private Marker createMarker(LatLong p, int resource) { Drawable drawable = getResources().getDrawable(resource); Bitmap bitmap = AndroidGraphicFactory.convertToBitmap(drawable); return new Marker(p, bitmap, 0, -bitmap.getHeight() / 2); } - public void calcPath( final double fromLat, final double fromLon, - final double toLat, final double toLon ) - { + public void calcPath(final double fromLat, final double fromLon, + final double toLat, final double toLon) { log("calculating path ..."); - new AsyncTask() - { + new AsyncTask() { float time; - protected PathWrapper doInBackground( Void... v ) - { + protected PathWrapper doInBackground(Void... v) { StopWatch sw = new StopWatch().start(); GHRequest req = new GHRequest(fromLat, fromLon, toLat, toLon). setAlgorithm(Algorithms.DIJKSTRA_BI); @@ -509,10 +429,8 @@ protected PathWrapper doInBackground( Void... v ) return resp.getBest(); } - protected void onPostExecute( PathWrapper resp ) - { - if (!resp.hasErrors()) - { + protected void onPostExecute(PathWrapper resp) { + if (!resp.hasErrors()) { log("from:" + fromLat + "," + fromLon + " to:" + toLat + "," + toLon + " found path with distance:" + resp.getDistance() / 1000f + ", nodes:" + resp.getPoints().getSize() + ", time:" @@ -522,8 +440,7 @@ protected void onPostExecute( PathWrapper resp ) mapView.getLayerManager().getLayers().add(createPolyline(resp)); //mapView.redraw(); - } else - { + } else { logUser("Error:" + resp.getErrors()); } shortestPathRunning = false; @@ -531,40 +448,31 @@ protected void onPostExecute( PathWrapper resp ) }.execute(); } - private void log( String str ) - { + private void log(String str) { Log.i("GH", str); } - private void log( String str, Throwable t ) - { + private void log(String str, Throwable t) { Log.i("GH", str, t); } - private void logUser( String str ) - { + private void logUser(String str) { log(str); Toast.makeText(this, str, Toast.LENGTH_LONG).show(); } - private static final int NEW_MENU_ID = Menu.FIRST + 1; - @Override - public boolean onCreateOptionsMenu( Menu menu ) - { + public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu); menu.add(0, NEW_MENU_ID, 0, "Google"); // menu.add(0, NEW_MENU_ID + 1, 0, "Other"); return true; } - public boolean onOptionsItemSelected( MenuItem item ) - { - switch (item.getItemId()) - { + public boolean onOptionsItemSelected(MenuItem item) { + switch (item.getItemId()) { case NEW_MENU_ID: - if (start == null || end == null) - { + if (start == null || end == null) { logUser("tap screen to set start and end of route"); break; } @@ -580,4 +488,8 @@ public boolean onOptionsItemSelected( MenuItem item ) } return true; } + + public interface MySpinnerListener { + void onSelect(String selectedArea, String selectedFile); + } } diff --git a/core/nb-configuration.xml b/core/nb-configuration.xml index c95ec725462..ef9a362e2e2 100644 --- a/core/nb-configuration.xml +++ b/core/nb-configuration.xml @@ -20,6 +20,6 @@ Any value defined here will override the pom.xml file value but is only applicable to the current project. --> false - all + all diff --git a/core/src/main/java/com/graphhopper/GHRequest.java b/core/src/main/java/com/graphhopper/GHRequest.java index d572e3ac6cd..783c07a0350 100644 --- a/core/src/main/java/com/graphhopper/GHRequest.java +++ b/core/src/main/java/com/graphhopper/GHRequest.java @@ -33,25 +33,21 @@ * @author Peter Karich * @author ratrun */ -public class GHRequest -{ - private String algo = ""; +public class GHRequest { private final List points; private final HintsMap hints = new HintsMap(); - private boolean possibleToAdd = false; - private Locale locale = Locale.US; - // List of favored start (1st element) and arrival heading (all other). // Headings are north based azimuth (clockwise) in (0, 360) or NaN for equal preference private final List favoredHeadings; + private String algo = ""; + private boolean possibleToAdd = false; + private Locale locale = Locale.US; - public GHRequest() - { + public GHRequest() { this(5); } - public GHRequest( int size ) - { + public GHRequest(int size) { points = new ArrayList(size); favoredHeadings = new ArrayList(size); possibleToAdd = true; @@ -62,17 +58,15 @@ public GHRequest( int size ) * with a preferred start and end heading. Headings are north based azimuth (clockwise) in (0, * 360) or NaN for equal preference. */ - public GHRequest( double fromLat, double fromLon, double toLat, double toLon, - double startHeading, double endHeading ) - { + public GHRequest(double fromLat, double fromLon, double toLat, double toLon, + double startHeading, double endHeading) { this(new GHPoint(fromLat, fromLon), new GHPoint(toLat, toLon), startHeading, endHeading); } /** * Set routing request from specified startPlace (fromLat, fromLon) to endPlace (toLat, toLon) */ - public GHRequest( double fromLat, double fromLon, double toLat, double toLon ) - { + public GHRequest(double fromLat, double fromLon, double toLat, double toLon) { this(new GHPoint(fromLat, fromLon), new GHPoint(toLat, toLon)); } @@ -80,8 +74,7 @@ public GHRequest( double fromLat, double fromLon, double toLat, double toLon ) * Set routing request from specified startPlace to endPlace with a preferred start and end * heading. Headings are north based azimuth (clockwise) in (0, 360) or NaN for equal preference */ - public GHRequest( GHPoint startPlace, GHPoint endPlace, double startHeading, double endHeading ) - { + public GHRequest(GHPoint startPlace, GHPoint endPlace, double startHeading, double endHeading) { if (startPlace == null) throw new IllegalStateException("'from' cannot be null"); @@ -99,27 +92,25 @@ public GHRequest( GHPoint startPlace, GHPoint endPlace, double startHeading, dou favoredHeadings.add(endHeading); } - public GHRequest( GHPoint startPlace, GHPoint endPlace ) - { + public GHRequest(GHPoint startPlace, GHPoint endPlace) { this(startPlace, endPlace, Double.NaN, Double.NaN); } /** * Set routing request *

- * @param points List of stopover points in order: start, 1st stop, 2nd stop, ..., end + * + * @param points List of stopover points in order: start, 1st stop, 2nd stop, ..., end * @param favoredHeadings List of favored headings for starting (start point) and arrival (via - * and end points) Headings are north based azimuth (clockwise) in (0, 360) or NaN for equal - * preference + * and end points) Headings are north based azimuth (clockwise) in (0, 360) or NaN for equal + * preference */ - public GHRequest( List points, List favoredHeadings ) - { + public GHRequest(List points, List favoredHeadings) { if (points.size() != favoredHeadings.size()) throw new IllegalArgumentException("Size of headings (" + favoredHeadings.size() + ") must match size of points (" + points.size() + ")"); - for (Double heading : favoredHeadings) - { + for (Double heading : favoredHeadings) { validateAzimuthValue(heading); } this.points = points; @@ -129,21 +120,21 @@ public GHRequest( List points, List favoredHeadings ) /** * Set routing request *

+ * * @param points List of stopover points in order: start, 1st stop, 2nd stop, ..., end */ - public GHRequest( List points ) - { + public GHRequest(List points) { this(points, Collections.nCopies(points.size(), Double.NaN)); } /** * Add stopover point to routing request. *

- * @param point geographical position (see GHPoint) + * + * @param point geographical position (see GHPoint) * @param favoredHeading north based azimuth (clockwise) in (0, 360) or NaN for equal preference */ - public GHRequest addPoint( GHPoint point, double favoredHeading ) - { + public GHRequest addPoint(GHPoint point, double favoredHeading) { if (point == null) throw new IllegalArgumentException("point cannot be null"); @@ -160,10 +151,10 @@ public GHRequest addPoint( GHPoint point, double favoredHeading ) /** * Add stopover point to routing request. *

+ * * @param point geographical position (see GHPoint) */ - public GHRequest addPoint( GHPoint point ) - { + public GHRequest addPoint(GHPoint point) { addPoint(point, Double.NaN); return this; } @@ -171,113 +162,95 @@ public GHRequest addPoint( GHPoint point ) /** * @return north based azimuth (clockwise) in (0, 360) or NaN for equal preference */ - public double getFavoredHeading( int i ) - { + public double getFavoredHeading(int i) { return favoredHeadings.get(i); } /** * @return if there exist a preferred heading for start/via/end point i */ - public boolean hasFavoredHeading( int i ) - { + public boolean hasFavoredHeading(int i) { if (i >= favoredHeadings.size()) return false; return !Double.isNaN(favoredHeadings.get(i)); } - private void validateAzimuthValue( double heading ) - { + private void validateAzimuthValue(double heading) { // heading must be in (0, 360) oder NaN if (!Double.isNaN(heading) && (Double.compare(heading, 360) > 0 || Double.compare(heading, 0) < 0)) throw new IllegalArgumentException("Heading " + heading + " must be in range (0,360) or NaN"); } - public List getPoints() - { + public List getPoints() { return points; } + public String getAlgorithm() { + return algo; + } + /** * For possible values see AlgorithmOptions.* */ - public GHRequest setAlgorithm( String algo ) - { + public GHRequest setAlgorithm(String algo) { if (algo != null) this.algo = Helper.camelCaseToUnderScore(algo); return this; } - public String getAlgorithm() - { - return algo; - } - - public Locale getLocale() - { + public Locale getLocale() { return locale; } - public GHRequest setLocale( Locale locale ) - { + public GHRequest setLocale(Locale locale) { if (locale != null) this.locale = locale; return this; } - public GHRequest setLocale( String localeStr ) - { + public GHRequest setLocale(String localeStr) { return setLocale(Helper.getLocale(localeStr)); } + public String getWeighting() { + return hints.getWeighting(); + } + /** * By default it supports fastest and shortest. Or specify empty to use default. */ - public GHRequest setWeighting( String w ) - { + public GHRequest setWeighting(String w) { hints.setWeighting(w); return this; } - public String getWeighting() - { - return hints.getWeighting(); + public String getVehicle() { + return hints.getVehicle(); } /** * Specifiy car, bike or foot. Or specify empty to use default. */ - public GHRequest setVehicle( String vehicle ) - { + public GHRequest setVehicle(String vehicle) { hints.setVehicle(vehicle); return this; } - public String getVehicle() - { - return hints.getVehicle(); - } - @Override - public String toString() - { + public String toString() { String res = ""; - for (GHPoint point : points) - { - if (res.isEmpty()) - { + for (GHPoint point : points) { + if (res.isEmpty()) { res = point.toString(); - } else - { + } else { res += "; " + point.toString(); } } return res + "(" + algo + ")"; } - public HintsMap getHints() - { + public HintsMap getHints() { return hints; } } diff --git a/core/src/main/java/com/graphhopper/GHResponse.java b/core/src/main/java/com/graphhopper/GHResponse.java index 0fc94918926..9c69411efd9 100644 --- a/core/src/main/java/com/graphhopper/GHResponse.java +++ b/core/src/main/java/com/graphhopper/GHResponse.java @@ -25,29 +25,26 @@ /** * Wrapper containing path and error output of GraphHopper. *

+ * * @author Peter Karich */ -public class GHResponse -{ - private String debugInfo = ""; +public class GHResponse { private final List errors = new ArrayList(4); private final PMap hintsMap = new PMap(); private final List pathWrappers = new ArrayList(5); + private String debugInfo = ""; - public GHResponse() - { + public GHResponse() { } - public void add( PathWrapper altResponse ) - { + public void add(PathWrapper altResponse) { pathWrappers.add(altResponse); } /** * Returns the best path. */ - public PathWrapper getBest() - { + public PathWrapper getBest() { if (pathWrappers.isEmpty()) throw new RuntimeException("Cannot fetch best response if list is empty"); @@ -57,21 +54,18 @@ public PathWrapper getBest() /** * This method returns the best path as well as all alternatives. */ - public List getAll() - { + public List getAll() { return pathWrappers; } /** * This method returns true if there are alternative paths available besides the best. */ - public boolean hasAlternatives() - { + public boolean hasAlternatives() { return pathWrappers.size() > 1; } - public void addDebugInfo( String debugInfo ) - { + public void addDebugInfo(String debugInfo) { if (debugInfo == null) throw new IllegalStateException("Debug information has to be none null"); @@ -81,11 +75,9 @@ public void addDebugInfo( String debugInfo ) this.debugInfo += debugInfo; } - public String getDebugInfo() - { + public String getDebugInfo() { String str = debugInfo; - for (PathWrapper ar : pathWrappers) - { + for (PathWrapper ar : pathWrappers) { if (!str.isEmpty()) str += "; "; @@ -98,13 +90,11 @@ public String getDebugInfo() * This method returns true if one of the paths has an error or if the response itself is * errornous. */ - public boolean hasErrors() - { + public boolean hasErrors() { if (!errors.isEmpty()) return true; - for (PathWrapper ar : pathWrappers) - { + for (PathWrapper ar : pathWrappers) { if (ar.hasErrors()) return true; } @@ -115,35 +105,29 @@ public boolean hasErrors() /** * This method returns all the explicitely added errors and the errors of all paths. */ - public List getErrors() - { + public List getErrors() { List list = new ArrayList(); list.addAll(errors); - for (PathWrapper ar : pathWrappers) - { + for (PathWrapper ar : pathWrappers) { list.addAll(ar.getErrors()); } return list; } - public GHResponse addErrors( List errors ) - { + public GHResponse addErrors(List errors) { this.errors.addAll(errors); return this; } - public GHResponse addError( Throwable error ) - { + public GHResponse addError(Throwable error) { this.errors.add(error); return this; } @Override - public String toString() - { + public String toString() { String str = ""; - for (PathWrapper a : pathWrappers) - { + for (PathWrapper a : pathWrappers) { str += "; " + a.toString(); } @@ -156,8 +140,7 @@ public String toString() return str; } - public PMap getHints() - { + public PMap getHints() { return hintsMap; } } diff --git a/core/src/main/java/com/graphhopper/GraphHopper.java b/core/src/main/java/com/graphhopper/GraphHopper.java index 54b61837eb6..6f85a26b122 100644 --- a/core/src/main/java/com/graphhopper/GraphHopper.java +++ b/core/src/main/java/com/graphhopper/GraphHopper.java @@ -17,15 +17,6 @@ */ package com.graphhopper; -import com.graphhopper.routing.weighting.GenericWeighting; -import com.graphhopper.routing.weighting.TurnWeighting; -import com.graphhopper.routing.weighting.ShortFastestWeighting; -import com.graphhopper.routing.weighting.Weighting; -import com.graphhopper.routing.weighting.FastestWeighting; -import com.graphhopper.routing.weighting.CurvatureWeighting; -import com.graphhopper.routing.weighting.ShortestWeighting; -import com.graphhopper.routing.weighting.PriorityWeighting; -import com.graphhopper.routing.subnetwork.PrepareRoutingSubnetworks; import com.graphhopper.reader.DataReader; import com.graphhopper.reader.dem.CGIARProvider; import com.graphhopper.reader.dem.ElevationProvider; @@ -33,17 +24,18 @@ import com.graphhopper.routing.*; import com.graphhopper.routing.ch.CHAlgoFactoryDecorator; import com.graphhopper.routing.ch.PrepareContractionHierarchies; +import com.graphhopper.routing.subnetwork.PrepareRoutingSubnetworks; import com.graphhopper.routing.template.AlternativeRoutingTemplate; import com.graphhopper.routing.template.RoundTripRoutingTemplate; import com.graphhopper.routing.template.RoutingTemplate; import com.graphhopper.routing.template.ViaRoutingTemplate; import com.graphhopper.routing.util.*; +import com.graphhopper.routing.weighting.*; import com.graphhopper.storage.*; import com.graphhopper.storage.index.LocationIndex; import com.graphhopper.storage.index.LocationIndexTree; import com.graphhopper.storage.index.QueryResult; import com.graphhopper.util.*; -import static com.graphhopper.util.Parameters.Algorithms.*; import com.graphhopper.util.Parameters.CH; import com.graphhopper.util.Parameters.Routing; import com.graphhopper.util.exceptions.PointOutOfBoundsException; @@ -57,15 +49,24 @@ import java.text.DateFormat; import java.util.*; +import static com.graphhopper.util.Parameters.Algorithms.*; + /** * Easy to use access point to configure import and (offline) routing. * * @author Peter Karich * @see GraphHopperAPI */ -public class GraphHopper implements GraphHopperAPI -{ +public class GraphHopper implements GraphHopperAPI { private final Logger logger = LoggerFactory.getLogger(getClass()); + private final String fileLockName = "gh.lock"; + private final Set algoDecorators = new LinkedHashSet<>(); + // for CH prepare + private final CHAlgoFactoryDecorator chFactoryDecorator = new CHAlgoFactoryDecorator(); + // utils + private final TranslationMap trMap = new TranslationMap().doImport(); + boolean removeZipped = true; + boolean enableInstructions = true; // for graph: private GraphHopperStorage ghStorage; private EncodingManager encodingManager; @@ -73,19 +74,15 @@ public class GraphHopper implements GraphHopperAPI private String ghLocation = ""; private DAType dataAccessType = DAType.RAM_STORE; private boolean sortGraph = false; - boolean removeZipped = true; private boolean elevation = false; private LockFactory lockFactory = new NativeFSLockFactory(); - private final String fileLockName = "gh.lock"; private boolean allowWrites = true; - boolean enableInstructions = true; private String preferredLanguage = ""; private boolean fullyLoaded = false; // for routing private int maxRoundTripRetries = 3; private boolean simplifyResponse = true; private TraversalMode traversalMode = TraversalMode.NODE_BASED; - private final Set algoDecorators = new LinkedHashSet<>(); private int maxVisitedNodes = Integer.MAX_VALUE; // for index private LocationIndex locationIndex; @@ -94,20 +91,15 @@ public class GraphHopper implements GraphHopperAPI // for prepare private int minNetworkSize = 200; private int minOneWayNetworkSize = 0; - // for CH prepare - private final CHAlgoFactoryDecorator chFactoryDecorator = new CHAlgoFactoryDecorator(); // for data reader private String dataReaderFile; private double dataReaderWayPointMaxDistance = 1; private int dataReaderWorkerThreads = -1; private boolean calcPoints = true; - // utils - private final TranslationMap trMap = new TranslationMap().doImport(); private ElevationProvider eleProvider = ElevationProvider.NOOP; private FlagEncoderFactory flagEncoderFactory = FlagEncoderFactory.DEFAULT; - public GraphHopper() - { + public GraphHopper() { chFactoryDecorator.setEnabled(true); algoDecorators.add(chFactoryDecorator); } @@ -115,20 +107,32 @@ public GraphHopper() /** * For testing only */ - protected GraphHopper loadGraph( GraphHopperStorage g ) - { + protected GraphHopper loadGraph(GraphHopperStorage g) { this.ghStorage = g; fullyLoaded = true; initLocationIndex(); return this; } + /** + * @return the first flag encoder of the encoding manager + */ + FlagEncoder getDefaultVehicle() { + if (encodingManager == null) + throw new IllegalStateException("No encoding manager specified or loaded"); + + return encodingManager.fetchEdgeEncoders().get(0); + } + + public EncodingManager getEncodingManager() { + return encodingManager; + } + /** * Specify which vehicles can be read by this GraphHopper instance. An encoding manager defines * how data from every vehicle is written (und read) into edges of the graph. */ - public GraphHopper setEncodingManager( EncodingManager em ) - { + public GraphHopper setEncodingManager(EncodingManager em) { ensureNotLoaded(); this.encodingManager = em; if (em.needsTurnCostsSupport()) @@ -137,24 +141,11 @@ public GraphHopper setEncodingManager( EncodingManager em ) return this; } - /** - * @return the first flag encoder of the encoding manager - */ - FlagEncoder getDefaultVehicle() - { - if (encodingManager == null) - throw new IllegalStateException("No encoding manager specified or loaded"); - - return encodingManager.fetchEdgeEncoders().get(0); - } - - public EncodingManager getEncodingManager() - { - return encodingManager; + public ElevationProvider getElevationProvider() { + return eleProvider; } - public GraphHopper setElevationProvider( ElevationProvider eleProvider ) - { + public GraphHopper setElevationProvider(ElevationProvider eleProvider) { if (eleProvider == null || eleProvider == ElevationProvider.NOOP) setElevation(false); else @@ -163,24 +154,17 @@ public GraphHopper setElevationProvider( ElevationProvider eleProvider ) return this; } - public ElevationProvider getElevationProvider() - { - return eleProvider; - } - /** * Threads for data reading. */ - protected int getWorkerThreads() - { + protected int getWorkerThreads() { return dataReaderWorkerThreads; } /** * Return maximum distance (in meter) to reduce points via douglas peucker while OSM import. */ - protected double getWayPointMaxDistance() - { + protected double getWayPointMaxDistance() { return dataReaderWayPointMaxDistance; } @@ -188,32 +172,28 @@ protected double getWayPointMaxDistance() * This parameter specifies how to reduce points via douglas peucker while OSM import. Higher * value means more details, unit is meter. Default is 1. Disable via 0. */ - public GraphHopper setWayPointMaxDistance( double wayPointMaxDistance ) - { + public GraphHopper setWayPointMaxDistance(double wayPointMaxDistance) { this.dataReaderWayPointMaxDistance = wayPointMaxDistance; return this; } + public TraversalMode getTraversalMode() { + return traversalMode; + } + /** * Sets the default traversal mode used for the algorithms and preparation. */ - public GraphHopper setTraversalMode( TraversalMode traversalMode ) - { + public GraphHopper setTraversalMode(TraversalMode traversalMode) { this.traversalMode = traversalMode; return this; } - public TraversalMode getTraversalMode() - { - return traversalMode; - } - /** * Configures the underlying storage and response to be used on a well equipped server. Result * also optimized for usage in the web module i.e. try reduce network IO. */ - public GraphHopper forServer() - { + public GraphHopper forServer() { setSimplifyResponse(true); return setInMemory(); } @@ -222,8 +202,7 @@ public GraphHopper forServer() * Configures the underlying storage to be used on a Desktop computer or within another Java * application with enough RAM but no network latency. */ - public GraphHopper forDesktop() - { + public GraphHopper forDesktop() { setSimplifyResponse(false); return setInMemory(); } @@ -232,8 +211,7 @@ public GraphHopper forDesktop() * Configures the underlying storage to be used on a less powerful machine like Android or * Raspberry Pi with only few MB of RAM. */ - public GraphHopper forMobile() - { + public GraphHopper forMobile() { setSimplifyResponse(false); return setMemoryMapped(); } @@ -243,15 +221,13 @@ public GraphHopper forMobile() * probably slower query times, which would be e.g. not suitable for Android. The resolution * specifies the tile width (in meter). */ - public GraphHopper setPreciseIndexResolution( int precision ) - { + public GraphHopper setPreciseIndexResolution(int precision) { ensureNotLoaded(); preciseIndexResolution = precision; return this; } - public GraphHopper setMinNetworkSize( int minNetworkSize, int minOneWayNetworkSize ) - { + public GraphHopper setMinNetworkSize(int minNetworkSize, int minOneWayNetworkSize) { this.minNetworkSize = minNetworkSize; this.minOneWayNetworkSize = minOneWayNetworkSize; return this; @@ -260,8 +236,7 @@ public GraphHopper setMinNetworkSize( int minNetworkSize, int minOneWayNetworkSi /** * This method call results in an in-memory graph. */ - public GraphHopper setInMemory() - { + public GraphHopper setInMemory() { ensureNotLoaded(); dataAccessType = DAType.RAM_STORE; return this; @@ -274,8 +249,7 @@ public GraphHopper setInMemory() * * @param storeOnFlush true by default */ - public GraphHopper setStoreOnFlush( boolean storeOnFlush ) - { + public GraphHopper setStoreOnFlush(boolean storeOnFlush) { ensureNotLoaded(); if (storeOnFlush) dataAccessType = DAType.RAM_STORE; @@ -287,8 +261,7 @@ public GraphHopper setStoreOnFlush( boolean storeOnFlush ) /** * Enable memory mapped configuration if not enough memory is available on the target platform. */ - public GraphHopper setMemoryMapped() - { + public GraphHopper setMemoryMapped() { ensureNotLoaded(); dataAccessType = DAType.MMAP; return this; @@ -297,8 +270,7 @@ public GraphHopper setMemoryMapped() /** * Not yet stable enough to offer it for everyone */ - private GraphHopper setUnsafeMemory() - { + private GraphHopper setUnsafeMemory() { ensureNotLoaded(); dataAccessType = DAType.UNSAFE_STORE; return this; @@ -312,8 +284,7 @@ private GraphHopper setUnsafeMemory() * {@link GraphHopper#setCHWeightings(String...)} */ @Deprecated - public GraphHopper setCHWeighting( String weightingName ) - { + public GraphHopper setCHWeighting(String weightingName) { return this.setCHWeightings(weightingName); } @@ -323,36 +294,40 @@ public GraphHopper setCHWeighting( String weightingName ) * @deprecated Use getCHFactoryDecorator().setWeightingsAsStrings() instead. Will be removed in * 0.8. */ - public GraphHopper setCHWeightings( String... weightingNames ) - { + public GraphHopper setCHWeightings(String... weightingNames) { return this.setCHWeightings(Arrays.asList(weightingNames)); } + /** + * Returns all CHWeighting names + * + * @deprecated Use getCHFactoryDecorator().getWeightingsAsStrings() instead. Will be removed in + * 0.8. + */ + public List getCHWeightings() { + return chFactoryDecorator.getWeightingsAsStrings(); + } + /** * Enables the use of contraction hierarchies to reduce query times. Enabled by default. * * @param weightingList A list containing multiple weightings like: "fastest", "shortest" or - * your own weight-calculation type. - * + * your own weight-calculation type. * @deprecated Use getCHFactoryDecorator().setWeightingsAsStrings() instead. Will be removed in * 0.8. */ - public GraphHopper setCHWeightings( List weightingList ) - { + public GraphHopper setCHWeightings(List weightingList) { ensureNotLoaded(); chFactoryDecorator.setWeightingsAsStrings(weightingList); return this; } /** - * Returns all CHWeighting names - * - * @deprecated Use getCHFactoryDecorator().getWeightingsAsStrings() instead. Will be removed in + * @deprecated Use getCHFactoryDecorator().getCHPrepareThreads() instead. Will be removed in * 0.8. */ - public List getCHWeightings() - { - return chFactoryDecorator.getWeightingsAsStrings(); + public int getCHPrepareThreads() { + return chFactoryDecorator.getPreparationThreads(); } /** @@ -362,90 +337,74 @@ public List getCHWeightings() * @deprecated Use getCHFactoryDecorator().setCHPrepareThreads() instead. Will be removed in * 0.8. */ - public GraphHopper setCHPrepareThreads( int prepareThreads ) - { + public GraphHopper setCHPrepareThreads(int prepareThreads) { chFactoryDecorator.setPreparationThreads(prepareThreads); return this; } /** - * @deprecated Use getCHFactoryDecorator().getCHPrepareThreads() instead. Will be removed in - * 0.8. - */ - public int getCHPrepareThreads() - { - return chFactoryDecorator.getPreparationThreads(); - } - - /** - * * @deprecated Use setEnabled() instead. Will be removed in 0.8. */ - public GraphHopper setCHEnable( boolean enable ) - { + public GraphHopper setCHEnable(boolean enable) { return setCHEnabled(enable); } + public final boolean isCHEnabled() { + return chFactoryDecorator.isEnabled(); + } + /** * Enables or disables contraction hierarchies (CH). This speed-up mode is enabled by default. */ - public GraphHopper setCHEnabled( boolean enable ) - { + public GraphHopper setCHEnabled(boolean enable) { ensureNotLoaded(); chFactoryDecorator.setEnabled(enable); return this; } - public final boolean isCHEnabled() - { - return chFactoryDecorator.isEnabled(); + public int getMaxVisitedNodes() { + return maxVisitedNodes; } /** * This methods stops the algorithm from searching further if the resulting path would go over * the specified node count, important if none-CH routing is used. */ - public void setMaxVisitedNodes( int maxVisitedNodes ) - { + public void setMaxVisitedNodes(int maxVisitedNodes) { this.maxVisitedNodes = maxVisitedNodes; } - public int getMaxVisitedNodes() - { - return maxVisitedNodes; - } - /** * @return true if storing and fetching elevation data is enabled. Default is false */ - public boolean hasElevation() - { + public boolean hasElevation() { return elevation; } /** * Enable storing and fetching elevation data. Default is false */ - public GraphHopper setElevation( boolean includeElevation ) - { + public GraphHopper setElevation(boolean includeElevation) { this.elevation = includeElevation; return this; } + public boolean isEnableInstructions() { + return enableInstructions; + } + /** * This method specifies if the import should include way names to be able to return * instructions for a route. */ - public GraphHopper setEnableInstructions( boolean b ) - { + public GraphHopper setEnableInstructions(boolean b) { ensureNotLoaded(); enableInstructions = b; return this; } - public boolean isEnableInstructions() - { - return enableInstructions; + public String getPreferredLanguage() { + return preferredLanguage; } /** @@ -459,8 +418,7 @@ public boolean isEnableInstructions() * to default language. * */ - public GraphHopper setPreferredLanguage( String preferredLanguage ) - { + public GraphHopper setPreferredLanguage(String preferredLanguage) { ensureNotLoaded(); if (preferredLanguage == null) throw new IllegalArgumentException("preferred language cannot be null"); @@ -469,16 +427,10 @@ public GraphHopper setPreferredLanguage( String preferredLanguage ) return this; } - public String getPreferredLanguage() - { - return preferredLanguage; - } - /** * This methods enables gps point calculation. If disabled only distance will be calculated. */ - public GraphHopper setEnableCalcPoints( boolean b ) - { + public GraphHopper setEnableCalcPoints(boolean b) { calcPoints = b; return this; } @@ -487,17 +439,19 @@ public GraphHopper setEnableCalcPoints( boolean b ) * This method specifies if the returned path should be simplified or not, via douglas-peucker * or similar algorithm. */ - private GraphHopper setSimplifyResponse( boolean doSimplify ) - { + private GraphHopper setSimplifyResponse(boolean doSimplify) { this.simplifyResponse = doSimplify; return this; } + public String getGraphHopperLocation() { + return ghLocation; + } + /** * Sets the graphhopper folder. */ - public GraphHopper setGraphHopperLocation( String ghLocation ) - { + public GraphHopper setGraphHopperLocation(String ghLocation) { ensureNotLoaded(); if (ghLocation == null) throw new IllegalArgumentException("graphhopper location cannot be null"); @@ -506,17 +460,15 @@ public GraphHopper setGraphHopperLocation( String ghLocation ) return this; } - public String getGraphHopperLocation() - { - return ghLocation; + public String getDataReaderFile() { + return dataReaderFile; } /** * This file can be any file type supported by the DataReader. E.g. for the OSMReader it is the * OSM xml (.osm), a compressed xml (.osm.zip or .osm.gz) or a protobuf file (.pbf) */ - public GraphHopper setDataReaderFile( String dataReaderFileStr ) - { + public GraphHopper setDataReaderFile(String dataReaderFileStr) { ensureNotLoaded(); if (Helper.isEmpty(dataReaderFileStr)) throw new IllegalArgumentException("Data reader file cannot be empty."); @@ -525,80 +477,66 @@ public GraphHopper setDataReaderFile( String dataReaderFileStr ) return this; } - public String getDataReaderFile() - { - return dataReaderFile; - } - /** * The underlying graph used in algorithms. * * @throws IllegalStateException if graph is not instantiated. */ - public GraphHopperStorage getGraphHopperStorage() - { + public GraphHopperStorage getGraphHopperStorage() { if (ghStorage == null) throw new IllegalStateException("GraphHopper storage not initialized"); return ghStorage; } - public void setGraphHopperStorage( GraphHopperStorage ghStorage ) - { + public void setGraphHopperStorage(GraphHopperStorage ghStorage) { this.ghStorage = ghStorage; fullyLoaded = true; } - protected void setLocationIndex( LocationIndex locationIndex ) - { - this.locationIndex = locationIndex; - } - /** * The location index created from the graph. * * @throws IllegalStateException if index is not initialized */ - public LocationIndex getLocationIndex() - { + public LocationIndex getLocationIndex() { if (locationIndex == null) throw new IllegalStateException("Location index not initialized"); return locationIndex; } + protected void setLocationIndex(LocationIndex locationIndex) { + this.locationIndex = locationIndex; + } + /** * Sorts the graph which requires more RAM while import. See #12 */ - public GraphHopper setSortGraph( boolean sortGraph ) - { + public GraphHopper setSortGraph(boolean sortGraph) { ensureNotLoaded(); this.sortGraph = sortGraph; return this; } + public boolean isAllowWrites() { + return allowWrites; + } + /** * Specifies if it is allowed for GraphHopper to write. E.g. for read only filesystems it is not * possible to create a lock file and so we can avoid write locks. */ - public GraphHopper setAllowWrites( boolean allowWrites ) - { + public GraphHopper setAllowWrites(boolean allowWrites) { this.allowWrites = allowWrites; return this; } - public boolean isAllowWrites() - { - return allowWrites; - } - - public TranslationMap getTranslationMap() - { + public TranslationMap getTranslationMap() { return trMap; } - public GraphHopper setFlagEncoderFactory( FlagEncoderFactory factory ) - { + public GraphHopper setFlagEncoderFactory(FlagEncoderFactory factory) { this.flagEncoderFactory = factory; return this; } @@ -608,8 +546,7 @@ public GraphHopper setFlagEncoderFactory( FlagEncoderFactory factory ) * args) ala CmdArgs.read(args) or via configuration file ala * CmdArgs.readFromConfig("config.properties", "graphhopper.config") */ - public GraphHopper init( CmdArgs args ) - { + public GraphHopper init(CmdArgs args) { args = CmdArgs.readFromConfigAndMerge(args, "config", "graphhopper.config"); if (args.has("osmreader.osm")) throw new IllegalArgumentException("Instead osmreader.osm use datareader.file, for other changes see core/files/changelog.txt"); @@ -619,8 +556,7 @@ public GraphHopper init( CmdArgs args ) dataReaderFile = tmpOsmFile; String graphHopperFolder = args.get("graph.location", ""); - if (Helper.isEmpty(graphHopperFolder) && Helper.isEmpty(ghLocation)) - { + if (Helper.isEmpty(graphHopperFolder) && Helper.isEmpty(ghLocation)) { if (Helper.isEmpty(dataReaderFile)) throw new IllegalArgumentException("You need to specify an OSM file."); @@ -664,11 +600,9 @@ public GraphHopper init( CmdArgs args ) DAType elevationDAType = DAType.fromString(args.get("graph.elevation.dataaccess", "MMAP")); ElevationProvider tmpProvider = ElevationProvider.NOOP; - if (eleProviderStr.equalsIgnoreCase("srtm")) - { + if (eleProviderStr.equalsIgnoreCase("srtm")) { tmpProvider = new SRTMProvider(); - } else if (eleProviderStr.equalsIgnoreCase("cgiar")) - { + } else if (eleProviderStr.equalsIgnoreCase("cgiar")) { CGIARProvider cgiarProvider = new CGIARProvider(); cgiarProvider.setAutoRemoveTemporaryFiles(args.getBool("graph.elevation.cgiar.clear", true)); tmpProvider = cgiarProvider; @@ -706,8 +640,7 @@ public GraphHopper init( CmdArgs args ) return this; } - private void printInfo() - { + private void printInfo() { logger.info("version " + Constants.VERSION + "|" + Constants.BUILD_DATE + " (" + Constants.getVersions() + ")"); if (ghStorage != null) logger.info("graph " + ghStorage.toString() + ", details:" + ghStorage.toDetailsString()); @@ -718,14 +651,11 @@ private void printInfo() * graph will be stored to disc so on a second call this method will only load the graph from * disc which is usually a lot faster. */ - public GraphHopper importOrLoad() - { - if (!load(ghLocation)) - { + public GraphHopper importOrLoad() { + if (!load(ghLocation)) { printInfo(); process(ghLocation); - } else - { + } else { printInfo(); } return this; @@ -734,44 +664,37 @@ public GraphHopper importOrLoad() /** * Creates the graph from OSM data. */ - private GraphHopper process( String graphHopperLocation ) - { + private GraphHopper process(String graphHopperLocation) { setGraphHopperLocation(graphHopperLocation); Lock lock = null; - try - { - if (ghStorage.getDirectory().getDefaultType().isStoring()) - { + try { + if (ghStorage.getDirectory().getDefaultType().isStoring()) { lockFactory.setLockDir(new File(graphHopperLocation)); lock = lockFactory.create(fileLockName, true); if (!lock.tryLock()) throw new RuntimeException("To avoid multiple writers we need to obtain a write lock but it failed. In " + graphHopperLocation, lock.getObtainFailedReason()); } - try - { + try { DataReader reader = importData(); DateFormat f = Helper.createFormatter(); ghStorage.getProperties().put("datareader.import.date", f.format(new Date())); if (reader.getDataDate() != null) ghStorage.getProperties().put("datareader.data.date", f.format(reader.getDataDate())); - } catch (IOException ex) - { + } catch (IOException ex) { throw new RuntimeException("Cannot read file " + getDataReaderFile(), ex); } cleanUp(); postProcessing(); flush(); - } finally - { + } finally { if (lock != null) lock.release(); } return this; } - protected DataReader importData() throws IOException - { + protected DataReader importData() throws IOException { ensureWriteAccess(); if (ghStorage == null) throw new IllegalStateException("Load graph before importing OSM data"); @@ -788,14 +711,12 @@ protected DataReader importData() throws IOException return reader; } - protected DataReader createReader( GraphHopperStorage ghStorage ) - { + protected DataReader createReader(GraphHopperStorage ghStorage) { throw new UnsupportedOperationException("Cannot create DataReader. Solutions: avoid import via calling load directly, " + "provide a DataReader or use e.g. GraphHopperOSM or a different subclass"); } - protected DataReader initDataReader( DataReader reader ) - { + protected DataReader initDataReader(DataReader reader) { if (dataReaderFile == null) throw new IllegalArgumentException("No file for DataReader specified"); @@ -811,37 +732,29 @@ protected DataReader initDataReader( DataReader reader ) * Opens existing graph. * * @param graphHopperFolder is the folder containing graphhopper files. Can be a compressed file - * too ala folder-content.ghz. + * too ala folder-content.ghz. */ @Override - public boolean load( String graphHopperFolder ) - { + public boolean load(String graphHopperFolder) { if (Helper.isEmpty(graphHopperFolder)) throw new IllegalStateException("GraphHopperLocation is not specified. Call setGraphHopperLocation or init before"); if (fullyLoaded) throw new IllegalStateException("graph is already successfully loaded"); - if (graphHopperFolder.endsWith("-gh")) - { + if (graphHopperFolder.endsWith("-gh")) { // do nothing - } else if (graphHopperFolder.endsWith(".osm") || graphHopperFolder.endsWith(".xml")) - { + } else if (graphHopperFolder.endsWith(".osm") || graphHopperFolder.endsWith(".xml")) { throw new IllegalArgumentException("GraphHopperLocation cannot be the OSM file. Instead you need to use importOrLoad"); - } else if (!graphHopperFolder.contains(".")) - { + } else if (!graphHopperFolder.contains(".")) { if (new File(graphHopperFolder + "-gh").exists()) graphHopperFolder += "-gh"; - } else - { + } else { File compressed = new File(graphHopperFolder + ".ghz"); - if (compressed.exists() && !compressed.isDirectory()) - { - try - { + if (compressed.exists() && !compressed.isDirectory()) { + try { new Unzipper().unzip(compressed.getAbsolutePath(), graphHopperFolder, removeZipped); - } catch (IOException ex) - { + } catch (IOException ex) { throw new RuntimeException("Couldn't extract file " + compressed.getAbsolutePath() + " to " + graphHopperFolder, ex); } @@ -860,12 +773,10 @@ public boolean load( String graphHopperFolder ) GraphExtension ext = encodingManager.needsTurnCostsSupport() ? new TurnCostExtension() : new GraphExtension.NoOpExtension(); - if (chFactoryDecorator.isEnabled()) - { + if (chFactoryDecorator.isEnabled()) { initCHAlgoFactoryDecorator(); ghStorage = new GraphHopperStorage(chFactoryDecorator.getWeightings(), dir, encodingManager, hasElevation(), ext); - } else - { + } else { ghStorage = new GraphHopperStorage(dir, encodingManager, hasElevation(), ext); } @@ -875,12 +786,10 @@ public boolean load( String graphHopperFolder ) return false; Lock lock = null; - try - { + try { // create locks only if writes are allowed, if they are not allowed a lock cannot be created // (e.g. on a read only filesystem locks would fail) - if (ghStorage.getDirectory().getDefaultType().isStoring() && isAllowWrites()) - { + if (ghStorage.getDirectory().getDefaultType().isStoring() && isAllowWrites()) { lockFactory.setLockDir(new File(ghLocation)); lock = lockFactory.create(fileLockName, false); if (!lock.tryLock()) @@ -893,18 +802,15 @@ public boolean load( String graphHopperFolder ) postProcessing(); fullyLoaded = true; return true; - } finally - { + } finally { if (lock != null) lock.release(); } } - public RoutingAlgorithmFactory getAlgorithmFactory( HintsMap map ) - { + public RoutingAlgorithmFactory getAlgorithmFactory(HintsMap map) { RoutingAlgorithmFactory routingAlgorithmFactory = new RoutingAlgorithmFactorySimple(); - for (RoutingAlgorithmFactoryDecorator decorator : algoDecorators) - { + for (RoutingAlgorithmFactoryDecorator decorator : algoDecorators) { if (decorator.isEnabled()) routingAlgorithmFactory = decorator.getDecoratedAlgorithmFactory(routingAlgorithmFactory, map); } @@ -912,26 +818,21 @@ public RoutingAlgorithmFactory getAlgorithmFactory( HintsMap map ) return routingAlgorithmFactory; } - public GraphHopper addAlgorithmFactoryDecorator( RoutingAlgorithmFactoryDecorator algoFactoryDecorator ) - { + public GraphHopper addAlgorithmFactoryDecorator(RoutingAlgorithmFactoryDecorator algoFactoryDecorator) { if (!algoDecorators.add(algoFactoryDecorator)) throw new IllegalArgumentException("Decorator was already added " + algoFactoryDecorator.getClass()); return this; } - public final CHAlgoFactoryDecorator getCHFactoryDecorator() - { + public final CHAlgoFactoryDecorator getCHFactoryDecorator() { return chFactoryDecorator; } - private void initCHAlgoFactoryDecorator() - { + private void initCHAlgoFactoryDecorator() { if (!chFactoryDecorator.hasWeightings()) - for (FlagEncoder encoder : encodingManager.fetchEdgeEncoders()) - { - for (String chWeightingStr : chFactoryDecorator.getWeightingsAsStrings()) - { + for (FlagEncoder encoder : encodingManager.fetchEdgeEncoders()) { + for (String chWeightingStr : chFactoryDecorator.getWeightingsAsStrings()) { Weighting weighting = createWeighting(new HintsMap(chWeightingStr), encoder); chFactoryDecorator.addWeighting(weighting); } @@ -943,20 +844,17 @@ private void initCHAlgoFactoryDecorator() * * @deprecated use getCHFactoryDecorator().createPreparations() instead. Will be removed in 0.8. */ - protected void createCHPreparations() - { + protected void createCHPreparations() { chFactoryDecorator.createPreparations(ghStorage, traversalMode); } /** * Does the preparation and creates the location index */ - public void postProcessing() - { + public void postProcessing() { // Later: move this into the GraphStorage.optimize method // Or: Doing it after preparation to optimize shortcuts too. But not possible yet #12 - if (sortGraph) - { + if (sortGraph) { if (ghStorage.isCHPossible() && isPrepared()) throw new IllegalArgumentException("Sorting a prepared CHGraph is not possible yet. See #12"); @@ -974,8 +872,7 @@ public void postProcessing() prepare(); } - private boolean isPrepared() - { + private boolean isPrepared() { return "true".equals(ghStorage.getProperties().get("prepare.done")); } @@ -985,35 +882,29 @@ private boolean isPrepared() * you use the GraphHopper Web module. * * @param weightingMap all parameters influencing the weighting. E.g. parameters coming via - * GHRequest.getHints or directly via "&api.xy=" from the URL of the web UI - * @param encoder the required vehicle + * GHRequest.getHints or directly via "&api.xy=" from the URL of the web UI + * @param encoder the required vehicle * @return the weighting to be used for route calculation * @see HintsMap */ - public Weighting createWeighting( HintsMap weightingMap, FlagEncoder encoder ) - { + public Weighting createWeighting(HintsMap weightingMap, FlagEncoder encoder) { String weighting = weightingMap.getWeighting().toLowerCase(); - if (encoder.supports(GenericWeighting.class)) - { + if (encoder.supports(GenericWeighting.class)) { DataFlagEncoder dataEncoder = (DataFlagEncoder) encoder; return new GenericWeighting(dataEncoder, dataEncoder.readStringMap(weightingMap)); - } else if ("shortest".equalsIgnoreCase(weighting)) - { + } else if ("shortest".equalsIgnoreCase(weighting)) { return new ShortestWeighting(encoder); - } else if ("fastest".equalsIgnoreCase(weighting) || weighting.isEmpty()) - { + } else if ("fastest".equalsIgnoreCase(weighting) || weighting.isEmpty()) { if (encoder.supports(PriorityWeighting.class)) return new PriorityWeighting(encoder, weightingMap); else return new FastestWeighting(encoder, weightingMap); - } else if ("curvature".equalsIgnoreCase(weighting)) - { + } else if ("curvature".equalsIgnoreCase(weighting)) { if (encoder.supports(CurvatureWeighting.class)) return new CurvatureWeighting(encoder, weightingMap); - } else if ("short_fastest".equalsIgnoreCase(weighting)) - { + } else if ("short_fastest".equalsIgnoreCase(weighting)) { return new ShortFastestWeighting(encoder, weightingMap); } @@ -1023,23 +914,20 @@ public Weighting createWeighting( HintsMap weightingMap, FlagEncoder encoder ) /** * Potentially wraps the specified weighting into a TurnWeighting instance. */ - public Weighting createTurnWeighting( Graph graph, FlagEncoder encoder, Weighting weighting, TraversalMode tMode ) - { + public Weighting createTurnWeighting(Graph graph, FlagEncoder encoder, Weighting weighting, TraversalMode tMode) { if (encoder.supports(TurnWeighting.class) && !tMode.equals(TraversalMode.NODE_BASED)) return new TurnWeighting(weighting, encoder, (TurnCostExtension) graph.getExtension()); return weighting; } @Override - public GHResponse route( GHRequest request ) - { + public GHResponse route(GHRequest request) { GHResponse response = new GHResponse(); calcPaths(request, response); return response; } - public List calcPaths( GHRequest request, GHResponse ghRsp ) - { + public List calcPaths(GHRequest request, GHResponse ghRsp) { if (ghStorage == null || !fullyLoaded) throw new IllegalStateException("Do a successful call to load or importOrLoad before routing"); @@ -1048,14 +936,12 @@ public List calcPaths( GHRequest request, GHResponse ghRsp ) // default handling String vehicle = request.getVehicle(); - if (vehicle.isEmpty()) - { + if (vehicle.isEmpty()) { vehicle = getDefaultVehicle().toString(); request.setVehicle(vehicle); } - try - { + try { if (!encodingManager.supports(vehicle)) throw new IllegalArgumentException("Vehicle " + vehicle + " unsupported. " + "Supported are: " + getEncodingManager()); @@ -1087,8 +973,7 @@ else if (ALT_ROUTE.equalsIgnoreCase(algoStr)) int maxRetries = routingTemplate.getMaxRetries(); Locale locale = request.getLocale(); Translation tr = trMap.getWithFallBack(locale); - for (int i = 0; i < maxRetries; i++) - { + for (int i = 0; i < maxRetries; i++) { StopWatch sw = new StopWatch().start(); qResults = routingTemplate.lookup(points, encoder); ghRsp.addDebugInfo("idLookup:" + sw.stop().getSeconds() + "s"); @@ -1103,8 +988,7 @@ else if (ALT_ROUTE.equalsIgnoreCase(algoStr)) if (!chFactoryDecorator.isDisablingAllowed() && forceFlexibleMode) throw new IllegalArgumentException("Flexible mode not enabled on the server-side"); - if (chFactoryDecorator.isEnabled() && !forceFlexibleMode) - { + if (chFactoryDecorator.isEnabled() && !forceFlexibleMode) { boolean forceCHHeading = hints.getBool(CH.FORCE_HEADING, false); if (!forceCHHeading && request.hasFavoredHeading(0)) throw new IllegalArgumentException("Heading is not (fully) supported for CHGraph. See issue #483"); @@ -1115,8 +999,7 @@ else if (!(tmpAlgoFactory instanceof PrepareContractionHierarchies)) weighting = ((PrepareContractionHierarchies) tmpAlgoFactory).getWeighting(); routingGraph = ghStorage.getGraph(CHGraph.class, weighting); - } else - { + } else { weighting = createWeighting(hints, encoder); ghRsp.addDebugInfo("tmode:" + tMode.toString()); } @@ -1153,33 +1036,27 @@ else if (!(tmpAlgoFactory instanceof PrepareContractionHierarchies)) return altPaths; - } catch (IllegalArgumentException ex) - { + } catch (IllegalArgumentException ex) { ghRsp.addError(ex); return Collections.emptyList(); } } - private void checkIfPointsAreInBounds( List points ) - { + private void checkIfPointsAreInBounds(List points) { BBox bounds = getGraphHopperStorage().getBounds(); - for (int i = 0; i < points.size(); i++) - { + for (int i = 0; i < points.size(); i++) { GHPoint point = points.get(i); - if (!bounds.contains(point.getLat(), point.getLon())) - { + if (!bounds.contains(point.getLat(), point.getLon())) { throw new PointOutOfBoundsException("Point " + i + " is ouf of bounds: " + point, i); } } } - protected LocationIndex createLocationIndex( Directory dir ) - { + protected LocationIndex createLocationIndex(Directory dir) { LocationIndexTree tmpIndex = new LocationIndexTree(ghStorage, dir); tmpIndex.setResolution(preciseIndexResolution); tmpIndex.setMaxRegionSearch(maxRegionSearch); - if (!tmpIndex.loadExisting()) - { + if (!tmpIndex.loadExisting()) { ensureWriteAccess(); tmpIndex.prepareIndex(); } @@ -1190,19 +1067,16 @@ protected LocationIndex createLocationIndex( Directory dir ) /** * Initializes the location index after the import is done. */ - protected void initLocationIndex() - { + protected void initLocationIndex() { if (locationIndex != null) throw new IllegalStateException("Cannot initialize locationIndex twice!"); locationIndex = createLocationIndex(ghStorage.getDirectory()); } - protected void prepare() - { + protected void prepare() { boolean tmpPrepare = chFactoryDecorator.isEnabled(); - if (tmpPrepare) - { + if (tmpPrepare) { ensureWriteAccess(); if (chFactoryDecorator.getPreparationThreads() > 1 && dataAccessType.isMMap() && !dataAccessType.isSynched()) @@ -1217,8 +1091,7 @@ protected void prepare() /** * Internal method to clean up the graph. */ - protected void cleanUp() - { + protected void cleanUp() { int prevNodeCount = ghStorage.getNodes(); PrepareRoutingSubnetworks preparation = new PrepareRoutingSubnetworks(ghStorage, encodingManager.fetchEdgeEncoders()); preparation.setMinNetworkSize(minNetworkSize); @@ -1232,8 +1105,7 @@ protected void cleanUp() + " less nodes"); } - protected void flush() - { + protected void flush() { logger.info("flushing graph " + ghStorage.toString() + ", details:" + ghStorage.toDetailsString() + ", " + Helper.getMemInfo() + ")"); ghStorage.flush(); @@ -1245,19 +1117,16 @@ protected void flush() * Releases all associated resources like memory or files. But it does not remove them. To * remove the files created in graphhopperLocation you have to call clean(). */ - public void close() - { + public void close() { if (ghStorage != null) ghStorage.close(); if (locationIndex != null) locationIndex.close(); - try - { + try { lockFactory.forceRemove(fileLockName, true); - } catch (Exception ex) - { + } catch (Exception ex) { // silently fail e.g. on Windows where we cannot remove an unreleased native lock } } @@ -1266,8 +1135,7 @@ public void close() * Removes the on-disc routing files. Call only after calling close or before importOrLoad or * load */ - public void clean() - { + public void clean() { if (getGraphHopperLocation().isEmpty()) throw new IllegalStateException("Cannot clean GraphHopper without specified graphHopperLocation"); @@ -1275,14 +1143,12 @@ public void clean() Helper.removeDir(folder); } - protected void ensureNotLoaded() - { + protected void ensureNotLoaded() { if (fullyLoaded) throw new IllegalStateException("No configuration changes are possible after loading the graph"); } - protected void ensureWriteAccess() - { + protected void ensureWriteAccess() { if (!allowWrites) throw new IllegalStateException("Writes are not allowed!"); } diff --git a/core/src/main/java/com/graphhopper/GraphHopperAPI.java b/core/src/main/java/com/graphhopper/GraphHopperAPI.java index 512517631b1..8576fb65c22 100644 --- a/core/src/main/java/com/graphhopper/GraphHopperAPI.java +++ b/core/src/main/java/com/graphhopper/GraphHopperAPI.java @@ -20,22 +20,24 @@ /** * Wrapper of the graphhopper online or offline API. Provides read only access. *

+ * * @author Peter Karich */ -public interface GraphHopperAPI -{ +public interface GraphHopperAPI { /** * Connects to the specified service (graphhopper URL) or loads a graph from the graphhopper * folder. *

+ * * @return true if successfully connected or loaded */ - boolean load( String urlOrFile ); + boolean load(String urlOrFile); /** * Calculates the path from specified request visiting the specified locations. *

+ * * @return the response with the route and possible errors */ - GHResponse route( GHRequest request ); + GHResponse route(GHRequest request); } diff --git a/core/src/main/java/com/graphhopper/PathWrapper.java b/core/src/main/java/com/graphhopper/PathWrapper.java index 395615a42ba..bb4d9034d23 100644 --- a/core/src/main/java/com/graphhopper/PathWrapper.java +++ b/core/src/main/java/com/graphhopper/PathWrapper.java @@ -20,6 +20,7 @@ import com.graphhopper.util.InstructionList; import com.graphhopper.util.PointList; import com.graphhopper.util.shapes.BBox; + import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -27,10 +28,11 @@ /** * This class holds the data like points and instructions of a Path. *

+ * * @author Peter Karich */ -public class PathWrapper -{ +public class PathWrapper { + private final List errors = new ArrayList(4); private List description; private double distance; private double ascend; @@ -41,27 +43,23 @@ public class PathWrapper private InstructionList instructions; private PointList waypointList = PointList.EMPTY; private PointList pointList = PointList.EMPTY; - private final List errors = new ArrayList(4); /** * @return the description of this route alternative to make it meaningful for the user e.g. it * displays one or two main roads of the route. */ - public List getDescription() - { + public List getDescription() { if (description == null) return Collections.emptyList(); return description; } - public PathWrapper setDescription( List names ) - { + public PathWrapper setDescription(List names) { this.description = names; return this; } - public PathWrapper addDebugInfo( String debugInfo ) - { + public PathWrapper addDebugInfo(String debugInfo) { if (debugInfo == null) throw new IllegalStateException("Debug information has to be none null"); @@ -72,125 +70,109 @@ public PathWrapper addDebugInfo( String debugInfo ) return this; } - public String getDebugInfo() - { + public String getDebugInfo() { return debugInfo; } - public PathWrapper setPoints( PointList points ) - { - if (pointList != PointList.EMPTY) - throw new IllegalStateException("Cannot call setPoint twice"); - - pointList = points; - return this; - } - /** * This method returns all points on the path. Keep in mind that calculating the distance from * these points might yield different results compared to getDistance as points could have been * simplified on import or after querying. */ - public PointList getPoints() - { + public PointList getPoints() { check("getPoints"); return pointList; } - /** - * This method initializes this path with the snapped input points. - */ - public void setWaypoints( PointList wpList ) - { - if (waypointList != PointList.EMPTY) - throw new IllegalStateException("Cannot call setWaypoints twice"); + public PathWrapper setPoints(PointList points) { + if (pointList != PointList.EMPTY) + throw new IllegalStateException("Cannot call setPoint twice"); - this.waypointList = wpList; + pointList = points; + return this; } /** * This method returns the input points snapped to the road network. */ - public PointList getWaypoints() - { + public PointList getWaypoints() { check("getWaypoints"); return waypointList; } - public PathWrapper setDistance( double distance ) - { - this.distance = distance; - return this; + /** + * This method initializes this path with the snapped input points. + */ + public void setWaypoints(PointList wpList) { + if (waypointList != PointList.EMPTY) + throw new IllegalStateException("Cannot call setWaypoints twice"); + + this.waypointList = wpList; } /** * This method returns the distance of the path. Always prefer this method over * getPoints().calcDistance *

+ * * @return distance in meter */ - public double getDistance() - { + public double getDistance() { check("getDistance"); return distance; } - public PathWrapper setAscend( double ascend ) - { - if (ascend < 0) - throw new IllegalArgumentException("ascend has to be strictly positive"); - - this.ascend = ascend; + public PathWrapper setDistance(double distance) { + this.distance = distance; return this; } /** * This method returns the total elevation change (going upwards) in meter. *

+ * * @return ascend in meter */ - public double getAscend() - { + public double getAscend() { return ascend; } - public PathWrapper setDescend( double descend ) - { - if (descend < 0) - throw new IllegalArgumentException("descend has to be strictly positive"); + public PathWrapper setAscend(double ascend) { + if (ascend < 0) + throw new IllegalArgumentException("ascend has to be strictly positive"); - this.descend = descend; + this.ascend = ascend; return this; } /** * This method returns the total elevation change (going downwards) in meter. *

+ * * @return decline in meter */ - public double getDescend() - { + public double getDescend() { return descend; } - public PathWrapper setTime( long timeInMillis ) - { - this.time = timeInMillis; + public PathWrapper setDescend(double descend) { + if (descend < 0) + throw new IllegalArgumentException("descend has to be strictly positive"); + + this.descend = descend; return this; } /** * @return time in millis */ - public long getTime() - { + public long getTime() { check("getTimes"); return time; } - public PathWrapper setRouteWeight( double weight ) - { - this.routeWeight = weight; + public PathWrapper setTime(long timeInMillis) { + this.time = timeInMillis; return this; } @@ -199,33 +181,33 @@ public PathWrapper setRouteWeight( double weight ) * only if you know what you are doing, e.g. only to compare routes gained with the same query * parameters like vehicle. */ - public double getRouteWeight() - { + public double getRouteWeight() { check("getRouteWeight"); return routeWeight; } + public PathWrapper setRouteWeight(double weight) { + this.routeWeight = weight; + return this; + } + /** * Calculates the bounding box of this route response */ - public BBox calcRouteBBox( BBox _fallback ) - { + public BBox calcRouteBBox(BBox _fallback) { check("calcRouteBBox"); BBox bounds = BBox.createInverse(_fallback.hasElevation()); int len = pointList.getSize(); if (len == 0) return _fallback; - for (int i = 0; i < len; i++) - { + for (int i = 0; i < len; i++) { double lat = pointList.getLatitude(i); double lon = pointList.getLongitude(i); - if (bounds.hasElevation()) - { + if (bounds.hasElevation()) { double ele = pointList.getEle(i); bounds.update(lat, lon, ele); - } else - { + } else { bounds.update(lat, lon); } } @@ -233,8 +215,7 @@ public BBox calcRouteBBox( BBox _fallback ) } @Override - public String toString() - { + public String toString() { String str = "nodes:" + pointList.getSize() + "; " + pointList.toString(); if (instructions != null && !instructions.isEmpty()) str += ", " + instructions.toString(); @@ -245,13 +226,7 @@ public String toString() return str; } - public void setInstructions( InstructionList instructions ) - { - this.instructions = instructions; - } - - public InstructionList getInstructions() - { + public InstructionList getInstructions() { check("getInstructions"); if (instructions == null) throw new IllegalArgumentException("To access instructions you need to enable creation before routing"); @@ -259,10 +234,12 @@ public InstructionList getInstructions() return instructions; } - private void check( String method ) - { - if (hasErrors()) - { + public void setInstructions(InstructionList instructions) { + this.instructions = instructions; + } + + private void check(String method) { + if (hasErrors()) { throw new RuntimeException("You cannot call " + method + " if response contains errors. Check this with ghResponse.hasErrors(). " + "Errors are: " + getErrors()); } @@ -271,24 +248,20 @@ private void check( String method ) /** * @return true if this alternative response contains one or more errors */ - public boolean hasErrors() - { + public boolean hasErrors() { return !errors.isEmpty(); } - public List getErrors() - { + public List getErrors() { return errors; } - public PathWrapper addError( Throwable error ) - { + public PathWrapper addError(Throwable error) { errors.add(error); return this; } - public PathWrapper addErrors( List errors ) - { + public PathWrapper addErrors(List errors) { this.errors.addAll(errors); return this; } diff --git a/core/src/main/java/com/graphhopper/coll/BinHeapWrapper.java b/core/src/main/java/com/graphhopper/coll/BinHeapWrapper.java index c406be5e2d2..3db143816ff 100644 --- a/core/src/main/java/com/graphhopper/coll/BinHeapWrapper.java +++ b/core/src/main/java/com/graphhopper/coll/BinHeapWrapper.java @@ -20,12 +20,11 @@ /** * @author Peter Karich */ -public interface BinHeapWrapper -{ +public interface BinHeapWrapper { - void update( K key, E element ); + void update(K key, E element); - void insert( K key, E element ); + void insert(K key, E element); boolean isEmpty(); @@ -40,5 +39,5 @@ public interface BinHeapWrapper // not necessary? V pollValue(); void clear(); - void ensureCapacity( int size ); + void ensureCapacity(int size); } diff --git a/core/src/main/java/com/graphhopper/coll/CompressedArray.java b/core/src/main/java/com/graphhopper/coll/CompressedArray.java index db273f9a155..9e9443f79d9 100644 --- a/core/src/main/java/com/graphhopper/coll/CompressedArray.java +++ b/core/src/main/java/com/graphhopper/coll/CompressedArray.java @@ -33,10 +33,10 @@ * Stores the entries in compressed segments. The methods de(compress) are taken from lucene * CompressionTools. Before accessing the stored values be sure you called flush. *

+ * * @author Peter Karich */ -public class CompressedArray -{ +public class CompressedArray { private int compressionLevel = 5; private VLongStorage currentWriter; private int currentEntry = 0; @@ -45,20 +45,17 @@ public class CompressedArray private int approxBytesPerEntry; private SpatialKeyAlgo algo; - public CompressedArray() - { + public CompressedArray() { this(100, 200, 4); } /** - * @param _segments initialize with this number of segments - * @param entriesPerSeg a static number which sets the entries per segment + * @param _segments initialize with this number of segments + * @param entriesPerSeg a static number which sets the entries per segment * @param approxBytesPerEntry an *approximative* number (as entries can have different lengths) */ - public CompressedArray( int _segments, int entriesPerSeg, int approxBytesPerEntry ) - { - if (entriesPerSeg < 1) - { + public CompressedArray(int _segments, int entriesPerSeg, int approxBytesPerEntry) { + if (entriesPerSeg < 1) { throw new IllegalArgumentException("at least one entry should be per segment"); } this.entriesPerSegment = entriesPerSeg; @@ -67,16 +64,60 @@ public CompressedArray( int _segments, int entriesPerSeg, int approxBytesPerEntr algo = new SpatialKeyAlgo(63); } - public CompressedArray setCompressionLevel( int compressionLevel ) - { + /** + * Compresses the specified byte range using the specified compressionLevel (constants are + * defined in java.util.zip.Deflater). + */ + public static byte[] compress(byte[] value, int offset, int length, int compressionLevel) { + /* Create an expandable byte array to hold the compressed data. + * You cannot use an array that's the same size as the orginal because + * there is no guarantee that the compressed data will be smaller than + * the uncompressed data. */ + ByteArrayOutputStream bos = new ByteArrayOutputStream(length); + Deflater compressor = new Deflater(); + try { + compressor.setLevel(compressionLevel); + compressor.setInput(value, offset, length); + compressor.finish(); + final byte[] buf = new byte[1024]; + while (!compressor.finished()) { + int count = compressor.deflate(buf); + bos.write(buf, 0, count); + } + } finally { + compressor.end(); + } + return bos.toByteArray(); + } + + /** + * Decompress the byte array previously returned by compress + */ + public static byte[] decompress(byte[] value) throws DataFormatException { + // Create an expandable byte array to hold the decompressed data + ByteArrayOutputStream bos = new ByteArrayOutputStream(value.length); + Inflater decompressor = new Inflater(); + try { + decompressor.setInput(value); + final byte[] buf = new byte[1024]; + while (!decompressor.finished()) { + int count = decompressor.inflate(buf); + bos.write(buf, 0, count); + } + } finally { + decompressor.end(); + } + + return bos.toByteArray(); + } + + public CompressedArray setCompressionLevel(int compressionLevel) { this.compressionLevel = compressionLevel; return this; } - public void write( double lat, double lon ) - { - try - { + public void write(double lat, double lon) { + try { if (currentWriter == null) currentWriter = new VLongStorage(entriesPerSegment * approxBytesPerEntry); @@ -85,131 +126,61 @@ public void write( double lat, double lon ) // but compression of vlong is much more efficient than directly storing the integers currentWriter.writeVLong(latlon); currentEntry++; - if (currentEntry >= entriesPerSegment) - { + if (currentEntry >= entriesPerSegment) { flush(); } - } catch (Exception ex) - { + } catch (Exception ex) { throw new RuntimeException(ex); } } - public GHPoint get( long index ) - { + public GHPoint get(long index) { int segmentNo = (int) (index / entriesPerSegment); int entry = (int) (index % entriesPerSegment); - try - { - if (segmentNo >= segments.size()) - { + try { + if (segmentNo >= segments.size()) { return null; } byte[] bytes = segments.get(segmentNo); VLongStorage store = new VLongStorage(decompress(bytes)); long len = store.getLength(); - for (int i = 0; store.getPosition() < len; i++) - { + for (int i = 0; store.getPosition() < len; i++) { long latlon = store.readVLong(); - if (i == entry) - { + if (i == entry) { GHPoint point = new GHPoint(); algo.decode(latlon, point); return point; } } return null; - } catch (ArrayIndexOutOfBoundsException ex) - { + } catch (ArrayIndexOutOfBoundsException ex) { throw new RuntimeException("index " + index + "=> segNo:" + segmentNo + ", entry=" + entry + ", segments:" + segments.size(), ex); - } catch (Exception ex) - { + } catch (Exception ex) { throw new RuntimeException(ex); } } - public void flush() - { - if (currentWriter == null) - { + public void flush() { + if (currentWriter == null) { return; } - try - { + try { currentWriter.trimToSize(); byte[] input = currentWriter.getBytes(); segments.add(compress(input, 0, input.length, compressionLevel)); currentWriter = null; currentEntry = 0; - } catch (Exception ex) - { + } catch (Exception ex) { throw new RuntimeException(ex); } } - public float calcMemInMB() - { + public float calcMemInMB() { long bytes = 0; - for (int i = 0; i < segments.size(); i++) - { + for (int i = 0; i < segments.size(); i++) { bytes += segments.get(i).length; } return (float) (segments.size() * 4 + bytes) / Helper.MB; } - - /** - * Compresses the specified byte range using the specified compressionLevel (constants are - * defined in java.util.zip.Deflater). - */ - public static byte[] compress( byte[] value, int offset, int length, int compressionLevel ) - { - /* Create an expandable byte array to hold the compressed data. - * You cannot use an array that's the same size as the orginal because - * there is no guarantee that the compressed data will be smaller than - * the uncompressed data. */ - ByteArrayOutputStream bos = new ByteArrayOutputStream(length); - Deflater compressor = new Deflater(); - try - { - compressor.setLevel(compressionLevel); - compressor.setInput(value, offset, length); - compressor.finish(); - final byte[] buf = new byte[1024]; - while (!compressor.finished()) - { - int count = compressor.deflate(buf); - bos.write(buf, 0, count); - } - } finally - { - compressor.end(); - } - return bos.toByteArray(); - } - - /** - * Decompress the byte array previously returned by compress - */ - public static byte[] decompress( byte[] value ) throws DataFormatException - { - // Create an expandable byte array to hold the decompressed data - ByteArrayOutputStream bos = new ByteArrayOutputStream(value.length); - Inflater decompressor = new Inflater(); - try - { - decompressor.setInput(value); - final byte[] buf = new byte[1024]; - while (!decompressor.finished()) - { - int count = decompressor.inflate(buf); - bos.write(buf, 0, count); - } - } finally - { - decompressor.end(); - } - - return bos.toByteArray(); - } } diff --git a/core/src/main/java/com/graphhopper/coll/GHBitSet.java b/core/src/main/java/com/graphhopper/coll/GHBitSet.java index e18356fcbdc..74f8e1ad00c 100644 --- a/core/src/main/java/com/graphhopper/coll/GHBitSet.java +++ b/core/src/main/java/com/graphhopper/coll/GHBitSet.java @@ -21,15 +21,15 @@ * Wrapper interface of an integer container for different implementations like OpenBitset, BitSet, * ... *

+ * * @author Peter Karich */ -public interface GHBitSet -{ - boolean contains( int index ); +public interface GHBitSet { + boolean contains(int index); - void add( int index ); + void add(int index); - void remove( int index ); + void remove(int index); int getCardinality(); @@ -38,12 +38,13 @@ public interface GHBitSet /** * Searches for a greater or equal entry and returns it. *

+ * * @return -1 if nothing found */ - int next( int index ); + int next(int index); /** * @return the specified MyBitSet bs */ - GHBitSet copyTo( GHBitSet bs ); + GHBitSet copyTo(GHBitSet bs); } diff --git a/core/src/main/java/com/graphhopper/coll/GHBitSetImpl.java b/core/src/main/java/com/graphhopper/coll/GHBitSetImpl.java index d2f0bfda667..26a83fd969b 100644 --- a/core/src/main/java/com/graphhopper/coll/GHBitSetImpl.java +++ b/core/src/main/java/com/graphhopper/coll/GHBitSetImpl.java @@ -22,64 +22,51 @@ /** * @author Peter Karich */ -public class GHBitSetImpl extends BitSet implements GHBitSet -{ - public GHBitSetImpl() - { +public class GHBitSetImpl extends BitSet implements GHBitSet { + public GHBitSetImpl() { } - public GHBitSetImpl( int nbits ) - { + public GHBitSetImpl(int nbits) { super(nbits); } @Override - public final boolean contains( int index ) - { + public final boolean contains(int index) { return super.get(index); } @Override - public final void add( int index ) - { + public final void add(int index) { super.set(index); } @Override - public final int getCardinality() - { + public final int getCardinality() { return super.cardinality(); } @Override - public final int next( int index ) - { + public final int next(int index) { return super.nextSetBit(index); } - public final int nextClear( int index ) - { + public final int nextClear(int index) { return super.nextClearBit(index); } @Override - public void remove( int index ) - { + public void remove(int index) { super.clear(index); } @Override - public final GHBitSet copyTo( GHBitSet bs ) - { + public final GHBitSet copyTo(GHBitSet bs) { bs.clear(); - if (bs instanceof GHBitSetImpl) - { + if (bs instanceof GHBitSetImpl) { ((GHBitSetImpl) bs).or(this); - } else - { + } else { for (int index = super.nextSetBit(0); index >= 0; - index = super.nextSetBit(index + 1)) - { + index = super.nextSetBit(index + 1)) { bs.add(index); } } diff --git a/core/src/main/java/com/graphhopper/coll/GHLongIntBTree.java b/core/src/main/java/com/graphhopper/coll/GHLongIntBTree.java index 583ef85befc..5021ea4e2cf 100644 --- a/core/src/main/java/com/graphhopper/coll/GHLongIntBTree.java +++ b/core/src/main/java/com/graphhopper/coll/GHLongIntBTree.java @@ -18,22 +18,21 @@ package com.graphhopper.coll; import com.graphhopper.util.Helper; - -import java.util.Arrays; - import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.Arrays; + /** * An in-memory simple B-Tree. Later we'll use DataAccess to allow on-disc storage for very large * data sets. Delete not supported. *

+ * * @author Peter Karich */ -public class GHLongIntBTree implements LongIntMap -{ - private Logger logger = LoggerFactory.getLogger(getClass()); +public class GHLongIntBTree implements LongIntMap { private final int noNumberValue = -1; + private Logger logger = LoggerFactory.getLogger(getClass()); private long size; private int maxLeafEntries; private int initLeafSize; @@ -42,51 +41,67 @@ public class GHLongIntBTree implements LongIntMap private int height; private BTreeEntry root; - public GHLongIntBTree( int maxLeafEntries ) - { + public GHLongIntBTree(int maxLeafEntries) { this.maxLeafEntries = maxLeafEntries; - if (maxLeafEntries < 1) - { + if (maxLeafEntries < 1) { throw new IllegalArgumentException("illegal maxLeafEntries:" + maxLeafEntries); } - if (maxLeafEntries % 2 == 0) - { + if (maxLeafEntries % 2 == 0) { maxLeafEntries++; } splitIndex = maxLeafEntries / 2; - if (maxLeafEntries < 10) - { + if (maxLeafEntries < 10) { factor = 2; initLeafSize = 1; - } else if (maxLeafEntries < 20) - { + } else if (maxLeafEntries < 20) { factor = 2; initLeafSize = 4; - } else - { + } else { factor = 1.7f; initLeafSize = maxLeafEntries / 10; } clear(); } + // LATER: see OSMIDMap for a version where we use DataAccess + static int binarySearch(long keys[], int start, int len, long key) { + int high = start + len, low = start - 1, guess; + while (high - low > 1) { + // use >>> for average or we could get an integer overflow. + guess = (high + low) >>> 1; + long guessedKey = keys[guess]; + if (guessedKey < key) { + low = guess; + } else { + high = guess; + } + } + + if (high == start + len) { + return ~(start + len); + } + + long highKey = keys[high]; + if (highKey == key) { + return high; + } else { + return ~high; + } + } + @Override - public int put( long key, int value ) - { - if (key == noNumberValue) - { + public int put(long key, int value) { + if (key == noNumberValue) { throw new IllegalArgumentException("Illegal key " + key); } ReturnValue rv = root.put(key, value); - if (rv.tree != null) - { + if (rv.tree != null) { height++; root = rv.tree; } - if (rv.oldValue == noNumberValue) - { + if (rv.oldValue == noNumberValue) { // successfully inserted size++; if (size % 1000000 == 0) @@ -96,19 +111,16 @@ public int put( long key, int value ) } @Override - public int get( long key ) - { + public int get(long key) { return root.get(key); } - int height() - { + int height() { return height; } @Override - public long getSize() - { + public long getSize() { return size; } @@ -116,65 +128,74 @@ public long getSize() * @return memory usage in MB */ @Override - public int getMemoryUsage() - { + public int getMemoryUsage() { return Math.round(root.getCapacity() / Helper.MB); } - void clear() - { + void clear() { size = 0; height = 1; root = new BTreeEntry(initLeafSize, true); } - int getNoNumberValue() - { + int getNoNumberValue() { return noNumberValue; } - void flush() - { + void flush() { throw new IllegalStateException("not supported yet"); } - private int getEntries() - { + private int getEntries() { return root.getEntries(); } - static class ReturnValue - { + @Override + public void optimize() { + if (getSize() > 10000) { +// StopWatch sw = new StopWatch().start(); +// int old = memoryUsage(); + root.compact(); +// logger.info(size + "| osmIdMap.optimize took: " + sw.stop().getSeconds() +// + " => freed: " + (old - memoryUsage()) + "MB"); + } + } + + @Override + public String toString() { + return "Height:" + height() + ", entries:" + getEntries(); + } + + void print() { + logger.info(root.toString(1)); + } + + static class ReturnValue { int oldValue; BTreeEntry tree; - public ReturnValue() - { + public ReturnValue() { } - public ReturnValue( int oldValue ) - { + public ReturnValue(int oldValue) { this.oldValue = oldValue; } } - class BTreeEntry - { + class BTreeEntry { int entrySize; long keys[]; int values[]; BTreeEntry children[]; boolean isLeaf; - public BTreeEntry( int tmpSize, boolean leaf ) - { + public BTreeEntry(int tmpSize, boolean leaf) { this.isLeaf = leaf; keys = new long[tmpSize]; values = new int[tmpSize]; - if (!isLeaf) - // in a b-tree we need one more entry to point to all children! - { + if (!isLeaf) { + // in a b-tree we need one more entry to point to all children! children = new BTreeEntry[tmpSize + 1]; } } @@ -183,11 +204,9 @@ public BTreeEntry( int tmpSize, boolean leaf ) * @return the old value which was associated with the specified key or if no update it * returns noNumberValue */ - ReturnValue put( long key, int newValue ) - { + ReturnValue put(long key, int newValue) { int index = binarySearch(keys, 0, entrySize, key); - if (index >= 0) - { + if (index >= 0) { // update int oldValue = values[index]; values[index] = newValue; @@ -196,50 +215,35 @@ ReturnValue put( long key, int newValue ) index = ~index; ReturnValue downTreeRV; - if (isLeaf || children[index] == null) - { + if (isLeaf || children[index] == null) { // insert downTreeRV = new ReturnValue(noNumberValue); downTreeRV.tree = checkSplitEntry(); - if (downTreeRV.tree == null) - { + if (downTreeRV.tree == null) { insertKeyValue(index, key, newValue); - } else - { - if (index <= splitIndex) - { - downTreeRV.tree.children[0].insertKeyValue(index, key, newValue); - } else - { - downTreeRV.tree.children[1].insertKeyValue(index - splitIndex - 1, key, newValue); - } + } else if (index <= splitIndex) { + downTreeRV.tree.children[0].insertKeyValue(index, key, newValue); + } else { + downTreeRV.tree.children[1].insertKeyValue(index - splitIndex - 1, key, newValue); } return downTreeRV; } downTreeRV = children[index].put(key, newValue); - if (downTreeRV.oldValue != noNumberValue) - // only update + if (downTreeRV.oldValue != noNumberValue) // only update { return downTreeRV; } - if (downTreeRV.tree != null) - { + if (downTreeRV.tree != null) { // split this treeEntry if it is too big BTreeEntry returnTree, downTree = returnTree = checkSplitEntry(); - if (downTree == null) - { + if (downTree == null) { insertTree(index, downTreeRV.tree); - } else - { - if (index <= splitIndex) - { - downTree.children[0].insertTree(index, downTreeRV.tree); - } else - { - downTree.children[1].insertTree(index - splitIndex - 1, downTreeRV.tree); - } + } else if (index <= splitIndex) { + downTree.children[0].insertTree(index, downTreeRV.tree); + } else { + downTree.children[1].insertTree(index - splitIndex - 1, downTreeRV.tree); } downTreeRV.tree = returnTree; @@ -251,10 +255,8 @@ ReturnValue put( long key, int newValue ) * @return null if nothing to do or a new sub tree if this tree capacity is no longer * sufficient. */ - BTreeEntry checkSplitEntry() - { - if (entrySize < maxLeafEntries) - { + BTreeEntry checkSplitEntry() { + if (entrySize < maxLeafEntries) { return null; } @@ -278,28 +280,23 @@ BTreeEntry checkSplitEntry() return newTree; } - void copy( BTreeEntry fromChild, BTreeEntry toChild, int from, int count ) - { + void copy(BTreeEntry fromChild, BTreeEntry toChild, int from, int count) { System.arraycopy(fromChild.keys, from, toChild.keys, 0, count); System.arraycopy(fromChild.values, from, toChild.values, 0, count); - if (!fromChild.isLeaf) - { + if (!fromChild.isLeaf) { System.arraycopy(fromChild.children, from, toChild.children, 0, count + 1); } toChild.entrySize = count; } - void insertKeyValue( int index, long key, int newValue ) - { + void insertKeyValue(int index, long key, int newValue) { ensureSize(entrySize + 1); int count = entrySize - index; - if (count > 0) - { + if (count > 0) { System.arraycopy(keys, index, keys, index + 1, count); System.arraycopy(values, index, values, index + 1, count); - if (!isLeaf) - { + if (!isLeaf) { System.arraycopy(children, index + 1, children, index + 2, count); } } @@ -309,11 +306,9 @@ void insertKeyValue( int index, long key, int newValue ) entrySize++; } - void insertTree( int index, BTreeEntry tree ) - { + void insertTree(int index, BTreeEntry tree) { insertKeyValue(index, tree.keys[0], tree.values[0]); - if (!isLeaf) - { + if (!isLeaf) { // overwrite children children[index] = tree.children[0]; // set @@ -321,16 +316,13 @@ void insertTree( int index, BTreeEntry tree ) } } - int get( long key ) - { + int get(long key) { int index = binarySearch(keys, 0, entrySize, key); - if (index >= 0) - { + if (index >= 0) { return values[index]; } index = ~index; - if (isLeaf || children[index] == null) - { + if (isLeaf || children[index] == null) { return noNumberValue; } return children[index].get(key); @@ -339,16 +331,12 @@ int get( long key ) /** * @return used bytes */ - long getCapacity() - { + long getCapacity() { long cap = keys.length * (8 + 4) + 3 * 12 + 4 + 1; - if (!isLeaf) - { + if (!isLeaf) { cap += children.length * 4; - for (int i = 0; i < children.length; i++) - { - if (children[i] != null) - { + for (int i = 0; i < children.length; i++) { + if (children[i] != null) { cap += children[i].getCapacity(); } } @@ -356,15 +344,11 @@ long getCapacity() return cap; } - int getEntries() - { + int getEntries() { int entries = 1; - if (!isLeaf) - { - for (int i = 0; i < children.length; i++) - { - if (children[i] != null) - { + if (!isLeaf) { + for (int i = 0; i < children.length; i++) { + if (children[i] != null) { entries += children[i].getEntries(); } } @@ -372,70 +356,53 @@ int getEntries() return entries; } - void ensureSize( int size ) - { - if (size <= keys.length) - { + void ensureSize(int size) { + if (size <= keys.length) { return; } int newSize = Math.min(maxLeafEntries, Math.max(size + 1, Math.round(size * factor))); keys = Arrays.copyOf(keys, newSize); values = Arrays.copyOf(values, newSize); - if (!isLeaf) - { + if (!isLeaf) { children = Arrays.copyOf(children, newSize + 1); } } - void compact() - { + void compact() { int tolerance = 1; - if (entrySize + tolerance < keys.length) - { + if (entrySize + tolerance < keys.length) { keys = Arrays.copyOf(keys, entrySize); values = Arrays.copyOf(values, entrySize); - if (!isLeaf) - { + if (!isLeaf) { children = Arrays.copyOf(children, entrySize + 1); } } - if (!isLeaf) - { - for (int i = 0; i < children.length; i++) - { - if (children[i] != null) - { + if (!isLeaf) { + for (int i = 0; i < children.length; i++) { + if (children[i] != null) { children[i].compact(); } } } } - String toString( int height ) - { + String toString(int height) { String str = height + ": "; - for (int i = 0; i < entrySize; i++) - { - if (i > 0) - { + for (int i = 0; i < entrySize; i++) { + if (i > 0) { str += ","; } - if (keys[i] == noNumberValue) - { + if (keys[i] == noNumberValue) { str += "-"; - } else - { + } else { str += keys[i]; } } str += "\n"; - if (!isLeaf) - { - for (int i = 0; i < entrySize + 1; i++) - { - if (children[i] != null) - { + if (!isLeaf) { + for (int i = 0; i < entrySize + 1; i++) { + if (children[i] != null) { str += children[i].toString(height + 1) + "| "; } } @@ -443,61 +410,4 @@ String toString( int height ) return str; } } - - @Override - public void optimize() - { - if (getSize() > 10000) - { -// StopWatch sw = new StopWatch().start(); -// int old = memoryUsage(); - root.compact(); -// logger.info(size + "| osmIdMap.optimize took: " + sw.stop().getSeconds() -// + " => freed: " + (old - memoryUsage()) + "MB"); - } - } - - @Override - public String toString() - { - return "Height:" + height() + ", entries:" + getEntries(); - } - - void print() - { - logger.info(root.toString(1)); - } - - // LATER: see OSMIDMap for a version where we use DataAccess - static int binarySearch( long keys[], int start, int len, long key ) - { - int high = start + len, low = start - 1, guess; - while (high - low > 1) - { - // use >>> for average or we could get an integer overflow. - guess = (high + low) >>> 1; - long guessedKey = keys[guess]; - if (guessedKey < key) - { - low = guess; - } else - { - high = guess; - } - } - - if (high == start + len) - { - return ~(start + len); - } - - long highKey = keys[high]; - if (highKey == key) - { - return high; - } else - { - return ~high; - } - } } diff --git a/core/src/main/java/com/graphhopper/coll/GHSortedCollection.java b/core/src/main/java/com/graphhopper/coll/GHSortedCollection.java index 4020c9dcfc6..16bf8a2b617 100644 --- a/core/src/main/java/com/graphhopper/coll/GHSortedCollection.java +++ b/core/src/main/java/com/graphhopper/coll/GHSortedCollection.java @@ -27,86 +27,71 @@ * A priority queue implemented by a treemap to allow fast key update. Or should we use a standard * b-tree? *

+ * * @author Peter Karich */ -public class GHSortedCollection -{ - private int size; +public class GHSortedCollection { private final int slidingMeanValue = 20; private final TreeMap map; + private int size; - public GHSortedCollection() - { + public GHSortedCollection() { // use size as indicator for maxEntries => try radix sort? map = new TreeMap(); } - public void clear() - { + public void clear() { size = 0; map.clear(); } - void remove( int key, int value ) - { + void remove(int key, int value) { TIntHashSet set = map.get(value); - if (set == null || !set.remove(key)) - { + if (set == null || !set.remove(key)) { throw new IllegalStateException("cannot remove key " + key + " with value " + value + " - did you insert " + key + "," + value + " before?"); } size--; - if (set.isEmpty()) - { + if (set.isEmpty()) { map.remove(value); } } - public void update( int key, int oldValue, int value ) - { + public void update(int key, int oldValue, int value) { remove(key, oldValue); insert(key, value); } - public void insert( int key, int value ) - { + public void insert(int key, int value) { TIntHashSet set = map.get(value); - if (set == null) - { + if (set == null) { map.put(value, set = new TIntHashSet(slidingMeanValue)); } // else // slidingMeanValue = Math.max(5, (slidingMeanValue + set.size()) / 2); - if (!set.add(key)) - { + if (!set.add(key)) { throw new IllegalStateException("use update if you want to update " + key); } size++; } - public int peekValue() - { - if (size == 0) - { + public int peekValue() { + if (size == 0) { throw new IllegalStateException("collection is already empty!?"); } Entry e = map.firstEntry(); - if (e.getValue().isEmpty()) - { + if (e.getValue().isEmpty()) { throw new IllegalStateException("internal set is already empty!?"); } return map.firstEntry().getKey(); } - public int peekKey() - { - if (size == 0) - { + public int peekKey() { + if (size == 0) { throw new IllegalStateException("collection is already empty!?"); } TIntHashSet set = map.firstEntry().getValue(); - if (set.isEmpty()) - { + if (set.isEmpty()) { throw new IllegalStateException("internal set is already empty!?"); } return set.iterator().next(); @@ -115,64 +100,52 @@ public int peekKey() /** * @return removes the smallest entry (key and value) from this collection */ - public int pollKey() - { + public int pollKey() { size--; - if (size < 0) - { + if (size < 0) { throw new IllegalStateException("collection is already empty!?"); } Entry e = map.firstEntry(); TIntHashSet set = e.getValue(); TIntIterator iter = set.iterator(); - if (set.isEmpty()) - { + if (set.isEmpty()) { throw new IllegalStateException("internal set is already empty!?"); } int val = iter.next(); iter.remove(); - if (set.isEmpty()) - { + if (set.isEmpty()) { map.remove(e.getKey()); } return val; } - public int getSize() - { + public int getSize() { return size; } - public boolean isEmpty() - { + public boolean isEmpty() { return size == 0; } - public int getSlidingMeanValue() - { + public int getSlidingMeanValue() { return slidingMeanValue; } @Override - public String toString() - { + public String toString() { int min = Integer.MAX_VALUE; int max = Integer.MIN_VALUE; - for (Entry e : map.entrySet()) - { + for (Entry e : map.entrySet()) { int tmpSize = e.getValue().size(); - if (min > tmpSize) - { + if (min > tmpSize) { min = tmpSize; } - if (max < tmpSize) - { + if (max < tmpSize) { max = tmpSize; } } String str = ""; - if (!isEmpty()) - { + if (!isEmpty()) { str = ", minEntry=(" + peekKey() + "=>" + peekValue() + ")"; } return "size=" + size + ", treeMap.size=" + map.size() diff --git a/core/src/main/java/com/graphhopper/coll/GHTBitSet.java b/core/src/main/java/com/graphhopper/coll/GHTBitSet.java index 67353399543..0cf1c615d32 100644 --- a/core/src/main/java/com/graphhopper/coll/GHTBitSet.java +++ b/core/src/main/java/com/graphhopper/coll/GHTBitSet.java @@ -23,75 +23,62 @@ /** * Implements the bitset interface via a trove THashSet. More efficient for a few entries. *

+ * * @author Peter Karich */ -public class GHTBitSet implements GHBitSet -{ +public class GHTBitSet implements GHBitSet { private final TIntHashSet tHash; - public GHTBitSet( TIntHashSet set ) - { + public GHTBitSet(TIntHashSet set) { tHash = set; } - public GHTBitSet( int no ) - { + public GHTBitSet(int no) { tHash = new TIntHashSet(no, 0.7f, -1); } - public GHTBitSet() - { + public GHTBitSet() { this(1000); } @Override - public final boolean contains( int index ) - { + public final boolean contains(int index) { return tHash.contains(index); } @Override - public final void add( int index ) - { + public final void add(int index) { tHash.add(index); } @Override - public final String toString() - { + public final String toString() { return tHash.toString(); } @Override - public final int getCardinality() - { + public final int getCardinality() { return tHash.size(); } @Override - public final void clear() - { + public final void clear() { tHash.clear(); } @Override - public void remove( int index ) - { + public void remove(int index) { tHash.remove(index); } @Override - public final GHBitSet copyTo( GHBitSet bs ) - { + public final GHBitSet copyTo(GHBitSet bs) { bs.clear(); - if (bs instanceof GHTBitSet) - { + if (bs instanceof GHTBitSet) { ((GHTBitSet) bs).tHash.addAll(this.tHash); - } else - { + } else { TIntIterator iter = tHash.iterator(); - while (iter.hasNext()) - { + while (iter.hasNext()) { bs.add(iter.next()); } } @@ -99,8 +86,7 @@ public final GHBitSet copyTo( GHBitSet bs ) } @Override - public int next( int index ) - { + public int next(int index) { throw new UnsupportedOperationException("Not supported yet."); } } diff --git a/core/src/main/java/com/graphhopper/coll/GHTreeMapComposed.java b/core/src/main/java/com/graphhopper/coll/GHTreeMapComposed.java index 38abd7f07f1..90bc1abfc93 100644 --- a/core/src/main/java/com/graphhopper/coll/GHTreeMapComposed.java +++ b/core/src/main/java/com/graphhopper/coll/GHTreeMapComposed.java @@ -25,54 +25,46 @@ * A priority queue implemented by a TreeMap. As the tree map does not allow duplicated we compose * the key via priority | nodeId. *

+ * * @author Peter Karich */ -public class GHTreeMapComposed -{ +public class GHTreeMapComposed { private static final Integer NOT_EMPTY = new Integer(-3); private final BitUtil bitUtil = BitUtil.BIG; private final TreeMap map; - public GHTreeMapComposed() - { + public GHTreeMapComposed() { map = new TreeMap(); } - public void clear() - { + public void clear() { map.clear(); } - void remove( int key, int value ) - { + void remove(int key, int value) { long v = bitUtil.toLong(value, key); - if (!map.remove(v).equals(NOT_EMPTY)) - { + if (!map.remove(v).equals(NOT_EMPTY)) { throw new IllegalStateException("cannot remove key " + key + " with value " + value + " - did you insert " + key + "," + value + " before?"); } } - public void update( int key, int oldValue, int value ) - { + public void update(int key, int oldValue, int value) { remove(key, oldValue); insert(key, value); } - public void insert( int key, int value ) - { + public void insert(int key, int value) { long v = bitUtil.toLong(value, key); map.put(v, NOT_EMPTY); } - public int peekValue() - { + public int peekValue() { long key = map.firstEntry().getKey(); return (int) (key >> 32); } - public int peekKey() - { + public int peekKey() { long key = map.firstEntry().getKey(); return (int) (key & 0xFFFFFFFFL); } @@ -80,8 +72,7 @@ public int peekKey() /** * @return removes the smallest entry (key and value) from this collection */ - public int pollKey() - { + public int pollKey() { if (map.isEmpty()) throw new IllegalStateException("Cannot poll collection is empty!"); @@ -89,19 +80,16 @@ public int pollKey() return (int) (key & 0xFFFFFFFFL); } - public int getSize() - { + public int getSize() { return map.size(); } - public boolean isEmpty() - { + public boolean isEmpty() { return map.isEmpty(); } @Override - public String toString() - { + public String toString() { return map.toString(); } } diff --git a/core/src/main/java/com/graphhopper/coll/IntDoubleBinHeap.java b/core/src/main/java/com/graphhopper/coll/IntDoubleBinHeap.java index ac0f1b33233..d2f06e69906 100644 --- a/core/src/main/java/com/graphhopper/coll/IntDoubleBinHeap.java +++ b/core/src/main/java/com/graphhopper/coll/IntDoubleBinHeap.java @@ -20,14 +20,11 @@ /** * @author Peter Karich */ -public class IntDoubleBinHeap extends OTPIntDoubleBinHeap implements BinHeapWrapper -{ - public IntDoubleBinHeap() - { +public class IntDoubleBinHeap extends OTPIntDoubleBinHeap implements BinHeapWrapper { + public IntDoubleBinHeap() { } - public IntDoubleBinHeap( int capacity ) - { + public IntDoubleBinHeap(int capacity) { super(capacity); } } diff --git a/core/src/main/java/com/graphhopper/coll/LongIntMap.java b/core/src/main/java/com/graphhopper/coll/LongIntMap.java index a9f12120bfc..015e47b6b26 100644 --- a/core/src/main/java/com/graphhopper/coll/LongIntMap.java +++ b/core/src/main/java/com/graphhopper/coll/LongIntMap.java @@ -20,11 +20,10 @@ /** * @author Peter Karich */ -public interface LongIntMap -{ - int put( long key, int value ); +public interface LongIntMap { + int put(long key, int value); - int get( long key ); + int get(long key); long getSize(); diff --git a/core/src/main/java/com/graphhopper/coll/MapEntry.java b/core/src/main/java/com/graphhopper/coll/MapEntry.java index bd740cc1199..e68530d629d 100644 --- a/core/src/main/java/com/graphhopper/coll/MapEntry.java +++ b/core/src/main/java/com/graphhopper/coll/MapEntry.java @@ -23,72 +23,61 @@ /** * Simple impl of Map.Entry. So that we can have ordered maps. *

+ * * @author Peter Karich */ -public class MapEntry implements Map.Entry, Serializable -{ +public class MapEntry implements Map.Entry, Serializable { private static final long serialVersionUID = 1L; private K key; private V value; - public MapEntry( K key, V value ) - { + public MapEntry(K key, V value) { this.key = key; this.value = value; } @Override - public K getKey() - { + public K getKey() { return key; } @Override - public V getValue() - { + public V getValue() { return value; } @Override - public V setValue( V value ) - { + public V setValue(V value) { this.value = value; return value; } @Override - public String toString() - { + public String toString() { return getKey() + ", " + getValue(); } @SuppressWarnings("unchecked") @Override - public boolean equals( Object obj ) - { - if (obj == null) - { + public boolean equals(Object obj) { + if (obj == null) { return false; } - if (getClass() != obj.getClass()) - { + if (getClass() != obj.getClass()) { return false; } final MapEntry other = (MapEntry) obj; - if (this.key != other.key && (this.key == null || !this.key.equals(other.key))) - { + if (this.key != other.key && (this.key == null || !this.key.equals(other.key))) { return false; } - if (this.value != other.value && (this.value == null || !this.value.equals(other.value))) - { + if (this.value != other.value && (this.value == null || !this.value.equals(other.value))) { return false; } return true; } @Override - public int hashCode() - { + public int hashCode() { int hash = 7; hash = 19 * hash + (this.key != null ? this.key.hashCode() : 0); hash = 19 * hash + (this.value != null ? this.value.hashCode() : 0); diff --git a/core/src/main/java/com/graphhopper/coll/OSMIDMap.java b/core/src/main/java/com/graphhopper/coll/OSMIDMap.java index db0e38699ca..2051b6f31ee 100644 --- a/core/src/main/java/com/graphhopper/coll/OSMIDMap.java +++ b/core/src/main/java/com/graphhopper/coll/OSMIDMap.java @@ -26,25 +26,23 @@ * This is a special purpose map for writing increasing OSM IDs with consecutive values. It stores * a map from long to int in a memory friendly way and but does NOT provide O(1) access. *

+ * * @author Peter Karich */ -public class OSMIDMap implements LongIntMap -{ +public class OSMIDMap implements LongIntMap { private static final BitUtil bitUtil = BitUtil.LITTLE; private final DataAccess keys; private final DataAccess values; - private long lastKey = Long.MIN_VALUE; - private long size; private final int noEntryValue; private final Directory dir; + private long lastKey = Long.MIN_VALUE; + private long size; - public OSMIDMap( Directory dir ) - { + public OSMIDMap(Directory dir) { this(dir, -1); } - public OSMIDMap( Directory dir, int noNumber ) - { + public OSMIDMap(Directory dir, int noNumber) { this.dir = dir; this.noEntryValue = noNumber; keys = dir.find("osmid_map_keys"); @@ -53,19 +51,42 @@ public OSMIDMap( Directory dir, int noNumber ) values.create(1000); } - public void remove() - { + static long binarySearch(DataAccess da, long start, long len, long key) { + long high = start + len, low = start - 1, guess; + byte[] longBytes = new byte[8]; + while (high - low > 1) { + // use >>> for average or we could get an integer overflow. + guess = (high + low) >>> 1; + long tmp = guess << 3; + da.getBytes(tmp, longBytes, 8); + long guessedKey = bitUtil.toLong(longBytes); + if (guessedKey < key) + low = guess; + else + high = guess; + } + + if (high == start + len) + return ~(start + len); + + long tmp = high << 3; + da.getBytes(tmp, longBytes, 8); + long highKey = bitUtil.toLong(longBytes); + if (highKey == key) + return high; + else + return ~high; + } + + public void remove() { dir.remove(keys); } @Override - public int put( long key, int value ) - { - if (key <= lastKey) - { + public int put(long key, int value) { + if (key <= lastKey) { long oldValueIndex = binarySearch(keys, 0, getSize(), key); - if (oldValueIndex < 0) - { + if (oldValueIndex < 0) { throw new IllegalStateException("Cannot insert keys lower than " + "the last key " + key + " < " + lastKey + ". Only updating supported"); } @@ -89,8 +110,7 @@ public int put( long key, int value ) } @Override - public int get( long key ) - { + public int get(long key) { long retIndex = binarySearch(keys, 0, getSize(), key); if (retIndex < 0) return noEntryValue; @@ -98,54 +118,21 @@ public int get( long key ) return values.getInt(retIndex * 4); } - static long binarySearch( DataAccess da, long start, long len, long key ) - { - long high = start + len, low = start - 1, guess; - byte[] longBytes = new byte[8]; - while (high - low > 1) - { - // use >>> for average or we could get an integer overflow. - guess = (high + low) >>> 1; - long tmp = guess << 3; - da.getBytes(tmp, longBytes, 8); - long guessedKey = bitUtil.toLong(longBytes); - if (guessedKey < key) - low = guess; - else - high = guess; - } - - if (high == start + len) - return ~(start + len); - - long tmp = high << 3; - da.getBytes(tmp, longBytes, 8); - long highKey = bitUtil.toLong(longBytes); - if (highKey == key) - return high; - else - return ~high; - } - @Override - public long getSize() - { + public long getSize() { return size / 4; } - public long getCapacity() - { + public long getCapacity() { return keys.getCapacity(); } @Override - public int getMemoryUsage() - { + public int getMemoryUsage() { return Math.round(getCapacity() / Helper.MB); } @Override - public void optimize() - { + public void optimize() { } } diff --git a/core/src/main/java/com/graphhopper/coll/SparseIntIntArray.java b/core/src/main/java/com/graphhopper/coll/SparseIntIntArray.java index 6fca6822368..a60d8fd9206 100644 --- a/core/src/main/java/com/graphhopper/coll/SparseIntIntArray.java +++ b/core/src/main/java/com/graphhopper/coll/SparseIntIntArray.java @@ -22,16 +22,17 @@ *

* SparseArrays map ints to ints. Unlike a normal array of ints, there can be gaps in the indices. */ -public class SparseIntIntArray -{ +public class SparseIntIntArray { private static final int DELETED = Integer.MIN_VALUE; private boolean mGarbage = false; + private int[] mKeys; + private int[] mValues; + private int mSize; /** * Creates a new SparseIntIntArray containing no mappings. */ - public SparseIntIntArray() - { + public SparseIntIntArray() { this(10); } @@ -39,27 +40,45 @@ public SparseIntIntArray() * Creates a new SparseIntIntArray containing no mappings that will not require any additional * memory allocation to store the specified number of mappings. */ - public SparseIntIntArray( int cap ) - { - try - { + public SparseIntIntArray(int cap) { + try { cap = Helper.idealIntArraySize(cap); mKeys = new int[cap]; mValues = new int[cap]; mSize = 0; - } catch (OutOfMemoryError err) - { + } catch (OutOfMemoryError err) { System.err.println("requested capacity " + cap); throw err; } } + static int binarySearch(int[] a, int start, int len, int key) { + int high = start + len, low = start - 1, guess; + while (high - low > 1) { + // use >>> for average or we could get an integer overflow. + guess = (high + low) >>> 1; + + if (a[guess] < key) { + low = guess; + } else { + high = guess; + } + } + + if (high == start + len) { + return ~(start + len); + } else if (a[high] == key) { + return high; + } else { + return ~high; + } + } + /** * Gets the Object mapped from the specified key, or null if no such mapping has * been made. */ - public int get( int key ) - { + public int get(int key) { return get(key, -1); } @@ -67,14 +86,11 @@ public int get( int key ) * Gets the Object mapped from the specified key, or the specified Object if no such mapping has * been made. */ - private int get( int key, int valueIfKeyNotFound ) - { + private int get(int key, int valueIfKeyNotFound) { int i = binarySearch(mKeys, 0, mSize, key); - if (i < 0 || mValues[i] == DELETED) - { + if (i < 0 || mValues[i] == DELETED) { return valueIfKeyNotFound; - } else - { + } else { return mValues[i]; } } @@ -82,30 +98,24 @@ private int get( int key, int valueIfKeyNotFound ) /** * Removes the mapping from the specified key, if there was any. */ - public void remove( int key ) - { + public void remove(int key) { int i = binarySearch(mKeys, 0, mSize, key); - if (i >= 0 && mValues[i] != DELETED) - { + if (i >= 0 && mValues[i] != DELETED) { mValues[i] = DELETED; mGarbage = true; } } - private void gc() - { + private void gc() { int n = mSize; int o = 0; int[] keys = mKeys; int[] values = mValues; - for (int i = 0; i < n; i++) - { + for (int i = 0; i < n; i++) { int val = values[i]; - if (val != DELETED) - { - if (i != o) - { + if (val != DELETED) { + if (i != o) { keys[o] = keys[i]; values[o] = val; } @@ -122,34 +132,28 @@ private void gc() * Adds a mapping from the specified key to the specified value, replacing the previous mapping * from the specified key if there was one. */ - public int put( int key, int value ) - { + public int put(int key, int value) { int i = binarySearch(mKeys, 0, mSize, key); - if (i >= 0) - { + if (i >= 0) { mValues[i] = value; - } else - { + } else { i = ~i; - if (i < mSize && mValues[i] == DELETED) - { + if (i < mSize && mValues[i] == DELETED) { mKeys[i] = key; mValues[i] = value; return i; } - if (mGarbage && mSize >= mKeys.length) - { + if (mGarbage && mSize >= mKeys.length) { gc(); // Search again because indices may have changed. i = ~binarySearch(mKeys, 0, mSize, key); } - if (mSize >= mKeys.length) - { + if (mSize >= mKeys.length) { int n = Helper.idealIntArraySize(mSize + 1); int[] nkeys = new int[n]; @@ -162,8 +166,7 @@ public int put( int key, int value ) mValues = nvalues; } - if (mSize - i != 0) - { + if (mSize - i != 0) { System.arraycopy(mKeys, i, mKeys, i + 1, mSize - i); System.arraycopy(mValues, i, mValues, i + 1, mSize - i); } @@ -178,10 +181,8 @@ public int put( int key, int value ) /** * Returns the number of key-value mappings that this SparseIntIntArray currently stores. */ - public int getSize() - { - if (mGarbage) - { + public int getSize() { + if (mGarbage) { gc(); } @@ -192,10 +193,8 @@ public int getSize() * Given an index in the range 0...size()-1, returns the key from the * indexth key-value mapping that this SparseIntIntArray stores. */ - public int keyAt( int index ) - { - if (mGarbage) - { + public int keyAt(int index) { + if (mGarbage) { gc(); } @@ -206,10 +205,8 @@ public int keyAt( int index ) * Given an index in the range 0...size()-1, sets a new key for the * indexth key-value mapping that this SparseIntIntArray stores. */ - public void setKeyAt( int index, int key ) - { - if (mGarbage) - { + public void setKeyAt(int index, int key) { + if (mGarbage) { gc(); } @@ -220,10 +217,8 @@ public void setKeyAt( int index, int key ) * Given an index in the range 0...size()-1, returns the value from the * indexth key-value mapping that this SparseIntIntArray stores. */ - public int valueAt( int index ) - { - if (mGarbage) - { + public int valueAt(int index) { + if (mGarbage) { gc(); } @@ -234,10 +229,8 @@ public int valueAt( int index ) * Given an index in the range 0...size()-1, sets a new value for the * indexth key-value mapping that this SparseIntIntArray stores. */ - public void setValueAt( int index, int value ) - { - if (mGarbage) - { + public void setValueAt(int index, int value) { + if (mGarbage) { gc(); } @@ -247,12 +240,10 @@ public void setValueAt( int index, int value ) /** * Removes all key-value mappings from this SparseIntIntArray. */ - public void clear() - { + public void clear() { int n = mSize; int[] values = mValues; - for (int i = 0; i < n; i++) - { + for (int i = 0; i < n; i++) { values[i] = -1; } @@ -264,21 +255,17 @@ public void clear() * Puts a key/value pair into the array, optimizing for the case where the key is greater than * all existing keys in the array. */ - public int append( int key, int value ) - { - if (mSize != 0 && key <= mKeys[mSize - 1]) - { + public int append(int key, int value) { + if (mSize != 0 && key <= mKeys[mSize - 1]) { return put(key, value); } - if (mGarbage && mSize >= mKeys.length) - { + if (mGarbage && mSize >= mKeys.length) { gc(); } int pos = mSize; - if (pos >= mKeys.length) - { + if (pos >= mKeys.length) { int n = Helper.idealIntArraySize(pos + 1); int[] nkeys = new int[n]; @@ -298,15 +285,12 @@ public int append( int key, int value ) } @Override - public String toString() - { + public String toString() { StringBuilder sb = new StringBuilder(); - for (int i = 0; i < getSize(); i++) - { + for (int i = 0; i < getSize(); i++) { int k = mKeys[i]; int v = mValues[i]; - if (i > 0) - { + if (i > 0) { sb.append(","); } sb.append(k); @@ -319,41 +303,7 @@ public String toString() /** * Warning: returns ~index and not -(index+1) like trove and jdk do */ - public int binarySearch( int key ) - { + public int binarySearch(int key) { return binarySearch(mKeys, 0, mSize, key); } - - static int binarySearch( int[] a, int start, int len, int key ) - { - int high = start + len, low = start - 1, guess; - while (high - low > 1) - { - // use >>> for average or we could get an integer overflow. - guess = (high + low) >>> 1; - - if (a[guess] < key) - { - low = guess; - } else - { - high = guess; - } - } - - if (high == start + len) - { - return ~(start + len); - } else if (a[high] == key) - { - return high; - } else - { - return ~high; - } - } - - private int[] mKeys; - private int[] mValues; - private int mSize; } diff --git a/core/src/main/java/com/graphhopper/geohash/KeyAlgo.java b/core/src/main/java/com/graphhopper/geohash/KeyAlgo.java index a6d08116db0..43ccc517d5e 100644 --- a/core/src/main/java/com/graphhopper/geohash/KeyAlgo.java +++ b/core/src/main/java/com/graphhopper/geohash/KeyAlgo.java @@ -23,19 +23,19 @@ * Defines the mapping between a one dimensional 'number' and a point (lat, lon) which is limited to * a defined bounds. *

+ * * @author Peter Karich */ -public interface KeyAlgo -{ +public interface KeyAlgo { /** * Sets the bounds of the underlying key algorithm. */ - KeyAlgo setBounds( double minLonInit, double maxLonInit, double minLatInit, double maxLatInit ); + KeyAlgo setBounds(double minLonInit, double maxLonInit, double minLatInit, double maxLatInit); - long encode( GHPoint coord ); + long encode(GHPoint coord); - long encode( double lat, double lon ); + long encode(double lat, double lon); - void decode( long spatialKey, GHPoint latLon ); + void decode(long spatialKey, GHPoint latLon); } diff --git a/core/src/main/java/com/graphhopper/geohash/LinearKeyAlgo.java b/core/src/main/java/com/graphhopper/geohash/LinearKeyAlgo.java index 01b3248f8d0..e2a1a9d0677 100644 --- a/core/src/main/java/com/graphhopper/geohash/LinearKeyAlgo.java +++ b/core/src/main/java/com/graphhopper/geohash/LinearKeyAlgo.java @@ -23,6 +23,7 @@ /** * This class maps lat,lon to a (tile)number unlike SpatialKeyAlgo. *

+ * * @author Peter Karich */ // A 4*3 precision linear key will look like @@ -36,54 +37,48 @@ // |----|----|----|----| // // lon -public class LinearKeyAlgo implements KeyAlgo -{ +public class LinearKeyAlgo implements KeyAlgo { + private static final double C = 1 - 1e-15; + private final int latUnits, lonUnits; private BBox bounds; private double latDelta, lonDelta; - private final int latUnits, lonUnits; - private static final double C = 1 - 1e-15; - public LinearKeyAlgo( int latUnits, int lonUnits ) - { + public LinearKeyAlgo(int latUnits, int lonUnits) { this.latUnits = latUnits; this.lonUnits = lonUnits; setWorldBounds(); } @Override - public LinearKeyAlgo setBounds( double minLonInit, double maxLonInit, double minLatInit, double maxLatInit ) - { + public LinearKeyAlgo setBounds(double minLonInit, double maxLonInit, double minLatInit, double maxLatInit) { bounds = new BBox(minLonInit, maxLonInit, minLatInit, maxLatInit); latDelta = (bounds.maxLat - bounds.minLat) / latUnits; lonDelta = (bounds.maxLon - bounds.minLon) / lonUnits; return this; } - public LinearKeyAlgo setBounds( BBox bounds ) - { + public LinearKeyAlgo setBounds(BBox bounds) { setBounds(bounds.minLon, bounds.maxLon, bounds.minLat, bounds.maxLat); return this; } - protected void setWorldBounds() - { + protected void setWorldBounds() { setBounds(-180, 180, -90, 90); } @Override - public long encode( GHPoint coord ) - { + public long encode(GHPoint coord) { return encode(coord.lat, coord.lon); } /** * Take latitude and longitude as input. *

+ * * @return the linear key */ @Override - public final long encode( double lat, double lon ) - { + public final long encode(double lat, double lon) { lat = Math.min(Math.max(lat, bounds.minLat), bounds.maxLat); lon = Math.min(Math.max(lon, bounds.minLon), bounds.maxLon); // introduce a minor correction to round to lower grid entry! @@ -95,24 +90,22 @@ public final long encode( double lat, double lon ) /** * This method returns latitude and longitude via latLon - calculated from specified linearKey *

+ * * @param linearKey is the input */ @Override - public final void decode( long linearKey, GHPoint latLon ) - { + public final void decode(long linearKey, GHPoint latLon) { double lat = linearKey / lonUnits * latDelta + bounds.minLat; double lon = linearKey % lonUnits * lonDelta + bounds.minLon; latLon.lat = lat + latDelta / 2; latLon.lon = lon + lonDelta / 2; } - public double getLatDelta() - { + public double getLatDelta() { return latDelta; } - public double getLonDelta() - { + public double getLonDelta() { return lonDelta; } diff --git a/core/src/main/java/com/graphhopper/geohash/SpatialKeyAlgo.java b/core/src/main/java/com/graphhopper/geohash/SpatialKeyAlgo.java index 32f213be2a0..49b08fef9dd 100644 --- a/core/src/main/java/com/graphhopper/geohash/SpatialKeyAlgo.java +++ b/core/src/main/java/com/graphhopper/geohash/SpatialKeyAlgo.java @@ -55,6 +55,7 @@ * But when stored e.g. as int one would need to (left) shift several times if precision is only * 3bits. *

+ * * @author Peter Karich */ // A 2 bit precision spatial key could look like @@ -70,8 +71,7 @@ // |----|----|----|----| // | // lon0 == 0 | lon0 == 1 -public class SpatialKeyAlgo implements KeyAlgo -{ +public class SpatialKeyAlgo implements KeyAlgo { private BBox bbox; private int allBits; private long initialBits; @@ -79,13 +79,11 @@ public class SpatialKeyAlgo implements KeyAlgo /** * @param allBits how many bits should be used for the spatial key when encoding/decoding */ - public SpatialKeyAlgo( int allBits ) - { + public SpatialKeyAlgo(int allBits) { myinit(allBits); } - private void myinit( int allBits ) - { + private void myinit(int allBits) { if (allBits > 64) throw new IllegalStateException("allBits is too big and does not fit into 8 bytes"); @@ -102,13 +100,11 @@ private void myinit( int allBits ) /** * @return the number of involved bits */ - public int getBits() - { + public int getBits() { return allBits; } - public int getExactPrecision() - { + public int getExactPrecision() { // 360 / 2^(allBits/2) = 1/precision int p = (int) (Math.pow(2, allBits) / 360); // no rounding error @@ -116,38 +112,34 @@ public int getExactPrecision() return (int) Math.log10(p); } - public SpatialKeyAlgo bounds( BBox box ) - { + public SpatialKeyAlgo bounds(BBox box) { bbox = box.clone(); return this; } @Override - public SpatialKeyAlgo setBounds( double minLonInit, double maxLonInit, double minLatInit, double maxLatInit ) - { + public SpatialKeyAlgo setBounds(double minLonInit, double maxLonInit, double minLatInit, double maxLatInit) { bounds(new BBox(minLonInit, maxLonInit, minLatInit, maxLatInit)); return this; } - protected void setWorldBounds() - { + protected void setWorldBounds() { setBounds(-180, 180, -90, 90); } @Override - public long encode( GHPoint coord ) - { + public long encode(GHPoint coord) { return encode(coord.lat, coord.lon); } /** * Take latitude and longitude as input. *

+ * * @return the spatial key */ @Override - public final long encode( double lat, double lon ) - { + public final long encode(double lat, double lon) { // PERFORMANCE: int operations would be faster than double (for further comparison etc) // but we would need 'long' because 'int factorForPrecision' is not enough (problem: coord!=decode(encode(coord)) see testBijection) // and 'long'-ops are more expensive than double (at least on 32bit systems) @@ -157,16 +149,12 @@ public final long encode( double lat, double lon ) double minLonTmp = bbox.minLon; double maxLonTmp = bbox.maxLon; int i = 0; - while (true) - { - if (minLatTmp < maxLatTmp) - { + while (true) { + if (minLatTmp < maxLatTmp) { double midLat = (minLatTmp + maxLatTmp) / 2; - if (lat < midLat) - { + if (lat < midLat) { maxLatTmp = midLat; - } else - { + } else { hash |= 1; minLatTmp = midLat; } @@ -179,14 +167,11 @@ public final long encode( double lat, double lon ) // if allBits is an odd number break; - if (minLonTmp < maxLonTmp) - { + if (minLonTmp < maxLonTmp) { double midLon = (minLonTmp + maxLonTmp) / 2; - if (lon < midLon) - { + if (lon < midLon) { maxLonTmp = midLon; - } else - { + } else { hash |= 1; minLonTmp = midLon; } @@ -203,11 +188,11 @@ public final long encode( double lat, double lon ) /** * This method returns latitude and longitude via latLon - calculated from specified spatialKey *

+ * * @param spatialKey is the input */ @Override - public final void decode( long spatialKey, GHPoint latLon ) - { + public final void decode(long spatialKey, GHPoint latLon) { // Performance: calculating 'midLon' and 'midLat' on the fly is not slower than using // precalculated values from arrays and for 'bits' a precalculated array is even slightly slower! @@ -217,26 +202,21 @@ public final void decode( long spatialKey, GHPoint latLon ) double lat = bbox.minLat; double lon = bbox.minLon; long bits = initialBits; - while (true) - { - if ((spatialKey & bits) != 0) - { + while (true) { + if ((spatialKey & bits) != 0) { lat += midLat; } midLat /= 2; bits >>>= 1; - if ((spatialKey & bits) != 0) - { + if ((spatialKey & bits) != 0) { lon += midLon; } midLon /= 2; - if (bits > 1) - { + if (bits > 1) { bits >>>= 1; - } else - { + } else { break; } } @@ -249,8 +229,7 @@ public final void decode( long spatialKey, GHPoint latLon ) } @Override - public String toString() - { + public String toString() { return "bits:" + allBits + ", bounds:" + bbox; } } diff --git a/core/src/main/java/com/graphhopper/reader/ConditionalTagInspector.java b/core/src/main/java/com/graphhopper/reader/ConditionalTagInspector.java index 121379184bd..0eb450a05d8 100644 --- a/core/src/main/java/com/graphhopper/reader/ConditionalTagInspector.java +++ b/core/src/main/java/com/graphhopper/reader/ConditionalTagInspector.java @@ -18,12 +18,10 @@ package com.graphhopper.reader; /** - * * @author Peter Karich */ -public interface ConditionalTagInspector -{ - boolean isRestrictedWayConditionallyPermitted( ReaderWay way ); +public interface ConditionalTagInspector { + boolean isRestrictedWayConditionallyPermitted(ReaderWay way); - boolean isPermittedWayConditionallyRestricted( ReaderWay way ); + boolean isPermittedWayConditionallyRestricted(ReaderWay way); } diff --git a/core/src/main/java/com/graphhopper/reader/DataReader.java b/core/src/main/java/com/graphhopper/reader/DataReader.java index 9bcf1e43147..e1e71fea556 100644 --- a/core/src/main/java/com/graphhopper/reader/DataReader.java +++ b/core/src/main/java/com/graphhopper/reader/DataReader.java @@ -19,6 +19,7 @@ import com.graphhopper.reader.dem.ElevationProvider; import com.graphhopper.routing.util.EncodingManager; + import java.io.File; import java.io.IOException; import java.util.Date; @@ -26,17 +27,16 @@ /** * @author Peter Karich */ -public interface DataReader -{ - DataReader setFile( File file ); +public interface DataReader { + DataReader setFile(File file); - DataReader setElevationProvider( ElevationProvider ep ); + DataReader setElevationProvider(ElevationProvider ep); - DataReader setWorkerThreads( int workerThreads ); + DataReader setWorkerThreads(int workerThreads); - DataReader setEncodingManager( EncodingManager em ); + DataReader setEncodingManager(EncodingManager em); - DataReader setWayPointMaxDistance( double wayPointMaxDistance ); + DataReader setWayPointMaxDistance(double wayPointMaxDistance); /** * This method triggers reading the underlying data to create a graph diff --git a/core/src/main/java/com/graphhopper/reader/PillarInfo.java b/core/src/main/java/com/graphhopper/reader/PillarInfo.java index 4621113af28..4d9558a1d5f 100644 --- a/core/src/main/java/com/graphhopper/reader/PillarInfo.java +++ b/core/src/main/java/com/graphhopper/reader/PillarInfo.java @@ -25,18 +25,17 @@ /** * This class helps to store lat,lon,ele for every node parsed in OSMReader *

+ * * @author Peter Karich */ -public class PillarInfo implements PointAccess -{ +public class PillarInfo implements PointAccess { private static final int LAT = 0 * 4, LON = 1 * 4, ELE = 2 * 4; private final boolean enabled3D; private final DataAccess da; private final int rowSizeInBytes; private final Directory dir; - public PillarInfo( boolean enabled3D, Directory dir ) - { + public PillarInfo(boolean enabled3D, Directory dir) { this.enabled3D = enabled3D; this.dir = dir; this.da = dir.find("tmp_pillar_info").create(100); @@ -44,38 +43,32 @@ public PillarInfo( boolean enabled3D, Directory dir ) } @Override - public boolean is3D() - { + public boolean is3D() { return enabled3D; } @Override - public int getDimension() - { + public int getDimension() { return enabled3D ? 3 : 2; } @Override - public void ensureNode( int nodeId ) - { + public void ensureNode(int nodeId) { long tmp = (long) nodeId * rowSizeInBytes; da.ensureCapacity(tmp + rowSizeInBytes); } @Override - public void setNode( int nodeId, double lat, double lon ) - { + public void setNode(int nodeId, double lat, double lon) { _setNode(nodeId, lat, lon, Double.NaN); } @Override - public void setNode( int nodeId, double lat, double lon, double ele ) - { + public void setNode(int nodeId, double lat, double lon, double ele) { _setNode(nodeId, lat, lon, ele); } - private void _setNode( int nodeId, double lat, double lon, double ele ) - { + private void _setNode(int nodeId, double lat, double lon, double ele) { ensureNode(nodeId); long tmp = (long) nodeId * rowSizeInBytes; da.setInt(tmp + LAT, Helper.degreeToInt(lat)); @@ -86,34 +79,29 @@ private void _setNode( int nodeId, double lat, double lon, double ele ) } @Override - public double getLatitude( int id ) - { + public double getLatitude(int id) { int intVal = da.getInt((long) id * rowSizeInBytes + LAT); return Helper.intToDegree(intVal); } @Override - public double getLat( int id ) - { + public double getLat(int id) { return getLatitude(id); } @Override - public double getLongitude( int id ) - { + public double getLongitude(int id) { int intVal = da.getInt((long) id * rowSizeInBytes + LON); return Helper.intToDegree(intVal); } @Override - public double getLon( int id ) - { + public double getLon(int id) { return getLongitude(id); } @Override - public double getElevation( int id ) - { + public double getElevation(int id) { if (!is3D()) return Double.NaN; @@ -122,13 +110,11 @@ public double getElevation( int id ) } @Override - public double getEle( int id ) - { + public double getEle(int id) { return getElevation(id); } - public void clear() - { + public void clear() { dir.remove(da); } } diff --git a/core/src/main/java/com/graphhopper/reader/ReaderElement.java b/core/src/main/java/com/graphhopper/reader/ReaderElement.java index 418412e47d7..2f7cd6b2e60 100644 --- a/core/src/main/java/com/graphhopper/reader/ReaderElement.java +++ b/core/src/main/java/com/graphhopper/reader/ReaderElement.java @@ -26,11 +26,11 @@ /** * Base class for all network objects *

+ * * @author Nop * @author Peter */ -public abstract class ReaderElement -{ +public abstract class ReaderElement { public static final int NODE = 0; public static final int WAY = 1; public static final int RELATION = 2; @@ -39,25 +39,21 @@ public abstract class ReaderElement private final long id; private final Map properties = new HashMap(5); - protected ReaderElement( long id, int type ) - { + protected ReaderElement(long id, int type) { this.id = id; this.type = type; } - public long getId() - { + public long getId() { return id; } - protected String tagsToString() - { + protected String tagsToString() { if (properties.isEmpty()) return ""; StringBuilder tagTxt = new StringBuilder(); - for (Map.Entry entry : properties.entrySet()) - { + for (Map.Entry entry : properties.entrySet()) { tagTxt.append(entry.getKey()); tagTxt.append("="); tagTxt.append(entry.getValue()); @@ -66,50 +62,42 @@ protected String tagsToString() return tagTxt.toString(); } - protected Map getTags() - { + protected Map getTags() { return properties; } - public void setTags( Map newTags ) - { + public void setTags(Map newTags) { properties.clear(); if (newTags != null) - for (Entry e : newTags.entrySet()) - { + for (Entry e : newTags.entrySet()) { setTag(e.getKey(), e.getValue()); } } - public boolean hasTags() - { + public boolean hasTags() { return !properties.isEmpty(); } - public String getTag( String name ) - { + public String getTag(String name) { return (String) properties.get(name); } @SuppressWarnings("unchecked") - public T getTag( String key, T defaultValue ) - { + public T getTag(String key, T defaultValue) { T val = (T) properties.get(key); if (val == null) return defaultValue; return val; } - public void setTag( String name, Object value ) - { + public void setTag(String name, Object value) { properties.put(name, value); } /** * Check that the object has a given tag with a given value. */ - public boolean hasTag( String key, Object value ) - { + public boolean hasTag(String key, Object value) { return value.equals(getTag(key, "")); } @@ -117,8 +105,7 @@ public boolean hasTag( String key, Object value ) * Check that a given tag has one of the specified values. If no values are given, just checks * for presence of the tag */ - public boolean hasTag( String key, String... values ) - { + public boolean hasTag(String key, String... values) { Object value = properties.get(key); if (value == null) return false; @@ -127,8 +114,7 @@ public boolean hasTag( String key, String... values ) if (values.length == 0) return true; - for (String val : values) - { + for (String val : values) { if (val.equals(value)) return true; } @@ -138,8 +124,7 @@ public boolean hasTag( String key, String... values ) /** * Check that a given tag has one of the specified values. */ - public final boolean hasTag( String key, Set values ) - { + public final boolean hasTag(String key, Set values) { return values.contains(getTag(key, "")); } @@ -147,10 +132,8 @@ public final boolean hasTag( String key, Set values ) * Check a number of tags in the given order for the any of the given values. Used to parse * hierarchical access restrictions */ - public boolean hasTag( List keyList, Set values ) - { - for (String key : keyList) - { + public boolean hasTag(List keyList, Set values) { + for (String key : keyList) { if (values.contains(getTag(key, ""))) return true; } @@ -160,39 +143,32 @@ public boolean hasTag( List keyList, Set values ) /** * Returns the first existing tag of the specified list where the order is important. */ - public String getFirstPriorityTag( List restrictions ) - { - for (String str : restrictions) - { + public String getFirstPriorityTag(List restrictions) { + for (String str : restrictions) { if (hasTag(str)) return getTag(str); } return ""; } - public void removeTag( String name ) - { + public void removeTag(String name) { properties.remove(name); } - public void clearTags() - { + public void clearTags() { properties.clear(); } - public int getType() - { + public int getType() { return type; } - public boolean isType( int type ) - { + public boolean isType(int type) { return this.type == type; } @Override - public String toString() - { + public String toString() { return properties.toString(); } } diff --git a/core/src/main/java/com/graphhopper/reader/ReaderNode.java b/core/src/main/java/com/graphhopper/reader/ReaderNode.java index 6660954e71f..9f5a27eb515 100644 --- a/core/src/main/java/com/graphhopper/reader/ReaderNode.java +++ b/core/src/main/java/com/graphhopper/reader/ReaderNode.java @@ -22,15 +22,14 @@ /** * Represents a node received from the reader. *

+ * * @author Nop */ -public class ReaderNode extends ReaderElement -{ +public class ReaderNode extends ReaderElement { private final double lat; private final double lon; - public ReaderNode( long id, PointAccess pointAccess, int accessId ) - { + public ReaderNode(long id, PointAccess pointAccess, int accessId) { super(id, NODE); this.lat = pointAccess.getLatitude(accessId); @@ -39,26 +38,22 @@ public ReaderNode( long id, PointAccess pointAccess, int accessId ) setTag("ele", pointAccess.getElevation(accessId)); } - public ReaderNode( long id, double lat, double lon ) - { + public ReaderNode(long id, double lat, double lon) { super(id, NODE); this.lat = lat; this.lon = lon; } - public double getLat() - { + public double getLat() { return lat; } - public double getLon() - { + public double getLon() { return lon; } - public double getEle() - { + public double getEle() { Object ele = getTags().get("ele"); if (ele == null) return Double.NaN; @@ -66,24 +61,19 @@ public double getEle() } @Override - public void setTag( String name, Object value ) - { - if ("ele".equals(name)) - { + public void setTag(String name, Object value) { + if ("ele".equals(name)) { if (value == null) value = null; - else if (value instanceof String) - { + else if (value instanceof String) { String str = (String) value; str = str.trim().replaceAll("\\,", "."); if (str.isEmpty()) value = null; else - try - { + try { value = Double.parseDouble(str); - } catch (NumberFormatException ex) - { + } catch (NumberFormatException ex) { return; } } else @@ -94,8 +84,7 @@ else if (value instanceof String) } @Override - public String toString() - { + public String toString() { StringBuilder txt = new StringBuilder(); txt.append("Node: "); txt.append(getId()); @@ -103,8 +92,7 @@ public String toString() txt.append(getLat()); txt.append(" lon="); txt.append(getLon()); - if (!getTags().isEmpty()) - { + if (!getTags().isEmpty()) { txt.append("\n"); txt.append(tagsToString()); } diff --git a/core/src/main/java/com/graphhopper/reader/ReaderRelation.java b/core/src/main/java/com/graphhopper/reader/ReaderRelation.java index c9c1bab9120..32f1a7e959d 100644 --- a/core/src/main/java/com/graphhopper/reader/ReaderRelation.java +++ b/core/src/main/java/com/graphhopper/reader/ReaderRelation.java @@ -23,47 +23,39 @@ /** * Represents a relation received from the reader. *

+ * * @author Nop */ -public class ReaderRelation extends ReaderElement -{ +public class ReaderRelation extends ReaderElement { protected final List members = new ArrayList(5); - public ReaderRelation( long id ) - { + public ReaderRelation(long id) { super(id, RELATION); } @Override - public String toString() - { + public String toString() { return "Relation (" + getId() + ", " + members.size() + " members)"; } - public List getMembers() - { + public List getMembers() { return members; } - public boolean isMetaRelation() - { - for (Member member : members) - { - if (member.getType() == RELATION) - { + public boolean isMetaRelation() { + for (Member member : members) { + if (member.getType() == RELATION) { return true; } } return false; } - public boolean isMixedRelation() - { + public boolean isMixedRelation() { boolean hasRel = false; boolean hasOther = false; - for (Member member : members) - { + for (Member member : members) { if (member.getType() == RELATION) hasRel = true; else @@ -75,25 +67,21 @@ public boolean isMixedRelation() return false; } - public void removeRelations() - { - for (int i = members.size() - 1; i >= 0; i--) - { + public void removeRelations() { + for (int i = members.size() - 1; i >= 0; i--) { if (members.get(i).getType() == RELATION) members.remove(i); } } - public void add( Member member ) - { + public void add(Member member) { members.add(member); } /** * Container class for relation members */ - public static class Member - { + public static class Member { public static final int NODE = 0; public static final int WAY = 1; public static final int RELATION = 2; @@ -101,41 +89,35 @@ public static class Member private final long ref; private final String role; - public Member( Member input ) - { + public Member(Member input) { type = input.type; ref = input.ref; role = input.role; } - public Member( int type, long ref, String role ) - { + public Member(int type, long ref, String role) { this.type = type; this.ref = ref; this.role = role; } @Override - public String toString() - { + public String toString() { return "Member " + type + ":" + ref; } - public int getType() - { + public int getType() { return type; } /** * member reference which is an OSM ID */ - public long getRef() - { + public long getRef() { return ref; } - public String getRole() - { + public String getRole() { return role; } } diff --git a/core/src/main/java/com/graphhopper/reader/ReaderWay.java b/core/src/main/java/com/graphhopper/reader/ReaderWay.java index d7a70259104..4a40fe3126c 100644 --- a/core/src/main/java/com/graphhopper/reader/ReaderWay.java +++ b/core/src/main/java/com/graphhopper/reader/ReaderWay.java @@ -23,25 +23,22 @@ /** * Represents a way received from the reader. *

+ * * @author Nop */ -public class ReaderWay extends ReaderElement -{ +public class ReaderWay extends ReaderElement { protected final TLongList nodes = new TLongArrayList(5); - public ReaderWay( long id ) - { + public ReaderWay(long id) { super(id, WAY); } - public TLongList getNodes() - { + public TLongList getNodes() { return nodes; } @Override - public String toString() - { + public String toString() { return "Way id:" + getId() + ", nodes:" + nodes.size() + ", tags:" + super.toString(); } } diff --git a/core/src/main/java/com/graphhopper/reader/dem/CGIARProvider.java b/core/src/main/java/com/graphhopper/reader/dem/CGIARProvider.java index fc602d29b2d..973a54bf034 100644 --- a/core/src/main/java/com/graphhopper/reader/dem/CGIARProvider.java +++ b/core/src/main/java/com/graphhopper/reader/dem/CGIARProvider.java @@ -23,21 +23,23 @@ import com.graphhopper.storage.GHDirectory; import com.graphhopper.util.Downloader; import com.graphhopper.util.Helper; +import org.apache.xmlgraphics.image.codec.tiff.TIFFDecodeParam; +import org.apache.xmlgraphics.image.codec.tiff.TIFFImageDecoder; +import org.apache.xmlgraphics.image.codec.util.SeekableStream; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.awt.image.Raster; -import java.io.*; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; import java.net.SocketTimeoutException; import java.util.HashMap; import java.util.Map; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; -import org.apache.xmlgraphics.image.codec.tiff.TIFFDecodeParam; -import org.apache.xmlgraphics.image.codec.tiff.TIFFImageDecoder; -import org.apache.xmlgraphics.image.codec.util.SeekableStream; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - /** * Elevation data from CGIAR project http://srtm.csi.cgiar.org/ 'PROCESSED SRTM DATA VERSION 4.1'. * Every file covers a region of 5x5 degree. License granted for all people using GraphHopper: @@ -50,35 +52,62 @@ * * *

+ * * @author NopMap * @author Peter Karich */ -public class CGIARProvider implements ElevationProvider -{ +public class CGIARProvider implements ElevationProvider { private static final int WIDTH = 6000; - private Downloader downloader = new Downloader("GraphHopper CGIARReader").setTimeout(10000); + final double precision = 1e7; private final Logger logger = LoggerFactory.getLogger(getClass()); private final Map cacheData = new HashMap(); + private final double invPrecision = 1 / precision; + private final int degree = 5; + private Downloader downloader = new Downloader("GraphHopper CGIARReader").setTimeout(10000); private File cacheDir = new File("/tmp/cgiar"); // for alternatives see #346 private String baseUrl = "http://srtm.csi.cgiar.org/SRT-ZIP/SRTM_V41/SRTM_Data_GeoTiff"; private Directory dir; private DAType daType = DAType.MMAP; - final double precision = 1e7; - private final double invPrecision = 1 / precision; - private final int degree = 5; private boolean calcMean = false; private boolean autoRemoveTemporary = true; private long sleep = 2000; + public static void main(String[] args) { + CGIARProvider provider = new CGIARProvider(); + + System.out.println(provider.getEle(46, -20)); + + // 337.0 + System.out.println(provider.getEle(49.949784, 11.57517)); + // 453.0 + System.out.println(provider.getEle(49.968668, 11.575127)); + // 447.0 + System.out.println(provider.getEle(49.968682, 11.574842)); + + // 3131 + System.out.println(provider.getEle(-22.532854, -65.110474)); + + // 123 + System.out.println(provider.getEle(38.065392, -87.099609)); + + // 1615 + System.out.println(provider.getEle(40, -105.2277023)); + System.out.println(provider.getEle(39.99999999, -105.2277023)); + System.out.println(provider.getEle(39.9999999, -105.2277023)); + // 1617 + System.out.println(provider.getEle(39.999999, -105.2277023)); + + // 0 + System.out.println(provider.getEle(29.840644, -42.890625)); + } + @Override - public void setCalcMean( boolean eleCalcMean ) - { + public void setCalcMean(boolean eleCalcMean) { calcMean = eleCalcMean; } - void setSleep( long sleep ) - { + void setSleep(long sleep) { this.sleep = sleep; } @@ -87,39 +116,32 @@ void setSleep( long sleep ) * our DataAccess object, so this option can be used to disable the default clear mechanism via * specifying 'false'. */ - public void setAutoRemoveTemporaryFiles( boolean autoRemoveTemporary ) - { + public void setAutoRemoveTemporaryFiles(boolean autoRemoveTemporary) { this.autoRemoveTemporary = autoRemoveTemporary; } - public void setDownloader( Downloader downloader ) - { + public void setDownloader(Downloader downloader) { this.downloader = downloader; } @Override - public ElevationProvider setCacheDir( File cacheDir ) - { + public ElevationProvider setCacheDir(File cacheDir) { if (cacheDir.exists() && !cacheDir.isDirectory()) throw new IllegalArgumentException("Cache path has to be a directory"); - try - { + try { this.cacheDir = cacheDir.getCanonicalFile(); - } catch (IOException ex) - { + } catch (IOException ex) { throw new RuntimeException(ex); } return this; } - protected File getCacheDir() - { + protected File getCacheDir() { return cacheDir; } @Override - public ElevationProvider setBaseURL( String baseUrl ) - { + public ElevationProvider setBaseURL(String baseUrl) { if (baseUrl == null || baseUrl.isEmpty()) throw new IllegalArgumentException("baseUrl cannot be empty"); @@ -128,15 +150,13 @@ public ElevationProvider setBaseURL( String baseUrl ) } @Override - public ElevationProvider setDAType( DAType daType ) - { + public ElevationProvider setDAType(DAType daType) { this.daType = daType; return this; } @Override - public double getEle( double lat, double lon ) - { + public double getEle(double lat, double lon) { // no data we can avoid the trouble if (lat > 60 || lat < -60) return 0; @@ -145,8 +165,7 @@ public double getEle( double lat, double lon ) lon = (int) (lon * precision) / precision; String name = getFileName(lat, lon); HeightTile demProvider = cacheData.get(name); - if (demProvider == null) - { + if (demProvider == null) { if (!cacheDir.exists()) cacheDir.mkdirs(); @@ -160,41 +179,32 @@ public double getEle( double lat, double lon ) DataAccess heights = getDirectory().find(name + ".gh"); demProvider.setHeights(heights); boolean loadExisting = false; - try - { + try { loadExisting = heights.loadExisting(); - } catch (Exception ex) - { + } catch (Exception ex) { logger.warn("cannot load " + name + ", error: " + ex.getMessage()); } - if (!loadExisting) - { + if (!loadExisting) { String tifName = name + ".tif"; String zippedURL = baseUrl + "/" + name + ".zip"; File file = new File(cacheDir, new File(zippedURL).getName()); // get zip file if not already in cacheDir - unzip later and in-memory only! - if (!file.exists()) - { - try - { + if (!file.exists()) { + try { int max = 3; - for (int trial = 0; trial < max; trial++) - { - try - { + for (int trial = 0; trial < max; trial++) { + try { downloader.downloadFile(zippedURL, file.getAbsolutePath()); break; - } catch (SocketTimeoutException ex) - { + } catch (SocketTimeoutException ex) { // just try again after a little nap Thread.sleep(sleep); if (trial >= max - 1) throw ex; continue; - } catch (IOException ex) - { + } catch (IOException ex) { demProvider.setSeaLevel(true); // use small size on disc and in-memory heights.setSegmentSize(100).create(10). @@ -202,8 +212,7 @@ public double getEle( double lat, double lon ) return 0; } } - } catch (Exception ex) - { + } catch (Exception ex) { throw new RuntimeException(ex); } } @@ -215,39 +224,32 @@ public double getEle( double lat, double lon ) // decode tiff data Raster raster; SeekableStream ss = null; - try - { + try { InputStream is = new FileInputStream(file); ZipInputStream zis = new ZipInputStream(is); // find tif file in zip ZipEntry entry = zis.getNextEntry(); - while (entry != null && !entry.getName().equals(tifName)) - { + while (entry != null && !entry.getName().equals(tifName)) { entry = zis.getNextEntry(); } ss = SeekableStream.wrapInputStream(zis, true); TIFFImageDecoder imageDecoder = new TIFFImageDecoder(ss, new TIFFDecodeParam()); raster = imageDecoder.decodeAsRaster(); - } catch (Exception e) - { + } catch (Exception e) { throw new RuntimeException("Can't decode " + tifName, e); - } finally - { + } finally { if (ss != null) Helper.close(ss); } - // logger.info("start converting to our format"); + // logger.info("start converting to our format"); final int height = raster.getHeight(); final int width = raster.getWidth(); int x = 0, y = 0; - try - { - for (y = 0; y < height; y++) - { - for (x = 0; x < width; x++) - { + try { + for (y = 0; y < height; y++) { + for (x = 0; x < width; x++) { short val = (short) raster.getPixel(x, y, (int[]) null)[0]; if (val < -1000 || val > 12000) val = Short.MIN_VALUE; @@ -258,8 +260,7 @@ public double getEle( double lat, double lon ) heights.flush(); // TODO remove tifName and zip? - } catch (Exception ex) - { + } catch (Exception ex) { throw new RuntimeException("Problem at x:" + x + ", y:" + y, ex); } } // loadExisting @@ -271,8 +272,7 @@ public double getEle( double lat, double lon ) return demProvider.getHeight(lat, lon); } - int down( double val ) - { + int down(double val) { // 'rounding' to closest 5 int intVal = (int) (val / degree) * degree; if (!(val >= 0 || intVal - val < invPrecision)) @@ -281,8 +281,7 @@ int down( double val ) return intVal; } - protected String getFileName( double lat, double lon ) - { + protected String getFileName(double lat, double lon) { lon = 1 + (180 + lon) / degree; int lonInt = (int) lon; lat = 1 + (60 - lat) / degree; @@ -303,8 +302,7 @@ protected String getFileName( double lat, double lon ) } @Override - public void release() - { + public void release() { cacheData.clear(); // for memory mapped type we create temporary unpacked files which should be removed @@ -313,47 +311,15 @@ public void release() } @Override - public String toString() - { + public String toString() { return "CGIAR"; } - private Directory getDirectory() - { + private Directory getDirectory() { if (dir != null) return dir; logger.info(this.toString() + " Elevation Provider, from: " + baseUrl + ", to: " + cacheDir + ", as: " + daType); return dir = new GHDirectory(cacheDir.getAbsolutePath(), daType); } - - public static void main( String[] args ) - { - CGIARProvider provider = new CGIARProvider(); - - System.out.println(provider.getEle(46, -20)); - - // 337.0 - System.out.println(provider.getEle(49.949784, 11.57517)); - // 453.0 - System.out.println(provider.getEle(49.968668, 11.575127)); - // 447.0 - System.out.println(provider.getEle(49.968682, 11.574842)); - - // 3131 - System.out.println(provider.getEle(-22.532854, -65.110474)); - - // 123 - System.out.println(provider.getEle(38.065392, -87.099609)); - - // 1615 - System.out.println(provider.getEle(40, -105.2277023)); - System.out.println(provider.getEle(39.99999999, -105.2277023)); - System.out.println(provider.getEle(39.9999999, -105.2277023)); - // 1617 - System.out.println(provider.getEle(39.999999, -105.2277023)); - - // 0 - System.out.println(provider.getEle(29.840644, -42.890625)); - } } diff --git a/core/src/main/java/com/graphhopper/reader/dem/ElevationProvider.java b/core/src/main/java/com/graphhopper/reader/dem/ElevationProvider.java index 66f29103b4c..5afa5794fa9 100644 --- a/core/src/main/java/com/graphhopper/reader/dem/ElevationProvider.java +++ b/core/src/main/java/com/graphhopper/reader/dem/ElevationProvider.java @@ -24,75 +24,67 @@ /** * @author Peter Karich */ -public interface ElevationProvider -{ +public interface ElevationProvider { + ElevationProvider NOOP = new ElevationProvider() { + @Override + public double getEle(double lat, double lon) { + return Double.NaN; + } + + @Override + public ElevationProvider setCacheDir(File cacheDir) { + return this; + } + + @Override + public ElevationProvider setBaseURL(String baseURL) { + return this; + } + + @Override + public ElevationProvider setDAType(DAType daType) { + return this; + } + + @Override + public void release() { + } + + @Override + public void setCalcMean(boolean eleCalcMean) { + } + }; + /** * @return returns the hight in meter or Double.NaN if invalid */ - double getEle( double lat, double lon ); + double getEle(double lat, double lon); /** * Specifies the service URL where to download the elevation data. An empty string should set it * to the default URL. Default is a provider-dependent URL which should work out of the box. */ - ElevationProvider setBaseURL( String baseURL ); + ElevationProvider setBaseURL(String baseURL); /** * Specifies the directory where to temporarily store the elevation data after fetched from base * URL. Default is a custom provider-dependent subdirectory in '/tmp' */ - ElevationProvider setCacheDir( File cacheDir ); + ElevationProvider setCacheDir(File cacheDir); /** * Set to true if you have a small area and need high speed access. Default is DAType.MMAP */ - ElevationProvider setDAType( DAType daType ); + ElevationProvider setDAType(DAType daType); /** * Configuration option to include surrounding elevation points when fetching the elevation. Has * only an effect if called before the first getEle call. Turned off by default. */ - void setCalcMean( boolean calcMean ); + void setCalcMean(boolean calcMean); /** * Release resources. */ void release(); - - ElevationProvider NOOP = new ElevationProvider() - { - @Override - public double getEle( double lat, double lon ) - { - return Double.NaN; - } - - @Override - public ElevationProvider setCacheDir( File cacheDir ) - { - return this; - } - - @Override - public ElevationProvider setBaseURL( String baseURL ) - { - return this; - } - - @Override - public ElevationProvider setDAType( DAType daType ) - { - return this; - } - - @Override - public void release() - { - } - - @Override - public void setCalcMean( boolean eleCalcMean ) - { - } - }; } diff --git a/core/src/main/java/com/graphhopper/reader/dem/HeightTile.java b/core/src/main/java/com/graphhopper/reader/dem/HeightTile.java index e665441697b..7fb681172ea 100644 --- a/core/src/main/java/com/graphhopper/reader/dem/HeightTile.java +++ b/core/src/main/java/com/graphhopper/reader/dem/HeightTile.java @@ -19,32 +19,30 @@ import com.graphhopper.storage.DataAccess; -import java.awt.Color; -import java.awt.Graphics; +import javax.imageio.ImageIO; +import java.awt.*; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.util.concurrent.atomic.AtomicInteger; -import javax.imageio.ImageIO; /** * One rectangle of height data from Shuttle Radar Topography Mission. *

+ * * @author Peter Karich */ -public class HeightTile -{ - private DataAccess heights; +public class HeightTile { private final int minLat; private final int minLon; private final int width; private final int degree; private final double lowerBound; private final double higherBound; + private DataAccess heights; private boolean calcMean; - public HeightTile( int minLat, int minLon, int width, double precision, int degree ) - { + public HeightTile(int minLat, int minLon, int width, double precision, int degree) { this.minLat = minLat; this.minLon = minLon; this.width = width; @@ -55,30 +53,25 @@ public HeightTile( int minLat, int minLon, int width, double precision, int degr this.degree = degree; } - public HeightTile setCalcMean( boolean b ) - { + public HeightTile setCalcMean(boolean b) { this.calcMean = b; return this; } - public HeightTile setSeaLevel( boolean b ) - { - heights.setHeader(0, b ? 1 : 0); - return this; + public boolean isSeaLevel() { + return heights.getHeader(0) == 1; } - public boolean isSeaLevel() - { - return heights.getHeader(0) == 1; + public HeightTile setSeaLevel(boolean b) { + heights.setHeader(0, b ? 1 : 0); + return this; } - void setHeights( DataAccess da ) - { + void setHeights(DataAccess da) { this.heights = da; } - public double getHeight( double lat, double lon ) - { + public double getHeight(double lat, double lon) { double deltaLat = Math.abs(lat - minLat); double deltaLon = Math.abs(lon - minLon); if (deltaLat > higherBound || deltaLat < lowerBound) @@ -103,8 +96,7 @@ public double getHeight( double lat, double lon ) if (value == Short.MIN_VALUE) return Double.NaN; - if (calcMean) - { + if (calcMean) { if (lonSimilar > 0) value += includePoint(daPointer - 2, counter); @@ -121,8 +113,7 @@ public double getHeight( double lat, double lon ) return (double) value / counter.get(); } - private double includePoint( int pointer, AtomicInteger counter ) - { + private double includePoint(int pointer, AtomicInteger counter) { short value = heights.getShort(pointer); if (value == Short.MIN_VALUE) return 0; @@ -131,31 +122,25 @@ private double includePoint( int pointer, AtomicInteger counter ) return value; } - public void toImage( String imageFile ) throws IOException - { + public void toImage(String imageFile) throws IOException { ImageIO.write(makeARGB(), "PNG", new File(imageFile)); } - protected BufferedImage makeARGB() - { + protected BufferedImage makeARGB() { int height = width; BufferedImage argbImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); Graphics g = argbImage.getGraphics(); long len = width * width; - for (int i = 0; i < len; i++) - { + for (int i = 0; i < len; i++) { int lonSimilar = i % width; // no need for width - y as coordinate system for Graphics is already this way int latSimilar = i / width; int green = Math.abs(heights.getShort(i * 2)); - if (green == 0) - { + if (green == 0) { g.setColor(new Color(255, 0, 0, 255)); - } else - { + } else { int red = 0; - while (green > 255) - { + while (green > 255) { green = green / 10; red += 50; } @@ -169,8 +154,7 @@ protected BufferedImage makeARGB() return argbImage; } - public BufferedImage getImageFromArray( int[] pixels, int width ) - { + public BufferedImage getImageFromArray(int[] pixels, int width) { int height = width; BufferedImage tmpImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB_PRE); tmpImage.setRGB(0, 0, width, height, pixels, 0, width); @@ -178,8 +162,7 @@ public BufferedImage getImageFromArray( int[] pixels, int width ) } @Override - public String toString() - { + public String toString() { return minLat + "," + minLon; } } diff --git a/core/src/main/java/com/graphhopper/reader/dem/SRTMProvider.java b/core/src/main/java/com/graphhopper/reader/dem/SRTMProvider.java index 8f56703ea7e..88c67bc64de 100644 --- a/core/src/main/java/com/graphhopper/reader/dem/SRTMProvider.java +++ b/core/src/main/java/com/graphhopper/reader/dem/SRTMProvider.java @@ -40,57 +40,54 @@ * to the geometric center of the lower left sample, which in the case of SRTM3 data will be about * 90 meters in extent. *

+ * * @author Peter Karich */ -public class SRTMProvider implements ElevationProvider -{ - public static void main( String[] args ) throws IOException - { - SRTMProvider provider = new SRTMProvider(); - // 1046 - System.out.println(provider.getEle(47.468668, 14.575127)); - // 1113 - System.out.println(provider.getEle(47.467753, 14.573911)); - - // 1946 - System.out.println(provider.getEle(46.468835, 12.578777)); - - // 845 - System.out.println(provider.getEle(48.469123, 9.576393)); - - // 1113 vs new: - provider.setCalcMean(true); - System.out.println(provider.getEle(47.467753, 14.573911)); - } - +public class SRTMProvider implements ElevationProvider { private static final BitUtil BIT_UTIL = BitUtil.BIG; private final Logger logger = LoggerFactory.getLogger(getClass()); private final int DEFAULT_WIDTH = 1201; private final int WIDTH_BYTE_INDEX = 0; - private Directory dir; - private DAType daType = DAType.MMAP; - private Downloader downloader = new Downloader("GraphHopper SRTMReader").setTimeout(10000); - private File cacheDir = new File("/tmp/srtm"); // use a map as an array is not quite useful if we want to hold only parts of the world private final TIntObjectHashMap cacheData = new TIntObjectHashMap(); private final TIntObjectHashMap areas = new TIntObjectHashMap(); private final double precision = 1e7; private final double invPrecision = 1 / precision; + private Directory dir; + private DAType daType = DAType.MMAP; + private Downloader downloader = new Downloader("GraphHopper SRTMReader").setTimeout(10000); + private File cacheDir = new File("/tmp/srtm"); // possible alternatives see #451 // http://mirror.ufs.ac.za/datasets/SRTM3/ //"http://dds.cr.usgs.gov/srtm/version2_1/SRTM3/" private String baseUrl = "https://srtm.kurviger.de/SRTM3/"; private boolean calcMean = false; - public SRTMProvider() - { + public SRTMProvider() { // move to explicit calls? init(); } + public static void main(String[] args) throws IOException { + SRTMProvider provider = new SRTMProvider(); + // 1046 + System.out.println(provider.getEle(47.468668, 14.575127)); + // 1113 + System.out.println(provider.getEle(47.467753, 14.573911)); + + // 1946 + System.out.println(provider.getEle(46.468835, 12.578777)); + + // 845 + System.out.println(provider.getEle(48.469123, 9.576393)); + + // 1113 vs new: + provider.setCalcMean(true); + System.out.println(provider.getEle(47.467753, 14.573911)); + } + @Override - public void setCalcMean( boolean calcMean ) - { + public void setCalcMean(boolean calcMean) { this.calcMean = calcMean; } @@ -98,19 +95,12 @@ public void setCalcMean( boolean calcMean ) * The URLs are a bit ugly and so we need to find out which area name a certain lat,lon * coordinate has. */ - private SRTMProvider init() - { - try - { - String strs[] = - { - "Africa", "Australia", "Eurasia", "Islands", "North_America", "South_America" - }; - for (String str : strs) - { + private SRTMProvider init() { + try { + String strs[] = {"Africa", "Australia", "Eurasia", "Islands", "North_America", "South_America"}; + for (String str : strs) { InputStream is = getClass().getResourceAsStream(str + "_names.txt"); - for (String line : Helper.readFile(new InputStreamReader(is, Helper.UTF_CS))) - { + for (String line : Helper.readFile(new InputStreamReader(is, Helper.UTF_CS))) { int lat = Integer.parseInt(line.substring(1, 3)); if (line.substring(0, 1).charAt(0) == 'S') lat = -lat; @@ -126,43 +116,36 @@ private SRTMProvider init() } } return this; - } catch (Exception ex) - { + } catch (Exception ex) { throw new IllegalStateException("Cannot load area names from classpath", ex); } } // use int key instead of string for lower memory usage - private int calcIntKey( double lat, double lon ) - { + private int calcIntKey(double lat, double lon) { // we could use LinearKeyAlgo but this is simpler as we only need integer precision: return (down(lat) + 90) * 1000 + down(lon) + 180; } - public void setDownloader( Downloader downloader ) - { + public void setDownloader(Downloader downloader) { this.downloader = downloader; } @Override - public ElevationProvider setCacheDir( File cacheDir ) - { + public ElevationProvider setCacheDir(File cacheDir) { if (cacheDir.exists() && !cacheDir.isDirectory()) throw new IllegalArgumentException("Cache path has to be a directory"); - try - { + try { this.cacheDir = cacheDir.getCanonicalFile(); - } catch (IOException ex) - { + } catch (IOException ex) { throw new RuntimeException(ex); } return this; } @Override - public ElevationProvider setBaseURL( String baseUrl ) - { + public ElevationProvider setBaseURL(String baseUrl) { if (baseUrl == null || baseUrl.isEmpty()) throw new IllegalArgumentException("baseUrl cannot be empty"); @@ -171,22 +154,19 @@ public ElevationProvider setBaseURL( String baseUrl ) } @Override - public ElevationProvider setDAType( DAType daType ) - { + public ElevationProvider setDAType(DAType daType) { this.daType = daType; return this; } - int down( double val ) - { + int down(double val) { int intVal = (int) val; if (val >= 0 || intVal - val < invPrecision) return intVal; return intVal - 1; } - String getFileString( double lat, double lon ) - { + String getFileString(double lat, double lon) { int intKey = calcIntKey(lat, lon); String str = areas.get(intKey); if (str == null) @@ -218,8 +198,7 @@ String getFileString( double lat, double lon ) } @Override - public double getEle( double lat, double lon ) - { + public double getEle(double lat, double lon) { lat = (int) (lat * precision) / precision; lon = (int) (lon * precision) / precision; int intKey = calcIntKey(lat, lon); @@ -236,11 +215,9 @@ public double getEle( double lat, double lon ) DataAccess heights = getDirectory().find("dem" + intKey); boolean loadExisting = false; - try - { + try { loadExisting = heights.loadExisting(); - } catch (Exception ex) - { + } catch (Exception ex) { logger.warn("cannot load dem" + intKey + ", error:" + ex.getMessage()); } @@ -258,14 +235,11 @@ public double getEle( double lat, double lon ) return demProvider.getHeight(lat, lon); } - private void updateHeightsFromZipFile( String fileDetails, DataAccess heights ) throws RuntimeException - { - try - { + private void updateHeightsFromZipFile(String fileDetails, DataAccess heights) throws RuntimeException { + try { byte[] bytes = getByteArrayFromZipFile(fileDetails); heights.create(bytes.length); - for (int bytePos = 0; bytePos < bytes.length; bytePos += 2) - { + for (int bytePos = 0; bytePos < bytes.length; bytePos += 2) { short val = BIT_UTIL.toShort(bytes, bytePos); if (val < -1000 || val > 12000) val = Short.MIN_VALUE; @@ -275,27 +249,22 @@ private void updateHeightsFromZipFile( String fileDetails, DataAccess heights ) heights.setHeader(WIDTH_BYTE_INDEX, bytes.length / 2); heights.flush(); - } catch (Exception ex) - { + } catch (Exception ex) { throw new RuntimeException(ex); } } - private byte[] getByteArrayFromZipFile( String fileDetails ) throws InterruptedException, FileNotFoundException, IOException - { + private byte[] getByteArrayFromZipFile(String fileDetails) throws InterruptedException, FileNotFoundException, IOException { String zippedURL = baseUrl + "/" + fileDetails + ".hgt.zip"; File file = new File(cacheDir, new File(zippedURL).getName()); InputStream is; // get zip file if not already in cacheDir if (!file.exists()) - for (int i = 0; i < 3; i++) - { - try - { + for (int i = 0; i < 3; i++) { + try { downloader.downloadFile(zippedURL, file.getAbsolutePath()); break; - } catch (SocketTimeoutException ex) - { + } catch (SocketTimeoutException ex) { // just try again after a little nap Thread.sleep(2000); continue; @@ -309,8 +278,7 @@ private byte[] getByteArrayFromZipFile( String fileDetails ) throws InterruptedE ByteArrayOutputStream os = new ByteArrayOutputStream(); byte[] buffer = new byte[0xFFFF]; int len; - while ((len = buff.read(buffer)) > 0) - { + while ((len = buff.read(buffer)) > 0) { os.write(buffer, 0, len); } os.flush(); @@ -319,8 +287,7 @@ private byte[] getByteArrayFromZipFile( String fileDetails ) throws InterruptedE } @Override - public void release() - { + public void release() { cacheData.clear(); // for memory mapped type we create temporary unpacked files which should be removed @@ -329,13 +296,11 @@ public void release() } @Override - public String toString() - { + public String toString() { return "SRTM"; } - private Directory getDirectory() - { + private Directory getDirectory() { if (dir != null) return dir; diff --git a/core/src/main/java/com/graphhopper/reader/osm/conditional/ConditionalOSMTagInspector.java b/core/src/main/java/com/graphhopper/reader/osm/conditional/ConditionalOSMTagInspector.java index 58975322d17..db9d1dd2aaa 100644 --- a/core/src/main/java/com/graphhopper/reader/osm/conditional/ConditionalOSMTagInspector.java +++ b/core/src/main/java/com/graphhopper/reader/osm/conditional/ConditionalOSMTagInspector.java @@ -19,37 +19,35 @@ import com.graphhopper.reader.ConditionalTagInspector; import com.graphhopper.reader.ReaderWay; -import java.util.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.*; + /** * Inspects the conditional tags of an OSMWay according to the given conditional tags. *

+ * * @author Robin Boldt */ -public class ConditionalOSMTagInspector implements ConditionalTagInspector -{ +public class ConditionalOSMTagInspector implements ConditionalTagInspector { private final Logger logger = LoggerFactory.getLogger(getClass()); - // enabling by default makes noise but could improve OSM data - private boolean enabledLogs = true; private final List tagsToCheck; private final Map valueMap; private final ConditionalParser permitParser, restrictiveParser; + // enabling by default makes noise but could improve OSM data + private boolean enabledLogs = true; - public ConditionalOSMTagInspector( Object value, List tagsToCheck, - Set restrictiveValues, Set permittedValues ) - { + public ConditionalOSMTagInspector(Object value, List tagsToCheck, + Set restrictiveValues, Set permittedValues) { this(tagsToCheck, createDefaultMapping(value), restrictiveValues, permittedValues, false); } - public ConditionalOSMTagInspector( List tagsToCheck, Map valueMap, - Set restrictiveValues, Set permittedValues, boolean enabledLogs ) - { + public ConditionalOSMTagInspector(List tagsToCheck, Map valueMap, + Set restrictiveValues, Set permittedValues, boolean enabledLogs) { this.valueMap = valueMap; this.tagsToCheck = new ArrayList<>(tagsToCheck.size()); - for (String tagToCheck : tagsToCheck) - { + for (String tagToCheck : tagsToCheck) { this.tagsToCheck.add(tagToCheck + ":conditional"); } @@ -61,8 +59,7 @@ public ConditionalOSMTagInspector( List tagsToCheck, Map this.restrictiveParser = new ConditionalParser(restrictiveValues, logUnsupportedFeatures); } - static Map createDefaultMapping( Object value ) - { + static Map createDefaultMapping(Object value) { // parse date range and value is the time Map map = new HashMap(1); map.put(DateRange.KEY, value); @@ -70,44 +67,36 @@ static Map createDefaultMapping( Object value ) } @Override - public boolean isRestrictedWayConditionallyPermitted( ReaderWay way ) - { + public boolean isRestrictedWayConditionallyPermitted(ReaderWay way) { return applies(way, true); } @Override - public boolean isPermittedWayConditionallyRestricted( ReaderWay way ) - { + public boolean isPermittedWayConditionallyRestricted(ReaderWay way) { return applies(way, false); } - protected boolean applies( ReaderWay way, boolean checkPermissiveValues ) - { - for (int index = 0; index < tagsToCheck.size(); index++) - { + protected boolean applies(ReaderWay way, boolean checkPermissiveValues) { + for (int index = 0; index < tagsToCheck.size(); index++) { String tagToCheck = tagsToCheck.get(index); String val = way.getTag(tagToCheck); if (val == null || val.isEmpty()) continue; - try - { + try { ValueRange valueRange; if (checkPermissiveValues) valueRange = permitParser.getRange(val); else valueRange = restrictiveParser.getRange(val); - if (valueRange != null) - { + if (valueRange != null) { Object value = valueMap.get(valueRange.getKey()); if (value != null && valueRange.isInRange(value)) return true; } - } catch (Exception e) - { - if (enabledLogs) - { + } catch (Exception e) { + if (enabledLogs) { // log only if no date ala 21:00 as currently date and numbers do not support time precise restrictions if (!val.contains(":")) logger.warn(way.getId() + " - could not parse the conditional value:" + val + " of tag:" + tagToCheck + ". Exception:" + e.getMessage()); diff --git a/core/src/main/java/com/graphhopper/reader/osm/conditional/ConditionalParser.java b/core/src/main/java/com/graphhopper/reader/osm/conditional/ConditionalParser.java index ae466ed5b31..b39009d4bcf 100644 --- a/core/src/main/java/com/graphhopper/reader/osm/conditional/ConditionalParser.java +++ b/core/src/main/java/com/graphhopper/reader/osm/conditional/ConditionalParser.java @@ -27,33 +27,29 @@ * Parses a conditional tag according to * http://wiki.openstreetmap.org/wiki/Conditional_restrictions. *

+ * * @author Robin Boldt */ -public class ConditionalParser -{ +public class ConditionalParser { private final Logger logger = LoggerFactory.getLogger(getClass()); private final Set restrictedTags; private final boolean enabledLogs; - public ConditionalParser( Set restrictedTags ) - { + public ConditionalParser(Set restrictedTags) { this(restrictedTags, false); } - public ConditionalParser( Set restrictedTags, boolean enabledLogs ) - { + public ConditionalParser(Set restrictedTags, boolean enabledLogs) { // use map => key & type (date vs. double) this.restrictedTags = restrictedTags; this.enabledLogs = enabledLogs; } - public ValueRange getRange( String conditionalTag ) throws ParseException - { + public ValueRange getRange(String conditionalTag) throws ParseException { if (conditionalTag == null || conditionalTag.isEmpty() || !conditionalTag.contains("@")) return null; - if (conditionalTag.contains(";")) - { + if (conditionalTag.contains(";")) { if (enabledLogs) logger.warn("We do not support multiple conditions yet: " + conditionalTag); return null; @@ -74,50 +70,42 @@ public ValueRange getRange( String conditionalTag ) throws ParseException conditional = conditional.trim(); int index = conditional.indexOf(">"); - if (index > 0 && conditional.length() > 2) - { + if (index > 0 && conditional.length() > 2) { final String key = conditional.substring(0, index).trim(); // for now just ignore equals sign if (conditional.charAt(index + 1) == '=') index++; final double value = parseNumber(conditional.substring(index + 1)); - return new ValueRange() - { + return new ValueRange() { @Override - public boolean isInRange( Number obj ) - { + public boolean isInRange(Number obj) { return obj.doubleValue() > value; } @Override - public String getKey() - { + public String getKey() { return key; } }; } index = conditional.indexOf("<"); - if (index > 0 && conditional.length() > 2) - { + if (index > 0 && conditional.length() > 2) { final String key = conditional.substring(0, index).trim(); if (conditional.charAt(index + 1) == '=') index++; final double value = parseNumber(conditional.substring(index + 1)); - return new ValueRange() - { + return new ValueRange() { @Override - public boolean isInRange( Number obj ) - { + public boolean isInRange(Number obj) { return obj.doubleValue() < value; } @Override - public String getKey() - { + public String getKey() { return key; } }; @@ -126,11 +114,9 @@ public String getKey() return DateRangeParser.parseDateRange(conditional); } - protected double parseNumber( String str ) - { + protected double parseNumber(String str) { int untilIndex = str.length() - 1; - for (; untilIndex >= 0; untilIndex--) - { + for (; untilIndex >= 0; untilIndex--) { if (Character.isDigit(str.charAt(untilIndex))) break; } diff --git a/core/src/main/java/com/graphhopper/reader/osm/conditional/DateRange.java b/core/src/main/java/com/graphhopper/reader/osm/conditional/DateRange.java index be092e21115..c8e5aeb6009 100644 --- a/core/src/main/java/com/graphhopper/reader/osm/conditional/DateRange.java +++ b/core/src/main/java/com/graphhopper/reader/osm/conditional/DateRange.java @@ -18,54 +18,46 @@ package com.graphhopper.reader.osm.conditional; import com.graphhopper.util.Helper; + import java.text.DateFormat; import java.util.Calendar; /** * This class represents a date range and is able to determine if a given date is in that range. *

+ * * @author Robin Boldt */ -public class DateRange implements ValueRange -{ +public class DateRange implements ValueRange { + public static final String KEY = "DateRange"; private final Calendar from; private final Calendar to; - // Do not compare years boolean yearless = false; - boolean dayOnly = false; - boolean reverse = false; - public DateRange( ParsedCalendar from, ParsedCalendar to ) - { + public DateRange(ParsedCalendar from, ParsedCalendar to) { Calendar fromCal = from.parsedCalendar; Calendar toCal = to.parsedCalendar; // This should never happen - if (fromCal.get(Calendar.ERA) != toCal.get(Calendar.ERA)) - { + if (fromCal.get(Calendar.ERA) != toCal.get(Calendar.ERA)) { throw new IllegalArgumentException("Different ERAs are not allowed. From:" + from + " To:" + to); } - if (from.isYearless() && to.isYearless()) - { + if (from.isYearless() && to.isYearless()) { yearless = true; } - if (from.isDayOnly() && to.isDayOnly()) - { + if (from.isDayOnly() && to.isDayOnly()) { dayOnly = true; } - if (fromCal.getTimeInMillis() > toCal.getTimeInMillis()) - { - if (!yearless && !dayOnly) - { + if (fromCal.getTimeInMillis() > toCal.getTimeInMillis()) { + if (!yearless && !dayOnly) { throw new IllegalArgumentException("From after to makes no sense, except for isYearless and isDayOnly DateRanges. From:" + from + " To:" + to); - } else - { + } else { reverse = true; } } @@ -74,28 +66,21 @@ public DateRange( ParsedCalendar from, ParsedCalendar to ) this.to = to.getMax(); } - public static final String KEY = "DateRange"; - @Override - public String getKey() - { + public String getKey() { return KEY; } @Override - public boolean isInRange( Calendar date ) - { + public boolean isInRange(Calendar date) { if (!yearless && !dayOnly) return date.after(from) && date.before(to); - if (dayOnly) - { + if (dayOnly) { int currentDayOfWeek = date.get(Calendar.DAY_OF_WEEK); - if (reverse) - { + if (reverse) { return from.get(Calendar.DAY_OF_WEEK) <= currentDayOfWeek || currentDayOfWeek <= to.get(Calendar.DAY_OF_WEEK); - } else - { + } else { return from.get(Calendar.DAY_OF_WEEK) <= currentDayOfWeek && currentDayOfWeek <= to.get(Calendar.DAY_OF_WEEK); } } @@ -106,26 +91,22 @@ public boolean isInRange( Calendar date ) return isInRangeYearless(date); } - private boolean isInRangeYearless( Calendar date ) - { + private boolean isInRangeYearless(Calendar date) { if (from.get(Calendar.MONTH) < date.get(Calendar.MONTH) && date.get(Calendar.MONTH) < to.get(Calendar.MONTH)) return true; - if (from.get(Calendar.MONTH) == date.get(Calendar.MONTH) && to.get(Calendar.MONTH) == date.get(Calendar.MONTH)) - { + if (from.get(Calendar.MONTH) == date.get(Calendar.MONTH) && to.get(Calendar.MONTH) == date.get(Calendar.MONTH)) { if (from.get(Calendar.DAY_OF_MONTH) <= date.get(Calendar.DAY_OF_MONTH) && date.get(Calendar.DAY_OF_MONTH) <= to.get(Calendar.DAY_OF_MONTH)) return true; else return false; } - if (from.get(Calendar.MONTH) == date.get(Calendar.MONTH)) - { + if (from.get(Calendar.MONTH) == date.get(Calendar.MONTH)) { if (from.get(Calendar.DAY_OF_MONTH) <= date.get(Calendar.DAY_OF_MONTH)) return true; else return false; } - if (to.get(Calendar.MONTH) == date.get(Calendar.MONTH)) - { + if (to.get(Calendar.MONTH) == date.get(Calendar.MONTH)) { if (date.get(Calendar.DAY_OF_MONTH) <= to.get(Calendar.DAY_OF_MONTH)) return true; else @@ -134,28 +115,24 @@ private boolean isInRangeYearless( Calendar date ) return false; } - private boolean isInRangeYearlessReverse( Calendar date ) - { + private boolean isInRangeYearlessReverse(Calendar date) { int currMonth = date.get(Calendar.MONTH); if (from.get(Calendar.MONTH) < currMonth || currMonth < to.get(Calendar.MONTH)) return true; - if (from.get(Calendar.MONTH) == currMonth && to.get(Calendar.MONTH) == currMonth) - { + if (from.get(Calendar.MONTH) == currMonth && to.get(Calendar.MONTH) == currMonth) { if (from.get(Calendar.DAY_OF_MONTH) < date.get(Calendar.DAY_OF_MONTH) || date.get(Calendar.DAY_OF_MONTH) < to.get(Calendar.DAY_OF_MONTH)) return true; else return false; } - if (from.get(Calendar.MONTH) == currMonth) - { + if (from.get(Calendar.MONTH) == currMonth) { if (from.get(Calendar.DAY_OF_MONTH) <= date.get(Calendar.DAY_OF_MONTH)) return true; else return false; } - if (to.get(Calendar.MONTH) == currMonth) - { + if (to.get(Calendar.MONTH) == currMonth) { if (date.get(Calendar.DAY_OF_MONTH) <= to.get(Calendar.DAY_OF_MONTH)) return true; else @@ -165,8 +142,7 @@ private boolean isInRangeYearlessReverse( Calendar date ) } @Override - public String toString() - { + public String toString() { DateFormat f = Helper.createFormatter(); return "yearless:" + yearless + ", dayOnly:" + dayOnly + ", reverse:" + reverse + ", from:" + f.format(from.getTime()) + ", to:" + f.format(to.getTime()); diff --git a/core/src/main/java/com/graphhopper/reader/osm/conditional/DateRangeParser.java b/core/src/main/java/com/graphhopper/reader/osm/conditional/DateRangeParser.java index d86fdd422d8..a496697e54c 100644 --- a/core/src/main/java/com/graphhopper/reader/osm/conditional/DateRangeParser.java +++ b/core/src/main/java/com/graphhopper/reader/osm/conditional/DateRangeParser.java @@ -18,74 +18,66 @@ package com.graphhopper.reader.osm.conditional; import com.graphhopper.util.Helper; -import static com.graphhopper.util.Helper.createFormatter; + import java.text.DateFormat; import java.text.ParseException; -import java.util.*; +import java.util.Arrays; +import java.util.Calendar; +import java.util.List; +import java.util.Locale; + +import static com.graphhopper.util.Helper.createFormatter; /** * Parses a DateRange from OpenStreetMap. Currently only DateRanges that last at least one day are * supported. The Syntax is allowed inputs is described here: * http://wiki.openstreetmap.org/wiki/Key:opening_hours. *

+ * * @author Robin Boldt */ -public class DateRangeParser -{ +public class DateRangeParser { private static final DateFormat YEAR_MONTH_DAY_DF = createFormatter("yyyy MMM dd"); private static final DateFormat MONTH_DAY_DF = createFormatter("MMM dd"); private static final DateFormat MONTH_DAY2_DF = createFormatter("dd.MM"); private static final DateFormat YEAR_MONTH_DF = createFormatter("yyyy MMM"); private static final DateFormat MONTH_DF = createFormatter("MMM"); - private static final List DAY_NAMES = Arrays.asList(new String[] - { + private static final List DAY_NAMES = Arrays.asList(new String[]{ "Su", "Mo", "Tu", "We", "Th", "Fr", "Sa" }); - public static Calendar createCalendar() - { + public static Calendar createCalendar() { // Use locale US as exception here (instead of UK) to match week order "Su-Sa" used in Calendar for day_of_week. // Inconsistent but we should not use US for other date handling stuff like strange default formatting, related to #647. return Calendar.getInstance(Helper.UTC, Locale.US); } - static ParsedCalendar parseDateString( String dateString ) throws ParseException - { + static ParsedCalendar parseDateString(String dateString) throws ParseException { // Replace occurences of public holidays dateString = dateString.replaceAll("(,( )*)?(PH|SH)", ""); dateString = dateString.trim(); Calendar calendar = createCalendar(); ParsedCalendar parsedCalendar; - try - { + try { calendar.setTime(YEAR_MONTH_DAY_DF.parse(dateString)); parsedCalendar = new ParsedCalendar(ParsedCalendar.ParseType.YEAR_MONTH_DAY, calendar); - } catch (ParseException e1) - { - try - { + } catch (ParseException e1) { + try { calendar.setTime(MONTH_DAY_DF.parse(dateString)); parsedCalendar = new ParsedCalendar(ParsedCalendar.ParseType.MONTH_DAY, calendar); - } catch (ParseException e2) - { - try - { + } catch (ParseException e2) { + try { calendar.setTime(MONTH_DAY2_DF.parse(dateString)); parsedCalendar = new ParsedCalendar(ParsedCalendar.ParseType.MONTH_DAY, calendar); - } catch (ParseException e3) - { - try - { + } catch (ParseException e3) { + try { calendar.setTime(YEAR_MONTH_DF.parse(dateString)); parsedCalendar = new ParsedCalendar(ParsedCalendar.ParseType.YEAR_MONTH, calendar); - } catch (ParseException e4) - { - try - { + } catch (ParseException e4) { + try { calendar.setTime(MONTH_DF.parse(dateString)); parsedCalendar = new ParsedCalendar(ParsedCalendar.ParseType.MONTH, calendar); - } catch (ParseException e5) - { + } catch (ParseException e5) { int index = DAY_NAMES.indexOf(dateString); if (index < 0) throw new ParseException("Unparseable date: \"" + dateString + "\"", 0); @@ -102,8 +94,7 @@ static ParsedCalendar parseDateString( String dateString ) throws ParseException return parsedCalendar; } - public static DateRange parseDateRange( String dateRangeString ) throws ParseException - { + public static DateRange parseDateRange(String dateRangeString) throws ParseException { if (dateRangeString == null || dateRangeString.isEmpty()) throw new IllegalArgumentException("Passing empty Strings is not allowed"); diff --git a/core/src/main/java/com/graphhopper/reader/osm/conditional/ParsedCalendar.java b/core/src/main/java/com/graphhopper/reader/osm/conditional/ParsedCalendar.java index 1748d02b143..1def0407da4 100644 --- a/core/src/main/java/com/graphhopper/reader/osm/conditional/ParsedCalendar.java +++ b/core/src/main/java/com/graphhopper/reader/osm/conditional/ParsedCalendar.java @@ -18,43 +18,38 @@ package com.graphhopper.reader.osm.conditional; import com.graphhopper.util.Helper; + import java.util.Calendar; /** * This class represents a parsed Date and the parse type. *

+ * * @author Robin Boldt */ -public class ParsedCalendar -{ +public class ParsedCalendar { public final ParseType parseType; public final Calendar parsedCalendar; - public ParsedCalendar( ParseType parseType, Calendar parsedCalendar ) - { + public ParsedCalendar(ParseType parseType, Calendar parsedCalendar) { this.parseType = parseType; this.parsedCalendar = parsedCalendar; } - public boolean isYearless() - { + public boolean isYearless() { return parseType == ParseType.MONTH || parseType == ParseType.MONTH_DAY; } - public boolean isDayless() - { + public boolean isDayless() { return parseType == ParseType.MONTH || parseType == ParseType.YEAR_MONTH; } - public boolean isDayOnly() - { + public boolean isDayOnly() { return parseType == ParseType.DAY; } - public Calendar getMax() - { - if (isDayless()) - { + public Calendar getMax() { + if (isDayless()) { parsedCalendar.set(Calendar.DAY_OF_MONTH, parsedCalendar.getActualMaximum(Calendar.DAY_OF_MONTH)); } parsedCalendar.set(Calendar.HOUR_OF_DAY, parsedCalendar.getActualMaximum(Calendar.HOUR_OF_DAY)); @@ -65,10 +60,8 @@ public Calendar getMax() return parsedCalendar; } - public Calendar getMin() - { - if (isDayless()) - { + public Calendar getMin() { + if (isDayless()) { parsedCalendar.set(Calendar.DAY_OF_MONTH, parsedCalendar.getActualMinimum(Calendar.DAY_OF_MONTH)); } parsedCalendar.set(Calendar.HOUR_OF_DAY, parsedCalendar.getActualMinimum(Calendar.HOUR_OF_DAY)); @@ -80,13 +73,11 @@ public Calendar getMin() } @Override - public String toString() - { + public String toString() { return parseType + "; " + Helper.createFormatter().format(parsedCalendar.getTime()); } - public enum ParseType - { + public enum ParseType { YEAR_MONTH_DAY, YEAR_MONTH, MONTH_DAY, diff --git a/core/src/main/java/com/graphhopper/reader/osm/conditional/ValueRange.java b/core/src/main/java/com/graphhopper/reader/osm/conditional/ValueRange.java index aa36053b275..5867d3e19d7 100644 --- a/core/src/main/java/com/graphhopper/reader/osm/conditional/ValueRange.java +++ b/core/src/main/java/com/graphhopper/reader/osm/conditional/ValueRange.java @@ -18,15 +18,13 @@ package com.graphhopper.reader.osm.conditional; /** - * * @author Peter Karich */ -public interface ValueRange -{ +public interface ValueRange { /** * Checks if the value is in range of this ValueRange check method. */ - boolean isInRange( T value ); + boolean isInRange(T value); String getKey(); } diff --git a/core/src/main/java/com/graphhopper/routing/AStar.java b/core/src/main/java/com/graphhopper/routing/AStar.java index 98c569130ea..f416263162b 100644 --- a/core/src/main/java/com/graphhopper/routing/AStar.java +++ b/core/src/main/java/com/graphhopper/routing/AStar.java @@ -17,22 +17,21 @@ */ package com.graphhopper.routing; -import com.graphhopper.util.DistancePlaneProjection; -import gnu.trove.map.TIntObjectMap; -import gnu.trove.map.hash.TIntObjectHashMap; - -import java.util.PriorityQueue; - import com.graphhopper.routing.util.FlagEncoder; import com.graphhopper.routing.util.TraversalMode; -import com.graphhopper.routing.weighting.Weighting; -import com.graphhopper.routing.weighting.WeightApproximator; import com.graphhopper.routing.weighting.BeelineWeightApproximator; -import com.graphhopper.storage.SPTEntry; +import com.graphhopper.routing.weighting.WeightApproximator; +import com.graphhopper.routing.weighting.Weighting; import com.graphhopper.storage.Graph; +import com.graphhopper.storage.SPTEntry; +import com.graphhopper.util.DistancePlaneProjection; import com.graphhopper.util.EdgeExplorer; import com.graphhopper.util.EdgeIterator; import com.graphhopper.util.Parameters; +import gnu.trove.map.TIntObjectMap; +import gnu.trove.map.hash.TIntObjectHashMap; + +import java.util.PriorityQueue; /** * This class implements the A* algorithm according to @@ -40,10 +39,10 @@ *

* Different distance calculations can be used via setApproximation. *

+ * * @author Peter Karich */ -public class AStar extends AbstractRoutingAlgorithm -{ +public class AStar extends AbstractRoutingAlgorithm { private WeightApproximator weightApprox; private int visitedCount; private TIntObjectMap fromMap; @@ -51,8 +50,7 @@ public class AStar extends AbstractRoutingAlgorithm private AStarEntry currEdge; private int to1 = -1; - public AStar( Graph graph, FlagEncoder encoder, Weighting weighting, TraversalMode tMode ) - { + public AStar(Graph graph, FlagEncoder encoder, Weighting weighting, TraversalMode tMode) { super(graph, encoder, weighting, tMode); int size = Math.min(Math.max(200, graph.getNodes() / 10), 2000); initCollections(size); @@ -64,40 +62,34 @@ public AStar( Graph graph, FlagEncoder encoder, Weighting weighting, TraversalMo /** * @param approx defines how distance to goal Node is approximated */ - public AStar setApproximation( WeightApproximator approx ) - { + public AStar setApproximation(WeightApproximator approx) { weightApprox = approx; return this; } - protected void initCollections( int size ) - { + protected void initCollections(int size) { fromMap = new TIntObjectHashMap(); prioQueueOpenSet = new PriorityQueue(size); } @Override - public Path calcPath( int from, int to ) - { + public Path calcPath(int from, int to) { checkAlreadyRun(); to1 = to; weightApprox.setGoalNode(to); double weightToGoal = weightApprox.approximate(from); currEdge = new AStarEntry(EdgeIterator.NO_EDGE, from, 0 + weightToGoal, 0); - if (!traversalMode.isEdgeBased()) - { + if (!traversalMode.isEdgeBased()) { fromMap.put(from, currEdge); } return runAlgo(); } - private Path runAlgo() - { + private Path runAlgo() { double currWeightToGoal, estimationFullWeight; EdgeExplorer explorer = outEdgeExplorer; - while (true) - { + while (true) { int currVertex = currEdge.adjNode; visitedCount++; if (isMaxVisitedNodesExceeded()) @@ -107,8 +99,7 @@ private Path runAlgo() break; EdgeIterator iter = explorer.setBaseNode(currVertex); - while (iter.next()) - { + while (iter.next()) { if (!accept(iter, currEdge.edge)) continue; @@ -120,16 +111,13 @@ private Path runAlgo() continue; AStarEntry ase = fromMap.get(traversalId); - if (ase == null || ase.weightOfVisitedPath > alreadyVisitedWeight) - { + if (ase == null || ase.weightOfVisitedPath > alreadyVisitedWeight) { currWeightToGoal = weightApprox.approximate(neighborNode); estimationFullWeight = alreadyVisitedWeight + currWeightToGoal; - if (ase == null) - { + if (ase == null) { ase = new AStarEntry(iter.getEdge(), neighborNode, estimationFullWeight, alreadyVisitedWeight); fromMap.put(traversalId, ase); - } else - { + } else { assert (ase.weight > 0.9999999 * estimationFullWeight) : "Inconsistent distance estimate " + ase.weight + " vs " + estimationFullWeight + " (" + ase.weight / estimationFullWeight + "), and:" + ase.weightOfVisitedPath + " vs " + alreadyVisitedWeight + " (" + ase.weightOfVisitedPath / alreadyVisitedWeight + ")"; @@ -158,49 +146,41 @@ private Path runAlgo() } @Override - protected Path extractPath() - { + protected Path extractPath() { return new Path(graph, flagEncoder).setWeight(currEdge.weight).setSPTEntry(currEdge).extract(); } @Override - protected SPTEntry createSPTEntry( int node, double weight ) - { + protected SPTEntry createSPTEntry(int node, double weight) { throw new IllegalStateException("use AStarEdge constructor directly"); } @Override - protected boolean finished() - { + protected boolean finished() { return currEdge.adjNode == to1; } @Override - public int getVisitedNodes() - { + public int getVisitedNodes() { return visitedCount; } - public static class AStarEntry extends SPTEntry - { + @Override + public String getName() { + return Parameters.Algorithms.ASTAR; + } + + public static class AStarEntry extends SPTEntry { double weightOfVisitedPath; - public AStarEntry( int edgeId, int adjNode, double weightForHeap, double weightOfVisitedPath ) - { + public AStarEntry(int edgeId, int adjNode, double weightForHeap, double weightOfVisitedPath) { super(edgeId, adjNode, weightForHeap); this.weightOfVisitedPath = weightOfVisitedPath; } @Override - public final double getWeightOfVisitedPath() - { + public final double getWeightOfVisitedPath() { return weightOfVisitedPath; } } - - @Override - public String getName() - { - return Parameters.Algorithms.ASTAR; - } } diff --git a/core/src/main/java/com/graphhopper/routing/AStarBidirection.java b/core/src/main/java/com/graphhopper/routing/AStarBidirection.java index 5cd36c4f715..ef9478570d5 100644 --- a/core/src/main/java/com/graphhopper/routing/AStarBidirection.java +++ b/core/src/main/java/com/graphhopper/routing/AStarBidirection.java @@ -17,21 +17,21 @@ */ package com.graphhopper.routing; +import com.graphhopper.routing.AStar.AStarEntry; +import com.graphhopper.routing.util.FlagEncoder; +import com.graphhopper.routing.util.TraversalMode; import com.graphhopper.routing.weighting.BeelineWeightApproximator; -import com.graphhopper.routing.weighting.WeightApproximator; import com.graphhopper.routing.weighting.ConsistentWeightApproximator; +import com.graphhopper.routing.weighting.WeightApproximator; import com.graphhopper.routing.weighting.Weighting; -import com.graphhopper.routing.util.*; +import com.graphhopper.storage.Graph; +import com.graphhopper.storage.SPTEntry; +import com.graphhopper.util.*; import gnu.trove.map.TIntObjectMap; import gnu.trove.map.hash.TIntObjectHashMap; import java.util.PriorityQueue; -import com.graphhopper.routing.AStar.AStarEntry; -import com.graphhopper.storage.SPTEntry; -import com.graphhopper.storage.Graph; -import com.graphhopper.util.*; - /** * This class implements a bidirectional A* algorithm. It is interesting to note that a * bidirectional dijkstra is far more efficient than a single direction one. The same does not hold @@ -54,23 +54,22 @@ * or could we even use this three phase approach? * www.lix.polytechnique.fr/~giacomon/papers/bidirtimedep.pdf *

+ * * @author Peter Karich * @author jansoe */ -public class AStarBidirection extends AbstractBidirAlgo -{ - private ConsistentWeightApproximator weightApprox; - private PriorityQueue prioQueueOpenSetFrom; +public class AStarBidirection extends AbstractBidirAlgo { protected TIntObjectMap bestWeightMapFrom; - private PriorityQueue prioQueueOpenSetTo; protected TIntObjectMap bestWeightMapTo; - private TIntObjectMap bestWeightMapOther; protected AStarEntry currFrom; protected AStarEntry currTo; protected PathBidirRef bestPath; + private ConsistentWeightApproximator weightApprox; + private PriorityQueue prioQueueOpenSetFrom; + private PriorityQueue prioQueueOpenSetTo; + private TIntObjectMap bestWeightMapOther; - public AStarBidirection( Graph graph, FlagEncoder encoder, Weighting weighting, TraversalMode tMode ) - { + public AStarBidirection(Graph graph, FlagEncoder encoder, Weighting weighting, TraversalMode tMode) { super(graph, encoder, weighting, tMode); int size = Math.min(Math.max(200, graph.getNodes() / 10), 2000); initCollections(size); @@ -79,8 +78,7 @@ public AStarBidirection( Graph graph, FlagEncoder encoder, Weighting weighting, setApproximation(defaultApprox); } - protected void initCollections( int size ) - { + protected void initCollections(int size) { prioQueueOpenSetFrom = new PriorityQueue(size); bestWeightMapFrom = new TIntObjectHashMap(size); @@ -91,96 +89,76 @@ protected void initCollections( int size ) /** * @param approx if true it enables approximative distance calculation from lat,lon values */ - public AStarBidirection setApproximation( WeightApproximator approx ) - { + public AStarBidirection setApproximation(WeightApproximator approx) { weightApprox = new ConsistentWeightApproximator(approx); return this; } @Override - protected SPTEntry createSPTEntry( int node, double weight ) - { + protected SPTEntry createSPTEntry(int node, double weight) { throw new IllegalStateException("use AStarEdge constructor directly"); } @Override - public void initFrom( int from, double weight ) - { + public void initFrom(int from, double weight) { currFrom = new AStarEntry(EdgeIterator.NO_EDGE, from, weight, weight); weightApprox.setSourceNode(from); prioQueueOpenSetFrom.add(currFrom); - if (currTo != null) - { + if (currTo != null) { currFrom.weight += weightApprox.approximate(currFrom.adjNode, false); currTo.weight += weightApprox.approximate(currTo.adjNode, true); } - if (!traversalMode.isEdgeBased()) - { + if (!traversalMode.isEdgeBased()) { bestWeightMapFrom.put(from, currFrom); - if (currTo != null) - { + if (currTo != null) { bestWeightMapOther = bestWeightMapTo; updateBestPath(GHUtility.getEdge(graph, from, currTo.adjNode), currTo, from); } - } else - { - if (currTo != null && currTo.adjNode == from) - { - // special case of identical start and end - bestPath.sptEntry = currFrom; - bestPath.edgeTo = currTo; - finishedFrom = true; - finishedTo = true; - } + } else if (currTo != null && currTo.adjNode == from) { + // special case of identical start and end + bestPath.sptEntry = currFrom; + bestPath.edgeTo = currTo; + finishedFrom = true; + finishedTo = true; } } @Override - public void initTo( int to, double weight ) - { + public void initTo(int to, double weight) { currTo = new AStarEntry(EdgeIterator.NO_EDGE, to, weight, weight); weightApprox.setGoalNode(to); prioQueueOpenSetTo.add(currTo); - if (currFrom != null) - { + if (currFrom != null) { currFrom.weight += weightApprox.approximate(currFrom.adjNode, false); currTo.weight += weightApprox.approximate(currTo.adjNode, true); } - if (!traversalMode.isEdgeBased()) - { + if (!traversalMode.isEdgeBased()) { bestWeightMapTo.put(to, currTo); - if (currFrom != null) - { + if (currFrom != null) { bestWeightMapOther = bestWeightMapFrom; updateBestPath(GHUtility.getEdge(graph, currFrom.adjNode, to), currFrom, to); } - } else - { - if (currFrom != null && currFrom.adjNode == to) - { - // special case of identical start and end - bestPath.sptEntry = currFrom; - bestPath.edgeTo = currTo; - finishedFrom = true; - finishedTo = true; - } + } else if (currFrom != null && currFrom.adjNode == to) { + // special case of identical start and end + bestPath.sptEntry = currFrom; + bestPath.edgeTo = currTo; + finishedFrom = true; + finishedTo = true; } } @Override - protected Path createAndInitPath() - { + protected Path createAndInitPath() { bestPath = new PathBidirRef(graph, flagEncoder); return bestPath; } @Override - protected Path extractPath() - { + protected Path extractPath() { if (finished()) return bestPath.extract(); @@ -188,20 +166,17 @@ protected Path extractPath() } @Override - protected double getCurrentFromWeight() - { + protected double getCurrentFromWeight() { return currFrom.weight; } @Override - protected double getCurrentToWeight() - { + protected double getCurrentToWeight() { return currTo.weight; } @Override - protected boolean finished() - { + protected boolean finished() { if (finishedFrom || finishedTo) return true; @@ -210,8 +185,7 @@ protected boolean finished() } @Override - boolean fillEdgesFrom() - { + boolean fillEdgesFrom() { if (prioQueueOpenSetFrom.isEmpty()) return false; @@ -223,8 +197,7 @@ boolean fillEdgesFrom() } @Override - boolean fillEdgesTo() - { + boolean fillEdgesTo() { if (prioQueueOpenSetTo.isEmpty()) return false; @@ -235,14 +208,12 @@ boolean fillEdgesTo() return true; } - private void fillEdges( AStarEntry currEdge, PriorityQueue prioQueueOpenSet, - TIntObjectMap bestWeightMap, EdgeExplorer explorer, boolean reverse ) - { + private void fillEdges(AStarEntry currEdge, PriorityQueue prioQueueOpenSet, + TIntObjectMap bestWeightMap, EdgeExplorer explorer, boolean reverse) { int currNode = currEdge.adjNode; EdgeIterator iter = explorer.setBaseNode(currNode); - while (iter.next()) - { + while (iter.next()) { if (!accept(iter, currEdge.edge)) continue; @@ -256,16 +227,13 @@ private void fillEdges( AStarEntry currEdge, PriorityQueue prioQueue continue; AStarEntry ase = bestWeightMap.get(traversalId); - if (ase == null || ase.getWeightOfVisitedPath() > alreadyVisitedWeight) - { + if (ase == null || ase.getWeightOfVisitedPath() > alreadyVisitedWeight) { double currWeightToGoal = weightApprox.approximate(neighborNode, reverse); double estimationFullWeight = alreadyVisitedWeight + currWeightToGoal; - if (ase == null) - { + if (ase == null) { ase = new AStarEntry(iter.getEdge(), neighborNode, estimationFullWeight, alreadyVisitedWeight); bestWeightMap.put(traversalId, ase); - } else - { + } else { assert (ase.weight > 0.999999 * estimationFullWeight) : "Inconsistent distance estimate " + ase.weight + " vs " + estimationFullWeight + " (" + ase.weight / estimationFullWeight + "), and:" + ase.getWeightOfVisitedPath() + " vs " + alreadyVisitedWeight + " (" + ase.getWeightOfVisitedPath() / alreadyVisitedWeight + ")"; @@ -282,8 +250,7 @@ private void fillEdges( AStarEntry currEdge, PriorityQueue prioQueue } } - public void updateBestPath( EdgeIteratorState edgeState, AStarEntry entryCurrent, int currLoc ) - { + public void updateBestPath(EdgeIteratorState edgeState, AStarEntry entryCurrent, int currLoc) { AStarEntry entryOther = bestWeightMapOther.get(currLoc); if (entryOther == null) return; @@ -291,26 +258,20 @@ public void updateBestPath( EdgeIteratorState edgeState, AStarEntry entryCurrent boolean reverse = bestWeightMapFrom == bestWeightMapOther; // update μ double newWeight = entryCurrent.weightOfVisitedPath + entryOther.weightOfVisitedPath; - if (traversalMode.isEdgeBased()) - { + if (traversalMode.isEdgeBased()) { if (entryOther.edge != entryCurrent.edge) throw new IllegalStateException("cannot happen for edge based execution of " + getName()); // see DijkstraBidirectionRef - if (entryOther.adjNode != entryCurrent.adjNode) - { + if (entryOther.adjNode != entryCurrent.adjNode) { entryCurrent = (AStar.AStarEntry) entryCurrent.parent; newWeight -= weighting.calcWeight(edgeState, reverse, EdgeIterator.NO_EDGE); - } else - { + } else if (!traversalMode.hasUTurnSupport()) // we detected a u-turn at meeting point, skip if not supported - if (!traversalMode.hasUTurnSupport()) - return; - } + return; } - if (newWeight < bestPath.getWeight()) - { + if (newWeight < bestPath.getWeight()) { bestPath.setSwitchToFrom(reverse); bestPath.sptEntry = entryCurrent; bestPath.edgeTo = entryOther; @@ -319,8 +280,7 @@ public void updateBestPath( EdgeIteratorState edgeState, AStarEntry entryCurrent } @Override - public String getName() - { + public String getName() { return Parameters.Algorithms.ASTAR_BI; } } diff --git a/core/src/main/java/com/graphhopper/routing/AbstractBidirAlgo.java b/core/src/main/java/com/graphhopper/routing/AbstractBidirAlgo.java index 9386fb28a4d..82499bcc25a 100644 --- a/core/src/main/java/com/graphhopper/routing/AbstractBidirAlgo.java +++ b/core/src/main/java/com/graphhopper/routing/AbstractBidirAlgo.java @@ -25,18 +25,22 @@ /** * Common subclass for bidirectional algorithms. *

+ * * @author Peter Karich */ -public abstract class AbstractBidirAlgo extends AbstractRoutingAlgorithm -{ - int visitedCountFrom; - int visitedCountTo; +public abstract class AbstractBidirAlgo extends AbstractRoutingAlgorithm { protected boolean finishedFrom; protected boolean finishedTo; + int visitedCountFrom; + int visitedCountTo; + + public AbstractBidirAlgo(Graph graph, FlagEncoder encoder, Weighting weighting, TraversalMode tMode) { + super(graph, encoder, weighting, tMode); + } - abstract void initFrom( int from, double dist ); + abstract void initFrom(int from, double dist); - abstract void initTo( int to, double dist ); + abstract void initTo(int to, double dist); protected abstract Path createAndInitPath(); @@ -48,14 +52,8 @@ public abstract class AbstractBidirAlgo extends AbstractRoutingAlgorithm abstract boolean fillEdgesTo(); - public AbstractBidirAlgo( Graph graph, FlagEncoder encoder, Weighting weighting, TraversalMode tMode ) - { - super(graph, encoder, weighting, tMode); - } - @Override - public Path calcPath( int from, int to ) - { + public Path calcPath(int from, int to) { checkAlreadyRun(); createAndInitPath(); initFrom(from, 0); @@ -64,10 +62,8 @@ public Path calcPath( int from, int to ) return extractPath(); } - protected void runAlgo() - { - while (!finished() && !isMaxVisitedNodesExceeded()) - { + protected void runAlgo() { + while (!finished() && !isMaxVisitedNodesExceeded()) { if (!finishedFrom) finishedFrom = !fillEdgesFrom(); @@ -77,8 +73,7 @@ protected void runAlgo() } @Override - public int getVisitedNodes() - { + public int getVisitedNodes() { return visitedCountFrom + visitedCountTo; } } diff --git a/core/src/main/java/com/graphhopper/routing/AbstractRoutingAlgorithm.java b/core/src/main/java/com/graphhopper/routing/AbstractRoutingAlgorithm.java index 3b688b1e137..a83f4e8f7aa 100644 --- a/core/src/main/java/com/graphhopper/routing/AbstractRoutingAlgorithm.java +++ b/core/src/main/java/com/graphhopper/routing/AbstractRoutingAlgorithm.java @@ -17,11 +17,14 @@ */ package com.graphhopper.routing; +import com.graphhopper.routing.util.DefaultEdgeFilter; +import com.graphhopper.routing.util.EdgeFilter; +import com.graphhopper.routing.util.FlagEncoder; +import com.graphhopper.routing.util.TraversalMode; import com.graphhopper.routing.weighting.Weighting; -import com.graphhopper.routing.util.*; -import com.graphhopper.storage.SPTEntry; import com.graphhopper.storage.Graph; import com.graphhopper.storage.NodeAccess; +import com.graphhopper.storage.SPTEntry; import com.graphhopper.util.EdgeExplorer; import com.graphhopper.util.EdgeIterator; import com.graphhopper.util.EdgeIteratorState; @@ -32,27 +35,25 @@ /** * @author Peter Karich */ -public abstract class AbstractRoutingAlgorithm implements RoutingAlgorithm -{ - private EdgeFilter additionalEdgeFilter; +public abstract class AbstractRoutingAlgorithm implements RoutingAlgorithm { protected final Graph graph; - protected NodeAccess nodeAccess; - protected EdgeExplorer inEdgeExplorer; - protected EdgeExplorer outEdgeExplorer; protected final Weighting weighting; protected final FlagEncoder flagEncoder; protected final TraversalMode traversalMode; + protected NodeAccess nodeAccess; + protected EdgeExplorer inEdgeExplorer; + protected EdgeExplorer outEdgeExplorer; protected int maxVisitedNodes = Integer.MAX_VALUE; + private EdgeFilter additionalEdgeFilter; private boolean alreadyRun; /** - * @param graph specifies the graph where this algorithm will run on - * @param encoder sets the used vehicle (bike, car, foot) - * @param weighting set the used weight calculation (e.g. fastest, shortest). + * @param graph specifies the graph where this algorithm will run on + * @param encoder sets the used vehicle (bike, car, foot) + * @param weighting set the used weight calculation (e.g. fastest, shortest). * @param traversalMode how the graph is traversed e.g. if via nodes or edges. */ - public AbstractRoutingAlgorithm( Graph graph, FlagEncoder encoder, Weighting weighting, TraversalMode traversalMode ) - { + public AbstractRoutingAlgorithm(Graph graph, FlagEncoder encoder, Weighting weighting, TraversalMode traversalMode) { this.weighting = weighting; this.flagEncoder = encoder; this.traversalMode = traversalMode; @@ -63,39 +64,33 @@ public AbstractRoutingAlgorithm( Graph graph, FlagEncoder encoder, Weighting wei } @Override - public void setMaxVisitedNodes( int numberOfNodes ) - { + public void setMaxVisitedNodes(int numberOfNodes) { this.maxVisitedNodes = numberOfNodes; } - public RoutingAlgorithm setEdgeFilter( EdgeFilter additionalEdgeFilter ) - { + public RoutingAlgorithm setEdgeFilter(EdgeFilter additionalEdgeFilter) { this.additionalEdgeFilter = additionalEdgeFilter; return this; } - protected boolean accept( EdgeIterator iter, int prevOrNextEdgeId ) - { + protected boolean accept(EdgeIterator iter, int prevOrNextEdgeId) { if (!traversalMode.hasUTurnSupport() && iter.getEdge() == prevOrNextEdgeId) return false; return additionalEdgeFilter == null || additionalEdgeFilter.accept(iter); } - protected void updateBestPath( EdgeIteratorState edgeState, SPTEntry bestSPTEntry, int traversalId ) - { + protected void updateBestPath(EdgeIteratorState edgeState, SPTEntry bestSPTEntry, int traversalId) { } - protected void checkAlreadyRun() - { + protected void checkAlreadyRun() { if (alreadyRun) throw new IllegalStateException("Create a new instance per call"); alreadyRun = true; } - protected SPTEntry createSPTEntry( int node, double weight ) - { + protected SPTEntry createSPTEntry(int node, double weight) { return new SPTEntry(EdgeIterator.NO_EDGE, node, weight); } @@ -103,6 +98,7 @@ protected SPTEntry createSPTEntry( int node, double weight ) * To be overwritten from extending class. Should we make this available in RoutingAlgorithm * interface? *

+ * * @return true if finished. */ protected abstract boolean finished(); @@ -111,35 +107,31 @@ protected SPTEntry createSPTEntry( int node, double weight ) * To be overwritten from extending class. Should we make this available in RoutingAlgorithm * interface? *

+ * * @return true if finished. */ protected abstract Path extractPath(); @Override - public List calcPaths( int from, int to ) - { + public List calcPaths(int from, int to) { return Collections.singletonList(calcPath(from, to)); } - protected Path createEmptyPath() - { + protected Path createEmptyPath() { return new Path(graph, flagEncoder); } @Override - public String getName() - { + public String getName() { return getClass().getSimpleName(); } @Override - public String toString() - { + public String toString() { return getName() + "|" + weighting; } - protected boolean isMaxVisitedNodesExceeded() - { + protected boolean isMaxVisitedNodesExceeded() { return maxVisitedNodes < getVisitedNodes(); } } diff --git a/core/src/main/java/com/graphhopper/routing/AlgorithmOptions.java b/core/src/main/java/com/graphhopper/routing/AlgorithmOptions.java index aeb0116098a..de9127ab6f1 100644 --- a/core/src/main/java/com/graphhopper/routing/AlgorithmOptions.java +++ b/core/src/main/java/com/graphhopper/routing/AlgorithmOptions.java @@ -32,121 +32,106 @@ * build(); * *

+ * * @author Peter Karich */ -public class AlgorithmOptions -{ +public class AlgorithmOptions { + private final PMap hints = new PMap(5); private String algorithm = Parameters.Algorithms.DIJKSTRA_BI; private Weighting weighting; private TraversalMode traversalMode = TraversalMode.NODE_BASED; private FlagEncoder flagEncoder; private int maxVisitedNodes = Integer.MAX_VALUE; - private final PMap hints = new PMap(5); - private AlgorithmOptions() - { + private AlgorithmOptions() { } /** * Default traversal mode NODE_BASED is used. */ - public AlgorithmOptions( String algorithm, FlagEncoder flagEncoder, Weighting weighting ) - { + public AlgorithmOptions(String algorithm, FlagEncoder flagEncoder, Weighting weighting) { this.algorithm = algorithm; this.weighting = weighting; this.flagEncoder = flagEncoder; } - public AlgorithmOptions( String algorithm, FlagEncoder flagEncoder, Weighting weighting, TraversalMode tMode ) - { + public AlgorithmOptions(String algorithm, FlagEncoder flagEncoder, Weighting weighting, TraversalMode tMode) { this.algorithm = algorithm; this.weighting = weighting; this.flagEncoder = flagEncoder; this.traversalMode = tMode; } + /** + * This method starts the building process for AlgorithmOptions. + */ + public static Builder start() { + return new Builder(); + } + + /** + * This method clones the specified AlgorithmOption object with the possibility for further + * changes. + */ + public static Builder start(AlgorithmOptions opts) { + Builder b = new Builder(); + if (opts.algorithm != null) + b.algorithm(opts.getAlgorithm()); + if (opts.flagEncoder != null) + b.flagEncoder(opts.getFlagEncoder()); + if (opts.traversalMode != null) + b.traversalMode(opts.getTraversalMode()); + if (opts.weighting != null) + b.weighting(opts.getWeighting()); + if (opts.maxVisitedNodes >= 0) + b.maxVisitedNodes(opts.maxVisitedNodes); + return b; + } + /** * @return the traversal mode, where node-based is the default. */ - public TraversalMode getTraversalMode() - { + public TraversalMode getTraversalMode() { return traversalMode; } - public Weighting getWeighting() - { + public Weighting getWeighting() { assertNotNull(weighting, "weighting"); return weighting; } - public String getAlgorithm() - { + public String getAlgorithm() { assertNotNull(algorithm, "algorithm"); return algorithm; } - public FlagEncoder getFlagEncoder() - { + public FlagEncoder getFlagEncoder() { assertNotNull(flagEncoder, "flagEncoder"); return flagEncoder; } - public int getMaxVisitedNodes() - { + public int getMaxVisitedNodes() { return maxVisitedNodes; } - public PMap getHints() - { + public PMap getHints() { return hints; } - private void assertNotNull( Object optionValue, String optionName ) - { + private void assertNotNull(Object optionValue, String optionName) { if (optionValue == null) throw new NullPointerException("Option '" + optionName + "' must NOT be null"); } @Override - public String toString() - { + public String toString() { return algorithm + ", " + weighting + ", " + flagEncoder + ", " + traversalMode; } - /** - * This method starts the building process for AlgorithmOptions. - */ - public static Builder start() - { - return new Builder(); - } - - /** - * This method clones the specified AlgorithmOption object with the possibility for further - * changes. - */ - public static Builder start( AlgorithmOptions opts ) - { - Builder b = new Builder(); - if (opts.algorithm != null) - b.algorithm(opts.getAlgorithm()); - if (opts.flagEncoder != null) - b.flagEncoder(opts.getFlagEncoder()); - if (opts.traversalMode != null) - b.traversalMode(opts.getTraversalMode()); - if (opts.weighting != null) - b.weighting(opts.getWeighting()); - if (opts.maxVisitedNodes >= 0) - b.maxVisitedNodes(opts.maxVisitedNodes); - return b; - } - - public static class Builder - { + public static class Builder { private final AlgorithmOptions opts = new AlgorithmOptions(); - public Builder traversalMode( TraversalMode traversalMode ) - { + public Builder traversalMode(TraversalMode traversalMode) { if (traversalMode == null) throw new IllegalArgumentException("null as traversal mode is not allowed"); @@ -154,8 +139,7 @@ public Builder traversalMode( TraversalMode traversalMode ) return this; } - public Builder weighting( Weighting weighting ) - { + public Builder weighting(Weighting weighting) { this.opts.weighting = weighting; return this; } @@ -163,32 +147,27 @@ public Builder weighting( Weighting weighting ) /** * For possible values see AlgorithmOptions.* */ - public Builder algorithm( String algorithm ) - { + public Builder algorithm(String algorithm) { this.opts.algorithm = algorithm; return this; } - public Builder flagEncoder( FlagEncoder flagEncoder ) - { + public Builder flagEncoder(FlagEncoder flagEncoder) { this.opts.flagEncoder = flagEncoder; return this; } - public Builder maxVisitedNodes( int maxVisitedNodes ) - { + public Builder maxVisitedNodes(int maxVisitedNodes) { this.opts.maxVisitedNodes = maxVisitedNodes; return this; } - public Builder hints( PMap hints ) - { + public Builder hints(PMap hints) { this.opts.hints.put(hints); return this; } - public AlgorithmOptions build() - { + public AlgorithmOptions build() { return opts; } } diff --git a/core/src/main/java/com/graphhopper/routing/AlternativeRoute.java b/core/src/main/java/com/graphhopper/routing/AlternativeRoute.java index ce810e6ecc2..e118e7af808 100644 --- a/core/src/main/java/com/graphhopper/routing/AlternativeRoute.java +++ b/core/src/main/java/com/graphhopper/routing/AlternativeRoute.java @@ -21,24 +21,23 @@ import com.graphhopper.routing.util.FlagEncoder; import com.graphhopper.routing.util.TraversalMode; import com.graphhopper.routing.weighting.Weighting; -import com.graphhopper.storage.SPTEntry; import com.graphhopper.storage.Graph; +import com.graphhopper.storage.SPTEntry; import com.graphhopper.util.EdgeIterator; import com.graphhopper.util.EdgeIteratorState; import com.graphhopper.util.GHUtility; import com.graphhopper.util.Parameters; import gnu.trove.map.TIntObjectMap; import gnu.trove.map.hash.TIntObjectHashMap; +import gnu.trove.procedure.TIntObjectProcedure; +import gnu.trove.procedure.TObjectProcedure; +import gnu.trove.set.TIntSet; +import gnu.trove.set.hash.TIntHashSet; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; - -import gnu.trove.procedure.TIntObjectProcedure; -import gnu.trove.procedure.TObjectProcedure; -import gnu.trove.set.TIntSet; -import gnu.trove.set.hash.TIntHashSet; import java.util.concurrent.atomic.AtomicInteger; /** @@ -61,8 +60,13 @@ * * @author Peter Karich */ -public class AlternativeRoute implements RoutingAlgorithm -{ +public class AlternativeRoute implements RoutingAlgorithm { + private static final Comparator ALT_COMPARATOR = new Comparator() { + @Override + public int compare(AlternativeInfo o1, AlternativeInfo o2) { + return Double.compare(o1.sortBy, o2.sortBy); + } + }; private final Graph graph; private final FlagEncoder flagEncoder; private final Weighting weighting; @@ -73,22 +77,40 @@ public class AlternativeRoute implements RoutingAlgorithm // the higher the maxWeightFactor the higher the explorationFactor needs to be // 1 is default for bidir Dijkstra, 0.8 seems to be a very similar value for bidir A* but roughly 1/2 of the nodes explored private double maxExplorationFactor = 0.8; - private double maxShareFactor = 0.6; private double minPlateauFactor = 0.2; private int maxPaths = 2; - public AlternativeRoute( Graph graph, FlagEncoder flagEncoder, Weighting weighting, TraversalMode traversalMode ) - { + public AlternativeRoute(Graph graph, FlagEncoder flagEncoder, Weighting weighting, TraversalMode traversalMode) { this.graph = graph; this.flagEncoder = flagEncoder; this.weighting = weighting; this.traversalMode = traversalMode; } + static List getAltNames(Graph graph, SPTEntry ee) { + if (ee == null || !EdgeIterator.Edge.isValid(ee.edge)) + return Collections.emptyList(); + + EdgeIteratorState iter = graph.getEdgeIteratorState(ee.edge, Integer.MIN_VALUE); + if (iter == null) + return Collections.emptyList(); + + String str = iter.getName(); + if (str.isEmpty()) + return Collections.emptyList(); + + return Collections.singletonList(str); + } + + static double calcSortBy(double weightInfluence, double weight, + double shareInfluence, double shareWeight, + double plateauInfluence, double plateauWeight) { + return weightInfluence * weight + shareInfluence * shareWeight + plateauInfluence * plateauWeight; + } + @Override - public void setMaxVisitedNodes( int numberOfNodes ) - { + public void setMaxVisitedNodes(int numberOfNodes) { this.maxVisitedNodes = numberOfNodes; } @@ -97,8 +119,7 @@ public void setMaxVisitedNodes( int numberOfNodes ) * all alternatives with a weight 2 times longer than the optimal weight are return. (default is * 1) */ - public void setMaxWeightFactor( double maxWeightFactor ) - { + public void setMaxWeightFactor(double maxWeightFactor) { this.maxWeightFactor = maxWeightFactor; } @@ -106,16 +127,14 @@ public void setMaxWeightFactor( double maxWeightFactor ) * This parameter is used to avoid alternatives too similar to the best path. Specify 0.5 to * force a same paths of maximum 50%. The unit is the 'weight' returned in the Weighting. */ - public void setMaxShareFactor( double maxShareFactor ) - { + public void setMaxShareFactor(double maxShareFactor) { this.maxShareFactor = maxShareFactor; } /** * This method sets the minimum plateau portion of every alternative path that is required. */ - public void setMinPlateauFactor( double minPlateauFactor ) - { + public void setMinPlateauFactor(double minPlateauFactor) { this.minPlateauFactor = minPlateauFactor; } @@ -125,16 +144,14 @@ public void setMinPlateauFactor( double minPlateauFactor ) * 1.5) and a lower value to improve query time but reduces the possibility to find * alternatives. */ - public void setMaxExplorationFactor( double explorationFactor ) - { + public void setMaxExplorationFactor(double explorationFactor) { this.maxExplorationFactor = explorationFactor; } /** * Specifies how many paths (including the optimal) are returned. (default is 2) */ - public void setMaxPaths( int maxPaths ) - { + public void setMaxPaths(int maxPaths) { this.maxPaths = maxPaths; if (this.maxPaths < 2) throw new IllegalStateException("Use normal algorithm with less overhead instead if no alternatives are required"); @@ -145,8 +162,7 @@ public void setMaxPaths( int maxPaths ) * alternatives are searched and they are only accepted if they are not too similar but close to * the best path. */ - public List calcAlternatives( int from, int to ) - { + public List calcAlternatives(int from, int to) { AlternativeBidirSearch altBidirDijktra = new AlternativeBidirSearch( graph, flagEncoder, weighting, traversalMode, maxExplorationFactor * 2); altBidirDijktra.setMaxVisitedNodes(maxVisitedNodes); @@ -159,46 +175,31 @@ public List calcAlternatives( int from, int to ) } @Override - public Path calcPath( int from, int to ) - { + public Path calcPath(int from, int to) { return calcPaths(from, to).get(0); } @Override - public List calcPaths( int from, int to ) - { + public List calcPaths(int from, int to) { List alts = calcAlternatives(from, to); List paths = new ArrayList(alts.size()); - for (AlternativeInfo a : alts) - { + for (AlternativeInfo a : alts) { paths.add(a.getPath()); } return paths; } - private static final Comparator ALT_COMPARATOR = new Comparator() - { - @Override - public int compare( AlternativeInfo o1, AlternativeInfo o2 ) - { - return Double.compare(o1.sortBy, o2.sortBy); - } - }; - @Override - public String getName() - { + public String getName() { return Parameters.Algorithms.ALT_ROUTE; } @Override - public int getVisitedNodes() - { + public int getVisitedNodes() { return visitedNodes; } - public static class AlternativeInfo - { + public static class AlternativeInfo { private final double sortBy; private final Path path; private final SPTEntry shareStart; @@ -206,9 +207,8 @@ public static class AlternativeInfo private final double shareWeight; private final List names; - public AlternativeInfo( double sortBy, Path path, SPTEntry shareStart, SPTEntry shareEnd, - double shareWeight, List altNames ) - { + public AlternativeInfo(double sortBy, Path path, SPTEntry shareStart, SPTEntry shareEnd, + double shareWeight, List altNames) { this.names = altNames; this.sortBy = sortBy; this.path = path; @@ -218,34 +218,28 @@ public AlternativeInfo( double sortBy, Path path, SPTEntry shareStart, SPTEntry this.shareWeight = shareWeight; } - public Path getPath() - { + public Path getPath() { return path; } - public SPTEntry getShareStart() - { + public SPTEntry getShareStart() { return shareStart; } - public SPTEntry getShareEnd() - { + public SPTEntry getShareEnd() { return shareEnd; } - public double getShareWeight() - { + public double getShareWeight() { return shareWeight; } - public double getSortBy() - { + public double getSortBy() { return sortBy; } @Override - public String toString() - { + public String toString() { return names + ", sortBy:" + sortBy + ", shareWeight:" + shareWeight + ", " + path; } } @@ -253,30 +247,25 @@ public String toString() /** * Helper class to find alternatives and alternatives for round trip. */ - public static class AlternativeBidirSearch extends AStarBidirection - { + public static class AlternativeBidirSearch extends AStarBidirection { private final double explorationFactor; - public AlternativeBidirSearch( Graph graph, FlagEncoder encoder, Weighting weighting, TraversalMode tMode, - double explorationFactor ) - { + public AlternativeBidirSearch(Graph graph, FlagEncoder encoder, Weighting weighting, TraversalMode tMode, + double explorationFactor) { super(graph, encoder, weighting, tMode); this.explorationFactor = explorationFactor; } - public TIntObjectMap getBestWeightMapFrom() - { + public TIntObjectMap getBestWeightMapFrom() { return bestWeightMapFrom; } - public TIntObjectMap getBestWeightMapTo() - { + public TIntObjectMap getBestWeightMapTo() { return bestWeightMapTo; } @Override - public boolean finished() - { + public boolean finished() { // we need to finish BOTH searches identical to CH if (finishedFrom && finishedTo) return true; @@ -295,8 +284,7 @@ public boolean finished() // For bidir A* and AStarEdge.getWeightOfVisitedPath see comment in AStarBidirection.finished } - public Path searchBest( int to, int from ) - { + public Path searchBest(int to, int from) { createAndInitPath(); initFrom(to, 0); initTo(from, 0); @@ -309,11 +297,10 @@ public Path searchBest( int to, int from ) * @return the information necessary to handle alternative paths. Note that the paths are * not yet extracted. */ - public List calcAlternatives( final int maxPaths, - double maxWeightFactor, final double weightInfluence, - final double maxShareFactor, final double shareInfluence, - final double minPlateauFactor, final double plateauInfluence ) - { + public List calcAlternatives(final int maxPaths, + double maxWeightFactor, final double weightInfluence, + final double maxShareFactor, final double shareInfluence, + final double minPlateauFactor, final double plateauInfluence) { final double maxWeight = maxWeightFactor * bestPath.getWeight(); final TIntObjectHashMap traversalIDMap = new TIntObjectHashMap(); final AtomicInteger startTID = addToMap(traversalIDMap, bestPath); @@ -334,19 +321,16 @@ public List calcAlternatives( final int maxPaths, alternatives.add(bestAlt); final List bestPathEntries = new ArrayList(2); - bestWeightMapFrom.forEachEntry(new TIntObjectProcedure() - { + bestWeightMapFrom.forEachEntry(new TIntObjectProcedure() { @Override - public boolean execute( final int traversalId, final SPTEntry fromSPTEntry ) - { + public boolean execute(final int traversalId, final SPTEntry fromSPTEntry) { SPTEntry toSPTEntry = bestWeightMapTo.get(traversalId); if (toSPTEntry == null) return true; - if (traversalMode.isEdgeBased()) - { + if (traversalMode.isEdgeBased()) { if (toSPTEntry.parent != null) - // move to parent for two reasons: + // move to parent for two reasons: // 1. make only turn costs missing in 'weight' and not duplicating current edge.weight // 2. to avoid duplicate edge in Path toSPTEntry = toSPTEntry.parent; @@ -362,21 +346,19 @@ public boolean execute( final int traversalId, final SPTEntry fromSPTEntry ) return true; // (2) Use the start traversal ID of a plateau as ID for the alternative path. - // Accept from-EdgeEntries only if such a start of a plateau - // i.e. discard if its parent has the same edgeId as the next to-SPTEntry. + // Accept from-EdgeEntries only if such a start of a plateau + // i.e. discard if its parent has the same edgeId as the next to-SPTEntry. // Ignore already added best path if (isBestPath(fromSPTEntry, bestPath)) return true; // For edge based traversal we need the next entry to find out the plateau start SPTEntry tmpFromEntry = traversalMode.isEdgeBased() ? fromSPTEntry.parent : fromSPTEntry; - if (tmpFromEntry == null || tmpFromEntry.parent == null) - { - // we can be here only if edge based and only if entry is not part of the best path + if (tmpFromEntry == null || tmpFromEntry.parent == null) { + // we can be here only if edge based and only if entry is not part of the best path // e.g. when starting point has two edges and one is part of the best path the other edge is path of an alternative assert traversalMode.isEdgeBased(); - } else - { + } else { int nextToTraversalId = traversalMode.createTraversalId(tmpFromEntry.adjNode, tmpFromEntry.parent.adjNode, tmpFromEntry.edge, true); SPTEntry tmpNextToSPTEntry = bestWeightMapTo.get(nextToTraversalId); @@ -390,7 +372,7 @@ public boolean execute( final int traversalId, final SPTEntry fromSPTEntry ) return true; } - // (3a) calculate plateau, we know we are at the beginning of the 'from'-side of + // (3a) calculate plateau, we know we are at the beginning of the 'from'-side of // the plateau A-B-C and go further to B // where B is the next-'from' of A and B is also the previous-'to' of A. // @@ -404,8 +386,7 @@ public boolean execute( final int traversalId, final SPTEntry fromSPTEntry ) double plateauWeight = 0; SPTEntry prevToSPTEntry = toSPTEntry; // List plateauEdges = new ArrayList(); - while (prevToSPTEntry.parent != null) - { + while (prevToSPTEntry.parent != null) { int nextFromTraversalId = traversalMode.createTraversalId(prevToSPTEntry.adjNode, prevToSPTEntry.parent.adjNode, prevToSPTEntry.edge, false); @@ -429,21 +410,19 @@ public boolean execute( final int traversalId, final SPTEntry fromSPTEntry ) if (fromSPTEntry.parent == null) throw new IllegalStateException("not implemented yet. in case of an edge based traversal the parent of fromSPTEntry could be null"); - // (3b) calculate share + // (3b) calculate share SPTEntry fromEE = getFirstShareEE(fromSPTEntry.parent, true); SPTEntry toEE = getFirstShareEE(toSPTEntry.parent, false); double shareWeight = fromEE.getWeightOfVisitedPath() + toEE.getWeightOfVisitedPath(); boolean smallShare = shareWeight / bestPath.getWeight() < maxShareFactor; - if (smallShare) - { + if (smallShare) { List altNames = getAltNames(graph, fromSPTEntry); double sortBy = calcSortBy(weightInfluence, weight, shareInfluence, shareWeight, plateauInfluence, plateauWeight); double worstSortBy = getWorstSortBy(); // plateaus.add(new PlateauInfo(altName, plateauEdges)); - if (sortBy < worstSortBy || alternatives.size() < maxPaths) - { + if (sortBy < worstSortBy || alternatives.size() < maxPaths) { Path path = new PathBidirRef(graph, flagEncoder). setSPTEntryTo(toSPTEntry).setSPTEntry(fromSPTEntry). setWeight(weight); @@ -469,10 +448,8 @@ public boolean execute( final int traversalId, final SPTEntry fromSPTEntry ) /** * Extract path until we stumble over an existing traversal id */ - SPTEntry getFirstShareEE( SPTEntry startEE, boolean reverse ) - { - while (startEE.parent != null) - { + SPTEntry getFirstShareEE(SPTEntry startEE, boolean reverse) { + while (startEE.parent != null) { // TODO we could make use of traversal ID directly if stored in SPTEntry int tid = traversalMode.createTraversalId(startEE.adjNode, startEE.parent.adjNode, startEE.edge, reverse); if (isAlreadyExisting(tid)) @@ -488,13 +465,10 @@ SPTEntry getFirstShareEE( SPTEntry startEE, boolean reverse ) * This method returns true if the specified tid is already existent in the * traversalIDMap */ - boolean isAlreadyExisting( final int tid ) - { - return !traversalIDMap.forEachValue(new TObjectProcedure() - { + boolean isAlreadyExisting(final int tid) { + return !traversalIDMap.forEachValue(new TObjectProcedure() { @Override - public boolean execute( TIntSet set ) - { + public boolean execute(TIntSet set) { return !set.contains(tid); } }); @@ -503,28 +477,23 @@ public boolean execute( TIntSet set ) /** * Return the current worst weight for all alternatives */ - double getWorstSortBy() - { + double getWorstSortBy() { if (alternatives.isEmpty()) throw new IllegalStateException("Empty alternative list cannot happen"); return alternatives.get(alternatives.size() - 1).sortBy; } // returns true if fromSPTEntry is identical to the specified best path - boolean isBestPath( SPTEntry fromSPTEntry, Path bestPath ) - { - if (traversalMode.isEdgeBased()) - { - if (GHUtility.getEdgeFromEdgeKey(startTID.get()) == fromSPTEntry.edge) - { + boolean isBestPath(SPTEntry fromSPTEntry, Path bestPath) { + if (traversalMode.isEdgeBased()) { + if (GHUtility.getEdgeFromEdgeKey(startTID.get()) == fromSPTEntry.edge) { if (fromSPTEntry.parent == null) throw new IllegalStateException("best path must have no parent but was non-null: " + fromSPTEntry); return true; } - } else if (fromSPTEntry.parent == null) - { + } else if (fromSPTEntry.parent == null) { bestPathEntries.add(fromSPTEntry); if (bestPathEntries.size() > 1) throw new IllegalStateException("There is only one best path but was: " + bestPathEntries); @@ -546,19 +515,15 @@ boolean isBestPath( SPTEntry fromSPTEntry, Path bestPath ) /** * This method adds the traversal IDs of the specified path as set to the specified map. */ - AtomicInteger addToMap( TIntObjectHashMap map, Path path ) - { + AtomicInteger addToMap(TIntObjectHashMap map, Path path) { TIntSet set = new TIntHashSet(); final AtomicInteger startTID = new AtomicInteger(-1); - for (EdgeIteratorState iterState : path.calcEdges()) - { + for (EdgeIteratorState iterState : path.calcEdges()) { int tid = traversalMode.createTraversalId(iterState, false); set.add(tid); - if (startTID.get() < 0) - { + if (startTID.get() < 0) { // for node based traversal we need to explicitely add base node as starting node and to list - if (!traversalMode.isEdgeBased()) - { + if (!traversalMode.isEdgeBased()) { tid = iterState.getBaseNode(); set.add(tid); } @@ -571,53 +536,25 @@ AtomicInteger addToMap( TIntObjectHashMap map, Path path ) } } - static List getAltNames( Graph graph, SPTEntry ee ) - { - if (ee == null || !EdgeIterator.Edge.isValid(ee.edge)) - return Collections.emptyList(); - - EdgeIteratorState iter = graph.getEdgeIteratorState(ee.edge, Integer.MIN_VALUE); - if (iter == null) - return Collections.emptyList(); - - String str = iter.getName(); - if (str.isEmpty()) - return Collections.emptyList(); - - return Collections.singletonList(str); - } - - static double calcSortBy( double weightInfluence, double weight, - double shareInfluence, double shareWeight, - double plateauInfluence, double plateauWeight ) - { - return weightInfluence * weight + shareInfluence * shareWeight + plateauInfluence * plateauWeight; - } - - public static class PlateauInfo - { + public static class PlateauInfo { String name; List edges; - public PlateauInfo( String name, List edges ) - { + public PlateauInfo(String name, List edges) { this.name = name; this.edges = edges; } @Override - public String toString() - { + public String toString() { return name; } - public List getEdges() - { + public List getEdges() { return edges; } - public String getName() - { + public String getName() { return name; } } diff --git a/core/src/main/java/com/graphhopper/routing/Dijkstra.java b/core/src/main/java/com/graphhopper/routing/Dijkstra.java index 7ceea137d66..c8583f3655c 100644 --- a/core/src/main/java/com/graphhopper/routing/Dijkstra.java +++ b/core/src/main/java/com/graphhopper/routing/Dijkstra.java @@ -17,74 +17,66 @@ */ package com.graphhopper.routing; -import gnu.trove.map.TIntObjectMap; -import gnu.trove.map.hash.TIntObjectHashMap; - -import java.util.PriorityQueue; - import com.graphhopper.routing.util.FlagEncoder; import com.graphhopper.routing.util.TraversalMode; import com.graphhopper.routing.weighting.Weighting; -import com.graphhopper.storage.SPTEntry; import com.graphhopper.storage.Graph; +import com.graphhopper.storage.SPTEntry; import com.graphhopper.util.EdgeExplorer; import com.graphhopper.util.EdgeIterator; import com.graphhopper.util.Parameters; +import gnu.trove.map.TIntObjectMap; +import gnu.trove.map.hash.TIntObjectHashMap; + +import java.util.PriorityQueue; /** * Implements a single source shortest path algorithm * http://en.wikipedia.org/wiki/Dijkstra's_algorithm *

+ * * @author Peter Karich */ -public class Dijkstra extends AbstractRoutingAlgorithm -{ +public class Dijkstra extends AbstractRoutingAlgorithm { protected TIntObjectMap fromMap; protected PriorityQueue fromHeap; protected SPTEntry currEdge; private int visitedNodes; private int to = -1; - public Dijkstra( Graph graph, FlagEncoder encoder, Weighting weighting, TraversalMode tMode ) - { + public Dijkstra(Graph graph, FlagEncoder encoder, Weighting weighting, TraversalMode tMode) { super(graph, encoder, weighting, tMode); int size = Math.min(Math.max(200, graph.getNodes() / 10), 2000); initCollections(size); } - protected void initCollections( int size ) - { + protected void initCollections(int size) { fromHeap = new PriorityQueue(size); fromMap = new TIntObjectHashMap(size); } @Override - public Path calcPath( int from, int to ) - { + public Path calcPath(int from, int to) { checkAlreadyRun(); this.to = to; currEdge = createSPTEntry(from, 0); - if (!traversalMode.isEdgeBased()) - { + if (!traversalMode.isEdgeBased()) { fromMap.put(from, currEdge); } runAlgo(); return extractPath(); } - protected void runAlgo() - { + protected void runAlgo() { EdgeExplorer explorer = outEdgeExplorer; - while (true) - { + while (true) { visitedNodes++; if (isMaxVisitedNodesExceeded() || finished()) break; int startNode = currEdge.adjNode; EdgeIterator iter = explorer.setBaseNode(startNode); - while (iter.next()) - { + while (iter.next()) { if (!accept(iter, currEdge.edge)) continue; @@ -94,14 +86,12 @@ protected void runAlgo() continue; SPTEntry nEdge = fromMap.get(traversalId); - if (nEdge == null) - { + if (nEdge == null) { nEdge = new SPTEntry(iter.getEdge(), iter.getAdjNode(), tmpWeight); nEdge.parent = currEdge; fromMap.put(traversalId, nEdge); fromHeap.add(nEdge); - } else if (nEdge.weight > tmpWeight) - { + } else if (nEdge.weight > tmpWeight) { fromHeap.remove(nEdge); nEdge.edge = iter.getEdge(); nEdge.weight = tmpWeight; @@ -123,14 +113,12 @@ protected void runAlgo() } @Override - protected boolean finished() - { + protected boolean finished() { return currEdge.adjNode == to; } @Override - protected Path extractPath() - { + protected Path extractPath() { if (currEdge == null || !finished()) return createEmptyPath(); @@ -138,14 +126,12 @@ protected Path extractPath() } @Override - public int getVisitedNodes() - { + public int getVisitedNodes() { return visitedNodes; } @Override - public String getName() - { + public String getName() { return Parameters.Algorithms.DIJKSTRA; } } diff --git a/core/src/main/java/com/graphhopper/routing/DijkstraBidirectionRef.java b/core/src/main/java/com/graphhopper/routing/DijkstraBidirectionRef.java index 121f86bb23f..2b126e2b55b 100644 --- a/core/src/main/java/com/graphhopper/routing/DijkstraBidirectionRef.java +++ b/core/src/main/java/com/graphhopper/routing/DijkstraBidirectionRef.java @@ -17,46 +17,43 @@ */ package com.graphhopper.routing; -import gnu.trove.map.TIntObjectMap; -import gnu.trove.map.hash.TIntObjectHashMap; - -import java.util.PriorityQueue; - import com.graphhopper.routing.util.FlagEncoder; import com.graphhopper.routing.util.TraversalMode; import com.graphhopper.routing.weighting.Weighting; -import com.graphhopper.storage.SPTEntry; import com.graphhopper.storage.Graph; +import com.graphhopper.storage.SPTEntry; import com.graphhopper.util.*; +import gnu.trove.map.TIntObjectMap; +import gnu.trove.map.hash.TIntObjectHashMap; + +import java.util.PriorityQueue; /** * Calculates best path in bidirectional way. *

* 'Ref' stands for reference implementation and is using the normal Java-'reference'-way. *

+ * * @author Peter Karich */ -public class DijkstraBidirectionRef extends AbstractBidirAlgo -{ - private PriorityQueue openSetFrom; - private PriorityQueue openSetTo; +public class DijkstraBidirectionRef extends AbstractBidirAlgo { protected TIntObjectMap bestWeightMapFrom; protected TIntObjectMap bestWeightMapTo; protected TIntObjectMap bestWeightMapOther; protected SPTEntry currFrom; protected SPTEntry currTo; protected PathBidirRef bestPath; + private PriorityQueue openSetFrom; + private PriorityQueue openSetTo; private boolean updateBestPath = true; - public DijkstraBidirectionRef( Graph graph, FlagEncoder encoder, Weighting weighting, TraversalMode tMode ) - { + public DijkstraBidirectionRef(Graph graph, FlagEncoder encoder, Weighting weighting, TraversalMode tMode) { super(graph, encoder, weighting, tMode); int size = Math.min(Math.max(200, graph.getNodes() / 10), 2000); initCollections(size); } - protected void initCollections( int size ) - { + protected void initCollections(int size) { openSetFrom = new PriorityQueue(size); bestWeightMapFrom = new TIntObjectHashMap(size); @@ -65,67 +62,51 @@ protected void initCollections( int size ) } @Override - public void initFrom( int from, double weight ) - { + public void initFrom(int from, double weight) { currFrom = createSPTEntry(from, weight); openSetFrom.add(currFrom); - if (!traversalMode.isEdgeBased()) - { + if (!traversalMode.isEdgeBased()) { bestWeightMapFrom.put(from, currFrom); - if (currTo != null) - { + if (currTo != null) { bestWeightMapOther = bestWeightMapTo; updateBestPath(GHUtility.getEdge(graph, from, currTo.adjNode), currTo, from); } - } else - { - if (currTo != null && currTo.adjNode == from) - { - // special case of identical start and end - bestPath.sptEntry = currFrom; - bestPath.edgeTo = currTo; - finishedFrom = true; - finishedTo = true; - } + } else if (currTo != null && currTo.adjNode == from) { + // special case of identical start and end + bestPath.sptEntry = currFrom; + bestPath.edgeTo = currTo; + finishedFrom = true; + finishedTo = true; } } @Override - public void initTo( int to, double weight ) - { + public void initTo(int to, double weight) { currTo = createSPTEntry(to, weight); openSetTo.add(currTo); - if (!traversalMode.isEdgeBased()) - { + if (!traversalMode.isEdgeBased()) { bestWeightMapTo.put(to, currTo); - if (currFrom != null) - { + if (currFrom != null) { bestWeightMapOther = bestWeightMapFrom; updateBestPath(GHUtility.getEdge(graph, currFrom.adjNode, to), currFrom, to); } - } else - { - if (currFrom != null && currFrom.adjNode == to) - { - // special case of identical start and end - bestPath.sptEntry = currFrom; - bestPath.edgeTo = currTo; - finishedFrom = true; - finishedTo = true; - } + } else if (currFrom != null && currFrom.adjNode == to) { + // special case of identical start and end + bestPath.sptEntry = currFrom; + bestPath.edgeTo = currTo; + finishedFrom = true; + finishedTo = true; } } @Override - protected Path createAndInitPath() - { + protected Path createAndInitPath() { bestPath = new PathBidirRef(graph, flagEncoder); return bestPath; } @Override - protected Path extractPath() - { + protected Path extractPath() { if (finished()) return bestPath.extract(); @@ -133,20 +114,17 @@ protected Path extractPath() } @Override - protected double getCurrentFromWeight() - { + protected double getCurrentFromWeight() { return currFrom.weight; } @Override - protected double getCurrentToWeight() - { + protected double getCurrentToWeight() { return currTo.weight; } @Override - public boolean fillEdgesFrom() - { + public boolean fillEdgesFrom() { if (openSetFrom.isEmpty()) return false; @@ -158,8 +136,7 @@ public boolean fillEdgesFrom() } @Override - public boolean fillEdgesTo() - { + public boolean fillEdgesTo() { if (openSetTo.isEmpty()) return false; currTo = openSetTo.poll(); @@ -174,20 +151,17 @@ public boolean fillEdgesTo() // => when scanning an arc (v, w) in the forward search and w is scanned in the reverseOrder // search, update extractPath = μ if df (v) + (v, w) + dr (w) < μ @Override - public boolean finished() - { + public boolean finished() { if (finishedFrom || finishedTo) return true; return currFrom.weight + currTo.weight >= bestPath.getWeight(); } - void fillEdges( SPTEntry currEdge, PriorityQueue prioQueue, - TIntObjectMap shortestWeightMap, EdgeExplorer explorer, boolean reverse ) - { + void fillEdges(SPTEntry currEdge, PriorityQueue prioQueue, + TIntObjectMap shortestWeightMap, EdgeExplorer explorer, boolean reverse) { EdgeIterator iter = explorer.setBaseNode(currEdge.adjNode); - while (iter.next()) - { + while (iter.next()) { if (!accept(iter, currEdge.edge)) continue; @@ -197,14 +171,12 @@ void fillEdges( SPTEntry currEdge, PriorityQueue prioQueue, continue; SPTEntry ee = shortestWeightMap.get(traversalId); - if (ee == null) - { + if (ee == null) { ee = new SPTEntry(iter.getEdge(), iter.getAdjNode(), tmpWeight); ee.parent = currEdge; shortestWeightMap.put(traversalId, ee); prioQueue.add(ee); - } else if (ee.weight > tmpWeight) - { + } else if (ee.weight > tmpWeight) { prioQueue.remove(ee); ee.edge = iter.getEdge(); ee.weight = tmpWeight; @@ -219,8 +191,7 @@ void fillEdges( SPTEntry currEdge, PriorityQueue prioQueue, } @Override - protected void updateBestPath( EdgeIteratorState edgeState, SPTEntry entryCurrent, int traversalId ) - { + protected void updateBestPath(EdgeIteratorState edgeState, SPTEntry entryCurrent, int traversalId) { SPTEntry entryOther = bestWeightMapOther.get(traversalId); if (entryOther == null) return; @@ -229,26 +200,20 @@ protected void updateBestPath( EdgeIteratorState edgeState, SPTEntry entryCurren // update μ double newWeight = entryCurrent.weight + entryOther.weight; - if (traversalMode.isEdgeBased()) - { + if (traversalMode.isEdgeBased()) { if (entryOther.edge != entryCurrent.edge) throw new IllegalStateException("cannot happen for edge based execution of " + getName()); - if (entryOther.adjNode != entryCurrent.adjNode) - { + if (entryOther.adjNode != entryCurrent.adjNode) { // prevents the path to contain the edge at the meeting point twice and subtract the weight (excluding turn weight => no previous edge) entryCurrent = entryCurrent.parent; newWeight -= weighting.calcWeight(edgeState, reverse, EdgeIterator.NO_EDGE); - } else - { + } else if (!traversalMode.hasUTurnSupport()) // we detected a u-turn at meeting point, skip if not supported - if (!traversalMode.hasUTurnSupport()) - return; - } + return; } - if (newWeight < bestPath.getWeight()) - { + if (newWeight < bestPath.getWeight()) { bestPath.setSwitchToFrom(reverse); bestPath.setSPTEntry(entryCurrent); bestPath.setWeight(newWeight); @@ -256,23 +221,19 @@ protected void updateBestPath( EdgeIteratorState edgeState, SPTEntry entryCurren } } - TIntObjectMap getBestFromMap() - { + TIntObjectMap getBestFromMap() { return bestWeightMapFrom; } - TIntObjectMap getBestToMap() - { + TIntObjectMap getBestToMap() { return bestWeightMapTo; } - void setBestOtherMap( TIntObjectMap other ) - { + void setBestOtherMap(TIntObjectMap other) { bestWeightMapOther = other; } - void setFromDataStructures( DijkstraBidirectionRef dijkstra ) - { + void setFromDataStructures(DijkstraBidirectionRef dijkstra) { openSetFrom = dijkstra.openSetFrom; bestWeightMapFrom = dijkstra.bestWeightMapFrom; finishedFrom = dijkstra.finishedFrom; @@ -281,8 +242,7 @@ void setFromDataStructures( DijkstraBidirectionRef dijkstra ) // outEdgeExplorer } - void setToDataStructures( DijkstraBidirectionRef dijkstra ) - { + void setToDataStructures(DijkstraBidirectionRef dijkstra) { openSetTo = dijkstra.openSetTo; bestWeightMapTo = dijkstra.bestWeightMapTo; finishedTo = dijkstra.finishedTo; @@ -291,19 +251,16 @@ void setToDataStructures( DijkstraBidirectionRef dijkstra ) // inEdgeExplorer } - void setUpdateBestPath( boolean b ) - { + void setUpdateBestPath(boolean b) { updateBestPath = b; } - void setBestPath( PathBidirRef bestPath ) - { + void setBestPath(PathBidirRef bestPath) { this.bestPath = bestPath; } @Override - public String getName() - { + public String getName() { return Parameters.Algorithms.DIJKSTRA_BI; } } diff --git a/core/src/main/java/com/graphhopper/routing/DijkstraOneToMany.java b/core/src/main/java/com/graphhopper/routing/DijkstraOneToMany.java index f8ef0973065..20c9991a3be 100644 --- a/core/src/main/java/com/graphhopper/routing/DijkstraOneToMany.java +++ b/core/src/main/java/com/graphhopper/routing/DijkstraOneToMany.java @@ -33,14 +33,14 @@ * A simple dijkstra tuned to perform one to many queries more efficient than Dijkstra. Old data * structures are cached between requests and potentially reused. Useful for CH preparation. *

+ * * @author Peter Karich */ -public class DijkstraOneToMany extends AbstractRoutingAlgorithm -{ +public class DijkstraOneToMany extends AbstractRoutingAlgorithm { private static final int EMPTY_PARENT = -1; private static final int NOT_FOUND = -1; - protected double[] weights; private final TIntArrayListWithCap changedNodes; + protected double[] weights; private int[] parents; private int[] edgeIds; private IntDoubleBinHeap heap; @@ -50,8 +50,7 @@ public class DijkstraOneToMany extends AbstractRoutingAlgorithm private int currNode, fromNode, to; private double weightLimit = Double.MAX_VALUE; - public DijkstraOneToMany( Graph graph, FlagEncoder encoder, Weighting weighting, TraversalMode tMode ) - { + public DijkstraOneToMany(Graph graph, FlagEncoder encoder, Weighting weighting, TraversalMode tMode) { super(graph, encoder, weighting, tMode); parents = new int[graph.getNodes()]; @@ -69,16 +68,14 @@ public DijkstraOneToMany( Graph graph, FlagEncoder encoder, Weighting weighting, } @Override - public Path calcPath( int from, int to ) - { + public Path calcPath(int from, int to) { fromNode = from; endNode = findEndNode(from, to); return extractPath(); } @Override - public Path extractPath() - { + public Path extractPath() { PathNative p = new PathNative(graph, flagEncoder, parents, edgeIds); if (endNode >= 0) p.setWeight(weights[endNode]); @@ -93,29 +90,24 @@ public Path extractPath() /** * Call clear if you have a different start node and need to clear the cache. */ - public DijkstraOneToMany clear() - { + public DijkstraOneToMany clear() { doClear = true; return this; } - public double getWeight( int endNode ) - { + public double getWeight(int endNode) { return weights[endNode]; } - public int findEndNode( int from, int to ) - { + public int findEndNode(int from, int to) { if (weights.length < 2) return NOT_FOUND; this.to = to; - if (doClear) - { + if (doClear) { doClear = false; int vn = changedNodes.size(); - for (int i = 0; i < vn; i++) - { + for (int i = 0; i < vn; i++) { int n = changedNodes.get(i); weights[n] = Double.MAX_VALUE; parents[n] = EMPTY_PARENT; @@ -126,13 +118,11 @@ public int findEndNode( int from, int to ) changedNodes.reset(); currNode = from; - if (!traversalMode.isEdgeBased()) - { + if (!traversalMode.isEdgeBased()) { weights[currNode] = 0; changedNodes.add(currNode); } - } else - { + } else { // Cached! Re-use existing data structures int parentNode = parents[to]; if (parentNode != EMPTY_PARENT && weights[to] <= weights[currNode]) @@ -147,20 +137,17 @@ public int findEndNode( int from, int to ) visitedNodes = 0; // we call 'finished' before heap.peek_element but this would add unnecessary overhead for this special case so we do it outside of the loop - if (finished()) - { + if (finished()) { // then we need a small workaround for special cases see #707 if (heap.isEmpty()) doClear = true; return currNode; } - while (true) - { + while (true) { visitedNodes++; EdgeIterator iter = outEdgeExplorer.setBaseNode(currNode); - while (iter.next()) - { + while (iter.next()) { int adjNode = iter.getAdjNode(); int prevEdgeId = edgeIds[adjNode]; if (!accept(iter, prevEdgeId)) @@ -171,16 +158,14 @@ public int findEndNode( int from, int to ) continue; double w = weights[adjNode]; - if (w == Double.MAX_VALUE) - { + if (w == Double.MAX_VALUE) { parents[adjNode] = currNode; weights[adjNode] = tmpWeight; heap.insert_(tmpWeight, adjNode); changedNodes.add(adjNode); edgeIds[adjNode] = iter.getEdge(); - } else if (w > tmpWeight) - { + } else if (w > tmpWeight) { parents[adjNode] = currNode; weights[adjNode] = tmpWeight; heap.update_(tmpWeight, adjNode); @@ -202,23 +187,19 @@ public int findEndNode( int from, int to ) } @Override - public boolean finished() - { + public boolean finished() { return currNode == to; } - public void setWeightLimit( double weightLimit ) - { + public void setWeightLimit(double weightLimit) { this.weightLimit = weightLimit; } - protected boolean isWeightLimitExceeded() - { + protected boolean isWeightLimitExceeded() { return weights[currNode] > weightLimit; } - public void close() - { + public void close() { weights = null; parents = null; edgeIds = null; @@ -226,22 +207,19 @@ public void close() } @Override - public int getVisitedNodes() - { + public int getVisitedNodes() { return visitedNodes; } @Override - public String getName() - { + public String getName() { return Parameters.Algorithms.DIJKSTRA_ONE_TO_MANY; } /** * List currently used memory in MB (approximately) */ - public String getMemoryUsageAsString() - { + public String getMemoryUsageAsString() { long len = weights.length; return ((8L + 4L + 4L) * len + changedNodes.getCapacity() * 4L @@ -249,14 +227,11 @@ public String getMemoryUsageAsString() + "MB"; } - private static class TIntArrayListWithCap extends TIntArrayList - { - public TIntArrayListWithCap() - { + private static class TIntArrayListWithCap extends TIntArrayList { + public TIntArrayListWithCap() { } - public int getCapacity() - { + public int getCapacity() { return _data.length; } } diff --git a/core/src/main/java/com/graphhopper/routing/Path.java b/core/src/main/java/com/graphhopper/routing/Path.java index 0c9ac60de9b..91e59fc6355 100644 --- a/core/src/main/java/com/graphhopper/routing/Path.java +++ b/core/src/main/java/com/graphhopper/routing/Path.java @@ -19,9 +19,9 @@ import com.graphhopper.routing.util.DefaultEdgeFilter; import com.graphhopper.routing.util.FlagEncoder; -import com.graphhopper.storage.SPTEntry; import com.graphhopper.storage.Graph; import com.graphhopper.storage.NodeAccess; +import com.graphhopper.storage.SPTEntry; import com.graphhopper.util.*; import gnu.trove.list.TIntList; import gnu.trove.list.array.TIntArrayList; @@ -35,34 +35,33 @@ * edge determination faster and less complex as there could be several edges (u,v) especially for * graphs with shortcuts. *

+ * * @author Peter Karich * @author Ottavio Campana * @author jan soe */ -public class Path -{ +public class Path { private static final AngleCalc AC = Helper.ANGLE_CALC; - private List description; + final StopWatch extractSW = new StopWatch("extract"); protected Graph graph; - private FlagEncoder encoder; protected double distance; // we go upwards (via SPTEntry.parent) from the goal node to the origin node protected boolean reverseOrder = true; protected long time; - private boolean found; /** * Shortest path tree entry */ protected SPTEntry sptEntry; - final StopWatch extractSW = new StopWatch("extract"); - private int fromNode = -1; protected int endNode = -1; + private List description; + private FlagEncoder encoder; + private boolean found; + private int fromNode = -1; private TIntList edgeIds; private double weight; private NodeAccess nodeAccess; - public Path( Graph graph, FlagEncoder encoder ) - { + public Path(Graph graph, FlagEncoder encoder) { this.weight = Double.MAX_VALUE; this.graph = graph; this.nodeAccess = graph.getNodeAccess(); @@ -73,8 +72,7 @@ public Path( Graph graph, FlagEncoder encoder ) /** * Populates an unextracted path instances from the specified path p. */ - Path( Path p ) - { + Path(Path p) { this(p.graph, p.encoder); weight = p.weight; edgeIds = new TIntArrayList(p.edgeIds); @@ -85,69 +83,59 @@ public Path( Graph graph, FlagEncoder encoder ) * @return the description of this route alternative to make it meaningful for the user e.g. it * displays one or two main roads of the route. */ - public List getDescription() - { + public List getDescription() { if (description == null) return Collections.emptyList(); return description; } - public Path setDescription( List description ) - { + public Path setDescription(List description) { this.description = description; return this; } - public Path setSPTEntry( SPTEntry sptEntry ) - { + public Path setSPTEntry(SPTEntry sptEntry) { this.sptEntry = sptEntry; return this; } - protected void addEdge( int edge ) - { + protected void addEdge(int edge) { edgeIds.add(edge); } - protected Path setEndNode( int end ) - { + protected Path setEndNode(int end) { endNode = end; return this; } - /** - * We need to remember fromNode explicitly as its not saved in one edgeId of edgeIds. - */ - protected Path setFromNode( int from ) - { - fromNode = from; - return this; - } - /** * @return the first node of this Path. */ - private int getFromNode() - { + private int getFromNode() { if (fromNode < 0) throw new IllegalStateException("Call extract() before retrieving fromNode"); return fromNode; } - public boolean isFound() - { + /** + * We need to remember fromNode explicitly as its not saved in one edgeId of edgeIds. + */ + protected Path setFromNode(int from) { + fromNode = from; + return this; + } + + public boolean isFound() { return found; } - public Path setFound( boolean found ) - { + public Path setFound(boolean found) { this.found = found; return this; } - void reverseOrder() - { + void reverseOrder() { if (!reverseOrder) throw new IllegalStateException("Switching order multiple times is not supported"); @@ -158,29 +146,25 @@ void reverseOrder() /** * @return distance in meter */ - public double getDistance() - { + public double getDistance() { return distance; } /** * @return time in millis */ - public long getTime() - { + public long getTime() { return time; } /** * This weight will be updated during the algorithm. The initial value is maximum double. */ - public double getWeight() - { + public double getWeight() { return weight; } - public Path setWeight( double w ) - { + public Path setWeight(double w) { this.weight = w; return this; } @@ -188,16 +172,14 @@ public Path setWeight( double w ) /** * Extracts the Path from the shortest-path-tree determined by sptEntry. */ - public Path extract() - { + public Path extract() { if (isFound()) throw new IllegalStateException("Extract can only be called once"); extractSW.start(); SPTEntry goalEdge = sptEntry; setEndNode(goalEdge.adjNode); - while (EdgeIterator.Edge.isValid(goalEdge.edge)) - { + while (EdgeIterator.Edge.isValid(goalEdge.edge)) { processEdge(goalEdge.edge, goalEdge.adjNode); goalEdge = goalEdge.parent; } @@ -211,29 +193,25 @@ public Path extract() /** * Yields the final edge of the path */ - public EdgeIteratorState getFinalEdge() - { + public EdgeIteratorState getFinalEdge() { return graph.getEdgeIteratorState(edgeIds.get(edgeIds.size() - 1), endNode); } /** * @return the time it took to extract the path in nano (!) seconds */ - public long getExtractTime() - { + public long getExtractTime() { return extractSW.getNanos(); } - public String getDebugInfo() - { + public String getDebugInfo() { return extractSW.toString(); } /** * Calls getDistance and adds the edgeId. */ - protected void processEdge( int edgeId, int adjNode ) - { + protected void processEdge(int edgeId, int adjNode) { EdgeIteratorState iter = graph.getEdgeIteratorState(edgeId, adjNode); double dist = iter.getDistance(); distance += dist; @@ -246,8 +224,7 @@ protected void processEdge( int edgeId, int adjNode ) * Calculates the time in millis for the specified distance in meter and speed (in km/h) via * flags. */ - protected long calcMillis( double distance, long flags, boolean revert ) - { + protected long calcMillis(double distance, long flags, boolean revert) { if (revert && !encoder.isBackward(flags) || !revert && !encoder.isForward(flags)) throw new IllegalStateException("Calculating time should not require to read speed from edge in wrong direction. " @@ -263,27 +240,18 @@ protected long calcMillis( double distance, long flags, boolean revert ) return (long) (distance * 3600 / speed); } - /** - * The callback used in forEveryEdge. - */ - private static interface EdgeVisitor - { - void next( EdgeIteratorState edgeBase, int index ); - } - /** * Iterates over all edges in this path sorted from start to end and calls the visitor callback * for every edge. *

+ * * @param visitor callback to handle every edge. The edge is decoupled from the iterator and can - * be stored. + * be stored. */ - private void forEveryEdge( EdgeVisitor visitor ) - { + private void forEveryEdge(EdgeVisitor visitor) { int tmpNode = getFromNode(); int len = edgeIds.size(); - for (int i = 0; i < len; i++) - { + for (int i = 0; i < len; i++) { EdgeIteratorState edgeBase = graph.getEdgeIteratorState(edgeIds.get(i), tmpNode); if (edgeBase == null) throw new IllegalStateException("Edge " + edgeIds.get(i) + " was empty when requested with node " + tmpNode @@ -299,17 +267,14 @@ private void forEveryEdge( EdgeVisitor visitor ) /** * Returns the list of all edges. */ - public List calcEdges() - { + public List calcEdges() { final List edges = new ArrayList(edgeIds.size()); if (edgeIds.isEmpty()) return edges; - forEveryEdge(new EdgeVisitor() - { + forEveryEdge(new EdgeVisitor() { @Override - public void next( EdgeIteratorState eb, int i ) - { + public void next(EdgeIteratorState eb, int i) { edges.add(eb); } }); @@ -319,13 +284,10 @@ public void next( EdgeIteratorState eb, int i ) /** * @return the uncached node indices of the tower nodes in this path. */ - public TIntList calcNodes() - { + public TIntList calcNodes() { final TIntArrayList nodes = new TIntArrayList(edgeIds.size() + 1); - if (edgeIds.isEmpty()) - { - if (isFound()) - { + if (edgeIds.isEmpty()) { + if (isFound()) { nodes.add(endNode); } return nodes; @@ -333,11 +295,9 @@ public TIntList calcNodes() int tmpNode = getFromNode(); nodes.add(tmpNode); - forEveryEdge(new EdgeVisitor() - { + forEveryEdge(new EdgeVisitor() { @Override - public void next( EdgeIteratorState eb, int i ) - { + public void next(EdgeIteratorState eb, int i) { nodes.add(eb.getAdjNode()); } }); @@ -347,15 +307,13 @@ public void next( EdgeIteratorState eb, int i ) /** * This method calculated a list of points for this path *

+ * * @return this path its geometry */ - public PointList calcPoints() - { + public PointList calcPoints() { final PointList points = new PointList(edgeIds.size() + 1, nodeAccess.is3D()); - if (edgeIds.isEmpty()) - { - if (isFound()) - { + if (edgeIds.isEmpty()) { + if (isFound()) { points.add(graph.getNodeAccess(), endNode); } return points; @@ -363,14 +321,11 @@ public PointList calcPoints() int tmpNode = getFromNode(); points.add(nodeAccess, tmpNode); - forEveryEdge(new EdgeVisitor() - { + forEveryEdge(new EdgeVisitor() { @Override - public void next( EdgeIteratorState eb, int index ) - { + public void next(EdgeIteratorState eb, int index) { PointList pl = eb.fetchWayGeometry(2); - for (int j = 0; j < pl.getSize(); j++) - { + for (int j = 0; j < pl.getSize(); j++) { points.add(pl, j); } } @@ -381,21 +336,17 @@ public void next( EdgeIteratorState eb, int index ) /** * @return the list of instructions for this path. */ - public InstructionList calcInstructions( final Translation tr ) - { + public InstructionList calcInstructions(final Translation tr) { final InstructionList ways = new InstructionList(edgeIds.size() / 4, tr); - if (edgeIds.isEmpty()) - { - if (isFound()) - { + if (edgeIds.isEmpty()) { + if (isFound()) { ways.add(new FinishInstruction(nodeAccess, endNode)); } return ways; } final int tmpNode = getFromNode(); - forEveryEdge(new EdgeVisitor() - { + forEveryEdge(new EdgeVisitor() { /* * We need three points to make directions * @@ -428,8 +379,7 @@ public InstructionList calcInstructions( final Translation tr ) private EdgeExplorer outEdgeExplorer = graph.createEdgeExplorer(new DefaultEdgeFilter(encoder, false, true)); @Override - public void next( EdgeIteratorState edge, int index ) - { + public void next(EdgeIteratorState edge, int index) { // baseNode is the current node and adjNode is the next int adjNode = edge.getAdjNode(); int baseNode = edge.getBaseNode(); @@ -441,12 +391,10 @@ public void next( EdgeIteratorState edge, int index ) PointList wayGeo = edge.fetchWayGeometry(3); boolean isRoundabout = encoder.isBool(flags, FlagEncoder.K_ROUNDABOUT); - if (wayGeo.getSize() <= 2) - { + if (wayGeo.getSize() <= 2) { latitude = adjLat; longitude = adjLon; - } else - { + } else { latitude = wayGeo.getLatitude(1); longitude = wayGeo.getLongitude(1); assert java.lang.Double.compare(prevLat, nodeAccess.getLatitude(baseNode)) == 0; @@ -464,23 +412,19 @@ public void next( EdgeIteratorState edge, int index ) prevName = name; prevAnnotation = annotation; - } else if (isRoundabout) - // remark: names and annotations within roundabout are ignored - { + } else if (isRoundabout) { + // remark: names and annotations within roundabout are ignored if (!prevInRoundabout) //just entered roundabout { int sign = Instruction.USE_ROUNDABOUT; RoundaboutInstruction roundaboutInstruction = new RoundaboutInstruction(sign, name, annotation, new PointList(10, nodeAccess.is3D())); - if (prevName != null) - { + if (prevName != null) { // check if there is an exit at the same node the roundabout was entered EdgeIterator edgeIter = outEdgeExplorer.setBaseNode(baseNode); - while (edgeIter.next()) - { + while (edgeIter.next()) { if ((edgeIter.getAdjNode() != prevNode) - && !encoder.isBool(edgeIter.getFlags(), FlagEncoder.K_ROUNDABOUT)) - { + && !encoder.isBool(edgeIter.getFlags(), FlagEncoder.K_ROUNDABOUT)) { roundaboutInstruction.increaseExitNumber(); break; } @@ -509,10 +453,8 @@ public void next( EdgeIteratorState edge, int index ) // Add passed exits to instruction. A node is counted if there is at least one outgoing edge // out of the roundabout EdgeIterator edgeIter = outEdgeExplorer.setBaseNode(adjNode); - while (edgeIter.next()) - { - if (!encoder.isBool(edgeIter.getFlags(), FlagEncoder.K_ROUNDABOUT)) - { + while (edgeIter.next()) { + if (!encoder.isBool(edgeIter.getFlags(), FlagEncoder.K_ROUNDABOUT)) { ((RoundaboutInstruction) prevInstruction).increaseExitNumber(); break; } @@ -542,8 +484,7 @@ public void next( EdgeIteratorState edge, int index ) prevName = name; prevAnnotation = annotation; - } else if ((!name.equals(prevName)) || (!annotation.equals(prevAnnotation))) - { + } else if ((!name.equals(prevName)) || (!annotation.equals(prevAnnotation))) { prevOrientation = AC.calcOrientation(doublePrevLat, doublePrevLong, prevLat, prevLon); double orientation = AC.calcOrientation(prevLat, prevLon, latitude, longitude); orientation = AC.alignOrientation(prevOrientation, orientation); @@ -551,21 +492,18 @@ public void next( EdgeIteratorState edge, int index ) double absDelta = Math.abs(delta); int sign; - if (absDelta < 0.2) - { + if (absDelta < 0.2) { // 0.2 ~= 11° sign = Instruction.CONTINUE_ON_STREET; - } else if (absDelta < 0.8) - { + } else if (absDelta < 0.8) { // 0.8 ~= 40° if (delta > 0) sign = Instruction.TURN_SLIGHT_LEFT; else sign = Instruction.TURN_SLIGHT_RIGHT; - } else if (absDelta < 1.8) - { + } else if (absDelta < 1.8) { // 1.8 ~= 103° if (delta > 0) sign = Instruction.TURN_LEFT; @@ -584,12 +522,10 @@ public void next( EdgeIteratorState edge, int index ) updatePointsAndInstruction(edge, wayGeo); - if (wayGeo.getSize() <= 2) - { + if (wayGeo.getSize() <= 2) { doublePrevLat = prevLat; doublePrevLong = prevLon; - } else - { + } else { int beforeLast = wayGeo.getSize() - 2; doublePrevLat = wayGeo.getLatitude(beforeLast); doublePrevLong = wayGeo.getLongitude(beforeLast); @@ -601,10 +537,8 @@ public void next( EdgeIteratorState edge, int index ) prevLon = adjLon; boolean lastEdge = index == edgeIds.size() - 1; - if (lastEdge) - { - if (isRoundabout) - { + if (lastEdge) { + if (isRoundabout) { // calc angle between roundabout entrance and finish double orientation = AC.calcOrientation(doublePrevLat, doublePrevLong, prevLat, prevLon); orientation = AC.alignOrientation(prevOrientation, orientation); @@ -616,12 +550,10 @@ public void next( EdgeIteratorState edge, int index ) } } - private void updatePointsAndInstruction( EdgeIteratorState edge, PointList pl ) - { + private void updatePointsAndInstruction(EdgeIteratorState edge, PointList pl) { // skip adjNode int len = pl.size() - 1; - for (int i = 0; i < len; i++) - { + for (int i = 0; i < len; i++) { prevInstruction.getPoints().add(pl, i); } double newDist = edge.getDistance(); @@ -635,16 +567,13 @@ private void updatePointsAndInstruction( EdgeIteratorState edge, PointList pl ) } @Override - public String toString() - { + public String toString() { return "distance:" + getDistance() + ", edges:" + edgeIds.size(); } - public String toDetailsString() - { + public String toDetailsString() { String str = ""; - for (int i = 0; i < edgeIds.size(); i++) - { + for (int i = 0; i < edgeIds.size(); i++) { if (i > 0) str += "->"; @@ -652,4 +581,11 @@ public String toDetailsString() } return toString() + ", found:" + isFound() + ", " + str; } + + /** + * The callback used in forEveryEdge. + */ + private static interface EdgeVisitor { + void next(EdgeIteratorState edgeBase, int index); + } } diff --git a/core/src/main/java/com/graphhopper/routing/PathBidirRef.java b/core/src/main/java/com/graphhopper/routing/PathBidirRef.java index 5bfb551c5b4..d22c2ff5000 100644 --- a/core/src/main/java/com/graphhopper/routing/PathBidirRef.java +++ b/core/src/main/java/com/graphhopper/routing/PathBidirRef.java @@ -18,40 +18,36 @@ package com.graphhopper.routing; import com.graphhopper.routing.util.FlagEncoder; -import com.graphhopper.storage.SPTEntry; import com.graphhopper.storage.Graph; +import com.graphhopper.storage.SPTEntry; import com.graphhopper.util.EdgeIterator; /** * This class creates a DijkstraPath from two Edge's resulting from a BidirectionalDijkstra *

+ * * @author Peter Karich */ -public class PathBidirRef extends Path -{ +public class PathBidirRef extends Path { protected SPTEntry edgeTo; private boolean switchWrapper = false; - public PathBidirRef( Graph g, FlagEncoder encoder ) - { + public PathBidirRef(Graph g, FlagEncoder encoder) { super(g, encoder); } - PathBidirRef( PathBidirRef p ) - { + PathBidirRef(PathBidirRef p) { super(p); edgeTo = p.edgeTo; switchWrapper = p.switchWrapper; } - public PathBidirRef setSwitchToFrom( boolean b ) - { + public PathBidirRef setSwitchToFrom(boolean b) { switchWrapper = b; return this; } - public PathBidirRef setSPTEntryTo( SPTEntry edgeTo ) - { + public PathBidirRef setSPTEntryTo(SPTEntry edgeTo) { this.edgeTo = edgeTo; return this; } @@ -60,8 +56,7 @@ public PathBidirRef setSPTEntryTo( SPTEntry edgeTo ) * Extracts path from two shortest-path-tree */ @Override - public Path extract() - { + public Path extract() { if (sptEntry == null || edgeTo == null) return this; @@ -69,16 +64,14 @@ public Path extract() throw new IllegalStateException("Locations of the 'to'- and 'from'-Edge has to be the same." + toString() + ", fromEntry:" + sptEntry + ", toEntry:" + edgeTo); extractSW.start(); - if (switchWrapper) - { + if (switchWrapper) { SPTEntry ee = sptEntry; sptEntry = edgeTo; edgeTo = ee; } SPTEntry currEdge = sptEntry; - while (EdgeIterator.Edge.isValid(currEdge.edge)) - { + while (EdgeIterator.Edge.isValid(currEdge.edge)) { processEdge(currEdge.edge, currEdge.adjNode); currEdge = currEdge.parent; } @@ -86,8 +79,7 @@ public Path extract() reverseOrder(); currEdge = edgeTo; int tmpEdge = currEdge.edge; - while (EdgeIterator.Edge.isValid(tmpEdge)) - { + while (EdgeIterator.Edge.isValid(tmpEdge)) { currEdge = currEdge.parent; processEdge(tmpEdge, currEdge.adjNode); tmpEdge = currEdge.edge; diff --git a/core/src/main/java/com/graphhopper/routing/PathNative.java b/core/src/main/java/com/graphhopper/routing/PathNative.java index 6fbe9e16131..310969d8a32 100644 --- a/core/src/main/java/com/graphhopper/routing/PathNative.java +++ b/core/src/main/java/com/graphhopper/routing/PathNative.java @@ -24,15 +24,14 @@ /** * This class creates a Path from a DijkstraOneToMany node *

+ * * @author Peter Karich */ -public class PathNative extends Path -{ +public class PathNative extends Path { private final int[] parentNodes; private final int[] parentEdges; - public PathNative( Graph g, FlagEncoder encoder, int[] parentNodes, int[] parentEdges ) - { + public PathNative(Graph g, FlagEncoder encoder, int[] parentNodes, int[] parentEdges) { super(g, encoder); this.parentNodes = parentNodes; this.parentEdges = parentEdges; @@ -42,13 +41,11 @@ public PathNative( Graph g, FlagEncoder encoder, int[] parentNodes, int[] parent * Extracts path from two shortest-path-tree */ @Override - public Path extract() - { + public Path extract() { if (endNode < 0) return this; - while (true) - { + while (true) { int edgeId = parentEdges[endNode]; if (!EdgeIterator.Edge.isValid(edgeId)) break; diff --git a/core/src/main/java/com/graphhopper/routing/QueryGraph.java b/core/src/main/java/com/graphhopper/routing/QueryGraph.java index 3a18acfa552..4acb3453f88 100644 --- a/core/src/main/java/com/graphhopper/routing/QueryGraph.java +++ b/core/src/main/java/com/graphhopper/routing/QueryGraph.java @@ -20,7 +20,10 @@ import com.graphhopper.routing.util.AllEdgesIterator; import com.graphhopper.routing.util.DefaultEdgeFilter; import com.graphhopper.routing.util.EdgeFilter; -import com.graphhopper.storage.*; +import com.graphhopper.storage.Graph; +import com.graphhopper.storage.GraphExtension; +import com.graphhopper.storage.NodeAccess; +import com.graphhopper.storage.TurnCostExtension; import com.graphhopper.storage.index.QueryResult; import com.graphhopper.util.*; import com.graphhopper.util.shapes.BBox; @@ -43,35 +46,105 @@ * * @author Peter Karich */ -public class QueryGraph implements Graph -{ +public class QueryGraph implements Graph { + final static int VE_BASE = 0, VE_BASE_REV = 1, VE_ADJ = 2, VE_ADJ_REV = 3; + private static final AngleCalc AC = Helper.ANGLE_CALC; private final Graph mainGraph; private final NodeAccess mainNodeAccess; private final int mainNodes; private final int mainEdges; private final QueryGraph baseGraph; private final GraphExtension wrappedExtension; - private List queryResults; + // TODO when spreading it on different threads we need multiple independent explorers + private final Map cacheMap = new HashMap(4); /** * Virtual edges are created between existing graph and new virtual tower nodes. For every * virtual node there are 4 edges: base-snap, snap-base, snap-adj, adj-snap. */ List virtualEdges; - final static int VE_BASE = 0, VE_BASE_REV = 1, VE_ADJ = 2, VE_ADJ_REV = 3; - + private List queryResults; /** * Store lat,lon of virtual tower nodes. */ private PointList virtualNodes; - private static final AngleCalc AC = Helper.ANGLE_CALC; - private List modifiedEdges = new ArrayList(5); + private final NodeAccess nodeAccess = new NodeAccess() { + @Override + public void ensureNode(int nodeId) { + mainNodeAccess.ensureNode(nodeId); + } - // TODO when spreading it on different threads we need multiple independent explorers - private final Map cacheMap = new HashMap(4); + @Override + public boolean is3D() { + return mainNodeAccess.is3D(); + } + + @Override + public int getDimension() { + return mainNodeAccess.getDimension(); + } + + @Override + public double getLatitude(int nodeId) { + if (isVirtualNode(nodeId)) + return virtualNodes.getLatitude(nodeId - mainNodes); + return mainNodeAccess.getLatitude(nodeId); + } + + @Override + public double getLongitude(int nodeId) { + if (isVirtualNode(nodeId)) + return virtualNodes.getLongitude(nodeId - mainNodes); + return mainNodeAccess.getLongitude(nodeId); + } + + @Override + public double getElevation(int nodeId) { + if (isVirtualNode(nodeId)) + return virtualNodes.getElevation(nodeId - mainNodes); + return mainNodeAccess.getElevation(nodeId); + } + + @Override + public int getAdditionalNodeField(int nodeId) { + if (isVirtualNode(nodeId)) + return 0; + return mainNodeAccess.getAdditionalNodeField(nodeId); + } + + @Override + public void setNode(int nodeId, double lat, double lon) { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public void setNode(int nodeId, double lat, double lon, double ele) { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public void setAdditionalNodeField(int nodeId, int additionalValue) { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public double getLat(int nodeId) { + return getLatitude(nodeId); + } + + @Override + public double getLon(int nodeId) { + return getLongitude(nodeId); + } + + @Override + public double getEle(int nodeId) { + return getElevation(nodeId); + } + }; + private List modifiedEdges = new ArrayList(5); private boolean useEdgeExplorerCache = false; - public QueryGraph( Graph graph ) - { + public QueryGraph(Graph graph) { mainGraph = graph; mainNodeAccess = graph.getNodeAccess(); mainNodes = graph.getNodes(); @@ -83,12 +156,10 @@ public QueryGraph( Graph graph ) wrappedExtension = mainGraph.getExtension(); // create very lightweight QueryGraph which uses variables from this QueryGraph (same virtual edges) - baseGraph = new QueryGraph(graph.getBaseGraph(), this) - { + baseGraph = new QueryGraph(graph.getBaseGraph(), this) { // override method to avoid stackoverflow @Override - public QueryGraph setUseEdgeExplorerCache( boolean useEECache ) - { + public QueryGraph setUseEdgeExplorerCache(boolean useEECache) { baseGraph.useEdgeExplorerCache = useEECache; return baseGraph; } @@ -98,8 +169,7 @@ public QueryGraph setUseEdgeExplorerCache( boolean useEECache ) /** * See 'lookup' for further variables that are initialized */ - private QueryGraph( Graph graph, QueryGraph superQueryGraph ) - { + private QueryGraph(Graph graph, QueryGraph superQueryGraph) { mainGraph = graph; baseGraph = this; wrappedExtension = superQueryGraph.wrappedExtension; @@ -111,8 +181,7 @@ private QueryGraph( Graph graph, QueryGraph superQueryGraph ) /** * Convenient method to initialize this QueryGraph with the two specified query results. */ - public QueryGraph lookup( QueryResult fromRes, QueryResult toRes ) - { + public QueryGraph lookup(QueryResult fromRes, QueryResult toRes) { List results = new ArrayList(2); results.add(fromRes); results.add(toRes); @@ -124,8 +193,7 @@ public QueryGraph lookup( QueryResult fromRes, QueryResult toRes ) * For all specified query results calculate snapped point and set closest node and edge to a * virtual one if necessary. Additionally the wayIndex can change if an edge is swapped. */ - public void lookup( List resList ) - { + public void lookup(List resList) { if (isInitialized()) throw new IllegalStateException("Call lookup only once. Otherwise you'll have problems for queries sharing the same edge."); @@ -141,8 +209,7 @@ public void lookup( List resList ) // Phase 1 // calculate snapped point and swap direction of closest edge if necessary - for (QueryResult res : resList) - { + for (QueryResult res : resList) { // Do not create virtual node for a query result if it is directly on a tower node or not found if (res.getSnappedPosition() == QueryResult.Position.TOWER) continue; @@ -153,24 +220,22 @@ public void lookup( List resList ) int base = closestEdge.getBaseNode(); - // Force the identical direction for all closest edges. + // Force the identical direction for all closest edges. // It is important to sort multiple results for the same edge by its wayIndex boolean doReverse = base > closestEdge.getAdjNode(); - if (base == closestEdge.getAdjNode()) - { + if (base == closestEdge.getAdjNode()) { // check for special case #162 where adj == base and force direction via latitude comparison PointList pl = closestEdge.fetchWayGeometry(0); if (pl.size() > 1) doReverse = pl.getLatitude(0) > pl.getLatitude(pl.size() - 1); } - if (doReverse) - { + if (doReverse) { closestEdge = closestEdge.detach(true); PointList fullPL = closestEdge.fetchWayGeometry(3); res.setClosestEdge(closestEdge); if (res.getSnappedPosition() == QueryResult.Position.PILLAR) - // ON pillar node + // ON pillar node res.setWayIndex(fullPL.getSize() - res.getWayIndex() - 1); else // for case "OFF pillar node" @@ -183,8 +248,7 @@ public void lookup( List resList ) // find multiple results on same edge int edgeId = closestEdge.getEdge(); List list = edge2res.get(edgeId); - if (list == null) - { + if (list == null) { list = new ArrayList(5); edge2res.put(edgeId, list); } @@ -194,24 +258,19 @@ public void lookup( List resList ) // Phase 2 - now it is clear which points cut one edge // 1. create point lists // 2. create virtual edges between virtual nodes and its neighbor (virtual or normal nodes) - edge2res.forEachValue(new TObjectProcedure>() - { + edge2res.forEachValue(new TObjectProcedure>() { @Override - public boolean execute( List results ) - { + public boolean execute(List results) { // we can expect at least one entry in the results EdgeIteratorState closestEdge = results.get(0).getClosestEdge(); final PointList fullPL = closestEdge.fetchWayGeometry(3); int baseNode = closestEdge.getBaseNode(); // sort results on the same edge by the wayIndex and if equal by distance to pillar node - Collections.sort(results, new Comparator() - { + Collections.sort(results, new Comparator() { @Override - public int compare( QueryResult o1, QueryResult o2 ) - { + public int compare(QueryResult o1, QueryResult o2) { int diff = o1.getWayIndex() - o2.getWayIndex(); - if (diff == 0) - { + if (diff == 0) { // sort by distance from snappedPoint to fullPL.get(wayIndex) if wayIndex is identical GHPoint p1 = o1.getSnappedPoint(); GHPoint p2 = o2.getSnappedPoint(); @@ -240,10 +299,9 @@ public int compare( QueryResult o1, QueryResult o2 ) boolean addedEdges = false; // Create base and adjacent PointLists for all none-equal virtual nodes. - // We do so via inserting them at the correct position of fullPL and cutting the + // We do so via inserting them at the correct position of fullPL and cutting the // fullPL into the right pieces. - for (int counter = 0; counter < results.size(); counter++) - { + for (int counter = 0; counter < results.size(); counter++) { QueryResult res = results.get(counter); if (res.getClosestEdge().getBaseNode() != baseNode) throw new IllegalStateException("Base nodes have to be identical but were not: " + closestEdge + " vs " + res.getClosestEdge()); @@ -251,8 +309,7 @@ public int compare( QueryResult o1, QueryResult o2 ) GHPoint3D currSnapped = res.getSnappedPoint(); // no new virtual nodes if exactly the same snapped point - if (prevPoint.equals(currSnapped)) - { + if (prevPoint.equals(currSnapped)) { res.setClosestNode(prevNodeId); continue; } @@ -266,8 +323,7 @@ public int compare( QueryResult o1, QueryResult o2 ) virtualNodes.add(currSnapped.lat, currSnapped.lon, currSnapped.ele); // add edges again to set adjacent edges for newVirtNodeId - if (addedEdges) - { + if (addedEdges) { virtualEdges.add(virtualEdges.get(virtualEdges.size() - 2)); virtualEdges.add(virtualEdges.get(virtualEdges.size() - 2)); } @@ -293,21 +349,18 @@ public int compare( QueryResult o1, QueryResult o2 ) } @Override - public Graph getBaseGraph() - { - // Note: if the mainGraph of this QueryGraph is a CHGraph then ignoring the shortcuts will produce a + public Graph getBaseGraph() { + // Note: if the mainGraph of this QueryGraph is a CHGraph then ignoring the shortcuts will produce a // huge gap of edgeIds between base and virtual edge ids. The only solution would be to move virtual edges - // directly after normal edge ids which is ugly as we limit virtual edges to N edges and waste memory or make everything more complex. + // directly after normal edge ids which is ugly as we limit virtual edges to N edges and waste memory or make everything more complex. return baseGraph; } - public boolean isVirtualEdge( int edgeId ) - { + public boolean isVirtualEdge(int edgeId) { return edgeId >= mainEdges; } - public boolean isVirtualNode( int nodeId ) - { + public boolean isVirtualNode(int nodeId) { return nodeId >= mainNodes; } @@ -319,58 +372,21 @@ public boolean isVirtualNode( int nodeId ) * Currently we can cache only the ALL_EDGES filter or instances of the DefaultEdgeFilter where * three edge explorers will be created: forward OR backward OR both. */ - public QueryGraph setUseEdgeExplorerCache( boolean useEECache ) - { + public QueryGraph setUseEdgeExplorerCache(boolean useEECache) { this.useEdgeExplorerCache = useEECache; this.baseGraph.setUseEdgeExplorerCache(useEECache); return this; } - class QueryGraphTurnExt extends TurnCostExtension - { - private final TurnCostExtension mainTurnExtension; - - public QueryGraphTurnExt() - { - this.mainTurnExtension = (TurnCostExtension) mainGraph.getExtension(); - } - - @Override - public long getTurnCostFlags( int edgeFrom, int nodeVia, int edgeTo ) - { - if (isVirtualNode(nodeVia)) - { - return 0; - } else if (isVirtualEdge(edgeFrom) || isVirtualEdge(edgeTo)) - { - if (isVirtualEdge(edgeFrom)) - { - edgeFrom = queryResults.get((edgeFrom - mainEdges) / 4).getClosestEdge().getEdge(); - } - if (isVirtualEdge(edgeTo)) - { - edgeTo = queryResults.get((edgeTo - mainEdges) / 4).getClosestEdge().getEdge(); - } - return mainTurnExtension.getTurnCostFlags(edgeFrom, nodeVia, edgeTo); - - } else - { - return mainTurnExtension.getTurnCostFlags(edgeFrom, nodeVia, edgeTo); - } - } - } - - private void createEdges( int origTraversalKey, int origRevTraversalKey, - GHPoint3D prevSnapped, int prevWayIndex, GHPoint3D currSnapped, int wayIndex, - PointList fullPL, EdgeIteratorState closestEdge, - int prevNodeId, int nodeId, long reverseFlags ) - { + private void createEdges(int origTraversalKey, int origRevTraversalKey, + GHPoint3D prevSnapped, int prevWayIndex, GHPoint3D currSnapped, int wayIndex, + PointList fullPL, EdgeIteratorState closestEdge, + int prevNodeId, int nodeId, long reverseFlags) { int max = wayIndex + 1; // basePoints must have at least the size of 2 to make sure fetchWayGeometry(3) returns at least 2 PointList basePoints = new PointList(max - prevWayIndex + 1, mainNodeAccess.is3D()); basePoints.add(prevSnapped.lat, prevSnapped.lon, prevSnapped.ele); - for (int i = prevWayIndex; i < max; i++) - { + for (int i = prevWayIndex; i < max; i++) { basePoints.add(fullPL, i); } basePoints.add(currSnapped.lat, currSnapped.lon, currSnapped.ele); @@ -393,13 +409,13 @@ private void createEdges( int origTraversalKey, int origRevTraversalKey, * Set those edges at the virtual node (nodeId) to 'unfavored' that require at least a turn of * 100° from favoredHeading *

- * @param nodeId VirtualNode at which edges get unfavored + * + * @param nodeId VirtualNode at which edges get unfavored * @param favoredHeading north based azimuth of favored heading between 0 and 360 - * @param incoming if true, incoming edges are unfavored, else outgoing edges + * @param incoming if true, incoming edges are unfavored, else outgoing edges * @return boolean indicating if enforcement took place */ - public boolean enforceHeading( int nodeId, double favoredHeading, boolean incoming ) - { + public boolean enforceHeading(int nodeId, double favoredHeading, boolean incoming) { if (!isInitialized()) throw new IllegalStateException("QueryGraph.lookup has to be called in before heading enforcement"); @@ -415,19 +431,16 @@ public boolean enforceHeading( int nodeId, double favoredHeading, boolean incomi // either penalize incoming or outgoing edges List edgePositions = incoming ? Arrays.asList(VE_BASE, VE_ADJ_REV) : Arrays.asList(VE_BASE_REV, VE_ADJ); boolean enforcementOccurred = false; - for (int edgePos : edgePositions) - { + for (int edgePos : edgePositions) { VirtualEdgeIteratorState edge = virtualEdges.get(virtNodeIDintern * 4 + edgePos); PointList wayGeo = edge.fetchWayGeometry(3); double edgeOrientation; - if (incoming) - { + if (incoming) { int numWayPoints = wayGeo.getSize(); edgeOrientation = AC.calcOrientation(wayGeo.getLat(numWayPoints - 2), wayGeo.getLon(numWayPoints - 2), wayGeo.getLat(numWayPoints - 1), wayGeo.getLon(numWayPoints - 1)); - } else - { + } else { edgeOrientation = AC.calcOrientation(wayGeo.getLat(0), wayGeo.getLon(0), wayGeo.getLat(1), wayGeo.getLon(1)); } @@ -454,13 +467,13 @@ public boolean enforceHeading( int nodeId, double favoredHeading, boolean incomi * Set one specific edge at the virtual node with nodeId to 'unfavored' to enforce routing along * other edges *

- * @param nodeId VirtualNode at which edges get unfavored - * @param edgeId edge to become unfavored + * + * @param nodeId VirtualNode at which edges get unfavored + * @param edgeId edge to become unfavored * @param incoming if true, incoming edge is unfavored, else outgoing edge * @return boolean indicating if enforcement took place */ - public boolean enforceHeadingByEdgeId( int nodeId, int edgeId, boolean incoming ) - { + public boolean enforceHeadingByEdgeId(int nodeId, int edgeId, boolean incoming) { if (!isVirtualNode(nodeId)) return false; @@ -476,124 +489,29 @@ public boolean enforceHeadingByEdgeId( int nodeId, int edgeId, boolean incoming /** * Removes the 'unfavored' status of all virtual edges. */ - public void clearUnfavoredStatus() - { - for (VirtualEdgeIteratorState edge : modifiedEdges) - { + public void clearUnfavoredStatus() { + for (VirtualEdgeIteratorState edge : modifiedEdges) { edge.setUnfavored(false); } } @Override - public int getNodes() - { + public int getNodes() { return virtualNodes.getSize() + mainNodes; } @Override - public NodeAccess getNodeAccess() - { + public NodeAccess getNodeAccess() { return nodeAccess; } - private final NodeAccess nodeAccess = new NodeAccess() - { - @Override - public void ensureNode( int nodeId ) - { - mainNodeAccess.ensureNode(nodeId); - } - - @Override - public boolean is3D() - { - return mainNodeAccess.is3D(); - } - - @Override - public int getDimension() - { - return mainNodeAccess.getDimension(); - } - - @Override - public double getLatitude( int nodeId ) - { - if (isVirtualNode(nodeId)) - return virtualNodes.getLatitude(nodeId - mainNodes); - return mainNodeAccess.getLatitude(nodeId); - } - - @Override - public double getLongitude( int nodeId ) - { - if (isVirtualNode(nodeId)) - return virtualNodes.getLongitude(nodeId - mainNodes); - return mainNodeAccess.getLongitude(nodeId); - } - - @Override - public double getElevation( int nodeId ) - { - if (isVirtualNode(nodeId)) - return virtualNodes.getElevation(nodeId - mainNodes); - return mainNodeAccess.getElevation(nodeId); - } - - @Override - public int getAdditionalNodeField( int nodeId ) - { - if (isVirtualNode(nodeId)) - return 0; - return mainNodeAccess.getAdditionalNodeField(nodeId); - } - - @Override - public void setNode( int nodeId, double lat, double lon ) - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public void setNode( int nodeId, double lat, double lon, double ele ) - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public void setAdditionalNodeField( int nodeId, int additionalValue ) - { - throw new UnsupportedOperationException("Not supported yet."); - } - - @Override - public double getLat( int nodeId ) - { - return getLatitude(nodeId); - } - - @Override - public double getLon( int nodeId ) - { - return getLongitude(nodeId); - } - - @Override - public double getEle( int nodeId ) - { - return getElevation(nodeId); - } - }; - @Override - public BBox getBounds() - { + public BBox getBounds() { return mainGraph.getBounds(); } @Override - public EdgeIteratorState getEdgeIteratorState( int origEdgeId, int adjNode ) - { + public EdgeIteratorState getEdgeIteratorState(int origEdgeId, int adjNode) { if (!isVirtualEdge(origEdgeId)) return mainGraph.getEdgeIteratorState(origEdgeId, adjNode); @@ -610,8 +528,7 @@ public EdgeIteratorState getEdgeIteratorState( int origEdgeId, int adjNode ) + ". found edges were:" + eis + ", " + eis2); } - private int getPosOfReverseEdge( int edgeId ) - { + private int getPosOfReverseEdge(int edgeId) { // find reverse edge via convention. see virtualEdges comment above if (edgeId % 2 == 0) edgeId++; @@ -622,16 +539,13 @@ private int getPosOfReverseEdge( int edgeId ) } @Override - public EdgeExplorer createEdgeExplorer( final EdgeFilter edgeFilter ) - { + public EdgeExplorer createEdgeExplorer(final EdgeFilter edgeFilter) { if (!isInitialized()) throw new IllegalStateException("Call lookup before using this graph"); - if (useEdgeExplorerCache) - { + if (useEdgeExplorerCache) { int counter = -1; - if (edgeFilter instanceof DefaultEdgeFilter) - { + if (edgeFilter instanceof DefaultEdgeFilter) { DefaultEdgeFilter dee = (DefaultEdgeFilter) edgeFilter; counter = 0; if (dee.acceptsBackward()) @@ -642,16 +556,13 @@ public EdgeExplorer createEdgeExplorer( final EdgeFilter edgeFilter ) if (counter == 0) throw new IllegalStateException("You tried to use an edge filter blocking every access"); - } else if (edgeFilter == EdgeFilter.ALL_EDGES) - { + } else if (edgeFilter == EdgeFilter.ALL_EDGES) { counter = 4; } - if (counter >= 0) - { + if (counter >= 0) { EdgeExplorer cached = cacheMap.get(counter); - if (cached == null) - { + if (cached == null) { cached = createUncachedEdgeExplorer(edgeFilter); cacheMap.put(counter, cached); } @@ -661,8 +572,7 @@ public EdgeExplorer createEdgeExplorer( final EdgeFilter edgeFilter ) return createUncachedEdgeExplorer(edgeFilter); } - private EdgeExplorer createUncachedEdgeExplorer( EdgeFilter edgeFilter ) - { + private EdgeExplorer createUncachedEdgeExplorer(EdgeFilter edgeFilter) { // Iteration over virtual nodes needs to be thread safe if done from different explorer // so we need to create the mapping on EVERY call! // This needs to be a HashMap (and cannot be an array) as we also need to tweak edges for some mainNodes! @@ -673,9 +583,8 @@ private EdgeExplorer createUncachedEdgeExplorer( EdgeFilter edgeFilter ) final EdgeExplorer mainExplorer = mainGraph.createEdgeExplorer(edgeFilter); final TIntHashSet towerNodesToChange = new TIntHashSet(queryResults.size()); - // 1. virtualEdges should also get fresh EdgeIterators on every createEdgeExplorer call! - for (int i = 0; i < queryResults.size(); i++) - { + // 1. virtualEdges should also get fresh EdgeIterators on every createEdgeExplorer call! + for (int i = 0; i < queryResults.size(); i++) { // create outgoing edges VirtualEdgeIterator virtEdgeIter = new VirtualEdgeIterator(2); EdgeIteratorState baseRevEdge = virtualEdges.get(i * 4 + VE_BASE_REV); @@ -688,21 +597,19 @@ private EdgeExplorer createUncachedEdgeExplorer( EdgeFilter edgeFilter ) int virtNode = mainNodes + i; node2EdgeMap.put(virtNode, virtEdgeIter); - // replace edge list of neighboring tower nodes: + // replace edge list of neighboring tower nodes: // add virtual edges only and collect tower nodes where real edges will be added in step 2. // // base node int towerNode = baseRevEdge.getAdjNode(); - if (!isVirtualNode(towerNode)) - { + if (!isVirtualNode(towerNode)) { towerNodesToChange.add(towerNode); addVirtualEdges(node2EdgeMap, edgeFilter, true, towerNode, i); } // adj node towerNode = adjEdge.getAdjNode(); - if (!isVirtualNode(towerNode)) - { + if (!isVirtualNode(towerNode)) { towerNodesToChange.add(towerNode); addVirtualEdges(node2EdgeMap, edgeFilter, false, towerNode, i); } @@ -711,21 +618,17 @@ private EdgeExplorer createUncachedEdgeExplorer( EdgeFilter edgeFilter ) // 2. the connected tower nodes from mainGraph need fresh EdgeIterators with possible fakes // where 'fresh' means independent of previous call and respecting the edgeFilter // -> setup fake iterators of detected tower nodes (virtual edges are already added) - towerNodesToChange.forEach(new TIntProcedure() - { + towerNodesToChange.forEach(new TIntProcedure() { @Override - public boolean execute( int value ) - { + public boolean execute(int value) { fillVirtualEdges(node2EdgeMap, value, mainExplorer); return true; } }); - return new EdgeExplorer() - { + return new EdgeExplorer() { @Override - public EdgeIterator setBaseNode( int baseNode ) - { + public EdgeIterator setBaseNode(int baseNode) { VirtualEdgeIterator iter = node2EdgeMap.get(baseNode); if (iter != null) return iter.reset(); @@ -738,12 +641,10 @@ public EdgeIterator setBaseNode( int baseNode ) /** * Creates a fake edge iterator pointing to multiple edge states. */ - private void addVirtualEdges( TIntObjectMap node2EdgeMap, EdgeFilter filter, boolean base, - int node, int virtNode ) - { + private void addVirtualEdges(TIntObjectMap node2EdgeMap, EdgeFilter filter, boolean base, + int node, int virtNode) { VirtualEdgeIterator existingIter = node2EdgeMap.get(node); - if (existingIter == null) - { + if (existingIter == null) { existingIter = new VirtualEdgeIterator(10); node2EdgeMap.put(node, existingIter); } @@ -754,75 +655,89 @@ private void addVirtualEdges( TIntObjectMap node2EdgeMap, E existingIter.add(edge); } - void fillVirtualEdges( TIntObjectMap node2Edge, int towerNode, EdgeExplorer mainExpl ) - { + void fillVirtualEdges(TIntObjectMap node2Edge, int towerNode, EdgeExplorer mainExpl) { if (isVirtualNode(towerNode)) throw new IllegalStateException("Node should not be virtual:" + towerNode + ", " + node2Edge); VirtualEdgeIterator vIter = node2Edge.get(towerNode); TIntArrayList ignoreEdges = new TIntArrayList(vIter.count() * 2); - while (vIter.next()) - { + while (vIter.next()) { EdgeIteratorState edge = queryResults.get(vIter.getAdjNode() - mainNodes).getClosestEdge(); ignoreEdges.add(edge.getEdge()); } vIter.reset(); EdgeIterator iter = mainExpl.setBaseNode(towerNode); - while (iter.next()) - { + while (iter.next()) { if (!ignoreEdges.contains(iter.getEdge())) vIter.add(iter.detach(false)); } } - private boolean isInitialized() - { + private boolean isInitialized() { return queryResults != null; } @Override - public EdgeExplorer createEdgeExplorer() - { + public EdgeExplorer createEdgeExplorer() { return createEdgeExplorer(EdgeFilter.ALL_EDGES); } @Override - public AllEdgesIterator getAllEdges() - { + public AllEdgesIterator getAllEdges() { throw new UnsupportedOperationException("Not supported yet."); } @Override - public EdgeIteratorState edge( int a, int b ) - { + public EdgeIteratorState edge(int a, int b) { throw exc(); } - public EdgeIteratorState edge( int a, int b, double distance, int flags ) - { + public EdgeIteratorState edge(int a, int b, double distance, int flags) { throw exc(); } @Override - public EdgeIteratorState edge( int a, int b, double distance, boolean bothDirections ) - { + public EdgeIteratorState edge(int a, int b, double distance, boolean bothDirections) { throw exc(); } @Override - public Graph copyTo( Graph g ) - { + public Graph copyTo(Graph g) { throw exc(); } @Override - public GraphExtension getExtension() - { + public GraphExtension getExtension() { return wrappedExtension; } - private UnsupportedOperationException exc() - { + private UnsupportedOperationException exc() { return new UnsupportedOperationException("QueryGraph cannot be modified."); } + + class QueryGraphTurnExt extends TurnCostExtension { + private final TurnCostExtension mainTurnExtension; + + public QueryGraphTurnExt() { + this.mainTurnExtension = (TurnCostExtension) mainGraph.getExtension(); + } + + @Override + public long getTurnCostFlags(int edgeFrom, int nodeVia, int edgeTo) { + if (isVirtualNode(nodeVia)) { + return 0; + } else if (isVirtualEdge(edgeFrom) || isVirtualEdge(edgeTo)) { + if (isVirtualEdge(edgeFrom)) { + edgeFrom = queryResults.get((edgeFrom - mainEdges) / 4).getClosestEdge().getEdge(); + } + if (isVirtualEdge(edgeTo)) { + edgeTo = queryResults.get((edgeTo - mainEdges) / 4).getClosestEdge().getEdge(); + } + return mainTurnExtension.getTurnCostFlags(edgeFrom, nodeVia, edgeTo); + + } else { + return mainTurnExtension.getTurnCostFlags(edgeFrom, nodeVia, edgeTo); + } + } + } } diff --git a/core/src/main/java/com/graphhopper/routing/RoutingAlgorithm.java b/core/src/main/java/com/graphhopper/routing/RoutingAlgorithm.java index 33fa2e97772..26f863a4ead 100644 --- a/core/src/main/java/com/graphhopper/routing/RoutingAlgorithm.java +++ b/core/src/main/java/com/graphhopper/routing/RoutingAlgorithm.java @@ -18,34 +18,35 @@ package com.graphhopper.routing; import com.graphhopper.util.NotThreadSafe; + import java.util.List; /** * Calculates the shortest path from the specified node ids. Can be used only once. *

+ * * @author Peter Karich */ @NotThreadSafe -public interface RoutingAlgorithm -{ +public interface RoutingAlgorithm { /** * Calculates the best path between the specified nodes. * * @return the path. Call the method found() to make sure that the path is valid. */ - Path calcPath( int from, int to ); + Path calcPath(int from, int to); /** * Calculates multiple possibilities for a path. * * @see #calcPath(int, int) */ - List calcPaths( int from, int to ); + List calcPaths(int from, int to); /** * Limit the search to numberOfNodes. See #681 */ - void setMaxVisitedNodes( int numberOfNodes ); + void setMaxVisitedNodes(int numberOfNodes); /** * @return name of this algorithm diff --git a/core/src/main/java/com/graphhopper/routing/RoutingAlgorithmFactory.java b/core/src/main/java/com/graphhopper/routing/RoutingAlgorithmFactory.java index e01b55ad50c..8468fbc0288 100644 --- a/core/src/main/java/com/graphhopper/routing/RoutingAlgorithmFactory.java +++ b/core/src/main/java/com/graphhopper/routing/RoutingAlgorithmFactory.java @@ -24,7 +24,6 @@ * * @author Peter Karich */ -public interface RoutingAlgorithmFactory -{ - RoutingAlgorithm createAlgo( Graph g, AlgorithmOptions opts ); +public interface RoutingAlgorithmFactory { + RoutingAlgorithm createAlgo(Graph g, AlgorithmOptions opts); } diff --git a/core/src/main/java/com/graphhopper/routing/RoutingAlgorithmFactoryDecorator.java b/core/src/main/java/com/graphhopper/routing/RoutingAlgorithmFactoryDecorator.java index 78a30979db0..0ee3ebd3eeb 100644 --- a/core/src/main/java/com/graphhopper/routing/RoutingAlgorithmFactoryDecorator.java +++ b/core/src/main/java/com/graphhopper/routing/RoutingAlgorithmFactoryDecorator.java @@ -20,16 +20,14 @@ import com.graphhopper.routing.util.HintsMap; /** - * * @author Peter Karich */ -public interface RoutingAlgorithmFactoryDecorator -{ +public interface RoutingAlgorithmFactoryDecorator { boolean isEnabled(); /** * This method returns the specified algoFactory unchanged, decorates it or completely replaces * it depending on the specified optionsMap. */ - RoutingAlgorithmFactory getDecoratedAlgorithmFactory( RoutingAlgorithmFactory algoFactory, HintsMap optionsMap ); + RoutingAlgorithmFactory getDecoratedAlgorithmFactory(RoutingAlgorithmFactory algoFactory, HintsMap optionsMap); } diff --git a/core/src/main/java/com/graphhopper/routing/RoutingAlgorithmFactorySimple.java b/core/src/main/java/com/graphhopper/routing/RoutingAlgorithmFactorySimple.java index e1918eb39c4..2bd37dde6b1 100644 --- a/core/src/main/java/com/graphhopper/routing/RoutingAlgorithmFactorySimple.java +++ b/core/src/main/java/com/graphhopper/routing/RoutingAlgorithmFactorySimple.java @@ -22,47 +22,41 @@ import com.graphhopper.storage.Graph; import com.graphhopper.storage.NodeAccess; import com.graphhopper.util.Helper; + import static com.graphhopper.util.Parameters.Algorithms.*; import static com.graphhopper.util.Parameters.Algorithms.AltRoute.*; /** * A simple factory creating normal algorithms (RoutingAlgorithm) without preparation. *

+ * * @author Peter Karich */ -public class RoutingAlgorithmFactorySimple implements RoutingAlgorithmFactory -{ +public class RoutingAlgorithmFactorySimple implements RoutingAlgorithmFactory { @Override - public RoutingAlgorithm createAlgo( Graph g, AlgorithmOptions opts ) - { + public RoutingAlgorithm createAlgo(Graph g, AlgorithmOptions opts) { RoutingAlgorithm ra; String algoStr = opts.getAlgorithm(); - if (DIJKSTRA_BI.equalsIgnoreCase(algoStr)) - { + if (DIJKSTRA_BI.equalsIgnoreCase(algoStr)) { ra = new DijkstraBidirectionRef(g, opts.getFlagEncoder(), opts.getWeighting(), opts.getTraversalMode()); - } else if (DIJKSTRA.equalsIgnoreCase(algoStr)) - { + } else if (DIJKSTRA.equalsIgnoreCase(algoStr)) { ra = new Dijkstra(g, opts.getFlagEncoder(), opts.getWeighting(), opts.getTraversalMode()); - } else if (ASTAR_BI.equalsIgnoreCase(algoStr)) - { + } else if (ASTAR_BI.equalsIgnoreCase(algoStr)) { AStarBidirection aStarBi = new AStarBidirection(g, opts.getFlagEncoder(), opts.getWeighting(), opts.getTraversalMode()); aStarBi.setApproximation(getApproximation(ASTAR_BI, opts, g.getNodeAccess())); ra = aStarBi; - } else if (DIJKSTRA_ONE_TO_MANY.equalsIgnoreCase(algoStr)) - { + } else if (DIJKSTRA_ONE_TO_MANY.equalsIgnoreCase(algoStr)) { ra = new DijkstraOneToMany(g, opts.getFlagEncoder(), opts.getWeighting(), opts.getTraversalMode()); - } else if (ASTAR.equalsIgnoreCase(algoStr)) - { + } else if (ASTAR.equalsIgnoreCase(algoStr)) { AStar aStar = new AStar(g, opts.getFlagEncoder(), opts.getWeighting(), opts.getTraversalMode()); aStar.setApproximation(getApproximation(ASTAR, opts, g.getNodeAccess())); ra = aStar; - } else if (ALT_ROUTE.equalsIgnoreCase(algoStr)) - { + } else if (ALT_ROUTE.equalsIgnoreCase(algoStr)) { AlternativeRoute altRouteAlgo = new AlternativeRoute(g, opts.getFlagEncoder(), opts.getWeighting(), opts.getTraversalMode()); altRouteAlgo.setMaxPaths(opts.getHints().getInt(MAX_PATHS, 2)); altRouteAlgo.setMaxWeightFactor(opts.getHints().getDouble(MAX_WEIGHT, 1.4)); @@ -71,8 +65,7 @@ public RoutingAlgorithm createAlgo( Graph g, AlgorithmOptions opts ) altRouteAlgo.setMaxExplorationFactor(opts.getHints().getDouble("alternative_route.max_exploration_factor", 1)); ra = altRouteAlgo; - } else - { + } else { throw new IllegalArgumentException("Algorithm " + algoStr + " not found in " + getClass().getName()); } @@ -80,8 +73,7 @@ public RoutingAlgorithm createAlgo( Graph g, AlgorithmOptions opts ) return ra; } - private WeightApproximator getApproximation( String prop, AlgorithmOptions opts, NodeAccess na ) - { + private WeightApproximator getApproximation(String prop, AlgorithmOptions opts, NodeAccess na) { String approxAsStr = opts.getHints().get(prop + ".approximation", "BeelineSimplification"); double epsilon = opts.getHints().getDouble(prop + ".epsilon", 1); diff --git a/core/src/main/java/com/graphhopper/routing/VirtualEdgeIterator.java b/core/src/main/java/com/graphhopper/routing/VirtualEdgeIterator.java index ef5c19a31b7..c940072dfdc 100644 --- a/core/src/main/java/com/graphhopper/routing/VirtualEdgeIterator.java +++ b/core/src/main/java/com/graphhopper/routing/VirtualEdgeIterator.java @@ -18,7 +18,10 @@ package com.graphhopper.routing; import com.graphhopper.routing.util.FlagEncoder; -import com.graphhopper.util.*; +import com.graphhopper.util.CHEdgeIteratorState; +import com.graphhopper.util.EdgeIterator; +import com.graphhopper.util.EdgeIteratorState; +import com.graphhopper.util.PointList; import java.util.ArrayList; import java.util.List; @@ -26,197 +29,165 @@ /** * @author Peter Karich */ -class VirtualEdgeIterator implements EdgeIterator, CHEdgeIteratorState -{ +class VirtualEdgeIterator implements EdgeIterator, CHEdgeIteratorState { private final List edges; private int current; - public VirtualEdgeIterator( int edgeCount ) - { + public VirtualEdgeIterator(int edgeCount) { edges = new ArrayList(edgeCount); reset(); } - void add( EdgeIteratorState edge ) - { + void add(EdgeIteratorState edge) { edges.add(edge); } - EdgeIterator reset() - { + EdgeIterator reset() { current = -1; return this; } - int count() - { + int count() { return edges.size(); } @Override - public boolean next() - { + public boolean next() { current++; return current < edges.size(); } @Override - public EdgeIteratorState detach( boolean reverse ) - { + public EdgeIteratorState detach(boolean reverse) { if (reverse) throw new IllegalStateException("Not yet supported"); return edges.get(current); } @Override - public int getEdge() - { + public int getEdge() { return edges.get(current).getEdge(); } @Override - public int getBaseNode() - { + public int getBaseNode() { return edges.get(current).getBaseNode(); } @Override - public int getAdjNode() - { + public int getAdjNode() { return edges.get(current).getAdjNode(); } @Override - public PointList fetchWayGeometry( int mode ) - { + public PointList fetchWayGeometry(int mode) { return edges.get(current).fetchWayGeometry(mode); } @Override - public EdgeIteratorState setWayGeometry( PointList list ) - { + public EdgeIteratorState setWayGeometry(PointList list) { return edges.get(current).setWayGeometry(list); } @Override - public double getDistance() - { + public double getDistance() { return edges.get(current).getDistance(); } @Override - public EdgeIteratorState setDistance( double dist ) - { + public EdgeIteratorState setDistance(double dist) { return edges.get(current).setDistance(dist); } @Override - public long getFlags() - { + public long getFlags() { return edges.get(current).getFlags(); } @Override - public EdgeIteratorState setFlags( long flags ) - { + public EdgeIteratorState setFlags(long flags) { return edges.get(current).setFlags(flags); } @Override - public String getName() - { + public String getName() { return edges.get(current).getName(); } @Override - public EdgeIteratorState setName( String name ) - { + public EdgeIteratorState setName(String name) { return edges.get(current).setName(name); } @Override - public boolean getBool( int key, boolean _default ) - { + public boolean getBool(int key, boolean _default) { return edges.get(current).getBool(key, _default); } @Override - public String toString() - { + public String toString() { return edges.toString(); } @Override - public int getAdditionalField() - { + public int getAdditionalField() { return edges.get(current).getAdditionalField(); } @Override - public EdgeIteratorState setAdditionalField( int value ) - { + public EdgeIteratorState setAdditionalField(int value) { return edges.get(current).setAdditionalField(value); } @Override - public EdgeIteratorState copyPropertiesTo( EdgeIteratorState edge ) - { + public EdgeIteratorState copyPropertiesTo(EdgeIteratorState edge) { return edges.get(current).copyPropertiesTo(edge); } @Override - public boolean isBackward( FlagEncoder encoder ) - { + public boolean isBackward(FlagEncoder encoder) { return edges.get(current).isBackward(encoder); } @Override - public boolean isForward( FlagEncoder encoder ) - { + public boolean isForward(FlagEncoder encoder) { return edges.get(current).isForward(encoder); } @Override - public boolean isShortcut() - { + public boolean isShortcut() { EdgeIteratorState edge = edges.get(current); return edge instanceof CHEdgeIteratorState && ((CHEdgeIteratorState) edge).isShortcut(); } @Override - public double getWeight() - { + public double getWeight() { // will be called only from PreparationWeighting and if isShortcut is true return ((CHEdgeIteratorState) edges.get(current)).getWeight(); } @Override - public CHEdgeIteratorState setWeight( double weight ) - { + public CHEdgeIteratorState setWeight(double weight) { throw new UnsupportedOperationException("Not supported."); } @Override - public int getSkippedEdge1() - { + public int getSkippedEdge1() { throw new UnsupportedOperationException("Not supported."); } @Override - public int getSkippedEdge2() - { + public int getSkippedEdge2() { throw new UnsupportedOperationException("Not supported."); } @Override - public void setSkippedEdges( int edge1, int edge2 ) - { + public void setSkippedEdges(int edge1, int edge2) { throw new UnsupportedOperationException("Not supported."); } @Override - public boolean canBeOverwritten( long flags ) - { + public boolean canBeOverwritten(long flags) { throw new UnsupportedOperationException("Not supported."); } } diff --git a/core/src/main/java/com/graphhopper/routing/VirtualEdgeIteratorState.java b/core/src/main/java/com/graphhopper/routing/VirtualEdgeIteratorState.java index 3945c5c9190..f443f4b7030 100644 --- a/core/src/main/java/com/graphhopper/routing/VirtualEdgeIteratorState.java +++ b/core/src/main/java/com/graphhopper/routing/VirtualEdgeIteratorState.java @@ -18,7 +18,10 @@ package com.graphhopper.routing; import com.graphhopper.routing.util.FlagEncoder; -import com.graphhopper.util.*; +import com.graphhopper.util.CHEdgeIteratorState; +import com.graphhopper.util.EdgeIteratorState; +import com.graphhopper.util.GHUtility; +import com.graphhopper.util.PointList; /** * Creates an edge state decoupled from a graph where nodes, pointList, etc are kept in memory. @@ -26,21 +29,19 @@ * Note, this class is not suited for public use and can change with minor releases unexpectedly or * even gets removed. */ -public class VirtualEdgeIteratorState implements EdgeIteratorState, CHEdgeIteratorState -{ +public class VirtualEdgeIteratorState implements EdgeIteratorState, CHEdgeIteratorState { private final PointList pointList; private final int edgeId; - private double distance; - private long flags; - private String name; private final int baseNode; private final int adjNode; private final int originalTraversalKey; + private double distance; + private long flags; + private String name; // indication if edges are dispreferred as start/stop edge private boolean unfavored; - public VirtualEdgeIteratorState( int originalTraversalKey, int edgeId, int baseNode, int adjNode, double distance, long flags, String name, PointList pointList ) - { + public VirtualEdgeIteratorState(int originalTraversalKey, int edgeId, int baseNode, int adjNode, double distance, long flags, String name, PointList pointList) { this.originalTraversalKey = originalTraversalKey; this.edgeId = edgeId; this.baseNode = baseNode; @@ -55,34 +56,30 @@ public VirtualEdgeIteratorState( int originalTraversalKey, int edgeId, int baseN * This method returns the original edge via its traversal key. I.e. also the direction is * already correctly encoded. *

+ * * @see GHUtility#createEdgeKey(int, int, int, boolean) */ - public int getOriginalTraversalKey() - { + public int getOriginalTraversalKey() { return originalTraversalKey; } @Override - public int getEdge() - { + public int getEdge() { return edgeId; } @Override - public int getBaseNode() - { + public int getBaseNode() { return baseNode; } @Override - public int getAdjNode() - { + public int getAdjNode() { return adjNode; } @Override - public PointList fetchWayGeometry( int mode ) - { + public PointList fetchWayGeometry(int mode) { if (pointList.getSize() == 0) return PointList.EMPTY; // due to API we need to create a new instance per call! @@ -92,8 +89,7 @@ else if (mode == 1) return pointList.copy(0, pointList.getSize() - 1); else if (mode == 2) return pointList.copy(1, pointList.getSize()); - else if (mode == 0) - { + else if (mode == 0) { if (pointList.getSize() == 1) return PointList.EMPTY; return pointList.copy(1, pointList.getSize() - 1); @@ -102,53 +98,45 @@ else if (mode == 0) } @Override - public EdgeIteratorState setWayGeometry( PointList list ) - { + public EdgeIteratorState setWayGeometry(PointList list) { throw new UnsupportedOperationException("Not supported for virtual edge. Set when creating it."); } @Override - public double getDistance() - { + public double getDistance() { return distance; } @Override - public EdgeIteratorState setDistance( double dist ) - { + public EdgeIteratorState setDistance(double dist) { this.distance = dist; return this; } @Override - public long getFlags() - { + public long getFlags() { return flags; } @Override - public EdgeIteratorState setFlags( long flags ) - { + public EdgeIteratorState setFlags(long flags) { this.flags = flags; return this; } @Override - public String getName() - { + public String getName() { return name; } @Override - public EdgeIteratorState setName( String name ) - { + public EdgeIteratorState setName(String name) { this.name = name; return this; } @Override - public boolean getBool( int key, boolean _default ) - { + public boolean getBool(int key, boolean _default) { if (key == EdgeIteratorState.K_UNFAVORED_EDGE) return unfavored; @@ -159,92 +147,77 @@ public boolean getBool( int key, boolean _default ) /** * This method sets edge to unfavored status for routing from or to the start/stop points. */ - public void setUnfavored( boolean unfavored ) - { + public void setUnfavored(boolean unfavored) { this.unfavored = unfavored; } @Override - public String toString() - { + public String toString() { return baseNode + "->" + adjNode; } @Override - public boolean isShortcut() - { + public boolean isShortcut() { return false; } @Override - public boolean isForward( FlagEncoder encoder ) - { + public boolean isForward(FlagEncoder encoder) { return encoder.isForward(getFlags()); } @Override - public boolean isBackward( FlagEncoder encoder ) - { + public boolean isBackward(FlagEncoder encoder) { return encoder.isBackward(getFlags()); } @Override - public int getAdditionalField() - { + public int getAdditionalField() { throw new UnsupportedOperationException("Not supported."); } @Override - public boolean canBeOverwritten( long flags ) - { + public boolean canBeOverwritten(long flags) { throw new UnsupportedOperationException("Not supported."); } @Override - public int getSkippedEdge1() - { + public int getSkippedEdge1() { throw new UnsupportedOperationException("Not supported."); } @Override - public int getSkippedEdge2() - { + public int getSkippedEdge2() { throw new UnsupportedOperationException("Not supported."); } @Override - public void setSkippedEdges( int edge1, int edge2 ) - { + public void setSkippedEdges(int edge1, int edge2) { throw new UnsupportedOperationException("Not supported."); } @Override - public EdgeIteratorState detach( boolean reverse ) - { + public EdgeIteratorState detach(boolean reverse) { throw new UnsupportedOperationException("Not supported."); } @Override - public EdgeIteratorState setAdditionalField( int value ) - { + public EdgeIteratorState setAdditionalField(int value) { throw new UnsupportedOperationException("Not supported."); } @Override - public EdgeIteratorState copyPropertiesTo( EdgeIteratorState edge ) - { + public EdgeIteratorState copyPropertiesTo(EdgeIteratorState edge) { throw new UnsupportedOperationException("Not supported."); } @Override - public CHEdgeIteratorState setWeight( double weight ) - { + public CHEdgeIteratorState setWeight(double weight) { throw new UnsupportedOperationException("Not supported."); } @Override - public double getWeight() - { + public double getWeight() { throw new UnsupportedOperationException("Not supported."); } diff --git a/core/src/main/java/com/graphhopper/routing/ch/CHAlgoFactoryDecorator.java b/core/src/main/java/com/graphhopper/routing/ch/CHAlgoFactoryDecorator.java index c19661d2251..d00df1eed50 100644 --- a/core/src/main/java/com/graphhopper/routing/ch/CHAlgoFactoryDecorator.java +++ b/core/src/main/java/com/graphhopper/routing/ch/CHAlgoFactoryDecorator.java @@ -19,15 +19,16 @@ import com.graphhopper.routing.RoutingAlgorithmFactory; import com.graphhopper.routing.RoutingAlgorithmFactoryDecorator; -import com.graphhopper.routing.weighting.AbstractWeighting; import com.graphhopper.routing.util.HintsMap; import com.graphhopper.routing.util.TraversalMode; +import com.graphhopper.routing.weighting.AbstractWeighting; import com.graphhopper.routing.weighting.Weighting; import com.graphhopper.storage.*; import com.graphhopper.util.CmdArgs; import com.graphhopper.util.Helper; import com.graphhopper.util.Parameters.CH; -import static com.graphhopper.util.Parameters.CH.DISABLE; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.util.ArrayList; import java.util.Arrays; @@ -35,8 +36,8 @@ import java.util.List; import java.util.concurrent.ExecutorService; import java.util.concurrent.TimeUnit; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; + +import static com.graphhopper.util.Parameters.CH.DISABLE; /** * This class implements the CH decorator for the routing algorithm factory and provides several @@ -44,8 +45,7 @@ * * @author Peter Karich */ -public class CHAlgoFactoryDecorator implements RoutingAlgorithmFactoryDecorator -{ +public class CHAlgoFactoryDecorator implements RoutingAlgorithmFactoryDecorator { private final Logger logger = LoggerFactory.getLogger(getClass()); private final List preparations = new ArrayList<>(); // we need to decouple weighting objects from the weighting list of strings @@ -63,14 +63,12 @@ public class CHAlgoFactoryDecorator implements RoutingAlgorithmFactoryDecorator private int preparationContractedNodes = -1; private double preparationLogMessages = -1; - public CHAlgoFactoryDecorator() - { + public CHAlgoFactoryDecorator() { setPreparationThreads(1); setWeightingsAsStrings(Arrays.asList(getDefaultWeighting())); } - public void init( CmdArgs args ) - { + public void init(CmdArgs args) { setPreparationThreads(args.getInt("prepare.threads", getPreparationThreads())); String deprecatedWeightingConfig = args.get("prepare.chWeighting", ""); @@ -84,12 +82,10 @@ public void init( CmdArgs args ) if (chWeightingsStr.isEmpty()) chWeightingsStr = args.get("prepare.chWeightings", ""); - if ("no".equals(chWeightingsStr)) - { + if ("no".equals(chWeightingsStr)) { // default is fastest and we need to clear this explicitely weightingsAsStrings.clear(); - } else if (!chWeightingsStr.isEmpty()) - { + } else if (!chWeightingsStr.isEmpty()) { List tmpCHWeightingList = Arrays.asList(chWeightingsStr.split(",")); setWeightingsAsStrings(tmpCHWeightingList); } @@ -106,107 +102,90 @@ public void init( CmdArgs args ) setPreparationLogMessages(args.getDouble("prepare.log_messages", getPreparationLogMessages())); } - public CHAlgoFactoryDecorator setPreparationPeriodicUpdates( int preparePeriodicUpdates ) - { + public int getPreparationPeriodicUpdates() { + return preparationPeriodicUpdates; + } + + public CHAlgoFactoryDecorator setPreparationPeriodicUpdates(int preparePeriodicUpdates) { this.preparationPeriodicUpdates = preparePeriodicUpdates; return this; } - public int getPreparationPeriodicUpdates() - { - return preparationPeriodicUpdates; + public int getPreparationContractedNodes() { + return preparationContractedNodes; } - public CHAlgoFactoryDecorator setPreparationContractedNodes( int prepareContractedNodes ) - { + public CHAlgoFactoryDecorator setPreparationContractedNodes(int prepareContractedNodes) { this.preparationContractedNodes = prepareContractedNodes; return this; } - public int getPreparationContractedNodes() - { - return preparationContractedNodes; + public int getPreparationLazyUpdates() { + return preparationLazyUpdates; } - public CHAlgoFactoryDecorator setPreparationLazyUpdates( int prepareLazyUpdates ) - { + public CHAlgoFactoryDecorator setPreparationLazyUpdates(int prepareLazyUpdates) { this.preparationLazyUpdates = prepareLazyUpdates; return this; } - public int getPreparationLazyUpdates() - { - return preparationLazyUpdates; + public double getPreparationLogMessages() { + return preparationLogMessages; } - public CHAlgoFactoryDecorator setPreparationLogMessages( double prepareLogMessages ) - { + public CHAlgoFactoryDecorator setPreparationLogMessages(double prepareLogMessages) { this.preparationLogMessages = prepareLogMessages; return this; } - public double getPreparationLogMessages() - { - return preparationLogMessages; + public int getPreparationNeighborUpdates() { + return preparationNeighborUpdates; } - public CHAlgoFactoryDecorator setPreparationNeighborUpdates( int prepareNeighborUpdates ) - { + public CHAlgoFactoryDecorator setPreparationNeighborUpdates(int prepareNeighborUpdates) { this.preparationNeighborUpdates = prepareNeighborUpdates; return this; } - public int getPreparationNeighborUpdates() - { - return preparationNeighborUpdates; + @Override + public final boolean isEnabled() { + return enabled; } /** * Enables or disables contraction hierarchies (CH). This speed-up mode is enabled by default. */ - public final void setEnabled( boolean enabled ) - { + public final void setEnabled(boolean enabled) { this.enabled = enabled; } - @Override - public final boolean isEnabled() - { - return enabled; + public final boolean isDisablingAllowed() { + return disablingAllowed || !isEnabled(); } /** * This method specifies if it is allowed to disable CH routing at runtime via routing hints. */ - public final CHAlgoFactoryDecorator setDisablingAllowed( boolean disablingAllowed ) - { + public final CHAlgoFactoryDecorator setDisablingAllowed(boolean disablingAllowed) { this.disablingAllowed = disablingAllowed; return this; } - public final boolean isDisablingAllowed() - { - return disablingAllowed || !isEnabled(); - } - /** * Decouple weightings from PrepareContractionHierarchies as we need weightings for the * graphstorage and the graphstorage for the preparation. */ - public CHAlgoFactoryDecorator addWeighting( Weighting weighting ) - { + public CHAlgoFactoryDecorator addWeighting(Weighting weighting) { weightings.add(weighting); return this; } - public CHAlgoFactoryDecorator addWeighting( String weighting ) - { + public CHAlgoFactoryDecorator addWeighting(String weighting) { weightingsAsStrings.add(weighting); return this; } - public CHAlgoFactoryDecorator addPreparation( PrepareContractionHierarchies pch ) - { + public CHAlgoFactoryDecorator addPreparation(PrepareContractionHierarchies pch) { preparations.add(pch); int lastIndex = preparations.size() - 1; if (lastIndex >= weightings.size()) @@ -219,35 +198,37 @@ public CHAlgoFactoryDecorator addPreparation( PrepareContractionHierarchies pch return this; } - public final boolean hasWeightings() - { + public final boolean hasWeightings() { return !weightings.isEmpty(); } - public final List getWeightings() - { + public final List getWeightings() { return weightings; } - public CHAlgoFactoryDecorator setWeightingsAsStrings( String... weightingNames ) - { + public CHAlgoFactoryDecorator setWeightingsAsStrings(String... weightingNames) { return setWeightingsAsStrings(Arrays.asList(weightingNames)); } + public List getWeightingsAsStrings() { + if (this.weightingsAsStrings.isEmpty()) + throw new IllegalStateException("Potential bug: chWeightingList is empty"); + + return this.weightingsAsStrings; + } + /** * Enables the use of contraction hierarchies to reduce query times. Enabled by default. * * @param weightingList A list containing multiple weightings like: "fastest", "shortest" or - * your own weight-calculation type. + * your own weight-calculation type. */ - public CHAlgoFactoryDecorator setWeightingsAsStrings( List weightingList ) - { + public CHAlgoFactoryDecorator setWeightingsAsStrings(List weightingList) { if (weightingList.isEmpty()) throw new IllegalArgumentException("It is not allowed to pass an emtpy weightingList"); weightingsAsStrings.clear(); - for (String strWeighting : weightingList) - { + for (String strWeighting : weightingList) { strWeighting = strWeighting.toLowerCase(); strWeighting = strWeighting.trim(); addWeighting(strWeighting); @@ -255,27 +236,16 @@ public CHAlgoFactoryDecorator setWeightingsAsStrings( List weightingList return this; } - public List getWeightingsAsStrings() - { - if (this.weightingsAsStrings.isEmpty()) - throw new IllegalStateException("Potential bug: chWeightingList is empty"); - - return this.weightingsAsStrings; - } - - private String getDefaultWeighting() - { + private String getDefaultWeighting() { return weightingsAsStrings.isEmpty() ? "fastest" : weightingsAsStrings.get(0); } - public List getPreparations() - { + public List getPreparations() { return preparations; } @Override - public RoutingAlgorithmFactory getDecoratedAlgorithmFactory( RoutingAlgorithmFactory defaultAlgoFactory, HintsMap map ) - { + public RoutingAlgorithmFactory getDecoratedAlgorithmFactory(RoutingAlgorithmFactory defaultAlgoFactory, HintsMap map) { boolean forceFlexMode = map.getBool(DISABLE, false); if (!isEnabled() || forceFlexMode) return defaultAlgoFactory; @@ -286,8 +256,7 @@ public RoutingAlgorithmFactory getDecoratedAlgorithmFactory( RoutingAlgorithmFac if (map.getWeighting().isEmpty()) map.setWeighting(getDefaultWeighting()); - for (PrepareContractionHierarchies p : preparations) - { + for (PrepareContractionHierarchies p : preparations) { if (p.getWeighting().matches(map)) return p; } @@ -295,36 +264,29 @@ public RoutingAlgorithmFactory getDecoratedAlgorithmFactory( RoutingAlgorithmFac throw new IllegalArgumentException("Cannot find RoutingAlgorithmFactory for weighting map " + map); } + public int getPreparationThreads() { + return preparationThreads; + } + /** * This method changes the number of threads used for preparation on import. Default is 1. Make * sure that you have enough memory to increase this number! */ - public void setPreparationThreads( int preparationThreads ) - { + public void setPreparationThreads(int preparationThreads) { this.preparationThreads = preparationThreads; this.chPreparePool = java.util.concurrent.Executors.newFixedThreadPool(preparationThreads); } - public int getPreparationThreads() - { - return preparationThreads; - } - - public void prepare( final StorableProperties properties ) - { + public void prepare(final StorableProperties properties) { int counter = 0; - for (final PrepareContractionHierarchies prepare : getPreparations()) - { + for (final PrepareContractionHierarchies prepare : getPreparations()) { logger.info((++counter) + "/" + getPreparations().size() + " calling prepare.doWork for " + prepare.getWeighting() + " ... (" + Helper.getMemInfo() + ")"); final String name = AbstractWeighting.weightingToFileName(prepare.getWeighting()); - chPreparePool.execute(new Runnable() - { + chPreparePool.execute(new Runnable() { @Override - public void run() - { + public void run() { String errorKey = "prepare.error." + name; - try - { + try { properties.put(errorKey, "CH preparation incomplete"); // toString is not taken into account so we need to cheat, see http://stackoverflow.com/q/6113746/194609 for other options @@ -332,8 +294,7 @@ public void run() prepare.doWork(); properties.remove(errorKey); properties.put("prepare.date." + name, Helper.createFormatter().format(new Date())); - } catch (Exception ex) - { + } catch (Exception ex) { logger.error("Problem while CH preparation " + name, ex); properties.put(errorKey, ex.getMessage()); } @@ -342,20 +303,17 @@ public void run() } chPreparePool.shutdown(); - try - { + try { if (!chPreparePool.awaitTermination(Integer.MAX_VALUE, TimeUnit.DAYS)) chPreparePool.shutdownNow(); - } catch (InterruptedException ie) - { + } catch (InterruptedException ie) { chPreparePool.shutdownNow(); throw new RuntimeException(ie); } } - public void createPreparations( GraphHopperStorage ghStorage, TraversalMode traversalMode ) - { + public void createPreparations(GraphHopperStorage ghStorage, TraversalMode traversalMode) { if (!isEnabled() || !preparations.isEmpty()) return; if (weightings.isEmpty()) @@ -363,8 +321,7 @@ public void createPreparations( GraphHopperStorage ghStorage, TraversalMode trav traversalMode = getNodeBase(); - for (Weighting weighting : getWeightings()) - { + for (Weighting weighting : getWeightings()) { PrepareContractionHierarchies tmpPrepareCH = new PrepareContractionHierarchies( new GHDirectory("", DAType.RAM_INT), ghStorage, ghStorage.getGraph(CHGraph.class, weighting), weighting.getFlagEncoder(), weighting, traversalMode); @@ -381,8 +338,7 @@ public void createPreparations( GraphHopperStorage ghStorage, TraversalMode trav * For now only node based will work, later on we can easily find usage of this method to remove * it. */ - public TraversalMode getNodeBase() - { + public TraversalMode getNodeBase() { return TraversalMode.NODE_BASED; } } diff --git a/core/src/main/java/com/graphhopper/routing/ch/Path4CH.java b/core/src/main/java/com/graphhopper/routing/ch/Path4CH.java index 3033596c6e9..112bbbe2c6a 100644 --- a/core/src/main/java/com/graphhopper/routing/ch/Path4CH.java +++ b/core/src/main/java/com/graphhopper/routing/ch/Path4CH.java @@ -25,31 +25,27 @@ /** * Recursivly unpack shortcuts. *

+ * * @author Peter Karich * @see PrepareContractionHierarchies */ -public class Path4CH extends PathBidirRef -{ +public class Path4CH extends PathBidirRef { private final Graph routingGraph; - public Path4CH( Graph routingGraph, Graph baseGraph, FlagEncoder encoder ) - { + public Path4CH(Graph routingGraph, Graph baseGraph, FlagEncoder encoder) { super(baseGraph, encoder); this.routingGraph = routingGraph; } @Override - protected final void processEdge( int tmpEdge, int endNode ) - { + protected final void processEdge(int tmpEdge, int endNode) { // Shortcuts do only contain valid weight so first expand before adding // to distance and time expandEdge((CHEdgeIteratorState) routingGraph.getEdgeIteratorState(tmpEdge, endNode), false); } - private void expandEdge( CHEdgeIteratorState mainEdgeState, boolean reverse ) - { - if (!mainEdgeState.isShortcut()) - { + private void expandEdge(CHEdgeIteratorState mainEdgeState, boolean reverse) { + if (!mainEdgeState.isShortcut()) { double dist = mainEdgeState.getDistance(); distance += dist; long flags = mainEdgeState.getFlags(); @@ -63,16 +59,14 @@ private void expandEdge( CHEdgeIteratorState mainEdgeState, boolean reverse ) int from = mainEdgeState.getBaseNode(), to = mainEdgeState.getAdjNode(); // get properties like speed of the edge in the correct direction - if (reverse) - { + if (reverse) { int tmp = from; from = to; to = tmp; } // getEdgeProps could possibly return an empty edge if the shortcut is available for both directions - if (reverseOrder) - { + if (reverseOrder) { CHEdgeIteratorState edgeState = (CHEdgeIteratorState) routingGraph.getEdgeIteratorState(skippedEdge1, to); boolean empty = edgeState == null; if (empty) @@ -86,8 +80,7 @@ private void expandEdge( CHEdgeIteratorState mainEdgeState, boolean reverse ) edgeState = (CHEdgeIteratorState) routingGraph.getEdgeIteratorState(skippedEdge2, from); expandEdge(edgeState, true); - } else - { + } else { CHEdgeIteratorState iter = (CHEdgeIteratorState) routingGraph.getEdgeIteratorState(skippedEdge1, from); boolean empty = iter == null; if (empty) diff --git a/core/src/main/java/com/graphhopper/routing/ch/PreparationWeighting.java b/core/src/main/java/com/graphhopper/routing/ch/PreparationWeighting.java index 723425060a4..25e97e21324 100644 --- a/core/src/main/java/com/graphhopper/routing/ch/PreparationWeighting.java +++ b/core/src/main/java/com/graphhopper/routing/ch/PreparationWeighting.java @@ -18,35 +18,32 @@ package com.graphhopper.routing.ch; import com.graphhopper.routing.util.FlagEncoder; -import com.graphhopper.routing.weighting.Weighting; import com.graphhopper.routing.util.HintsMap; -import com.graphhopper.util.EdgeIteratorState; +import com.graphhopper.routing.weighting.Weighting; import com.graphhopper.util.CHEdgeIteratorState; +import com.graphhopper.util.EdgeIteratorState; /** * Used in CH preparation and therefor assumed that all edges are of type CHEdgeIteratorState *

+ * * @author Peter Karich * @see PrepareContractionHierarchies */ -public class PreparationWeighting implements Weighting -{ +public class PreparationWeighting implements Weighting { private final Weighting userWeighting; - public PreparationWeighting( Weighting userWeighting ) - { + public PreparationWeighting(Weighting userWeighting) { this.userWeighting = userWeighting; } @Override - public final double getMinWeight( double distance ) - { + public final double getMinWeight(double distance) { return userWeighting.getMinWeight(distance); } @Override - public double calcWeight( EdgeIteratorState edgeState, boolean reverse, int prevOrNextEdgeId ) - { + public double calcWeight(EdgeIteratorState edgeState, boolean reverse, int prevOrNextEdgeId) { CHEdgeIteratorState tmp = (CHEdgeIteratorState) edgeState; if (tmp.isShortcut()) // if a shortcut is in both directions the weight is identical => no need for 'reverse' @@ -56,26 +53,22 @@ public double calcWeight( EdgeIteratorState edgeState, boolean reverse, int prev } @Override - public FlagEncoder getFlagEncoder() - { + public FlagEncoder getFlagEncoder() { return userWeighting.getFlagEncoder(); } @Override - public boolean matches( HintsMap map ) - { + public boolean matches(HintsMap map) { return getName().equals(map.getWeighting()) && userWeighting.getFlagEncoder().toString().equals(map.getVehicle()); } @Override - public String getName() - { + public String getName() { return "prepare|" + userWeighting.getName(); } @Override - public String toString() - { + public String toString() { return getName(); } } diff --git a/core/src/main/java/com/graphhopper/routing/ch/PrepareContractionHierarchies.java b/core/src/main/java/com/graphhopper/routing/ch/PrepareContractionHierarchies.java index 90ba8be0f9d..ea91eb86fa3 100644 --- a/core/src/main/java/com/graphhopper/routing/ch/PrepareContractionHierarchies.java +++ b/core/src/main/java/com/graphhopper/routing/ch/PrepareContractionHierarchies.java @@ -17,20 +17,24 @@ */ package com.graphhopper.routing.ch; -import com.graphhopper.routing.weighting.Weighting; -import com.graphhopper.routing.weighting.AbstractWeighting; import com.graphhopper.coll.GHTreeMapComposed; import com.graphhopper.routing.*; import com.graphhopper.routing.util.*; +import com.graphhopper.routing.weighting.AbstractWeighting; +import com.graphhopper.routing.weighting.Weighting; import com.graphhopper.storage.*; import com.graphhopper.util.*; -import static com.graphhopper.util.Parameters.Algorithms.*; - -import java.util.*; - import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.HashMap; +import java.util.Map; +import java.util.Random; +import java.util.Set; + +import static com.graphhopper.util.Parameters.Algorithms.ASTAR_BI; +import static com.graphhopper.util.Parameters.Algorithms.DIJKSTRA_BI; + /** * This class prepares the graph for a bidirectional algorithm supporting contraction hierarchies * ie. an algorithm returned by createAlgo. @@ -41,38 +45,39 @@ * The only difference is that we use two skipped edges instead of one skipped node for faster * unpacking. *

+ * * @author Peter Karich */ -public class PrepareContractionHierarchies extends AbstractAlgoPreparation implements RoutingAlgorithmFactory -{ +public class PrepareContractionHierarchies extends AbstractAlgoPreparation implements RoutingAlgorithmFactory { private final Logger logger = LoggerFactory.getLogger(getClass()); private final PreparationWeighting prepareWeighting; private final FlagEncoder prepareFlagEncoder; private final TraversalMode traversalMode; + private final LevelEdgeFilter levelFilter; + private final GraphHopperStorage ghStorage; + private final CHGraphImpl prepareGraph; + private final DataAccess originalEdges; + private final Map shortcuts = new HashMap(); + private final Random rand = new Random(123); + private final StopWatch allSW = new StopWatch(); + AddShortcutHandler addScHandler = new AddShortcutHandler(); + CalcShortcutHandler calcScHandler = new CalcShortcutHandler(); private CHEdgeExplorer vehicleInExplorer; private CHEdgeExplorer vehicleOutExplorer; private CHEdgeExplorer vehicleAllExplorer; private CHEdgeExplorer vehicleAllTmpExplorer; private CHEdgeExplorer calcPrioAllExplorer; - private final LevelEdgeFilter levelFilter; private int maxLevel; - private final GraphHopperStorage ghStorage; - private final CHGraphImpl prepareGraph; - // the most important nodes comes last private GHTreeMapComposed sortedNodes; private int oldPriorities[]; - private final DataAccess originalEdges; - private final Map shortcuts = new HashMap(); private IgnoreNodeFilter ignoreNodeFilter; private DijkstraOneToMany prepareAlgo; private long counter; private int newShortcuts; private long dijkstraCount; private double meanDegree; - private final Random rand = new Random(123); private StopWatch dijkstraSW = new StopWatch(); - private final StopWatch allSW = new StopWatch(); private int periodicUpdatesPercentage = 20; private int lastNodesLazyUpdatePercentage = 10; private int neighborUpdatePercentage = 20; @@ -85,9 +90,8 @@ public class PrepareContractionHierarchies extends AbstractAlgoPreparation imple private double neighborTime; private int maxEdgesCount; - public PrepareContractionHierarchies( Directory dir, GraphHopperStorage ghStorage, CHGraph chGraph, - FlagEncoder encoder, Weighting weighting, TraversalMode traversalMode ) - { + public PrepareContractionHierarchies(Directory dir, GraphHopperStorage ghStorage, CHGraph chGraph, + FlagEncoder encoder, Weighting weighting, TraversalMode traversalMode) { this.ghStorage = ghStorage; this.prepareGraph = (CHGraphImpl) chGraph; this.traversalMode = traversalMode; @@ -103,11 +107,11 @@ public PrepareContractionHierarchies( Directory dir, GraphHopperStorage ghStorag * The higher the values are the longer the preparation takes but the less shortcuts are * produced. *

+ * * @param periodicUpdates specifies how often periodic updates will happen. Use something less - * than 10. + * than 10. */ - public PrepareContractionHierarchies setPeriodicUpdates( int periodicUpdates ) - { + public PrepareContractionHierarchies setPeriodicUpdates(int periodicUpdates) { if (periodicUpdates < 0) return this; if (periodicUpdates > 100) @@ -119,10 +123,9 @@ public PrepareContractionHierarchies setPeriodicUpdates( int periodicUpdates ) /** * @param lazyUpdates specifies when lazy updates will happen, measured relative to all existing - * nodes. 100 means always. + * nodes. 100 means always. */ - public PrepareContractionHierarchies setLazyUpdates( int lazyUpdates ) - { + public PrepareContractionHierarchies setLazyUpdates(int lazyUpdates) { if (lazyUpdates < 0) return this; @@ -136,8 +139,7 @@ public PrepareContractionHierarchies setLazyUpdates( int lazyUpdates ) /** * @param neighborUpdates specifies how often neighbor updates will happen. 100 means always. */ - public PrepareContractionHierarchies setNeighborUpdates( int neighborUpdates ) - { + public PrepareContractionHierarchies setNeighborUpdates(int neighborUpdates) { if (neighborUpdates < 0) return this; @@ -152,8 +154,7 @@ public PrepareContractionHierarchies setNeighborUpdates( int neighborUpdates ) * Specifies how often a log message should be printed. Specify something around 20 (20% of the * start nodes). */ - public PrepareContractionHierarchies setLogMessages( double logMessages ) - { + public PrepareContractionHierarchies setLogMessages(double logMessages) { if (logMessages >= 0) this.logMessagesPercentage = logMessages; return this; @@ -163,8 +164,7 @@ public PrepareContractionHierarchies setLogMessages( double logMessages ) * Define how many nodes (percentage) should be contracted. Less nodes means slower query but * faster contraction duration. */ - public PrepareContractionHierarchies setContractedNodes( double nodesContracted ) - { + public PrepareContractionHierarchies setContractedNodes(double nodesContracted) { if (nodesContracted < 0) return this; @@ -180,14 +180,12 @@ public PrepareContractionHierarchies setContractedNodes( double nodesContracted * be too high for your mobile application. E.g. A 500km query only traverses roughly 2000 * nodes. */ - public void setInitialCollectionSize( int initialCollectionSize ) - { + public void setInitialCollectionSize(int initialCollectionSize) { this.initialCollectionSize = initialCollectionSize; } @Override - public void doWork() - { + public void doWork() { if (prepareFlagEncoder == null) throw new IllegalStateException("No vehicle encoder set."); @@ -204,16 +202,13 @@ public void doWork() contractNodes(); } - boolean prepareNodes() - { + boolean prepareNodes() { int nodes = prepareGraph.getNodes(); - for (int node = 0; node < nodes; node++) - { + for (int node = 0; node < nodes; node++) { prepareGraph.setLevel(node, maxLevel); } - for (int node = 0; node < nodes; node++) - { + for (int node = 0; node < nodes; node++) { int priority = oldPriorities[node] = calculatePriority(node); sortedNodes.insert(node, priority); } @@ -224,8 +219,7 @@ boolean prepareNodes() return true; } - void contractNodes() - { + void contractNodes() { meanDegree = prepareGraph.getAllEdges().getMaxId() / prepareGraph.getNodes(); int level = 1; counter = 0; @@ -260,16 +254,13 @@ void contractNodes() neighborUpdate = false; StopWatch neighborSW = new StopWatch(); - while (!sortedNodes.isEmpty()) - { - // periodically update priorities of ALL nodes - if (periodicUpdate && counter > 0 && counter % periodicUpdatesCount == 0) - { + while (!sortedNodes.isEmpty()) { + // periodically update priorities of ALL nodes + if (periodicUpdate && counter > 0 && counter % periodicUpdatesCount == 0) { periodSW.start(); sortedNodes.clear(); int len = prepareGraph.getNodes(); - for (int node = 0; node < len; node++) - { + for (int node = 0; node < len; node++) { if (prepareGraph.getLevel(node) != maxLevel) continue; @@ -282,8 +273,7 @@ void contractNodes() throw new IllegalStateException("Cannot prepare as no unprepared nodes where found. Called preparation twice?"); } - if (counter % logSize == 0) - { + if (counter % logSize == 0) { dijkstraTime += dijkstraSW.getSeconds(); periodTime += periodSW.getSeconds(); lazyTime += lazySW.getSeconds(); @@ -306,12 +296,10 @@ void contractNodes() counter++; int polledNode = sortedNodes.pollKey(); - if (!sortedNodes.isEmpty() && sortedNodes.getSize() < lastNodesLazyUpdates) - { + if (!sortedNodes.isEmpty() && sortedNodes.getSize() < lastNodesLazyUpdates) { lazySW.start(); int priority = oldPriorities[polledNode] = calculatePriority(polledNode); - if (priority > sortedNodes.peekValue()) - { + if (priority > sortedNodes.peekValue()) { // current node got more important => insert as new value and contract it later sortedNodes.insert(polledNode, priority); lazySW.stop(); @@ -320,7 +308,7 @@ void contractNodes() lazySW.stop(); } - // contract! + // contract! newShortcuts += addShortcuts(polledNode); prepareGraph.setLevel(polledNode, level); level++; @@ -330,14 +318,12 @@ void contractNodes() break; CHEdgeIterator iter = vehicleAllExplorer.setBaseNode(polledNode); - while (iter.next()) - { + while (iter.next()) { int nn = iter.getAdjNode(); if (prepareGraph.getLevel(nn) != maxLevel) continue; - if (neighborUpdate && rand.nextInt(100) < neighborUpdatePercentage) - { + if (neighborUpdate && rand.nextInt(100) < neighborUpdatePercentage) { neighborSW.start(); int oldPrio = oldPriorities[nn]; int priority = oldPriorities[nn] = calculatePriority(nn); @@ -373,153 +359,45 @@ void contractNodes() + ", " + Helper.getMemInfo()); } - public long getDijkstraCount() - { + public long getDijkstraCount() { return dijkstraCount; } - public double getLazyTime() - { + public double getLazyTime() { return lazyTime; } - public double getPeriodTime() - { + public double getPeriodTime() { return periodTime; } - public double getDijkstraTime() - { + public double getDijkstraTime() { return dijkstraTime; } - public double getNeighborTime() - { + public double getNeighborTime() { return neighborTime; } - public Weighting getWeighting() - { + public Weighting getWeighting() { return prepareGraph.getWeighting(); } - public void close() - { + public void close() { prepareAlgo.close(); originalEdges.close(); sortedNodes = null; oldPriorities = null; } - AddShortcutHandler addScHandler = new AddShortcutHandler(); - CalcShortcutHandler calcScHandler = new CalcShortcutHandler(); - - private String getTimesAsString() - { + private String getTimesAsString() { return "t(dijk):" + Helper.round2(dijkstraTime) + ", t(period):" + Helper.round2(periodTime) + ", t(lazy):" + Helper.round2(lazyTime) + ", t(neighbor):" + Helper.round2(neighborTime); } - interface ShortcutHandler - { - void foundShortcut( int u_fromNode, int w_toNode, - double existingDirectWeight, double distance, - EdgeIterator outgoingEdges, - int skippedEdge1, int incomingEdgeOrigCount ); - - int getNode(); - } - - class CalcShortcutHandler implements ShortcutHandler - { - int node; - int originalEdgesCount; - int shortcuts; - - public CalcShortcutHandler setNode( int n ) - { - node = n; - originalEdgesCount = 0; - shortcuts = 0; - return this; - } - - @Override - public int getNode() - { - return node; - } - - @Override - public void foundShortcut( int u_fromNode, int w_toNode, - double existingDirectWeight, double distance, - EdgeIterator outgoingEdges, - int skippedEdge1, int incomingEdgeOrigCount ) - { - shortcuts++; - originalEdgesCount += incomingEdgeOrigCount + getOrigEdgeCount(outgoingEdges.getEdge()); - } - } - - class AddShortcutHandler implements ShortcutHandler - { - int node; - - public AddShortcutHandler() - { - } - - @Override - public int getNode() - { - return node; - } - - public AddShortcutHandler setNode( int n ) - { - shortcuts.clear(); - node = n; - return this; - } - - @Override - public void foundShortcut( int u_fromNode, int w_toNode, - double existingDirectWeight, double existingDistSum, - EdgeIterator outgoingEdges, - int skippedEdge1, int incomingEdgeOrigCount ) - { - // FOUND shortcut - // but be sure that it is the only shortcut in the collection - // and also in the graph for u->w. If existing AND identical weight => update setProperties. - // Hint: shortcuts are always one-way due to distinct level of every node but we don't - // know yet the levels so we need to determine the correct direction or if both directions - Shortcut sc = new Shortcut(u_fromNode, w_toNode, existingDirectWeight, existingDistSum); - if (shortcuts.containsKey(sc)) - return; - - Shortcut tmpSc = new Shortcut(w_toNode, u_fromNode, existingDirectWeight, existingDistSum); - Shortcut tmpRetSc = shortcuts.get(tmpSc); - if (tmpRetSc != null) - { - // overwrite flags only if skipped edges are identical - if (tmpRetSc.skippedEdge2 == skippedEdge1 && tmpRetSc.skippedEdge1 == outgoingEdges.getEdge()) - { - tmpRetSc.flags = PrepareEncoder.getScDirMask(); - return; - } - } - - shortcuts.put(sc, sc); - sc.skippedEdge1 = skippedEdge1; - sc.skippedEdge2 = outgoingEdges.getEdge(); - sc.originalEdges = incomingEdgeOrigCount + getOrigEdgeCount(outgoingEdges.getEdge()); - } - } - - Set testFindShortcuts( int node ) - { + Set testFindShortcuts(int node) { findShortcuts(addScHandler.setNode(node)); return shortcuts.keySet(); } @@ -530,8 +408,7 @@ Set testFindShortcuts( int node ) * the priority(v). Otherwise updating the priority before contracting in contractNodes() could * lead to a slowish or even endless loop. */ - int calculatePriority( int v ) - { + int calculatePriority(int v) { // set of shortcuts that would be added if adjNode v would be contracted next. findShortcuts(calcScHandler.setNode(v)); @@ -547,15 +424,14 @@ int calculatePriority( int v ) // originalEdgesCount += sc.originalEdges; // } - // # lowest influence on preparation speed or shortcut creation count + // # lowest influence on preparation speed or shortcut creation count // (but according to paper should speed up queries) // // number of already contracted neighbors of v int contractedNeighbors = 0; int degree = 0; CHEdgeIterator iter = calcPrioAllExplorer.setBaseNode(v); - while (iter.next()) - { + while (iter.next()) { degree++; if (iter.isShortcut()) contractedNeighbors++; @@ -564,7 +440,7 @@ int calculatePriority( int v ) // from shortcuts we can compute the edgeDifference // # low influence: with it the shortcut creation is slightly faster // - // |shortcuts(v)| − |{(u, v) | v uncontracted}| − |{(v, w) | v uncontracted}| + // |shortcuts(v)| − |{(u, v) | v uncontracted}| − |{(v, w) | v uncontracted}| // meanDegree is used instead of outDegree+inDegree as if one adjNode is in both directions // only one bucket memory is used. Additionally one shortcut could also stand for two directions. int edgeDifference = calcScHandler.shortcuts - degree; @@ -577,13 +453,11 @@ int calculatePriority( int v ) /** * Finds shortcuts, does not change the underlying graph. */ - void findShortcuts( ShortcutHandler sch ) - { + void findShortcuts(ShortcutHandler sch) { long tmpDegreeCounter = 0; EdgeIterator incomingEdges = vehicleInExplorer.setBaseNode(sch.getNode()); // collect outgoing nodes (goal-nodes) only once - while (incomingEdges.next()) - { + while (incomingEdges.next()) { int u_fromNode = incomingEdges.getAdjNode(); // accept only uncontracted nodes if (prepareGraph.getLevel(u_fromNode) != maxLevel) @@ -598,8 +472,7 @@ void findShortcuts( ShortcutHandler sch ) // force fresh maps etc as this cannot be determined by from node alone (e.g. same from node but different avoidNode) prepareAlgo.clear(); tmpDegreeCounter++; - while (outgoingEdges.next()) - { + while (outgoingEdges.next()) { int w_toNode = outgoingEdges.getAdjNode(); // add only uncontracted nodes if (prepareGraph.getLevel(w_toNode) != maxLevel || u_fromNode == w_toNode) @@ -629,7 +502,7 @@ void findShortcuts( ShortcutHandler sch ) // compare end node as the limit could force dijkstra to finish earlier if (endNode == w_toNode && prepareAlgo.getWeight(endNode) <= existingDirectWeight) - // FOUND witness path, so do not add shortcut + // FOUND witness path, so do not add shortcut continue; sch.foundShortcut(u_fromNode, w_toNode, @@ -638,8 +511,7 @@ void findShortcuts( ShortcutHandler sch ) skippedEdge1, incomingEdgeOrigCount); } } - if (sch instanceof AddShortcutHandler) - { + if (sch instanceof AddShortcutHandler) { // sliding mean value when using "*2" => slower changes meanDegree = (meanDegree * 2 + tmpDegreeCounter) / 3; // meanDegree = (meanDegree + tmpDegreeCounter) / 2; @@ -649,26 +521,21 @@ void findShortcuts( ShortcutHandler sch ) /** * Introduces the necessary shortcuts for adjNode v in the graph. */ - int addShortcuts( int v ) - { + int addShortcuts(int v) { shortcuts.clear(); findShortcuts(addScHandler.setNode(v)); int tmpNewShortcuts = 0; NEXT_SC: - for (Shortcut sc : shortcuts.keySet()) - { + for (Shortcut sc : shortcuts.keySet()) { boolean updatedInGraph = false; // check if we need to update some existing shortcut in the graph CHEdgeIterator iter = vehicleOutExplorer.setBaseNode(sc.from); - while (iter.next()) - { - if (iter.isShortcut() && iter.getAdjNode() == sc.to && iter.canBeOverwritten(sc.flags)) - { + while (iter.next()) { + if (iter.isShortcut() && iter.getAdjNode() == sc.to && iter.canBeOverwritten(sc.flags)) { if (sc.weight >= prepareWeighting.calcWeight(iter, false, EdgeIterator.NO_EDGE)) continue NEXT_SC; - if (iter.getEdge() == sc.skippedEdge1 || iter.getEdge() == sc.skippedEdge2) - { + if (iter.getEdge() == sc.skippedEdge1 || iter.getEdge() == sc.skippedEdge2) { throw new IllegalStateException("Shortcut cannot update itself! " + iter.getEdge() + ", skipEdge1:" + sc.skippedEdge1 + ", skipEdge2:" + sc.skippedEdge2 + ", edge " + iter + ":" + getCoords(iter, prepareGraph) @@ -689,8 +556,7 @@ int addShortcuts( int v ) } } - if (!updatedInGraph) - { + if (!updatedInGraph) { CHEdgeIteratorState edgeState = prepareGraph.shortcut(sc.from, sc.to); // note: flags overwrite weight => call first edgeState.setFlags(sc.flags); @@ -704,8 +570,7 @@ int addShortcuts( int v ) return tmpNewShortcuts; } - String getCoords( EdgeIteratorState e, Graph g ) - { + String getCoords(EdgeIteratorState e, Graph g) { NodeAccess na = g.getNodeAccess(); int base = e.getBaseNode(); int adj = e.getAdjNode(); @@ -713,8 +578,7 @@ String getCoords( EdgeIteratorState e, Graph g ) + na.getLat(base) + "," + na.getLon(base) + " -> " + na.getLat(adj) + "," + na.getLon(adj); } - PrepareContractionHierarchies initFromGraph() - { + PrepareContractionHierarchies initFromGraph() { ghStorage.freeze(); maxEdgesCount = ghStorage.getAllEdges().getMaxId(); vehicleInExplorer = prepareGraph.createEdgeExplorer(new DefaultEdgeFilter(prepareFlagEncoder, true, false)); @@ -722,11 +586,9 @@ PrepareContractionHierarchies initFromGraph() final EdgeFilter allFilter = new DefaultEdgeFilter(prepareFlagEncoder, true, true); // filter by vehicle and level number - final EdgeFilter accessWithLevelFilter = new LevelEdgeFilter(prepareGraph) - { + final EdgeFilter accessWithLevelFilter = new LevelEdgeFilter(prepareGraph) { @Override - public final boolean accept( EdgeIteratorState edgeState ) - { + public final boolean accept(EdgeIteratorState edgeState) { if (!super.accept(edgeState)) return false; @@ -740,8 +602,8 @@ public final boolean accept( EdgeIteratorState edgeState ) vehicleAllTmpExplorer = prepareGraph.createEdgeExplorer(allFilter); calcPrioAllExplorer = prepareGraph.createEdgeExplorer(accessWithLevelFilter); - // Use an alternative to PriorityQueue as it has some advantages: - // 1. Gets automatically smaller if less entries are stored => less total RAM used. + // Use an alternative to PriorityQueue as it has some advantages: + // 1. Gets automatically smaller if less entries are stored => less total RAM used. // Important because Graph is increasing until the end. // 2. is slightly faster // but we need the additional oldPriorities array to keep the old value which is necessary for the update method @@ -751,44 +613,14 @@ public final boolean accept( EdgeIteratorState edgeState ) return this; } - public int getShortcuts() - { + public int getShortcuts() { return newShortcuts; } - static class IgnoreNodeFilter implements EdgeFilter - { - int avoidNode; - int maxLevel; - CHGraph graph; - - public IgnoreNodeFilter( CHGraph g, int maxLevel ) - { - this.graph = g; - this.maxLevel = maxLevel; - } - - public IgnoreNodeFilter setAvoidNode( int node ) - { - this.avoidNode = node; - return this; - } - - @Override - public final boolean accept( EdgeIteratorState iter ) - { - // ignore if it is skipNode or adjNode is already contracted - int node = iter.getAdjNode(); - return avoidNode != node && graph.getLevel(node) == maxLevel; - } - } - - private void setOrigEdgeCount( int edgeId, int value ) - { + private void setOrigEdgeCount(int edgeId, int value) { edgeId -= maxEdgesCount; - if (edgeId < 0) - { - // ignore setting as every normal edge has original edge count of 1 + if (edgeId < 0) { + // ignore setting as every normal edge has original edge count of 1 if (value != 1) throw new IllegalStateException("Trying to set original edge count for normal edge to a value = " + value + ", edge:" + (edgeId + maxEdgesCount) + ", max:" + maxEdgesCount + ", graph.max:" + ghStorage.getAllEdges().getMaxId()); @@ -800,8 +632,7 @@ private void setOrigEdgeCount( int edgeId, int value ) originalEdges.setInt(tmp, value); } - private int getOrigEdgeCount( int edgeId ) - { + private int getOrigEdgeCount(int edgeId) { edgeId -= maxEdgesCount; if (edgeId < 0) return 1; @@ -812,17 +643,13 @@ private int getOrigEdgeCount( int edgeId ) } @Override - public RoutingAlgorithm createAlgo( Graph graph, AlgorithmOptions opts ) - { + public RoutingAlgorithm createAlgo(Graph graph, AlgorithmOptions opts) { AbstractBidirAlgo algo; - if (ASTAR_BI.equals(opts.getAlgorithm())) - { + if (ASTAR_BI.equals(opts.getAlgorithm())) { algo = createAStarBidirection(graph); - } else if (DIJKSTRA_BI.equals(opts.getAlgorithm())) - { + } else if (DIJKSTRA_BI.equals(opts.getAlgorithm())) { algo = createDijkstraBidirection(graph); - } else - { + } else { throw new IllegalArgumentException("Algorithm " + opts.getAlgorithm() + " not supported for Contraction Hierarchies. Try with ch.disable=true"); } @@ -832,61 +659,52 @@ public RoutingAlgorithm createAlgo( Graph graph, AlgorithmOptions opts ) } private AStarBidirection createAStarBidirection(final Graph graph) { - return new AStarBidirection(graph, prepareFlagEncoder, prepareWeighting, traversalMode) - { - @Override - protected void initCollections( int nodes ) - { - // algorithm with CH does not need that much memory pre allocated - super.initCollections(Math.min(initialCollectionSize, nodes)); - } + return new AStarBidirection(graph, prepareFlagEncoder, prepareWeighting, traversalMode) { + @Override + protected void initCollections(int nodes) { + // algorithm with CH does not need that much memory pre allocated + super.initCollections(Math.min(initialCollectionSize, nodes)); + } - @Override - protected boolean finished() - { - // we need to finish BOTH searches for CH! - if (finishedFrom && finishedTo) - return true; + @Override + protected boolean finished() { + // we need to finish BOTH searches for CH! + if (finishedFrom && finishedTo) + return true; - // changed finish condition for CH - return currFrom.weight >= bestPath.getWeight() && currTo.weight >= bestPath.getWeight(); - } + // changed finish condition for CH + return currFrom.weight >= bestPath.getWeight() && currTo.weight >= bestPath.getWeight(); + } - @Override - protected Path createAndInitPath() - { - bestPath = new Path4CH(graph, graph.getBaseGraph(), flagEncoder); - return bestPath; - } + @Override + protected Path createAndInitPath() { + bestPath = new Path4CH(graph, graph.getBaseGraph(), flagEncoder); + return bestPath; + } - @Override - public String getName() - { - return "astarbiCH"; - } + @Override + public String getName() { + return "astarbiCH"; + } - @Override + @Override - public String toString() - { - return getName() + "|" + prepareWeighting; - } - }; + public String toString() { + return getName() + "|" + prepareWeighting; + } + }; } private AbstractBidirAlgo createDijkstraBidirection(final Graph graph) { - return new DijkstraBidirectionRef(graph, prepareFlagEncoder, prepareWeighting, traversalMode) - { + return new DijkstraBidirectionRef(graph, prepareFlagEncoder, prepareWeighting, traversalMode) { @Override - protected void initCollections( int nodes ) - { + protected void initCollections(int nodes) { // algorithm with CH does not need that much memory pre allocated super.initCollections(Math.min(initialCollectionSize, nodes)); } @Override - public boolean finished() - { + public boolean finished() { // we need to finish BOTH searches for CH! if (finishedFrom && finishedTo) return true; @@ -896,28 +714,61 @@ public boolean finished() } @Override - protected Path createAndInitPath() - { + protected Path createAndInitPath() { bestPath = new Path4CH(graph, graph.getBaseGraph(), flagEncoder); return bestPath; } @Override - public String getName() - { + public String getName() { return "dijkstrabiCH"; } @Override - public String toString() - { + public String toString() { return getName() + "|" + prepareWeighting; } }; } - static class Shortcut - { + @Override + public String toString() { + return "prepare|CH|dijkstrabi"; + } + + interface ShortcutHandler { + void foundShortcut(int u_fromNode, int w_toNode, + double existingDirectWeight, double distance, + EdgeIterator outgoingEdges, + int skippedEdge1, int incomingEdgeOrigCount); + + int getNode(); + } + + static class IgnoreNodeFilter implements EdgeFilter { + int avoidNode; + int maxLevel; + CHGraph graph; + + public IgnoreNodeFilter(CHGraph g, int maxLevel) { + this.graph = g; + this.maxLevel = maxLevel; + } + + public IgnoreNodeFilter setAvoidNode(int node) { + this.avoidNode = node; + return this; + } + + @Override + public final boolean accept(EdgeIteratorState iter) { + // ignore if it is skipNode or adjNode is already contracted + int node = iter.getAdjNode(); + return avoidNode != node && graph.getLevel(node) == maxLevel; + } + } + + static class Shortcut { int from; int to; int skippedEdge1; @@ -927,8 +778,7 @@ static class Shortcut int originalEdges; long flags = PrepareEncoder.getScFwdDir(); - public Shortcut( int from, int to, double weight, double dist ) - { + public Shortcut(int from, int to, double weight, double dist) { this.from = from; this.to = to; this.weight = weight; @@ -936,8 +786,7 @@ public Shortcut( int from, int to, double weight, double dist ) } @Override - public int hashCode() - { + public int hashCode() { int hash = 5; hash = 23 * hash + from; hash = 23 * hash + to; @@ -946,8 +795,7 @@ public int hashCode() } @Override - public boolean equals( Object obj ) - { + public boolean equals(Object obj) { if (obj == null || getClass() != obj.getClass()) return false; @@ -959,8 +807,7 @@ public boolean equals( Object obj ) } @Override - public String toString() - { + public String toString() { String str; if (flags == PrepareEncoder.getScDirMask()) str = from + "<->"; @@ -971,9 +818,78 @@ public String toString() } } - @Override - public String toString() - { - return "prepare|CH|dijkstrabi"; + class CalcShortcutHandler implements ShortcutHandler { + int node; + int originalEdgesCount; + int shortcuts; + + @Override + public int getNode() { + return node; + } + + public CalcShortcutHandler setNode(int n) { + node = n; + originalEdgesCount = 0; + shortcuts = 0; + return this; + } + + @Override + public void foundShortcut(int u_fromNode, int w_toNode, + double existingDirectWeight, double distance, + EdgeIterator outgoingEdges, + int skippedEdge1, int incomingEdgeOrigCount) { + shortcuts++; + originalEdgesCount += incomingEdgeOrigCount + getOrigEdgeCount(outgoingEdges.getEdge()); + } + } + + class AddShortcutHandler implements ShortcutHandler { + int node; + + public AddShortcutHandler() { + } + + @Override + public int getNode() { + return node; + } + + public AddShortcutHandler setNode(int n) { + shortcuts.clear(); + node = n; + return this; + } + + @Override + public void foundShortcut(int u_fromNode, int w_toNode, + double existingDirectWeight, double existingDistSum, + EdgeIterator outgoingEdges, + int skippedEdge1, int incomingEdgeOrigCount) { + // FOUND shortcut + // but be sure that it is the only shortcut in the collection + // and also in the graph for u->w. If existing AND identical weight => update setProperties. + // Hint: shortcuts are always one-way due to distinct level of every node but we don't + // know yet the levels so we need to determine the correct direction or if both directions + Shortcut sc = new Shortcut(u_fromNode, w_toNode, existingDirectWeight, existingDistSum); + if (shortcuts.containsKey(sc)) + return; + + Shortcut tmpSc = new Shortcut(w_toNode, u_fromNode, existingDirectWeight, existingDistSum); + Shortcut tmpRetSc = shortcuts.get(tmpSc); + if (tmpRetSc != null) { + // overwrite flags only if skipped edges are identical + if (tmpRetSc.skippedEdge2 == skippedEdge1 && tmpRetSc.skippedEdge1 == outgoingEdges.getEdge()) { + tmpRetSc.flags = PrepareEncoder.getScDirMask(); + return; + } + } + + shortcuts.put(sc, sc); + sc.skippedEdge1 = skippedEdge1; + sc.skippedEdge2 = outgoingEdges.getEdge(); + sc.originalEdges = incomingEdgeOrigCount + getOrigEdgeCount(outgoingEdges.getEdge()); + } } } diff --git a/core/src/main/java/com/graphhopper/routing/ch/PrepareEncoder.java b/core/src/main/java/com/graphhopper/routing/ch/PrepareEncoder.java index dbded61e288..6129ed5321a 100644 --- a/core/src/main/java/com/graphhopper/routing/ch/PrepareEncoder.java +++ b/core/src/main/java/com/graphhopper/routing/ch/PrepareEncoder.java @@ -20,27 +20,24 @@ /** * The flags are stored differently for shortcuts: just one weight and the direction flags. *

+ * * @author Peter Karich */ -public class PrepareEncoder -{ +public class PrepareEncoder { // shortcut goes in one or both directions is also possible if weight is identical private static final long scFwdDir = 0x1; private static final long scBwdDir = 0x2; private static final long scDirMask = 0x3; - public static final long getScDirMask() - { + public static final long getScDirMask() { return scDirMask; } - public static final long getScFwdDir() - { + public static final long getScFwdDir() { return scFwdDir; } - public static final long getScBwdDir() - { + public static final long getScBwdDir() { return scBwdDir; } @@ -48,6 +45,7 @@ public static final long getScBwdDir() * Returns true if flags1 can be overwritten in the edge by flags2 without restricting or * changing the directions of flags1. *

+ * * @return true if flags2 is enabled in both directions or if both flags are pointing into the * same direction. */ @@ -56,8 +54,7 @@ public static final long getScBwdDir() // -> t | f | t // <- f | t | t // <-> f | f | t - public static final boolean canBeOverwritten( long flags1, long flags2 ) - { + public static final boolean canBeOverwritten(long flags1, long flags2) { return (flags2 & scDirMask) == scDirMask || (flags1 & scDirMask) == (flags2 & scDirMask); } diff --git a/core/src/main/java/com/graphhopper/routing/subnetwork/PrepareRoutingSubnetworks.java b/core/src/main/java/com/graphhopper/routing/subnetwork/PrepareRoutingSubnetworks.java index 3adc0c37277..3ac78fc2c2a 100644 --- a/core/src/main/java/com/graphhopper/routing/subnetwork/PrepareRoutingSubnetworks.java +++ b/core/src/main/java/com/graphhopper/routing/subnetwork/PrepareRoutingSubnetworks.java @@ -25,58 +25,52 @@ import com.graphhopper.storage.GraphHopperStorage; import com.graphhopper.util.*; import gnu.trove.list.TIntList; - -import java.util.*; -import java.util.concurrent.atomic.AtomicInteger; - +import gnu.trove.list.array.TIntArrayList; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import gnu.trove.list.array.TIntArrayList; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.atomic.AtomicInteger; /** * Removes nodes which are not part of the large networks. Ie. mostly nodes with no edges at all but * also small subnetworks which could be bugs in OSM data or indicate otherwise disconnected areas * e.g. via barriers or one way problems - see #86. *

+ * * @author Peter Karich */ -public class PrepareRoutingSubnetworks -{ +public class PrepareRoutingSubnetworks { private final Logger logger = LoggerFactory.getLogger(getClass()); private final GraphHopperStorage ghStorage; + private final AtomicInteger maxEdgesPerNode = new AtomicInteger(0); + private final List encoders; private int minNetworkSize = 200; private int minOneWayNetworkSize = 0; private int subnetworks = -1; - private final AtomicInteger maxEdgesPerNode = new AtomicInteger(0); - private final List encoders; - public PrepareRoutingSubnetworks( GraphHopperStorage ghStorage, List encoders ) - { + public PrepareRoutingSubnetworks(GraphHopperStorage ghStorage, List encoders) { this.ghStorage = ghStorage; this.encoders = encoders; } - public PrepareRoutingSubnetworks setMinNetworkSize( int minNetworkSize ) - { + public PrepareRoutingSubnetworks setMinNetworkSize(int minNetworkSize) { this.minNetworkSize = minNetworkSize; return this; } - public PrepareRoutingSubnetworks setMinOneWayNetworkSize( int minOnewayNetworkSize ) - { + public PrepareRoutingSubnetworks setMinOneWayNetworkSize(int minOnewayNetworkSize) { this.minOneWayNetworkSize = minOnewayNetworkSize; return this; } - public void doWork() - { + public void doWork() { if (minNetworkSize <= 0 && minOneWayNetworkSize <= 0) return; int unvisitedDeadEnds = 0; - for (FlagEncoder encoder : encoders) - { + for (FlagEncoder encoder : encoders) { // mark edges for one vehicle as inaccessible PrepEdgeFilter filter = new PrepEdgeFilter(encoder); if (minOneWayNetworkSize > 0) @@ -96,41 +90,35 @@ public void doWork() ghStorage.optimize(); } - public int getMaxSubnetworks() - { + public int getMaxSubnetworks() { return subnetworks; } /** * This method finds the double linked components according to the specified filter. */ - List findSubnetworks( PrepEdgeFilter filter ) - { + List findSubnetworks(PrepEdgeFilter filter) { final FlagEncoder encoder = filter.getEncoder(); final EdgeExplorer explorer = ghStorage.createEdgeExplorer(filter); int locs = ghStorage.getNodes(); List list = new ArrayList(100); final GHBitSet bs = new GHBitSetImpl(locs); - for (int start = 0; start < locs; start++) - { + for (int start = 0; start < locs; start++) { if (bs.contains(start)) continue; final TIntArrayList intList = new TIntArrayList(20); list.add(intList); - new BreadthFirstSearch() - { + new BreadthFirstSearch() { int tmpCounter = 0; @Override - protected GHBitSet createBitSet() - { + protected GHBitSet createBitSet() { return bs; } @Override - protected final boolean goFurther( int nodeId ) - { + protected final boolean goFurther(int nodeId) { if (tmpCounter > maxEdgesPerNode.get()) maxEdgesPerNode.set(tmpCounter); @@ -140,10 +128,8 @@ protected final boolean goFurther( int nodeId ) } @Override - protected final boolean checkAdjacent( EdgeIteratorState edge ) - { - if (encoder.isForward(edge.getFlags()) || encoder.isBackward(edge.getFlags())) - { + protected final boolean checkAdjacent(EdgeIteratorState edge) { + if (encoder.isForward(edge.getFlags()) || encoder.isBackward(edge.getFlags())) { tmpCounter++; return true; } @@ -159,8 +145,7 @@ protected final boolean checkAdjacent( EdgeIteratorState edge ) /** * Deletes all but the largest subnetworks. */ - int keepLargeNetworks( PrepEdgeFilter filter, List components ) - { + int keepLargeNetworks(PrepEdgeFilter filter, List components) { if (components.size() <= 1) return 0; @@ -169,25 +154,21 @@ int keepLargeNetworks( PrepEdgeFilter filter, List components ) int allRemoved = 0; FlagEncoder encoder = filter.getEncoder(); EdgeExplorer explorer = ghStorage.createEdgeExplorer(filter); - for (TIntArrayList component : components) - { - if (maxCount < 0) - { + for (TIntArrayList component : components) { + if (maxCount < 0) { maxCount = component.size(); oldComponent = component; continue; } int removedEdges; - if (maxCount < component.size()) - { + if (maxCount < component.size()) { // new biggest area found. remove old removedEdges = removeEdges(explorer, encoder, oldComponent, minNetworkSize); maxCount = component.size(); oldComponent = component; - } else - { + } else { removedEdges = removeEdges(explorer, encoder, component, minNetworkSize); } @@ -199,11 +180,9 @@ int keepLargeNetworks( PrepEdgeFilter filter, List components ) return allRemoved; } - String toString( FlagEncoder encoder, EdgeIterator iter ) - { + String toString(FlagEncoder encoder, EdgeIterator iter) { String str = ""; - while (iter.next()) - { + while (iter.next()) { int adjNode = iter.getAdjNode(); str += adjNode + " (" + ghStorage.getNodeAccess().getLat(adjNode) + "," + ghStorage.getNodeAccess().getLon(adjNode) + "), "; str += "speed (fwd:" + encoder.getSpeed(iter.getFlags()) + ", rev:" + encoder.getReverseSpeed(iter.getFlags()) + "), "; @@ -220,10 +199,10 @@ String toString( FlagEncoder encoder, EdgeIterator iter ) * one-way road. This is clearly an error - but is causes the routing to fail when a point gets * connected to this small area. This routine removes all these networks from the graph. *

+ * * @return number of removed edges */ - int removeDeadEndUnvisitedNetworks( final PrepEdgeFilter bothFilter ) - { + int removeDeadEndUnvisitedNetworks(final PrepEdgeFilter bothFilter) { StopWatch sw = new StopWatch(bothFilter.getEncoder() + " findComponents").start(); final EdgeFilter outFilter = new DefaultEdgeFilter(bothFilter.getEncoder(), false, true); @@ -233,10 +212,8 @@ int removeDeadEndUnvisitedNetworks( final PrepEdgeFilter bothFilter ) EdgeExplorer explorer = ghStorage.createEdgeExplorer(outFilter); int nodes = ghStorage.getNodes(); GHBitSet ignoreSet = new GHBitSetImpl(ghStorage.getNodes()); - for (int start = 0; start < nodes; start++) - { - if (!ghStorage.isNodeRemoved(start)) - { + for (int start = 0; start < nodes; start++) { + if (!ghStorage.isNodeRemoved(start)) { EdgeIterator iter = explorer.setBaseNode(start); if (!iter.next()) ignoreSet.add(start); @@ -255,30 +232,26 @@ int removeDeadEndUnvisitedNetworks( final PrepEdgeFilter bothFilter ) * This method removes the access to edges available from the nodes contained in the components. * But only if a components' size is smaller then the specified min value. *

+ * * @return number of removed edges */ - int removeEdges( final PrepEdgeFilter bothFilter, List components, int min ) - { + int removeEdges(final PrepEdgeFilter bothFilter, List components, int min) { // remove edges determined from nodes but only if less than minimum size FlagEncoder encoder = bothFilter.getEncoder(); EdgeExplorer explorer = ghStorage.createEdgeExplorer(bothFilter); int removedEdges = 0; - for (TIntArrayList component : components) - { + for (TIntArrayList component : components) { removedEdges += removeEdges(explorer, encoder, component, min); } return removedEdges; } - int removeEdges( EdgeExplorer explorer, FlagEncoder encoder, TIntList component, int min ) - { + int removeEdges(EdgeExplorer explorer, FlagEncoder encoder, TIntList component, int min) { int removedEdges = 0; if (component.size() < min) - for (int i = 0; i < component.size(); i++) - { + for (int i = 0; i < component.size(); i++) { EdgeIterator edge = explorer.setBaseNode(component.get(i)); - while (edge.next()) - { + while (edge.next()) { edge.setFlags(encoder.setAccess(edge.getFlags(), false, false)); removedEdges++; } @@ -290,11 +263,9 @@ int removeEdges( EdgeExplorer explorer, FlagEncoder encoder, TIntList component, /** * Removes nodes if all edges are not accessible. I.e. removes zero degree nodes. */ - void markNodesRemovedIfUnreachable() - { + void markNodesRemovedIfUnreachable() { EdgeExplorer edgeExplorer = ghStorage.createEdgeExplorer(); - for (int nodeIndex = 0; nodeIndex < ghStorage.getNodes(); nodeIndex++) - { + for (int nodeIndex = 0; nodeIndex < ghStorage.getNodes(); nodeIndex++) { if (detectNodeRemovedForAllEncoders(edgeExplorer, nodeIndex)) ghStorage.markNodeRemoved(nodeIndex); } @@ -303,20 +274,18 @@ void markNodesRemovedIfUnreachable() /** * This method checks if the node is removed or inaccessible for ALL encoders. *

+ * * @return true if no edges are reachable from the specified nodeIndex for any flag encoder. */ - boolean detectNodeRemovedForAllEncoders( EdgeExplorer edgeExplorerAllEdges, int nodeIndex ) - { + boolean detectNodeRemovedForAllEncoders(EdgeExplorer edgeExplorerAllEdges, int nodeIndex) { // we could implement a 'fast check' for several previously marked removed nodes via GHBitSet // removedNodesPerVehicle. The problem is that we would need long-indices but BitSet only supports int (due to nodeIndex*numberOfEncoders) // if no edges are reachable return true EdgeIterator iter = edgeExplorerAllEdges.setBaseNode(nodeIndex); - while (iter.next()) - { + while (iter.next()) { // if at least on encoder allows one direction return false - for (FlagEncoder encoder : encoders) - { + for (FlagEncoder encoder : encoders) { if (encoder.isBackward(iter.getFlags()) || encoder.isForward(iter.getFlags())) return false; @@ -326,19 +295,16 @@ boolean detectNodeRemovedForAllEncoders( EdgeExplorer edgeExplorerAllEdges, int return true; } - static class PrepEdgeFilter extends DefaultEdgeFilter - { + static class PrepEdgeFilter extends DefaultEdgeFilter { FlagEncoder encoder; - public PrepEdgeFilter( FlagEncoder encoder ) - { + public PrepEdgeFilter(FlagEncoder encoder) { super(encoder); this.encoder = encoder; } - public FlagEncoder getEncoder() - { + public FlagEncoder getEncoder() { return encoder; } } diff --git a/core/src/main/java/com/graphhopper/routing/subnetwork/TarjansSCCAlgorithm.java b/core/src/main/java/com/graphhopper/routing/subnetwork/TarjansSCCAlgorithm.java index 52fb44ec879..959e77ba06c 100644 --- a/core/src/main/java/com/graphhopper/routing/subnetwork/TarjansSCCAlgorithm.java +++ b/core/src/main/java/com/graphhopper/routing/subnetwork/TarjansSCCAlgorithm.java @@ -37,8 +37,7 @@ * See http://en.wikipedia.org/wiki/Tarjan's_strongly_connected_components_algorithm. See * http://www.timl.id.au/?p=327 and http://homepages.ecs.vuw.ac.nz/~djp/files/P05.pdf */ -public class TarjansSCCAlgorithm -{ +public class TarjansSCCAlgorithm { private final ArrayList components = new ArrayList(); private final GraphHopperStorage graph; private final TIntArrayStack nodeStack; @@ -46,12 +45,11 @@ public class TarjansSCCAlgorithm private final GHBitSet ignoreSet; private final int[] nodeIndex; private final int[] nodeLowLink; - private int index = 1; private final EdgeFilter edgeFilter; + private int index = 1; - public TarjansSCCAlgorithm( GraphHopperStorage graph, GHBitSet ignoreSet, - final EdgeFilter edgeFilter ) - { + public TarjansSCCAlgorithm(GraphHopperStorage graph, GHBitSet ignoreSet, + final EdgeFilter edgeFilter) { this.graph = graph; this.nodeStack = new TIntArrayStack(); this.onStack = new GHBitSetImpl(graph.getNodes()); @@ -64,11 +62,9 @@ public TarjansSCCAlgorithm( GraphHopperStorage graph, GHBitSet ignoreSet, /** * Find and return list of all strongly connected components in g. */ - public List findComponents() - { + public List findComponents() { int nodes = graph.getNodes(); - for (int start = 0; start < nodes; start++) - { + for (int start = 0; start < nodes; start++) { if (nodeIndex[start] == 0 && !ignoreSet.contains(start) && !graph.isNodeRemoved(start)) @@ -81,24 +77,22 @@ public List findComponents() /** * Find all components reachable from firstNode, add them to 'components' *

+ * * @param firstNode start search of SCC at this node */ - private void strongConnect( int firstNode ) - { + private void strongConnect(int firstNode) { final Stack stateStack = new Stack(); stateStack.push(TarjanState.startState(firstNode)); // nextState label is equivalent to the function entry point in the recursive Tarjan's algorithm. nextState: - while (!stateStack.empty()) - { + while (!stateStack.empty()) { TarjanState state = stateStack.pop(); final int start = state.start; final EdgeIterator iter; - if (state.isStart()) - { + if (state.isStart()) { // We're traversing a new node 'start'. Set the depth index for this node to the smallest unused index. nodeIndex[start] = index; nodeLowLink[start] = index; @@ -108,8 +102,7 @@ private void strongConnect( int firstNode ) iter = graph.createEdgeExplorer(edgeFilter).setBaseNode(start); - } else - { + } else { // We're resuming iteration over the next child of 'start', set lowLink as appropriate. iter = state.iter; @@ -119,33 +112,28 @@ private void strongConnect( int firstNode ) // Each element (excluding the first) in the current component should be able to find // a successor with a lower nodeLowLink. - while (iter.next()) - { + while (iter.next()) { int connectedId = iter.getAdjNode(); if (ignoreSet.contains(start)) continue; - if (nodeIndex[connectedId] == 0) - { + if (nodeIndex[connectedId] == 0) { // Push resume and start states onto state stack to continue our DFS through the graph after the jump. // Ideally we'd just call strongConnectIterative(connectedId); stateStack.push(TarjanState.resumeState(start, iter)); stateStack.push(TarjanState.startState(connectedId)); continue nextState; - } else if (onStack.contains(connectedId)) - { + } else if (onStack.contains(connectedId)) { nodeLowLink[start] = Math.min(nodeLowLink[start], nodeIndex[connectedId]); } } // If nodeLowLink == nodeIndex, then we are the first element in a component. // Add all nodes higher up on nodeStack to this component. - if (nodeIndex[start] == nodeLowLink[start]) - { + if (nodeIndex[start] == nodeLowLink[start]) { TIntArrayList component = new TIntArrayList(); int node; - while ((node = nodeStack.pop()) != start) - { + while ((node = nodeStack.pop()) != start) { component.add(node); onStack.remove(node); } @@ -162,31 +150,26 @@ private void strongConnect( int firstNode ) * overflow exceptions. State is either 'start' for new nodes or 'resume' for partially * traversed nodes. */ - private static class TarjanState - { + private static class TarjanState { final int start; final EdgeIterator iter; - // Iterator only present in 'resume' state. - boolean isStart() - { - return iter == null; - } - - private TarjanState( final int start, final EdgeIterator iter ) - { + private TarjanState(final int start, final EdgeIterator iter) { this.start = start; this.iter = iter; } - public static TarjanState startState( int start ) - { + public static TarjanState startState(int start) { return new TarjanState(start, null); } - public static TarjanState resumeState( int start, EdgeIterator iter ) - { + public static TarjanState resumeState(int start, EdgeIterator iter) { return new TarjanState(start, iter); } + + // Iterator only present in 'resume' state. + boolean isStart() { + return iter == null; + } } } diff --git a/core/src/main/java/com/graphhopper/routing/template/AbstractRoutingTemplate.java b/core/src/main/java/com/graphhopper/routing/template/AbstractRoutingTemplate.java index 2ae5ac1c973..d2c2fae262b 100644 --- a/core/src/main/java/com/graphhopper/routing/template/AbstractRoutingTemplate.java +++ b/core/src/main/java/com/graphhopper/routing/template/AbstractRoutingTemplate.java @@ -2,22 +2,19 @@ import com.graphhopper.storage.index.QueryResult; import com.graphhopper.util.PointList; + import java.util.List; /** - * * @author Peter Karich */ -public class AbstractRoutingTemplate -{ +public class AbstractRoutingTemplate { // result from lookup protected List queryResults; - protected PointList getWaypoints() - { + protected PointList getWaypoints() { PointList pointList = new PointList(queryResults.size(), true); - for (QueryResult qr : queryResults) - { + for (QueryResult qr : queryResults) { pointList.add(qr.getSnappedPoint()); } return pointList; diff --git a/core/src/main/java/com/graphhopper/routing/template/AlternativeRoutingTemplate.java b/core/src/main/java/com/graphhopper/routing/template/AlternativeRoutingTemplate.java index 3edaad5b9b5..0eec83e01a7 100644 --- a/core/src/main/java/com/graphhopper/routing/template/AlternativeRoutingTemplate.java +++ b/core/src/main/java/com/graphhopper/routing/template/AlternativeRoutingTemplate.java @@ -26,28 +26,27 @@ import com.graphhopper.routing.RoutingAlgorithmFactory; import com.graphhopper.storage.index.LocationIndex; import com.graphhopper.util.Parameters.Routing; -import static com.graphhopper.util.Parameters.Routing.PASS_THROUGH; import com.graphhopper.util.PathMerger; import com.graphhopper.util.PointList; import com.graphhopper.util.Translation; + import java.util.Collections; import java.util.List; +import static com.graphhopper.util.Parameters.Routing.PASS_THROUGH; + /** * Implementation of a route with no via points but multiple path lists ('alternatives'). * * @author Peter Karich */ -final public class AlternativeRoutingTemplate extends ViaRoutingTemplate -{ - public AlternativeRoutingTemplate( GHRequest ghRequest, GHResponse ghRsp, LocationIndex locationIndex ) - { +final public class AlternativeRoutingTemplate extends ViaRoutingTemplate { + public AlternativeRoutingTemplate(GHRequest ghRequest, GHResponse ghRsp, LocationIndex locationIndex) { super(ghRequest, ghRsp, locationIndex); } @Override - public List calcPaths( QueryGraph queryGraph, RoutingAlgorithmFactory algoFactory, AlgorithmOptions algoOpts ) - { + public List calcPaths(QueryGraph queryGraph, RoutingAlgorithmFactory algoFactory, AlgorithmOptions algoOpts) { boolean withViaTurnPenalty = ghRequest.getHints().getBool(Routing.PASS_THROUGH, false); if (withViaTurnPenalty) throw new IllegalArgumentException("Alternative paths and " + PASS_THROUGH + " at the same time is currently not supported"); @@ -56,8 +55,7 @@ public List calcPaths( QueryGraph queryGraph, RoutingAlgorithmFactory algo } @Override - public boolean isReady( PathMerger pathMerger, Translation tr ) - { + public boolean isReady(PathMerger pathMerger, Translation tr) { if (pathList.isEmpty()) throw new RuntimeException("Empty paths for alternative route calculation not expected"); @@ -66,8 +64,7 @@ public boolean isReady( PathMerger pathMerger, Translation tr ) altResponse.setWaypoints(wpList); ghResponse.add(altResponse); pathMerger.doWork(altResponse, Collections.singletonList(pathList.get(0)), tr); - for (int index = 1; index < pathList.size(); index++) - { + for (int index = 1; index < pathList.size(); index++) { PathWrapper tmpAltRsp = new PathWrapper(); tmpAltRsp.setWaypoints(wpList); ghResponse.add(tmpAltRsp); diff --git a/core/src/main/java/com/graphhopper/routing/template/RoundTripRoutingTemplate.java b/core/src/main/java/com/graphhopper/routing/template/RoundTripRoutingTemplate.java index 38a2522006b..d7014461e5e 100644 --- a/core/src/main/java/com/graphhopper/routing/template/RoundTripRoutingTemplate.java +++ b/core/src/main/java/com/graphhopper/routing/template/RoundTripRoutingTemplate.java @@ -21,12 +21,12 @@ import com.graphhopper.GHResponse; import com.graphhopper.PathWrapper; import com.graphhopper.routing.*; -import com.graphhopper.routing.weighting.AvoidEdgesWeighting; import com.graphhopper.routing.util.DefaultEdgeFilter; import com.graphhopper.routing.util.EdgeFilter; import com.graphhopper.routing.util.FlagEncoder; import com.graphhopper.routing.util.tour.MultiPointTour; import com.graphhopper.routing.util.tour.TourStrategy; +import com.graphhopper.routing.weighting.AvoidEdgesWeighting; import com.graphhopper.storage.index.LocationIndex; import com.graphhopper.storage.index.QueryResult; import com.graphhopper.util.Helper; @@ -37,6 +37,7 @@ import com.graphhopper.util.Translation; import com.graphhopper.util.exceptions.CannotFindPointException; import com.graphhopper.util.shapes.GHPoint; + import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -48,18 +49,16 @@ * * @author Peter Karich */ -public class RoundTripRoutingTemplate extends AbstractRoutingTemplate implements RoutingTemplate -{ +public class RoundTripRoutingTemplate extends AbstractRoutingTemplate implements RoutingTemplate { private final int maxRetries; private final GHRequest ghRequest; private final GHResponse ghResponse; - private PathWrapper altResponse; private final LocationIndex locationIndex; + private PathWrapper altResponse; // result from route private List pathList; - public RoundTripRoutingTemplate( GHRequest request, GHResponse ghRsp, LocationIndex locationIndex, int maxRetries ) - { + public RoundTripRoutingTemplate(GHRequest request, GHResponse ghRsp, LocationIndex locationIndex, int maxRetries) { this.ghRequest = request; this.ghResponse = ghRsp; this.locationIndex = locationIndex; @@ -67,8 +66,7 @@ public RoundTripRoutingTemplate( GHRequest request, GHResponse ghRsp, LocationIn } @Override - public List lookup( List points, FlagEncoder encoder ) - { + public List lookup(List points, FlagEncoder encoder) { if (points.isEmpty()) throw new IllegalStateException("For round trip calculation one point is required"); final double distanceInMeter = ghRequest.getHints().getDouble(RoundTrip.DISTANCE, 10000); @@ -87,12 +85,10 @@ public List lookup( List points, FlagEncoder encoder ) queryResults.add(startQR); GHPoint last = points.get(0); - for (int i = 0; i < strategy.getNumberOfGeneratedPoints(); i++) - { + for (int i = 0; i < strategy.getNumberOfGeneratedPoints(); i++) { double heading = strategy.getHeadingForIteration(i); QueryResult result = generateValidPoint(last, strategy.getDistanceForIteration(i), heading, edgeFilter); - if (result == null) - { + if (result == null) { ghResponse.addError(new IllegalStateException("Could not find a valid point after " + maxRetries + " tries, for the point:" + last)); return Collections.emptyList(); } @@ -104,14 +100,12 @@ public List lookup( List points, FlagEncoder encoder ) return queryResults; } - void setQueryResults( List queryResults ) - { + void setQueryResults(List queryResults) { this.queryResults = queryResults; } @Override - public List calcPaths( QueryGraph queryGraph, RoutingAlgorithmFactory algoFactory, AlgorithmOptions algoOpts ) - { + public List calcPaths(QueryGraph queryGraph, RoutingAlgorithmFactory algoFactory, AlgorithmOptions algoOpts) { pathList = new ArrayList<>(queryResults.size() - 1); AvoidEdgesWeighting avoidPathWeighting = new AvoidEdgesWeighting(algoOpts.getWeighting()); @@ -123,8 +117,7 @@ public List calcPaths( QueryGraph queryGraph, RoutingAlgorithmFactory algo long visitedNodesSum = 0L; QueryResult start = queryResults.get(0); - for (int qrIndex = 1; qrIndex < queryResults.size(); qrIndex++) - { + for (int qrIndex = 1; qrIndex < queryResults.size(); qrIndex++) { RoutingAlgorithm algo = algoFactory.createAlgo(queryGraph, algoOpts); // instead getClosestNode (which might be a virtual one and introducing unnecessary tails of the route) // use next tower node -> getBaseNode or getAdjNode @@ -149,14 +142,12 @@ public List calcPaths( QueryGraph queryGraph, RoutingAlgorithmFactory algo return pathList; } - public void setPaths( List pathList ) - { + public void setPaths(List pathList) { this.pathList = pathList; } @Override - public boolean isReady( PathMerger pathMerger, Translation tr ) - { + public boolean isReady(PathMerger pathMerger, Translation tr) { altResponse = new PathWrapper(); altResponse.setWaypoints(getWaypoints()); ghResponse.add(altResponse); @@ -165,12 +156,10 @@ public boolean isReady( PathMerger pathMerger, Translation tr ) return true; } - private QueryResult generateValidPoint( GHPoint from, double distanceInMeters, double heading, - EdgeFilter edgeFilter ) - { + private QueryResult generateValidPoint(GHPoint from, double distanceInMeters, double heading, + EdgeFilter edgeFilter) { int tryCount = 0; - while (true) - { + while (true) { GHPoint generatedPoint = Helper.DIST_EARTH.projectCoordinate(from.getLat(), from.getLon(), distanceInMeters, heading); QueryResult qr = locationIndex.findClosest(generatedPoint.getLat(), generatedPoint.getLon(), edgeFilter); if (qr.isValid()) @@ -185,8 +174,7 @@ private QueryResult generateValidPoint( GHPoint from, double distanceInMeters, d } @Override - public int getMaxRetries() - { + public int getMaxRetries() { // with potentially retrying, including generating new route points, for now disabled return 1; } diff --git a/core/src/main/java/com/graphhopper/routing/template/RoutingTemplate.java b/core/src/main/java/com/graphhopper/routing/template/RoutingTemplate.java index 1ff1169ad10..d36445fb654 100644 --- a/core/src/main/java/com/graphhopper/routing/template/RoutingTemplate.java +++ b/core/src/main/java/com/graphhopper/routing/template/RoutingTemplate.java @@ -26,6 +26,7 @@ import com.graphhopper.util.PathMerger; import com.graphhopper.util.Translation; import com.graphhopper.util.shapes.GHPoint; + import java.util.List; /** @@ -34,24 +35,23 @@ * * @author Peter Karich */ -public interface RoutingTemplate -{ +public interface RoutingTemplate { /** * This method takes the query points and returns the looked up QueryResults. */ - List lookup( List points, FlagEncoder encoder ); + List lookup(List points, FlagEncoder encoder); /** * This method returns a list of Path objects which then can be merged to serve one route with * via points or multiple alternative paths. */ - List calcPaths( QueryGraph queryGraph, RoutingAlgorithmFactory algoFactory, AlgorithmOptions algoOpts ); + List calcPaths(QueryGraph queryGraph, RoutingAlgorithmFactory algoFactory, AlgorithmOptions algoOpts); /** * This method merges the returned paths appropriately e.g. all paths from the list into one * PathWrapper of GHResponse or multiple (via / round trip). */ - boolean isReady( PathMerger pathMerger, Translation tr ); + boolean isReady(PathMerger pathMerger, Translation tr); /** * This method returns the maximum number of full retries of these 3 steps diff --git a/core/src/main/java/com/graphhopper/routing/template/ViaRoutingTemplate.java b/core/src/main/java/com/graphhopper/routing/template/ViaRoutingTemplate.java index 8fbf134b176..cbf4374e5a1 100644 --- a/core/src/main/java/com/graphhopper/routing/template/ViaRoutingTemplate.java +++ b/core/src/main/java/com/graphhopper/routing/template/ViaRoutingTemplate.java @@ -26,10 +26,14 @@ import com.graphhopper.routing.util.FlagEncoder; import com.graphhopper.storage.index.LocationIndex; import com.graphhopper.storage.index.QueryResult; -import com.graphhopper.util.*; +import com.graphhopper.util.EdgeIteratorState; import com.graphhopper.util.Parameters.Routing; +import com.graphhopper.util.PathMerger; +import com.graphhopper.util.StopWatch; +import com.graphhopper.util.Translation; import com.graphhopper.util.exceptions.CannotFindPointException; import com.graphhopper.util.shapes.GHPoint; + import java.util.ArrayList; import java.util.List; @@ -38,32 +42,28 @@ * * @author Peter Karich */ -public class ViaRoutingTemplate extends AbstractRoutingTemplate implements RoutingTemplate -{ +public class ViaRoutingTemplate extends AbstractRoutingTemplate implements RoutingTemplate { protected final GHRequest ghRequest; protected final GHResponse ghResponse; protected final PathWrapper altResponse = new PathWrapper(); - private final LocationIndex locationIndex; + private final LocationIndex locationIndex; // result from route protected List pathList; - public ViaRoutingTemplate( GHRequest ghRequest, GHResponse ghRsp, LocationIndex locationIndex ) - { + public ViaRoutingTemplate(GHRequest ghRequest, GHResponse ghRsp, LocationIndex locationIndex) { this.locationIndex = locationIndex; this.ghRequest = ghRequest; this.ghResponse = ghRsp; } @Override - public List lookup( List points, FlagEncoder encoder ) - { + public List lookup(List points, FlagEncoder encoder) { if (points.size() < 2) throw new IllegalArgumentException("At least 2 points have to be specified, but was:" + points.size()); EdgeFilter edgeFilter = new DefaultEdgeFilter(encoder); queryResults = new ArrayList<>(points.size()); - for (int placeIndex = 0; placeIndex < points.size(); placeIndex++) - { + for (int placeIndex = 0; placeIndex < points.size(); placeIndex++) { GHPoint point = points.get(placeIndex); QueryResult res = locationIndex.findClosest(point.lat, point.lon, edgeFilter); if (!res.isValid()) @@ -71,27 +71,23 @@ public List lookup( List points, FlagEncoder encoder ) queryResults.add(res); } - + return queryResults; } @Override - public List calcPaths( QueryGraph queryGraph, RoutingAlgorithmFactory algoFactory, AlgorithmOptions algoOpts ) - { + public List calcPaths(QueryGraph queryGraph, RoutingAlgorithmFactory algoFactory, AlgorithmOptions algoOpts) { long visitedNodesSum = 0L; boolean viaTurnPenalty = ghRequest.getHints().getBool(Routing.PASS_THROUGH, false); int pointCounts = ghRequest.getPoints().size(); pathList = new ArrayList<>(pointCounts - 1); QueryResult fromQResult = queryResults.get(0); StopWatch sw; - for (int placeIndex = 1; placeIndex < pointCounts; placeIndex++) - { - if (placeIndex == 1) - { + for (int placeIndex = 1; placeIndex < pointCounts; placeIndex++) { + if (placeIndex == 1) { // enforce start direction queryGraph.enforceHeading(fromQResult.getClosestNode(), ghRequest.getFavoredHeading(0), false); - } else if (viaTurnPenalty) - { + } else if (viaTurnPenalty) { // enforce straight start after via stop Path prevRoute = pathList.get(placeIndex - 2); EdgeIteratorState incomingVirtualEdge = prevRoute.getFinalEdge(); @@ -113,8 +109,7 @@ public List calcPaths( QueryGraph queryGraph, RoutingAlgorithmFactory algo if (tmpPathList.isEmpty()) throw new IllegalStateException("At least one path has to be returned for " + fromQResult + " -> " + toQResult); - for (Path path : tmpPathList) - { + for (Path path : tmpPathList) { if (path.getTime() < 0) throw new RuntimeException("Time was negative. Please report as bug and include:" + ghRequest); @@ -141,8 +136,7 @@ public List calcPaths( QueryGraph queryGraph, RoutingAlgorithmFactory algo } @Override - public boolean isReady( PathMerger pathMerger, Translation tr ) - { + public boolean isReady(PathMerger pathMerger, Translation tr) { if (ghRequest.getPoints().size() - 1 != pathList.size()) throw new RuntimeException("There should be exactly one more points than paths. points:" + ghRequest.getPoints().size() + ", paths:" + pathList.size()); @@ -153,8 +147,7 @@ public boolean isReady( PathMerger pathMerger, Translation tr ) } @Override - public int getMaxRetries() - { + public int getMaxRetries() { return 1; } } diff --git a/core/src/main/java/com/graphhopper/routing/util/AbstractAlgoPreparation.java b/core/src/main/java/com/graphhopper/routing/util/AbstractAlgoPreparation.java index 48190ae2774..7d2715e1111 100644 --- a/core/src/main/java/com/graphhopper/routing/util/AbstractAlgoPreparation.java +++ b/core/src/main/java/com/graphhopper/routing/util/AbstractAlgoPreparation.java @@ -20,20 +20,17 @@ /** * @author Peter Karich */ -public abstract class AbstractAlgoPreparation -{ +public abstract class AbstractAlgoPreparation { private boolean prepared = false; - public void doWork() - { + public void doWork() { if (prepared) throw new IllegalStateException("Call doWork only once!"); prepared = true; } - public boolean isPrepared() - { + public boolean isPrepared() { return prepared; } } diff --git a/core/src/main/java/com/graphhopper/routing/util/AbstractFlagEncoder.java b/core/src/main/java/com/graphhopper/routing/util/AbstractFlagEncoder.java index e86c9352075..89331b5e247 100644 --- a/core/src/main/java/com/graphhopper/routing/util/AbstractFlagEncoder.java +++ b/core/src/main/java/com/graphhopper/routing/util/AbstractFlagEncoder.java @@ -17,37 +17,47 @@ */ package com.graphhopper.routing.util; -import com.graphhopper.routing.weighting.TurnWeighting; +import com.graphhopper.reader.ConditionalTagInspector; import com.graphhopper.reader.ReaderNode; import com.graphhopper.reader.ReaderRelation; import com.graphhopper.reader.ReaderWay; +import com.graphhopper.reader.osm.conditional.ConditionalOSMTagInspector; +import com.graphhopper.reader.osm.conditional.DateRangeParser; +import com.graphhopper.routing.weighting.TurnWeighting; +import com.graphhopper.util.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.graphhopper.util.*; - -import java.util.*; -import com.graphhopper.reader.ConditionalTagInspector; -import com.graphhopper.reader.osm.conditional.ConditionalOSMTagInspector; -import com.graphhopper.reader.osm.conditional.DateRangeParser; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; /** * Abstract class which handles flag decoding and encoding. Every encoder should be registered to a * EncodingManager to be usable. If you want the full long to be stored you need to enable this in * the GraphHopperStorage. *

+ * * @author Peter Karich * @author Nop * @see EncodingManager */ -public abstract class AbstractFlagEncoder implements FlagEncoder, TurnCostEncoder -{ - private final static Logger logger = LoggerFactory.getLogger(AbstractFlagEncoder.class); +public abstract class AbstractFlagEncoder implements FlagEncoder, TurnCostEncoder { protected final static int K_FORWARD = 0, K_BACKWARD = 1; - /* Edge Flag Encoder fields */ - private long nodeBitMask; - private long wayBitMask; - private long relBitMask; + private final static Logger logger = LoggerFactory.getLogger(AbstractFlagEncoder.class); + /* restriction definitions where order is important */ + protected final List restrictions = new ArrayList(5); + protected final Set intendedValues = new HashSet(5); + protected final Set restrictedValues = new HashSet(5); + protected final Set ferries = new HashSet(5); + protected final Set oneways = new HashSet(5); + // http://wiki.openstreetmap.org/wiki/Mapfeatures#Barrier + protected final Set absoluteBarriers = new HashSet(5); + protected final Set potentialBarriers = new HashSet(5); + protected final int speedBits; + protected final double speedFactor; + private final int maxTurnCosts; protected long forwardBit; protected long backwardBit; protected long directionBitMask; @@ -56,57 +66,41 @@ public abstract class AbstractFlagEncoder implements FlagEncoder, TurnCostEncode // bit to signal that way is accepted protected long acceptBit; protected long ferryBit; - protected PMap properties; - // This value determines the maximal possible speed of any road regardless the maxspeed value // lower values allow more compact representation of the routing graph protected int maxPossibleSpeed; - - private EncodedValue turnCostEncoder; - private long turnRestrictionBit; - private final int maxTurnCosts; - /* processing properties (to be initialized lazy when needed) */ protected EdgeExplorer edgeOutExplorer; protected EdgeExplorer edgeInExplorer; - - /* restriction definitions where order is important */ - protected final List restrictions = new ArrayList(5); - protected final Set intendedValues = new HashSet(5); - protected final Set restrictedValues = new HashSet(5); - protected final Set ferries = new HashSet(5); - protected final Set oneways = new HashSet(5); - // http://wiki.openstreetmap.org/wiki/Mapfeatures#Barrier - protected final Set absoluteBarriers = new HashSet(5); - protected final Set potentialBarriers = new HashSet(5); + /* Edge Flag Encoder fields */ + private long nodeBitMask; + private long wayBitMask; + private long relBitMask; + private EncodedValue turnCostEncoder; + private long turnRestrictionBit; private boolean blockByDefault = true; private boolean blockFords = true; - protected final int speedBits; - protected final double speedFactor; private boolean registered; private ConditionalTagInspector conditionalTagInspector; - public AbstractFlagEncoder( PMap properties ) - { + public AbstractFlagEncoder(PMap properties) { throw new RuntimeException("This method must be overridden in derived classes"); } - public AbstractFlagEncoder( String propertiesStr ) - { + public AbstractFlagEncoder(String propertiesStr) { this(new PMap(propertiesStr)); } /** - * @param speedBits specify the number of bits used for speed - * @param speedFactor specify the factor to multiple the stored value (can be used to increase - * or decrease accuracy of speed value) + * @param speedBits specify the number of bits used for speed + * @param speedFactor specify the factor to multiple the stored value (can be used to increase + * or decrease accuracy of speed value) * @param maxTurnCosts specify the maximum value used for turn costs, if this value is reached a - * turn is forbidden and results in costs of positive infinity. + * turn is forbidden and results in costs of positive infinity. */ - protected AbstractFlagEncoder( int speedBits, double speedFactor, int maxTurnCosts ) - { + protected AbstractFlagEncoder(int speedBits, double speedFactor, int maxTurnCosts) { this.maxTurnCosts = maxTurnCosts <= 0 ? 0 : maxTurnCosts; this.speedBits = speedBits; this.speedFactor = speedFactor; @@ -120,69 +114,61 @@ protected AbstractFlagEncoder( int speedBits, double speedFactor, int maxTurnCos } // should be called as last method in constructor, move out of the flag encoder somehow - protected void init() - { + protected void init() { // we should move 'OSM to object' logic into the DataReader like OSMReader, but this is a major task as we need to convert OSM format into kind of a standard/generic format conditionalTagInspector = new ConditionalOSMTagInspector(DateRangeParser.createCalendar(), restrictions, restrictedValues, intendedValues); } - protected void setConditionalTagInspector( ConditionalTagInspector conditionalTagInspector ) - { - this.conditionalTagInspector = conditionalTagInspector; + @Override + public boolean isRegistered() { + return registered; } - public void setRegistered( boolean registered ) - { + public void setRegistered(boolean registered) { this.registered = registered; } - @Override - public boolean isRegistered() - { - return registered; - } - /** * Should potential barriers block when no access limits are given? */ - public void setBlockByDefault( boolean blockByDefault ) - { + public void setBlockByDefault(boolean blockByDefault) { this.blockByDefault = blockByDefault; } - public void setBlockFords( boolean blockFords ) - { - this.blockFords = blockFords; + public boolean isBlockFords() { + return blockFords; } - public boolean isBlockFords() - { - return blockFords; + public void setBlockFords(boolean blockFords) { + this.blockFords = blockFords; } - public ConditionalTagInspector getConditionalTagInspector() - { + public ConditionalTagInspector getConditionalTagInspector() { return conditionalTagInspector; } + protected void setConditionalTagInspector(ConditionalTagInspector conditionalTagInspector) { + this.conditionalTagInspector = conditionalTagInspector; + } + /** * Defines the bits for the node flags, which are currently used for barriers only. *

+ * * @return incremented shift value pointing behind the last used bit */ - public int defineNodeBits( int index, int shift ) - { + public int defineNodeBits(int index, int shift) { return shift; } /** * Defines bits used for edge flags used for access, speed etc. *

+ * * @param shift bit offset for the first bit used by this encoder * @return incremented shift value pointing behind the last used bit */ - public int defineWayBits( int index, int shift ) - { + public int defineWayBits(int index, int shift) { // define the first 2 speedBits in flags for routing forwardBit = 1L << shift; backwardBit = 2L << shift; @@ -202,10 +188,10 @@ public int defineWayBits( int index, int shift ) /** * Defines the bits which are used for relation flags. *

+ * * @return incremented shift value pointing behind the last used bit */ - public int defineRelationBits( int index, int shift ) - { + public int defineRelationBits(int index, int shift) { return shift; } @@ -214,42 +200,40 @@ public int defineRelationBits( int index, int shift ) * In the pre-parsing step this method will be called to determine the useful relation tags. *

*/ - public abstract long handleRelationTags( ReaderRelation relation, long oldRelationFlags ); + public abstract long handleRelationTags(ReaderRelation relation, long oldRelationFlags); /** * Decide whether a way is routable for a given mode of travel. This skips some ways before * handleWayTags is called. *

+ * * @return the encoded value to indicate if this encoder allows travel or not. */ - public abstract long acceptWay( ReaderWay way ); + public abstract long acceptWay(ReaderWay way); /** * Analyze properties of a way and create the routing flags. This method is called in the second * parsing step. */ - public abstract long handleWayTags( ReaderWay way, long allowed, long relationFlags ); + public abstract long handleWayTags(ReaderWay way, long allowed, long relationFlags); /** * Parse tags on nodes. Node tags can add to speed (like traffic_signals) where the value is * strict negative or blocks access (like a barrier), then the value is strict positive.This * method is called in the second parsing step. */ - public long handleNodeTags( ReaderNode node ) - { + public long handleNodeTags(ReaderNode node) { // absolute barriers always block if (node.hasTag("barrier", absoluteBarriers)) return directionBitMask; // movable barriers block if they are not marked as passable - if (node.hasTag("barrier", potentialBarriers)) - { + if (node.hasTag("barrier", potentialBarriers)) { boolean locked = false; if (node.hasTag("locked", "yes")) locked = true; - for (String res : restrictions) - { + for (String res : restrictions) { if (!locked && node.hasTag(res, intendedValues)) return 0; @@ -265,8 +249,7 @@ public long handleNodeTags( ReaderNode node ) if (blockFords && (node.hasTag("highway", "ford") || node.hasTag("ford")) && !node.hasTag(restrictions, intendedValues) - && !node.hasTag("ford", "no")) - { + && !node.hasTag("ford", "no")) { return directionBitMask; } @@ -275,8 +258,7 @@ public long handleNodeTags( ReaderNode node ) } @Override - public InstructionAnnotation getAnnotation( long flags, Translation tr ) - { + public InstructionAnnotation getAnnotation(long flags, Translation tr) { return InstructionAnnotation.EMPTY; } @@ -285,8 +267,7 @@ public InstructionAnnotation getAnnotation( long flags, Translation tr ) * the access bits. But also direction dependent speed values should be swapped too. Keep in * mind that this method is performance critical! */ - public long reverseFlags( long flags ) - { + public long reverseFlags(long flags) { long dir = flags & directionBitMask; if (dir == directionBitMask || dir == 0) return flags; @@ -297,21 +278,18 @@ public long reverseFlags( long flags ) /** * Sets default flags with specified access. */ - public long flagsDefault( boolean forward, boolean backward ) - { + public long flagsDefault(boolean forward, boolean backward) { long flags = speedEncoder.setDefaultValue(0); return setAccess(flags, forward, backward); } @Override - public long setAccess( long flags, boolean forward, boolean backward ) - { + public long setAccess(long flags, boolean forward, boolean backward) { return setBool(setBool(flags, K_BACKWARD, backward), K_FORWARD, forward); } @Override - public long setSpeed( long flags, double speed ) - { + public long setSpeed(long flags, double speed) { if (speed < 0 || Double.isNaN(speed)) throw new IllegalArgumentException("Speed cannot be negative or NaN: " + speed + ", flags:" + BitUtil.LITTLE.toBitString(flags)); @@ -325,14 +303,12 @@ public long setSpeed( long flags, double speed ) return speedEncoder.setDoubleValue(flags, speed); } - protected long setLowSpeed( long flags, double speed, boolean reverse ) - { + protected long setLowSpeed(long flags, double speed, boolean reverse) { return setAccess(speedEncoder.setDoubleValue(flags, 0), false, false); } @Override - public double getSpeed( long flags ) - { + public double getSpeed(long flags) { double speedVal = speedEncoder.getDoubleValue(flags); if (speedVal < 0) throw new IllegalStateException("Speed was negative!? " + speedVal); @@ -341,34 +317,29 @@ public double getSpeed( long flags ) } @Override - public long setReverseSpeed( long flags, double speed ) - { + public long setReverseSpeed(long flags, double speed) { return setSpeed(flags, speed); } @Override - public double getReverseSpeed( long flags ) - { + public double getReverseSpeed(long flags) { return getSpeed(flags); } @Override - public long setProperties( double speed, boolean forward, boolean backward ) - { + public long setProperties(double speed, boolean forward, boolean backward) { return setAccess(setSpeed(0, speed), forward, backward); } @Override - public double getMaxSpeed() - { + public double getMaxSpeed() { return speedEncoder.getMaxValue(); } /** * @return -1 if no maxspeed found */ - protected double getMaxSpeed( ReaderWay way ) - { + protected double getMaxSpeed(ReaderWay way) { double maxSpeed = parseSpeed(way.getTag("maxspeed")); double fwdSpeed = parseSpeed(way.getTag("maxspeed:forward")); if (fwdSpeed >= 0 && (maxSpeed < 0 || fwdSpeed < maxSpeed)) @@ -382,8 +353,7 @@ protected double getMaxSpeed( ReaderWay way ) } @Override - public int hashCode() - { + public int hashCode() { int hash = 7; hash = 61 * hash + (int) this.directionBitMask; hash = 61 * hash + this.toString().hashCode(); @@ -391,8 +361,7 @@ public int hashCode() } @Override - public boolean equals( Object obj ) - { + public boolean equals(Object obj) { if (obj == null) return false; @@ -409,8 +378,7 @@ public boolean equals( Object obj ) /** * @return the speed in km/h */ - protected double parseSpeed( String str ) - { + protected double parseSpeed(String str) { if (Helper.isEmpty(str)) return -1; @@ -427,42 +395,35 @@ protected double parseSpeed( String str ) if (str.equals("walk") || str.endsWith(":living_street")) return 6; - try - { + try { int val; // see https://en.wikipedia.org/wiki/Knot_%28unit%29#Definitions int mpInteger = str.indexOf("mp"); - if (mpInteger > 0) - { + if (mpInteger > 0) { str = str.substring(0, mpInteger).trim(); val = Integer.parseInt(str); return val * DistanceCalcEarth.KM_MILE; } int knotInteger = str.indexOf("knots"); - if (knotInteger > 0) - { + if (knotInteger > 0) { str = str.substring(0, knotInteger).trim(); val = Integer.parseInt(str); return val * 1.852; } int kmInteger = str.indexOf("km"); - if (kmInteger > 0) - { + if (kmInteger > 0) { str = str.substring(0, kmInteger).trim(); - } else - { + } else { kmInteger = str.indexOf("kph"); - if (kmInteger > 0) - { + if (kmInteger > 0) { str = str.substring(0, kmInteger).trim(); } } return Integer.parseInt(str); - } catch (Exception ex) - { + } catch (Exception ex) { return -1; } } @@ -471,32 +432,26 @@ protected double parseSpeed( String str ) * Second parsing step. Invoked after splitting the edges. Currently used to offer a hook to * calculate precise speed values based on elevation data stored in the specified edge. */ - public void applyWayTags( ReaderWay way, EdgeIteratorState edge ) - { + public void applyWayTags(ReaderWay way, EdgeIteratorState edge) { } /** * Special handling for ferry ways. */ - protected double getFerrySpeed( ReaderWay way, double unknownSpeed, double shortTripsSpeed, double longTripsSpeed ) - { + protected double getFerrySpeed(ReaderWay way, double unknownSpeed, double shortTripsSpeed, double longTripsSpeed) { long duration = 0; - try - { + try { // During the reader process we have converted the duration value into a artificial tag called "duration:seconds". duration = Long.parseLong(way.getTag("duration:seconds")); - } catch (Exception ex) - { + } catch (Exception ex) { } // seconds to hours double durationInHours = duration / 60d / 60d; if (durationInHours > 0) - try - { + try { // Check if our graphhopper specific artificially created estimated_distance way tag is present Number estimatedLength = way.getTag("estimated_distance", null); - if (estimatedLength != null) - { + if (estimatedLength != null) { // to km double val = estimatedLength.doubleValue() / 1000; // If duration AND distance is available we can calculate the speed more precisely @@ -504,104 +459,87 @@ protected double getFerrySpeed( ReaderWay way, double unknownSpeed, double short double calculatedTripSpeed = val / durationInHours / 1.4; // Plausibility check especially for the case of wrongly used PxM format with the intention to // specify the duration in minutes, but actually using months - if (calculatedTripSpeed > 0.01d) - { + if (calculatedTripSpeed > 0.01d) { // If we have a very short ferry with an average lower compared to what we can encode // then we need to avoid setting it as otherwise the edge would not be found at all any more. - if (Math.round(calculatedTripSpeed) > speedEncoder.factor / 2) - { + if (Math.round(calculatedTripSpeed) > speedEncoder.factor / 2) { shortTripsSpeed = Math.round(calculatedTripSpeed); if (shortTripsSpeed > getMaxSpeed()) shortTripsSpeed = getMaxSpeed(); longTripsSpeed = shortTripsSpeed; - } else - { + } else { // Now we set to the lowest possible still accessible speed. shortTripsSpeed = speedEncoder.factor / 2; } - } else - { + } else { logger.warn("Unrealistic long duration ignored in way with way ID=" + way.getId() + " : Duration tag value=" + way.getTag("duration") + " (=" + Math.round(duration / 60d) + " minutes)"); durationInHours = 0; } } - } catch (Exception ex) - { + } catch (Exception ex) { } - if (durationInHours == 0) - { + if (durationInHours == 0) { // unknown speed -> put penalty on ferry transport return unknownSpeed; - } else if (durationInHours > 1) - { + } else if (durationInHours > 1) { // lengthy ferries should be faster than short trip ferry return longTripsSpeed; - } else - { + } else { return shortTripsSpeed; } } - void setWayBitMask( int usedBits, int shift ) - { + void setWayBitMask(int usedBits, int shift) { wayBitMask = (1L << usedBits) - 1; wayBitMask <<= shift; } - long getWayBitMask() - { + long getWayBitMask() { return wayBitMask; } - void setRelBitMask( int usedBits, int shift ) - { + void setRelBitMask(int usedBits, int shift) { relBitMask = (1L << usedBits) - 1; relBitMask <<= shift; } - long getRelBitMask() - { + long getRelBitMask() { return relBitMask; } - void setNodeBitMask( int usedBits, int shift ) - { + void setNodeBitMask(int usedBits, int shift) { nodeBitMask = (1L << usedBits) - 1; nodeBitMask <<= shift; } - long getNodeBitMask() - { + long getNodeBitMask() { return nodeBitMask; } /** * Defines the bits reserved for storing turn restriction and turn cost *

+ * * @param shift bit offset for the first bit used by this encoder * @return incremented shift value pointing behind the last used bit */ - public int defineTurnBits( int index, int shift ) - { + public int defineTurnBits(int index, int shift) { if (maxTurnCosts == 0) return shift; // optimization for turn restrictions only - else if (maxTurnCosts == 1) - { + else if (maxTurnCosts == 1) { turnRestrictionBit = 1L << shift; return shift + 1; } int turnBits = Helper.countBitValue(maxTurnCosts); - turnCostEncoder = new EncodedValue("TurnCost", shift, turnBits, 1, 0, maxTurnCosts) - { + turnCostEncoder = new EncodedValue("TurnCost", shift, turnBits, 1, 0, maxTurnCosts) { // override to avoid expensive Math.round @Override - public final long getValue( long flags ) - { + public final long getValue(long flags) { // find value flags &= mask; flags >>>= shift; @@ -612,8 +550,7 @@ public final long getValue( long flags ) } @Override - public boolean isTurnRestricted( long flags ) - { + public boolean isTurnRestricted(long flags) { if (maxTurnCosts == 0) return false; @@ -624,8 +561,7 @@ else if (maxTurnCosts == 1) } @Override - public double getTurnCost( long flags ) - { + public double getTurnCost(long flags) { if (maxTurnCosts == 0) return 0; @@ -640,21 +576,18 @@ else if (maxTurnCosts == 1) } @Override - public long getTurnFlags( boolean restricted, double costs ) - { + public long getTurnFlags(boolean restricted, double costs) { if (maxTurnCosts == 0) return 0; - else if (maxTurnCosts == 1) - { + else if (maxTurnCosts == 1) { if (costs != 0) throw new IllegalArgumentException("Only restrictions are supported"); return restricted ? turnRestrictionBit : 0; } - if (restricted) - { + if (restricted) { if (costs != 0 || Double.isInfinite(costs)) throw new IllegalArgumentException("Restricted turn can only have infinite costs (or use 0)"); } else if (costs >= maxTurnCosts) @@ -668,33 +601,27 @@ else if (maxTurnCosts == 1) return turnCostEncoder.setValue(0L, (int) costs); } - protected boolean isFerry( long internalFlags ) - { + protected boolean isFerry(long internalFlags) { return (internalFlags & ferryBit) != 0; } - protected boolean isAccept( long internalFlags ) - { + protected boolean isAccept(long internalFlags) { return (internalFlags & acceptBit) != 0; } @Override - public boolean isBackward( long flags ) - { + public boolean isBackward(long flags) { return (flags & backwardBit) != 0; } @Override - public boolean isForward( long flags ) - { + public boolean isForward(long flags) { return (flags & forwardBit) != 0; } @Override - public long setBool( long flags, int key, boolean value ) - { - switch (key) - { + public long setBool(long flags, int key, boolean value) { + switch (key) { case K_FORWARD: return value ? flags | forwardBit : flags & ~forwardBit; case K_BACKWARD: @@ -707,10 +634,8 @@ public long setBool( long flags, int key, boolean value ) } @Override - public boolean isBool( long flags, int key ) - { - switch (key) - { + public boolean isBool(long flags, int key) { + switch (key) { case K_FORWARD: return isForward(flags); case K_BACKWARD: @@ -723,54 +648,46 @@ public boolean isBool( long flags, int key ) } @Override - public long setLong( long flags, int key, long value ) - { + public long setLong(long flags, int key, long value) { throw new UnsupportedOperationException("Unknown key " + key + " for long value."); } @Override - public long getLong( long flags, int key ) - { + public long getLong(long flags, int key) { throw new UnsupportedOperationException("Unknown key " + key + " for long value."); } @Override - public long setDouble( long flags, int key, double value ) - { + public long setDouble(long flags, int key, double value) { throw new UnsupportedOperationException("Unknown key " + key + " for double value."); } @Override - public double getDouble( long flags, int key ) - { + public double getDouble(long flags, int key) { throw new UnsupportedOperationException("Unknown key " + key + " for double value."); } /** - * @param way: needed to retrieve tags + * @param way: needed to retrieve tags * @param speed: speed guessed e.g. from the road type or other tags * @return The assumed speed. */ - protected double applyMaxSpeed( ReaderWay way, double speed ) - { + protected double applyMaxSpeed(ReaderWay way, double speed) { double maxSpeed = getMaxSpeed(way); // We obay speed limits - if (maxSpeed >= 0) - { + if (maxSpeed >= 0) { // We assume that the average speed is 90% of the allowed maximum return maxSpeed * 0.9; } return speed; } - protected String getPropertiesString() - { + protected String getPropertiesString() { return "speed_factor=" + speedFactor + "|speed_bits=" + speedBits + "|turn_costs=" + (maxTurnCosts > 0); } @Override - public boolean supports( Class feature ) - { + public boolean supports(Class feature) { if (TurnWeighting.class.isAssignableFrom(feature)) return maxTurnCosts > 0; diff --git a/core/src/main/java/com/graphhopper/routing/util/AllCHEdgesIterator.java b/core/src/main/java/com/graphhopper/routing/util/AllCHEdgesIterator.java index 92f6a9468e8..d0716d5d77b 100644 --- a/core/src/main/java/com/graphhopper/routing/util/AllCHEdgesIterator.java +++ b/core/src/main/java/com/graphhopper/routing/util/AllCHEdgesIterator.java @@ -22,6 +22,5 @@ /** * @author Peter Karich */ -public interface AllCHEdgesIterator extends AllEdgesIterator, CHEdgeIterator -{ +public interface AllCHEdgesIterator extends AllEdgesIterator, CHEdgeIterator { } diff --git a/core/src/main/java/com/graphhopper/routing/util/AllEdgesIterator.java b/core/src/main/java/com/graphhopper/routing/util/AllEdgesIterator.java index a34bc60d44d..3a3e519e83f 100644 --- a/core/src/main/java/com/graphhopper/routing/util/AllEdgesIterator.java +++ b/core/src/main/java/com/graphhopper/routing/util/AllEdgesIterator.java @@ -22,8 +22,7 @@ /** * @author Peter Karich */ -public interface AllEdgesIterator extends EdgeIterator -{ +public interface AllEdgesIterator extends EdgeIterator { /** * @return the maximum edgeId in the graph which is roughly the number of edges. */ diff --git a/core/src/main/java/com/graphhopper/routing/util/Bike2WeightFlagEncoder.java b/core/src/main/java/com/graphhopper/routing/util/Bike2WeightFlagEncoder.java index ef14776495c..6e743fd6232 100644 --- a/core/src/main/java/com/graphhopper/routing/util/Bike2WeightFlagEncoder.java +++ b/core/src/main/java/com/graphhopper/routing/util/Bike2WeightFlagEncoder.java @@ -23,46 +23,40 @@ import com.graphhopper.util.PMap; import com.graphhopper.util.PointList; -import static com.graphhopper.util.Helper.*; +import static com.graphhopper.util.Helper.keepIn; /** * Stores two speed values into an edge to support avoiding too much incline *

+ * * @author Peter Karich */ -public class Bike2WeightFlagEncoder extends BikeFlagEncoder -{ +public class Bike2WeightFlagEncoder extends BikeFlagEncoder { private EncodedDoubleValue reverseSpeedEncoder; - public Bike2WeightFlagEncoder() - { + public Bike2WeightFlagEncoder() { super(); } - public Bike2WeightFlagEncoder( String propertiesStr ) - { + public Bike2WeightFlagEncoder(String propertiesStr) { super(new PMap(propertiesStr)); } - public Bike2WeightFlagEncoder( PMap properties ) - { + public Bike2WeightFlagEncoder(PMap properties) { super(properties); } - public Bike2WeightFlagEncoder( int speedBits, double speedFactor, int maxTurnCosts ) - { + public Bike2WeightFlagEncoder(int speedBits, double speedFactor, int maxTurnCosts) { super(speedBits, speedFactor, maxTurnCosts); } @Override - public int getVersion() - { + public int getVersion() { return 1; } @Override - public int defineWayBits( int index, int shift ) - { + public int defineWayBits(int index, int shift) { shift = super.defineWayBits(index, shift); reverseSpeedEncoder = new EncodedDoubleValue("Reverse Speed", shift, speedBits, speedFactor, getHighwaySpeed("cycleway"), maxPossibleSpeed); @@ -71,14 +65,12 @@ public int defineWayBits( int index, int shift ) } @Override - public double getReverseSpeed( long flags ) - { + public double getReverseSpeed(long flags) { return reverseSpeedEncoder.getDoubleValue(flags); } @Override - public long setReverseSpeed( long flags, double speed ) - { + public long setReverseSpeed(long flags, double speed) { if (speed < 0) throw new IllegalArgumentException("Speed cannot be negative: " + speed + ", flags:" + BitUtil.LITTLE.toBitString(flags)); @@ -92,8 +84,7 @@ public long setReverseSpeed( long flags, double speed ) } @Override - public long handleSpeed( ReaderWay way, double speed, long flags ) - { + public long handleSpeed(ReaderWay way, double speed, long flags) { // handle oneways flags = super.handleSpeed(way, speed, flags); if (isBackward(flags)) @@ -106,8 +97,7 @@ public long handleSpeed( ReaderWay way, double speed, long flags ) } @Override - protected long setLowSpeed( long flags, double speed, boolean reverse ) - { + protected long setLowSpeed(long flags, double speed, boolean reverse) { if (reverse) return setBool(reverseSpeedEncoder.setDoubleValue(flags, 0), K_BACKWARD, false); @@ -115,8 +105,7 @@ protected long setLowSpeed( long flags, double speed, boolean reverse ) } @Override - public long flagsDefault( boolean forward, boolean backward ) - { + public long flagsDefault(boolean forward, boolean backward) { long flags = super.flagsDefault(forward, backward); if (backward) return reverseSpeedEncoder.setDefaultValue(flags); @@ -125,8 +114,7 @@ public long flagsDefault( boolean forward, boolean backward ) } @Override - public long setProperties( double speed, boolean forward, boolean backward ) - { + public long setProperties(double speed, boolean forward, boolean backward) { long flags = super.setProperties(speed, forward, backward); if (backward) return setReverseSpeed(flags, speed); @@ -135,8 +123,7 @@ public long setProperties( double speed, boolean forward, boolean backward ) } @Override - public long reverseFlags( long flags ) - { + public long reverseFlags(long flags) { // swap access flags = super.reverseFlags(flags); @@ -147,20 +134,17 @@ public long reverseFlags( long flags ) } @Override - public void applyWayTags( ReaderWay way, EdgeIteratorState edge ) - { + public void applyWayTags(ReaderWay way, EdgeIteratorState edge) { PointList pl = edge.fetchWayGeometry(3); if (!pl.is3D()) throw new IllegalStateException("To support speed calculation based on elevation data it is necessary to enable import of it."); long flags = edge.getFlags(); - if (way.hasTag("tunnel", "yes") || way.hasTag("bridge", "yes") || way.hasTag("highway", "steps")) - { + if (way.hasTag("tunnel", "yes") || way.hasTag("bridge", "yes") || way.hasTag("highway", "steps")) { // do not change speed // note: although tunnel can have a difference in elevation it is very unlikely that the elevation data is correct for a tunnel - } else - { + } else { // Decrease the speed for ele increase (incline), and decrease the speed for ele decrease (decline). The speed-decrease // has to be bigger (compared to the speed-increase) for the same elevation difference to simulate loosing energy and avoiding hills. // For the reverse speed this has to be the opposite but again keeping in mind that up+down difference. @@ -178,12 +162,10 @@ public void applyWayTags( ReaderWay way, EdgeIteratorState edge ) return; double eleDelta = pl.getElevation(pl.size() - 1) - prevEle; - if (eleDelta > 0.1) - { + if (eleDelta > 0.1) { incEleSum = eleDelta; incDist2DSum = fullDist2D; - } else if (eleDelta < -0.1) - { + } else if (eleDelta < -0.1) { decEleSum = -eleDelta; decDist2DSum = fullDist2D; } @@ -217,8 +199,7 @@ public void applyWayTags( ReaderWay way, EdgeIteratorState edge ) double fwdDecline = decDist2DSum > 1 ? decEleSum / decDist2DSum : 0; double restDist2D = fullDist2D - incDist2DSum - decDist2DSum; double maxSpeed = getHighwaySpeed("cycleway"); - if (isForward(flags)) - { + if (isForward(flags)) { // use weighted mean so that longer incline influences speed more than shorter double speed = getSpeed(flags); double fwdFaster = 1 + 2 * keepIn(fwdDecline, 0, 0.2); @@ -229,8 +210,7 @@ public void applyWayTags( ReaderWay way, EdgeIteratorState edge ) flags = this.setSpeed(flags, keepIn(speed, PUSHING_SECTION_SPEED / 2, maxSpeed)); } - if (isBackward(flags)) - { + if (isBackward(flags)) { double speedReverse = getReverseSpeed(flags); double bwFaster = 1 + 2 * keepIn(fwdIncline, 0, 0.2); bwFaster = bwFaster * bwFaster; @@ -244,8 +224,7 @@ public void applyWayTags( ReaderWay way, EdgeIteratorState edge ) } @Override - public String toString() - { + public String toString() { return "bike2"; } } diff --git a/core/src/main/java/com/graphhopper/routing/util/BikeCommonFlagEncoder.java b/core/src/main/java/com/graphhopper/routing/util/BikeCommonFlagEncoder.java index 11c0b5823fc..25f77ff9757 100644 --- a/core/src/main/java/com/graphhopper/routing/util/BikeCommonFlagEncoder.java +++ b/core/src/main/java/com/graphhopper/routing/util/BikeCommonFlagEncoder.java @@ -17,11 +17,9 @@ */ package com.graphhopper.routing.util; -import com.graphhopper.routing.weighting.PriorityWeighting; import com.graphhopper.reader.ReaderRelation; import com.graphhopper.reader.ReaderWay; -import com.graphhopper.reader.osm.conditional.ConditionalOSMTagInspector; -import com.graphhopper.reader.osm.conditional.DateRangeParser; +import com.graphhopper.routing.weighting.PriorityWeighting; import com.graphhopper.util.Helper; import com.graphhopper.util.InstructionAnnotation; import com.graphhopper.util.Translation; @@ -33,18 +31,17 @@ /** * Defines bit layout of bicycles (not motorcycles) for speed, access and relations (network). *

+ * * @author Peter Karich * @author Nop * @author ratrun */ -public class BikeCommonFlagEncoder extends AbstractFlagEncoder -{ +public class BikeCommonFlagEncoder extends AbstractFlagEncoder { /** * Reports whether this edge is unpaved. */ public static final int K_UNPAVED = 100; protected static final int PUSHING_SECTION_SPEED = 4; - private long unpavedBit = 0; // Pushing section heighways are parts where you need to get off your bike and push it (German: Schiebestrecke) protected final HashSet pushingSectionsHighways = new HashSet(); protected final HashSet oppositeLanes = new HashSet(); @@ -58,17 +55,16 @@ public class BikeCommonFlagEncoder extends AbstractFlagEncoder // convert network tag of bicycle routes into a way route code private final Map bikeNetworkToCode = new HashMap(); protected EncodedValue relationCodeEncoder; - private EncodedValue wayTypeEncoder; EncodedValue priorityWayEncoder; - + private long unpavedBit = 0; + private EncodedValue wayTypeEncoder; // Car speed limit which switches the preference from UNCHANGED to AVOID_IF_POSSIBLE private int avoidSpeedLimit; // This is the specific bicycle class private String classBicycleKey; - protected BikeCommonFlagEncoder( int speedBits, double speedFactor, int maxTurnCosts ) - { + protected BikeCommonFlagEncoder(int speedBits, double speedFactor, int maxTurnCosts) { super(speedBits, speedFactor, maxTurnCosts); // strict set, usually vehicle and agricultural/forestry are ignored by cyclists restrictions.addAll(Arrays.asList("bicycle", "vehicle", "access")); @@ -198,18 +194,16 @@ protected BikeCommonFlagEncoder( int speedBits, double speedFactor, int maxTurnC setCyclingNetworkPreference("deprecated", AVOID_AT_ALL_COSTS.getValue()); - setAvoidSpeedLimit(71); + setAvoidSpeedLimit(71); } @Override - public int getVersion() - { + public int getVersion() { return 1; } @Override - public int defineWayBits( int index, int shift ) - { + public int defineWayBits(int index, int shift) { // first two bits are reserved for route handling in superclass shift = super.defineWayBits(index, shift); speedEncoder = new EncodedDoubleValue("Speed", shift, speedBits, speedFactor, highwaySpeeds.get("cycleway"), @@ -228,20 +222,16 @@ public int defineWayBits( int index, int shift ) } @Override - public int defineRelationBits( int index, int shift ) - { + public int defineRelationBits(int index, int shift) { relationCodeEncoder = new EncodedValue("RelationCode", shift, 3, 1, 0, 7); return shift + relationCodeEncoder.getBits(); } @Override - public long acceptWay( ReaderWay way ) - { + public long acceptWay(ReaderWay way) { String highwayValue = way.getTag("highway"); - if (highwayValue == null) - { - if (way.hasTag("route", ferries)) - { + if (highwayValue == null) { + if (way.hasTag("route", ferries)) { // if bike is NOT explicitly tagged allow bike but only if foot is not specified String bikeTag = way.getTag("bicycle"); if (bikeTag == null && !way.hasTag("foot") || "yes".equals(bikeTag)) @@ -278,8 +268,7 @@ public long acceptWay( ReaderWay way ) return 0; String sacScale = way.getTag("sac_scale"); - if (sacScale != null) - { + if (sacScale != null) { if ((way.hasTag("highway", "cycleway")) && (way.hasTag("sac_scale", "hiking"))) return acceptBit; @@ -293,23 +282,19 @@ public long acceptWay( ReaderWay way ) return acceptBit; } - boolean isSacScaleAllowed( String sacScale ) - { + boolean isSacScaleAllowed(String sacScale) { // other scales are nearly impossible by an ordinary bike, see http://wiki.openstreetmap.org/wiki/Key:sac_scale return "hiking".equals(sacScale); } @Override - public long handleRelationTags( ReaderRelation relation, long oldRelationFlags ) - { + public long handleRelationTags(ReaderRelation relation, long oldRelationFlags) { int code = 0; - if (relation.hasTag("route", "bicycle")) - { + if (relation.hasTag("route", "bicycle")) { Integer val = bikeNetworkToCode.get(relation.getTag("network")); if (val != null) code = val; - } else if (relation.hasTag("route", "ferry")) - { + } else if (relation.hasTag("route", "ferry")) { code = AVOID_IF_POSSIBLE.getValue(); } @@ -324,19 +309,17 @@ public long handleRelationTags( ReaderRelation relation, long oldRelationFlags ) * we can reach the maxspeed for bicycles in case that the road type speed is higher and not * just only 90%. *

- * @param way: needed to retrieve tags + * + * @param way: needed to retrieve tags * @param speed: speed guessed e.g. from the road type or other tags * @return The assumed avererage speed. */ @Override - protected double applyMaxSpeed( ReaderWay way, double speed ) - { + protected double applyMaxSpeed(ReaderWay way, double speed) { double maxSpeed = getMaxSpeed(way); - if (maxSpeed >= 0) - { + if (maxSpeed >= 0) { // We strictly obay speed limits, see #600 - if (maxSpeed < speed) - { + if (maxSpeed < speed) { return maxSpeed; } } @@ -344,27 +327,23 @@ protected double applyMaxSpeed( ReaderWay way, double speed ) } @Override - public long handleWayTags( ReaderWay way, long allowed, long relationFlags ) - { + public long handleWayTags(ReaderWay way, long allowed, long relationFlags) { if (!isAccept(allowed)) return 0; long flags = 0; double wayTypeSpeed = getSpeed(way); - if (!isFerry(allowed)) - { + if (!isFerry(allowed)) { wayTypeSpeed = applyMaxSpeed(way, wayTypeSpeed); flags = handleSpeed(way, wayTypeSpeed, flags); flags = handleBikeRelated(way, flags, relationFlags > UNCHANGED.getValue()); boolean isRoundabout = way.hasTag("junction", "roundabout"); - if (isRoundabout) - { + if (isRoundabout) { flags = setBool(flags, K_ROUNDABOUT, true); } - } else - { + } else { double ferrySpeed = getFerrySpeed(way, highwaySpeeds.get("living_street"), highwaySpeeds.get("track"), @@ -380,22 +359,18 @@ public long handleWayTags( ReaderWay way, long allowed, long relationFlags ) return flags; } - int getSpeed( ReaderWay way ) - { + int getSpeed(ReaderWay way) { int speed = PUSHING_SECTION_SPEED; String highwayTag = way.getTag("highway"); Integer highwaySpeed = highwaySpeeds.get(highwayTag); String s = way.getTag("surface"); - if (!Helper.isEmpty(s)) - { + if (!Helper.isEmpty(s)) { Integer surfaceSpeed = surfaceSpeeds.get(s); - if (surfaceSpeed != null) - { + if (surfaceSpeed != null) { speed = surfaceSpeed; // Boost handling for good surfaces - if (highwaySpeed != null && surfaceSpeed > highwaySpeed) - { + if (highwaySpeed != null && surfaceSpeed > highwaySpeed) { // Avoid boosting if pushing section if (pushingSectionsHighways.contains(highwayTag)) speed = highwaySpeed; @@ -403,16 +378,13 @@ int getSpeed( ReaderWay way ) speed = surfaceSpeed; } } - } else - { + } else { String tt = way.getTag("tracktype"); - if (!Helper.isEmpty(tt)) - { + if (!Helper.isEmpty(tt)) { Integer tInt = trackTypeSpeeds.get(tt); if (tInt != null) speed = tInt; - } else if (highwaySpeed != null) - { + } else if (highwaySpeed != null) { if (!way.hasTag("service")) speed = highwaySpeed; else @@ -423,10 +395,8 @@ int getSpeed( ReaderWay way ) // Until now we assumed that the way is no pushing section // Now we check that, but only in case that our speed is bigger compared to the PUSHING_SECTION_SPEED if (speed > PUSHING_SECTION_SPEED - && (way.hasTag("highway", pushingSectionsHighways) || way.hasTag("bicycle", "dismount"))) - { - if (!way.hasTag("bicycle", intendedValues)) - { + && (way.hasTag("highway", pushingSectionsHighways) || way.hasTag("bicycle", "dismount"))) { + if (!way.hasTag("bicycle", intendedValues)) { if (way.hasTag("highway", "steps")) speed = PUSHING_SECTION_SPEED / 2; else @@ -441,8 +411,7 @@ int getSpeed( ReaderWay way ) } @Override - public InstructionAnnotation getAnnotation( long flags, Translation tr ) - { + public InstructionAnnotation getAnnotation(long flags, Translation tr) { int paveType = 0; // paved if (isBool(flags, K_UNPAVED)) paveType = 1; // unpaved @@ -452,15 +421,13 @@ public InstructionAnnotation getAnnotation( long flags, Translation tr ) return new InstructionAnnotation(0, wayName); } - String getWayName( int pavementType, int wayType, Translation tr ) - { + String getWayName(int pavementType, int wayType, Translation tr) { String pavementName = ""; if (pavementType == 1) pavementName = tr.tr("unpaved"); String wayTypeName = ""; - switch (wayType) - { + switch (wayType) { case 0: wayTypeName = ""; break; @@ -475,8 +442,7 @@ String getWayName( int pavementType, int wayType, Translation tr ) break; } - if (pavementName.isEmpty()) - { + if (pavementName.isEmpty()) { if (wayType == 0 || wayType == 3) return ""; return wayTypeName; @@ -490,10 +456,10 @@ String getWayName( int pavementType, int wayType, Translation tr ) * In this method we prefer cycleways or roads with designated bike access and avoid big roads * or roads with trams or pedestrian. *

+ * * @return new priority based on priorityFromRelation and on the tags in ReaderWay. */ - protected int handlePriority( ReaderWay way, double wayTypeSpeed, int priorityFromRelation ) - { + protected int handlePriority(ReaderWay way, double wayTypeSpeed, int priorityFromRelation) { TreeMap weightToPrioMap = new TreeMap(); if (priorityFromRelation == 0) weightToPrioMap.put(0d, UNCHANGED.getValue()); @@ -507,19 +473,15 @@ protected int handlePriority( ReaderWay way, double wayTypeSpeed, int priorityFr } // Conversion of class value to priority. See http://wiki.openstreetmap.org/wiki/Class:bicycle - private PriorityCode convertClassValueToPriority( String tagvalue ) - { + private PriorityCode convertClassValueToPriority(String tagvalue) { int classvalue; - try - { + try { classvalue = Integer.parseInt(tagvalue); - } catch (NumberFormatException e) - { + } catch (NumberFormatException e) { return UNCHANGED; } - switch (classvalue) - { + switch (classvalue) { case 3: return BEST; case 2: @@ -541,14 +503,12 @@ private PriorityCode convertClassValueToPriority( String tagvalue ) /** * @param weightToPrioMap associate a weight with every priority. This sorted map allows - * subclasses to 'insert' more important priorities as well as overwrite determined priorities. + * subclasses to 'insert' more important priorities as well as overwrite determined priorities. */ - void collect( ReaderWay way, double wayTypeSpeed, TreeMap weightToPrioMap ) - { + void collect(ReaderWay way, double wayTypeSpeed, TreeMap weightToPrioMap) { String service = way.getTag("service"); String highway = way.getTag("highway"); - if (way.hasTag("bicycle", "designated") || way.hasTag("bicycle", "official")) - { + if (way.hasTag("bicycle", "designated") || way.hasTag("bicycle", "official")) { if ("path".equals(highway)) weightToPrioMap.put(100d, VERY_NICE.getValue()); else @@ -559,17 +519,14 @@ void collect( ReaderWay way, double wayTypeSpeed, TreeMap weigh weightToPrioMap.put(100d, VERY_NICE.getValue()); double maxSpeed = getMaxSpeed(way); - if (preferHighwayTags.contains(highway) || maxSpeed > 0 && maxSpeed <= 30) - { - if (maxSpeed < avoidSpeedLimit) - { + if (preferHighwayTags.contains(highway) || maxSpeed > 0 && maxSpeed <= 30) { + if (maxSpeed < avoidSpeedLimit) { weightToPrioMap.put(40d, PREFER.getValue()); if (way.hasTag("tunnel", intendedValues)) weightToPrioMap.put(40d, UNCHANGED.getValue()); } } else if (avoidHighwayTags.contains(highway) - || maxSpeed >= avoidSpeedLimit && !"track".equals(highway)) - { + || maxSpeed >= avoidSpeedLimit && !"track".equals(highway)) { weightToPrioMap.put(50d, REACH_DEST.getValue()); if (way.hasTag("tunnel", intendedValues)) weightToPrioMap.put(50d, AVOID_AT_ALL_COSTS.getValue()); @@ -577,8 +534,7 @@ void collect( ReaderWay way, double wayTypeSpeed, TreeMap weigh if (pushingSectionsHighways.contains(highway) || way.hasTag("bicycle", "use_sidepath") - || "parking_aisle".equals(service)) - { + || "parking_aisle".equals(service)) { if (way.hasTag("bicycle", "yes")) weightToPrioMap.put(100d, UNCHANGED.getValue()); else @@ -589,20 +545,17 @@ void collect( ReaderWay way, double wayTypeSpeed, TreeMap weigh weightToPrioMap.put(50d, AVOID_AT_ALL_COSTS.getValue()); String classBicycleValue = way.getTag(classBicycleKey); - if (classBicycleValue != null) - { + if (classBicycleValue != null) { // We assume that humans are better in classifying preferences compared to our algorithm above -> weight = 100 weightToPrioMap.put(100d, convertClassValueToPriority(classBicycleValue).getValue()); - } else - { + } else { String classBicycle = way.getTag("class:bicycle"); if (classBicycle != null) weightToPrioMap.put(100d, convertClassValueToPriority(classBicycle).getValue()); } // Increase the priority for scenic routes or in case that maxspeed limits our average speed as compensation. See #630 - if (way.hasTag("scenic", "yes") || ((maxSpeed > 0) && (maxSpeed < wayTypeSpeed))) - { + if (way.hasTag("scenic", "yes") || ((maxSpeed > 0) && (maxSpeed < wayTypeSpeed))) { if (weightToPrioMap.lastEntry().getValue() < BEST.getValue()) // Increase the prio by one step weightToPrioMap.put(110d, weightToPrioMap.lastEntry().getValue() + 1); @@ -612,8 +565,7 @@ void collect( ReaderWay way, double wayTypeSpeed, TreeMap weigh /** * Handle surface and wayType encoding */ - long handleBikeRelated( ReaderWay way, long encoded, boolean partOfCycleRelation ) - { + long handleBikeRelated(ReaderWay way, long encoded, boolean partOfCycleRelation) { String surfaceTag = way.getTag("surface"); String highway = way.getTag("highway"); String trackType = way.getTag("tracktype"); @@ -621,8 +573,7 @@ long handleBikeRelated( ReaderWay way, long encoded, boolean partOfCycleRelation // Populate unpavedBit if ("track".equals(highway) && (trackType == null || !"grade1".equals(trackType)) || "path".equals(highway) && surfaceTag == null - || unpavedSurfaceTags.contains(surfaceTag)) - { + || unpavedSurfaceTags.contains(surfaceTag)) { encoded = setBool(encoded, K_UNPAVED, true); } @@ -636,8 +587,7 @@ long handleBikeRelated( ReaderWay way, long encoded, boolean partOfCycleRelation if (isPushingSection && !partOfCycleRelation || "steps".equals(highway)) wayType = WayType.PUSHING_SECTION; - if (way.hasTag("bicycle", intendedValues)) - { + if (way.hasTag("bicycle", intendedValues)) { if (isPushingSection && !way.hasTag("bicycle", "designated")) wayType = WayType.OTHER_SMALL_WAY; else if (wayType == WayType.OTHER_SMALL_WAY || wayType == WayType.PUSHING_SECTION) @@ -649,10 +599,8 @@ else if (wayType == WayType.OTHER_SMALL_WAY || wayType == WayType.PUSHING_SECTIO } @Override - public long setBool( long flags, int key, boolean value ) - { - switch (key) - { + public long setBool(long flags, int key, boolean value) { + switch (key) { case K_UNPAVED: return value ? flags | unpavedBit : flags & ~unpavedBit; default: @@ -661,10 +609,8 @@ public long setBool( long flags, int key, boolean value ) } @Override - public boolean isBool( long flags, int key ) - { - switch (key) - { + public boolean isBool(long flags, int key) { + switch (key) { case K_UNPAVED: return (flags & unpavedBit) != 0; default: @@ -673,10 +619,8 @@ public boolean isBool( long flags, int key ) } @Override - public double getDouble( long flags, int key ) - { - switch (key) - { + public double getDouble(long flags, int key) { + switch (key) { case PriorityWeighting.KEY: return (double) priorityWayEncoder.getValue(flags) / BEST.getValue(); default: @@ -684,13 +628,11 @@ public double getDouble( long flags, int key ) } } - boolean isPushingSection( ReaderWay way ) - { + boolean isPushingSection(ReaderWay way) { return way.hasTag("highway", pushingSectionsHighways) || way.hasTag("railway", "platform") || way.hasTag("bicycle", "dismount"); } - protected long handleSpeed( ReaderWay way, double speed, long encoded ) - { + protected long handleSpeed(ReaderWay way, double speed, long encoded) { encoded = setSpeed(encoded, speed); // handle oneways @@ -703,8 +645,7 @@ protected long handleSpeed( ReaderWay way, double speed, long encoded ) if ((isOneway || way.hasTag("junction", "roundabout")) && !way.hasTag("oneway:bicycle", "no") && !way.hasTag("bicycle:backward") - && !way.hasTag("cycleway", oppositeLanes)) - { + && !way.hasTag("cycleway", oppositeLanes)) { boolean isBackward = way.hasTag("oneway", "-1") || way.hasTag("oneway:bicycle", "-1") || way.hasTag("vehicle:forward", "no") @@ -714,79 +655,66 @@ protected long handleSpeed( ReaderWay way, double speed, long encoded ) else encoded |= forwardBit; - } else - { + } else { encoded |= directionBitMask; } return encoded; } - private enum WayType - { - ROAD(0), - PUSHING_SECTION(1), - CYCLEWAY(2), - OTHER_SMALL_WAY(3); - - private final int value; - - private WayType( int value ) - { - this.value = value; - } - - public int getValue() - { - return value; - } - } - - protected void setHighwaySpeed( String highway, int speed ) - { + protected void setHighwaySpeed(String highway, int speed) { highwaySpeeds.put(highway, speed); } - protected int getHighwaySpeed( String key ) - { + protected int getHighwaySpeed(String key) { return highwaySpeeds.get(key); } - void setTrackTypeSpeed( String tracktype, int speed ) - { + void setTrackTypeSpeed(String tracktype, int speed) { trackTypeSpeeds.put(tracktype, speed); } - void setSurfaceSpeed( String surface, int speed ) - { + void setSurfaceSpeed(String surface, int speed) { surfaceSpeeds.put(surface, speed); } - void setCyclingNetworkPreference( String network, int code ) - { + void setCyclingNetworkPreference(String network, int code) { bikeNetworkToCode.put(network, code); } - void addPushingSection( String highway ) - { + void addPushingSection(String highway) { pushingSectionsHighways.add(highway); } @Override - public boolean supports( Class feature ) - { + public boolean supports(Class feature) { if (super.supports(feature)) return true; return PriorityWeighting.class.isAssignableFrom(feature); } - public void setAvoidSpeedLimit( int limit ) - { + public void setAvoidSpeedLimit(int limit) { avoidSpeedLimit = limit; } - protected void setSpecificClassBicycle( String subkey ) - { + protected void setSpecificClassBicycle(String subkey) { classBicycleKey = "class:bicycle:" + subkey; } + + private enum WayType { + ROAD(0), + PUSHING_SECTION(1), + CYCLEWAY(2), + OTHER_SMALL_WAY(3); + + private final int value; + + private WayType(int value) { + this.value = value; + } + + public int getValue() { + return value; + } + } } diff --git a/core/src/main/java/com/graphhopper/routing/util/BikeFlagEncoder.java b/core/src/main/java/com/graphhopper/routing/util/BikeFlagEncoder.java index f3e2ad8133d..34debf83649 100644 --- a/core/src/main/java/com/graphhopper/routing/util/BikeFlagEncoder.java +++ b/core/src/main/java/com/graphhopper/routing/util/BikeFlagEncoder.java @@ -23,23 +23,20 @@ /** * Specifies the settings for cycletouring/trekking *

+ * * @author ratrun * @author Peter Karich */ -public class BikeFlagEncoder extends BikeCommonFlagEncoder -{ - public BikeFlagEncoder() - { +public class BikeFlagEncoder extends BikeCommonFlagEncoder { + public BikeFlagEncoder() { this(4, 2, 0); } - public BikeFlagEncoder( String propertiesString ) - { + public BikeFlagEncoder(String propertiesString) { this(new PMap(propertiesString)); } - public BikeFlagEncoder( PMap properties ) - { + public BikeFlagEncoder(PMap properties) { this((int) properties.getLong("speed_bits", 4), properties.getLong("speed_factor", 2), properties.getBool("turn_costs", false) ? 1 : 0); @@ -47,8 +44,7 @@ public BikeFlagEncoder( PMap properties ) this.setBlockFords(properties.getBool("block_fords", true)); } - public BikeFlagEncoder( int speedBits, double speedFactor, int maxTurnCosts ) - { + public BikeFlagEncoder(int speedBits, double speedFactor, int maxTurnCosts) { super(speedBits, speedFactor, maxTurnCosts); addPushingSection("path"); addPushingSection("footway"); @@ -71,27 +67,24 @@ public BikeFlagEncoder( int speedBits, double speedFactor, int maxTurnCosts ) absoluteBarriers.add("kissing_gate"); setSpecificClassBicycle("touring"); - + init(); } @Override - public int getVersion() - { + public int getVersion() { return 1; } @Override - boolean isPushingSection( ReaderWay way ) - { + boolean isPushingSection(ReaderWay way) { String highway = way.getTag("highway"); String trackType = way.getTag("tracktype"); return super.isPushingSection(way) || "track".equals(highway) && trackType != null && !"grade1".equals(trackType); } @Override - public String toString() - { + public String toString() { return "bike"; } } diff --git a/core/src/main/java/com/graphhopper/routing/util/CarFlagEncoder.java b/core/src/main/java/com/graphhopper/routing/util/CarFlagEncoder.java index d3319bfcaea..d9e4a7722cb 100644 --- a/core/src/main/java/com/graphhopper/routing/util/CarFlagEncoder.java +++ b/core/src/main/java/com/graphhopper/routing/util/CarFlagEncoder.java @@ -27,11 +27,11 @@ /** * Defines bit layout for cars. (speed, access, ferries, ...) *

+ * * @author Peter Karich * @author Nop */ -public class CarFlagEncoder extends AbstractFlagEncoder -{ +public class CarFlagEncoder extends AbstractFlagEncoder { protected final Map trackTypeSpeedMap = new HashMap(); protected final Set badSurfaceSpeedMap = new HashSet(); /** @@ -41,13 +41,11 @@ public class CarFlagEncoder extends AbstractFlagEncoder */ protected final Map defaultSpeedMap = new HashMap(); - public CarFlagEncoder() - { + public CarFlagEncoder() { this(5, 5, 0); } - public CarFlagEncoder( PMap properties ) - { + public CarFlagEncoder(PMap properties) { this((int) properties.getLong("speed_bits", 5), properties.getDouble("speed_factor", 5), properties.getBool("turn_costs", false) ? 1 : 0); @@ -55,13 +53,11 @@ public CarFlagEncoder( PMap properties ) this.setBlockFords(properties.getBool("block_fords", true)); } - public CarFlagEncoder( String propertiesStr ) - { + public CarFlagEncoder(String propertiesStr) { this(new PMap(propertiesStr)); } - public CarFlagEncoder( int speedBits, double speedFactor, int maxTurnCosts ) - { + public CarFlagEncoder(int speedBits, double speedFactor, int maxTurnCosts) { super(speedBits, speedFactor, maxTurnCosts); restrictions.addAll(Arrays.asList("motorcar", "motor_vehicle", "vehicle", "access")); restrictedValues.add("private"); @@ -132,13 +128,12 @@ public CarFlagEncoder( int speedBits, double speedFactor, int maxTurnCosts ) defaultSpeedMap.put("road", 20); // forestry stuff defaultSpeedMap.put("track", 15); - + init(); } @Override - public int getVersion() - { + public int getVersion() { return 1; } @@ -146,8 +141,7 @@ public int getVersion() * Define the place of the speedBits in the edge flags for car. */ @Override - public int defineWayBits( int index, int shift ) - { + public int defineWayBits(int index, int shift) { // first two bits are reserved for route handling in superclass shift = super.defineWayBits(index, shift); speedEncoder = new EncodedDoubleValue("Speed", shift, speedBits, speedFactor, defaultSpeedMap.get("secondary"), @@ -155,18 +149,15 @@ public int defineWayBits( int index, int shift ) return shift + speedEncoder.getBits(); } - protected double getSpeed( ReaderWay way ) - { + protected double getSpeed(ReaderWay way) { String highwayValue = way.getTag("highway"); Integer speed = defaultSpeedMap.get(highwayValue); if (speed == null) throw new IllegalStateException(toString() + ", no speed found for: " + highwayValue + ", tags: " + way); - if (highwayValue.equals("track")) - { + if (highwayValue.equals("track")) { String tt = way.getTag("tracktype"); - if (!Helper.isEmpty(tt)) - { + if (!Helper.isEmpty(tt)) { Integer tInt = trackTypeSpeedMap.get(tt); if (tInt != null) speed = tInt; @@ -177,14 +168,11 @@ protected double getSpeed( ReaderWay way ) } @Override - public long acceptWay( ReaderWay way ) - { + public long acceptWay(ReaderWay way) { // TODO: Ferries have conditionals, like opening hours or are closed during some time in the year String highwayValue = way.getTag("highway"); - if (highwayValue == null) - { - if (way.hasTag("route", ferries)) - { + if (highwayValue == null) { + if (way.hasTag("route", ferries)) { String motorcarTag = way.getTag("motorcar"); if (motorcarTag == null) motorcarTag = way.getTag("motor_vehicle"); @@ -195,8 +183,7 @@ public long acceptWay( ReaderWay way ) return 0; } - if ("track".equals(highwayValue)) - { + if ("track".equals(highwayValue)) { String tt = way.getTag("tracktype"); if (tt != null && !tt.equals("grade1") && !tt.equals("grade2") && !tt.equals("grade3")) return 0; @@ -210,8 +197,7 @@ public long acceptWay( ReaderWay way ) // multiple restrictions needs special handling compared to foot and bike, see also motorcycle String firstValue = way.getFirstPriorityTag(restrictions); - if (!firstValue.isEmpty()) - { + if (!firstValue.isEmpty()) { if (restrictedValues.contains(firstValue) && !getConditionalTagInspector().isRestrictedWayConditionallyPermitted(way)) return 0; if (intendedValues.contains(firstValue)) @@ -229,20 +215,17 @@ public long acceptWay( ReaderWay way ) } @Override - public long handleRelationTags( ReaderRelation relation, long oldRelationFlags ) - { + public long handleRelationTags(ReaderRelation relation, long oldRelationFlags) { return oldRelationFlags; } @Override - public long handleWayTags( ReaderWay way, long allowed, long relationFlags ) - { + public long handleWayTags(ReaderWay way, long allowed, long relationFlags) { if (!isAccept(allowed)) return 0; long flags = 0; - if (!isFerry(allowed)) - { + if (!isFerry(allowed)) { // get assumed speed from highway type double speed = getSpeed(way); speed = applyMaxSpeed(way, speed); @@ -257,8 +240,7 @@ public long handleWayTags( ReaderWay way, long allowed, long relationFlags ) if (isRoundabout) flags = setBool(flags, K_ROUNDABOUT, true); - if (isOneway(way) || isRoundabout) - { + if (isOneway(way) || isRoundabout) { if (isBackwardOneway(way)) flags |= backwardBit; @@ -267,8 +249,7 @@ public long handleWayTags( ReaderWay way, long allowed, long relationFlags ) } else flags |= directionBitMask; - } else - { + } else { double ferrySpeed = getFerrySpeed(way, defaultSpeedMap.get("living_street"), defaultSpeedMap.get("service"), defaultSpeedMap.get("residential")); flags = setSpeed(flags, ferrySpeed); flags |= directionBitMask; @@ -280,8 +261,7 @@ public long handleWayTags( ReaderWay way, long allowed, long relationFlags ) /** * make sure that isOneway is called before */ - protected boolean isBackwardOneway( ReaderWay way ) - { + protected boolean isBackwardOneway(ReaderWay way) { return way.hasTag("oneway", "-1") || way.hasTag("vehicle:forward", "no") || way.hasTag("motor_vehicle:forward", "no"); @@ -290,15 +270,13 @@ protected boolean isBackwardOneway( ReaderWay way ) /** * make sure that isOneway is called before */ - protected boolean isForwardOneway( ReaderWay way ) - { + protected boolean isForwardOneway(ReaderWay way) { return !way.hasTag("oneway", "-1") && !way.hasTag("vehicle:forward", "no") && !way.hasTag("motor_vehicle:forward", "no"); } - protected boolean isOneway( ReaderWay way ) - { + protected boolean isOneway(ReaderWay way) { return way.hasTag("oneway", oneways) || way.hasTag("vehicle:backward") || way.hasTag("vehicle:forward") @@ -306,19 +284,15 @@ protected boolean isOneway( ReaderWay way ) || way.hasTag("motor_vehicle:forward"); } - public String getWayInfo( ReaderWay way ) - { + public String getWayInfo(ReaderWay way) { String str = ""; String highwayValue = way.getTag("highway"); // for now only motorway links - if ("motorway_link".equals(highwayValue)) - { + if ("motorway_link".equals(highwayValue)) { String destination = way.getTag("destination"); - if (!Helper.isEmpty(destination)) - { + if (!Helper.isEmpty(destination)) { int counter = 0; - for (String d : destination.split(";")) - { + for (String d : destination.split(";")) { if (d.trim().isEmpty()) continue; @@ -340,8 +314,7 @@ public String getWayInfo( ReaderWay way ) } @Override - public String toString() - { + public String toString() { return "car"; } } diff --git a/core/src/main/java/com/graphhopper/routing/util/DataFlagEncoder.java b/core/src/main/java/com/graphhopper/routing/util/DataFlagEncoder.java index 1d0b1c767de..b01fdd8d6f6 100644 --- a/core/src/main/java/com/graphhopper/routing/util/DataFlagEncoder.java +++ b/core/src/main/java/com/graphhopper/routing/util/DataFlagEncoder.java @@ -17,12 +17,13 @@ */ package com.graphhopper.routing.util; -import com.graphhopper.routing.weighting.GenericWeighting; import com.graphhopper.reader.ReaderRelation; import com.graphhopper.reader.ReaderWay; +import com.graphhopper.routing.weighting.GenericWeighting; import com.graphhopper.util.ConfigMap; import com.graphhopper.util.EdgeIteratorState; import com.graphhopper.util.PMap; + import java.util.*; import java.util.Map.Entry; @@ -35,28 +36,48 @@ * * @author Peter Karich */ -public class DataFlagEncoder extends AbstractFlagEncoder -{ +public class DataFlagEncoder extends AbstractFlagEncoder { + private static final Map DEFAULT_SPEEDS = new LinkedHashMap() { + { + put("motorway", 100d); + put("motorway_link", 70d); + put("motorroad", 90d); + put("trunk", 70d); + put("trunk_link", 65d); + put("primary", 65d); + put("primary_link", 60d); + put("secondary", 60d); + put("secondary_link", 50d); + put("tertiary", 50d); + put("tertiary_link", 40d); + put("unclassified", 30d); + put("residential", 30d); + put("living_street", 5d); + put("service", 20d); + put("road", 20d); + put("forestry", 15d); + put("track", 15d); + } + }; + private final Map surfaceMap = new HashMap<>(); + private final Map highwayMap = new HashMap<>(); + private final List transportModeList = new ArrayList<>(); + private final Map transportModeMap = new HashMap<>(); private long bit0; private EncodedDoubleValue carFwdMaxspeedEncoder; private EncodedDoubleValue carBwdMaxspeedEncoder; private EncodedValue surfaceEncoder; - private final Map surfaceMap = new HashMap<>(); private EncodedValue highwayEncoder; - private final Map highwayMap = new HashMap<>(); private EncodedValue transportModeEncoder; - private final List transportModeList = new ArrayList<>(); - private final Map transportModeMap = new HashMap<>(); - public DataFlagEncoder() - { + public DataFlagEncoder() { // TODO include turn information super(5, 5, 0); maxPossibleSpeed = 140; // // TODO restrictions (forestry, agricultural, emergency, destination, private, delivery, customers) - // + // // highway and certain tags like ferry and shuttle_train which can be used here (no logical overlap) List highwayList = Arrays.asList( @@ -69,8 +90,7 @@ public DataFlagEncoder() "cycleway", "steps", "path", "footway", "pedestrian", "ferry", "shuttle_train"); int counter = 0; - for (String hw : highwayList) - { + for (String hw : highwayList) { highwayMap.put(hw, counter++); } @@ -78,16 +98,14 @@ public DataFlagEncoder() // Also 'roundabout' needs a separate bit as a tunnel or a bridge can be a roundabout at the same time. transportModeList.addAll(Arrays.asList("_default", "bridge", "tunnel", "ford", "aerialway")); counter = 0; - for (String tm : transportModeList) - { + for (String tm : transportModeList) { transportModeMap.put(tm, counter++); } List surfaceList = Arrays.asList("_default", "asphalt", "unpaved", "paved", "gravel", "ground", "dirt", "grass", "concrete", "paving_stones", "sand", "compacted", "cobblestone", "mud", "ice"); counter = 0; - for (String s : surfaceList) - { + for (String s : surfaceList) { surfaceMap.put(s, counter++); } @@ -96,8 +114,7 @@ public DataFlagEncoder() } @Override - public int defineWayBits( int index, int shift ) - { + public int defineWayBits(int index, int shift) { // TODO use this approach in other flag encoders too then we can do a global swap for all and bit0 can be at position 0! bit0 = 1L << shift; shift++; @@ -124,33 +141,27 @@ public int defineWayBits( int index, int shift ) } @Override - public long handleRelationTags( ReaderRelation relation, long oldRelationFlags ) - { + public long handleRelationTags(ReaderRelation relation, long oldRelationFlags) { return 0; } @Override - public long acceptWay( ReaderWay way ) - { + public long acceptWay(ReaderWay way) { return 1; } @Override - public long handleWayTags( ReaderWay way, long allowed, long relationFlags ) - { - try - { + public long handleWayTags(ReaderWay way, long allowed, long relationFlags) { + try { // HIGHWAY String highwayValue = way.getTag("highway"); Integer hwValue = highwayMap.get(highwayValue); if (way.hasTag("impassable", "yes") || way.hasTag("status", "impassable")) hwValue = 0; - if (hwValue == null) - { + if (hwValue == null) { hwValue = 0; - if (way.hasTag("route", ferries)) - { + if (way.hasTag("route", ferries)) { String motorcarTag = way.getTag("motorcar"); if (motorcarTag == null) motorcarTag = way.getTag("motor_vehicle"); @@ -196,10 +207,8 @@ public long handleWayTags( ReaderWay way, long allowed, long relationFlags ) // TRANSPORT MODE int tmValue = 0; - for (String tm : transportModeList) - { - if (way.hasTag(tm)) - { + for (String tm : transportModeList) { + if (way.hasTag(tm)) { tmValue = transportModeMap.get(tm); break; } @@ -218,8 +227,7 @@ public long handleWayTags( ReaderWay way, long allowed, long relationFlags ) || way.hasTag("motor_vehicle:backward") || way.hasTag("motor_vehicle:forward"); - if (isOneway || isRoundabout) - { + if (isOneway || isRoundabout) { boolean isBackward = way.hasTag("oneway", "-1") || way.hasTag("vehicle:forward", "no") || way.hasTag("motor_vehicle:forward", "no"); @@ -234,15 +242,13 @@ public long handleWayTags( ReaderWay way, long allowed, long relationFlags ) throw new IllegalStateException("bit0 has to be empty on creation"); return flags; - } catch (Exception ex) - { + } catch (Exception ex) { throw new RuntimeException("Error while parsing way " + way.toString(), ex); } } @Override - public long reverseFlags( long flags ) - { + public long reverseFlags(long flags) { // see #728 for an explanation return flags ^ bit0; @@ -261,32 +267,26 @@ public long reverseFlags( long flags ) * Interpret flags in forward direction if bit0 is empty. This method is used when accessing * direction dependent values and avoid reverse flags, see #728. */ - private boolean isBit0Empty( long flags ) - { + private boolean isBit0Empty(long flags) { return (flags & bit0) == 0; } - public int getHighway( EdgeIteratorState edge ) - { + public int getHighway(EdgeIteratorState edge) { return (int) highwayEncoder.getValue(edge.getFlags()); } - public String getHighwayAsString( EdgeIteratorState edge ) - { + public String getHighwayAsString(EdgeIteratorState edge) { int val = getHighway(edge); - for (Entry e : highwayMap.entrySet()) - { + for (Entry e : highwayMap.entrySet()) { if (e.getValue() == val) return e.getKey(); } return null; } - public double[] getHighwaySpeedMap( Map map ) - { + public double[] getHighwaySpeedMap(Map map) { double[] res = new double[highwayMap.size()]; - for (Entry e : map.entrySet()) - { + for (Entry e : map.entrySet()) { Integer integ = highwayMap.get(e.getKey()); if (integ == null) throw new IllegalArgumentException("Graph not prepared for highway=" + e.getKey()); @@ -299,43 +299,35 @@ public double[] getHighwaySpeedMap( Map map ) return res; } - public int getSurface( EdgeIteratorState edge ) - { + public int getSurface(EdgeIteratorState edge) { return (int) surfaceEncoder.getValue(edge.getFlags()); } - public String getSurfaceAsString( EdgeIteratorState edge ) - { + public String getSurfaceAsString(EdgeIteratorState edge) { int val = getSurface(edge); - for (Entry e : surfaceMap.entrySet()) - { + for (Entry e : surfaceMap.entrySet()) { if (e.getValue() == val) return e.getKey(); } return null; } - public int getTransportMode( EdgeIteratorState edge ) - { + public int getTransportMode(EdgeIteratorState edge) { return (int) transportModeEncoder.getValue(edge.getFlags()); } - public String getTransportModeAsString( EdgeIteratorState edge ) - { + public String getTransportModeAsString(EdgeIteratorState edge) { int val = getTransportMode(edge); - for (Entry e : transportModeMap.entrySet()) - { + for (Entry e : transportModeMap.entrySet()) { if (e.getValue() == val) return e.getKey(); } return null; } - public double[] getTransportModeMap( Map map ) - { + public double[] getTransportModeMap(Map map) { double[] res = new double[transportModeMap.size()]; - for (Entry e : map.entrySet()) - { + for (Entry e : map.entrySet()) { Integer integ = transportModeMap.get(e.getKey()); if (integ == null) throw new IllegalArgumentException("Graph not prepared for transport_mode=" + e.getKey()); @@ -348,20 +340,17 @@ public double[] getTransportModeMap( Map map ) return res; } - public boolean isRoundabout( EdgeIteratorState edge ) - { + public boolean isRoundabout(EdgeIteratorState edge) { // use direct call instead of isBool return (edge.getFlags() & roundaboutBit) != 0; } - public int getAccessType( String accessStr ) - { + public int getAccessType(String accessStr) { // access, motor_vehicle, bike, foot, hgv, bus return 0; } - public final boolean isForward( EdgeIteratorState edge, int accessType ) - { + public final boolean isForward(EdgeIteratorState edge, int accessType) { // TODO shift dependent on the accessType // use only one bit for foot? long flags = edge.getFlags(); @@ -369,27 +358,23 @@ public final boolean isForward( EdgeIteratorState edge, int accessType ) } @Override - public final boolean isForward( long flags ) - { + public final boolean isForward(long flags) { // TODO remove old method return (flags & (isBit0Empty(flags) ? forwardBit : backwardBit)) != 0; } - public final boolean isBackward( EdgeIteratorState edge, int accessType ) - { + public final boolean isBackward(EdgeIteratorState edge, int accessType) { long flags = edge.getFlags(); return (flags & (isBit0Empty(flags) ? backwardBit : forwardBit)) != 0; } @Override - public final boolean isBackward( long flags ) - { + public final boolean isBackward(long flags) { // TODO remove old method return (flags & (isBit0Empty(flags) ? backwardBit : forwardBit)) != 0; } - public double getMaxspeed( EdgeIteratorState edge, int accessType, boolean reverse ) - { + public double getMaxspeed(EdgeIteratorState edge, int accessType, boolean reverse) { long flags = edge.getFlags(); if (!isBit0Empty(flags)) reverse = !reverse; @@ -410,77 +395,65 @@ public double getMaxspeed( EdgeIteratorState edge, int accessType, boolean rever } @Override - public long flagsDefault( boolean forward, boolean backward ) - { + public long flagsDefault(boolean forward, boolean backward) { // just pick car mode to set access values? throw new RuntimeException("do not call flagsDefault"); } @Override - public long setAccess( long flags, boolean forward, boolean backward ) - { + public long setAccess(long flags, boolean forward, boolean backward) { // TODO in subnetwork we need to remove access for certain profiles or set of roads? return flags; } @Override - public long setSpeed( long flags, double speed ) - { + public long setSpeed(long flags, double speed) { throw new RuntimeException("do not call setSpeed"); } @Override - protected long setLowSpeed( long flags, double speed, boolean reverse ) - { + protected long setLowSpeed(long flags, double speed, boolean reverse) { throw new RuntimeException("do not call setLowSpeed"); } @Override - public double getSpeed( long flags ) - { + public double getSpeed(long flags) { // TODO fix Path.calcMillis(Path.java:255) // use pluggable weighting.calcMillis but include reverse somehow return 50; } @Override - public long setReverseSpeed( long flags, double speed ) - { + public long setReverseSpeed(long flags, double speed) { throw new RuntimeException("do not call setReverseSpeed"); } @Override - public double getReverseSpeed( long flags ) - { + public double getReverseSpeed(long flags) { throw new RuntimeException("do not call getReverseSpeed"); } @Override - public long setProperties( double speed, boolean forward, boolean backward ) - { + public long setProperties(double speed, boolean forward, boolean backward) { throw new RuntimeException("do not call setProperties"); } @Override - protected double getMaxSpeed( ReaderWay way ) - { + protected double getMaxSpeed(ReaderWay way) { throw new RuntimeException("do not call getMaxSpeed(ReaderWay)"); } @Override - public double getMaxSpeed() - { + public double getMaxSpeed() { throw new RuntimeException("do not call getMaxSpeed"); } - public double getMaxPossibleSpeed() - { + public double getMaxPossibleSpeed() { return maxPossibleSpeed; } @Override - public boolean supports( Class feature ) - { + public boolean supports(Class feature) { boolean ret = super.supports(feature); if (ret) return true; @@ -489,50 +462,22 @@ public boolean supports( Class feature ) } @Override - public int getVersion() - { + public int getVersion() { return 1; } @Override - public String toString() - { + public String toString() { return "generic"; } - private static final Map DEFAULT_SPEEDS = new LinkedHashMap() - { - { - put("motorway", 100d); - put("motorway_link", 70d); - put("motorroad", 90d); - put("trunk", 70d); - put("trunk_link", 65d); - put("primary", 65d); - put("primary_link", 60d); - put("secondary", 60d); - put("secondary_link", 50d); - put("tertiary", 50d); - put("tertiary_link", 40d); - put("unclassified", 30d); - put("residential", 30d); - put("living_street", 5d); - put("service", 20d); - put("road", 20d); - put("forestry", 15d); - put("track", 15d); - } - }; - /** * This method creates a Config map out of the PMap. Later on this conversion should not be * necessary when we read JSON. */ - public ConfigMap readStringMap( PMap weightingMap ) - { + public ConfigMap readStringMap(PMap weightingMap) { Map map = new HashMap<>(); - for (Entry e : DEFAULT_SPEEDS.entrySet()) - { + for (Entry e : DEFAULT_SPEEDS.entrySet()) { map.put(e.getKey(), weightingMap.getDouble("highways." + e.getKey(), e.getValue())); } diff --git a/core/src/main/java/com/graphhopper/routing/util/DefaultEdgeFilter.java b/core/src/main/java/com/graphhopper/routing/util/DefaultEdgeFilter.java index e93c563a464..5cf1c5fbd4f 100644 --- a/core/src/main/java/com/graphhopper/routing/util/DefaultEdgeFilter.java +++ b/core/src/main/java/com/graphhopper/routing/util/DefaultEdgeFilter.java @@ -22,8 +22,7 @@ /** * @author Peter Karich */ -public class DefaultEdgeFilter implements EdgeFilter -{ +public class DefaultEdgeFilter implements EdgeFilter { private final boolean bwd; private final boolean fwd; private FlagEncoder encoder; @@ -31,37 +30,31 @@ public class DefaultEdgeFilter implements EdgeFilter /** * Creates an edges filter which accepts both direction of the specified vehicle. */ - public DefaultEdgeFilter( FlagEncoder encoder ) - { + public DefaultEdgeFilter(FlagEncoder encoder) { this(encoder, true, true); } - public DefaultEdgeFilter( FlagEncoder encoder, boolean bwd, boolean fwd ) - { + public DefaultEdgeFilter(FlagEncoder encoder, boolean bwd, boolean fwd) { this.encoder = encoder; this.bwd = bwd; this.fwd = fwd; } @Override - public final boolean accept( EdgeIteratorState iter ) - { + public final boolean accept(EdgeIteratorState iter) { return fwd && iter.isForward(encoder) || bwd && iter.isBackward(encoder); } - public boolean acceptsBackward() - { + public boolean acceptsBackward() { return bwd; } - public boolean acceptsForward() - { + public boolean acceptsForward() { return fwd; } @Override - public String toString() - { + public String toString() { return encoder.toString() + ", bwd:" + bwd + ", fwd:" + fwd; } } diff --git a/core/src/main/java/com/graphhopper/routing/util/DefaultFlagEncoderFactory.java b/core/src/main/java/com/graphhopper/routing/util/DefaultFlagEncoderFactory.java index 6d54f4dec37..3455ea07d1b 100644 --- a/core/src/main/java/com/graphhopper/routing/util/DefaultFlagEncoderFactory.java +++ b/core/src/main/java/com/graphhopper/routing/util/DefaultFlagEncoderFactory.java @@ -24,14 +24,12 @@ * * @author Peter Karich */ -public class DefaultFlagEncoderFactory implements FlagEncoderFactory -{ +public class DefaultFlagEncoderFactory implements FlagEncoderFactory { @Override - public FlagEncoder createFlagEncoder( String name, PMap configuration ) - { + public FlagEncoder createFlagEncoder(String name, PMap configuration) { if (name.equals(GENERIC)) return new DataFlagEncoder(); - + else if (name.equals(CAR)) return new CarFlagEncoder(configuration); diff --git a/core/src/main/java/com/graphhopper/routing/util/EdgeFilter.java b/core/src/main/java/com/graphhopper/routing/util/EdgeFilter.java index 9dc221b43ba..e06c4ac108d 100644 --- a/core/src/main/java/com/graphhopper/routing/util/EdgeFilter.java +++ b/core/src/main/java/com/graphhopper/routing/util/EdgeFilter.java @@ -22,21 +22,19 @@ /** * Class used to traverse a graph. *

+ * * @author Peter Karich */ -public interface EdgeFilter -{ - /** - * @return true if the current edge should be processed and false otherwise. - */ - boolean accept( EdgeIteratorState edgeState ); - - static final EdgeFilter ALL_EDGES = new EdgeFilter() - { +public interface EdgeFilter { + static final EdgeFilter ALL_EDGES = new EdgeFilter() { @Override - public final boolean accept( EdgeIteratorState edgeState ) - { + public final boolean accept(EdgeIteratorState edgeState) { return true; } }; + + /** + * @return true if the current edge should be processed and false otherwise. + */ + boolean accept(EdgeIteratorState edgeState); } diff --git a/core/src/main/java/com/graphhopper/routing/util/EncodedDoubleValue.java b/core/src/main/java/com/graphhopper/routing/util/EncodedDoubleValue.java index bb92772c948..2773a961b9f 100644 --- a/core/src/main/java/com/graphhopper/routing/util/EncodedDoubleValue.java +++ b/core/src/main/java/com/graphhopper/routing/util/EncodedDoubleValue.java @@ -20,41 +20,35 @@ /** * Encapsulates a bit-encoded value. *

+ * * @author Nop */ -public class EncodedDoubleValue extends EncodedValue -{ +public class EncodedDoubleValue extends EncodedValue { - public EncodedDoubleValue( String name, int shift, int bits, double factor, long defaultValue, int maxValue ) - { + public EncodedDoubleValue(String name, int shift, int bits, double factor, long defaultValue, int maxValue) { this(name, shift, bits, factor, defaultValue, maxValue, true); } - public EncodedDoubleValue( String name, int shift, int bits, double factor, long defaultValue, int maxValue, boolean allowZero ) - { + public EncodedDoubleValue(String name, int shift, int bits, double factor, long defaultValue, int maxValue, boolean allowZero) { super(name, shift, bits, factor, defaultValue, maxValue, allowZero); } @Override - public long setValue( long flags, long value ) - { + public long setValue(long flags, long value) { throw new IllegalStateException("Use setDoubleValue instead"); } @Override - public long getValue( long flags ) - { + public long getValue(long flags) { throw new IllegalStateException("Use setDoubleValue instead"); } @Override - public long setDefaultValue( long flags ) - { + public long setDefaultValue(long flags) { return setDoubleValue(flags, defaultValue); } - public long setDoubleValue( long flags, double value ) - { + public long setDoubleValue(long flags, double value) { if (Double.isNaN(value)) throw new IllegalStateException("Value cannot be NaN"); @@ -70,8 +64,7 @@ public long setDoubleValue( long flags, double value ) return flags | tmpValue; } - public double getDoubleValue( long flags ) - { + public double getDoubleValue(long flags) { // find value flags &= mask; flags >>>= shift; diff --git a/core/src/main/java/com/graphhopper/routing/util/EncodedValue.java b/core/src/main/java/com/graphhopper/routing/util/EncodedValue.java index c036be5a33c..f6d9074ad0d 100644 --- a/core/src/main/java/com/graphhopper/routing/util/EncodedValue.java +++ b/core/src/main/java/com/graphhopper/routing/util/EncodedValue.java @@ -20,15 +20,15 @@ /** * Encapsulates a bit-encoded value. *

+ * * @author Nop */ -public class EncodedValue -{ - private final String name; +public class EncodedValue { protected final long shift; protected final long mask; protected final double factor; protected final long defaultValue; + private final String name; private final long maxValue; private final boolean allowZero; private final int bits; @@ -36,20 +36,19 @@ public class EncodedValue /** * Define a bit-encoded value *

- * @param name Description for debugging - * @param shift bit index of this value - * @param bits number of bits reserved - * @param factor scaling factor for stored values + * + * @param name Description for debugging + * @param shift bit index of this value + * @param bits number of bits reserved + * @param factor scaling factor for stored values * @param defaultValue default value - * @param maxValue default maximum value + * @param maxValue default maximum value */ - public EncodedValue( String name, int shift, int bits, double factor, long defaultValue, int maxValue ) - { + public EncodedValue(String name, int shift, int bits, double factor, long defaultValue, int maxValue) { this(name, shift, bits, factor, defaultValue, maxValue, true); } - public EncodedValue( String name, int shift, int bits, double factor, long defaultValue, int maxValue, boolean allowZero ) - { + public EncodedValue(String name, int shift, int bits, double factor, long defaultValue, int maxValue, boolean allowZero) { this.name = name; this.shift = shift; this.factor = factor; @@ -64,8 +63,7 @@ public EncodedValue( String name, int shift, int bits, double factor, long defau this.allowZero = allowZero; } - protected void checkValue( long value ) - { + protected void checkValue(long value) { if (value > maxValue) throw new IllegalArgumentException(name + " value too large for encoding: " + value + ", maxValue:" + maxValue); if (value < 0) @@ -74,8 +72,7 @@ protected void checkValue( long value ) throw new IllegalArgumentException("zero " + name + " value not allowed! " + value); } - public long setValue( long flags, long value ) - { + public long setValue(long flags, long value) { checkValue(value); // scale value value /= factor; @@ -88,36 +85,32 @@ public long setValue( long flags, long value ) return flags | value; } - public long getValue( long flags ) - { + public long getValue(long flags) { // find value flags &= mask; flags >>>= shift; return Math.round(flags * factor); } - public int getBits() - { + public int getBits() { return bits; } - public long setDefaultValue( long flags ) - { + public long setDefaultValue(long flags) { return setValue(flags, defaultValue); } - public long getMaxValue() - { + public long getMaxValue() { return maxValue; } /** * Swap the contents controlled by this value encoder with the given value. *

+ * * @return the new flags */ - public long swap( long flags, EncodedValue otherEncoder ) - { + public long swap(long flags, EncodedValue otherEncoder) { long otherValue = otherEncoder.getValue(flags); flags = otherEncoder.setValue(flags, getValue(flags)); return setValue(flags, otherValue); diff --git a/core/src/main/java/com/graphhopper/routing/util/EncodingManager.java b/core/src/main/java/com/graphhopper/routing/util/EncodingManager.java index 79083a9caf1..a47905b4541 100644 --- a/core/src/main/java/com/graphhopper/routing/util/EncodingManager.java +++ b/core/src/main/java/com/graphhopper/routing/util/EncodingManager.java @@ -17,37 +17,38 @@ */ package com.graphhopper.routing.util; -import com.graphhopper.routing.weighting.TurnWeighting; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - import com.graphhopper.reader.ReaderNode; import com.graphhopper.reader.ReaderRelation; import com.graphhopper.reader.ReaderWay; +import com.graphhopper.routing.weighting.TurnWeighting; import com.graphhopper.storage.Directory; import com.graphhopper.storage.RAMDirectory; import com.graphhopper.storage.StorableProperties; import com.graphhopper.util.EdgeIteratorState; import com.graphhopper.util.PMap; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + /** * Manager class to register encoder, assign their flag values and check objects with all encoders * during parsing. *

+ * * @author Peter Karich * @author Nop */ -public class EncodingManager -{ +public class EncodingManager { + private static final String ERR = "Encoders are requesting more than %s bits of %s flags. "; + private static final String WAY_ERR = "Decrease the number of vehicles or increase the flags to take long via graph.bytes_for_flags=8"; private final List edgeEncoders = new ArrayList(); - + private final int bitsForEdgeFlags; + private final int bitsForTurnFlags = 8 * 4; private int nextWayBit = 0; private int nextNodeBit = 0; private int nextRelBit = 0; private int nextTurnBit = 0; - private final int bitsForEdgeFlags; - private final int bitsForTurnFlags = 8 * 4; private boolean enableInstructions = true; private String preferredLanguage = ""; @@ -55,46 +56,43 @@ public class EncodingManager * Instantiate manager with the given list of encoders. The manager knows several default * encoders ignoring case. *

+ * * @param flagEncodersStr comma delimited list of encoders. The order does not matter. */ - public EncodingManager( String flagEncodersStr ) - { + public EncodingManager(String flagEncodersStr) { this(FlagEncoderFactory.DEFAULT, flagEncodersStr, 4); } - public EncodingManager( FlagEncoderFactory factory, String flagEncodersStr, int bytesForEdgeFlags ) - { + public EncodingManager(FlagEncoderFactory factory, String flagEncodersStr, int bytesForEdgeFlags) { this(parseEncoderString(factory, flagEncodersStr), bytesForEdgeFlags); } /** * Instantiate manager with the given list of encoders. *

+ * * @param flagEncoders comma delimited list of encoders. The order does not matter. */ - public EncodingManager( FlagEncoder... flagEncoders ) - { + public EncodingManager(FlagEncoder... flagEncoders) { this(Arrays.asList(flagEncoders)); } /** * Instantiate manager with the given list of encoders. *

+ * * @param flagEncoders comma delimited list of encoders. The order does not matter. */ - public EncodingManager( List flagEncoders ) - { + public EncodingManager(List flagEncoders) { this(flagEncoders, 4); } - public EncodingManager( List flagEncoders, int bytesForEdgeFlags ) - { + public EncodingManager(List flagEncoders, int bytesForEdgeFlags) { if (bytesForEdgeFlags != 4 && bytesForEdgeFlags != 8) throw new IllegalStateException("For 'edge flags' currently only 4 or 8 bytes supported"); this.bitsForEdgeFlags = bytesForEdgeFlags * 8; - for (FlagEncoder flagEncoder : flagEncoders) - { + for (FlagEncoder flagEncoder : flagEncoders) { registerEncoder((AbstractFlagEncoder) flagEncoder); } @@ -102,13 +100,7 @@ public EncodingManager( List flagEncoders, int bytesForEd throw new IllegalStateException("No vehicles found"); } - public int getBytesForFlags() - { - return bitsForEdgeFlags / 8; - } - - static List parseEncoderString( FlagEncoderFactory factory, String encoderList ) - { + static List parseEncoderString(FlagEncoderFactory factory, String encoderList) { if (encoderList.contains(":")) throw new IllegalArgumentException("EncodingManager does no longer use reflection instantiate encoders directly."); @@ -118,15 +110,13 @@ static List parseEncoderString( FlagEncoderFactory factory, String String[] entries = encoderList.split(","); List resultEncoders = new ArrayList(); - for (String entry : entries) - { + for (String entry : entries) { entry = entry.trim().toLowerCase(); if (entry.isEmpty()) continue; String entryVal = ""; - if (entry.contains("|")) - { + if (entry.contains("|")) { entryVal = entry; entry = entry.split("\\|")[0]; } @@ -143,16 +133,46 @@ static List parseEncoderString( FlagEncoderFactory factory, String return resultEncoders; } - private static final String ERR = "Encoders are requesting more than %s bits of %s flags. "; - private static final String WAY_ERR = "Decrease the number of vehicles or increase the flags to take long via graph.bytes_for_flags=8"; + static String fixWayName(String str) { + if (str == null) + return ""; + return str.replaceAll(";[ ]*", ", "); + } + + /** + * Create the EncodingManager from the provided GraphHopper location. Throws an + * IllegalStateException if it fails. Used if no EncodingManager specified on load. + */ + public static EncodingManager create(FlagEncoderFactory factory, String ghLoc) { + Directory dir = new RAMDirectory(ghLoc, true); + StorableProperties properties = new StorableProperties(dir); + if (!properties.loadExisting()) + throw new IllegalStateException("Cannot load properties to fetch EncodingManager configuration at: " + + dir.getLocation()); + + // check encoding for compatibility + properties.checkVersions(false); + String acceptStr = properties.get("graph.flag_encoders"); + + if (acceptStr.isEmpty()) + throw new IllegalStateException("EncodingManager was not configured. And no one was found in the graph: " + + dir.getLocation()); - private void registerEncoder( AbstractFlagEncoder encoder ) - { + int bytesForFlags = 4; + if ("8".equals(properties.get("graph.bytes_for_flags"))) + bytesForFlags = 8; + return new EncodingManager(factory, acceptStr, bytesForFlags); + } + + public int getBytesForFlags() { + return bitsForEdgeFlags / 8; + } + + private void registerEncoder(AbstractFlagEncoder encoder) { if (encoder.isRegistered()) throw new IllegalStateException("You must not register a FlagEncoder (" + encoder.toString() + ") twice!"); - for (FlagEncoder fe : edgeEncoders) - { + for (FlagEncoder fe : edgeEncoders) { if (fe.toString().equals(encoder.toString())) throw new IllegalArgumentException("Cannot register edge encoder. Name already exists: " + fe.toString()); } @@ -190,20 +210,16 @@ private void registerEncoder( AbstractFlagEncoder encoder ) /** * @return true if the specified encoder is found */ - public boolean supports( String encoder ) - { + public boolean supports(String encoder) { return getEncoder(encoder, false) != null; } - public FlagEncoder getEncoder( String name ) - { + public FlagEncoder getEncoder(String name) { return getEncoder(name, true); } - private FlagEncoder getEncoder( String name, boolean throwExc ) - { - for (FlagEncoder encoder : edgeEncoders) - { + private FlagEncoder getEncoder(String name, boolean throwExc) { + for (FlagEncoder encoder : edgeEncoders) { if (name.equalsIgnoreCase(encoder.toString())) return encoder; } @@ -215,22 +231,18 @@ private FlagEncoder getEncoder( String name, boolean throwExc ) /** * Determine whether a way is routable for one of the added encoders. */ - public long acceptWay( ReaderWay way ) - { + public long acceptWay(ReaderWay way) { long includeWay = 0; - for (AbstractFlagEncoder encoder : edgeEncoders) - { + for (AbstractFlagEncoder encoder : edgeEncoders) { includeWay |= encoder.acceptWay(way); } return includeWay; } - public long handleRelationTags( ReaderRelation relation, long oldRelationFlags ) - { + public long handleRelationTags(ReaderRelation relation, long oldRelationFlags) { long flags = 0; - for (AbstractFlagEncoder encoder : edgeEncoders) - { + for (AbstractFlagEncoder encoder : edgeEncoders) { flags |= encoder.handleRelationTags(relation, oldRelationFlags); } @@ -241,14 +253,13 @@ public long handleRelationTags( ReaderRelation relation, long oldRelationFlags ) * Processes way properties of different kind to determine speed and direction. Properties are * directly encoded in 8 bytes. *

+ * * @param relationFlags The preprocessed relation flags is used to influence the way properties. * @return the encoded flags */ - public long handleWayTags( ReaderWay way, long includeWay, long relationFlags ) - { + public long handleWayTags(ReaderWay way, long includeWay, long relationFlags) { long flags = 0; - for (AbstractFlagEncoder encoder : edgeEncoders) - { + for (AbstractFlagEncoder encoder : edgeEncoders) { flags |= encoder.handleWayTags(way, includeWay, relationFlags & encoder.getRelBitMask()); } @@ -256,11 +267,9 @@ public long handleWayTags( ReaderWay way, long includeWay, long relationFlags ) } @Override - public String toString() - { + public String toString() { StringBuilder str = new StringBuilder(); - for (FlagEncoder encoder : edgeEncoders) - { + for (FlagEncoder encoder : edgeEncoders) { if (str.length() > 0) str.append(","); @@ -270,11 +279,9 @@ public String toString() return str.toString(); } - public String toDetailsString() - { + public String toDetailsString() { StringBuilder str = new StringBuilder(); - for (AbstractFlagEncoder encoder : edgeEncoders) - { + for (AbstractFlagEncoder encoder : edgeEncoders) { if (str.length() > 0) str.append(","); @@ -288,11 +295,9 @@ public String toDetailsString() return str.toString(); } - public long flagsDefault( boolean forward, boolean backward ) - { + public long flagsDefault(boolean forward, boolean backward) { long flags = 0; - for (AbstractFlagEncoder encoder : edgeEncoders) - { + for (AbstractFlagEncoder encoder : edgeEncoders) { flags |= encoder.flagsDefault(forward, backward); } return flags; @@ -301,28 +306,24 @@ public long flagsDefault( boolean forward, boolean backward ) /** * Reverse flags, to do so all encoders are called. */ - public long reverseFlags( long flags ) - { + public long reverseFlags(long flags) { // performance critical int len = edgeEncoders.size(); - for (int i = 0; i < len; i++) - { + for (int i = 0; i < len; i++) { flags = edgeEncoders.get(i).reverseFlags(flags); } return flags; } @Override - public int hashCode() - { + public int hashCode() { int hash = 5; hash = 53 * hash + (this.edgeEncoders != null ? this.edgeEncoders.hashCode() : 0); return hash; } @Override - public boolean equals( Object obj ) - { + public boolean equals(Object obj) { if (obj == null) return false; @@ -331,8 +332,7 @@ public boolean equals( Object obj ) final EncodingManager other = (EncodingManager) obj; if (this.edgeEncoders != other.edgeEncoders - && (this.edgeEncoders == null || !this.edgeEncoders.equals(other.edgeEncoders))) - { + && (this.edgeEncoders == null || !this.edgeEncoders.equals(other.edgeEncoders))) { return false; } return true; @@ -341,25 +341,21 @@ public boolean equals( Object obj ) /** * Analyze tags on osm node. Store node tags (barriers etc) for later usage while parsing way. */ - public long handleNodeTags( ReaderNode node ) - { + public long handleNodeTags(ReaderNode node) { long flags = 0; - for (AbstractFlagEncoder encoder : edgeEncoders) - { + for (AbstractFlagEncoder encoder : edgeEncoders) { flags |= encoder.handleNodeTags(node); } return flags; } - public EncodingManager setEnableInstructions( boolean enableInstructions ) - { + public EncodingManager setEnableInstructions(boolean enableInstructions) { this.enableInstructions = enableInstructions; return this; } - public EncodingManager setPreferredLanguage( String preferredLanguage ) - { + public EncodingManager setPreferredLanguage(String preferredLanguage) { if (preferredLanguage == null) throw new IllegalArgumentException("preferred language cannot be null"); @@ -367,11 +363,9 @@ public EncodingManager setPreferredLanguage( String preferredLanguage ) return this; } - public void applyWayTags( ReaderWay way, EdgeIteratorState edge ) - { + public void applyWayTags(ReaderWay way, EdgeIteratorState edge) { // storing the road name does not yet depend on the flagEncoder so manage it directly - if (enableInstructions) - { + if (enableInstructions) { // String wayInfo = carFlagEncoder.getWayInfo(way); // http://wiki.openstreetmap.org/wiki/Key:name String name = ""; @@ -381,8 +375,7 @@ public void applyWayTags( ReaderWay way, EdgeIteratorState edge ) name = fixWayName(way.getTag("name")); // http://wiki.openstreetmap.org/wiki/Key:ref String refName = fixWayName(way.getTag("ref")); - if (!refName.isEmpty()) - { + if (!refName.isEmpty()) { if (name.isEmpty()) name = refName; else @@ -392,8 +385,7 @@ public void applyWayTags( ReaderWay way, EdgeIteratorState edge ) edge.setName(name); } - for (AbstractFlagEncoder encoder : edgeEncoders) - { + for (AbstractFlagEncoder encoder : edgeEncoders) { encoder.applyWayTags(way, edge); } } @@ -401,53 +393,17 @@ public void applyWayTags( ReaderWay way, EdgeIteratorState edge ) /** * The returned list is never empty. */ - public List fetchEdgeEncoders() - { + public List fetchEdgeEncoders() { List list = new ArrayList(); list.addAll(edgeEncoders); return list; } - static String fixWayName( String str ) - { - if (str == null) - return ""; - return str.replaceAll(";[ ]*", ", "); - } - - public boolean needsTurnCostsSupport() - { - for (FlagEncoder encoder : edgeEncoders) - { + public boolean needsTurnCostsSupport() { + for (FlagEncoder encoder : edgeEncoders) { if (encoder.supports(TurnWeighting.class)) return true; } return false; } - - /** - * Create the EncodingManager from the provided GraphHopper location. Throws an - * IllegalStateException if it fails. Used if no EncodingManager specified on load. - */ - public static EncodingManager create( FlagEncoderFactory factory, String ghLoc ) - { - Directory dir = new RAMDirectory(ghLoc, true); - StorableProperties properties = new StorableProperties(dir); - if (!properties.loadExisting()) - throw new IllegalStateException("Cannot load properties to fetch EncodingManager configuration at: " - + dir.getLocation()); - - // check encoding for compatibility - properties.checkVersions(false); - String acceptStr = properties.get("graph.flag_encoders"); - - if (acceptStr.isEmpty()) - throw new IllegalStateException("EncodingManager was not configured. And no one was found in the graph: " - + dir.getLocation()); - - int bytesForFlags = 4; - if ("8".equals(properties.get("graph.bytes_for_flags"))) - bytesForFlags = 8; - return new EncodingManager(factory, acceptStr, bytesForFlags); - } } diff --git a/core/src/main/java/com/graphhopper/routing/util/FlagEncoder.java b/core/src/main/java/com/graphhopper/routing/util/FlagEncoder.java index 5caa3a1da2b..a9d8ac1a06b 100644 --- a/core/src/main/java/com/graphhopper/routing/util/FlagEncoder.java +++ b/core/src/main/java/com/graphhopper/routing/util/FlagEncoder.java @@ -24,10 +24,15 @@ * This class provides methods to define how a value (like speed or direction) converts to a flag * (currently an integer value), which is stored in an edge . *

+ * * @author Peter Karich */ -public interface FlagEncoder extends TurnCostEncoder -{ +public interface FlagEncoder extends TurnCostEncoder { + /** + * Reports whether this edge is part of a roundabout. + */ + static final int K_ROUNDABOUT = 2; + /** * @return the version of this FlagEncoder to enforce none-compatibility when new attributes are * introduced @@ -42,48 +47,46 @@ public interface FlagEncoder extends TurnCostEncoder /** * @return the speed in km/h for this direction, for backward direction use getReverseSpeed */ - double getSpeed( long flags ); + double getSpeed(long flags); /** * Sets the speed in km/h. *

+ * * @return modified setProperties */ - long setSpeed( long flags, double speed ); + long setSpeed(long flags, double speed); /** * @return the speed of the reverse direction in km/h */ - double getReverseSpeed( long flags ); + double getReverseSpeed(long flags); /** * Sets the reverse speed in the flags. */ - long setReverseSpeed( long flags, double speed ); + long setReverseSpeed(long flags, double speed); /** * Sets the access of the edge. *

+ * * @return modified flags */ - long setAccess( long flags, boolean forward, boolean backward ); + long setAccess(long flags, boolean forward, boolean backward); /** * Sets speed and access properties. *

+ * * @return created flags */ - long setProperties( double speed, boolean forward, boolean backward ); + long setProperties(double speed, boolean forward, boolean backward); /** * Reports whether the edge is available in forward direction for a certain vehicle */ - boolean isForward( long flags ); - - /** - * Reports whether the edge is available in backward direction for a certain vehicle - */ - boolean isBackward( long flags ); + boolean isForward(long flags); /* * Simple rules for every subclass which introduces a new key. It has to use the prefix K_ and @@ -91,42 +94,42 @@ public interface FlagEncoder extends TurnCostEncoder * Currently this means starting from 100, and subclasses of this class start from 10000 and so on. */ /** - * Reports whether this edge is part of a roundabout. + * Reports whether the edge is available in backward direction for a certain vehicle */ - static final int K_ROUNDABOUT = 2; + boolean isBackward(long flags); /** * Returns arbitrary boolean value identified by the specified key. */ - boolean isBool( long flags, int key ); + boolean isBool(long flags, int key); - long setBool( long flags, int key, boolean value ); + long setBool(long flags, int key, boolean value); /** * Returns arbitrary long value identified by the specified key. E.g. can be used to return the * way or surface type of an edge */ - long getLong( long flags, int key ); + long getLong(long flags, int key); - long setLong( long flags, int key, long value ); + long setLong(long flags, int key, long value); /** * Returns arbitrary double value identified by the specified key. E.g. can be used to return * the maximum width or height allowed for an edge. */ - double getDouble( long flags, int key ); + double getDouble(long flags, int key); - long setDouble( long flags, int key, double value ); + long setDouble(long flags, int key, double value); /** * Returns true if the feature class is supported like TurnWeighting or PriorityWeighting. */ - boolean supports( Class feature ); + boolean supports(Class feature); /** * @return additional cost or warning information for an instruction like ferry or road charges. */ - InstructionAnnotation getAnnotation( long flags, Translation tr ); + InstructionAnnotation getAnnotation(long flags, Translation tr); /** * @return true if already registered in an EncodingManager diff --git a/core/src/main/java/com/graphhopper/routing/util/FlagEncoderFactory.java b/core/src/main/java/com/graphhopper/routing/util/FlagEncoderFactory.java index 9c403b453d8..feee82647f6 100644 --- a/core/src/main/java/com/graphhopper/routing/util/FlagEncoderFactory.java +++ b/core/src/main/java/com/graphhopper/routing/util/FlagEncoderFactory.java @@ -20,11 +20,9 @@ import com.graphhopper.util.PMap; /** - * * @author Peter Karich */ -public interface FlagEncoderFactory -{ +public interface FlagEncoderFactory { final String CAR = "car"; final String BIKE = "bike"; final String BIKE2 = "bike2"; @@ -36,5 +34,5 @@ public interface FlagEncoderFactory final String GENERIC = "generic"; final FlagEncoderFactory DEFAULT = new DefaultFlagEncoderFactory(); - FlagEncoder createFlagEncoder( String name, PMap configuration ); + FlagEncoder createFlagEncoder(String name, PMap configuration); } diff --git a/core/src/main/java/com/graphhopper/routing/util/FootFlagEncoder.java b/core/src/main/java/com/graphhopper/routing/util/FootFlagEncoder.java index c1923db2cf5..79b0bf95eee 100644 --- a/core/src/main/java/com/graphhopper/routing/util/FootFlagEncoder.java +++ b/core/src/main/java/com/graphhopper/routing/util/FootFlagEncoder.java @@ -17,11 +17,9 @@ */ package com.graphhopper.routing.util; -import com.graphhopper.routing.weighting.PriorityWeighting; import com.graphhopper.reader.ReaderRelation; import com.graphhopper.reader.ReaderWay; -import com.graphhopper.reader.osm.conditional.ConditionalOSMTagInspector; -import com.graphhopper.reader.osm.conditional.DateRangeParser; +import com.graphhopper.routing.weighting.PriorityWeighting; import com.graphhopper.util.PMap; import java.util.*; @@ -33,48 +31,44 @@ * roads only. If you wish to also prefer routes due to beauty like hiking routes use the * HikeFlagEncoder instead. *

+ * * @author Peter Karich * @author Nop * @author Karl Hübner */ -public class FootFlagEncoder extends AbstractFlagEncoder -{ +public class FootFlagEncoder extends AbstractFlagEncoder { static final int SLOW_SPEED = 2; static final int MEAN_SPEED = 5; static final int FERRY_SPEED = 10; - private EncodedValue priorityWayEncoder; - private EncodedValue relationCodeEncoder; - protected HashSet sidewalkValues = new HashSet(5); - protected HashSet sidewalksNoValues = new HashSet(5); final Set safeHighwayTags = new HashSet(); final Set allowedHighwayTags = new HashSet(); final Set avoidHighwayTags = new HashSet(); // convert network tag of hiking routes into a way route code final Map hikingNetworkToCode = new HashMap(); + protected HashSet sidewalkValues = new HashSet(5); + protected HashSet sidewalksNoValues = new HashSet(5); + private EncodedValue priorityWayEncoder; + private EncodedValue relationCodeEncoder; /** * Should be only instantiated via EncodingManager */ - public FootFlagEncoder() - { + public FootFlagEncoder() { this(4, 1); } - public FootFlagEncoder( PMap properties ) - { + public FootFlagEncoder(PMap properties) { this((int) properties.getLong("speedBits", 4), properties.getDouble("speedFactor", 1)); this.properties = properties; this.setBlockFords(properties.getBool("blockFords", true)); } - public FootFlagEncoder( String propertiesStr ) - { + public FootFlagEncoder(String propertiesStr) { this(new PMap(propertiesStr)); } - public FootFlagEncoder( int speedBits, double speedFactor ) - { + public FootFlagEncoder(int speedBits, double speedFactor) { super(speedBits, speedFactor, 0); restrictions.addAll(Arrays.asList("foot", "access")); restrictedValues.add("private"); @@ -140,14 +134,12 @@ public FootFlagEncoder( int speedBits, double speedFactor ) } @Override - public int getVersion() - { + public int getVersion() { return 2; } @Override - public int defineWayBits( int index, int shift ) - { + public int defineWayBits(int index, int shift) { // first two bits are reserved for route handling in superclass shift = super.defineWayBits(index, shift); // larger value required - ferries are faster than pedestrians @@ -160,8 +152,7 @@ public int defineWayBits( int index, int shift ) } @Override - public int defineRelationBits( int index, int shift ) - { + public int defineRelationBits(int index, int shift) { relationCodeEncoder = new EncodedValue("RelationCode", shift, 3, 1, 0, 7); return shift + relationCodeEncoder.getBits(); } @@ -170,36 +161,34 @@ public int defineRelationBits( int index, int shift ) * Foot flag encoder does not provide any turn cost / restrictions */ @Override - public int defineTurnBits( int index, int shift ) - { + public int defineTurnBits(int index, int shift) { return shift; } /** * Foot flag encoder does not provide any turn cost / restrictions *

+ * * @return false */ @Override - public boolean isTurnRestricted( long flag ) - { + public boolean isTurnRestricted(long flag) { return false; } /** * Foot flag encoder does not provide any turn cost / restrictions *

+ * * @return 0 */ @Override - public double getTurnCost( long flag ) - { + public double getTurnCost(long flag) { return 0; } @Override - public long getTurnFlags( boolean restricted, double costs ) - { + public long getTurnFlags(boolean restricted, double costs) { return 0; } @@ -208,13 +197,10 @@ public long getTurnFlags( boolean restricted, double costs ) *

*/ @Override - public long acceptWay( ReaderWay way ) - { + public long acceptWay(ReaderWay way) { String highwayValue = way.getTag("highway"); - if (highwayValue == null) - { - if (way.hasTag("route", ferries)) - { + if (highwayValue == null) { + if (way.hasTag("route", ferries)) { String footTag = way.getTag("foot"); if (footTag == null || "yes".equals(footTag)) return acceptBit | ferryBit; @@ -228,8 +214,7 @@ public long acceptWay( ReaderWay way ) } String sacScale = way.getTag("sac_scale"); - if (sacScale != null) - { + if (sacScale != null) { if (!"hiking".equals(sacScale) && !"mountain_hiking".equals(sacScale) && !"demanding_mountain_hiking".equals(sacScale) && !"alpine_hiking".equals(sacScale)) // other scales are too dangerous, see http://wiki.openstreetmap.org/wiki/Key:sac_scale @@ -264,18 +249,15 @@ public long acceptWay( ReaderWay way ) } @Override - public long handleRelationTags( ReaderRelation relation, long oldRelationFlags ) - { + public long handleRelationTags(ReaderRelation relation, long oldRelationFlags) { int code = 0; - if (relation.hasTag("route", "hiking") || relation.hasTag("route", "foot")) - { + if (relation.hasTag("route", "hiking") || relation.hasTag("route", "foot")) { Integer val = hikingNetworkToCode.get(relation.getTag("network")); if (val != null) code = val; else code = hikingNetworkToCode.get("lwn"); - } else if (relation.hasTag("route", "ferry")) - { + } else if (relation.hasTag("route", "ferry")) { code = PriorityCode.AVOID_IF_POSSIBLE.getValue(); } @@ -286,23 +268,19 @@ public long handleRelationTags( ReaderRelation relation, long oldRelationFlags ) } @Override - public long handleWayTags( ReaderWay way, long allowed, long relationFlags ) - { + public long handleWayTags(ReaderWay way, long allowed, long relationFlags) { if (!isAccept(allowed)) return 0; long flags = 0; - if (!isFerry(allowed)) - { + if (!isFerry(allowed)) { String sacScale = way.getTag("sac_scale"); - if (sacScale != null) - { + if (sacScale != null) { if ("hiking".equals(sacScale)) flags = speedEncoder.setDoubleValue(flags, MEAN_SPEED); else flags = speedEncoder.setDoubleValue(flags, SLOW_SPEED); - } else - { + } else { flags = speedEncoder.setDoubleValue(flags, MEAN_SPEED); } flags |= directionBitMask; @@ -311,8 +289,7 @@ public long handleWayTags( ReaderWay way, long allowed, long relationFlags ) if (isRoundabout) flags = setBool(flags, K_ROUNDABOUT, true); - } else - { + } else { double ferrySpeed = getFerrySpeed(way, SLOW_SPEED, MEAN_SPEED, FERRY_SPEED); flags = setSpeed(flags, ferrySpeed); flags |= directionBitMask; @@ -327,10 +304,8 @@ public long handleWayTags( ReaderWay way, long allowed, long relationFlags ) } @Override - public double getDouble( long flags, int key ) - { - switch (key) - { + public double getDouble(long flags, int key) { + switch (key) { case PriorityWeighting.KEY: return (double) priorityWayEncoder.getValue(flags) / BEST.getValue(); default: @@ -338,8 +313,7 @@ public double getDouble( long flags, int key ) } } - protected int handlePriority( ReaderWay way, int priorityFromRelation ) - { + protected int handlePriority(ReaderWay way, int priorityFromRelation) { TreeMap weightToPrioMap = new TreeMap(); if (priorityFromRelation == 0) weightToPrioMap.put(0d, UNCHANGED.getValue()); @@ -354,27 +328,23 @@ protected int handlePriority( ReaderWay way, int priorityFromRelation ) /** * @param weightToPrioMap associate a weight with every priority. This sorted map allows - * subclasses to 'insert' more important priorities as well as overwrite determined priorities. + * subclasses to 'insert' more important priorities as well as overwrite determined priorities. */ - void collect( ReaderWay way, TreeMap weightToPrioMap ) - { + void collect(ReaderWay way, TreeMap weightToPrioMap) { String highway = way.getTag("highway"); if (way.hasTag("foot", "designated")) weightToPrioMap.put(100d, PREFER.getValue()); double maxSpeed = getMaxSpeed(way); - if (safeHighwayTags.contains(highway) || maxSpeed > 0 && maxSpeed <= 20) - { + if (safeHighwayTags.contains(highway) || maxSpeed > 0 && maxSpeed <= 20) { weightToPrioMap.put(40d, PREFER.getValue()); - if (way.hasTag("tunnel", intendedValues)) - { + if (way.hasTag("tunnel", intendedValues)) { if (way.hasTag("sidewalk", sidewalksNoValues)) weightToPrioMap.put(40d, AVOID_IF_POSSIBLE.getValue()); else weightToPrioMap.put(40d, UNCHANGED.getValue()); } - } else if (maxSpeed > 50 || avoidHighwayTags.contains(highway)) - { + } else if (maxSpeed > 50 || avoidHighwayTags.contains(highway)) { if (!way.hasTag("sidewalk", sidewalkValues)) weightToPrioMap.put(45d, AVOID_IF_POSSIBLE.getValue()); } @@ -384,8 +354,7 @@ void collect( ReaderWay way, TreeMap weightToPrioMap ) } @Override - public boolean supports( Class feature ) - { + public boolean supports(Class feature) { if (super.supports(feature)) return true; @@ -393,8 +362,7 @@ public boolean supports( Class feature ) } @Override - public String toString() - { + public String toString() { return "foot"; } } diff --git a/core/src/main/java/com/graphhopper/routing/util/HikeFlagEncoder.java b/core/src/main/java/com/graphhopper/routing/util/HikeFlagEncoder.java index 24862df8904..693fe653604 100644 --- a/core/src/main/java/com/graphhopper/routing/util/HikeFlagEncoder.java +++ b/core/src/main/java/com/graphhopper/routing/util/HikeFlagEncoder.java @@ -17,11 +17,11 @@ */ package com.graphhopper.routing.util; -import com.graphhopper.routing.weighting.PriorityWeighting; import com.graphhopper.reader.ReaderWay; +import com.graphhopper.routing.weighting.PriorityWeighting; import com.graphhopper.util.PMap; -import java.util.*; +import java.util.TreeMap; import static com.graphhopper.routing.util.PriorityCode.*; @@ -30,55 +30,46 @@ * * @author Peter Karich */ -public class HikeFlagEncoder extends FootFlagEncoder -{ +public class HikeFlagEncoder extends FootFlagEncoder { /** * Should be only instantiated via EncodingManager */ - public HikeFlagEncoder() - { + public HikeFlagEncoder() { this(4, 1); } - public HikeFlagEncoder( PMap properties ) - { + public HikeFlagEncoder(PMap properties) { this((int) properties.getLong("speedBits", 4), properties.getDouble("speedFactor", 1)); this.properties = properties; this.setBlockFords(properties.getBool("blockFords", true)); } - public HikeFlagEncoder( String propertiesStr ) - { + public HikeFlagEncoder(String propertiesStr) { this(new PMap(propertiesStr)); } - public HikeFlagEncoder( int speedBits, double speedFactor ) - { + public HikeFlagEncoder(int speedBits, double speedFactor) { super(speedBits, speedFactor); hikingNetworkToCode.put("iwn", BEST.getValue()); hikingNetworkToCode.put("nwn", BEST.getValue()); hikingNetworkToCode.put("rwn", VERY_NICE.getValue()); hikingNetworkToCode.put("lwn", VERY_NICE.getValue()); - + init(); } @Override - public int getVersion() - { + public int getVersion() { return 1; } @Override - public long acceptWay( ReaderWay way ) - { + public long acceptWay(ReaderWay way) { String highwayValue = way.getTag("highway"); - if (highwayValue == null) - { - if (way.hasTag("route", ferries)) - { + if (highwayValue == null) { + if (way.hasTag("route", ferries)) { String footTag = way.getTag("foot"); if (footTag == null || "yes".equals(footTag)) return acceptBit | ferryBit; @@ -121,25 +112,21 @@ public long acceptWay( ReaderWay way ) } @Override - void collect( ReaderWay way, TreeMap weightToPrioMap ) - { + void collect(ReaderWay way, TreeMap weightToPrioMap) { String highway = way.getTag("highway"); if (way.hasTag("foot", "designated")) weightToPrioMap.put(100d, PREFER.getValue()); double maxSpeed = getMaxSpeed(way); - if (safeHighwayTags.contains(highway) || maxSpeed > 0 && maxSpeed <= 20) - { + if (safeHighwayTags.contains(highway) || maxSpeed > 0 && maxSpeed <= 20) { weightToPrioMap.put(40d, PREFER.getValue()); - if (way.hasTag("tunnel", intendedValues)) - { + if (way.hasTag("tunnel", intendedValues)) { if (way.hasTag("sidewalk", sidewalksNoValues)) weightToPrioMap.put(40d, REACH_DEST.getValue()); else weightToPrioMap.put(40d, UNCHANGED.getValue()); } - } else if (maxSpeed > 50 || avoidHighwayTags.contains(highway)) - { + } else if (maxSpeed > 50 || avoidHighwayTags.contains(highway)) { if (way.hasTag("sidewalk", sidewalksNoValues)) weightToPrioMap.put(45d, WORST.getValue()); else @@ -151,8 +138,7 @@ void collect( ReaderWay way, TreeMap weightToPrioMap ) } @Override - public boolean supports( Class feature ) - { + public boolean supports(Class feature) { if (super.supports(feature)) return true; @@ -160,8 +146,7 @@ public boolean supports( Class feature ) } @Override - public String toString() - { + public String toString() { return "hike"; } } diff --git a/core/src/main/java/com/graphhopper/routing/util/HintsMap.java b/core/src/main/java/com/graphhopper/routing/util/HintsMap.java index 507ba6b75f0..cf107b074e2 100644 --- a/core/src/main/java/com/graphhopper/routing/util/HintsMap.java +++ b/core/src/main/java/com/graphhopper/routing/util/HintsMap.java @@ -22,50 +22,42 @@ /** * @author Peter Karich */ -public class HintsMap extends PMap -{ - public HintsMap() - { +public class HintsMap extends PMap { + public HintsMap() { } /** * Convenient constructor if only one parameter is provided *

*/ - public HintsMap( String weighting ) - { + public HintsMap(String weighting) { super(5); setWeighting(weighting); } @Override - public HintsMap put( String key, Object str ) - { + public HintsMap put(String key, Object str) { super.put(key, str); return this; } - public HintsMap setWeighting( String w ) - { + public String getWeighting() { + return super.get("weighting", "").toLowerCase(); + } + + public HintsMap setWeighting(String w) { if (w != null) super.put("weighting", w); return this; } - public String getWeighting() - { - return super.get("weighting", "").toLowerCase(); + public String getVehicle() { + return super.get("vehicle", "").toLowerCase(); } - public HintsMap setVehicle( String v ) - { + public HintsMap setVehicle(String v) { if (v != null) super.put("vehicle", v); return this; } - - public String getVehicle() - { - return super.get("vehicle", "").toLowerCase(); - } } diff --git a/core/src/main/java/com/graphhopper/routing/util/LevelEdgeFilter.java b/core/src/main/java/com/graphhopper/routing/util/LevelEdgeFilter.java index 1a69e4a1978..d3bbdaf569a 100644 --- a/core/src/main/java/com/graphhopper/routing/util/LevelEdgeFilter.java +++ b/core/src/main/java/com/graphhopper/routing/util/LevelEdgeFilter.java @@ -18,28 +18,26 @@ package com.graphhopper.routing.util; import com.graphhopper.storage.CHGraph; -import com.graphhopper.util.EdgeIteratorState; import com.graphhopper.util.CHEdgeIteratorState; +import com.graphhopper.util.EdgeIteratorState; /** * Only certain nodes are accepted and therefor the others are ignored. *

+ * * @author Peter Karich */ -public class LevelEdgeFilter implements EdgeFilter -{ +public class LevelEdgeFilter implements EdgeFilter { private final CHGraph graph; private final int maxNodes; - public LevelEdgeFilter( CHGraph g ) - { + public LevelEdgeFilter(CHGraph g) { graph = g; maxNodes = g.getNodes(); } @Override - public boolean accept( EdgeIteratorState edgeIterState ) - { + public boolean accept(EdgeIteratorState edgeIterState) { int base = edgeIterState.getBaseNode(); int adj = edgeIterState.getAdjNode(); // always accept virtual edges, see #288 diff --git a/core/src/main/java/com/graphhopper/routing/util/MotorcycleFlagEncoder.java b/core/src/main/java/com/graphhopper/routing/util/MotorcycleFlagEncoder.java index 3d2d6a70b4d..8bbbf2d4d96 100644 --- a/core/src/main/java/com/graphhopper/routing/util/MotorcycleFlagEncoder.java +++ b/core/src/main/java/com/graphhopper/routing/util/MotorcycleFlagEncoder.java @@ -17,9 +17,9 @@ */ package com.graphhopper.routing.util; +import com.graphhopper.reader.ReaderWay; import com.graphhopper.routing.weighting.CurvatureWeighting; import com.graphhopper.routing.weighting.PriorityWeighting; -import com.graphhopper.reader.ReaderWay; import com.graphhopper.util.BitUtil; import com.graphhopper.util.EdgeIteratorState; import com.graphhopper.util.PMap; @@ -35,23 +35,19 @@ * @author Peter Karich * @author boldtrn */ -public class MotorcycleFlagEncoder extends CarFlagEncoder -{ +public class MotorcycleFlagEncoder extends CarFlagEncoder { public static final int CURVATURE_KEY = 112; - + private final HashSet avoidSet = new HashSet(); + private final HashSet preferSet = new HashSet(); private EncodedDoubleValue reverseSpeedEncoder; private EncodedValue priorityWayEncoder; private EncodedValue curvatureEncoder; - private final HashSet avoidSet = new HashSet(); - private final HashSet preferSet = new HashSet(); - public MotorcycleFlagEncoder() - { + public MotorcycleFlagEncoder() { this(5, 5, 0); } - public MotorcycleFlagEncoder( PMap properties ) - { + public MotorcycleFlagEncoder(PMap properties) { this( (int) properties.getLong("speed_bits", 5), properties.getDouble("speed_factor", 5), @@ -61,13 +57,11 @@ public MotorcycleFlagEncoder( PMap properties ) this.setBlockFords(properties.getBool("block_fords", true)); } - public MotorcycleFlagEncoder( String propertiesStr ) - { + public MotorcycleFlagEncoder(String propertiesStr) { this(new PMap(propertiesStr)); } - public MotorcycleFlagEncoder( int speedBits, double speedFactor, int maxTurnCosts ) - { + public MotorcycleFlagEncoder(int speedBits, double speedFactor, int maxTurnCosts) { super(speedBits, speedFactor, maxTurnCosts); restrictions.remove("motorcar"); // moped, mofa @@ -121,13 +115,12 @@ public MotorcycleFlagEncoder( int speedBits, double speedFactor, int maxTurnCost defaultSpeedMap.put("road", 20); // forestry stuff defaultSpeedMap.put("track", 15); - + init(); } @Override - public int getVersion() - { + public int getVersion() { return 1; } @@ -135,8 +128,7 @@ public int getVersion() * Define the place of the speedBits in the edge flags for car. */ @Override - public int defineWayBits( int index, int shift ) - { + public int defineWayBits(int index, int shift) { // first two bits are reserved for route handling in superclass shift = super.defineWayBits(index, shift); reverseSpeedEncoder = new EncodedDoubleValue("Reverse Speed", shift, speedBits, speedFactor, @@ -153,13 +145,10 @@ public int defineWayBits( int index, int shift ) } @Override - public long acceptWay( ReaderWay way ) - { + public long acceptWay(ReaderWay way) { String highwayValue = way.getTag("highway"); - if (highwayValue == null) - { - if (way.hasTag("route", ferries)) - { + if (highwayValue == null) { + if (way.hasTag("route", ferries)) { String motorcycleTag = way.getTag("motorcycle"); if (motorcycleTag == null) motorcycleTag = way.getTag("motor_vehicle"); @@ -170,8 +159,7 @@ public long acceptWay( ReaderWay way ) return 0; } - if ("track".equals(highwayValue)) - { + if ("track".equals(highwayValue)) { String tt = way.getTag("tracktype"); if (tt != null && !tt.equals("grade1")) return 0; @@ -184,8 +172,7 @@ public long acceptWay( ReaderWay way ) return 0; String firstValue = way.getFirstPriorityTag(restrictions); - if (!firstValue.isEmpty()) - { + if (!firstValue.isEmpty()) { if (restrictedValues.contains(firstValue) && !getConditionalTagInspector().isRestrictedWayConditionallyPermitted(way)) return 0; if (intendedValues.contains(firstValue)) @@ -203,14 +190,12 @@ public long acceptWay( ReaderWay way ) } @Override - public long handleWayTags( ReaderWay way, long allowed, long priorityFromRelation ) - { + public long handleWayTags(ReaderWay way, long allowed, long priorityFromRelation) { if (!isAccept(allowed)) return 0; long flags = 0; - if (!isFerry(allowed)) - { + if (!isFerry(allowed)) { // get assumed speed from highway type double speed = getSpeed(way); speed = applyMaxSpeed(way, speed); @@ -227,26 +212,21 @@ public long handleWayTags( ReaderWay way, long allowed, long priorityFromRelatio if (isRoundabout) flags = setBool(0, K_ROUNDABOUT, true); - if (way.hasTag("oneway", oneways) || isRoundabout) - { - if (way.hasTag("oneway", "-1")) - { + if (way.hasTag("oneway", oneways) || isRoundabout) { + if (way.hasTag("oneway", "-1")) { flags = setReverseSpeed(flags, speed); flags |= backwardBit; - } else - { + } else { flags = setSpeed(flags, speed); flags |= forwardBit; } - } else - { + } else { flags = setSpeed(flags, speed); flags = setReverseSpeed(flags, speed); flags |= directionBitMask; } - } else - { + } else { double ferrySpeed = getFerrySpeed(way, defaultSpeedMap.get("living_street"), defaultSpeedMap.get("service"), defaultSpeedMap.get("residential")); flags = setSpeed(flags, ferrySpeed); flags = setReverseSpeed(flags, ferrySpeed); @@ -263,14 +243,12 @@ public long handleWayTags( ReaderWay way, long allowed, long priorityFromRelatio } @Override - public double getReverseSpeed( long flags ) - { + public double getReverseSpeed(long flags) { return reverseSpeedEncoder.getDoubleValue(flags); } @Override - public long setReverseSpeed( long flags, double speed ) - { + public long setReverseSpeed(long flags, double speed) { if (speed < 0) throw new IllegalArgumentException("Speed cannot be negative: " + speed + ", flags:" + BitUtil.LITTLE.toBitString(flags)); @@ -284,8 +262,7 @@ public long setReverseSpeed( long flags, double speed ) } @Override - protected long setLowSpeed( long flags, double speed, boolean reverse ) - { + protected long setLowSpeed(long flags, double speed, boolean reverse) { if (reverse) return setBool(reverseSpeedEncoder.setDoubleValue(flags, 0), K_BACKWARD, false); @@ -293,8 +270,7 @@ protected long setLowSpeed( long flags, double speed, boolean reverse ) } @Override - public long flagsDefault( boolean forward, boolean backward ) - { + public long flagsDefault(boolean forward, boolean backward) { long flags = super.flagsDefault(forward, backward); if (backward) return reverseSpeedEncoder.setDefaultValue(flags); @@ -303,8 +279,7 @@ public long flagsDefault( boolean forward, boolean backward ) } @Override - public long setProperties( double speed, boolean forward, boolean backward ) - { + public long setProperties(double speed, boolean forward, boolean backward) { long flags = super.setProperties(speed, forward, backward); if (backward) return setReverseSpeed(flags, speed); @@ -313,8 +288,7 @@ public long setProperties( double speed, boolean forward, boolean backward ) } @Override - public long reverseFlags( long flags ) - { + public long reverseFlags(long flags) { // swap access flags = super.reverseFlags(flags); @@ -325,10 +299,8 @@ public long reverseFlags( long flags ) } @Override - public double getDouble( long flags, int key ) - { - switch (key) - { + public double getDouble(long flags, int key) { + switch (key) { case PriorityWeighting.KEY: return (double) priorityWayEncoder.getValue(flags) / BEST.getValue(); case MotorcycleFlagEncoder.CURVATURE_KEY: @@ -338,14 +310,11 @@ public double getDouble( long flags, int key ) } } - private int handlePriority( ReaderWay way, long relationFlags ) - { + private int handlePriority(ReaderWay way, long relationFlags) { String highway = way.getTag("highway", ""); - if (avoidSet.contains(highway)) - { + if (avoidSet.contains(highway)) { return PriorityCode.WORST.getValue(); - } else if (preferSet.contains(highway)) - { + } else if (preferSet.contains(highway)) { return PriorityCode.BEST.getValue(); } @@ -353,8 +322,7 @@ private int handlePriority( ReaderWay way, long relationFlags ) } @Override - public void applyWayTags( ReaderWay way, EdgeIteratorState edge ) - { + public void applyWayTags(ReaderWay way, EdgeIteratorState edge) { double speed = this.getSpeed(edge.getFlags()); double roadDistance = edge.getDistance(); double beelineDistance = getBeelineDistance(way); @@ -367,18 +335,15 @@ public void applyWayTags( ReaderWay way, EdgeIteratorState edge ) edge.setFlags(this.curvatureEncoder.setValue(edge.getFlags(), convertToInt(bendiness))); } - private double getBeelineDistance( ReaderWay way ) - { + private double getBeelineDistance(ReaderWay way) { return way.getTag("estimated_distance", Double.POSITIVE_INFINITY); } /** * Streets that slow are not fun and probably in a town. */ - protected double discriminateSlowStreets( double bendiness, double speed ) - { - if (speed < 51) - { + protected double discriminateSlowStreets(double bendiness, double speed) { + if (speed < 51) { return 1; } return bendiness; @@ -390,10 +355,8 @@ protected double discriminateSlowStreets( double bendiness, double speed ) * approximated, therefore it can happen on straight roads, that the beeline is longer than the * road. */ - protected double correctErrors( double bendiness ) - { - if (bendiness < 0.01 || bendiness > 1) - { + protected double correctErrors(double bendiness) { + if (bendiness < 0.01 || bendiness > 1) { return 1; } return bendiness; @@ -403,34 +366,29 @@ protected double correctErrors( double bendiness ) * A good bendiness should become a greater impact. A bendiness close to 1 should not be * changed. */ - protected double increaseBendinessImpact( double bendiness ) - { + protected double increaseBendinessImpact(double bendiness) { return (Math.pow(bendiness, 2)); } @Override - public boolean supports( Class feature ) - { + public boolean supports(Class feature) { if (super.supports(feature)) return true; - if (CurvatureWeighting.class.isAssignableFrom(feature)) - { + if (CurvatureWeighting.class.isAssignableFrom(feature)) { return true; } return PriorityWeighting.class.isAssignableFrom(feature); } - protected int convertToInt( double bendiness ) - { + protected int convertToInt(double bendiness) { bendiness = bendiness * 10; return (int) bendiness; } @Override - public String toString() - { + public String toString() { return "motorcycle"; } } diff --git a/core/src/main/java/com/graphhopper/routing/util/MountainBikeFlagEncoder.java b/core/src/main/java/com/graphhopper/routing/util/MountainBikeFlagEncoder.java index e32930cebb0..697d198d15b 100644 --- a/core/src/main/java/com/graphhopper/routing/util/MountainBikeFlagEncoder.java +++ b/core/src/main/java/com/graphhopper/routing/util/MountainBikeFlagEncoder.java @@ -17,32 +17,27 @@ */ package com.graphhopper.routing.util; -import static com.graphhopper.routing.util.PriorityCode.BEST; -import static com.graphhopper.routing.util.PriorityCode.PREFER; -import static com.graphhopper.routing.util.PriorityCode.UNCHANGED; -import static com.graphhopper.routing.util.PriorityCode.VERY_NICE; - -import java.util.TreeMap; - import com.graphhopper.reader.ReaderRelation; import com.graphhopper.reader.ReaderWay; import com.graphhopper.util.PMap; +import java.util.TreeMap; + +import static com.graphhopper.routing.util.PriorityCode.*; + /** * Specifies the settings for mountain biking *

+ * * @author ratrun * @author Peter Karich */ -public class MountainBikeFlagEncoder extends BikeCommonFlagEncoder -{ - public MountainBikeFlagEncoder() - { +public class MountainBikeFlagEncoder extends BikeCommonFlagEncoder { + public MountainBikeFlagEncoder() { this(4, 2, 0); } - public MountainBikeFlagEncoder( PMap properties ) - { + public MountainBikeFlagEncoder(PMap properties) { this( (int) properties.getLong("speed_bits", 4), properties.getDouble("speed_factor", 2), @@ -52,13 +47,11 @@ public MountainBikeFlagEncoder( PMap properties ) this.setBlockFords(properties.getBool("block_fords", true)); } - public MountainBikeFlagEncoder( String propertiesStr ) - { + public MountainBikeFlagEncoder(String propertiesStr) { this(new PMap(propertiesStr)); } - public MountainBikeFlagEncoder( int speedBits, double speedFactor, int maxTurnCosts ) - { + public MountainBikeFlagEncoder(int speedBits, double speedFactor, int maxTurnCosts) { super(speedBits, speedFactor, maxTurnCosts); setTrackTypeSpeed("grade1", 18); // paved setTrackTypeSpeed("grade2", 16); // now unpaved ... @@ -141,24 +134,21 @@ public MountainBikeFlagEncoder( int speedBits, double speedFactor, int maxTurnCo potentialBarriers.add("kissing_gate"); setSpecificClassBicycle("mtb"); - + init(); } @Override - public int getVersion() - { + public int getVersion() { return 1; } @Override - void collect( ReaderWay way, double wayTypeSpeed, TreeMap weightToPrioMap ) - { + void collect(ReaderWay way, double wayTypeSpeed, TreeMap weightToPrioMap) { super.collect(way, wayTypeSpeed, weightToPrioMap); String highway = way.getTag("highway"); - if ("track".equals(highway)) - { + if ("track".equals(highway)) { String trackType = way.getTag("tracktype"); if ("grade1".equals(trackType)) weightToPrioMap.put(50d, UNCHANGED.getValue()); @@ -170,8 +160,7 @@ else if (trackType.startsWith("grade")) } @Override - public long handleRelationTags( ReaderRelation relation, long oldRelationFlags ) - { + public long handleRelationTags(ReaderRelation relation, long oldRelationFlags) { oldRelationFlags = super.handleRelationTags(relation, oldRelationFlags); int code = 0; if (relation.hasTag("route", "mtb")) @@ -184,16 +173,14 @@ public long handleRelationTags( ReaderRelation relation, long oldRelationFlags ) } @Override - boolean isSacScaleAllowed( String sacScale ) - { + boolean isSacScaleAllowed(String sacScale) { // other scales are too dangerous even for MTB, see http://wiki.openstreetmap.org/wiki/Key:sac_scale return "hiking".equals(sacScale) || "mountain_hiking".equals(sacScale) || "demanding_mountain_hiking".equals(sacScale) || "alpine_hiking".equals(sacScale); } @Override - public String toString() - { + public String toString() { return "mtb"; } } diff --git a/core/src/main/java/com/graphhopper/routing/util/PriorityCode.java b/core/src/main/java/com/graphhopper/routing/util/PriorityCode.java index 7f380b86237..e286be16e6a 100644 --- a/core/src/main/java/com/graphhopper/routing/util/PriorityCode.java +++ b/core/src/main/java/com/graphhopper/routing/util/PriorityCode.java @@ -21,10 +21,10 @@ * Used to store a priority value in the way flags of an edge. Used in combination with * PriorityWeighting *

+ * * @author Peter Karich */ -public enum PriorityCode -{ +public enum PriorityCode { WORST(0), AVOID_AT_ALL_COSTS(1), REACH_DEST(2), @@ -35,13 +35,11 @@ public enum PriorityCode BEST(7); private final int value; - private PriorityCode( int value ) - { + private PriorityCode(int value) { this.value = value; } - public int getValue() - { + public int getValue() { return value; } diff --git a/core/src/main/java/com/graphhopper/routing/util/RacingBikeFlagEncoder.java b/core/src/main/java/com/graphhopper/routing/util/RacingBikeFlagEncoder.java index d4ed20418b0..bc36886c319 100644 --- a/core/src/main/java/com/graphhopper/routing/util/RacingBikeFlagEncoder.java +++ b/core/src/main/java/com/graphhopper/routing/util/RacingBikeFlagEncoder.java @@ -20,25 +20,23 @@ import com.graphhopper.reader.ReaderWay; import com.graphhopper.util.PMap; -import static com.graphhopper.routing.util.PriorityCode.*; - import java.util.TreeMap; +import static com.graphhopper.routing.util.PriorityCode.*; + /** * Specifies the settings for race biking *

+ * * @author ratrun * @author Peter Karich */ -public class RacingBikeFlagEncoder extends BikeCommonFlagEncoder -{ - public RacingBikeFlagEncoder() - { +public class RacingBikeFlagEncoder extends BikeCommonFlagEncoder { + public RacingBikeFlagEncoder() { this(4, 2, 0); } - public RacingBikeFlagEncoder( PMap properties ) - { + public RacingBikeFlagEncoder(PMap properties) { this( (int) properties.getLong("speed_bits", 4), properties.getDouble("speed_factor", 2), @@ -48,13 +46,11 @@ public RacingBikeFlagEncoder( PMap properties ) this.setBlockFords(properties.getBool("block_fords", true)); } - public RacingBikeFlagEncoder( String propertiesStr ) - { + public RacingBikeFlagEncoder(String propertiesStr) { this(new PMap(propertiesStr)); } - public RacingBikeFlagEncoder( int speedBits, double speedFactor, int maxTurnCosts ) - { + public RacingBikeFlagEncoder(int speedBits, double speedFactor, int maxTurnCosts) { super(speedBits, speedFactor, maxTurnCosts); preferHighwayTags.add("road"); preferHighwayTags.add("secondary"); @@ -130,27 +126,23 @@ public RacingBikeFlagEncoder( int speedBits, double speedFactor, int maxTurnCost setAvoidSpeedLimit(81); setSpecificClassBicycle("roadcycling"); - + init(); } @Override - public int getVersion() - { + public int getVersion() { return 1; } @Override - void collect( ReaderWay way, double wayTypeSpeed, TreeMap weightToPrioMap ) - { + void collect(ReaderWay way, double wayTypeSpeed, TreeMap weightToPrioMap) { super.collect(way, wayTypeSpeed, weightToPrioMap); String highway = way.getTag("highway"); - if ("service".equals(highway)) - { + if ("service".equals(highway)) { weightToPrioMap.put(40d, UNCHANGED.getValue()); - } else if ("track".equals(highway)) - { + } else if ("track".equals(highway)) { String trackType = way.getTag("tracktype"); if ("grade1".equals(trackType)) weightToPrioMap.put(110d, PREFER.getValue()); @@ -160,8 +152,7 @@ else if (trackType == null || trackType.startsWith("grade")) } @Override - boolean isPushingSection( ReaderWay way ) - { + boolean isPushingSection(ReaderWay way) { String highway = way.getTag("highway"); String trackType = way.getTag("tracktype"); return way.hasTag("highway", pushingSectionsHighways) @@ -171,15 +162,13 @@ boolean isPushingSection( ReaderWay way ) } @Override - boolean isSacScaleAllowed( String sacScale ) - { + boolean isSacScaleAllowed(String sacScale) { // for racing bike it is only allowed if empty return false; } @Override - public String toString() - { + public String toString() { return "racingbike"; } } diff --git a/core/src/main/java/com/graphhopper/routing/util/TestAlgoCollector.java b/core/src/main/java/com/graphhopper/routing/util/TestAlgoCollector.java index 2d0dec7afc5..9a8995c95eb 100644 --- a/core/src/main/java/com/graphhopper/routing/util/TestAlgoCollector.java +++ b/core/src/main/java/com/graphhopper/routing/util/TestAlgoCollector.java @@ -17,11 +17,11 @@ */ package com.graphhopper.routing.util; -import com.graphhopper.routing.weighting.TurnWeighting; import com.graphhopper.PathWrapper; import com.graphhopper.routing.*; -import com.graphhopper.storage.Graph; +import com.graphhopper.routing.weighting.TurnWeighting; import com.graphhopper.storage.CHGraph; +import com.graphhopper.storage.Graph; import com.graphhopper.storage.TurnCostExtension; import com.graphhopper.storage.index.LocationIndex; import com.graphhopper.storage.index.QueryResult; @@ -35,21 +35,18 @@ /** * @author Peter Karich */ -public class TestAlgoCollector -{ +public class TestAlgoCollector { + public final List errors = new ArrayList(); private final String name; private final DistanceCalc distCalc = Helper.DIST_EARTH; private final TranslationMap trMap = new TranslationMap().doImport(); - public final List errors = new ArrayList(); - public TestAlgoCollector( String name ) - { + public TestAlgoCollector(String name) { this.name = name; } - public TestAlgoCollector assertDistance( AlgoHelperEntry algoEntry, List queryList, - OneRun oneRun ) - { + public TestAlgoCollector assertDistance(AlgoHelperEntry algoEntry, List queryList, + OneRun oneRun) { List altPaths = new ArrayList(); QueryGraph queryGraph = new QueryGraph(algoEntry.getQueryGraph()); queryGraph.lookup(queryList); @@ -58,8 +55,7 @@ public TestAlgoCollector assertDistance( AlgoHelperEntry algoEntry, List 2) - { + if (Math.abs(rsp.getDistance() - tmpDist) > 2) { errors.add(algoEntry + " path.getDistance was " + rsp.getDistance() + "\t pointList.calcDistance was " + tmpDist + "\t (expected points " + oneRun.getLocs() + ", expected distance " + oneRun.getDistance() + ") " + queryList); } - if (Math.abs(rsp.getDistance() - oneRun.getDistance()) > 2) - { + if (Math.abs(rsp.getDistance() - oneRun.getDistance()) > 2) { errors.add(algoEntry + " returns path not matching the expected distance of " + oneRun.getDistance() + "\t Returned was " + rsp.getDistance() + "\t (expected points " + oneRun.getLocs() + ", was " + pointList.getSize() + ") " + queryList); } // There are real world instances where A-B-C is identical to A-C (in meter precision). - if (Math.abs(pointList.getSize() - oneRun.getLocs()) > 1) - { + if (Math.abs(pointList.getSize() - oneRun.getLocs()) > 1) { errors.add(algoEntry + " returns path not matching the expected points of " + oneRun.getLocs() + "\t Returned was " + pointList.getSize() + "\t (expected distance " + oneRun.getDistance() + ", was " + rsp.getDistance() + ") " + queryList); @@ -106,19 +98,16 @@ public TestAlgoCollector assertDistance( AlgoHelperEntry algoEntry, List .1) - { + if (Math.abs(dist - expectedDist) > .1) { errors.add("queried lat,lon=" + (float) lat + "," + (float) lon + " (found: " + (float) found.lat + "," + (float) found.lon + ")" + "\n expected distance:" + expectedDist + ", but was:" + dist); @@ -126,156 +115,127 @@ void queryIndex( Graph g, LocationIndex idx, double lat, double lon, double expe } @Override - public String toString() - { + public String toString() { String str = ""; str += "FOUND " + errors.size() + " ERRORS.\n"; - for (String s : errors) - { + for (String s : errors) { str += s + ".\n"; } return str; } - void printSummary() - { - if (errors.size() > 0) - { + void printSummary() { + if (errors.size() > 0) { System.out.println("\n-------------------------------\n"); System.out.println(toString()); - } else - { + } else { System.out.println("SUCCESS for " + name + "!"); } } - public static class AlgoHelperEntry - { - private Graph queryGraph; + public static class AlgoHelperEntry { private final Graph baseGraph; private final LocationIndex idx; + private Graph queryGraph; private AlgorithmOptions opts; - public AlgoHelperEntry( Graph g, Graph baseGraph, AlgorithmOptions opts, LocationIndex idx ) - { + public AlgoHelperEntry(Graph g, Graph baseGraph, AlgorithmOptions opts, LocationIndex idx) { this.queryGraph = g; this.baseGraph = baseGraph; this.opts = opts; this.idx = idx; } - public Graph getQueryGraph() - { + public Graph getQueryGraph() { return queryGraph; } - public void setQueryGraph( Graph queryGraph ) - { + public void setQueryGraph(Graph queryGraph) { this.queryGraph = queryGraph; } - public Graph getBaseGraph() - { + public Graph getBaseGraph() { return baseGraph; } - public void setAlgorithmOptions( AlgorithmOptions opts ) - { + public void setAlgorithmOptions(AlgorithmOptions opts) { this.opts = opts; } - public LocationIndex getIdx() - { + public LocationIndex getIdx() { return idx; } - public RoutingAlgorithm createAlgo( Graph qGraph ) - { + public RoutingAlgorithm createAlgo(Graph qGraph) { return new RoutingAlgorithmFactorySimple().createAlgo(qGraph, opts); } @Override - public String toString() - { + public String toString() { return opts.getAlgorithm() + (queryGraph instanceof CHGraph ? "CH" : ""); } } - public static class OneRun - { + public static class OneRun { private final List assumptions = new ArrayList(); - public OneRun() - { + public OneRun() { } - public OneRun( double fromLat, double fromLon, double toLat, double toLon, double dist, int locs ) - { + public OneRun(double fromLat, double fromLon, double toLat, double toLon, double dist, int locs) { add(fromLat, fromLon, 0, 0); add(toLat, toLon, dist, locs); } - public OneRun add( double lat, double lon, double dist, int locs ) - { + public OneRun add(double lat, double lon, double dist, int locs) { assumptions.add(new AssumptionPerPath(lat, lon, dist, locs)); return this; } - public int getLocs() - { + public int getLocs() { int sum = 0; - for (AssumptionPerPath as : assumptions) - { + for (AssumptionPerPath as : assumptions) { sum += as.locs; } return sum; } - public void setLocs( int index, int locs ) - { + public void setLocs(int index, int locs) { assumptions.get(index).locs = locs; } - public double getDistance() - { + public double getDistance() { double sum = 0; - for (AssumptionPerPath as : assumptions) - { + for (AssumptionPerPath as : assumptions) { sum += as.distance; } return sum; } - public void setDistance( int index, double dist ) - { + public void setDistance(int index, double dist) { assumptions.get(index).distance = dist; } - public List getList( LocationIndex idx, EdgeFilter edgeFilter ) - { + public List getList(LocationIndex idx, EdgeFilter edgeFilter) { List qr = new ArrayList(); - for (AssumptionPerPath p : assumptions) - { + for (AssumptionPerPath p : assumptions) { qr.add(idx.findClosest(p.lat, p.lon, edgeFilter)); } return qr; } @Override - public String toString() - { + public String toString() { return assumptions.toString(); } } - static class AssumptionPerPath - { + static class AssumptionPerPath { double lat, lon; int locs; double distance; - public AssumptionPerPath( double lat, double lon, double distance, int locs ) - { + public AssumptionPerPath(double lat, double lon, double distance, int locs) { this.lat = lat; this.lon = lon; this.locs = locs; @@ -283,8 +243,7 @@ public AssumptionPerPath( double lat, double lon, double distance, int locs ) } @Override - public String toString() - { + public String toString() { return lat + ", " + lon + ", locs:" + locs + ", dist:" + distance; } } diff --git a/core/src/main/java/com/graphhopper/routing/util/TraversalMode.java b/core/src/main/java/com/graphhopper/routing/util/TraversalMode.java index d7d4798e0ef..e54ad8532b1 100644 --- a/core/src/main/java/com/graphhopper/routing/util/TraversalMode.java +++ b/core/src/main/java/com/graphhopper/routing/util/TraversalMode.java @@ -28,10 +28,10 @@ * still all are without via-way support. BTW: this would not be done at runtime, this would be a * pre-processing step to avoid performance penalties. *

+ * * @author Peter Karich */ -public enum TraversalMode -{ +public enum TraversalMode { /** * The simplest traversal mode but without turn restrictions or cost support. */ @@ -59,8 +59,7 @@ public enum TraversalMode private final int noOfStates; private final boolean uTurnSupport; - TraversalMode( boolean edgeBased, int noOfStates, boolean uTurnSupport ) - { + TraversalMode(boolean edgeBased, int noOfStates, boolean uTurnSupport) { this.edgeBased = edgeBased; this.noOfStates = noOfStates; this.uTurnSupport = uTurnSupport; @@ -69,20 +68,28 @@ public enum TraversalMode throw new IllegalArgumentException("Currently only 1 or 2 states allowed"); } + public static TraversalMode fromString(String name) { + try { + return valueOf(name.toUpperCase()); + } catch (Exception ex) { + throw new IllegalArgumentException("TraversalMode " + name + " not supported. " + + "Supported are: " + Arrays.asList(TraversalMode.values())); + } + } + /** * Returns the identifier to access the map of the shortest path tree according to the traversal * mode. E.g. returning the adjacent node id in node-based behavior whilst returning the edge id * in edge-based behavior *

+ * * @param iterState the current {@link EdgeIteratorState} - * @param reverse true, if traversal in backward direction. Will be true only for - * backward searches in bidirectional algorithms. + * @param reverse true, if traversal in backward direction. Will be true only for + * backward searches in bidirectional algorithms. * @return the identifier to access the shortest path tree */ - public final int createTraversalId( EdgeIteratorState iterState, boolean reverse ) - { - if (edgeBased) - { + public final int createTraversalId(EdgeIteratorState iterState, boolean reverse) { + if (edgeBased) { if (noOfStates == 1) return iterState.getEdge(); @@ -95,10 +102,8 @@ public final int createTraversalId( EdgeIteratorState iterState, boolean reverse /** * If you have an EdgeIteratorState the other createTraversalId is preferred! */ - public final int createTraversalId( int baseNode, int adjNode, int edgeId, boolean reverse ) - { - if (edgeBased) - { + public final int createTraversalId(int baseNode, int adjNode, int edgeId, boolean reverse) { + if (edgeBased) { if (noOfStates == 1) return edgeId; @@ -108,37 +113,21 @@ public final int createTraversalId( int baseNode, int adjNode, int edgeId, boole return adjNode; } - public int reverseEdgeKey( int edgeKey ) - { + public int reverseEdgeKey(int edgeKey) { if (edgeBased && noOfStates > 1) return GHUtility.reverseEdgeKey(edgeKey); return edgeKey; } - public int getNoOfStates() - { + public int getNoOfStates() { return noOfStates; } - public boolean isEdgeBased() - { + public boolean isEdgeBased() { return edgeBased; } - public final boolean hasUTurnSupport() - { + public final boolean hasUTurnSupport() { return uTurnSupport; } - - public static TraversalMode fromString( String name ) - { - try - { - return valueOf(name.toUpperCase()); - } catch (Exception ex) - { - throw new IllegalArgumentException("TraversalMode " + name + " not supported. " - + "Supported are: " + Arrays.asList(TraversalMode.values())); - } - } } diff --git a/core/src/main/java/com/graphhopper/routing/util/TurnCostEncoder.java b/core/src/main/java/com/graphhopper/routing/util/TurnCostEncoder.java index c3686b9db80..fadc4f1b247 100644 --- a/core/src/main/java/com/graphhopper/routing/util/TurnCostEncoder.java +++ b/core/src/main/java/com/graphhopper/routing/util/TurnCostEncoder.java @@ -20,52 +20,48 @@ /** * Encodes and decodes a turn restriction and turn costs within a integer flag *

+ * * @author Karl Hübner */ -public interface TurnCostEncoder -{ +public interface TurnCostEncoder { /** * @return true, if the turn restriction is encoded in the specified flags */ - boolean isTurnRestricted( long flags ); + boolean isTurnRestricted(long flags); /** * @return the costs encoded in the specified flag, if restricted it will be * Double.POSITIVE_INFINITY */ - double getTurnCost( long flags ); + double getTurnCost(long flags); /** * @param restricted true if restricted turn, equivalent to specifying of costs - * Double.POSITIVE_INFINITY - * @param costs the turn costs, specify 0 or Double.POSITIVE_INFINITY if restricted == true. - * Only used if restricted == false. + * Double.POSITIVE_INFINITY + * @param costs the turn costs, specify 0 or Double.POSITIVE_INFINITY if restricted == true. + * Only used if restricted == false. * @return the encoded flags */ - long getTurnFlags( boolean restricted, double costs ); + long getTurnFlags(boolean restricted, double costs); /** * whether turn costs nor turn restrictions will be encoded by this encoder, should be used for * pedestrians */ - class NoTurnCostsEncoder implements TurnCostEncoder - { + class NoTurnCostsEncoder implements TurnCostEncoder { @Override - public boolean isTurnRestricted( long flags ) - { + public boolean isTurnRestricted(long flags) { return false; } @Override - public double getTurnCost( long flags ) - { + public double getTurnCost(long flags) { return 0; } @Override - public long getTurnFlags( boolean restriction, double costs ) - { + public long getTurnFlags(boolean restriction, double costs) { return 0; } } diff --git a/core/src/main/java/com/graphhopper/routing/util/tour/MultiPointTour.java b/core/src/main/java/com/graphhopper/routing/util/tour/MultiPointTour.java index 96858905b0d..db68185c411 100644 --- a/core/src/main/java/com/graphhopper/routing/util/tour/MultiPointTour.java +++ b/core/src/main/java/com/graphhopper/routing/util/tour/MultiPointTour.java @@ -24,18 +24,15 @@ * * @author Robin Boldt */ -public class MultiPointTour extends TourStrategy -{ +public class MultiPointTour extends TourStrategy { private final int allPoints; private final double initialHeading; - public MultiPointTour( Random random, double distanceInMeter, int allPoints ) - { + public MultiPointTour(Random random, double distanceInMeter, int allPoints) { this(random, distanceInMeter, allPoints, Double.NaN); } - public MultiPointTour( Random random, double distanceInMeter, int allPoints, double initialHeading ) - { + public MultiPointTour(Random random, double distanceInMeter, int allPoints, double initialHeading) { super(random, distanceInMeter); this.allPoints = allPoints; if (Double.isNaN(initialHeading)) @@ -45,20 +42,17 @@ public MultiPointTour( Random random, double distanceInMeter, int allPoints, dou } @Override - public int getNumberOfGeneratedPoints() - { + public int getNumberOfGeneratedPoints() { return allPoints - 1; } @Override - public double getDistanceForIteration( int iteration ) - { + public double getDistanceForIteration(int iteration) { return slightlyModifyDistance(overallDistance / (allPoints + 1)); } @Override - public double getHeadingForIteration( int iteration ) - { + public double getHeadingForIteration(int iteration) { if (iteration == 0) return initialHeading; diff --git a/core/src/main/java/com/graphhopper/routing/util/tour/SinglePointTour.java b/core/src/main/java/com/graphhopper/routing/util/tour/SinglePointTour.java index 84efd6c7ef0..e01ab9675f6 100644 --- a/core/src/main/java/com/graphhopper/routing/util/tour/SinglePointTour.java +++ b/core/src/main/java/com/graphhopper/routing/util/tour/SinglePointTour.java @@ -24,28 +24,23 @@ * * @author Robin Boldt */ -public class SinglePointTour extends TourStrategy -{ - public SinglePointTour( Random random, double distanceInMeter ) - { +public class SinglePointTour extends TourStrategy { + public SinglePointTour(Random random, double distanceInMeter) { super(random, distanceInMeter); } @Override - public int getNumberOfGeneratedPoints() - { + public int getNumberOfGeneratedPoints() { return 1; } @Override - public double getDistanceForIteration( int iteration ) - { + public double getDistanceForIteration(int iteration) { return slightlyModifyDistance(overallDistance / 3); } @Override - public double getHeadingForIteration( int iteration ) - { + public double getHeadingForIteration(int iteration) { return random.nextInt(360); } } diff --git a/core/src/main/java/com/graphhopper/routing/util/tour/TourStrategy.java b/core/src/main/java/com/graphhopper/routing/util/tour/TourStrategy.java index ea0b1016472..e7663997a8f 100644 --- a/core/src/main/java/com/graphhopper/routing/util/tour/TourStrategy.java +++ b/core/src/main/java/com/graphhopper/routing/util/tour/TourStrategy.java @@ -24,13 +24,11 @@ * * @author Robin Boldt */ -public abstract class TourStrategy -{ +public abstract class TourStrategy { protected final Random random; protected final double overallDistance; - public TourStrategy( Random random, double distanceInMeter ) - { + public TourStrategy(Random random, double distanceInMeter) { this.random = random; this.overallDistance = distanceInMeter; } @@ -43,18 +41,17 @@ public TourStrategy( Random random, double distanceInMeter ) /** * Returns the distance in meter that is used for the generated point of a certain iteration */ - public abstract double getDistanceForIteration( int iteration ); + public abstract double getDistanceForIteration(int iteration); /** * Returns the north based heading between 0 and 360 for a certain iteration. */ - public abstract double getHeadingForIteration( int iteration ); + public abstract double getHeadingForIteration(int iteration); /** * Modifies the Distance up to +-10% */ - protected double slightlyModifyDistance( double distance ) - { + protected double slightlyModifyDistance(double distance) { double distanceModification = random.nextDouble() * .1 * distance; if (random.nextBoolean()) distanceModification = -distanceModification; diff --git a/core/src/main/java/com/graphhopper/routing/weighting/AbstractAdjustedWeighting.java b/core/src/main/java/com/graphhopper/routing/weighting/AbstractAdjustedWeighting.java index a344d2f93ba..0583990232c 100644 --- a/core/src/main/java/com/graphhopper/routing/weighting/AbstractAdjustedWeighting.java +++ b/core/src/main/java/com/graphhopper/routing/weighting/AbstractAdjustedWeighting.java @@ -25,12 +25,10 @@ * * @author Robin Boldt */ -public abstract class AbstractAdjustedWeighting implements Weighting -{ +public abstract class AbstractAdjustedWeighting implements Weighting { protected final Weighting superWeighting; - public AbstractAdjustedWeighting( Weighting superWeighting ) - { + public AbstractAdjustedWeighting(Weighting superWeighting) { if (superWeighting == null) throw new IllegalArgumentException("No super weighting set"); this.superWeighting = superWeighting; @@ -40,21 +38,18 @@ public AbstractAdjustedWeighting( Weighting superWeighting ) * Returns the flagEncoder of the superWeighting. Usually we do not have a Flagencoder here. */ @Override - public FlagEncoder getFlagEncoder() - { + public FlagEncoder getFlagEncoder() { return superWeighting.getFlagEncoder(); } @Override - public boolean matches( HintsMap reqMap ) - { + public boolean matches(HintsMap reqMap) { return getName().equals(reqMap.getWeighting()) && superWeighting.getFlagEncoder().toString().equals(reqMap.getVehicle()); } @Override - public String toString() - { + public String toString() { return getName() + "|" + superWeighting.toString(); } } diff --git a/core/src/main/java/com/graphhopper/routing/weighting/AbstractWeighting.java b/core/src/main/java/com/graphhopper/routing/weighting/AbstractWeighting.java index 57e443c2c18..4ed8950f2da 100644 --- a/core/src/main/java/com/graphhopper/routing/weighting/AbstractWeighting.java +++ b/core/src/main/java/com/graphhopper/routing/weighting/AbstractWeighting.java @@ -21,15 +21,12 @@ import com.graphhopper.routing.util.HintsMap; /** - * * @author Peter Karich */ -public abstract class AbstractWeighting implements Weighting -{ +public abstract class AbstractWeighting implements Weighting { protected final FlagEncoder flagEncoder; - public AbstractWeighting( FlagEncoder encoder ) - { + public AbstractWeighting(FlagEncoder encoder) { this.flagEncoder = encoder; if (!flagEncoder.isRegistered()) throw new IllegalStateException("Make sure you add the FlagEncoder " + flagEncoder + " to an EncodingManager before using it elsewhere"); @@ -37,30 +34,40 @@ public AbstractWeighting( FlagEncoder encoder ) throw new IllegalStateException("Not a valid name for a Weighting: " + getName()); } + static final boolean isValidName(String name) { + if (name == null || name.isEmpty()) + return false; + + return name.matches("[\\|_a-z]+"); + } + + /** + * Replaces all characters which are not numbers, characters or underscores with underscores + */ + public static String weightingToFileName(Weighting w) { + return w.toString().toLowerCase().replaceAll("\\|", "_"); + } + @Override - public boolean matches( HintsMap reqMap ) - { + public boolean matches(HintsMap reqMap) { return getName().equals(reqMap.getWeighting()) && flagEncoder.toString().equals(reqMap.getVehicle()); } @Override - public FlagEncoder getFlagEncoder() - { + public FlagEncoder getFlagEncoder() { return flagEncoder; } @Override - public int hashCode() - { + public int hashCode() { int hash = 7; hash = 71 * hash + toString().hashCode(); return hash; } @Override - public boolean equals( Object obj ) - { + public boolean equals(Object obj) { if (obj == null) return false; if (getClass() != obj.getClass()) @@ -69,25 +76,8 @@ public boolean equals( Object obj ) return toString().equals(other.toString()); } - static final boolean isValidName( String name ) - { - if (name == null || name.isEmpty()) - return false; - - return name.matches("[\\|_a-z]+"); - } - - /** - * Replaces all characters which are not numbers, characters or underscores with underscores - */ - public static String weightingToFileName( Weighting w ) - { - return w.toString().toLowerCase().replaceAll("\\|", "_"); - } - @Override - public String toString() - { + public String toString() { return getName() + "|" + flagEncoder; } } diff --git a/core/src/main/java/com/graphhopper/routing/weighting/AvoidEdgesWeighting.java b/core/src/main/java/com/graphhopper/routing/weighting/AvoidEdgesWeighting.java index 007c69b25f8..74e8032f7f7 100644 --- a/core/src/main/java/com/graphhopper/routing/weighting/AvoidEdgesWeighting.java +++ b/core/src/main/java/com/graphhopper/routing/weighting/AvoidEdgesWeighting.java @@ -20,6 +20,7 @@ import com.graphhopper.util.EdgeIteratorState; import gnu.trove.set.TIntSet; import gnu.trove.set.hash.TIntHashSet; + import java.util.Collection; /** @@ -27,20 +28,17 @@ * * @author RobinBoldt */ -public class AvoidEdgesWeighting extends AbstractAdjustedWeighting -{ +public class AvoidEdgesWeighting extends AbstractAdjustedWeighting { // contains the edge IDs of the already visited edges protected final TIntSet visitedEdges = new TIntHashSet(); private double edgePenaltyFactor = 5.0; - public AvoidEdgesWeighting( Weighting superWeighting ) - { + public AvoidEdgesWeighting(Weighting superWeighting) { super(superWeighting); } - public AvoidEdgesWeighting setEdgePenaltyFactor( double edgePenaltyFactor ) - { + public AvoidEdgesWeighting setEdgePenaltyFactor(double edgePenaltyFactor) { this.edgePenaltyFactor = edgePenaltyFactor; return this; } @@ -49,23 +47,19 @@ public AvoidEdgesWeighting setEdgePenaltyFactor( double edgePenaltyFactor ) * This method adds the specified path to this weighting which should be penalized in the * calcWeight method. */ - public void addEdges( Collection edges ) - { - for (EdgeIteratorState edge : edges) - { + public void addEdges(Collection edges) { + for (EdgeIteratorState edge : edges) { visitedEdges.add(edge.getEdge()); } } @Override - public double getMinWeight( double distance ) - { + public double getMinWeight(double distance) { return superWeighting.getMinWeight(distance); } @Override - public double calcWeight( EdgeIteratorState edgeState, boolean reverse, int prevOrNextEdgeId ) - { + public double calcWeight(EdgeIteratorState edgeState, boolean reverse, int prevOrNextEdgeId) { double weight = superWeighting.calcWeight(edgeState, reverse, prevOrNextEdgeId); if (visitedEdges.contains(edgeState.getEdge())) return weight * edgePenaltyFactor; @@ -74,8 +68,7 @@ public double calcWeight( EdgeIteratorState edgeState, boolean reverse, int prev } @Override - public String getName() - { + public String getName() { return "avoid_edges"; } } diff --git a/core/src/main/java/com/graphhopper/routing/weighting/BeelineWeightApproximator.java b/core/src/main/java/com/graphhopper/routing/weighting/BeelineWeightApproximator.java index adbe9c2af75..d0238466a18 100644 --- a/core/src/main/java/com/graphhopper/routing/weighting/BeelineWeightApproximator.java +++ b/core/src/main/java/com/graphhopper/routing/weighting/BeelineWeightApproximator.java @@ -17,7 +17,6 @@ */ package com.graphhopper.routing.weighting; -import com.graphhopper.routing.weighting.Weighting; import com.graphhopper.storage.NodeAccess; import com.graphhopper.util.DistanceCalc; import com.graphhopper.util.Helper; @@ -26,44 +25,39 @@ * Approximates the distance to the goal node by weighting the beeline distance according to the * distance weighting *

+ * * @author jansoe */ -public class BeelineWeightApproximator implements WeightApproximator -{ +public class BeelineWeightApproximator implements WeightApproximator { private final NodeAccess nodeAccess; private final Weighting weighting; private DistanceCalc distanceCalc = Helper.DIST_EARTH; private double toLat, toLon; private double epsilon = 1; - public BeelineWeightApproximator( NodeAccess nodeAccess, Weighting weighting ) - { + public BeelineWeightApproximator(NodeAccess nodeAccess, Weighting weighting) { this.nodeAccess = nodeAccess; this.weighting = weighting; } @Override - public void setGoalNode( int toNode ) - { + public void setGoalNode(int toNode) { toLat = nodeAccess.getLatitude(toNode); toLon = nodeAccess.getLongitude(toNode); } - public WeightApproximator setEpsilon( double epsilon ) - { + public WeightApproximator setEpsilon(double epsilon) { this.epsilon = epsilon; return this; } @Override - public WeightApproximator duplicate() - { + public WeightApproximator duplicate() { return new BeelineWeightApproximator(nodeAccess, weighting).setDistanceCalc(distanceCalc).setEpsilon(epsilon); } @Override - public double approximate( int fromNode ) - { + public double approximate(int fromNode) { double fromLat = nodeAccess.getLatitude(fromNode); double fromLon = nodeAccess.getLongitude(fromNode); double dist2goal = distanceCalc.calcDist(toLat, toLon, fromLat, fromLon); @@ -71,8 +65,7 @@ public double approximate( int fromNode ) return weight2goal * epsilon; } - public BeelineWeightApproximator setDistanceCalc( DistanceCalc distanceCalc ) - { + public BeelineWeightApproximator setDistanceCalc(DistanceCalc distanceCalc) { this.distanceCalc = distanceCalc; return this; } diff --git a/core/src/main/java/com/graphhopper/routing/weighting/ConsistentWeightApproximator.java b/core/src/main/java/com/graphhopper/routing/weighting/ConsistentWeightApproximator.java index 93a98a300f8..5160e1bf1b6 100644 --- a/core/src/main/java/com/graphhopper/routing/weighting/ConsistentWeightApproximator.java +++ b/core/src/main/java/com/graphhopper/routing/weighting/ConsistentWeightApproximator.java @@ -24,30 +24,26 @@ * Mitoh, K. (1994). A fast algorithm for finding better routes by ai search techniques. In VNIS, * pages 291–296. *

+ * * @author jansoe */ -public class ConsistentWeightApproximator -{ +public class ConsistentWeightApproximator { private final WeightApproximator uniDirApproximatorForward, uniDirApproximatorReverse; - public ConsistentWeightApproximator( WeightApproximator weightApprox ) - { + public ConsistentWeightApproximator(WeightApproximator weightApprox) { uniDirApproximatorForward = weightApprox; uniDirApproximatorReverse = weightApprox.duplicate(); } - public void setSourceNode( int sourceNode ) - { + public void setSourceNode(int sourceNode) { uniDirApproximatorReverse.setGoalNode(sourceNode); } - public void setGoalNode( int goalNode ) - { + public void setGoalNode(int goalNode) { uniDirApproximatorForward.setGoalNode(goalNode); } - public double approximate( int fromNode, boolean reverse ) - { + public double approximate(int fromNode, boolean reverse) { double weightApproximation = 0.5 * (uniDirApproximatorForward.approximate(fromNode) - uniDirApproximatorReverse.approximate(fromNode)); diff --git a/core/src/main/java/com/graphhopper/routing/weighting/CurvatureWeighting.java b/core/src/main/java/com/graphhopper/routing/weighting/CurvatureWeighting.java index c6b9f8fd57f..2e1f27a65a9 100644 --- a/core/src/main/java/com/graphhopper/routing/weighting/CurvatureWeighting.java +++ b/core/src/main/java/com/graphhopper/routing/weighting/CurvatureWeighting.java @@ -25,12 +25,10 @@ /** * This Class uses bendiness parameter to prefer curvy routes. */ -public class CurvatureWeighting extends PriorityWeighting -{ +public class CurvatureWeighting extends PriorityWeighting { private final double minFactor; - public CurvatureWeighting( FlagEncoder flagEncoder, PMap pMap ) - { + public CurvatureWeighting(FlagEncoder flagEncoder, PMap pMap) { super(flagEncoder, pMap); double minBendiness = 1; // see correctErrors @@ -39,14 +37,12 @@ public CurvatureWeighting( FlagEncoder flagEncoder, PMap pMap ) } @Override - public double getMinWeight( double distance ) - { + public double getMinWeight(double distance) { return minFactor * distance; } @Override - public double calcWeight( EdgeIteratorState edge, boolean reverse, int prevOrNextEdgeId ) - { + public double calcWeight(EdgeIteratorState edge, boolean reverse, int prevOrNextEdgeId) { double priority = flagEncoder.getDouble(edge.getFlags(), KEY); double bendiness = flagEncoder.getDouble(edge.getFlags(), MotorcycleFlagEncoder.CURVATURE_KEY); double speed = getRoadSpeed(edge, reverse); @@ -58,14 +54,12 @@ public double calcWeight( EdgeIteratorState edge, boolean reverse, int prevOrNex return (bendiness * regularWeight) / (0.5 + priority); } - protected double getRoadSpeed( EdgeIteratorState edge, boolean reverse ) - { + protected double getRoadSpeed(EdgeIteratorState edge, boolean reverse) { return reverse ? flagEncoder.getReverseSpeed(edge.getFlags()) : flagEncoder.getSpeed(edge.getFlags()); } @Override - public String getName() - { + public String getName() { return "curvature"; } } diff --git a/core/src/main/java/com/graphhopper/routing/weighting/FastestWeighting.java b/core/src/main/java/com/graphhopper/routing/weighting/FastestWeighting.java index ce8a2e7dd73..0e013f57283 100644 --- a/core/src/main/java/com/graphhopper/routing/weighting/FastestWeighting.java +++ b/core/src/main/java/com/graphhopper/routing/weighting/FastestWeighting.java @@ -29,8 +29,7 @@ * * @author Peter Karich */ -public class FastestWeighting extends AbstractWeighting -{ +public class FastestWeighting extends AbstractWeighting { /** * Converting to seconds is not necessary but makes adding other penalties easier (e.g. turn * costs or traffic light costs etc) @@ -39,27 +38,23 @@ public class FastestWeighting extends AbstractWeighting private final double headingPenalty; private final double maxSpeed; - public FastestWeighting( FlagEncoder encoder, PMap pMap ) - { + public FastestWeighting(FlagEncoder encoder, PMap pMap) { super(encoder); headingPenalty = pMap.getDouble(Routing.HEADING_PENALTY, Routing.DEFAULT_HEADING_PENALTY); maxSpeed = encoder.getMaxSpeed() / SPEED_CONV; } - public FastestWeighting( FlagEncoder encoder ) - { + public FastestWeighting(FlagEncoder encoder) { this(encoder, new PMap(0)); } @Override - public double getMinWeight( double distance ) - { + public double getMinWeight(double distance) { return distance / maxSpeed; } @Override - public double calcWeight( EdgeIteratorState edge, boolean reverse, int prevOrNextEdgeId ) - { + public double calcWeight(EdgeIteratorState edge, boolean reverse, int prevOrNextEdgeId) { double speed = reverse ? flagEncoder.getReverseSpeed(edge.getFlags()) : flagEncoder.getSpeed(edge.getFlags()); if (speed == 0) return Double.POSITIVE_INFINITY; @@ -75,8 +70,7 @@ public double calcWeight( EdgeIteratorState edge, boolean reverse, int prevOrNex } @Override - public String getName() - { + public String getName() { return "fastest"; } } diff --git a/core/src/main/java/com/graphhopper/routing/weighting/GenericWeighting.java b/core/src/main/java/com/graphhopper/routing/weighting/GenericWeighting.java index 6eee41c8341..a9657656093 100644 --- a/core/src/main/java/com/graphhopper/routing/weighting/GenericWeighting.java +++ b/core/src/main/java/com/graphhopper/routing/weighting/GenericWeighting.java @@ -28,8 +28,7 @@ * * @author Peter Karich */ -public class GenericWeighting extends AbstractWeighting -{ +public class GenericWeighting extends AbstractWeighting { /** * Converting to seconds is not necessary but makes adding other penalties easier (e.g. turn * costs or traffic light costs etc) @@ -41,16 +40,14 @@ public class GenericWeighting extends AbstractWeighting private final double[] speedArray; private final int accessType; - public GenericWeighting( DataFlagEncoder encoder, ConfigMap cMap ) - { + public GenericWeighting(DataFlagEncoder encoder, ConfigMap cMap) { super(encoder); gEncoder = encoder; headingPenalty = cMap.getDouble(Routing.HEADING_PENALTY, Routing.DEFAULT_HEADING_PENALTY); speedArray = gEncoder.getHighwaySpeedMap(cMap.getMap("highways", Double.class)); double tmpSpeed = 0; - for (double speed : speedArray) - { + for (double speed : speedArray) { if (speed > tmpSpeed) tmpSpeed = speed; } @@ -62,22 +59,18 @@ public GenericWeighting( DataFlagEncoder encoder, ConfigMap cMap ) } @Override - public double getMinWeight( double distance ) - { + public double getMinWeight(double distance) { return distance / maxSpeed; } @Override - public double calcWeight( EdgeIteratorState edge, boolean reverse, int prevOrNextEdgeId ) - { + public double calcWeight(EdgeIteratorState edge, boolean reverse, int prevOrNextEdgeId) { // handle oneways and removed edges via subnetwork removal (existing and allowed highway tags but 'island' edges) - if (reverse) - { + if (reverse) { if (!gEncoder.isBackward(edge, accessType)) return Double.POSITIVE_INFINITY; - } else - if (!gEncoder.isForward(edge, accessType)) - return Double.POSITIVE_INFINITY; + } else if (!gEncoder.isForward(edge, accessType)) + return Double.POSITIVE_INFINITY; // TODO to avoid expensive reverse flags include oneway accessibility // but how to include e.g. maxspeed as it depends on direction? Does highway depend on direction? @@ -108,15 +101,13 @@ public double calcWeight( EdgeIteratorState edge, boolean reverse, int prevOrNex // TODO avoid a certain (or multiple) bounding boxes (less efficient for just a few edges) or a list of edgeIDs (not good for large areas) // bbox.contains(nodeAccess.getLatitude(edge.getBaseNode()), nodeAccess.getLongitude(edge.getBaseNode())) time+=avoidPenalty; - // TODO surfaces can reduce average speed // TODO prefer or avoid bike and hike routes return time; } @Override - public String getName() - { + public String getName() { return "generic"; } } diff --git a/core/src/main/java/com/graphhopper/routing/weighting/PriorityWeighting.java b/core/src/main/java/com/graphhopper/routing/weighting/PriorityWeighting.java index 3def76d6620..91fe0183ede 100644 --- a/core/src/main/java/com/graphhopper/routing/weighting/PriorityWeighting.java +++ b/core/src/main/java/com/graphhopper/routing/weighting/PriorityWeighting.java @@ -24,37 +24,33 @@ /** * Special weighting for (motor)bike *

+ * * @author Peter Karich */ -public class PriorityWeighting extends FastestWeighting -{ +public class PriorityWeighting extends FastestWeighting { /** * For now used only in BikeCommonFlagEncoder, FootEncoder and MotorcycleFlagEncoder */ public static final int KEY = 101; private final double minFactor; - public PriorityWeighting( FlagEncoder encoder ) - { + public PriorityWeighting(FlagEncoder encoder) { this(encoder, new PMap(0)); } - public PriorityWeighting( FlagEncoder encoder, PMap pMap ) - { + public PriorityWeighting(FlagEncoder encoder, PMap pMap) { super(encoder, pMap); double maxPriority = 1; // BEST / BEST minFactor = 1 / (0.5 + maxPriority); } @Override - public double getMinWeight( double distance ) - { + public double getMinWeight(double distance) { return minFactor * super.getMinWeight(distance); } @Override - public double calcWeight( EdgeIteratorState edgeState, boolean reverse, int prevOrNextEdgeId ) - { + public double calcWeight(EdgeIteratorState edgeState, boolean reverse, int prevOrNextEdgeId) { double weight = super.calcWeight(edgeState, reverse, prevOrNextEdgeId); if (Double.isInfinite(weight)) return Double.POSITIVE_INFINITY; diff --git a/core/src/main/java/com/graphhopper/routing/weighting/ShortFastestWeighting.java b/core/src/main/java/com/graphhopper/routing/weighting/ShortFastestWeighting.java index f50a86bf1ff..f70852e5ede 100644 --- a/core/src/main/java/com/graphhopper/routing/weighting/ShortFastestWeighting.java +++ b/core/src/main/java/com/graphhopper/routing/weighting/ShortFastestWeighting.java @@ -27,8 +27,7 @@ * * @author Peter Karich */ -public class ShortFastestWeighting extends FastestWeighting -{ +public class ShortFastestWeighting extends FastestWeighting { // For now keep parameters local within class private static final String NAME = "short_fastest"; private static final String TIME_FACTOR = "short_fastest.time_factor"; @@ -36,8 +35,7 @@ public class ShortFastestWeighting extends FastestWeighting private final double distanceFactor; private final double timeFactor; - public ShortFastestWeighting( FlagEncoder encoder, PMap pMap ) - { + public ShortFastestWeighting(FlagEncoder encoder, PMap pMap) { super(encoder); timeFactor = checkBounds(TIME_FACTOR, pMap.getDouble(TIME_FACTOR, 1)); @@ -49,34 +47,29 @@ public ShortFastestWeighting( FlagEncoder encoder, PMap pMap ) throw new IllegalArgumentException("[" + NAME + "] one of distance_factor or time_factor has to be non-zero"); } - public ShortFastestWeighting( FlagEncoder encoder, double distanceFactor ) - { + public ShortFastestWeighting(FlagEncoder encoder, double distanceFactor) { super(encoder); this.distanceFactor = checkBounds(DISTANCE_FACTOR, distanceFactor); this.timeFactor = 1; } @Override - public double getMinWeight( double distance ) - { + public double getMinWeight(double distance) { return super.getMinWeight(distance * distanceFactor); } @Override - public double calcWeight( EdgeIteratorState edge, boolean reverse, int prevOrNextEdgeId ) - { + public double calcWeight(EdgeIteratorState edge, boolean reverse, int prevOrNextEdgeId) { double time = super.calcWeight(edge, reverse, prevOrNextEdgeId); return time * timeFactor + edge.getDistance() * distanceFactor; } @Override - public String getName() - { + public String getName() { return NAME; } - private double checkBounds( String key, double val ) - { + private double checkBounds(String key, double val) { if (val < 0 || val > 10) throw new IllegalArgumentException(key + " has invalid range should be within [0, 10]"); diff --git a/core/src/main/java/com/graphhopper/routing/weighting/ShortestWeighting.java b/core/src/main/java/com/graphhopper/routing/weighting/ShortestWeighting.java index 5cbadfe264c..f45caea13c5 100644 --- a/core/src/main/java/com/graphhopper/routing/weighting/ShortestWeighting.java +++ b/core/src/main/java/com/graphhopper/routing/weighting/ShortestWeighting.java @@ -24,30 +24,26 @@ * Calculates the shortest route - independent of a vehicle as the calculation is based on the * distance only. *

+ * * @author Peter Karich */ -public class ShortestWeighting extends AbstractWeighting -{ - public ShortestWeighting( FlagEncoder flagEncoder ) - { +public class ShortestWeighting extends AbstractWeighting { + public ShortestWeighting(FlagEncoder flagEncoder) { super(flagEncoder); } @Override - public double getMinWeight( double currDistToGoal ) - { + public double getMinWeight(double currDistToGoal) { return currDistToGoal; } @Override - public double calcWeight( EdgeIteratorState edgeState, boolean reverse, int prevOrNextEdgeId ) - { + public double calcWeight(EdgeIteratorState edgeState, boolean reverse, int prevOrNextEdgeId) { return edgeState.getDistance(); } @Override - public String getName() - { + public String getName() { return "shortest"; } } diff --git a/core/src/main/java/com/graphhopper/routing/weighting/TurnWeighting.java b/core/src/main/java/com/graphhopper/routing/weighting/TurnWeighting.java index ab54e207fe5..ae2b89d0d91 100644 --- a/core/src/main/java/com/graphhopper/routing/weighting/TurnWeighting.java +++ b/core/src/main/java/com/graphhopper/routing/weighting/TurnWeighting.java @@ -20,7 +20,6 @@ import com.graphhopper.routing.util.FlagEncoder; import com.graphhopper.routing.util.HintsMap; import com.graphhopper.routing.util.TurnCostEncoder; -import com.graphhopper.routing.weighting.Weighting; import com.graphhopper.storage.TurnCostExtension; import com.graphhopper.util.EdgeIterator; import com.graphhopper.util.EdgeIteratorState; @@ -28,11 +27,11 @@ /** * Provides methods to retrieve turn costs for a specific turn. *

+ * * @author Karl Hübner * @author Peter Karich */ -public class TurnWeighting implements Weighting -{ +public class TurnWeighting implements Weighting { /** * Encoder, which decodes the turn flags */ @@ -44,8 +43,7 @@ public class TurnWeighting implements Weighting /** * @param turnCostExt the turn cost storage to be used */ - public TurnWeighting( Weighting superWeighting, TurnCostEncoder encoder, TurnCostExtension turnCostExt ) - { + public TurnWeighting(Weighting superWeighting, TurnCostEncoder encoder, TurnCostExtension turnCostExt) { this.turnCostEncoder = encoder; this.superWeighting = superWeighting; this.turnCostExt = turnCostExt; @@ -59,21 +57,18 @@ public TurnWeighting( Weighting superWeighting, TurnCostEncoder encoder, TurnCos * Set the default cost for an u-turn in seconds. Default is 40s. Should be that high to avoid * 'tricking' other turn costs or restrictions. */ - public TurnWeighting setDefaultUTurnCost( double costInSeconds ) - { + public TurnWeighting setDefaultUTurnCost(double costInSeconds) { this.defaultUTurnCost = costInSeconds; return this; } @Override - public double getMinWeight( double distance ) - { + public double getMinWeight(double distance) { return superWeighting.getMinWeight(distance); } @Override - public double calcWeight( EdgeIteratorState edgeState, boolean reverse, int prevOrNextEdgeId ) - { + public double calcWeight(EdgeIteratorState edgeState, boolean reverse, int prevOrNextEdgeId) { double weight = superWeighting.calcWeight(edgeState, reverse, prevOrNextEdgeId); if (prevOrNextEdgeId == EdgeIterator.NO_EDGE) return weight; @@ -91,8 +86,7 @@ public double calcWeight( EdgeIteratorState edgeState, boolean reverse, int prev return weight + turnCosts; } - public double calcTurnWeight( int edgeFrom, int nodeVia, int edgeTo ) - { + public double calcTurnWeight(int edgeFrom, int nodeVia, int edgeTo) { long turnFlags = turnCostExt.getTurnCostFlags(edgeFrom, nodeVia, edgeTo); if (turnCostEncoder.isTurnRestricted(turnFlags)) return Double.POSITIVE_INFINITY; @@ -101,21 +95,18 @@ public double calcTurnWeight( int edgeFrom, int nodeVia, int edgeTo ) } @Override - public FlagEncoder getFlagEncoder() - { + public FlagEncoder getFlagEncoder() { return superWeighting.getFlagEncoder(); } @Override - public boolean matches( HintsMap weightingMap ) - { + public boolean matches(HintsMap weightingMap) { // TODO without 'turn' in comparison return superWeighting.matches(weightingMap); } @Override - public String getName() - { + public String getName() { return "turn|" + superWeighting.getName(); } } diff --git a/core/src/main/java/com/graphhopper/routing/weighting/WeightApproximator.java b/core/src/main/java/com/graphhopper/routing/weighting/WeightApproximator.java index ae22e785144..f0fd1948246 100644 --- a/core/src/main/java/com/graphhopper/routing/weighting/WeightApproximator.java +++ b/core/src/main/java/com/graphhopper/routing/weighting/WeightApproximator.java @@ -21,16 +21,16 @@ * Specifies a weight approximation between an node and the goalNode according to the specified * weighting. *

+ * * @author jansoe */ -public interface WeightApproximator -{ +public interface WeightApproximator { /** * @return minimal weight fromNode to the goalNode */ - double approximate( int fromNode ); + double approximate(int fromNode); - void setGoalNode( int to ); + void setGoalNode(int to); /** * makes a deep copy of itself diff --git a/core/src/main/java/com/graphhopper/routing/weighting/Weighting.java b/core/src/main/java/com/graphhopper/routing/weighting/Weighting.java index 1bd574567b4..1b29050f3ac 100644 --- a/core/src/main/java/com/graphhopper/routing/weighting/Weighting.java +++ b/core/src/main/java/com/graphhopper/routing/weighting/Weighting.java @@ -24,27 +24,28 @@ /** * Specifies how the best route is calculated. E.g. the fastest or shortest route. *

+ * * @author Peter Karich */ -public interface Weighting -{ +public interface Weighting { /** * Used only for the heuristic estimation in A *

+ * * @return minimal weight. E.g. if you calculate the fastest way it is distance/maxVelocity */ - double getMinWeight( double distance ); + double getMinWeight(double distance); /** - * @param edgeState the edge for which the weight should be calculated - * @param reverse if the specified edge is specified in reverse direction e.g. from the reverse - * case of a bidirectional search. + * @param edgeState the edge for which the weight should be calculated + * @param reverse if the specified edge is specified in reverse direction e.g. from the reverse + * case of a bidirectional search. * @param prevOrNextEdgeId if reverse is false this has to be the previous edgeId, if true it - * has to be the next edgeId in the direction from start to end. + * has to be the next edgeId in the direction from start to end. * @return the calculated weight with the specified velocity has to be in the range of 0 and * +Infinity. Make sure your method does not return NaN which can e.g. occur for 0/0. */ - double calcWeight( EdgeIteratorState edgeState, boolean reverse, int prevOrNextEdgeId ); + double calcWeight(EdgeIteratorState edgeState, boolean reverse, int prevOrNextEdgeId); FlagEncoder getFlagEncoder(); @@ -53,5 +54,5 @@ public interface Weighting /** * Returns true if the specified weighting and encoder matches to this Weighting. */ - boolean matches( HintsMap map ); + boolean matches(HintsMap map); } diff --git a/core/src/main/java/com/graphhopper/search/Geocoding.java b/core/src/main/java/com/graphhopper/search/Geocoding.java index 3f941375534..b8eb605d444 100644 --- a/core/src/main/java/com/graphhopper/search/Geocoding.java +++ b/core/src/main/java/com/graphhopper/search/Geocoding.java @@ -24,12 +24,12 @@ /** * Interface to convert from place names to points. *

+ * * @author Peter Karich */ -public interface Geocoding -{ +public interface Geocoding { /** * Returns a list of matching points for the specified place query string. */ - List names2places( GHPlace... place ); + List names2places(GHPlace... place); } diff --git a/core/src/main/java/com/graphhopper/search/NameIndex.java b/core/src/main/java/com/graphhopper/search/NameIndex.java index d63dc36e2fd..cbd4dc11a2f 100644 --- a/core/src/main/java/com/graphhopper/search/NameIndex.java +++ b/core/src/main/java/com/graphhopper/search/NameIndex.java @@ -29,8 +29,7 @@ * @author Ottavio Campana * @author Peter Karich */ -public class NameIndex implements Storable -{ +public class NameIndex implements Storable { private static final Logger logger = LoggerFactory.getLogger(NameIndex.class); private static final long START_POINTER = 1; private final DataAccess names; @@ -39,23 +38,19 @@ public class NameIndex implements Storable private String lastName; private long lastIndex; - public NameIndex( Directory dir ) - { + public NameIndex(Directory dir) { names = dir.find("names"); } @Override - public NameIndex create( long cap ) - { + public NameIndex create(long cap) { names.create(cap); return this; } @Override - public boolean loadExisting() - { - if (names.loadExisting()) - { + public boolean loadExisting() { + if (names.loadExisting()) { bytePointer = BitUtil.LITTLE.combineIntsToLong(names.getHeader(0), names.getHeader(4)); return true; } @@ -66,21 +61,17 @@ public boolean loadExisting() /** * @return the byte pointer to the name */ - public long put( String name ) - { - if (name == null || name.isEmpty()) - { + public long put(String name) { + if (name == null || name.isEmpty()) { return 0; } - if (name.equals(lastName)) - { + if (name.equals(lastName)) { return lastIndex; } byte[] bytes = getBytes(name); long oldPointer = bytePointer; names.ensureCapacity(bytePointer + 1 + bytes.length); - byte[] sizeBytes = new byte[] - { + byte[] sizeBytes = new byte[]{ (byte) bytes.length }; names.setBytes(bytePointer, sizeBytes, sizeBytes.length); @@ -92,15 +83,12 @@ public long put( String name ) return oldPointer; } - private byte[] getBytes( String name ) - { + private byte[] getBytes(String name) { byte[] bytes = null; - for (int i = 0; i < 2; i++) - { + for (int i = 0; i < 2; i++) { bytes = name.getBytes(Helper.UTF_CS); // we have to store the size of the array into *one* byte - if (bytes.length > 255) - { + if (bytes.length > 255) { String newName = name.substring(0, 256 / 4); logger.info("Way name is too long: " + name + " truncated to " + newName); name = newName; @@ -108,16 +96,14 @@ private byte[] getBytes( String name ) } break; } - if (bytes.length > 255) - { + if (bytes.length > 255) { // really make sure no such problem exists throw new IllegalStateException("Way name is too long: " + name); } return bytes; } - public String get( long pointer ) - { + public String get(long pointer) { if (pointer < 0) throw new IllegalStateException("Pointer to access NameIndex cannot be negative:" + pointer); @@ -134,38 +120,32 @@ public String get( long pointer ) } @Override - public void flush() - { + public void flush() { names.setHeader(0, BitUtil.LITTLE.getIntLow(bytePointer)); names.setHeader(4, BitUtil.LITTLE.getIntHigh(bytePointer)); names.flush(); } @Override - public void close() - { + public void close() { names.close(); } @Override - public boolean isClosed() - { + public boolean isClosed() { return names.isClosed(); } - public void setSegmentSize( int segments ) - { + public void setSegmentSize(int segments) { names.setSegmentSize(segments); } @Override - public long getCapacity() - { + public long getCapacity() { return names.getCapacity(); } - public void copyTo( NameIndex nameIndex ) - { + public void copyTo(NameIndex nameIndex) { names.copyTo(nameIndex.names); } } diff --git a/core/src/main/java/com/graphhopper/search/ReverseGeocoding.java b/core/src/main/java/com/graphhopper/search/ReverseGeocoding.java index b32f1e1f5e8..a0770791ec6 100644 --- a/core/src/main/java/com/graphhopper/search/ReverseGeocoding.java +++ b/core/src/main/java/com/graphhopper/search/ReverseGeocoding.java @@ -24,12 +24,12 @@ /** * Interface to convert from points to place names or node ids. *

+ * * @author Peter Karich */ -public interface ReverseGeocoding -{ +public interface ReverseGeocoding { /** * Tries to retrieve a locational string from the specified points (list of lat,lon). */ - List places2names( GHPlace... points ); + List places2names(GHPlace... points); } diff --git a/core/src/main/java/com/graphhopper/storage/AbstractDataAccess.java b/core/src/main/java/com/graphhopper/storage/AbstractDataAccess.java index e8dea943ba5..0f895b4b1a5 100644 --- a/core/src/main/java/com/graphhopper/storage/AbstractDataAccess.java +++ b/core/src/main/java/com/graphhopper/storage/AbstractDataAccess.java @@ -28,25 +28,23 @@ /** * @author Peter Karich */ -public abstract class AbstractDataAccess implements DataAccess -{ +public abstract class AbstractDataAccess implements DataAccess { protected static final int SEGMENT_SIZE_MIN = 1 << 7; - private static final int SEGMENT_SIZE_DEFAULT = 1 << 20; // reserve some space for downstream usage (in classes using/extending this) protected static final int HEADER_OFFSET = 20 * 4 + 20; protected static final byte[] EMPTY = new byte[1024]; - protected int header[] = new int[(HEADER_OFFSET - 20) / 4]; + private static final int SEGMENT_SIZE_DEFAULT = 1 << 20; + protected final ByteOrder byteOrder; + protected final BitUtil bitUtil; private final String location; + protected int header[] = new int[(HEADER_OFFSET - 20) / 4]; protected String name; protected int segmentSizeInBytes = SEGMENT_SIZE_DEFAULT; protected transient int segmentSizePower; protected transient int indexDivisor; - protected final ByteOrder byteOrder; - protected final BitUtil bitUtil; protected transient boolean closed = false; - public AbstractDataAccess( String name, String location, ByteOrder order ) - { + public AbstractDataAccess(String name, String location, ByteOrder order) { byteOrder = order; bitUtil = BitUtil.get(order); this.name = name; @@ -57,38 +55,32 @@ public AbstractDataAccess( String name, String location, ByteOrder order ) } @Override - public String getName() - { + public String getName() { return name; } - protected String getFullName() - { + protected String getFullName() { return location + name; } @Override - public void close() - { + public void close() { closed = true; } @Override - public boolean isClosed() - { + public boolean isClosed() { return closed; } @Override - public void setHeader( int bytePos, int value ) - { + public void setHeader(int bytePos, int value) { bytePos >>= 2; header[bytePos] = value; } @Override - public int getHeader( int bytePos ) - { + public int getHeader(int bytePos) { bytePos >>= 2; return header[bytePos]; } @@ -96,20 +88,17 @@ public int getHeader( int bytePos ) /** * Writes some internal data into the beginning of the specified file. */ - protected void writeHeader( RandomAccessFile file, long length, int segmentSize ) throws IOException - { + protected void writeHeader(RandomAccessFile file, long length, int segmentSize) throws IOException { file.seek(0); file.writeUTF("GH"); file.writeLong(length); file.writeInt(segmentSize); - for (int i = 0; i < header.length; i++) - { + for (int i = 0; i < header.length; i++) { file.writeInt(header[i]); } } - protected long readHeader( RandomAccessFile raFile ) throws IOException - { + protected long readHeader(RandomAccessFile raFile) throws IOException { raFile.seek(0); if (raFile.length() == 0) return -1; @@ -120,24 +109,20 @@ protected long readHeader( RandomAccessFile raFile ) throws IOException long bytes = raFile.readLong(); setSegmentSize(raFile.readInt()); - for (int i = 0; i < header.length; i++) - { + for (int i = 0; i < header.length; i++) { header[i] = raFile.readInt(); } return bytes; } - protected void copyHeader( DataAccess da ) - { - for (int h = 0; h < header.length * 4; h += 4) - { + protected void copyHeader(DataAccess da) { + for (int h = 0; h < header.length * 4; h += 4) { da.setHeader(h, getHeader(h)); } } @Override - public DataAccess copyTo( DataAccess da ) - { + public DataAccess copyTo(DataAccess da) { copyHeader(da); da.ensureCapacity(getCapacity()); long cap = getCapacity(); @@ -145,29 +130,22 @@ public DataAccess copyTo( DataAccess da ) int segSize = Math.min(da.getSegmentSize(), getSegmentSize()); byte[] bytes = new byte[segSize]; boolean externalIntBased = ((AbstractDataAccess) da).isIntBased(); - for (long bytePos = 0; bytePos < cap; bytePos += segSize) - { + for (long bytePos = 0; bytePos < cap; bytePos += segSize) { // read - if (isIntBased()) - { - for (int offset = 0; offset < segSize; offset += 4) - { + if (isIntBased()) { + for (int offset = 0; offset < segSize; offset += 4) { bitUtil.fromInt(bytes, getInt(bytePos + offset), offset); } - } else - { + } else { getBytes(bytePos, bytes, segSize); } // write - if (externalIntBased) - { - for (int offset = 0; offset < segSize; offset += 4) - { + if (externalIntBased) { + for (int offset = 0; offset < segSize; offset += 4) { da.setInt(bytePos + offset, bitUtil.toInt(bytes, offset)); } - } else - { + } else { da.setBytes(bytePos, bytes, segSize); } } @@ -175,10 +153,8 @@ public DataAccess copyTo( DataAccess da ) } @Override - public DataAccess setSegmentSize( int bytes ) - { - if (bytes > 0) - { + public DataAccess setSegmentSize(int bytes) { + if (bytes > 0) { // segment size should be a power of 2 int tmp = (int) (Math.log(bytes) / Math.log(2)); segmentSizeInBytes = Math.max((int) Math.pow(2, tmp), SEGMENT_SIZE_MIN); @@ -189,44 +165,35 @@ public DataAccess setSegmentSize( int bytes ) } @Override - public int getSegmentSize() - { + public int getSegmentSize() { return segmentSizeInBytes; } @Override - public String toString() - { + public String toString() { return getFullName(); } @Override - public void rename( String newName ) - { + public void rename(String newName) { File file = new File(location + name); - if (file.exists()) - { - try - { - if (!file.renameTo(new File(location + newName))) - { + if (file.exists()) { + try { + if (!file.renameTo(new File(location + newName))) { throw new IllegalStateException("Couldn't rename this " + getType() + " object to " + newName); } name = newName; - } catch (Exception ex) - { + } catch (Exception ex) { throw new IllegalStateException("Couldn't rename this " + getType() + " object!", ex); } - } else - { + } else { throw new IllegalStateException("File does not exist!? " + getFullName() + " Make sure that you flushed before renaming. Otherwise it could make problems" + " for memory mapped DataAccess objects"); } } - protected boolean checkBeforeRename( String newName ) - { + protected boolean checkBeforeRename(String newName) { if (Helper.isEmpty(newName)) throw new IllegalArgumentException("newName mustn't be empty!"); @@ -239,13 +206,11 @@ protected boolean checkBeforeRename( String newName ) return true; } - public boolean isStoring() - { + public boolean isStoring() { return true; } - protected boolean isIntBased() - { + protected boolean isIntBased() { return false; } } diff --git a/core/src/main/java/com/graphhopper/storage/BaseGraph.java b/core/src/main/java/com/graphhopper/storage/BaseGraph.java index e1db33f4341..ac19abc5451 100644 --- a/core/src/main/java/com/graphhopper/storage/BaseGraph.java +++ b/core/src/main/java/com/graphhopper/storage/BaseGraph.java @@ -26,10 +26,10 @@ import com.graphhopper.routing.util.FlagEncoder; import com.graphhopper.search.NameIndex; import com.graphhopper.util.*; -import static com.graphhopper.util.Helper.nf; - import com.graphhopper.util.shapes.BBox; +import static com.graphhopper.util.Helper.nf; + /** * The base graph handles nodes and edges file format. It can be used with different Directory * implementations like RAMDirectory for fast access or via MMapDirectory for virtual-memory and not @@ -41,52 +41,50 @@ * Life cycle: (1) object creation, (2) configuration via setters & getters, (3) create or * loadExisting, (4) usage, (5) flush, (6) close */ -class BaseGraph implements Graph -{ - // edge memory layout not found in EdgeAccess: - int E_GEO, E_NAME, E_ADDITIONAL; - /** - * Specifies how many entries (integers) are used per edge. - */ - int edgeEntryBytes; - private boolean initialized = false; +class BaseGraph implements Graph { final DataAccess edges; + final DataAccess nodes; + final BBox bounds; + final NodeAccess nodeAccess; + final GraphExtension extStorage; + final NameIndex nameIndex; + final BitUtil bitUtil; + final EncodingManager encodingManager; + final EdgeAccess edgeAccess; + // length | nodeA | nextNode | ... | nodeB + // as we use integer index in 'egdes' area => 'geometry' area is limited to 4GB (we use pos&neg values!) + private final DataAccess wayGeometry; + private final Directory dir; + private final InternalGraphEventListener listener; /** * interval [0,n) */ protected int edgeCount; // node memory layout: protected int N_EDGE_REF, N_LAT, N_LON, N_ELE, N_ADDITIONAL; + // edge memory layout not found in EdgeAccess: + int E_GEO, E_NAME, E_ADDITIONAL; + /** + * Specifies how many entries (integers) are used per edge. + */ + int edgeEntryBytes; /** * Specifies how many entries (integers) are used per node */ int nodeEntryBytes; - final DataAccess nodes; + private boolean initialized = false; /** * interval [0,n) */ private int nodeCount; - final BBox bounds; // remove markers are not yet persistent! private GHBitSet removedNodes; private int edgeEntryIndex, nodeEntryIndex; - final NodeAccess nodeAccess; - final GraphExtension extStorage; - // length | nodeA | nextNode | ... | nodeB - // as we use integer index in 'egdes' area => 'geometry' area is limited to 4GB (we use pos&neg values!) - private final DataAccess wayGeometry; private long maxGeoRef; - final NameIndex nameIndex; - final BitUtil bitUtil; - private final Directory dir; - final EncodingManager encodingManager; - private final InternalGraphEventListener listener; private boolean frozen = false; - final EdgeAccess edgeAccess; - public BaseGraph( Directory dir, final EncodingManager encodingManager, boolean withElevation, - InternalGraphEventListener listener, GraphExtension extendedStorage ) - { + public BaseGraph(Directory dir, final EncodingManager encodingManager, boolean withElevation, + InternalGraphEventListener listener, GraphExtension extendedStorage) { this.dir = dir; this.encodingManager = encodingManager; this.bitUtil = BitUtil.get(dir.getByteOrder()); @@ -95,54 +93,45 @@ public BaseGraph( Directory dir, final EncodingManager encodingManager, boolean this.nodes = dir.find("nodes"); this.edges = dir.find("edges"); this.listener = listener; - this.edgeAccess = new EdgeAccess(edges, bitUtil) - { + this.edgeAccess = new EdgeAccess(edges, bitUtil) { @Override - final EdgeIterable createSingleEdge( EdgeFilter filter ) - { + final EdgeIterable createSingleEdge(EdgeFilter filter) { return new EdgeIterable(BaseGraph.this, this, filter); } @Override - final int getEdgeRef( int nodeId ) - { + final int getEdgeRef(int nodeId) { return nodes.getInt((long) nodeId * nodeEntryBytes + N_EDGE_REF); } @Override - final void setEdgeRef( int nodeId, int edgeId ) - { + final void setEdgeRef(int nodeId, int edgeId) { nodes.setInt((long) nodeId * nodeEntryBytes + N_EDGE_REF, edgeId); } @Override - final int getEntryBytes() - { + final int getEntryBytes() { return edgeEntryBytes; } @Override - final long toPointer( int edgeId ) - { + final long toPointer(int edgeId) { assert isInBounds(edgeId) : "edgeId " + edgeId + " not in bounds [0," + edgeCount + ")"; return (long) edgeId * edgeEntryBytes; } @Override - final boolean isInBounds( int edgeId ) - { + final boolean isInBounds(int edgeId) { return edgeId < edgeCount && edgeId >= 0; } @Override - final long reverseFlags( long edgePointer, long flags ) - { + final long reverseFlags(long edgePointer, long flags) { return encodingManager.reverseFlags(flags); } @Override - public String toString() - { + public String toString() { return "base edge access"; } }; @@ -152,21 +141,24 @@ public String toString() this.extStorage.init(this, dir); } + private static boolean isTestingEnabled() { + boolean enableIfAssert = false; + assert (enableIfAssert = true) : true; + return enableIfAssert; + } + @Override - public Graph getBaseGraph() - { + public Graph getBaseGraph() { return this; } - void checkInit() - { + void checkInit() { if (initialized) throw new IllegalStateException("You cannot configure this GraphStorage " + "after calling create or loadExisting. Calling one of the methods twice is also not allowed."); } - protected int loadNodesHeader() - { + protected int loadNodesHeader() { nodeEntryBytes = nodes.getHeader(1 * 4); nodeCount = nodes.getHeader(2 * 4); bounds.minLon = Helper.intToDegree(nodes.getHeader(3 * 4)); @@ -174,8 +166,7 @@ protected int loadNodesHeader() bounds.minLat = Helper.intToDegree(nodes.getHeader(5 * 4)); bounds.maxLat = Helper.intToDegree(nodes.getHeader(6 * 4)); - if (bounds.hasElevation()) - { + if (bounds.hasElevation()) { bounds.minEle = Helper.intToEle(nodes.getHeader(7 * 4)); bounds.maxEle = Helper.intToEle(nodes.getHeader(8 * 4)); } @@ -184,16 +175,14 @@ protected int loadNodesHeader() return 10; } - protected int setNodesHeader() - { + protected int setNodesHeader() { nodes.setHeader(1 * 4, nodeEntryBytes); nodes.setHeader(2 * 4, nodeCount); nodes.setHeader(3 * 4, Helper.degreeToInt(bounds.minLon)); nodes.setHeader(4 * 4, Helper.degreeToInt(bounds.maxLon)); nodes.setHeader(5 * 4, Helper.degreeToInt(bounds.minLat)); nodes.setHeader(6 * 4, Helper.degreeToInt(bounds.maxLat)); - if (bounds.hasElevation()) - { + if (bounds.hasElevation()) { nodes.setHeader(7 * 4, Helper.eleToInt(bounds.minEle)); nodes.setHeader(8 * 4, Helper.eleToInt(bounds.maxEle)); } @@ -202,15 +191,13 @@ protected int setNodesHeader() return 10; } - protected int loadEdgesHeader() - { + protected int loadEdgesHeader() { edgeEntryBytes = edges.getHeader(0 * 4); edgeCount = edges.getHeader(1 * 4); return 5; } - protected int setEdgesHeader() - { + protected int setEdgesHeader() { edges.setHeader(0, edgeEntryBytes); edges.setHeader(1 * 4, edgeCount); edges.setHeader(2 * 4, encodingManager.hashCode()); @@ -218,21 +205,18 @@ protected int setEdgesHeader() return 5; } - protected int loadWayGeometryHeader() - { + protected int loadWayGeometryHeader() { maxGeoRef = bitUtil.combineIntsToLong(wayGeometry.getHeader(0), wayGeometry.getHeader(4)); return 1; } - protected int setWayGeometryHeader() - { + protected int setWayGeometryHeader() { wayGeometry.setHeader(0, bitUtil.getIntLow(maxGeoRef)); wayGeometry.setHeader(4, bitUtil.getIntHigh(maxGeoRef)); return 1; } - void initStorage() - { + void initStorage() { edgeEntryIndex = 0; nodeEntryIndex = 0; boolean flagsSizeIsLong = encodingManager.getBytesForFlags() == 8; @@ -272,37 +256,30 @@ void initStorage() /** * Initializes the node area with the empty edge value and default additional value. */ - void initNodeRefs( long oldCapacity, long newCapacity ) - { - for (long pointer = oldCapacity + N_EDGE_REF; pointer < newCapacity; pointer += nodeEntryBytes) - { + void initNodeRefs(long oldCapacity, long newCapacity) { + for (long pointer = oldCapacity + N_EDGE_REF; pointer < newCapacity; pointer += nodeEntryBytes) { nodes.setInt(pointer, EdgeIterator.NO_EDGE); } - if (extStorage.isRequireNodeField()) - { - for (long pointer = oldCapacity + N_ADDITIONAL; pointer < newCapacity; pointer += nodeEntryBytes) - { + if (extStorage.isRequireNodeField()) { + for (long pointer = oldCapacity + N_ADDITIONAL; pointer < newCapacity; pointer += nodeEntryBytes) { nodes.setInt(pointer, extStorage.getDefaultNodeFieldValue()); } } } - protected final int nextEdgeEntryIndex( int sizeInBytes ) - { + protected final int nextEdgeEntryIndex(int sizeInBytes) { int tmp = edgeEntryIndex; edgeEntryIndex += sizeInBytes; return tmp; } - protected final int nextNodeEntryIndex( int sizeInBytes ) - { + protected final int nextNodeEntryIndex(int sizeInBytes) { int tmp = nodeEntryIndex; nodeEntryIndex += sizeInBytes; return tmp; } - protected final void initNodeAndEdgeEntrySize() - { + protected final void initNodeAndEdgeEntrySize() { nodeEntryBytes = nodeEntryIndex; edgeEntryBytes = edgeEntryIndex; } @@ -311,8 +288,7 @@ protected final void initNodeAndEdgeEntrySize() * Check if byte capacity of DataAcess nodes object is sufficient to include node index, else * extend byte capacity */ - final void ensureNodeIndex( int nodeIndex ) - { + final void ensureNodeIndex(int nodeIndex) { if (!initialized) throw new AssertionError("The graph has not yet been initialized."); @@ -322,39 +298,33 @@ final void ensureNodeIndex( int nodeIndex ) long oldNodes = nodeCount; nodeCount = nodeIndex + 1; boolean capacityIncreased = nodes.ensureCapacity((long) nodeCount * nodeEntryBytes); - if (capacityIncreased) - { + if (capacityIncreased) { long newBytesCapacity = nodes.getCapacity(); initNodeRefs(oldNodes * nodeEntryBytes, newBytesCapacity); } } @Override - public int getNodes() - { + public int getNodes() { return nodeCount; } @Override - public NodeAccess getNodeAccess() - { + public NodeAccess getNodeAccess() { return nodeAccess; } @Override - public BBox getBounds() - { + public BBox getBounds() { return bounds; } @Override - public EdgeIteratorState edge( int a, int b, double distance, boolean bothDirection ) - { + public EdgeIteratorState edge(int a, int b, double distance, boolean bothDirection) { return edge(a, b).setDistance(distance).setFlags(encodingManager.flagsDefault(true, bothDirection)); } - void setSegmentSize( int bytes ) - { + void setSegmentSize(int bytes) { checkInit(); nodes.setSegmentSize(bytes); edges.setSegmentSize(bytes); @@ -363,8 +333,7 @@ void setSegmentSize( int bytes ) extStorage.setSegmentSize(bytes); } - synchronized void freeze() - { + synchronized void freeze() { if (isFrozen()) throw new IllegalStateException("base graph already frozen"); @@ -372,19 +341,16 @@ synchronized void freeze() listener.freeze(); } - synchronized boolean isFrozen() - { + synchronized boolean isFrozen() { return frozen; } - public void checkFreeze() - { + public void checkFreeze() { if (isFrozen()) throw new IllegalStateException("Cannot add edge or node after baseGraph.freeze was called"); } - void create( long initSize ) - { + void create(long initSize) { nodes.create(initSize); edges.create(initSize); wayGeometry.create(initSize); @@ -397,8 +363,7 @@ void create( long initSize ) initNodeRefs(0, nodes.getCapacity()); } - String toDetailsString() - { + String toDetailsString() { return "edges:" + nf(edgeCount) + "(" + edges.getCapacity() / Helper.MB + "MB), " + "nodes:" + nf(getNodes()) + "(" + nodes.getCapacity() / Helper.MB + "MB), " + "name:(" + nameIndex.getCapacity() / Helper.MB + "MB), " @@ -406,8 +371,7 @@ String toDetailsString() + "bounds:" + bounds; } - void flush() - { + void flush() { setNodesHeader(); setEdgesHeader(); setWayGeometryHeader(); @@ -419,8 +383,7 @@ void flush() extStorage.flush(); } - void close() - { + void close() { wayGeometry.close(); nameIndex.close(); edges.close(); @@ -428,19 +391,16 @@ void close() extStorage.close(); } - long getCapacity() - { + long getCapacity() { return edges.getCapacity() + nodes.getCapacity() + nameIndex.getCapacity() + wayGeometry.getCapacity() + extStorage.getCapacity(); } - long getMaxGeoRef() - { + long getMaxGeoRef() { return maxGeoRef; } - void loadExisting( String dim ) - { + void loadExisting(String dim) { if (!nodes.loadExisting()) throw new IllegalStateException("Cannot load nodes. corrupt file or directory? " + dir); @@ -472,8 +432,7 @@ void loadExisting( String dim ) /** * @return to */ - EdgeIteratorState copyProperties( CommonEdgeIterator from, EdgeIteratorState to ) - { + EdgeIteratorState copyProperties(CommonEdgeIterator from, EdgeIteratorState to) { to.setDistance(from.getDistance()). setName(from.getName()). setFlags(from.getDirectFlags()). @@ -487,11 +446,11 @@ EdgeIteratorState copyProperties( CommonEdgeIterator from, EdgeIteratorState to /** * Create edge between nodes a and b *

+ * * @return EdgeIteratorState of newly created edge */ @Override - public EdgeIteratorState edge( int nodeA, int nodeB ) - { + public EdgeIteratorState edge(int nodeA, int nodeB) { if (isFrozen()) throw new IllegalStateException("Cannot create edge if graph is already frozen"); @@ -507,18 +466,17 @@ public EdgeIteratorState edge( int nodeA, int nodeB ) } // for test only - void setEdgeCount( int cnt ) - { + void setEdgeCount(int cnt) { edgeCount = cnt; } /** * Determine next free edgeId and ensure byte capacity to store edge *

+ * * @return next free edgeId */ - protected int nextEdgeId() - { + protected int nextEdgeId() { int nextEdge = edgeCount; edgeCount++; if (edgeCount < 0) @@ -529,54 +487,45 @@ protected int nextEdgeId() } @Override - public EdgeIteratorState getEdgeIteratorState( int edgeId, int adjNode ) - { + public EdgeIteratorState getEdgeIteratorState(int edgeId, int adjNode) { if (!edgeAccess.isInBounds(edgeId)) throw new IllegalStateException("edgeId " + edgeId + " out of bounds"); checkAdjNodeBounds(adjNode); return edgeAccess.getEdgeProps(edgeId, adjNode); } - final void checkAdjNodeBounds( int adjNode ) - { + final void checkAdjNodeBounds(int adjNode) { if (adjNode < 0 && adjNode != Integer.MIN_VALUE || adjNode >= nodeCount) throw new IllegalStateException("adjNode " + adjNode + " out of bounds [0," + nf(nodeCount) + ")"); } @Override - public EdgeExplorer createEdgeExplorer( EdgeFilter filter ) - { + public EdgeExplorer createEdgeExplorer(EdgeFilter filter) { return new EdgeIterable(this, edgeAccess, filter); } @Override - public EdgeExplorer createEdgeExplorer() - { + public EdgeExplorer createEdgeExplorer() { return createEdgeExplorer(EdgeFilter.ALL_EDGES); } @Override - public AllEdgesIterator getAllEdges() - { + public AllEdgesIterator getAllEdges() { return new AllEdgeIterator(this, edgeAccess); } @Override - public Graph copyTo( Graph g ) - { + public Graph copyTo(Graph g) { initialized = true; - if (g.getClass().equals(getClass())) - { + if (g.getClass().equals(getClass())) { _copyTo((BaseGraph) g); return g; - } else - { + } else { return GHUtility.copyTo(this, g); } } - void _copyTo( BaseGraph clonedG ) - { + void _copyTo(BaseGraph clonedG) { if (clonedG.edgeEntryBytes != edgeEntryBytes) throw new IllegalStateException("edgeEntryBytes cannot be different for cloned graph. " + "Cloned: " + clonedG.edgeEntryBytes + " vs " + edgeEntryBytes); @@ -616,8 +565,7 @@ void _copyTo( BaseGraph clonedG ) clonedG.removedNodes = removedNodes.copyTo(new GHBitSetImpl()); } - protected void trimToSize() - { + protected void trimToSize() { long nodeCap = (long) nodeCount * nodeEntryBytes; nodes.trimTo(nodeCap); // long edgeCap = (long) (edgeCount + 1) * edgeEntrySize; @@ -629,9 +577,8 @@ protected void trimToSize() * moves the last nodes into the deleted nodes, where it needs to update the node ids in every * edge. */ - void inPlaceNodeRemove( int removeNodeCount ) - { - // Prepare edge-update of nodes which are connected to deleted nodes + void inPlaceNodeRemove(int removeNodeCount) { + // Prepare edge-update of nodes which are connected to deleted nodes int toMoveNodes = getNodes(); int itemsToMove = 0; @@ -641,20 +588,17 @@ void inPlaceNodeRemove( int removeNodeCount ) removedNodes.copyTo(toRemoveSet); EdgeExplorer delExplorer = createEdgeExplorer(EdgeFilter.ALL_EDGES); - // create map of old node ids pointing to new ids + // create map of old node ids pointing to new ids for (int removeNode = removedNodes.next(0); - removeNode >= 0; - removeNode = removedNodes.next(removeNode + 1)) - { + removeNode >= 0; + removeNode = removedNodes.next(removeNode + 1)) { EdgeIterator delEdgesIter = delExplorer.setBaseNode(removeNode); - while (delEdgesIter.next()) - { + while (delEdgesIter.next()) { toRemoveSet.add(delEdgesIter.getAdjNode()); } toMoveNodes--; - for (; toMoveNodes >= 0; toMoveNodes--) - { + for (; toMoveNodes >= 0; toMoveNodes--) { if (!removedNodes.contains(toMoveNodes)) break; } @@ -669,24 +613,20 @@ void inPlaceNodeRemove( int removeNodeCount ) // now similar process to disconnectEdges but only for specific nodes // all deleted nodes could be connected to existing. remove the connections for (int removeNode = toRemoveSet.next(0); - removeNode >= 0; - removeNode = toRemoveSet.next(removeNode + 1)) - { + removeNode >= 0; + removeNode = toRemoveSet.next(removeNode + 1)) { // remove all edges connected to the deleted nodes adjNodesToDelIter.setBaseNode(removeNode); long prev = EdgeIterator.NO_EDGE; - while (adjNodesToDelIter.next()) - { + while (adjNodesToDelIter.next()) { int nodeId = adjNodesToDelIter.getAdjNode(); // already invalidated - if (nodeId != EdgeAccess.NO_NODE && removedNodes.contains(nodeId)) - { + if (nodeId != EdgeAccess.NO_NODE && removedNodes.contains(nodeId)) { int edgeToRemove = adjNodesToDelIter.getEdge(); long edgeToRemovePointer = edgeAccess.toPointer(edgeToRemove); edgeAccess.internalEdgeDisconnect(edgeToRemove, prev, removeNode, nodeId); edgeAccess.invalidateEdge(edgeToRemovePointer); - } else - { + } else { prev = adjNodesToDelIter.edgePointer; } } @@ -695,12 +635,10 @@ void inPlaceNodeRemove( int removeNodeCount ) GHBitSet toMoveSet = new GHBitSetImpl(removeNodeCount * 3); EdgeExplorer movedEdgeExplorer = createEdgeExplorer(); // marks connected nodes to rewrite the edges - for (int i = 0; i < itemsToMove; i++) - { + for (int i = 0; i < itemsToMove; i++) { int oldI = oldToNewMap.keyAt(i); EdgeIterator movedEdgeIter = movedEdgeExplorer.setBaseNode(oldI); - while (movedEdgeIter.next()) - { + while (movedEdgeIter.next()) { int nodeId = movedEdgeIter.getAdjNode(); if (nodeId == EdgeAccess.NO_NODE) continue; @@ -714,14 +652,12 @@ void inPlaceNodeRemove( int removeNodeCount ) } // move nodes into deleted nodes - for (int i = 0; i < itemsToMove; i++) - { + for (int i = 0; i < itemsToMove; i++) { int oldI = oldToNewMap.keyAt(i); int newI = oldToNewMap.valueAt(i); long newOffset = (long) newI * nodeEntryBytes; long oldOffset = (long) oldI * nodeEntryBytes; - for (long j = 0; j < nodeEntryBytes; j += 4) - { + for (long j = 0; j < nodeEntryBytes; j += 4) { nodes.setInt(newOffset + j, nodes.getInt(oldOffset + j)); } } @@ -730,14 +666,13 @@ void inPlaceNodeRemove( int removeNodeCount ) // go through all edges and pick the necessary <- this is easier to implement than // a more efficient (?) breadth-first search EdgeIterator iter = getAllEdges(); - while (iter.next()) - { + while (iter.next()) { int nodeA = iter.getBaseNode(); int nodeB = iter.getAdjNode(); if (!toMoveSet.contains(nodeA) && !toMoveSet.contains(nodeB)) continue; - // now overwrite exiting edge with new node ids + // now overwrite exiting edge with new node ids // also flags and links could have changed due to different node order int updatedA = oldToNewMap.get(nodeA); if (updatedA < 0) @@ -766,11 +701,9 @@ void inPlaceNodeRemove( int removeNodeCount ) EdgeExplorer explorer = createEdgeExplorer(); // health check - if (isTestingEnabled()) - { + if (isTestingEnabled()) { iter = getAllEdges(); - while (iter.next()) - { + while (iter.next()) { int base = iter.getBaseNode(); int adj = iter.getAdjNode(); String str = iter.getEdge() @@ -785,18 +718,14 @@ void inPlaceNodeRemove( int removeNodeCount ) if (base >= nodeCount) throw new RuntimeException("Base node problem with edge " + str); - try - { + try { explorer.setBaseNode(adj).toString(); - } catch (Exception ex) - { + } catch (Exception ex) { org.slf4j.LoggerFactory.getLogger(getClass()).error("adj:" + adj); } - try - { + try { explorer.setBaseNode(base).toString(); - } catch (Exception ex) - { + } catch (Exception ex) { org.slf4j.LoggerFactory.getLogger(getClass()).error("base:" + base); } } @@ -807,23 +736,19 @@ void inPlaceNodeRemove( int removeNodeCount ) } @Override - public GraphExtension getExtension() - { + public GraphExtension getExtension() { return extStorage; } - public void setAdditionalEdgeField( long edgePointer, int value ) - { + public void setAdditionalEdgeField(long edgePointer, int value) { if (extStorage.isRequireEdgeField() && E_ADDITIONAL >= 0) edges.setInt(edgePointer + E_ADDITIONAL, value); else throw new AssertionError("This graph does not support an additional edge field."); } - private void setWayGeometry_( PointList pillarNodes, long edgePointer, boolean reverse ) - { - if (pillarNodes != null && !pillarNodes.isEmpty()) - { + private void setWayGeometry_(PointList pillarNodes, long edgePointer, boolean reverse) { + if (pillarNodes != null && !pillarNodes.isEmpty()) { if (pillarNodes.getDimension() != nodeAccess.getDimension()) throw new IllegalArgumentException("Cannot use pointlist which is " + pillarNodes.getDimension() + "D for graph which is " + nodeAccess.getDimension() + "D"); @@ -832,11 +757,9 @@ private void setWayGeometry_( PointList pillarNodes, long edgePointer, boolean r int len = pillarNodes.getSize(); int dim = nodeAccess.getDimension(); - if (existingGeoRef > 0) - { + if (existingGeoRef > 0) { final int count = wayGeometry.getInt(existingGeoRef * 4L); - if (len <= count) - { + if (len <= count) { setWayGeometryAtGeoRef(pillarNodes, edgePointer, reverse, existingGeoRef); return; } @@ -844,14 +767,12 @@ private void setWayGeometry_( PointList pillarNodes, long edgePointer, boolean r long nextGeoRef = nextGeoRef(len * dim); setWayGeometryAtGeoRef(pillarNodes, edgePointer, reverse, nextGeoRef); - } else - { + } else { edges.setInt(edgePointer + E_GEO, 0); } } - private void setWayGeometryAtGeoRef( PointList pillarNodes, long edgePointer, boolean reverse, long geoRef ) - { + private void setWayGeometryAtGeoRef(PointList pillarNodes, long edgePointer, boolean reverse, long geoRef) { int len = pillarNodes.getSize(); int dim = nodeAccess.getDimension(); long geoRefPosition = (long) geoRef * 4; @@ -862,8 +783,7 @@ private void setWayGeometryAtGeoRef( PointList pillarNodes, long edgePointer, bo edges.setInt(edgePointer + E_GEO, Helper.toSignedInt(geoRef)); } - private byte[] createWayGeometryBytes( PointList pillarNodes, boolean reverse ) - { + private byte[] createWayGeometryBytes(PointList pillarNodes, boolean reverse) { int len = pillarNodes.getSize(); int dim = nodeAccess.getDimension(); int totalLen = len * dim * 4 + 4; @@ -874,16 +794,14 @@ private byte[] createWayGeometryBytes( PointList pillarNodes, boolean reverse ) int tmpOffset = 4; boolean is3D = nodeAccess.is3D(); - for (int i = 0; i < len; i++) - { + for (int i = 0; i < len; i++) { double lat = pillarNodes.getLatitude(i); bitUtil.fromInt(bytes, Helper.degreeToInt(lat), tmpOffset); tmpOffset += 4; bitUtil.fromInt(bytes, Helper.degreeToInt(pillarNodes.getLongitude(i)), tmpOffset); tmpOffset += 4; - if (is3D) - { + if (is3D) { bitUtil.fromInt(bytes, Helper.eleToInt(pillarNodes.getElevation(i)), tmpOffset); tmpOffset += 4; } @@ -891,13 +809,11 @@ private byte[] createWayGeometryBytes( PointList pillarNodes, boolean reverse ) return bytes; } - private PointList fetchWayGeometry_( long edgePointer, boolean reverse, int mode, int baseNode, int adjNode ) - { + private PointList fetchWayGeometry_(long edgePointer, boolean reverse, int mode, int baseNode, int adjNode) { long geoRef = Helper.toUnsignedLong(edges.getInt(edgePointer + E_GEO)); int count = 0; byte[] bytes = null; - if (geoRef > 0) - { + if (geoRef > 0) { geoRef *= 4L; count = wayGeometry.getInt(geoRef); @@ -908,50 +824,38 @@ private PointList fetchWayGeometry_( long edgePointer, boolean reverse, int mode return PointList.EMPTY; PointList pillarNodes = new PointList(count + mode, nodeAccess.is3D()); - if (reverse) - { + if (reverse) { if ((mode & 2) != 0) pillarNodes.add(nodeAccess, adjNode); - } else - { - if ((mode & 1) != 0) - pillarNodes.add(nodeAccess, baseNode); - } + } else if ((mode & 1) != 0) + pillarNodes.add(nodeAccess, baseNode); int index = 0; - for (int i = 0; i < count; i++) - { + for (int i = 0; i < count; i++) { double lat = Helper.intToDegree(bitUtil.toInt(bytes, index)); index += 4; double lon = Helper.intToDegree(bitUtil.toInt(bytes, index)); index += 4; - if (nodeAccess.is3D()) - { + if (nodeAccess.is3D()) { pillarNodes.add(lat, lon, Helper.intToEle(bitUtil.toInt(bytes, index))); index += 4; - } else - { + } else { pillarNodes.add(lat, lon); } } - if (reverse) - { + if (reverse) { if ((mode & 1) != 0) pillarNodes.add(nodeAccess, baseNode); - + pillarNodes.reverse(); - } else - { - if ((mode & 2) != 0) - pillarNodes.add(nodeAccess, adjNode); - } + } else if ((mode & 2) != 0) + pillarNodes.add(nodeAccess, adjNode); return pillarNodes; } - private void setName( long edgePointer, String name ) - { + private void setName(long edgePointer, String name) { int nameIndexRef = (int) nameIndex.put(name); if (nameIndexRef < 0) throw new IllegalStateException("Too many names are stored, currently limited to int pointer"); @@ -959,28 +863,18 @@ private void setName( long edgePointer, String name ) edges.setInt(edgePointer + E_NAME, nameIndexRef); } - GHBitSet getRemovedNodes() - { + GHBitSet getRemovedNodes() { if (removedNodes == null) removedNodes = new GHBitSetImpl(getNodes()); return removedNodes; } - private static boolean isTestingEnabled() - { - boolean enableIfAssert = false; - assert (enableIfAssert = true) : true; - return enableIfAssert; - } - - private void ensureGeometry( long bytePos, int byteLength ) - { + private void ensureGeometry(long bytePos, int byteLength) { wayGeometry.ensureCapacity(bytePos + byteLength); } - private long nextGeoRef( int arrayLength ) - { + private long nextGeoRef(int arrayLength) { long tmp = maxGeoRef; maxGeoRef += arrayLength + 1L; if (maxGeoRef >= 0xFFFFffffL) @@ -989,13 +883,11 @@ private long nextGeoRef( int arrayLength ) return tmp; } - protected static class EdgeIterable extends CommonEdgeIterator implements EdgeExplorer, EdgeIterator - { + protected static class EdgeIterable extends CommonEdgeIterator implements EdgeExplorer, EdgeIterator { final EdgeFilter filter; int nextEdgeId; - public EdgeIterable( BaseGraph baseGraph, EdgeAccess edgeAccess, EdgeFilter filter ) - { + public EdgeIterable(BaseGraph baseGraph, EdgeAccess edgeAccess, EdgeFilter filter) { super(-1, edgeAccess, baseGraph); if (filter == null) @@ -1003,16 +895,13 @@ public EdgeIterable( BaseGraph baseGraph, EdgeAccess edgeAccess, EdgeFilter filt this.filter = filter; } - final void setEdgeId( int edgeId ) - { + final void setEdgeId(int edgeId) { this.nextEdgeId = this.edgeId = edgeId; } - final boolean init( int tmpEdgeId, int expectedAdjNode ) - { + final boolean init(int tmpEdgeId, int expectedAdjNode) { setEdgeId(tmpEdgeId); - if (tmpEdgeId != EdgeIterator.NO_EDGE) - { + if (tmpEdgeId != EdgeIterator.NO_EDGE) { selectEdgeAccess(); this.edgePointer = edgeAccess.toPointer(tmpEdgeId); } @@ -1025,12 +914,10 @@ final boolean init( int tmpEdgeId, int expectedAdjNode ) adjNode = edgeAccess.edges.getInt(edgePointer + edgeAccess.E_NODEB); // a next() call should return false nextEdgeId = EdgeIterator.NO_EDGE; - if (expectedAdjNode == adjNode || expectedAdjNode == Integer.MIN_VALUE) - { + if (expectedAdjNode == adjNode || expectedAdjNode == Integer.MIN_VALUE) { reverse = false; return true; - } else if (expectedAdjNode == baseNode) - { + } else if (expectedAdjNode == baseNode) { reverse = true; baseNode = adjNode; adjNode = expectedAdjNode; @@ -1039,29 +926,24 @@ final boolean init( int tmpEdgeId, int expectedAdjNode ) return false; } - final void _setBaseNode( int baseNode ) - { + final void _setBaseNode(int baseNode) { this.baseNode = baseNode; } @Override - public EdgeIterator setBaseNode( int baseNode ) - { + public EdgeIterator setBaseNode(int baseNode) { // always use base graph edge access setEdgeId(baseGraph.edgeAccess.getEdgeRef(baseNode)); _setBaseNode(baseNode); return this; } - protected void selectEdgeAccess() - { + protected void selectEdgeAccess() { } @Override - public final boolean next() - { - while (true) - { + public final boolean next() { + while (true) { if (nextEdgeId == EdgeIterator.NO_EDGE) return false; @@ -1083,15 +965,13 @@ public final boolean next() } @Override - public EdgeIteratorState detach( boolean reverseArg ) - { + public EdgeIteratorState detach(boolean reverseArg) { if (edgeId == nextEdgeId || edgeId == EdgeIterator.NO_EDGE) throw new IllegalStateException("call next before detaching or setEdgeId (edgeId:" + edgeId + " vs. next " + nextEdgeId + ")"); EdgeIterable iter = edgeAccess.createSingleEdge(filter); boolean ret; - if (reverseArg) - { + if (reverseArg) { ret = iter.init(edgeId, baseNode); // for #162 iter.reverse = !reverse; @@ -1105,29 +985,23 @@ public EdgeIteratorState detach( boolean reverseArg ) /** * Include all edges of this storage in the iterator. */ - protected static class AllEdgeIterator extends CommonEdgeIterator implements AllEdgesIterator - { - public AllEdgeIterator( BaseGraph baseGraph ) - { + protected static class AllEdgeIterator extends CommonEdgeIterator implements AllEdgesIterator { + public AllEdgeIterator(BaseGraph baseGraph) { this(baseGraph, baseGraph.edgeAccess); } - private AllEdgeIterator( BaseGraph baseGraph, EdgeAccess edgeAccess ) - { + private AllEdgeIterator(BaseGraph baseGraph, EdgeAccess edgeAccess) { super(-1, edgeAccess, baseGraph); } @Override - public int getMaxId() - { + public int getMaxId() { return baseGraph.edgeCount; } @Override - public boolean next() - { - while (true) - { + public boolean next() { + while (true) { edgeId++; edgePointer = (long) edgeId * edgeAccess.getEntryBytes(); if (!checkRange()) @@ -1146,27 +1020,23 @@ public boolean next() } } - protected boolean checkRange() - { + protected boolean checkRange() { return edgeId < baseGraph.edgeCount; } @Override - public final EdgeIteratorState detach( boolean reverseArg ) - { + public final EdgeIteratorState detach(boolean reverseArg) { if (edgePointer < 0) throw new IllegalStateException("call next before detaching"); AllEdgeIterator iter = new AllEdgeIterator(baseGraph, edgeAccess); iter.edgeId = edgeId; iter.edgePointer = edgePointer; - if (reverseArg) - { + if (reverseArg) { iter.reverse = !this.reverse; iter.baseNode = adjNode; iter.adjNode = baseNode; - } else - { + } else { iter.reverse = this.reverse; iter.baseNode = baseNode; iter.adjNode = adjNode; @@ -1178,55 +1048,47 @@ public final EdgeIteratorState detach( boolean reverseArg ) /** * Common private super class for AllEdgesIteratorImpl and EdgeIterable */ - static abstract class CommonEdgeIterator implements EdgeIteratorState - { + static abstract class CommonEdgeIterator implements EdgeIteratorState { + final BaseGraph baseGraph; protected long edgePointer; protected int baseNode; protected int adjNode; - // we need reverse if detach is called - boolean reverse = false; protected EdgeAccess edgeAccess; - final BaseGraph baseGraph; + // we need reverse if detach is called + boolean reverse = false; boolean freshFlags; - private long cachedFlags; int edgeId = -1; + private long cachedFlags; - public CommonEdgeIterator( long edgePointer, EdgeAccess edgeAccess, BaseGraph baseGraph ) - { + public CommonEdgeIterator(long edgePointer, EdgeAccess edgeAccess, BaseGraph baseGraph) { this.edgePointer = edgePointer; this.edgeAccess = edgeAccess; this.baseGraph = baseGraph; } @Override - public final int getBaseNode() - { + public final int getBaseNode() { return baseNode; } @Override - public final int getAdjNode() - { + public final int getAdjNode() { return adjNode; } @Override - public final double getDistance() - { + public final double getDistance() { return edgeAccess.getDist(edgePointer); } @Override - public final EdgeIteratorState setDistance( double dist ) - { + public final EdgeIteratorState setDistance(double dist) { edgeAccess.setDist(edgePointer, dist); return this; } - final long getDirectFlags() - { - if (!freshFlags) - { + final long getDirectFlags() { + if (!freshFlags) { cachedFlags = edgeAccess.getFlags_(edgePointer, reverse); freshFlags = true; } @@ -1234,14 +1096,12 @@ final long getDirectFlags() } @Override - public long getFlags() - { + public long getFlags() { return getDirectFlags(); } @Override - public final EdgeIteratorState setFlags( long fl ) - { + public final EdgeIteratorState setFlags(long fl) { edgeAccess.setFlags_(edgePointer, reverse, fl); cachedFlags = fl; freshFlags = true; @@ -1249,21 +1109,18 @@ public final EdgeIteratorState setFlags( long fl ) } @Override - public final int getAdditionalField() - { + public final int getAdditionalField() { return baseGraph.edges.getInt(edgePointer + baseGraph.E_ADDITIONAL); } @Override - public final EdgeIteratorState setAdditionalField( int value ) - { + public final EdgeIteratorState setAdditionalField(int value) { baseGraph.setAdditionalEdgeField(edgePointer, value); return this; } @Override - public final EdgeIteratorState copyPropertiesTo( EdgeIteratorState edge ) - { + public final EdgeIteratorState copyPropertiesTo(EdgeIteratorState edge) { return baseGraph.copyProperties(this, edge); } @@ -1271,8 +1128,7 @@ public final EdgeIteratorState copyPropertiesTo( EdgeIteratorState edge ) * Reports whether the edge is available in forward direction for the specified encoder. */ @Override - public boolean isForward( FlagEncoder encoder ) - { + public boolean isForward(FlagEncoder encoder) { return encoder.isForward(getDirectFlags()); } @@ -1280,54 +1136,46 @@ public boolean isForward( FlagEncoder encoder ) * Reports whether the edge is available in backward direction for the specified encoder. */ @Override - public boolean isBackward( FlagEncoder encoder ) - { + public boolean isBackward(FlagEncoder encoder) { return encoder.isBackward(getDirectFlags()); } @Override - public EdgeIteratorState setWayGeometry( PointList pillarNodes ) - { + public EdgeIteratorState setWayGeometry(PointList pillarNodes) { baseGraph.setWayGeometry_(pillarNodes, edgePointer, reverse); return this; } @Override - public PointList fetchWayGeometry( int mode ) - { + public PointList fetchWayGeometry(int mode) { return baseGraph.fetchWayGeometry_(edgePointer, reverse, mode, getBaseNode(), getAdjNode()); } @Override - public int getEdge() - { + public int getEdge() { return edgeId; } @Override - public String getName() - { + public String getName() { int nameIndexRef = baseGraph.edges.getInt(edgePointer + baseGraph.E_NAME); return baseGraph.nameIndex.get(nameIndexRef); } @Override - public EdgeIteratorState setName( String name ) - { + public EdgeIteratorState setName(String name) { baseGraph.setName(edgePointer, name); return this; } @Override - public final boolean getBool( int key, boolean _default ) - { + public final boolean getBool(int key, boolean _default) { // for non-existent keys return default return _default; } @Override - public final String toString() - { + public final String toString() { return getEdge() + " " + getBaseNode() + "-" + getAdjNode(); } } diff --git a/core/src/main/java/com/graphhopper/storage/CHGraph.java b/core/src/main/java/com/graphhopper/storage/CHGraph.java index e1468785456..b1cad636289 100644 --- a/core/src/main/java/com/graphhopper/storage/CHGraph.java +++ b/core/src/main/java/com/graphhopper/storage/CHGraph.java @@ -27,36 +27,36 @@ * levels for a node and creating shortcuts, which are additional 'artificial' edges to speedup * traversal in certain cases. *

+ * * @author Peter Karich */ -public interface CHGraph extends Graph -{ +public interface CHGraph extends Graph { /** * This methods sets the level of the specified node. */ - void setLevel( int nodeId, int level ); + void setLevel(int nodeId, int level); /** * @return the level of the specified node. */ - int getLevel( int nodeId ); + int getLevel(int nodeId); - boolean isShortcut( int edgeId ); + boolean isShortcut(int edgeId); /** * This method creates a shortcut between a to b which is nearly identical to creating an edge * except that it can be excluded or included for certain traversals or algorithms. */ - CHEdgeIteratorState shortcut( int a, int b ); + CHEdgeIteratorState shortcut(int a, int b); @Override - CHEdgeIteratorState getEdgeIteratorState( int edgeId, int endNode ); + CHEdgeIteratorState getEdgeIteratorState(int edgeId, int endNode); @Override CHEdgeExplorer createEdgeExplorer(); @Override - CHEdgeExplorer createEdgeExplorer( EdgeFilter filter ); + CHEdgeExplorer createEdgeExplorer(EdgeFilter filter); @Override AllCHEdgesIterator getAllEdges(); diff --git a/core/src/main/java/com/graphhopper/storage/CHGraphImpl.java b/core/src/main/java/com/graphhopper/storage/CHGraphImpl.java index 82aa164e4e7..b153604ff19 100644 --- a/core/src/main/java/com/graphhopper/storage/CHGraphImpl.java +++ b/core/src/main/java/com/graphhopper/storage/CHGraphImpl.java @@ -17,47 +17,48 @@ */ package com.graphhopper.storage; -import com.graphhopper.routing.weighting.Weighting; -import com.graphhopper.routing.weighting.AbstractWeighting; import com.graphhopper.routing.ch.PrepareEncoder; -import com.graphhopper.routing.util.*; +import com.graphhopper.routing.util.AllCHEdgesIterator; +import com.graphhopper.routing.util.EdgeFilter; +import com.graphhopper.routing.util.FlagEncoder; +import com.graphhopper.routing.weighting.AbstractWeighting; +import com.graphhopper.routing.weighting.Weighting; import com.graphhopper.storage.BaseGraph.AllEdgeIterator; import com.graphhopper.storage.BaseGraph.CommonEdgeIterator; import com.graphhopper.storage.BaseGraph.EdgeIterable; import com.graphhopper.util.*; -import static com.graphhopper.util.Helper.nf; import com.graphhopper.util.shapes.BBox; +import static com.graphhopper.util.Helper.nf; + /** * A Graph implementation necessary for Contraction Hierarchies. This class enables the storage to * hold the level of a node and shortcut edges per edge. *

+ * * @author Peter Karich */ -public class CHGraphImpl implements CHGraph, Storable -{ +public class CHGraphImpl implements CHGraph, Storable { private static final double WEIGHT_FACTOR = 1000f; // 2 bits for access, for now only 32bit => not Long.MAX private static final long MAX_WEIGHT_LONG = (Integer.MAX_VALUE >> 2) << 2; private static final double MAX_WEIGHT = (Integer.MAX_VALUE >> 2) / WEIGHT_FACTOR; - private int N_LEVEL; - int N_CH_REF; - // shortcut memory layout is synced with edges indices until E_FLAGS, then: - private int S_SKIP_EDGE1, S_SKIP_EDGE2; - - int shortcutEntryBytes; - private int shortcutCount = 0; final DataAccess shortcuts; - // the nodesCH storage is limited via baseGraph.nodeCount too - int nodeCHEntryBytes; final DataAccess nodesCH; final long scDirMask = PrepareEncoder.getScDirMask(); private final BaseGraph baseGraph; private final EdgeAccess chEdgeAccess; private final Weighting weighting; + int N_CH_REF; + int shortcutEntryBytes; + // the nodesCH storage is limited via baseGraph.nodeCount too + int nodeCHEntryBytes; + private int N_LEVEL; + // shortcut memory layout is synced with edges indices until E_FLAGS, then: + private int S_SKIP_EDGE1, S_SKIP_EDGE2; + private int shortcutCount = 0; - CHGraphImpl( Weighting w, Directory dir, final BaseGraph baseGraph ) - { + CHGraphImpl(Weighting w, Directory dir, final BaseGraph baseGraph) { if (w == null) throw new IllegalStateException("Weighting for CHGraph cannot be null"); @@ -66,49 +67,41 @@ public class CHGraphImpl implements CHGraph, Storable final String name = AbstractWeighting.weightingToFileName(w); this.nodesCH = dir.find("nodes_ch_" + name); this.shortcuts = dir.find("shortcuts_" + name); - this.chEdgeAccess = new EdgeAccess(shortcuts, baseGraph.bitUtil) - { + this.chEdgeAccess = new EdgeAccess(shortcuts, baseGraph.bitUtil) { @Override - final EdgeIterable createSingleEdge( EdgeFilter edgeFilter ) - { + final EdgeIterable createSingleEdge(EdgeFilter edgeFilter) { return new CHEdgeIteratorImpl(baseGraph, this, edgeFilter); } @Override - final int getEdgeRef( int nodeId ) - { + final int getEdgeRef(int nodeId) { return nodesCH.getInt((long) nodeId * nodeCHEntryBytes + N_CH_REF); } @Override - final void setEdgeRef( int nodeId, int edgeId ) - { + final void setEdgeRef(int nodeId, int edgeId) { nodesCH.setInt((long) nodeId * nodeCHEntryBytes + N_CH_REF, edgeId); } @Override - final int getEntryBytes() - { + final int getEntryBytes() { return shortcutEntryBytes; } @Override - final long toPointer( int shortcutId ) - { + final long toPointer(int shortcutId) { assert isInBounds(shortcutId) : "shortcutId " + shortcutId + " not in bounds [" + baseGraph.edgeCount + ", " + (baseGraph.edgeCount + shortcutCount) + ")"; return (long) (shortcutId - baseGraph.edgeCount) * shortcutEntryBytes; } @Override - final boolean isInBounds( int shortcutId ) - { + final boolean isInBounds(int shortcutId) { int tmp = shortcutId - baseGraph.edgeCount; return tmp < shortcutCount && tmp >= 0; } @Override - final long reverseFlags( long edgePointer, long flags ) - { + final long reverseFlags(long edgePointer, long flags) { boolean isShortcut = edgePointer >= toPointer(baseGraph.edgeCount); if (!isShortcut) return baseGraph.edgeAccess.reverseFlags(edgePointer, flags); @@ -123,47 +116,40 @@ final long reverseFlags( long edgePointer, long flags ) } @Override - public String toString() - { + public String toString() { return "ch edge access " + name; } }; } - public final Weighting getWeighting() - { + public final Weighting getWeighting() { return weighting; } @Override - public boolean isShortcut( int edgeId ) - { + public boolean isShortcut(int edgeId) { assert baseGraph.isFrozen() : "level graph not yet frozen"; return edgeId >= baseGraph.edgeCount; } @Override - public final void setLevel( int nodeIndex, int level ) - { + public final void setLevel(int nodeIndex, int level) { checkNodeId(nodeIndex); nodesCH.setInt((long) nodeIndex * nodeCHEntryBytes + N_LEVEL, level); } @Override - public final int getLevel( int nodeIndex ) - { + public final int getLevel(int nodeIndex) { checkNodeId(nodeIndex); return nodesCH.getInt((long) nodeIndex * nodeCHEntryBytes + N_LEVEL); } - final void checkNodeId( int nodeId ) - { + final void checkNodeId(int nodeId) { assert nodeId < baseGraph.getNodes() : "node " + nodeId + " is invalid. Not in [0," + baseGraph.getNodes() + ")"; } @Override - public CHEdgeIteratorState shortcut( int a, int b ) - { + public CHEdgeIteratorState shortcut(int a, int b) { if (!baseGraph.isFrozen()) throw new IllegalStateException("Cannot create shortcut if graph is not yet frozen"); @@ -178,8 +164,7 @@ public CHEdgeIteratorState shortcut( int a, int b ) return iter; } - protected int nextShortcutId() - { + protected int nextShortcutId() { int nextSC = shortcutCount; shortcutCount++; if (shortcutCount < 0) @@ -190,14 +175,12 @@ protected int nextShortcutId() } @Override - public EdgeIteratorState edge( int a, int b, double distance, boolean bothDirections ) - { + public EdgeIteratorState edge(int a, int b, double distance, boolean bothDirections) { return edge(a, b).setDistance(distance).setFlags(baseGraph.encodingManager.flagsDefault(true, bothDirections)); } @Override - public CHEdgeIteratorState edge( int a, int b ) - { + public CHEdgeIteratorState edge(int a, int b) { // increase edge array not for shortcuts baseGraph.ensureNodeIndex(Math.max(a, b)); int edgeId = baseGraph.edgeAccess.internalEdgeAdd(baseGraph.nextEdgeId(), a, b); @@ -208,62 +191,50 @@ public CHEdgeIteratorState edge( int a, int b ) } @Override - public CHEdgeExplorer createEdgeExplorer() - { + public CHEdgeExplorer createEdgeExplorer() { return createEdgeExplorer(EdgeFilter.ALL_EDGES); } @Override - public CHEdgeExplorer createEdgeExplorer( EdgeFilter filter ) - { + public CHEdgeExplorer createEdgeExplorer(EdgeFilter filter) { return new CHEdgeIteratorImpl(baseGraph, chEdgeAccess, filter); } @Override - public final CHEdgeIteratorState getEdgeIteratorState( int edgeId, int endNode ) - { - if (isShortcut(edgeId)) - { + public final CHEdgeIteratorState getEdgeIteratorState(int edgeId, int endNode) { + if (isShortcut(edgeId)) { if (!chEdgeAccess.isInBounds(edgeId)) throw new IllegalStateException("shortcutId " + edgeId + " out of bounds"); - } else - { - if (!baseGraph.edgeAccess.isInBounds(edgeId)) - throw new IllegalStateException("edgeId " + edgeId + " out of bounds"); - } + } else if (!baseGraph.edgeAccess.isInBounds(edgeId)) + throw new IllegalStateException("edgeId " + edgeId + " out of bounds"); return (CHEdgeIteratorState) chEdgeAccess.getEdgeProps(edgeId, endNode); } @Override - public int getNodes() - { + public int getNodes() { return baseGraph.getNodes(); } @Override - public NodeAccess getNodeAccess() - { + public NodeAccess getNodeAccess() { return baseGraph.getNodeAccess(); } @Override - public BBox getBounds() - { + public BBox getBounds() { return baseGraph.getBounds(); } - void _freeze() - { + void _freeze() { long maxCapacity = ((long) getNodes()) * nodeCHEntryBytes; nodesCH.ensureCapacity(maxCapacity); long baseCapacity = baseGraph.nodes.getCapacity(); // copy normal edge refs into ch edge refs for (long pointer = N_CH_REF, basePointer = baseGraph.N_EDGE_REF; - pointer < maxCapacity; - pointer += nodeCHEntryBytes, basePointer += baseGraph.nodeEntryBytes) - { + pointer < maxCapacity; + pointer += nodeCHEntryBytes, basePointer += baseGraph.nodeEntryBytes) { if (basePointer >= baseCapacity) throw new IllegalStateException("Cannot copy edge refs into ch graph. " + "pointer:" + pointer + ", cap:" + maxCapacity + ", basePtr:" + basePointer + ", baseCap:" + baseCapacity); @@ -272,28 +243,175 @@ void _freeze() } } - String toDetailsString() - { + String toDetailsString() { return toString() + ", shortcuts:" + nf(shortcutCount) + ", nodesCH:(" + nodesCH.getCapacity() / Helper.MB + "MB)"; } - class CHEdgeIteratorImpl extends EdgeIterable implements CHEdgeExplorer, CHEdgeIterator - { - public CHEdgeIteratorImpl( BaseGraph baseGraph, EdgeAccess edgeAccess, EdgeFilter filter ) - { + /** + * Disconnects the edges (higher to lower node) via the specified edgeState pointing from lower to + * higher node. + *

+ * + * @param edgeState the edge from lower to higher + */ + public void disconnect(CHEdgeExplorer explorer, EdgeIteratorState edgeState) { + // search edge with opposite direction but we need to know the previousEdge for the internalEdgeDisconnect so we cannot simply do: + // EdgeIteratorState tmpIter = getEdgeProps(iter.getEdge(), iter.getBaseNode()); + CHEdgeIterator tmpIter = explorer.setBaseNode(edgeState.getAdjNode()); + int tmpPrevEdge = EdgeIterator.NO_EDGE; + while (tmpIter.next()) { + if (tmpIter.isShortcut() && tmpIter.getEdge() == edgeState.getEdge()) { + // TODO this is ugly, move this somehow into the underlying iteration logic + long edgePointer = tmpPrevEdge == EdgeIterator.NO_EDGE ? -1 + : isShortcut(tmpPrevEdge) ? chEdgeAccess.toPointer(tmpPrevEdge) : baseGraph.edgeAccess.toPointer(tmpPrevEdge); + chEdgeAccess.internalEdgeDisconnect(edgeState.getEdge(), edgePointer, + edgeState.getAdjNode(), edgeState.getBaseNode()); + break; + } + + tmpPrevEdge = tmpIter.getEdge(); + } + } + + @Override + public AllCHEdgesIterator getAllEdges() { + return new AllCHEdgesIteratorImpl(baseGraph); + } + + final void setWeight(CommonEdgeIterator edge, double weight) { + if (weight < 0) + throw new IllegalArgumentException("weight cannot be negative but was " + weight); + + long weightLong; + if (weight > MAX_WEIGHT) + weightLong = MAX_WEIGHT_LONG; + else + weightLong = ((long) (weight * WEIGHT_FACTOR)) << 2; + + long accessFlags = edge.getDirectFlags() & scDirMask; + edge.setFlags(weightLong | accessFlags); + } + + final double getWeight(CommonEdgeIterator edge) { + // no need for reverseFlags call (shortcut has identical weight if both dies) and also no need for 64bit + long flags32bit = edge.getDirectFlags(); + double weight = (flags32bit >>> 2) / WEIGHT_FACTOR; + if (weight >= MAX_WEIGHT) + return Double.POSITIVE_INFINITY; + + return weight; + } + + protected int loadEdgesHeader() { + shortcutCount = shortcuts.getHeader(0 * 4); + shortcutEntryBytes = shortcuts.getHeader(1 * 4); + return 3; + } + + protected int setEdgesHeader() { + shortcuts.setHeader(0 * 4, shortcutCount); + shortcuts.setHeader(1 * 4, shortcutEntryBytes); + return 3; + } + + @Override + public GraphExtension getExtension() { + return baseGraph.getExtension(); + } + + @Override + public Graph getBaseGraph() { + return baseGraph; + } + + @Override + public Graph copyTo(Graph g) { + CHGraphImpl tmpG = ((CHGraphImpl) g); + + nodesCH.copyTo(tmpG.nodesCH); + shortcuts.copyTo(tmpG.shortcuts); + + tmpG.N_LEVEL = N_LEVEL; + tmpG.N_CH_REF = N_CH_REF; + tmpG.nodeCHEntryBytes = nodeCHEntryBytes; + return g; + } + + void initStorage() { + EdgeAccess ea = baseGraph.edgeAccess; + chEdgeAccess.init(ea.E_NODEA, ea.E_NODEB, ea.E_LINKA, ea.E_LINKB, ea.E_DIST, ea.E_FLAGS, false); + // shortcuts + S_SKIP_EDGE1 = ea.E_FLAGS + 4; + S_SKIP_EDGE2 = S_SKIP_EDGE1 + 4; + shortcutEntryBytes = S_SKIP_EDGE2 + 4; + + // node based data: + N_LEVEL = 0; + N_CH_REF = N_LEVEL + 4; + nodeCHEntryBytes = N_CH_REF + 4; + } + + void setSegmentSize(int bytes) { + nodesCH.setSegmentSize(bytes); + shortcuts.setSegmentSize(bytes); + } + + @Override + public CHGraph create(long bytes) { + nodesCH.create(bytes); + shortcuts.create(bytes); + return this; + } + + @Override + public boolean loadExisting() { + if (!nodesCH.loadExisting() || !shortcuts.loadExisting()) + return false; + + loadEdgesHeader(); + return true; + } + + @Override + public void flush() { + nodesCH.flush(); + shortcuts.flush(); + } + + @Override + public void close() { + nodesCH.close(); + shortcuts.close(); + } + + @Override + public boolean isClosed() { + return nodesCH.isClosed(); + } + + @Override + public long getCapacity() { + return nodesCH.getCapacity() + shortcuts.getCapacity(); + } + + @Override + public String toString() { + return "CHGraph|" + getWeighting().toString(); + } + + class CHEdgeIteratorImpl extends EdgeIterable implements CHEdgeExplorer, CHEdgeIterator { + public CHEdgeIteratorImpl(BaseGraph baseGraph, EdgeAccess edgeAccess, EdgeFilter filter) { super(baseGraph, edgeAccess, filter); } @Override - public final long getFlags() - { + public final long getFlags() { checkShortcut(false, "getFlags"); return super.getDirectFlags(); } @Override - public final CHEdgeIterator setBaseNode( int baseNode ) - { + public final CHEdgeIterator setBaseNode(int baseNode) { assert baseGraph.isFrozen() : "Traversal CHGraph is only possible if BaseGraph is frozen"; // always use ch edge access @@ -303,11 +421,9 @@ public final CHEdgeIterator setBaseNode( int baseNode ) } @Override - public final void setSkippedEdges( int edge1, int edge2 ) - { + public final void setSkippedEdges(int edge1, int edge2) { checkShortcut(true, "setSkippedEdges"); - if (EdgeIterator.Edge.isValid(edge1) != EdgeIterator.Edge.isValid(edge2)) - { + if (EdgeIterator.Edge.isValid(edge1) != EdgeIterator.Edge.isValid(edge2)) { throw new IllegalStateException("Skipped edges of a shortcut needs " + "to be both valid or invalid but they were not " + edge1 + ", " + edge2); } @@ -316,29 +432,25 @@ public final void setSkippedEdges( int edge1, int edge2 ) } @Override - public final int getSkippedEdge1() - { + public final int getSkippedEdge1() { checkShortcut(true, "getSkippedEdge1"); return shortcuts.getInt(edgePointer + S_SKIP_EDGE1); } @Override - public final int getSkippedEdge2() - { + public final int getSkippedEdge2() { checkShortcut(true, "getSkippedEdge2"); return shortcuts.getInt(edgePointer + S_SKIP_EDGE2); } @Override - public final boolean isShortcut() - { + public final boolean isShortcut() { // assert baseGraph.isFrozen() : "chgraph not yet frozen"; return edgeId >= baseGraph.edgeCount; } @Override - public boolean isBackward( FlagEncoder encoder ) - { + public boolean isBackward(FlagEncoder encoder) { assert encoder == weighting.getFlagEncoder() : encoder + " vs. " + weighting.getFlagEncoder(); if (isShortcut()) return (getDirectFlags() & PrepareEncoder.getScBwdDir()) != 0; @@ -347,8 +459,7 @@ public boolean isBackward( FlagEncoder encoder ) } @Override - public boolean isForward( FlagEncoder encoder ) - { + public boolean isForward(FlagEncoder encoder) { assert encoder == weighting.getFlagEncoder() : encoder + " vs. " + weighting.getFlagEncoder(); if (isShortcut()) return (getDirectFlags() & PrepareEncoder.getScFwdDir()) != 0; @@ -357,23 +468,20 @@ public boolean isForward( FlagEncoder encoder ) } @Override - public final CHEdgeIteratorState setWeight( double weight ) - { + public final CHEdgeIteratorState setWeight(double weight) { checkShortcut(true, "setWeight"); CHGraphImpl.this.setWeight(this, weight); return this; } @Override - public final double getWeight() - { + public final double getWeight() { checkShortcut(true, "getWeight"); return CHGraphImpl.this.getWeight(this); } @Override - protected final void selectEdgeAccess() - { + protected final void selectEdgeAccess() { if (nextEdgeId < baseGraph.edgeCount) // iterate over edges edgeAccess = baseGraph.edgeAccess; @@ -382,98 +490,51 @@ protected final void selectEdgeAccess() edgeAccess = chEdgeAccess; } - public void checkShortcut( boolean shouldBeShortcut, String methodName ) - { - if (isShortcut()) - { + public void checkShortcut(boolean shouldBeShortcut, String methodName) { + if (isShortcut()) { if (!shouldBeShortcut) throw new IllegalStateException("Cannot call " + methodName + " on shortcut " + getEdge()); - } else - { - if (shouldBeShortcut) - throw new IllegalStateException("Method " + methodName + " only for shortcuts " + getEdge()); - } + } else if (shouldBeShortcut) + throw new IllegalStateException("Method " + methodName + " only for shortcuts " + getEdge()); } @Override - public final String getName() - { + public final String getName() { checkShortcut(false, "getName"); return super.getName(); } @Override - public final EdgeIteratorState setName( String name ) - { + public final EdgeIteratorState setName(String name) { checkShortcut(false, "setName"); return super.setName(name); } @Override - public final PointList fetchWayGeometry( int mode ) - { + public final PointList fetchWayGeometry(int mode) { checkShortcut(false, "fetchWayGeometry"); return super.fetchWayGeometry(mode); } @Override - public final EdgeIteratorState setWayGeometry( PointList list ) - { + public final EdgeIteratorState setWayGeometry(PointList list) { checkShortcut(false, "setWayGeometry"); return super.setWayGeometry(list); } @Override - public boolean canBeOverwritten( long flags ) - { + public boolean canBeOverwritten(long flags) { return PrepareEncoder.canBeOverwritten(getDirectFlags(), flags); } } - /** - * Disconnects the edges (higher to lower node) via the specified edgeState pointing from lower to - * higher node. - *

- * @param edgeState the edge from lower to higher - */ - public void disconnect( CHEdgeExplorer explorer, EdgeIteratorState edgeState ) - { - // search edge with opposite direction but we need to know the previousEdge for the internalEdgeDisconnect so we cannot simply do: - // EdgeIteratorState tmpIter = getEdgeProps(iter.getEdge(), iter.getBaseNode()); - CHEdgeIterator tmpIter = explorer.setBaseNode(edgeState.getAdjNode()); - int tmpPrevEdge = EdgeIterator.NO_EDGE; - while (tmpIter.next()) - { - if (tmpIter.isShortcut() && tmpIter.getEdge() == edgeState.getEdge()) - { - // TODO this is ugly, move this somehow into the underlying iteration logic - long edgePointer = tmpPrevEdge == EdgeIterator.NO_EDGE ? -1 - : isShortcut(tmpPrevEdge) ? chEdgeAccess.toPointer(tmpPrevEdge) : baseGraph.edgeAccess.toPointer(tmpPrevEdge); - chEdgeAccess.internalEdgeDisconnect(edgeState.getEdge(), edgePointer, - edgeState.getAdjNode(), edgeState.getBaseNode()); - break; - } - - tmpPrevEdge = tmpIter.getEdge(); - } - } - - @Override - public AllCHEdgesIterator getAllEdges() - { - return new AllCHEdgesIteratorImpl(baseGraph); - } - - class AllCHEdgesIteratorImpl extends AllEdgeIterator implements AllCHEdgesIterator - { - public AllCHEdgesIteratorImpl( BaseGraph baseGraph ) - { + class AllCHEdgesIteratorImpl extends AllEdgeIterator implements AllCHEdgesIterator { + public AllCHEdgesIteratorImpl(BaseGraph baseGraph) { super(baseGraph); } @Override - protected final boolean checkRange() - { + protected final boolean checkRange() { if (isShortcut()) return edgeId < shortcutCount; @@ -488,16 +549,14 @@ protected final boolean checkRange() } @Override - public int getEdge() - { + public int getEdge() { if (isShortcut()) return baseGraph.edgeCount + edgeId; return super.getEdge(); } @Override - public boolean isBackward( FlagEncoder encoder ) - { + public boolean isBackward(FlagEncoder encoder) { assert encoder == weighting.getFlagEncoder() : encoder + " vs. " + weighting.getFlagEncoder(); if (isShortcut()) return (getDirectFlags() & PrepareEncoder.getScBwdDir()) != 0; @@ -506,8 +565,7 @@ public boolean isBackward( FlagEncoder encoder ) } @Override - public boolean isForward( FlagEncoder encoder ) - { + public boolean isForward(FlagEncoder encoder) { assert encoder == weighting.getFlagEncoder() : encoder + " vs. " + weighting.getFlagEncoder(); if (isShortcut()) return (getDirectFlags() & PrepareEncoder.getScFwdDir()) != 0; @@ -516,199 +574,53 @@ public boolean isForward( FlagEncoder encoder ) } @Override - public final long getFlags() - { + public final long getFlags() { if (isShortcut()) throw new IllegalStateException("Shortcut should not need to return raw flags!"); return getDirectFlags(); } @Override - public int getMaxId() - { + public int getMaxId() { return super.getMaxId() + shortcutCount; } @Override - public final void setSkippedEdges( int edge1, int edge2 ) - { + public final void setSkippedEdges(int edge1, int edge2) { baseGraph.edges.setInt(edgePointer + S_SKIP_EDGE1, edge1); baseGraph.edges.setInt(edgePointer + S_SKIP_EDGE2, edge2); } @Override - public final int getSkippedEdge1() - { + public final int getSkippedEdge1() { return baseGraph.edges.getInt(edgePointer + S_SKIP_EDGE1); } @Override - public final int getSkippedEdge2() - { + public final int getSkippedEdge2() { return baseGraph.edges.getInt(edgePointer + S_SKIP_EDGE2); } @Override - public final boolean isShortcut() - { + public final boolean isShortcut() { assert baseGraph.isFrozen() : "level graph not yet frozen"; return edgeAccess == chEdgeAccess; } @Override - public final CHEdgeIteratorState setWeight( double weight ) - { + public final CHEdgeIteratorState setWeight(double weight) { CHGraphImpl.this.setWeight(this, weight); return this; } @Override - public final double getWeight() - { + public final double getWeight() { return CHGraphImpl.this.getWeight(this); } @Override - public boolean canBeOverwritten( long flags ) - { + public boolean canBeOverwritten(long flags) { return PrepareEncoder.canBeOverwritten(getDirectFlags(), flags); } } - - final void setWeight( CommonEdgeIterator edge, double weight ) - { - if (weight < 0) - throw new IllegalArgumentException("weight cannot be negative but was " + weight); - - long weightLong; - if (weight > MAX_WEIGHT) - weightLong = MAX_WEIGHT_LONG; - else - weightLong = ((long) (weight * WEIGHT_FACTOR)) << 2; - - long accessFlags = edge.getDirectFlags() & scDirMask; - edge.setFlags(weightLong | accessFlags); - } - - final double getWeight( CommonEdgeIterator edge ) - { - // no need for reverseFlags call (shortcut has identical weight if both dies) and also no need for 64bit - long flags32bit = edge.getDirectFlags(); - double weight = (flags32bit >>> 2) / WEIGHT_FACTOR; - if (weight >= MAX_WEIGHT) - return Double.POSITIVE_INFINITY; - - return weight; - } - - protected int loadEdgesHeader() - { - shortcutCount = shortcuts.getHeader(0 * 4); - shortcutEntryBytes = shortcuts.getHeader(1 * 4); - return 3; - } - - protected int setEdgesHeader() - { - shortcuts.setHeader(0 * 4, shortcutCount); - shortcuts.setHeader(1 * 4, shortcutEntryBytes); - return 3; - } - - @Override - public GraphExtension getExtension() - { - return baseGraph.getExtension(); - } - - @Override - public Graph getBaseGraph() - { - return baseGraph; - } - - @Override - public Graph copyTo( Graph g ) - { - CHGraphImpl tmpG = ((CHGraphImpl) g); - - nodesCH.copyTo(tmpG.nodesCH); - shortcuts.copyTo(tmpG.shortcuts); - - tmpG.N_LEVEL = N_LEVEL; - tmpG.N_CH_REF = N_CH_REF; - tmpG.nodeCHEntryBytes = nodeCHEntryBytes; - return g; - } - - void initStorage() - { - EdgeAccess ea = baseGraph.edgeAccess; - chEdgeAccess.init(ea.E_NODEA, ea.E_NODEB, ea.E_LINKA, ea.E_LINKB, ea.E_DIST, ea.E_FLAGS, false); - // shortcuts - S_SKIP_EDGE1 = ea.E_FLAGS + 4; - S_SKIP_EDGE2 = S_SKIP_EDGE1 + 4; - shortcutEntryBytes = S_SKIP_EDGE2 + 4; - - // node based data: - N_LEVEL = 0; - N_CH_REF = N_LEVEL + 4; - nodeCHEntryBytes = N_CH_REF + 4; - } - - void setSegmentSize( int bytes ) - { - nodesCH.setSegmentSize(bytes); - shortcuts.setSegmentSize(bytes); - } - - @Override - public CHGraph create( long bytes ) - { - nodesCH.create(bytes); - shortcuts.create(bytes); - return this; - } - - @Override - public boolean loadExisting() - { - if (!nodesCH.loadExisting() || !shortcuts.loadExisting()) - return false; - - loadEdgesHeader(); - return true; - } - - @Override - public void flush() - { - nodesCH.flush(); - shortcuts.flush(); - } - - @Override - public void close() - { - nodesCH.close(); - shortcuts.close(); - } - - @Override - public boolean isClosed() - { - return nodesCH.isClosed(); - } - - @Override - public long getCapacity() - { - return nodesCH.getCapacity() + shortcuts.getCapacity(); - } - - @Override - public String toString() - { - return "CHGraph|" + getWeighting().toString(); - } } diff --git a/core/src/main/java/com/graphhopper/storage/DAType.java b/core/src/main/java/com/graphhopper/storage/DAType.java index c2ba4fb48d4..3d95dd37ec3 100644 --- a/core/src/main/java/com/graphhopper/storage/DAType.java +++ b/core/src/main/java/com/graphhopper/storage/DAType.java @@ -20,10 +20,10 @@ /** * Defines how a DataAccess object is created. *

+ * * @author Peter Karich */ -public class DAType -{ +public class DAType { /** * The DA object is hold entirely in-memory. Loading and flushing is a no-op. See RAMDataAccess. */ @@ -56,21 +56,13 @@ public class DAType * Experimental API. Do not use yet. */ public static final DAType UNSAFE_STORE = new DAType(MemRef.UNSAFE, true, false, true, false); - - public enum MemRef - { - HEAP, MMAP, UNSAFE /*, DIRECT */ - - } - private final MemRef memRef; private final boolean storing; private final boolean integ; private final boolean synched; private final boolean allowWrites; - public DAType( DAType type, boolean synched ) - { + public DAType(DAType type, boolean synched) { this(type.getMemRef(), type.isStoring(), type.isInteg(), type.isAllowWrites(), synched); if (!synched) throw new IllegalStateException("constructor can only be used with synched=true"); @@ -78,8 +70,7 @@ public DAType( DAType type, boolean synched ) throw new IllegalStateException("something went wrong as DataAccess object is already synched!?"); } - public DAType( MemRef memRef, boolean storing, boolean integ, boolean allowWrites, boolean synched ) - { + public DAType(MemRef memRef, boolean storing, boolean integ, boolean allowWrites, boolean synched) { this.memRef = memRef; this.storing = storing; this.integ = integ; @@ -87,45 +78,56 @@ public DAType( MemRef memRef, boolean storing, boolean integ, boolean allowWrite this.synched = synched; } + public static DAType fromString(String dataAccess) { + dataAccess = dataAccess.toUpperCase(); + DAType type; + if (dataAccess.contains("MMAP")) + type = DAType.MMAP; + else if (dataAccess.contains("UNSAFE")) + type = DAType.UNSAFE_STORE; + else if (dataAccess.contains("RAM_STORE")) + type = DAType.RAM_STORE; + else + type = DAType.RAM; + + if (dataAccess.contains("SYNC")) + type = new DAType(type, true); + return type; + } + /** * Memory mapped or purely in memory? default is HEAP */ - MemRef getMemRef() - { + MemRef getMemRef() { return memRef; } - public boolean isAllowWrites() - { + public boolean isAllowWrites() { return allowWrites; } /** * @return true if data resides in the JVM heap. */ - public boolean isInMemory() - { + public boolean isInMemory() { return memRef == MemRef.HEAP; } - public boolean isMMap() - { + public boolean isMMap() { return memRef == MemRef.MMAP; } /** * Temporary data or store (with loading and storing)? default is false */ - public boolean isStoring() - { + public boolean isStoring() { return storing; } /** * Optimized for integer values? default is false */ - public boolean isInteg() - { + public boolean isInteg() { return integ; } @@ -134,14 +136,12 @@ public boolean isInteg() * DataAccess object is only read-thread safe where a memory mapped one is not even * read-threadsafe! */ - public boolean isSynched() - { + public boolean isSynched() { return synched; } @Override - public String toString() - { + public String toString() { String str; if (getMemRef() == MemRef.MMAP) str = "MMAP"; @@ -159,30 +159,8 @@ else if (getMemRef() == MemRef.HEAP) return str; } - public static DAType fromString( String dataAccess ) - { - dataAccess = dataAccess.toUpperCase(); - DAType type; - if (dataAccess.contains("MMAP")) - type = DAType.MMAP; - else if (dataAccess.contains("UNSAFE")) - type = DAType.UNSAFE_STORE; - else - { - if (dataAccess.contains("RAM_STORE")) - type = DAType.RAM_STORE; - else - type = DAType.RAM; - } - - if (dataAccess.contains("SYNC")) - type = new DAType(type, true); - return type; - } - @Override - public int hashCode() - { + public int hashCode() { int hash = 7; hash = 59 * hash + 37 * this.memRef.hashCode(); hash = 59 * hash + (this.storing ? 1 : 0); @@ -192,8 +170,7 @@ public int hashCode() } @Override - public boolean equals( Object obj ) - { + public boolean equals(Object obj) { if (obj == null) return false; if (getClass() != obj.getClass()) @@ -209,4 +186,10 @@ public boolean equals( Object obj ) return false; return true; } + + public enum MemRef { + HEAP, MMAP, UNSAFE + /*, DIRECT */ + + } } diff --git a/core/src/main/java/com/graphhopper/storage/DataAccess.java b/core/src/main/java/com/graphhopper/storage/DataAccess.java index b79ec19702f..1acc1b978ef 100644 --- a/core/src/main/java/com/graphhopper/storage/DataAccess.java +++ b/core/src/main/java/com/graphhopper/storage/DataAccess.java @@ -25,10 +25,10 @@ * Life cycle: (1) object creation, (2) configuration (e.g. segment size), (3) create or * loadExisting, (4) usage and calling ensureCapacity if necessary, (5) close *

+ * * @author Peter Karich */ -public interface DataAccess extends Storable -{ +public interface DataAccess extends Storable { /** * The logical identification of this object. */ @@ -38,89 +38,92 @@ public interface DataAccess extends Storable * Renames the underlying DataAccess object. (Flushing shouldn't be necessary before or * afterwards) *

+ * * @throws IllegalStateException if a rename is not possible */ - void rename( String newName ); + void rename(String newName); /** * Set 4 bytes at position 'bytePos' to the specified value */ - void setInt( long bytePos, int value ); + void setInt(long bytePos, int value); /** * Get 4 bytes from position 'bytePos' */ - int getInt( long bytePos ); + int getInt(long bytePos); /** * Set 2 bytes at position 'index' to the specified value */ - void setShort( long bytePos, short value ); + void setShort(long bytePos, short value); /** * Get 2 bytes from position 'index' */ - short getShort( long bytePos ); + short getShort(long bytePos); /** * Set bytes from position 'index' to the specified values */ - void setBytes( long bytePos, byte[] values, int length ); + void setBytes(long bytePos, byte[] values, int length); /** * Get bytes from position 'index' *

+ * * @param values acts as output */ - void getBytes( long bytePos, byte[] values, int length ); + void getBytes(long bytePos, byte[] values, int length); /** * Set 4 bytes at the header space index to the specified value */ - void setHeader( int bytePos, int value ); + void setHeader(int bytePos, int value); /** * Get 4 bytes from the header at 'index' */ - int getHeader( int bytePos ); + int getHeader(int bytePos); /** * The first time you use a DataAccess object after configuring it you need to call this. After * that first call you have to use ensureCapacity to ensure that enough space is reserved. */ @Override - DataAccess create( long bytes ); + DataAccess create(long bytes); /** * Ensures that the capacity of this object is at least the specified bytes. The first time you * have to call 'create' instead. *

+ * * @return true if size was increased * @see #create(long) */ - boolean ensureCapacity( long bytes ); + boolean ensureCapacity(long bytes); /** * Reduces the allocate space to the specified bytes. Warning: it'll free the space even if it * is in use! */ - void trimTo( long bytes ); + void trimTo(long bytes); /** * Copies the content from this object into the specified one. */ - DataAccess copyTo( DataAccess da ); + DataAccess copyTo(DataAccess da); /** - * In order to increase allocated space one needs to layout the underlying storage in segments. - * This is how you can customize the size. + * @return the size of one segment in bytes */ - DataAccess setSegmentSize( int bytes ); + int getSegmentSize(); /** - * @return the size of one segment in bytes + * In order to increase allocated space one needs to layout the underlying storage in segments. + * This is how you can customize the size. */ - int getSegmentSize(); + DataAccess setSegmentSize(int bytes); /** * @return the number of segments. diff --git a/core/src/main/java/com/graphhopper/storage/Directory.java b/core/src/main/java/com/graphhopper/storage/Directory.java index 6659b04b300..9bce1480917 100644 --- a/core/src/main/java/com/graphhopper/storage/Directory.java +++ b/core/src/main/java/com/graphhopper/storage/Directory.java @@ -25,10 +25,10 @@ * Directory as we need one to maintain one DataAccess object for nodes, edges and location2id * index. *

+ * * @author Peter Karich */ -public interface Directory -{ +public interface Directory { /** * @return an id or location in the local filesystem. */ @@ -43,9 +43,9 @@ public interface Directory * Tries to find the object with that name if not existent it creates one and associates the * location with it. A name is unique in one Directory. */ - DataAccess find( String name ); + DataAccess find(String name); - DataAccess find( String name, DAType type ); + DataAccess find(String name, DAType type); /** * Renames the specified DataAccess object into one. @@ -54,7 +54,7 @@ public interface Directory /** * Removes the specified object from the directory. */ - void remove( DataAccess da ); + void remove(DataAccess da); /** * @return the default type of a newly created DataAccess object diff --git a/core/src/main/java/com/graphhopper/storage/EdgeAccess.java b/core/src/main/java/com/graphhopper/storage/EdgeAccess.java index 627d2f0ea80..747e1951d27 100644 --- a/core/src/main/java/com/graphhopper/storage/EdgeAccess.java +++ b/core/src/main/java/com/graphhopper/storage/EdgeAccess.java @@ -25,25 +25,22 @@ /** * @author Peter Karich */ -abstract class EdgeAccess -{ +abstract class EdgeAccess { + static final int NO_NODE = -1; // distance of around +-1000 000 meter are ok private static final double INT_DIST_FACTOR = 1000d; static double MAX_DIST = (Integer.MAX_VALUE - 1) / INT_DIST_FACTOR; - static final int NO_NODE = -1; - int E_NODEA, E_NODEB, E_LINKA, E_LINKB, E_DIST, E_FLAGS; final DataAccess edges; private final BitUtil bitUtil; + int E_NODEA, E_NODEB, E_LINKA, E_LINKB, E_DIST, E_FLAGS; private boolean flagsSizeIsLong; - EdgeAccess( DataAccess edges, BitUtil bitUtil ) - { + EdgeAccess(DataAccess edges, BitUtil bitUtil) { this.edges = edges; this.bitUtil = bitUtil; } - final void init( int E_NODEA, int E_NODEB, int E_LINKA, int E_LINKB, int E_DIST, int E_FLAGS, boolean flagsSizeIsLong ) - { + final void init(int E_NODEA, int E_NODEB, int E_LINKA, int E_LINKB, int E_DIST, int E_FLAGS, boolean flagsSizeIsLong) { this.E_NODEA = E_NODEA; this.E_NODEB = E_NODEB; this.E_LINKA = E_LINKA; @@ -53,60 +50,54 @@ final void init( int E_NODEA, int E_NODEB, int E_LINKA, int E_LINKB, int E_DIST, this.flagsSizeIsLong = flagsSizeIsLong; } - abstract BaseGraph.EdgeIterable createSingleEdge( EdgeFilter edgeFilter ); + abstract BaseGraph.EdgeIterable createSingleEdge(EdgeFilter edgeFilter); - abstract long toPointer( int edgeOrShortcutId ); + abstract long toPointer(int edgeOrShortcutId); - abstract boolean isInBounds( int edgeOrShortcutId ); + abstract boolean isInBounds(int edgeOrShortcutId); - abstract long reverseFlags( long edgePointer, long flags ); + abstract long reverseFlags(long edgePointer, long flags); - abstract int getEdgeRef( int nodeId ); + abstract int getEdgeRef(int nodeId); - abstract void setEdgeRef( int nodeId, int edgeId ); + abstract void setEdgeRef(int nodeId, int edgeId); abstract int getEntryBytes(); - final void invalidateEdge( long edgePointer ) - { + final void invalidateEdge(long edgePointer) { edges.setInt(edgePointer + E_NODEA, NO_NODE); } - final void setDist( long edgePointer, double distance ) - { + final void setDist(long edgePointer, double distance) { edges.setInt(edgePointer + E_DIST, distToInt(distance)); } /** * Translates double distance to integer in order to save it in a DataAccess object */ - private int distToInt( double distance ) - { + private int distToInt(double distance) { int integ = (int) (distance * INT_DIST_FACTOR); if (integ < 0) throw new IllegalArgumentException("Distance cannot be negative: " + distance); if (integ >= Integer.MAX_VALUE) return Integer.MAX_VALUE; - // throw new IllegalArgumentException("Distance too large leading to overflowed integer (#435): " + distance + " "); + // throw new IllegalArgumentException("Distance too large leading to overflowed integer (#435): " + distance + " "); return integ; } /** * returns distance (already translated from integer to double) */ - final double getDist( long pointer ) - { + final double getDist(long pointer) { int val = edges.getInt(pointer + E_DIST); // do never return infinity even if INT MAX, see #435 return val / INT_DIST_FACTOR; } - final long getFlags_( long edgePointer, boolean reverse ) - { + final long getFlags_(long edgePointer, boolean reverse) { int low = edges.getInt(edgePointer + E_FLAGS); long resFlags = low; - if (flagsSizeIsLong) - { + if (flagsSizeIsLong) { int high = edges.getInt(edgePointer + E_FLAGS + 4); resFlags = bitUtil.combineIntsToLong(low, high); } @@ -116,8 +107,7 @@ final long getFlags_( long edgePointer, boolean reverse ) return resFlags; } - final long setFlags_( long edgePointer, boolean reverse, long flags ) - { + final long setFlags_(long edgePointer, boolean reverse, long flags) { if (reverse) flags = reverseFlags(edgePointer, flags); @@ -132,8 +122,7 @@ final long setFlags_( long edgePointer, boolean reverse, long flags ) /** * Write new edge between nodes fromNodeId, and toNodeId both to nodes index and edges index */ - final int internalEdgeAdd( int newEdgeId, int fromNodeId, int toNodeId ) - { + final int internalEdgeAdd(int newEdgeId, int fromNodeId, int toNodeId) { writeEdge(newEdgeId, fromNodeId, toNodeId, EdgeIterator.NO_EDGE, EdgeIterator.NO_EDGE); connectNewEdge(fromNodeId, newEdgeId); if (fromNodeId != toNodeId) @@ -141,8 +130,7 @@ final int internalEdgeAdd( int newEdgeId, int fromNodeId, int toNodeId ) return newEdgeId; } - final int getOtherNode( int nodeThis, long edgePointer ) - { + final int getOtherNode(int nodeThis, long edgePointer) { int nodeA = edges.getInt(edgePointer + E_NODEA); if (nodeA == nodeThis) // return b @@ -151,21 +139,17 @@ final int getOtherNode( int nodeThis, long edgePointer ) return nodeA; } - private long _getLinkPosInEdgeArea( int nodeThis, int nodeOther, long edgePointer ) - { + private long _getLinkPosInEdgeArea(int nodeThis, int nodeOther, long edgePointer) { return nodeThis <= nodeOther ? edgePointer + E_LINKA : edgePointer + E_LINKB; } - final int getEdgeRef( int nodeThis, int nodeOther, long edgePointer ) - { + final int getEdgeRef(int nodeThis, int nodeOther, long edgePointer) { return edges.getInt(_getLinkPosInEdgeArea(nodeThis, nodeOther, edgePointer)); } - final void connectNewEdge( int fromNode, int newOrExistingEdge ) - { + final void connectNewEdge(int fromNode, int newOrExistingEdge) { int edge = getEdgeRef(fromNode); - if (edge > EdgeIterator.NO_EDGE) - { + if (edge > EdgeIterator.NO_EDGE) { long edgePointer = toPointer(newOrExistingEdge); int otherNode = getOtherNode(fromNode, edgePointer); long lastLink = _getLinkPosInEdgeArea(fromNode, otherNode, edgePointer); @@ -174,10 +158,8 @@ final void connectNewEdge( int fromNode, int newOrExistingEdge ) setEdgeRef(fromNode, newOrExistingEdge); } - final long writeEdge( int edgeId, int nodeThis, int nodeOther, int nextEdge, int nextEdgeOther ) - { - if (nodeThis > nodeOther) - { + final long writeEdge(int edgeId, int nodeThis, int nodeOther, int nextEdge, int nextEdgeOther) { + if (nodeThis > nodeOther) { int tmp = nodeThis; nodeThis = nodeOther; nodeOther = tmp; @@ -200,20 +182,18 @@ final long writeEdge( int edgeId, int nodeThis, int nodeOther, int nextEdge, int * This method disconnects the specified edge from the list of edges of the specified node. It * does not release the freed space to be reused. *

+ * * @param edgeToUpdatePointer if it is negative then the nextEdgeId will be saved to refToEdges - * of nodes + * of nodes */ - final long internalEdgeDisconnect( int edgeToRemove, long edgeToUpdatePointer, int baseNode, int adjNode ) - { + final long internalEdgeDisconnect(int edgeToRemove, long edgeToUpdatePointer, int baseNode, int adjNode) { long edgeToRemovePointer = toPointer(edgeToRemove); // an edge is shared across the two nodes even if the edge is not in both directions // so we need to know two edge-pointers pointing to the edge before edgeToRemovePointer int nextEdgeId = getEdgeRef(baseNode, adjNode, edgeToRemovePointer); - if (edgeToUpdatePointer < 0) - { + if (edgeToUpdatePointer < 0) { setEdgeRef(baseNode, nextEdgeId); - } else - { + } else { // adjNode is different for the edge we want to update with the new link long link = edges.getInt(edgeToUpdatePointer + E_NODEA) == baseNode ? edgeToUpdatePointer + E_LINKA : edgeToUpdatePointer + E_LINKB; @@ -222,8 +202,7 @@ final long internalEdgeDisconnect( int edgeToRemove, long edgeToUpdatePointer, i return edgeToRemovePointer; } - final EdgeIteratorState getEdgeProps( int edgeId, int adjNode ) - { + final EdgeIteratorState getEdgeProps(int edgeId, int adjNode) { if (edgeId <= EdgeIterator.NO_EDGE) throw new IllegalStateException("edgeId invalid " + edgeId + ", " + this); diff --git a/core/src/main/java/com/graphhopper/storage/GHDirectory.java b/core/src/main/java/com/graphhopper/storage/GHDirectory.java index d6e0ea8d5d6..5d289bd5444 100644 --- a/core/src/main/java/com/graphhopper/storage/GHDirectory.java +++ b/core/src/main/java/com/graphhopper/storage/GHDirectory.java @@ -28,18 +28,17 @@ /** * Implements some common methods for the subclasses. *

+ * * @author Peter Karich */ -public class GHDirectory implements Directory -{ - protected Map map = new HashMap(); - protected Map types = new HashMap(); +public class GHDirectory implements Directory { protected final String location; private final DAType defaultType; private final ByteOrder byteOrder = ByteOrder.LITTLE_ENDIAN; + protected Map map = new HashMap(); + protected Map types = new HashMap(); - public GHDirectory( String _location, DAType defaultType ) - { + public GHDirectory(String _location, DAType defaultType) { this.defaultType = defaultType; if (Helper.isEmpty(_location)) _location = new File("").getAbsolutePath(); @@ -54,15 +53,12 @@ public GHDirectory( String _location, DAType defaultType ) // set default access to integer based // improves performance on server side, 10% faster for queries and preparation - if (this.defaultType.isInMemory()) - { - if (isStoring()) - { + if (this.defaultType.isInMemory()) { + if (isStoring()) { put("location_index", DAType.RAM_INT_STORE); put("edges", DAType.RAM_INT_STORE); put("nodes", DAType.RAM_INT_STORE); - } else - { + } else { put("location_index", DAType.RAM_INT); put("edges", DAType.RAM_INT); put("nodes", DAType.RAM_INT); @@ -71,13 +67,11 @@ public GHDirectory( String _location, DAType defaultType ) } @Override - public ByteOrder getByteOrder() - { + public ByteOrder getByteOrder() { return byteOrder; } - public Directory put( String name, DAType type ) - { + public Directory put(String name, DAType type) { if (!name.equals(name.toLowerCase())) throw new IllegalArgumentException("Since 0.7 DataAccess objects does no longer accept upper case names"); @@ -86,8 +80,7 @@ public Directory put( String name, DAType type ) } @Override - public DataAccess find( String name ) - { + public DataAccess find(String name) { DAType type = types.get(name); if (type == null) type = defaultType; @@ -96,24 +89,20 @@ public DataAccess find( String name ) } @Override - public DataAccess find( String name, DAType type ) - { + public DataAccess find(String name, DAType type) { if (!name.equals(name.toLowerCase())) throw new IllegalArgumentException("Since 0.7 DataAccess objects does no longer accept upper case names"); DataAccess da = map.get(name); - if (da != null) - { + if (da != null) { if (!type.equals(da.getType())) throw new IllegalStateException("Found existing DataAccess object '" + name + "' but types did not match. Requested:" + type + ", was:" + da.getType()); return da; } - if (type.isInMemory()) - { - if (type.isInteg()) - { + if (type.isInMemory()) { + if (type.isInteg()) { if (type.isStoring()) da = new RAMIntDataAccess(name, location, true, byteOrder); else @@ -122,11 +111,9 @@ public DataAccess find( String name, DAType type ) da = new RAMDataAccess(name, location, true, byteOrder); else da = new RAMDataAccess(name, location, false, byteOrder); - } else if (type.isMMap()) - { + } else if (type.isMMap()) { da = new MMapDataAccess(name, location, byteOrder, type.isAllowWrites()); - } else - { + } else { da = new UnsafeDataAccess(name, location, byteOrder); } @@ -138,15 +125,13 @@ public DataAccess find( String name, DAType type ) } @Override - public void clear() - { + public void clear() { // If there is at least one MMap DA then do not apply the cleanHack // for every single mmap DA as this is very slow if lots of DataAccess objects were collected // => forceClean == false MMapDataAccess mmapDA = null; - for (DataAccess da : map.values()) - { + for (DataAccess da : map.values()) { if (da instanceof MMapDataAccess) mmapDA = (MMapDataAccess) da; @@ -158,14 +143,12 @@ public void clear() } @Override - public void remove( DataAccess da ) - { + public void remove(DataAccess da) { removeFromMap(da.getName()); removeDA(da, da.getName(), true); } - void removeDA( DataAccess da, String name, boolean forceClean ) - { + void removeDA(DataAccess da, String name, boolean forceClean) { if (da instanceof MMapDataAccess) ((MMapDataAccess) da).close(forceClean); else @@ -175,47 +158,40 @@ void removeDA( DataAccess da, String name, boolean forceClean ) Helper.removeDir(new File(location + name)); } - void removeFromMap( String name ) - { + void removeFromMap(String name) { DataAccess da = map.remove(name); if (da == null) throw new IllegalStateException("Couldn't remove dataAccess object:" + name); } @Override - public DAType getDefaultType() - { + public DAType getDefaultType() { return defaultType; } - public boolean isStoring() - { + public boolean isStoring() { return defaultType.isStoring(); } @Override - public Directory create() - { + public Directory create() { if (isStoring()) new File(location).mkdirs(); return this; } @Override - public Collection getAll() - { + public Collection getAll() { return map.values(); } @Override - public String toString() - { + public String toString() { return getLocation(); } @Override - public String getLocation() - { + public String getLocation() { return location; } } diff --git a/core/src/main/java/com/graphhopper/storage/GHNodeAccess.java b/core/src/main/java/com/graphhopper/storage/GHNodeAccess.java index e3eabb896e7..19c4615d391 100644 --- a/core/src/main/java/com/graphhopper/storage/GHNodeAccess.java +++ b/core/src/main/java/com/graphhopper/storage/GHNodeAccess.java @@ -22,47 +22,41 @@ /** * A helper class for GraphHopperStorage for its node access. *

+ * * @author Peter Karich */ -class GHNodeAccess implements NodeAccess -{ +class GHNodeAccess implements NodeAccess { private final BaseGraph that; private final boolean elevation; - public GHNodeAccess( BaseGraph that, boolean withElevation ) - { + public GHNodeAccess(BaseGraph that, boolean withElevation) { this.that = that; this.elevation = withElevation; } @Override - public void ensureNode( int nodeId ) - { + public void ensureNode(int nodeId) { that.ensureNodeIndex(nodeId); } @Override - public final void setNode( int nodeId, double lat, double lon ) - { + public final void setNode(int nodeId, double lat, double lon) { setNode(nodeId, lat, lon, Double.NaN); } @Override - public final void setNode( int nodeId, double lat, double lon, double ele ) - { + public final void setNode(int nodeId, double lat, double lon, double ele) { that.ensureNodeIndex(nodeId); long tmp = (long) nodeId * that.nodeEntryBytes; that.nodes.setInt(tmp + that.N_LAT, Helper.degreeToInt(lat)); that.nodes.setInt(tmp + that.N_LON, Helper.degreeToInt(lon)); - if (is3D()) - { + if (is3D()) { // meter precision is sufficient for now that.nodes.setInt(tmp + that.N_ELE, Helper.eleToInt(ele)); that.bounds.update(lat, lon, ele); - } else - { + } else { that.bounds.update(lat, lon); } @@ -72,20 +66,17 @@ public final void setNode( int nodeId, double lat, double lon, double ele ) } @Override - public final double getLatitude( int nodeId ) - { + public final double getLatitude(int nodeId) { return Helper.intToDegree(that.nodes.getInt((long) nodeId * that.nodeEntryBytes + that.N_LAT)); } @Override - public final double getLongitude( int nodeId ) - { + public final double getLongitude(int nodeId) { return Helper.intToDegree(that.nodes.getInt((long) nodeId * that.nodeEntryBytes + that.N_LON)); } @Override - public final double getElevation( int nodeId ) - { + public final double getElevation(int nodeId) { if (!elevation) throw new IllegalStateException("Cannot access elevation - 3D is not enabled"); @@ -93,40 +84,33 @@ public final double getElevation( int nodeId ) } @Override - public final double getEle( int nodeId ) - { + public final double getEle(int nodeId) { return getElevation(nodeId); } @Override - public final double getLat( int nodeId ) - { + public final double getLat(int nodeId) { return getLatitude(nodeId); } @Override - public final double getLon( int nodeId ) - { + public final double getLon(int nodeId) { return getLongitude(nodeId); } @Override - public final void setAdditionalNodeField( int index, int additionalValue ) - { - if (that.extStorage.isRequireNodeField() && that.N_ADDITIONAL >= 0) - { + public final void setAdditionalNodeField(int index, int additionalValue) { + if (that.extStorage.isRequireNodeField() && that.N_ADDITIONAL >= 0) { that.ensureNodeIndex(index); long tmp = (long) index * that.nodeEntryBytes; that.nodes.setInt(tmp + that.N_ADDITIONAL, additionalValue); - } else - { + } else { throw new AssertionError("This graph does not provide an additional node field"); } } @Override - public final int getAdditionalNodeField( int index ) - { + public final int getAdditionalNodeField(int index) { if (that.extStorage.isRequireNodeField() && that.N_ADDITIONAL >= 0) return that.nodes.getInt((long) index * that.nodeEntryBytes + that.N_ADDITIONAL); else @@ -134,14 +118,12 @@ public final int getAdditionalNodeField( int index ) } @Override - public final boolean is3D() - { + public final boolean is3D() { return elevation; } @Override - public int getDimension() - { + public int getDimension() { if (elevation) return 3; return 2; diff --git a/core/src/main/java/com/graphhopper/storage/Graph.java b/core/src/main/java/com/graphhopper/storage/Graph.java index 60ce33fb19e..36f25f837c1 100644 --- a/core/src/main/java/com/graphhopper/storage/Graph.java +++ b/core/src/main/java/com/graphhopper/storage/Graph.java @@ -17,8 +17,8 @@ */ package com.graphhopper.storage; -import com.graphhopper.routing.util.EdgeFilter; import com.graphhopper.routing.util.AllEdgesIterator; +import com.graphhopper.routing.util.EdgeFilter; import com.graphhopper.util.EdgeExplorer; import com.graphhopper.util.EdgeIteratorState; import com.graphhopper.util.shapes.BBox; @@ -27,10 +27,10 @@ * An interface to represent a (geo) graph - suited for efficient storage as it can be requested via * indices called node IDs. To get the lat,lon point you need to set up a LocationIndex instance. *

+ * * @author Peter Karich */ -public interface Graph -{ +public interface Graph { /** * @return a graph which behaves like an unprepared graph and e.g. the normal unidirectional * Dijkstra or any graph traversal algorithm can be executed. @@ -56,27 +56,29 @@ public interface Graph * Creates an edge between the nodes a and b. To set distance or access use the returned edge * and e.g. edgeState.setDistance *

+ * * @param a the index of the starting (tower) node of the edge * @param b the index of the ending (tower) node of the edge * @return the newly created edge */ - EdgeIteratorState edge( int a, int b ); + EdgeIteratorState edge(int a, int b); /** * Use edge(a,b).setDistance().setFlags instead */ - EdgeIteratorState edge( int a, int b, double distance, boolean bothDirections ); + EdgeIteratorState edge(int a, int b, double distance, boolean bothDirections); /** * Returns a wrapper over the specified edgeId. *

+ * * @param adjNode is the node that will be returned via adjNode(). If adjNode is - * Integer.MIN_VALUE then the edge with uncertain values for adjNode and baseNode (two - * possibilities) will be returned. + * Integer.MIN_VALUE then the edge with uncertain values for adjNode and baseNode (two + * possibilities) will be returned. * @return an edge iterator state or potentially null if adjNode does not match * @throws IllegalStateException if edgeId is not valid */ - EdgeIteratorState getEdgeIteratorState( int edgeId, int adjNode ); + EdgeIteratorState getEdgeIteratorState(int edgeId, int adjNode); /** * @return all edges in this graph, where baseNode will be the smaller node. @@ -88,10 +90,11 @@ public interface Graph * node. Reduce calling this method as much as possible, e.g. create an explorer before a for * loop! *

+ * * @see EdgeExplorer * @see Graph#createEdgeExplorer() */ - EdgeExplorer createEdgeExplorer( EdgeFilter filter ); + EdgeExplorer createEdgeExplorer(EdgeFilter filter); /** * @see Graph#createEdgeExplorer(com.graphhopper.routing.util.EdgeFilter) @@ -101,9 +104,10 @@ public interface Graph /** * Copy this Graph into the specified Graph g. *

+ * * @return the specified Graph g */ - Graph copyTo( Graph g ); + Graph copyTo(Graph g); /** * @return the graph extension like a TurnCostExtension diff --git a/core/src/main/java/com/graphhopper/storage/GraphBuilder.java b/core/src/main/java/com/graphhopper/storage/GraphBuilder.java index 1b02090124c..00693d2dcae 100644 --- a/core/src/main/java/com/graphhopper/storage/GraphBuilder.java +++ b/core/src/main/java/com/graphhopper/storage/GraphBuilder.java @@ -19,15 +19,16 @@ import com.graphhopper.routing.util.EncodingManager; import com.graphhopper.routing.weighting.Weighting; + import java.util.Arrays; /** * For now this is just a helper class to quickly create a GraphStorage. *

+ * * @author Peter Karich */ -public class GraphBuilder -{ +public class GraphBuilder { private final EncodingManager encodingManager; private String location; private boolean mmap; @@ -36,60 +37,51 @@ public class GraphBuilder private long byteCapacity = 100; private Weighting singleCHWeighting; - public GraphBuilder( EncodingManager encodingManager ) - { + public GraphBuilder(EncodingManager encodingManager) { this.encodingManager = encodingManager; } /** * This method enables creating a CHGraph with the specified weighting. */ - public GraphBuilder setCHGraph( Weighting singleCHWeighting ) - { + public GraphBuilder setCHGraph(Weighting singleCHWeighting) { this.singleCHWeighting = singleCHWeighting; return this; } - public GraphBuilder setLocation( String location ) - { + public GraphBuilder setLocation(String location) { this.location = location; return this; } - public GraphBuilder setStore( boolean store ) - { + public GraphBuilder setStore(boolean store) { this.store = store; return this; } - public GraphBuilder setMmap( boolean mmap ) - { + public GraphBuilder setMmap(boolean mmap) { this.mmap = mmap; return this; } - public GraphBuilder setExpectedSize( byte cap ) - { + public GraphBuilder setExpectedSize(byte cap) { this.byteCapacity = cap; return this; } - public GraphBuilder set3D( boolean withElevation ) - { + public GraphBuilder set3D(boolean withElevation) { this.elevation = withElevation; return this; } - public boolean hasElevation() - { + public boolean hasElevation() { return elevation; } /** * Creates a CHGraph */ - public CHGraph chGraphCreate( Weighting singleCHWeighting ) - { + public CHGraph chGraphCreate(Weighting singleCHWeighting) { return setCHGraph(singleCHWeighting).create().getGraph(CHGraph.class, singleCHWeighting); } @@ -98,8 +90,7 @@ public CHGraph chGraphCreate( Weighting singleCHWeighting ) * Afterwards you'll need to call GraphStorage. Create to have a usable object. Better use * create. */ - public GraphHopperStorage build() - { + public GraphHopperStorage build() { Directory dir; if (mmap) dir = new MMapDirectory(location); @@ -118,19 +109,16 @@ public GraphHopperStorage build() /** * Default graph is a GraphStorage with an in memory directory and disabled storing on flush. */ - public GraphHopperStorage create() - { + public GraphHopperStorage create() { return build().create(byteCapacity); } /** * @throws IllegalStateException if not loadable. */ - public GraphHopperStorage load() - { + public GraphHopperStorage load() { GraphHopperStorage gs = build(); - if (!gs.loadExisting()) - { + if (!gs.loadExisting()) { throw new IllegalStateException("Cannot load graph " + location); } return gs; diff --git a/core/src/main/java/com/graphhopper/storage/GraphExtension.java b/core/src/main/java/com/graphhopper/storage/GraphExtension.java index a8dec4bba54..08aacbf05a1 100644 --- a/core/src/main/java/com/graphhopper/storage/GraphExtension.java +++ b/core/src/main/java/com/graphhopper/storage/GraphExtension.java @@ -21,8 +21,7 @@ * If you need custom storages, like turn cost tables, or osmid tables for your graph you implement * this interface and put it in any graph storage you want. */ -public interface GraphExtension extends Storable -{ +public interface GraphExtension extends Storable { /** * @return true, if and only if, if an additional field at the graphs node storage is required */ @@ -46,109 +45,94 @@ public interface GraphExtension extends Storable /** * initializes the extended storage by giving the base graph */ - void init( Graph graph, Directory dir ); + void init(Graph graph, Directory dir); /** * sets the segment size in all additional data storages */ - void setSegmentSize( int bytes ); + void setSegmentSize(int bytes); /** * creates a copy of this extended storage */ - GraphExtension copyTo( GraphExtension extStorage ); + GraphExtension copyTo(GraphExtension extStorage); /** * default implementation defines no additional fields or any logic. there's like nothing , like * the default behavior. */ - class NoOpExtension implements GraphExtension - { + class NoOpExtension implements GraphExtension { @Override - public boolean isRequireNodeField() - { + public boolean isRequireNodeField() { return false; } @Override - public boolean isRequireEdgeField() - { + public boolean isRequireEdgeField() { return false; } @Override - public int getDefaultNodeFieldValue() - { + public int getDefaultNodeFieldValue() { return 0; } @Override - public int getDefaultEdgeFieldValue() - { + public int getDefaultEdgeFieldValue() { return 0; } @Override - public void init( Graph graph, Directory dir ) - { + public void init(Graph graph, Directory dir) { // noop } @Override - public GraphExtension create( long byteCount ) - { + public GraphExtension create(long byteCount) { // noop return this; } @Override - public boolean loadExisting() - { + public boolean loadExisting() { // noop return true; } @Override - public void setSegmentSize( int bytes ) - { + public void setSegmentSize(int bytes) { // noop } @Override - public void flush() - { + public void flush() { // noop } @Override - public void close() - { + public void close() { // noop } @Override - public long getCapacity() - { + public long getCapacity() { return 0; } @Override - public GraphExtension copyTo( GraphExtension extStorage ) - { + public GraphExtension copyTo(GraphExtension extStorage) { // noop return extStorage; } @Override - public String toString() - { + public String toString() { return "NoExt"; } @Override - public boolean isClosed() - { + public boolean isClosed() { return false; } } diff --git a/core/src/main/java/com/graphhopper/storage/GraphHopperStorage.java b/core/src/main/java/com/graphhopper/storage/GraphHopperStorage.java index fa3d9f10046..e472d927166 100644 --- a/core/src/main/java/com/graphhopper/storage/GraphHopperStorage.java +++ b/core/src/main/java/com/graphhopper/storage/GraphHopperStorage.java @@ -17,11 +17,14 @@ */ package com.graphhopper.storage; +import com.graphhopper.routing.util.AllEdgesIterator; +import com.graphhopper.routing.util.EdgeFilter; +import com.graphhopper.routing.util.EncodingManager; import com.graphhopper.routing.weighting.Weighting; -import com.graphhopper.routing.util.*; import com.graphhopper.util.EdgeExplorer; import com.graphhopper.util.EdgeIteratorState; import com.graphhopper.util.shapes.BBox; + import java.util.ArrayList; import java.util.Collection; import java.util.Collections; @@ -33,12 +36,12 @@ * different traversal methods. By default this class implements the graph interface and results in * identical behavior as the Graph instance from getGraph(Graph.class) *

+ * * @author Peter Karich * @see GraphBuilder to create a (CH)Graph easier * @see #getGraph(java.lang.Class) */ -public final class GraphHopperStorage implements GraphStorage, Graph -{ +public final class GraphHopperStorage implements GraphStorage, Graph { private final Directory dir; private final EncodingManager encodingManager; private final StorableProperties properties; @@ -46,14 +49,12 @@ public final class GraphHopperStorage implements GraphStorage, Graph // same flush order etc private final Collection chGraphs = new ArrayList(5); - public GraphHopperStorage( Directory dir, EncodingManager encodingManager, boolean withElevation, GraphExtension extendedStorage ) - { + public GraphHopperStorage(Directory dir, EncodingManager encodingManager, boolean withElevation, GraphExtension extendedStorage) { this(Collections.emptyList(), dir, encodingManager, withElevation, extendedStorage); } - public GraphHopperStorage( List chWeightings, Directory dir, final EncodingManager encodingManager, - boolean withElevation, GraphExtension extendedStorage ) - { + public GraphHopperStorage(List chWeightings, Directory dir, final EncodingManager encodingManager, + boolean withElevation, GraphExtension extendedStorage) { if (extendedStorage == null) throw new IllegalArgumentException("GraphExtension cannot be null, use NoOpExtension"); @@ -63,30 +64,24 @@ public GraphHopperStorage( List chWeightings, Directory dir this.encodingManager = encodingManager; this.dir = dir; this.properties = new StorableProperties(dir); - InternalGraphEventListener listener = new InternalGraphEventListener() - { + InternalGraphEventListener listener = new InternalGraphEventListener() { @Override - public void initStorage() - { - for (CHGraphImpl cg : chGraphs) - { + public void initStorage() { + for (CHGraphImpl cg : chGraphs) { cg.initStorage(); } } @Override - public void freeze() - { - for (CHGraphImpl cg : chGraphs) - { + public void freeze() { + for (CHGraphImpl cg : chGraphs) { cg._freeze(); } } }; this.baseGraph = new BaseGraph(dir, encodingManager, withElevation, listener, extendedStorage); - for (Weighting w : chWeightings) - { + for (Weighting w : chWeightings) { chGraphs.add(new CHGraphImpl(w, dir, this.baseGraph)); } } @@ -95,8 +90,7 @@ public void freeze() * This method returns the routing graph for the specified weighting, could be potentially * filled with shortcuts. */ - public T getGraph( Class clazz, Weighting weighting ) - { + public T getGraph(Class clazz, Weighting weighting) { if (clazz.equals(Graph.class)) return (T) baseGraph; @@ -107,8 +101,7 @@ public T getGraph( Class clazz, Weighting weighting ) throw new IllegalStateException("Cannot find CHGraph with null weighting"); List existing = new ArrayList(); - for (CHGraphImpl cg : chGraphs) - { + for (CHGraphImpl cg : chGraphs) { if (cg.getWeighting() == weighting) return (T) cg; @@ -118,8 +111,7 @@ public T getGraph( Class clazz, Weighting weighting ) throw new IllegalStateException("Cannot find CHGraph for specified weighting: " + weighting + ", existing:" + existing); } - public T getGraph( Class clazz ) - { + public T getGraph(Class clazz) { if (clazz.equals(Graph.class)) return (T) baseGraph; @@ -130,16 +122,13 @@ public T getGraph( Class clazz ) return (T) cg; } - public boolean isCHPossible() - { + public boolean isCHPossible() { return !chGraphs.isEmpty(); } - public List getCHWeightings() - { + public List getCHWeightings() { List list = new ArrayList(chGraphs.size()); - for (CHGraphImpl cg : chGraphs) - { + for (CHGraphImpl cg : chGraphs) { list.add(cg.getWeighting()); } return list; @@ -149,18 +138,15 @@ public List getCHWeightings() * @return the directory where this graph is stored. */ @Override - public Directory getDirectory() - { + public Directory getDirectory() { return dir; } @Override - public void setSegmentSize( int bytes ) - { + public void setSegmentSize(int bytes) { baseGraph.setSegmentSize(bytes); - for (CHGraphImpl cg : chGraphs) - { + for (CHGraphImpl cg : chGraphs) { cg.setSegmentSize(bytes); } } @@ -169,8 +155,7 @@ public void setSegmentSize( int bytes ) * After configuring this storage you need to create it explicitly. */ @Override - public GraphHopperStorage create( long byteCount ) - { + public GraphHopperStorage create(long byteCount) { baseGraph.checkInit(); if (encodingManager == null) throw new IllegalStateException("EncodingManager can only be null if you call loadExisting"); @@ -188,8 +173,7 @@ public GraphHopperStorage create( long byteCount ) baseGraph.create(initSize); - for (CHGraphImpl cg : chGraphs) - { + for (CHGraphImpl cg : chGraphs) { cg.create(byteCount); } @@ -198,37 +182,31 @@ public GraphHopperStorage create( long byteCount ) } @Override - public EncodingManager getEncodingManager() - { + public EncodingManager getEncodingManager() { return encodingManager; } @Override - public StorableProperties getProperties() - { + public StorableProperties getProperties() { return properties; } - public void setAdditionalEdgeField( long edgePointer, int value ) - { + public void setAdditionalEdgeField(long edgePointer, int value) { baseGraph.setAdditionalEdgeField(edgePointer, value); } @Override - public void markNodeRemoved( int index ) - { + public void markNodeRemoved(int index) { baseGraph.getRemovedNodes().add(index); } @Override - public boolean isNodeRemoved( int index ) - { + public boolean isNodeRemoved(int index) { return baseGraph.getRemovedNodes().contains(index); } @Override - public void optimize() - { + public void optimize() { if (isFrozen()) throw new IllegalStateException("do not optimize after graph was frozen"); @@ -245,17 +223,14 @@ public void optimize() } @Override - public boolean loadExisting() - { + public boolean loadExisting() { baseGraph.checkInit(); - if (properties.loadExisting()) - { + if (properties.loadExisting()) { properties.checkVersions(false); // check encoding for compatiblity String flagEncodersStr = properties.get("graph.flag_encoders"); - if (!flagEncodersStr.isEmpty() && !encodingManager.toDetailsString().equalsIgnoreCase(flagEncodersStr)) - { + if (!flagEncodersStr.isEmpty() && !encodingManager.toDetailsString().equalsIgnoreCase(flagEncodersStr)) { throw new IllegalStateException("Encoding does not match:\nGraphhopper config: " + encodingManager.toDetailsString() + "\nGraph: " + flagEncodersStr + ", dir:" + dir.getLocation()); } @@ -276,8 +251,7 @@ public boolean loadExisting() if (!loadedCHWeightings.equals(configuredCHWeightings)) throw new IllegalStateException("Configured graph.ch.weightings: " + configuredCHWeightings + " is not equal to loaded " + loadedCHWeightings); - for (CHGraphImpl cg : chGraphs) - { + for (CHGraphImpl cg : chGraphs) { if (!cg.loadExisting()) throw new IllegalStateException("Cannot load " + cg); } @@ -288,10 +262,8 @@ public boolean loadExisting() } @Override - public void flush() - { - for (CHGraphImpl cg : chGraphs) - { + public void flush() { + for (CHGraphImpl cg : chGraphs) { cg.setEdgesHeader(); cg.flush(); } @@ -301,30 +273,25 @@ public void flush() } @Override - public void close() - { + public void close() { properties.close(); baseGraph.close(); - for (CHGraphImpl cg : chGraphs) - { + for (CHGraphImpl cg : chGraphs) { cg.close(); } } @Override - public boolean isClosed() - { + public boolean isClosed() { return baseGraph.nodes.isClosed(); } @Override - public long getCapacity() - { + public long getCapacity() { long cnt = baseGraph.getCapacity() + properties.getCapacity(); - for (CHGraphImpl cg : chGraphs) - { + for (CHGraphImpl cg : chGraphs) { cnt += cg.getCapacity(); } return cnt; @@ -334,23 +301,19 @@ public long getCapacity() * Avoid that edges and nodes of the base graph are further modified. Necessary as hook for e.g. * ch graphs on top to initilize themself */ - public synchronized void freeze() - { + public synchronized void freeze() { if (!baseGraph.isFrozen()) baseGraph.freeze(); } - boolean isFrozen() - { + boolean isFrozen() { return baseGraph.isFrozen(); } @Override - public String toDetailsString() - { + public String toDetailsString() { String str = baseGraph.toDetailsString(); - for (CHGraphImpl cg : chGraphs) - { + for (CHGraphImpl cg : chGraphs) { str += ", " + cg.toDetailsString(); } @@ -358,8 +321,7 @@ public String toDetailsString() } @Override - public String toString() - { + public String toString() { return (isCHPossible() ? "CH|" : "") + encodingManager + "|" + getDirectory().getDefaultType() @@ -373,74 +335,62 @@ public String toString() // Graph g = storage.getGraph(Graph.class); // instead directly the storage can be used to traverse the base graph @Override - public Graph getBaseGraph() - { + public Graph getBaseGraph() { return baseGraph; } @Override - public int getNodes() - { + public int getNodes() { return baseGraph.getNodes(); } @Override - public NodeAccess getNodeAccess() - { + public NodeAccess getNodeAccess() { return baseGraph.getNodeAccess(); } @Override - public BBox getBounds() - { + public BBox getBounds() { return baseGraph.getBounds(); } @Override - public EdgeIteratorState edge( int a, int b ) - { + public EdgeIteratorState edge(int a, int b) { return baseGraph.edge(a, b); } @Override - public EdgeIteratorState edge( int a, int b, double distance, boolean bothDirections ) - { + public EdgeIteratorState edge(int a, int b, double distance, boolean bothDirections) { return baseGraph.edge(a, b, distance, bothDirections); } @Override - public EdgeIteratorState getEdgeIteratorState( int edgeId, int adjNode ) - { + public EdgeIteratorState getEdgeIteratorState(int edgeId, int adjNode) { return baseGraph.getEdgeIteratorState(edgeId, adjNode); } @Override - public AllEdgesIterator getAllEdges() - { + public AllEdgesIterator getAllEdges() { return baseGraph.getAllEdges(); } @Override - public EdgeExplorer createEdgeExplorer( EdgeFilter filter ) - { + public EdgeExplorer createEdgeExplorer(EdgeFilter filter) { return baseGraph.createEdgeExplorer(filter); } @Override - public EdgeExplorer createEdgeExplorer() - { + public EdgeExplorer createEdgeExplorer() { return baseGraph.createEdgeExplorer(); } @Override - public Graph copyTo( Graph g ) - { + public Graph copyTo(Graph g) { return baseGraph.copyTo(g); } @Override - public GraphExtension getExtension() - { + public GraphExtension getExtension() { return baseGraph.getExtension(); } } diff --git a/core/src/main/java/com/graphhopper/storage/GraphStorage.java b/core/src/main/java/com/graphhopper/storage/GraphStorage.java index 6ce014f5c26..0a7b947ae4e 100644 --- a/core/src/main/java/com/graphhopper/storage/GraphStorage.java +++ b/core/src/main/java/com/graphhopper/storage/GraphStorage.java @@ -19,13 +19,12 @@ import com.graphhopper.routing.util.EncodingManager; -public interface GraphStorage extends Storable -{ +public interface GraphStorage extends Storable { Directory getDirectory(); EncodingManager getEncodingManager(); - void setSegmentSize( int bytes ); + void setSegmentSize(int bytes); String toDetailsString(); @@ -34,12 +33,12 @@ public interface GraphStorage extends Storable /** * Schedule the deletion of the specified node until an optimize() call happens */ - void markNodeRemoved( int index ); + void markNodeRemoved(int index); /** * Checks if the specified node is marked as removed. */ - boolean isNodeRemoved( int index ); + boolean isNodeRemoved(int index); /** * Performs optimization routines like deletion or node rearrangements. diff --git a/core/src/main/java/com/graphhopper/storage/IntIterator.java b/core/src/main/java/com/graphhopper/storage/IntIterator.java index 87883f44de9..a5f260d7718 100644 --- a/core/src/main/java/com/graphhopper/storage/IntIterator.java +++ b/core/src/main/java/com/graphhopper/storage/IntIterator.java @@ -20,21 +20,17 @@ /** * @author Peter Karich */ -public interface IntIterator -{ +public interface IntIterator { boolean next(); int getValue(); void remove(); - class Helper - { - public static int count( IntIterator iter ) - { + class Helper { + public static int count(IntIterator iter) { int counter = 0; - while (iter.next()) - { + while (iter.next()) { ++counter; } return counter; diff --git a/core/src/main/java/com/graphhopper/storage/InternalGraphEventListener.java b/core/src/main/java/com/graphhopper/storage/InternalGraphEventListener.java index ce1c2aefedc..b8fc9049926 100644 --- a/core/src/main/java/com/graphhopper/storage/InternalGraphEventListener.java +++ b/core/src/main/java/com/graphhopper/storage/InternalGraphEventListener.java @@ -20,8 +20,7 @@ /** * @author Peter Karich */ -public interface InternalGraphEventListener -{ +public interface InternalGraphEventListener { void freeze(); void initStorage(); diff --git a/core/src/main/java/com/graphhopper/storage/Lock.java b/core/src/main/java/com/graphhopper/storage/Lock.java index 9c95c71e38e..85bfab65fc4 100644 --- a/core/src/main/java/com/graphhopper/storage/Lock.java +++ b/core/src/main/java/com/graphhopper/storage/Lock.java @@ -20,10 +20,10 @@ /** * A write lock. Influenced by Lucene code *

+ * * @author Peter Karich */ -public interface Lock -{ +public interface Lock { String getName(); boolean tryLock(); diff --git a/core/src/main/java/com/graphhopper/storage/LockFactory.java b/core/src/main/java/com/graphhopper/storage/LockFactory.java index f114c5c8a8b..b3fbe856f5d 100644 --- a/core/src/main/java/com/graphhopper/storage/LockFactory.java +++ b/core/src/main/java/com/graphhopper/storage/LockFactory.java @@ -22,20 +22,19 @@ /** * @author Peter Karich */ -public interface LockFactory -{ - void setLockDir( File lockDir ); +public interface LockFactory { + void setLockDir(File lockDir); /** * This creates a file for write or read locks depending on the specified writeAccess property. * Important note: even for read locks we need write access to the underlying filesystem in * order to avoid writes from other processes. */ - Lock create( String fileName, boolean writeAccess ); + Lock create(String fileName, boolean writeAccess); /** * Removes the specified lock. Note: on windows we cannot forcefully remove an unreleased native * lock */ - void forceRemove( String fileName, boolean writeAccess ); + void forceRemove(String fileName, boolean writeAccess); } diff --git a/core/src/main/java/com/graphhopper/storage/MMapDataAccess.java b/core/src/main/java/com/graphhopper/storage/MMapDataAccess.java index 9febc102e0a..93f368bbb81 100644 --- a/core/src/main/java/com/graphhopper/storage/MMapDataAccess.java +++ b/core/src/main/java/com/graphhopper/storage/MMapDataAccess.java @@ -35,50 +35,42 @@ * This is a data structure which uses the operating system to synchronize between disc and memory. * Use {@link SynchedDAWrapper} if you intent to use this from multiple threads! *

+ * * @author Peter Karich */ @NotThreadSafe -public class MMapDataAccess extends AbstractDataAccess -{ +public class MMapDataAccess extends AbstractDataAccess { + private final boolean allowWrites; private RandomAccessFile raFile; private List segments = new ArrayList(); private boolean cleanAndRemap = false; - private final boolean allowWrites; - MMapDataAccess( String name, String location, ByteOrder order, boolean allowWrites ) - { + MMapDataAccess(String name, String location, ByteOrder order, boolean allowWrites) { super(name, location, order); this.allowWrites = allowWrites; } - MMapDataAccess cleanAndRemap( boolean cleanAndRemap ) - { + MMapDataAccess cleanAndRemap(boolean cleanAndRemap) { this.cleanAndRemap = cleanAndRemap; return this; } - private void initRandomAccessFile() - { - if (raFile != null) - { + private void initRandomAccessFile() { + if (raFile != null) { return; } - try - { + try { // raFile necessary for loadExisting and create raFile = new RandomAccessFile(getFullName(), allowWrites ? "rw" : "r"); - } catch (IOException ex) - { + } catch (IOException ex) { throw new RuntimeException(ex); } } @Override - public MMapDataAccess create( long bytes ) - { - if (!segments.isEmpty()) - { + public MMapDataAccess create(long bytes) { + if (!segments.isEmpty()) { throw new IllegalThreadStateException("already created"); } initRandomAccessFile(); @@ -89,8 +81,7 @@ public MMapDataAccess create( long bytes ) } @Override - public DataAccess copyTo( DataAccess da ) - { + public DataAccess copyTo(DataAccess da) { // if(da instanceof MMapDataAccess) { // TODO PERFORMANCE make copying into mmap a lot faster via bytebuffer // also copying into RAMDataAccess could be faster via bytebuffer @@ -100,13 +91,11 @@ public DataAccess copyTo( DataAccess da ) } @Override - public boolean ensureCapacity( long bytes ) - { + public boolean ensureCapacity(long bytes) { return mapIt(HEADER_OFFSET, bytes, true); } - protected boolean mapIt( long offset, long byteCount, boolean clearNew ) - { + protected boolean mapIt(long offset, long byteCount, boolean clearNew) { if (byteCount < 0) throw new IllegalArgumentException("new capacity has to be strictly positive"); @@ -128,18 +117,15 @@ protected boolean mapIt( long offset, long byteCount, boolean clearNew ) int newSegments; int i = 0; long newFileLength = offset + segmentsToMap * longSegmentSize; - try - { + try { // ugly remapping // http://stackoverflow.com/q/14011919/194609 - if (cleanAndRemap) - { + if (cleanAndRemap) { newSegments = segmentsToMap; clean(0, segments.size()); Helper.cleanHack(); segments.clear(); - } else - { + } else { // This approach is probably problematic but a bit faster if done often. // Here we rely on the OS+file system that increasing the file // size has no effect on the old mappings! @@ -148,14 +134,12 @@ protected boolean mapIt( long offset, long byteCount, boolean clearNew ) } // rely on automatically increasing when mapping // raFile.setLength(newFileLength); - for (; i < newSegments; i++) - { + for (; i < newSegments; i++) { segments.add(newByteBuffer(bufferStart, longSegmentSize)); bufferStart += longSegmentSize; } return true; - } catch (IOException ex) - { + } catch (IOException ex) { // we could get an exception here if buffer is too small and area too large // e.g. I got an exception for the 65421th buffer (probably around 2**16 == 65536) throw new RuntimeException("Couldn't map buffer " + i + " of " + segmentsToMap @@ -164,40 +148,32 @@ protected boolean mapIt( long offset, long byteCount, boolean clearNew ) } } - private ByteBuffer newByteBuffer( long offset, long byteCount ) throws IOException - { + private ByteBuffer newByteBuffer(long offset, long byteCount) throws IOException { // If we request a buffer larger than the file length, it will automatically increase the file length! // Will this cause problems? http://stackoverflow.com/q/14011919/194609 // For trimTo we need to reset the file length later to reduce that size ByteBuffer buf = null; IOException ioex = null; // One retry if it fails. It could fail e.g. if previously buffer wasn't yet unmapped from the jvm - for (int trial = 0; trial < 1;) - { - try - { + for (int trial = 0; trial < 1;) { + try { buf = raFile.getChannel().map( allowWrites ? FileChannel.MapMode.READ_WRITE : FileChannel.MapMode.READ_ONLY, offset, byteCount); break; - } catch (IOException tmpex) - { + } catch (IOException tmpex) { ioex = tmpex; trial++; Helper.cleanHack(); - try - { + try { // mini sleep to let JVM do unmapping Thread.sleep(5); - } catch (InterruptedException iex) - { + } catch (InterruptedException iex) { } } } - if (buf == null) - { - if (ioex == null) - { + if (buf == null) { + if (ioex == null) { throw new AssertionError("internal problem as the exception 'ioex' shouldn't be null"); } throw ioex; @@ -206,16 +182,13 @@ private ByteBuffer newByteBuffer( long offset, long byteCount ) throws IOExcepti buf.order(byteOrder); boolean tmp = false; - if (tmp) - { + if (tmp) { int count = (int) (byteCount / EMPTY.length); - for (int i = 0; i < count; i++) - { + for (int i = 0; i < count; i++) { buf.put(EMPTY); } int len = (int) (byteCount % EMPTY.length); - if (len > 0) - { + if (len > 0) { buf.put(EMPTY, count * EMPTY.length, len); } } @@ -223,8 +196,7 @@ private ByteBuffer newByteBuffer( long offset, long byteCount ) throws IOExcepti } @Override - public boolean loadExisting() - { + public boolean loadExisting() { if (segments.size() > 0) throw new IllegalStateException("already initialized"); @@ -236,32 +208,26 @@ public boolean loadExisting() return false; initRandomAccessFile(); - try - { + try { long byteCount = readHeader(raFile); if (byteCount < 0) return false; mapIt(HEADER_OFFSET, byteCount - HEADER_OFFSET, false); return true; - } catch (IOException ex) - { + } catch (IOException ex) { throw new RuntimeException("Problem while loading " + getFullName(), ex); } } @Override - public void flush() - { + public void flush() { if (isClosed()) throw new IllegalStateException("already closed"); - try - { - if (!segments.isEmpty() && segments.get(0) instanceof MappedByteBuffer) - { - for (ByteBuffer bb : segments) - { + try { + if (!segments.isEmpty() && segments.get(0) instanceof MappedByteBuffer) { + for (ByteBuffer bb : segments) { ((MappedByteBuffer) bb).force(); } } @@ -271,25 +237,22 @@ public void flush() // http://stackoverflow.com/q/14011398/194609 raFile.getFD().sync(); // equivalent to raFile.getChannel().force(true); - } catch (Exception ex) - { + } catch (Exception ex) { throw new RuntimeException(ex); } } @Override - public void close() - { + public void close() { super.close(); close(true); } /** * @param forceClean if true the clean hack (system.gc) will be executed and forces the system - * to cleanup the mmap resources. Set false if you need to close many MMapDataAccess objects. + * to cleanup the mmap resources. Set false if you need to close many MMapDataAccess objects. */ - void close( boolean forceClean ) - { + void close(boolean forceClean) { clean(0, segments.size()); segments.clear(); Helper.close(raFile); @@ -298,40 +261,35 @@ void close( boolean forceClean ) } @Override - public final void setInt( long bytePos, int value ) - { + public final void setInt(long bytePos, int value) { int bufferIndex = (int) (bytePos >> segmentSizePower); int index = (int) (bytePos & indexDivisor); segments.get(bufferIndex).putInt(index, value); } @Override - public final int getInt( long bytePos ) - { + public final int getInt(long bytePos) { int bufferIndex = (int) (bytePos >> segmentSizePower); int index = (int) (bytePos & indexDivisor); return segments.get(bufferIndex).getInt(index); } @Override - public final void setShort( long bytePos, short value ) - { + public final void setShort(long bytePos, short value) { int bufferIndex = (int) (bytePos >>> segmentSizePower); int index = (int) (bytePos & indexDivisor); segments.get(bufferIndex).putShort(index, value); } @Override - public final short getShort( long bytePos ) - { + public final short getShort(long bytePos) { int bufferIndex = (int) (bytePos >>> segmentSizePower); int index = (int) (bytePos & indexDivisor); return segments.get(bufferIndex).getShort(index); } @Override - public void setBytes( long bytePos, byte[] values, int length ) - { + public void setBytes(long bytePos, byte[] values, int length) { assert length <= segmentSizeInBytes : "the length has to be smaller or equal to the segment size: " + length + " vs. " + segmentSizeInBytes; int bufferIndex = (int) (bytePos >>> segmentSizePower); int index = (int) (bytePos & indexDivisor); @@ -339,55 +297,47 @@ public void setBytes( long bytePos, byte[] values, int length ) bb.position(index); int delta = index + length - segmentSizeInBytes; - if (delta > 0) - { + if (delta > 0) { length -= delta; bb.put(values, 0, length); bb = segments.get(bufferIndex + 1); bb.position(0); bb.put(values, length, delta); - } else - { + } else { bb.put(values, 0, length); } } @Override - public void getBytes( long bytePos, byte[] values, int length ) - { + public void getBytes(long bytePos, byte[] values, int length) { assert length <= segmentSizeInBytes : "the length has to be smaller or equal to the segment size: " + length + " vs. " + segmentSizeInBytes; int bufferIndex = (int) (bytePos >>> segmentSizePower); int index = (int) (bytePos & indexDivisor); ByteBuffer bb = segments.get(bufferIndex); bb.position(index); int delta = index + length - segmentSizeInBytes; - if (delta > 0) - { + if (delta > 0) { length -= delta; bb.get(values, 0, length); bb = segments.get(bufferIndex + 1); bb.position(0); bb.get(values, length, delta); - } else - { + } else { bb.get(values, 0, length); } } @Override - public long getCapacity() - { + public long getCapacity() { long cap = 0; - for (ByteBuffer bb : segments) - { + for (ByteBuffer bb : segments) { cap += bb.capacity(); } return cap; } @Override - public int getSegments() - { + public int getSegments() { return segments.size(); } @@ -395,13 +345,12 @@ public int getSegments() * Cleans up MappedByteBuffers. Be sure you bring the segments list in a consistent state * afterwards. *

+ * * @param from inclusive - * @param to exclusive + * @param to exclusive */ - private void clean( int from, int to ) - { - for (int i = from; i < to; i++) - { + private void clean(int from, int to) { + for (int i = from; i < to; i++) { ByteBuffer bb = segments.get(i); Helper.cleanMappedByteBuffer(bb); segments.set(i, null); @@ -409,15 +358,12 @@ private void clean( int from, int to ) } @Override - public void trimTo( long capacity ) - { - if (capacity < segmentSizeInBytes) - { + public void trimTo(long capacity) { + if (capacity < segmentSizeInBytes) { capacity = segmentSizeInBytes; } int remainingSegNo = (int) (capacity / segmentSizeInBytes); - if (capacity % segmentSizeInBytes != 0) - { + if (capacity % segmentSizeInBytes != 0) { remainingSegNo++; } @@ -425,25 +371,20 @@ public void trimTo( long capacity ) Helper.cleanHack(); segments = new ArrayList(segments.subList(0, remainingSegNo)); - try - { + try { // windows does not allow changing the length of an open files - if (!Constants.WINDOWS) - // reduce file size - { + if (!Constants.WINDOWS) { + // reduce file size raFile.setLength(HEADER_OFFSET + remainingSegNo * segmentSizeInBytes); } - } catch (Exception ex) - { + } catch (Exception ex) { throw new RuntimeException(ex); } } - boolean releaseSegment( int segNumber ) - { + boolean releaseSegment(int segNumber) { ByteBuffer segment = segments.get(segNumber); - if (segment instanceof MappedByteBuffer) - { + if (segment instanceof MappedByteBuffer) { ((MappedByteBuffer) segment).force(); } @@ -454,10 +395,8 @@ boolean releaseSegment( int segNumber ) } @Override - public void rename( String newName ) - { - if (!checkBeforeRename(newName)) - { + public void rename(String newName) { + if (!checkBeforeRename(newName)) { return; } close(); @@ -470,8 +409,7 @@ public void rename( String newName ) } @Override - public DAType getType() - { + public DAType getType() { return DAType.MMAP; } } diff --git a/core/src/main/java/com/graphhopper/storage/MMapDirectory.java b/core/src/main/java/com/graphhopper/storage/MMapDirectory.java index aa4bb3995e8..c50f11e830d 100644 --- a/core/src/main/java/com/graphhopper/storage/MMapDirectory.java +++ b/core/src/main/java/com/graphhopper/storage/MMapDirectory.java @@ -20,20 +20,18 @@ /** * Manages memory mapped DataAccess objects. *

+ * * @author Peter Karich * @see MMapDataAccess */ -public class MMapDirectory extends GHDirectory -{ +public class MMapDirectory extends GHDirectory { // reserve the empty constructor for direct mapped memory - private MMapDirectory() - { + private MMapDirectory() { this(""); throw new IllegalStateException("reserved for direct mapped memory"); } - public MMapDirectory( String _location ) - { + public MMapDirectory(String _location) { super(_location, DAType.MMAP); } } diff --git a/core/src/main/java/com/graphhopper/storage/NativeFSLockFactory.java b/core/src/main/java/com/graphhopper/storage/NativeFSLockFactory.java index dd7b61ebdd1..5e7cc8ddd7f 100644 --- a/core/src/main/java/com/graphhopper/storage/NativeFSLockFactory.java +++ b/core/src/main/java/com/graphhopper/storage/NativeFSLockFactory.java @@ -28,30 +28,43 @@ /** * Creates a write lock file. Influenced by Lucene code *

+ * * @author Peter Karich */ -public class NativeFSLockFactory implements LockFactory -{ +public class NativeFSLockFactory implements LockFactory { private File lockDir; - public NativeFSLockFactory() - { + public NativeFSLockFactory() { } - public NativeFSLockFactory( File dir ) - { + public NativeFSLockFactory(File dir) { this.lockDir = dir; } + public static void main(String[] args) throws IOException { + // trying FileLock mechanics in different processes + File file = new File("tmp.lock"); + + file.createNewFile(); + FileChannel channel = new RandomAccessFile(file, "r").getChannel(); + + boolean shared = true; + FileLock lock1 = channel.tryLock(0, Long.MAX_VALUE, shared); + + System.out.println("locked " + lock1); + System.in.read(); + + System.out.println("release " + lock1); + lock1.release(); + } + @Override - public void setLockDir( File lockDir ) - { + public void setLockDir(File lockDir) { this.lockDir = lockDir; } @Override - public synchronized Lock create( String fileName, boolean writeAccess ) - { + public synchronized Lock create(String fileName, boolean writeAccess) { if (lockDir == null) throw new RuntimeException("Set lockDir before creating " + (writeAccess ? "write" : "read") + " locks"); @@ -59,10 +72,8 @@ public synchronized Lock create( String fileName, boolean writeAccess ) } @Override - public synchronized void forceRemove( String fileName, boolean writeAccess ) - { - if (lockDir.exists()) - { + public synchronized void forceRemove(String fileName, boolean writeAccess) { + if (lockDir.exists()) { create(fileName, writeAccess).release(); File lockFile = new File(lockDir, fileName); if (lockFile.exists() && !lockFile.delete()) @@ -70,21 +81,17 @@ public synchronized void forceRemove( String fileName, boolean writeAccess ) } } - static class NativeLock implements Lock - { - private RandomAccessFile tmpRaFile; - private FileChannel tmpChannel; - private FileLock tmpLock; - + static class NativeLock implements Lock { private final String name; private final File lockDir; private final File lockFile; private final boolean writeLock; - + private RandomAccessFile tmpRaFile; + private FileChannel tmpChannel; + private FileLock tmpLock; private Exception failedReason; - public NativeLock( File lockDir, String fileName, boolean writeLock ) - { + public NativeLock(File lockDir, String fileName, boolean writeLock) { this.name = fileName; this.lockDir = lockDir; this.lockFile = new File(lockDir, fileName); @@ -92,15 +99,13 @@ public NativeLock( File lockDir, String fileName, boolean writeLock ) } @Override - public synchronized boolean tryLock() - { + public synchronized boolean tryLock() { // already locked if (lockExists()) return false; // on-the-fly: make sure directory exists - if (!lockDir.exists()) - { + if (!lockDir.exists()) { if (!lockDir.mkdirs()) throw new RuntimeException("Directory " + lockDir + " does not exist and cannot created to place lock file there: " + lockFile); } @@ -108,39 +113,30 @@ public synchronized boolean tryLock() if (!lockDir.isDirectory()) throw new IllegalArgumentException("lockDir has to be a directory: " + lockDir); - try - { + try { failedReason = null; // we need write access even for read locks - in order to create the lock file! tmpRaFile = new RandomAccessFile(lockFile, "rw"); - } catch (IOException ex) - { + } catch (IOException ex) { failedReason = ex; return false; } - try - { + try { tmpChannel = tmpRaFile.getChannel(); - try - { + try { tmpLock = tmpChannel.tryLock(0, Long.MAX_VALUE, !writeLock); // OverlappingFileLockException is not an IOException! - } catch (Exception ex) - { + } catch (Exception ex) { failedReason = ex; - } finally - { - if (tmpLock == null) - { + } finally { + if (tmpLock == null) { Helper.close(tmpChannel); tmpChannel = null; } } - } finally - { - if (tmpChannel == null) - { + } finally { + if (tmpChannel == null) { Helper.close(tmpRaFile); tmpRaFile = null; } @@ -148,64 +144,49 @@ public synchronized boolean tryLock() return lockExists(); } - private synchronized boolean lockExists() - { + private synchronized boolean lockExists() { return tmpLock != null; } @Override - public synchronized boolean isLocked() - { + public synchronized boolean isLocked() { if (!lockFile.exists()) return false; if (lockExists()) return true; - try - { + try { boolean obtained = tryLock(); if (obtained) release(); return !obtained; - } catch (Exception ex) - { + } catch (Exception ex) { return false; } } @Override - public synchronized void release() - { - if (lockExists()) - { - try - { + public synchronized void release() { + if (lockExists()) { + try { failedReason = null; tmpLock.release(); - } catch (Exception ex) - { + } catch (Exception ex) { throw new RuntimeException(ex); - } finally - { + } finally { tmpLock = null; - try - { + try { tmpChannel.close(); - } catch (Exception ex) - { + } catch (Exception ex) { throw new RuntimeException(ex); - } finally - { + } finally { tmpChannel = null; - try - { + try { tmpRaFile.close(); - } catch (Exception ex) - { + } catch (Exception ex) { throw new RuntimeException(ex); - } finally - { + } finally { tmpRaFile = null; } } @@ -215,39 +196,18 @@ public synchronized void release() } @Override - public String getName() - { + public String getName() { return name; } @Override - public Exception getObtainFailedReason() - { + public Exception getObtainFailedReason() { return failedReason; } @Override - public String toString() - { + public String toString() { return lockFile.toString(); } } - - public static void main( String[] args ) throws IOException - { - // trying FileLock mechanics in different processes - File file = new File("tmp.lock"); - - file.createNewFile(); - FileChannel channel = new RandomAccessFile(file, "r").getChannel(); - - boolean shared = true; - FileLock lock1 = channel.tryLock(0, Long.MAX_VALUE, shared); - - System.out.println("locked " + lock1); - System.in.read(); - - System.out.println("release " + lock1); - lock1.release(); - } } diff --git a/core/src/main/java/com/graphhopper/storage/NodeAccess.java b/core/src/main/java/com/graphhopper/storage/NodeAccess.java index d5996f3c790..83d632544b5 100644 --- a/core/src/main/java/com/graphhopper/storage/NodeAccess.java +++ b/core/src/main/java/com/graphhopper/storage/NodeAccess.java @@ -24,22 +24,23 @@ * EdgeExplorer as it needs multiple instances for different threads or loops but without the need * for an additional iterator. *

+ * * @author Peter Karich */ -public interface NodeAccess extends PointAccess -{ +public interface NodeAccess extends PointAccess { /** * @return the additional value at the specified node index * @throws AssertionError if, and only if, the extendedStorage does not require an additional - * node field + * node field */ - int getAdditionalNodeField( int nodeId ); + int getAdditionalNodeField(int nodeId); /** * Sets the additional value at the specified node index *

+ * * @throws AssertionError if, and only if, the extendedStorage does not require an additional - * node field + * node field */ - void setAdditionalNodeField( int nodeId, int additionalValue ); + void setAdditionalNodeField(int nodeId, int additionalValue); } diff --git a/core/src/main/java/com/graphhopper/storage/RAMDataAccess.java b/core/src/main/java/com/graphhopper/storage/RAMDataAccess.java index e877ed0960e..9c312104ee4 100644 --- a/core/src/main/java/com/graphhopper/storage/RAMDataAccess.java +++ b/core/src/main/java/com/graphhopper/storage/RAMDataAccess.java @@ -17,27 +17,26 @@ */ package com.graphhopper.storage; +import org.slf4j.LoggerFactory; + import java.io.File; import java.io.IOException; import java.io.RandomAccessFile; import java.nio.ByteOrder; import java.util.Arrays; -import org.slf4j.LoggerFactory; - /** * This is an in-memory byte-based data structure with the possibility to be stored on flush(). * Thread safe. *

+ * * @author Peter Karich */ -public class RAMDataAccess extends AbstractDataAccess -{ +public class RAMDataAccess extends AbstractDataAccess { private byte[][] segments = new byte[0][]; private boolean store; - RAMDataAccess( String name, String location, boolean store, ByteOrder order ) - { + RAMDataAccess(String name, String location, boolean store, ByteOrder order) { super(name, location, order); this.store = store; } @@ -45,44 +44,37 @@ public class RAMDataAccess extends AbstractDataAccess /** * @param store true if in-memory data should be saved when calling flush */ - public RAMDataAccess store( boolean store ) - { + public RAMDataAccess store(boolean store) { this.store = store; return this; } @Override - public boolean isStoring() - { + public boolean isStoring() { return store; } @Override - public DataAccess copyTo( DataAccess da ) - { - if (da instanceof RAMDataAccess) - { + public DataAccess copyTo(DataAccess da) { + if (da instanceof RAMDataAccess) { copyHeader(da); RAMDataAccess rda = (RAMDataAccess) da; // TODO PERFORMANCE we could reuse rda segments! rda.segments = new byte[segments.length][]; - for (int i = 0; i < segments.length; i++) - { + for (int i = 0; i < segments.length; i++) { byte[] area = segments[i]; rda.segments[i] = Arrays.copyOf(area, area.length); } rda.setSegmentSize(segmentSizeInBytes); // leave id, store and close unchanged return da; - } else - { + } else { return super.copyTo(da); } } @Override - public RAMDataAccess create( long bytes ) - { + public RAMDataAccess create(long bytes) { if (segments.length > 0) throw new IllegalThreadStateException("already created"); @@ -93,8 +85,7 @@ public RAMDataAccess create( long bytes ) } @Override - public boolean ensureCapacity( long bytes ) - { + public boolean ensureCapacity(long bytes) { if (bytes < 0) throw new IllegalArgumentException("new capacity has to be strictly positive"); @@ -107,16 +98,13 @@ public boolean ensureCapacity( long bytes ) if (newBytes % segmentSizeInBytes != 0) segmentsToCreate++; - try - { + try { byte[][] newSegs = Arrays.copyOf(segments, segments.length + segmentsToCreate); - for (int i = segments.length; i < newSegs.length; i++) - { + for (int i = segments.length; i < newSegs.length; i++) { newSegs[i] = new byte[1 << segmentSizePower]; } segments = newSegs; - } catch (OutOfMemoryError err) - { + } catch (OutOfMemoryError err) { throw new OutOfMemoryError(err.getMessage() + " - problem when allocating new memory. Old capacity: " + cap + ", new bytes:" + newBytes + ", segmentSizeIntsPower:" + segmentSizePower + ", new segments:" + segmentsToCreate + ", existing:" + segments.length); @@ -125,8 +113,7 @@ public boolean ensureCapacity( long bytes ) } @Override - public boolean loadExisting() - { + public boolean loadExisting() { if (segments.length > 0) throw new IllegalStateException("already initialized"); @@ -140,11 +127,9 @@ public boolean loadExisting() if (!file.exists() || file.length() == 0) return false; - try - { + try { RandomAccessFile raFile = new RandomAccessFile(getFullName(), "r"); - try - { + try { long byteCount = readHeader(raFile) - HEADER_OFFSET; if (byteCount < 0) return false; @@ -156,8 +141,7 @@ public boolean loadExisting() segmentCount++; segments = new byte[segmentCount][]; - for (int s = 0; s < segmentCount; s++) - { + for (int s = 0; s < segmentCount; s++) { byte[] bytes = new byte[segmentSizeInBytes]; int read = raFile.read(bytes); if (read <= 0) @@ -166,52 +150,43 @@ public boolean loadExisting() segments[s] = bytes; } return true; - } finally - { + } finally { raFile.close(); } - } catch (IOException ex) - { + } catch (IOException ex) { throw new RuntimeException("Problem while loading " + getFullName(), ex); } } @Override - public void flush() - { + public void flush() { if (closed) throw new IllegalStateException("already closed"); if (!store) return; - try - { + try { RandomAccessFile raFile = new RandomAccessFile(getFullName(), "rw"); - try - { + try { long len = getCapacity(); writeHeader(raFile, len, segmentSizeInBytes); raFile.seek(HEADER_OFFSET); // raFile.writeInt() <- too slow, so copy into byte array - for (int s = 0; s < segments.length; s++) - { + for (int s = 0; s < segments.length; s++) { byte area[] = segments[s]; raFile.write(area); } - } finally - { + } finally { raFile.close(); } - } catch (Exception ex) - { + } catch (Exception ex) { throw new RuntimeException("Couldn't store bytes to " + toString(), ex); } } @Override - public final void setInt( long bytePos, int value ) - { + public final void setInt(long bytePos, int value) { assert segmentSizePower > 0 : "call create or loadExisting before usage!"; int bufferIndex = (int) (bytePos >>> segmentSizePower); int index = (int) (bytePos & indexDivisor); @@ -220,14 +195,12 @@ public final void setInt( long bytePos, int value ) } @Override - public final int getInt( long bytePos ) - { + public final int getInt(long bytePos) { assert segmentSizePower > 0 : "call create or loadExisting before usage!"; int bufferIndex = (int) (bytePos >>> segmentSizePower); int index = (int) (bytePos & indexDivisor); assert index + 4 <= segmentSizeInBytes : "integer cannot be distributed over two segments"; - if (bufferIndex > segments.length) - { + if (bufferIndex > segments.length) { LoggerFactory.getLogger(getClass()).error(getName() + ", segments:" + segments.length + ", bufIndex:" + bufferIndex + ", bytePos:" + bytePos + ", segPower:" + segmentSizePower); @@ -236,8 +209,7 @@ public final int getInt( long bytePos ) } @Override - public final void setShort( long bytePos, short value ) - { + public final void setShort(long bytePos, short value) { assert segmentSizePower > 0 : "call create or loadExisting before usage!"; int bufferIndex = (int) (bytePos >>> segmentSizePower); int index = (int) (bytePos & indexDivisor); @@ -246,8 +218,7 @@ public final void setShort( long bytePos, short value ) } @Override - public final short getShort( long bytePos ) - { + public final short getShort(long bytePos) { assert segmentSizePower > 0 : "call create or loadExisting before usage!"; int bufferIndex = (int) (bytePos >>> segmentSizePower); int index = (int) (bytePos & indexDivisor); @@ -256,72 +227,61 @@ public final short getShort( long bytePos ) } @Override - public void setBytes( long bytePos, byte[] values, int length ) - { + public void setBytes(long bytePos, byte[] values, int length) { assert length <= segmentSizeInBytes : "the length has to be smaller or equal to the segment size: " + length + " vs. " + segmentSizeInBytes; assert segmentSizePower > 0 : "call create or loadExisting before usage!"; int bufferIndex = (int) (bytePos >>> segmentSizePower); int index = (int) (bytePos & indexDivisor); byte[] seg = segments[bufferIndex]; int delta = index + length - segmentSizeInBytes; - if (delta > 0) - { + if (delta > 0) { length -= delta; System.arraycopy(values, 0, seg, index, length); seg = segments[bufferIndex + 1]; System.arraycopy(values, length, seg, 0, delta); - } else - { + } else { System.arraycopy(values, 0, seg, index, length); } } @Override - public void getBytes( long bytePos, byte[] values, int length ) - { + public void getBytes(long bytePos, byte[] values, int length) { assert length <= segmentSizeInBytes : "the length has to be smaller or equal to the segment size: " + length + " vs. " + segmentSizeInBytes; assert segmentSizePower > 0 : "call create or loadExisting before usage!"; int bufferIndex = (int) (bytePos >>> segmentSizePower); int index = (int) (bytePos & indexDivisor); byte[] seg = segments[bufferIndex]; int delta = index + length - segmentSizeInBytes; - if (delta > 0) - { + if (delta > 0) { length -= delta; System.arraycopy(seg, index, values, 0, length); seg = segments[bufferIndex + 1]; System.arraycopy(seg, 0, values, length, delta); - } else - { + } else { System.arraycopy(seg, index, values, 0, length); } } @Override - public void close() - { + public void close() { super.close(); segments = new byte[0][]; closed = true; } @Override - public long getCapacity() - { + public long getCapacity() { return (long) getSegments() * segmentSizeInBytes; } @Override - public int getSegments() - { + public int getSegments() { return segments.length; } @Override - public void trimTo( long capacity ) - { - if (capacity > getCapacity()) - { + public void trimTo(long capacity) { + if (capacity > getCapacity()) { throw new IllegalStateException("Cannot increase capacity (" + getCapacity() + ") to " + capacity + " via trimTo. Use ensureCapacity instead. "); } @@ -330,8 +290,7 @@ public void trimTo( long capacity ) capacity = segmentSizeInBytes; int remainingSegments = (int) (capacity / segmentSizeInBytes); - if (capacity % segmentSizeInBytes != 0) - { + if (capacity % segmentSizeInBytes != 0) { remainingSegments++; } @@ -339,14 +298,11 @@ public void trimTo( long capacity ) } @Override - public void rename( String newName ) - { - if (!checkBeforeRename(newName)) - { + public void rename(String newName) { + if (!checkBeforeRename(newName)) { return; } - if (store) - { + if (store) { super.rename(newName); } @@ -355,8 +311,7 @@ public void rename( String newName ) } @Override - public DAType getType() - { + public DAType getType() { if (isStoring()) return DAType.RAM_STORE; return DAType.RAM; diff --git a/core/src/main/java/com/graphhopper/storage/RAMDirectory.java b/core/src/main/java/com/graphhopper/storage/RAMDirectory.java index 2daac01da6a..c41e2c7e338 100644 --- a/core/src/main/java/com/graphhopper/storage/RAMDirectory.java +++ b/core/src/main/java/com/graphhopper/storage/RAMDirectory.java @@ -20,28 +20,25 @@ /** * Manages in-memory DataAccess objects. *

+ * * @author Peter Karich * @see RAMDataAccess * @see RAMIntDataAccess */ -public class RAMDirectory extends GHDirectory -{ - public RAMDirectory() - { +public class RAMDirectory extends GHDirectory { + public RAMDirectory() { this("", false); } - public RAMDirectory( String location ) - { + public RAMDirectory(String location) { this(location, false); } /** * @param store true if you want that the RAMDirectory can be loaded or saved on demand, false - * if it should be entirely in RAM + * if it should be entirely in RAM */ - public RAMDirectory( String _location, boolean store ) - { + public RAMDirectory(String _location, boolean store) { super(_location, store ? DAType.RAM_STORE : DAType.RAM); } } diff --git a/core/src/main/java/com/graphhopper/storage/RAMIntDataAccess.java b/core/src/main/java/com/graphhopper/storage/RAMIntDataAccess.java index 43c7bf9dda0..bacca7eb4c8 100644 --- a/core/src/main/java/com/graphhopper/storage/RAMIntDataAccess.java +++ b/core/src/main/java/com/graphhopper/storage/RAMIntDataAccess.java @@ -27,17 +27,16 @@ * This is an in-memory data structure based on an integer array. With the possibility to be stored * on flush(). *

+ * * @author Peter Karich */ -class RAMIntDataAccess extends AbstractDataAccess -{ +class RAMIntDataAccess extends AbstractDataAccess { private int[][] segments = new int[0][]; private boolean closed = false; private boolean store; private transient int segmentSizeIntsPower; - RAMIntDataAccess( String name, String location, boolean store, ByteOrder order ) - { + RAMIntDataAccess(String name, String location, boolean store, ByteOrder order) { super(name, location, order); this.store = store; } @@ -45,46 +44,38 @@ class RAMIntDataAccess extends AbstractDataAccess /** * @param store true if in-memory data should be saved when calling flush */ - public RAMIntDataAccess setStore( boolean store ) - { + public RAMIntDataAccess setStore(boolean store) { this.store = store; return this; } @Override - public boolean isStoring() - { + public boolean isStoring() { return store; } @Override - public DataAccess copyTo( DataAccess da ) - { - if (da instanceof RAMIntDataAccess) - { + public DataAccess copyTo(DataAccess da) { + if (da instanceof RAMIntDataAccess) { copyHeader(da); RAMIntDataAccess rda = (RAMIntDataAccess) da; // TODO PERFORMANCE we could reuse rda segments! rda.segments = new int[segments.length][]; - for (int i = 0; i < segments.length; i++) - { + for (int i = 0; i < segments.length; i++) { int[] area = segments[i]; rda.segments[i] = Arrays.copyOf(area, area.length); } rda.setSegmentSize(segmentSizeInBytes); // leave id, store and close unchanged return da; - } else - { + } else { return super.copyTo(da); } } @Override - public RAMIntDataAccess create( long bytes ) - { - if (segments.length > 0) - { + public RAMIntDataAccess create(long bytes) { + if (segments.length > 0) { throw new IllegalThreadStateException("already created"); } @@ -95,8 +86,7 @@ public RAMIntDataAccess create( long bytes ) } @Override - public boolean ensureCapacity( long bytes ) - { + public boolean ensureCapacity(long bytes) { if (bytes < 0) throw new IllegalArgumentException("new capacity has to be strictly positive"); @@ -109,17 +99,14 @@ public boolean ensureCapacity( long bytes ) if (newBytes % segmentSizeInBytes != 0) segmentsToCreate++; - try - { + try { int[][] newSegs = Arrays.copyOf(segments, segments.length + segmentsToCreate); - for (int i = segments.length; i < newSegs.length; i++) - { + for (int i = segments.length; i < newSegs.length; i++) { newSegs[i] = new int[1 << segmentSizeIntsPower]; } segments = newSegs; return true; - } catch (OutOfMemoryError err) - { + } catch (OutOfMemoryError err) { throw new OutOfMemoryError(err.getMessage() + " - problem when allocating new memory. Old capacity: " + cap + ", new bytes:" + newBytes + ", segmentSizeIntsPower:" + segmentSizeIntsPower + ", new segments:" + segmentsToCreate + ", existing:" + segments.length); @@ -127,8 +114,7 @@ public boolean ensureCapacity( long bytes ) } @Override - public boolean loadExisting() - { + public boolean loadExisting() { if (segments.length > 0) throw new IllegalStateException("already initialized"); @@ -139,18 +125,14 @@ public boolean loadExisting() return false; File file = new File(getFullName()); - if (!file.exists() || file.length() == 0) - { + if (!file.exists() || file.length() == 0) { return false; } - try - { + try { RandomAccessFile raFile = new RandomAccessFile(getFullName(), "r"); - try - { + try { long byteCount = readHeader(raFile) - HEADER_OFFSET; - if (byteCount < 0) - { + if (byteCount < 0) { return false; } byte[] bytes = new byte[segmentSizeInBytes]; @@ -161,71 +143,57 @@ public boolean loadExisting() segmentCount++; segments = new int[segmentCount][]; - for (int s = 0; s < segmentCount; s++) - { + for (int s = 0; s < segmentCount; s++) { int read = raFile.read(bytes) / 4; int area[] = new int[read]; - for (int j = 0; j < read; j++) - { + for (int j = 0; j < read; j++) { area[j] = bitUtil.toInt(bytes, j * 4); } segments[s] = area; } return true; - } finally - { + } finally { raFile.close(); } - } catch (IOException ex) - { + } catch (IOException ex) { throw new RuntimeException("Problem while loading " + getFullName(), ex); } } @Override - public void flush() - { - if (closed) - { + public void flush() { + if (closed) { throw new IllegalStateException("already closed"); } - if (!store) - { + if (!store) { return; } - try - { + try { RandomAccessFile raFile = new RandomAccessFile(getFullName(), "rw"); - try - { + try { long len = getCapacity(); writeHeader(raFile, len, segmentSizeInBytes); raFile.seek(HEADER_OFFSET); // raFile.writeInt() <- too slow, so copy into byte array - for (int s = 0; s < segments.length; s++) - { + for (int s = 0; s < segments.length; s++) { int area[] = segments[s]; int intLen = area.length; byte[] byteArea = new byte[intLen * 4]; - for (int i = 0; i < intLen; i++) - { + for (int i = 0; i < intLen; i++) { bitUtil.fromInt(byteArea, area[i], i * 4); } raFile.write(byteArea); } - } finally - { + } finally { raFile.close(); } - } catch (Exception ex) - { + } catch (Exception ex) { throw new RuntimeException("Couldn't store integers to " + toString(), ex); } } @Override - public final void setInt( long bytePos, int value ) - { + public final void setInt(long bytePos, int value) { assert segmentSizeIntsPower > 0 : "call create or loadExisting before usage!"; bytePos >>>= 2; int bufferIndex = (int) (bytePos >>> segmentSizeIntsPower); @@ -234,8 +202,7 @@ public final void setInt( long bytePos, int value ) } @Override - public final int getInt( long bytePos ) - { + public final int getInt(long bytePos) { assert segmentSizeIntsPower > 0 : "call create or loadExisting before usage!"; bytePos >>>= 2; int bufferIndex = (int) (bytePos >>> segmentSizeIntsPower); @@ -244,8 +211,7 @@ public final int getInt( long bytePos ) } @Override - public final void setShort( long bytePos, short value ) - { + public final void setShort(long bytePos, short value) { assert segmentSizeIntsPower > 0 : "call create or loadExisting before usage!"; if (bytePos % 4 != 0 && bytePos % 4 != 2) throw new IllegalMonitorStateException("bytePos of wrong multiple for RAMInt " + bytePos); @@ -260,8 +226,7 @@ public final void setShort( long bytePos, short value ) } @Override - public final short getShort( long bytePos ) - { + public final short getShort(long bytePos) { assert segmentSizeIntsPower > 0 : "call create or loadExisting before usage!"; if (bytePos % 4 != 0 && bytePos % 4 != 2) throw new IllegalMonitorStateException("bytePos of wrong multiple for RAMInt " + bytePos); @@ -276,40 +241,34 @@ public final short getShort( long bytePos ) } @Override - public void getBytes( long bytePos, byte[] values, int length ) - { + public void getBytes(long bytePos, byte[] values, int length) { throw new UnsupportedOperationException(toString() + " does not support byte based acccess. Use RAMDataAccess instead"); } @Override - public void setBytes( long bytePos, byte[] values, int length ) - { + public void setBytes(long bytePos, byte[] values, int length) { throw new UnsupportedOperationException(toString() + " does not support byte based acccess. Use RAMDataAccess instead"); } @Override - public void close() - { + public void close() { super.close(); segments = new int[0][]; closed = true; } @Override - public long getCapacity() - { + public long getCapacity() { return (long) getSegments() * segmentSizeInBytes; } @Override - public int getSegments() - { + public int getSegments() { return segments.length; } @Override - public DataAccess setSegmentSize( int bytes ) - { + public DataAccess setSegmentSize(int bytes) { super.setSegmentSize(bytes); segmentSizeIntsPower = (int) (Math.log(segmentSizeInBytes / 4) / Math.log(2)); indexDivisor = segmentSizeInBytes / 4 - 1; @@ -317,36 +276,29 @@ public DataAccess setSegmentSize( int bytes ) } @Override - public void trimTo( long capacity ) - { - if (capacity < segmentSizeInBytes) - { + public void trimTo(long capacity) { + if (capacity < segmentSizeInBytes) { capacity = segmentSizeInBytes; } int remainingSegments = (int) (capacity / segmentSizeInBytes); - if (capacity % segmentSizeInBytes != 0) - { + if (capacity % segmentSizeInBytes != 0) { remainingSegments++; } segments = Arrays.copyOf(segments, remainingSegments); } - boolean releaseSegment( int segNumber ) - { + boolean releaseSegment(int segNumber) { segments[segNumber] = null; return true; } @Override - public void rename( String newName ) - { - if (!checkBeforeRename(newName)) - { + public void rename(String newName) { + if (!checkBeforeRename(newName)) { return; } - if (store) - { + if (store) { super.rename(newName); } @@ -355,14 +307,12 @@ public void rename( String newName ) } @Override - protected boolean isIntBased() - { + protected boolean isIntBased() { return true; } @Override - public DAType getType() - { + public DAType getType() { if (isStoring()) return DAType.RAM_INT_STORE; return DAType.RAM_INT; diff --git a/core/src/main/java/com/graphhopper/storage/SPTEntry.java b/core/src/main/java/com/graphhopper/storage/SPTEntry.java index db055bb75a8..daaac4bcf88 100644 --- a/core/src/main/java/com/graphhopper/storage/SPTEntry.java +++ b/core/src/main/java/com/graphhopper/storage/SPTEntry.java @@ -20,17 +20,16 @@ /** * This class is used to create the shortest-path-tree from linked entities. *

+ * * @author Peter Karich */ -public class SPTEntry implements Cloneable, Comparable -{ +public class SPTEntry implements Cloneable, Comparable { public int edge; public int adjNode; public double weight; public SPTEntry parent; - public SPTEntry( int edgeId, int adjNode, double weight ) - { + public SPTEntry(int edgeId, int adjNode, double weight) { this.edge = edgeId; this.adjNode = adjNode; this.weight = weight; @@ -41,24 +40,20 @@ public SPTEntry( int edgeId, int adjNode, double weight ) * destination for the backward SPT. Where the variable 'weight' is used to let heap select * smallest *full* weight (from start to destination). */ - public double getWeightOfVisitedPath() - { + public double getWeightOfVisitedPath() { return weight; } @Override - public SPTEntry clone() - { + public SPTEntry clone() { return new SPTEntry(edge, adjNode, weight); } - public SPTEntry cloneFull() - { + public SPTEntry cloneFull() { SPTEntry de = clone(); SPTEntry tmpPrev = parent; SPTEntry cl = de; - while (tmpPrev != null) - { + while (tmpPrev != null) { cl.parent = tmpPrev.clone(); cl = cl.parent; tmpPrev = tmpPrev.parent; @@ -67,8 +62,7 @@ public SPTEntry cloneFull() } @Override - public int compareTo( SPTEntry o ) - { + public int compareTo(SPTEntry o) { if (weight < o.weight) return -1; @@ -77,8 +71,7 @@ public int compareTo( SPTEntry o ) } @Override - public String toString() - { + public String toString() { return adjNode + " (" + edge + ") weight: " + weight; } } diff --git a/core/src/main/java/com/graphhopper/storage/SimpleFSLockFactory.java b/core/src/main/java/com/graphhopper/storage/SimpleFSLockFactory.java index 616ff291756..82f1491f29b 100644 --- a/core/src/main/java/com/graphhopper/storage/SimpleFSLockFactory.java +++ b/core/src/main/java/com/graphhopper/storage/SimpleFSLockFactory.java @@ -23,30 +23,26 @@ /** * Creates a write lock file. Influenced by Lucene code *

+ * * @author Peter Karich */ -public class SimpleFSLockFactory implements LockFactory -{ +public class SimpleFSLockFactory implements LockFactory { private File lockDir; - public SimpleFSLockFactory() - { + public SimpleFSLockFactory() { } - public SimpleFSLockFactory( File dir ) - { + public SimpleFSLockFactory(File dir) { this.lockDir = dir; } @Override - public void setLockDir( File lockDir ) - { + public void setLockDir(File lockDir) { this.lockDir = lockDir; } @Override - public synchronized Lock create( String fileName, boolean writeAccess ) - { + public synchronized Lock create(String fileName, boolean writeAccess) { // TODO no read access-only support if (lockDir == null) throw new RuntimeException("Set lockDir before creating locks"); @@ -55,36 +51,30 @@ public synchronized Lock create( String fileName, boolean writeAccess ) } @Override - public synchronized void forceRemove( String fileName, boolean writeAccess ) - { - if (lockDir.exists()) - { + public synchronized void forceRemove(String fileName, boolean writeAccess) { + if (lockDir.exists()) { File lockFile = new File(lockDir, fileName); if (lockFile.exists() && !lockFile.delete()) throw new RuntimeException("Cannot delete " + lockFile); } } - static class SimpleLock implements Lock - { + static class SimpleLock implements Lock { private final File lockDir; private final File lockFile; private final String name; private IOException failedReason; - public SimpleLock( File lockDir, String fileName ) - { + public SimpleLock(File lockDir, String fileName) { this.name = fileName; this.lockDir = lockDir; this.lockFile = new File(lockDir, fileName); } @Override - public synchronized boolean tryLock() - { + public synchronized boolean tryLock() { // make sure directory exists, do it on-the-fly (not possible when setLockDir is called) - if (!lockDir.exists()) - { + if (!lockDir.exists()) { if (!lockDir.mkdirs()) throw new RuntimeException("Directory " + lockDir + " does not exist and cannot created to place lock file there: " + lockFile); } @@ -93,44 +83,37 @@ public synchronized boolean tryLock() if (!lockDir.isDirectory()) throw new IllegalArgumentException("lockDir has to be a directory: " + lockDir); - try - { + try { return lockFile.createNewFile(); - } catch (IOException ex) - { + } catch (IOException ex) { failedReason = ex; return false; } } @Override - public synchronized boolean isLocked() - { + public synchronized boolean isLocked() { return lockFile.exists(); } @Override - public synchronized void release() - { + public synchronized void release() { if (isLocked() && lockFile.exists() && !lockFile.delete()) throw new RuntimeException("Cannot release lock file: " + lockFile); } @Override - public String getName() - { + public String getName() { return name; } @Override - public synchronized Exception getObtainFailedReason() - { + public synchronized Exception getObtainFailedReason() { return failedReason; } @Override - public String toString() - { + public String toString() { return lockFile.toString(); } } diff --git a/core/src/main/java/com/graphhopper/storage/Storable.java b/core/src/main/java/com/graphhopper/storage/Storable.java index eff623ad8c9..ae1014cd8de 100644 --- a/core/src/main/java/com/graphhopper/storage/Storable.java +++ b/core/src/main/java/com/graphhopper/storage/Storable.java @@ -34,10 +34,10 @@ *

  • Finally do close() which does no flush()
  • * *

    + * * @author Peter Karich */ -public interface Storable extends Closeable -{ +public interface Storable extends Closeable { /** * @return true if successfully loaded from persistent storage. */ @@ -46,7 +46,7 @@ public interface Storable extends Closeable /** * Creates the underlying storage. First operation if it cannot be loaded. */ - T create( long byteCount ); + T create(long byteCount); /** * This method makes sure that the underlying data is written to the storage. Keep in mind that diff --git a/core/src/main/java/com/graphhopper/storage/StorableProperties.java b/core/src/main/java/com/graphhopper/storage/StorableProperties.java index a05b4f81367..cb3e04eb311 100644 --- a/core/src/main/java/com/graphhopper/storage/StorableProperties.java +++ b/core/src/main/java/com/graphhopper/storage/StorableProperties.java @@ -29,64 +29,55 @@ /** * Writes an in-memory HashMap into a file on flush. *

    + * * @author Peter Karich */ -public class StorableProperties implements Storable -{ +public class StorableProperties implements Storable { private final Map map = new LinkedHashMap(); private final DataAccess da; - public StorableProperties( Directory dir ) - { + public StorableProperties(Directory dir) { this.da = dir.find("properties"); // reduce size da.setSegmentSize(1 << 15); } @Override - public boolean loadExisting() - { + public boolean loadExisting() { if (!da.loadExisting()) return false; int len = (int) da.getCapacity(); byte[] bytes = new byte[len]; da.getBytes(0, bytes, len); - try - { + try { Helper.loadProperties(map, new StringReader(new String(bytes, Helper.UTF_CS))); return true; - } catch (IOException ex) - { + } catch (IOException ex) { throw new IllegalStateException(ex); } } @Override - public void flush() - { - try - { + public void flush() { + try { StringWriter sw = new StringWriter(); Helper.saveProperties(map, sw); // TODO at the moment the size is limited to da.segmentSize() ! byte[] bytes = sw.toString().getBytes(Helper.UTF_CS); da.setBytes(0, bytes, bytes.length); da.flush(); - } catch (IOException ex) - { + } catch (IOException ex) { throw new RuntimeException(ex); } } - public StorableProperties remove( String key ) - { + public StorableProperties remove(String key) { map.remove(key); return this; } - public StorableProperties put( String key, String val ) - { + public StorableProperties put(String key, String val) { map.put(key, val); return this; } @@ -94,8 +85,7 @@ public StorableProperties put( String key, String val ) /** * Before it saves this value it creates a string out of it. */ - public StorableProperties put( String key, Object val ) - { + public StorableProperties put(String key, Object val) { if (!key.equals(key.toLowerCase())) throw new IllegalArgumentException("Do not use upper case keys (" + key + ") for StorableProperties since 0.7"); @@ -103,8 +93,7 @@ public StorableProperties put( String key, Object val ) return this; } - public String get( String key ) - { + public String get(String key) { if (!key.equals(key.toLowerCase())) throw new IllegalArgumentException("Do not use upper case keys (" + key + ") for StorableProperties since 0.7"); @@ -116,32 +105,27 @@ public String get( String key ) } @Override - public void close() - { + public void close() { da.close(); } @Override - public boolean isClosed() - { + public boolean isClosed() { return da.isClosed(); } @Override - public StorableProperties create( long size ) - { + public StorableProperties create(long size) { da.create(size); return this; } @Override - public long getCapacity() - { + public long getCapacity() { return da.getCapacity(); } - public void putCurrentVersions() - { + public void putCurrentVersions() { put("nodes.version", Constants.VERSION_NODE); put("edges.version", Constants.VERSION_EDGE); put("geometry.version", Constants.VERSION_GEOMETRY); @@ -150,8 +134,7 @@ public void putCurrentVersions() put("shortcuts.version", Constants.VERSION_SHORTCUT); } - public String versionsToString() - { + public String versionsToString() { return get("nodes.version") + "," + get("edges.version") + "," + get("geometry.version") + "," @@ -159,8 +142,7 @@ public String versionsToString() + get("name_index.version"); } - public boolean checkVersions( boolean silent ) - { + public boolean checkVersions(boolean silent) { if (!check("nodes", Constants.VERSION_NODE, silent)) return false; @@ -184,11 +166,9 @@ public boolean checkVersions( boolean silent ) return true; } - boolean check( String key, int vers, boolean silent ) - { + boolean check(String key, int vers, boolean silent) { String str = get(key + ".version"); - if (!str.equals(vers + "")) - { + if (!str.equals(vers + "")) { if (silent) return false; @@ -199,16 +179,14 @@ boolean check( String key, int vers, boolean silent ) return true; } - public void copyTo( StorableProperties properties ) - { + public void copyTo(StorableProperties properties) { properties.map.clear(); properties.map.putAll(map); da.copyTo(properties.da); } @Override - public String toString() - { + public String toString() { return da.toString(); } } diff --git a/core/src/main/java/com/graphhopper/storage/SynchedDAWrapper.java b/core/src/main/java/com/graphhopper/storage/SynchedDAWrapper.java index 7bc31d42be4..0c257ce2212 100644 --- a/core/src/main/java/com/graphhopper/storage/SynchedDAWrapper.java +++ b/core/src/main/java/com/graphhopper/storage/SynchedDAWrapper.java @@ -20,154 +20,130 @@ /** * A simple wrapper to synchronize every DataAccess object. *

    + * * @author Peter Karich */ -class SynchedDAWrapper implements DataAccess -{ +class SynchedDAWrapper implements DataAccess { private final DataAccess inner; private final DAType type; - public SynchedDAWrapper( DataAccess inner ) - { + public SynchedDAWrapper(DataAccess inner) { this.inner = inner; this.type = new DAType(inner.getType(), true); } @Override - public synchronized String getName() - { + public synchronized String getName() { return inner.getName(); } @Override - public synchronized void rename( String newName ) - { + public synchronized void rename(String newName) { inner.rename(newName); } @Override - public synchronized void setInt( long bytePos, int value ) - { + public synchronized void setInt(long bytePos, int value) { inner.setInt(bytePos, value); } @Override - public synchronized int getInt( long bytePos ) - { + public synchronized int getInt(long bytePos) { return inner.getInt(bytePos); } @Override - public synchronized void setShort( long bytePos, short value ) - { + public synchronized void setShort(long bytePos, short value) { inner.setShort(bytePos, value); } @Override - public synchronized short getShort( long bytePos ) - { + public synchronized short getShort(long bytePos) { return inner.getShort(bytePos); } @Override - public synchronized void setBytes( long bytePos, byte[] values, int length ) - { + public synchronized void setBytes(long bytePos, byte[] values, int length) { inner.setBytes(bytePos, values, length); } @Override - public synchronized void getBytes( long bytePos, byte[] values, int length ) - { + public synchronized void getBytes(long bytePos, byte[] values, int length) { inner.getBytes(bytePos, values, length); } @Override - public synchronized void setHeader( int bytePos, int value ) - { + public synchronized void setHeader(int bytePos, int value) { inner.setHeader(bytePos, value); } @Override - public synchronized int getHeader( int bytePos ) - { + public synchronized int getHeader(int bytePos) { return inner.getHeader(bytePos); } @Override - public synchronized DataAccess create( long bytes ) - { + public synchronized DataAccess create(long bytes) { return inner.create(bytes); } @Override - public synchronized boolean ensureCapacity( long bytes ) - { + public synchronized boolean ensureCapacity(long bytes) { return inner.ensureCapacity(bytes); } @Override - public synchronized void trimTo( long bytes ) - { + public synchronized void trimTo(long bytes) { inner.trimTo(bytes); } @Override - public synchronized DataAccess copyTo( DataAccess da ) - { + public synchronized DataAccess copyTo(DataAccess da) { return inner.copyTo(da); } @Override - public synchronized DataAccess setSegmentSize( int bytes ) - { + public synchronized DataAccess setSegmentSize(int bytes) { return inner.setSegmentSize(bytes); } @Override - public synchronized int getSegmentSize() - { + public synchronized int getSegmentSize() { return inner.getSegmentSize(); } @Override - public synchronized int getSegments() - { + public synchronized int getSegments() { return inner.getSegments(); } @Override - public synchronized boolean loadExisting() - { + public synchronized boolean loadExisting() { return inner.loadExisting(); } @Override - public synchronized void flush() - { + public synchronized void flush() { inner.flush(); } @Override - public synchronized void close() - { + public synchronized void close() { inner.close(); } @Override - public boolean isClosed() - { + public boolean isClosed() { return inner.isClosed(); } @Override - public synchronized long getCapacity() - { + public synchronized long getCapacity() { return inner.getCapacity(); } @Override - public DAType getType() - { + public DAType getType() { return type; } } diff --git a/core/src/main/java/com/graphhopper/storage/TurnCostExtension.java b/core/src/main/java/com/graphhopper/storage/TurnCostExtension.java index a0cef3b3bdb..6d66721be12 100644 --- a/core/src/main/java/com/graphhopper/storage/TurnCostExtension.java +++ b/core/src/main/java/com/graphhopper/storage/TurnCostExtension.java @@ -24,11 +24,11 @@ * towards the first entry within a node cost table to identify turn restrictions, or later, turn * getCosts. *

    + * * @author Karl Hübner * @author Peter Karich */ -public class TurnCostExtension implements GraphExtension -{ +public class TurnCostExtension implements GraphExtension { /* pointer for no cost entry */ private static final int NO_TURN_ENTRY = -1; private static final long EMPTY_FLAGS = 0L; @@ -45,8 +45,7 @@ public class TurnCostExtension implements GraphExtension private int turnCostsCount; private NodeAccess nodeAccess; - public TurnCostExtension() - { + public TurnCostExtension() { TC_FROM = nextTurnCostEntryIndex(); TC_TO = nextTurnCostEntryIndex(); TC_FLAGS = nextTurnCostEntryIndex(); @@ -56,8 +55,7 @@ public TurnCostExtension() } @Override - public void init( Graph graph, Directory dir ) - { + public void init(Graph graph, Directory dir) { if (turnCostsCount > 0) throw new AssertionError("The turn cost storage must be initialized only once."); @@ -65,48 +63,41 @@ public void init( Graph graph, Directory dir ) this.turnCosts = dir.find("turn_costs"); } - private int nextTurnCostEntryIndex() - { + private int nextTurnCostEntryIndex() { turnCostsEntryIndex += 4; return turnCostsEntryIndex; } @Override - public void setSegmentSize( int bytes ) - { + public void setSegmentSize(int bytes) { turnCosts.setSegmentSize(bytes); } @Override - public TurnCostExtension create( long initBytes ) - { + public TurnCostExtension create(long initBytes) { turnCosts.create((long) initBytes * turnCostsEntryBytes); return this; } @Override - public void flush() - { + public void flush() { turnCosts.setHeader(0, turnCostsEntryBytes); turnCosts.setHeader(1 * 4, turnCostsCount); turnCosts.flush(); } @Override - public void close() - { + public void close() { turnCosts.close(); } @Override - public long getCapacity() - { + public long getCapacity() { return turnCosts.getCapacity(); } @Override - public boolean loadExisting() - { + public boolean loadExisting() { if (!turnCosts.loadExisting()) return false; @@ -119,12 +110,11 @@ public boolean loadExisting() * This method adds a new entry which is a turn restriction or cost information via the * turnFlags. */ - public void addTurnInfo( int fromEdge, int viaNode, int toEdge, long turnFlags ) - { + public void addTurnInfo(int fromEdge, int viaNode, int toEdge, long turnFlags) { // no need to store turn information if (turnFlags == EMPTY_FLAGS) return; - + // append int newEntryIndex = turnCostsCount; turnCostsCount++; @@ -132,20 +122,16 @@ public void addTurnInfo( int fromEdge, int viaNode, int toEdge, long turnFlags ) // determine if we already have an cost entry for this node int previousEntryIndex = nodeAccess.getAdditionalNodeField(viaNode); - if (previousEntryIndex == NO_TURN_ENTRY) - { + if (previousEntryIndex == NO_TURN_ENTRY) { // set cost-pointer to this new cost entry nodeAccess.setAdditionalNodeField(viaNode, newEntryIndex); - } else - { + } else { int i = 0; int tmp = previousEntryIndex; - while ((tmp = turnCosts.getInt((long) tmp * turnCostsEntryBytes + TC_NEXT)) != NO_TURN_ENTRY) - { + while ((tmp = turnCosts.getInt((long) tmp * turnCostsEntryBytes + TC_NEXT)) != NO_TURN_ENTRY) { previousEntryIndex = tmp; // search for the last added cost entry - if (i++ > 1000) - { + if (i++ > 1000) { throw new IllegalStateException("Something unexpected happened. A node probably will not have 1000+ relations."); } } @@ -164,8 +150,7 @@ public void addTurnInfo( int fromEdge, int viaNode, int toEdge, long turnFlags ) /** * @return turn flags of the specified node and edge properties. */ - public long getTurnCostFlags( int edgeFrom, int nodeVia, int edgeTo ) - { + public long getTurnCostFlags(int edgeFrom, int nodeVia, int edgeTo) { if (edgeFrom == EdgeIterator.NO_EDGE || edgeTo == EdgeIterator.NO_EDGE) throw new IllegalArgumentException("from and to edge cannot be NO_EDGE"); if (nodeVia < 0) @@ -174,17 +159,14 @@ public long getTurnCostFlags( int edgeFrom, int nodeVia, int edgeTo ) return nextCostFlags(edgeFrom, nodeVia, edgeTo); } - private long nextCostFlags( int edgeFrom, int nodeVia, int edgeTo ) - { + private long nextCostFlags(int edgeFrom, int nodeVia, int edgeTo) { int turnCostIndex = nodeAccess.getAdditionalNodeField(nodeVia); int i = 0; - for (; i < 1000; i++) - { + for (; i < 1000; i++) { if (turnCostIndex == NO_TURN_ENTRY) break; long turnCostPtr = (long) turnCostIndex * turnCostsEntryBytes; - if (edgeFrom == turnCosts.getInt(turnCostPtr + TC_FROM)) - { + if (edgeFrom == turnCosts.getInt(turnCostPtr + TC_FROM)) { if (edgeTo == turnCosts.getInt(turnCostPtr + TC_TO)) return turnCosts.getInt(turnCostPtr + TC_FLAGS); } @@ -201,41 +183,34 @@ private long nextCostFlags( int edgeFrom, int nodeVia, int edgeTo ) return EMPTY_FLAGS; } - private void ensureTurnCostIndex( int nodeIndex ) - { + private void ensureTurnCostIndex(int nodeIndex) { turnCosts.ensureCapacity(((long) nodeIndex + 4) * turnCostsEntryBytes); } @Override - public boolean isRequireNodeField() - { + public boolean isRequireNodeField() { //we require the additional field in the graph to point to the first entry in the node table return true; } @Override - public boolean isRequireEdgeField() - { + public boolean isRequireEdgeField() { return false; } @Override - public int getDefaultNodeFieldValue() - { + public int getDefaultNodeFieldValue() { return NO_TURN_ENTRY; } @Override - public int getDefaultEdgeFieldValue() - { + public int getDefaultEdgeFieldValue() { throw new UnsupportedOperationException("Not supported by this storage"); } @Override - public GraphExtension copyTo( GraphExtension clonedStorage ) - { - if (!(clonedStorage instanceof TurnCostExtension)) - { + public GraphExtension copyTo(GraphExtension clonedStorage) { + if (!(clonedStorage instanceof TurnCostExtension)) { throw new IllegalStateException("the extended storage to clone must be the same"); } @@ -248,14 +223,12 @@ public GraphExtension copyTo( GraphExtension clonedStorage ) } @Override - public boolean isClosed() - { + public boolean isClosed() { return turnCosts.isClosed(); } @Override - public String toString() - { + public String toString() { return "turn_cost"; } } diff --git a/core/src/main/java/com/graphhopper/storage/UnsafeDataAccess.java b/core/src/main/java/com/graphhopper/storage/UnsafeDataAccess.java index 915ffa996f4..93833622473 100644 --- a/core/src/main/java/com/graphhopper/storage/UnsafeDataAccess.java +++ b/core/src/main/java/com/graphhopper/storage/UnsafeDataAccess.java @@ -36,26 +36,23 @@ *

    * 3. Cannot be used on Android as no memory allocation methods are available there *

    + * * @author Peter Karich */ @NotThreadSafe -public class UnsafeDataAccess extends AbstractDataAccess -{ +public class UnsafeDataAccess extends AbstractDataAccess { @SuppressWarnings("all") static final sun.misc.Unsafe UNSAFE; - static - { - try - { + static { + try { // On Android getting Unsafe fails as the field is named THE_ONE but Android has no memory allocation methods so it won't work nevertheless. // On Android we need JNI+malloc https://github.com/libgdx/libgdx/blob/5945211a88570ced7eafce95c68f6f1f7124cd23/gdx/src/com/badlogic/gdx/utils/BufferUtils.java#L287 @SuppressWarnings("all") Field field = sun.misc.Unsafe.class.getDeclaredField("theUnsafe"); field.setAccessible(true); UNSAFE = (sun.misc.Unsafe) field.get(null); - } catch (Exception e) - { + } catch (Exception e) { throw new AssertionError(e); } } @@ -63,14 +60,12 @@ public class UnsafeDataAccess extends AbstractDataAccess private long address; private long capacity; - UnsafeDataAccess( String name, String location, ByteOrder order ) - { + UnsafeDataAccess(String name, String location, ByteOrder order) { super(name, location, order); } @Override - public UnsafeDataAccess create( long bytes ) - { + public UnsafeDataAccess create(long bytes) { // TODO use unsafe.pageSize() instead segmentSizeInBytes? // e.g. on my system pageSize is only 4096 setSegmentSize(segmentSizeInBytes); @@ -79,13 +74,11 @@ public UnsafeDataAccess create( long bytes ) } @Override - public final boolean ensureCapacity( long bytes ) - { + public final boolean ensureCapacity(long bytes) { return ensureCapacity(bytes, true); } - final boolean ensureCapacity( long bytes, boolean clearNewMem ) - { + final boolean ensureCapacity(long bytes, boolean clearNewMem) { long oldCap = getCapacity(); long newBytes = bytes - oldCap; if (newBytes <= 0) @@ -97,11 +90,9 @@ final boolean ensureCapacity( long bytes, boolean clearNewMem ) allSegments++; capacity = allSegments * segmentSizeInBytes; - try - { + try { address = UNSAFE.reallocateMemory(address, capacity); - } catch (OutOfMemoryError err) - { + } catch (OutOfMemoryError err) { throw new OutOfMemoryError(err.getMessage() + " - problem when allocating new memory. Old capacity: " + oldCap + ", new bytes:" + newBytes + ", segmentSizeIntsPower:" + segmentSizePower); } @@ -112,10 +103,8 @@ final boolean ensureCapacity( long bytes, boolean clearNewMem ) } @Override - public DataAccess copyTo( DataAccess da ) - { - if (da instanceof UnsafeDataAccess) - { + public DataAccess copyTo(DataAccess da) { + if (da instanceof UnsafeDataAccess) { // TODO unsafe.copyMemory(address, da.address, capacity); // return this; } @@ -123,8 +112,7 @@ public DataAccess copyTo( DataAccess da ) } @Override - public boolean loadExisting() - { + public boolean loadExisting() { if (isClosed()) throw new IllegalStateException("already closed"); @@ -132,11 +120,9 @@ public boolean loadExisting() if (!file.exists() || file.length() == 0) return false; - try - { + try { RandomAccessFile raFile = new RandomAccessFile(getFullName(), "r"); - try - { + try { long byteCount = readHeader(raFile) - HEADER_OFFSET; if (byteCount < 0) return false; @@ -148,8 +134,7 @@ public boolean loadExisting() ensureCapacity(byteCount, false); byte[] bytes = new byte[segmentSizeInBytes]; - for (int s = 0; s < segmentCount; s++) - { + for (int s = 0; s < segmentCount; s++) { int read = raFile.read(bytes); if (read <= 0) throw new IllegalStateException("segment " + s + " is empty? " + toString()); @@ -158,112 +143,92 @@ public boolean loadExisting() setBytes(s * segmentSizeInBytes, bytes, segmentSizeInBytes); } return true; - } finally - { + } finally { raFile.close(); } - } catch (IOException ex) - { + } catch (IOException ex) { throw new RuntimeException("Problem while loading " + getFullName(), ex); } } @Override - public void flush() - { + public void flush() { if (isClosed()) throw new IllegalStateException("already closed"); - try - { + try { RandomAccessFile raFile = new RandomAccessFile(getFullName(), "rw"); - try - { + try { long len = getCapacity(); writeHeader(raFile, len, segmentSizeInBytes); raFile.seek(HEADER_OFFSET); byte bytes[] = new byte[segmentSizeInBytes]; int segs = getSegments(); - for (int s = 0; s < segs; s++) - { + for (int s = 0; s < segs; s++) { getBytes(s * segmentSizeInBytes, bytes, segmentSizeInBytes); raFile.write(bytes); } - } finally - { + } finally { raFile.close(); } - } catch (Exception ex) - { + } catch (Exception ex) { throw new RuntimeException("Couldn't store bytes to " + toString(), ex); } } @Override - public void close() - { + public void close() { super.close(); UNSAFE.freeMemory(address); } @Override - public final void setInt( long bytePos, int value ) - { + public final void setInt(long bytePos, int value) { UNSAFE.putInt(address + bytePos, value); } @Override - public final int getInt( long bytePos ) - { + public final int getInt(long bytePos) { return UNSAFE.getInt(address + bytePos); } @Override - public short getShort( long bytePos ) - { + public short getShort(long bytePos) { return UNSAFE.getShort(address + bytePos); } @Override - public void setShort( long bytePos, short value ) - { + public void setShort(long bytePos, short value) { UNSAFE.putShort(address + bytePos, value); } @Override - public final void setBytes( long bytePos, byte[] values, int length ) - { - for (int offset = 0; offset < length; offset++) - { + public final void setBytes(long bytePos, byte[] values, int length) { + for (int offset = 0; offset < length; offset++) { UNSAFE.putByte(address + bytePos + offset, values[offset]); } } @Override - public final void getBytes( long bytePos, byte[] values, int length ) - { + public final void getBytes(long bytePos, byte[] values, int length) { assert length <= segmentSizeInBytes : "the length has to be smaller or equal to the segment size: " + length + " vs. " + segmentSizeInBytes; - for (int offset = 0; offset < length; offset++) - { + for (int offset = 0; offset < length; offset++) { values[offset] = UNSAFE.getByte(address + bytePos + offset); } } @Override - public final long getCapacity() - { + public final long getCapacity() { return capacity; } @Override - public final int getSegments() - { + public final int getSegments() { return (int) (capacity / segmentSizeInBytes); } @Override - public final void trimTo( long bytes ) - { + public final void trimTo(long bytes) { if (bytes > this.capacity) throw new IllegalStateException("Use ensureCapacity to increase capacity!"); @@ -276,8 +241,7 @@ public final void trimTo( long bytes ) } @Override - public DAType getType() - { + public DAType getType() { return DAType.UNSAFE_STORE; } } diff --git a/core/src/main/java/com/graphhopper/storage/VLongStorage.java b/core/src/main/java/com/graphhopper/storage/VLongStorage.java index 9b15d1d057b..bdf7402efd0 100644 --- a/core/src/main/java/com/graphhopper/storage/VLongStorage.java +++ b/core/src/main/java/com/graphhopper/storage/VLongStorage.java @@ -19,53 +19,43 @@ * Taken from lucene DataOutput. VLong's are longs which have variable length depending on the size. * When used together with 'delta compression' it is likely that you'll use only 1 byte per value. */ -public class VLongStorage -{ +public class VLongStorage { private byte[] bytes; private int pointer = 0; - public VLongStorage() - { + public VLongStorage() { this(10); } - public VLongStorage( int cap ) - { + public VLongStorage(int cap) { this(new byte[cap]); } - public VLongStorage( byte[] bytes ) - { + public VLongStorage(byte[] bytes) { this.bytes = bytes; } - public void seek( long pos ) - { + public void seek(long pos) { // TODO long vs. int pointer = (int) pos; } - public long getPosition() - { + public long getPosition() { return pointer; } - public long getLength() - { + public long getLength() { return bytes.length; } - byte readByte() - { + byte readByte() { byte b = bytes[pointer]; pointer++; return b; } - void writeByte( byte b ) - { - if (pointer >= bytes.length) - { + void writeByte(byte b) { + if (pointer >= bytes.length) { int cap = Math.max(10, (int) (pointer * 1.5f)); bytes = Arrays.copyOf(bytes, cap); } @@ -81,11 +71,9 @@ void writeByte( byte b ) *

    * See DataInput readVLong of Lucene */ - public final void writeVLong( long i ) - { + public final void writeVLong(long i) { assert i >= 0L; - while ((i & ~0x7FL) != 0L) - { + while ((i & ~0x7FL) != 0L) { writeByte((byte) ((i & 0x7FL) | 0x80L)); i >>>= 7; } @@ -98,8 +86,7 @@ public final void writeVLong( long i ) *

    * The format is described further in DataOutput writeVInt(int) from Lucene. */ - public long readVLong() - { + public long readVLong() { /* This is the original code of this method, * but a Hotspot bug (see LUCENE-2975) corrupts the for-loop if * readByte() is inlined. So the loop was unwinded! @@ -112,74 +99,62 @@ public long readVLong() return i; */ byte b = readByte(); - if (b >= 0) - { + if (b >= 0) { return b; } long i = b & 0x7FL; b = readByte(); i |= (b & 0x7FL) << 7; - if (b >= 0) - { + if (b >= 0) { return i; } b = readByte(); i |= (b & 0x7FL) << 14; - if (b >= 0) - { + if (b >= 0) { return i; } b = readByte(); i |= (b & 0x7FL) << 21; - if (b >= 0) - { + if (b >= 0) { return i; } b = readByte(); i |= (b & 0x7FL) << 28; - if (b >= 0) - { + if (b >= 0) { return i; } b = readByte(); i |= (b & 0x7FL) << 35; - if (b >= 0) - { + if (b >= 0) { return i; } b = readByte(); i |= (b & 0x7FL) << 42; - if (b >= 0) - { + if (b >= 0) { return i; } b = readByte(); i |= (b & 0x7FL) << 49; - if (b >= 0) - { + if (b >= 0) { return i; } b = readByte(); i |= (b & 0x7FL) << 56; - if (b >= 0) - { + if (b >= 0) { return i; } throw new RuntimeException("Invalid vLong detected (negative values disallowed)"); } - public void trimToSize() - { - if (bytes.length > pointer) - { + public void trimToSize() { + if (bytes.length > pointer) { byte[] tmp = new byte[pointer]; System.arraycopy(bytes, 0, tmp, 0, pointer); bytes = tmp; } } - public byte[] getBytes() - { + public byte[] getBytes() { return bytes; } } diff --git a/core/src/main/java/com/graphhopper/storage/index/BresenhamLine.java b/core/src/main/java/com/graphhopper/storage/index/BresenhamLine.java index 7a40f6b3820..316f0d6af41 100644 --- a/core/src/main/java/com/graphhopper/storage/index/BresenhamLine.java +++ b/core/src/main/java/com/graphhopper/storage/index/BresenhamLine.java @@ -26,38 +26,33 @@ * aliasing). See some discussion here: http://stackoverflow.com/a/3234074/194609 and here * http://stackoverflow.com/q/24679963/194609 *

    + * * @author Peter Karich */ -public class BresenhamLine -{ - public static void calcPoints( int y1, int x1, int y2, int x2, PointEmitter emitter ) - { +public class BresenhamLine { + public static void calcPoints(int y1, int x1, int y2, int x2, PointEmitter emitter) { bresenham(y1, x1, y2, x2, emitter); } - public static void bresenham( int y1, int x1, int y2, int x2, PointEmitter emitter ) - { + public static void bresenham(int y1, int x1, int y2, int x2, PointEmitter emitter) { boolean latIncreasing = y1 < y2; boolean lonIncreasing = x1 < x2; int dLat = Math.abs(y2 - y1), sLat = latIncreasing ? 1 : -1; int dLon = Math.abs(x2 - x1), sLon = lonIncreasing ? 1 : -1; int err = dLon - dLat; - while (true) - { + while (true) { emitter.set(y1, x1); if (y1 == y2 && x1 == x2) break; int tmpErr = 2 * err; - if (tmpErr > -dLat) - { + if (tmpErr > -dLat) { err -= dLat; x1 += sLon; } - if (tmpErr < dLon) - { + if (tmpErr < dLon) { err += dLon; y1 += sLat; } @@ -67,22 +62,19 @@ public static void bresenham( int y1, int x1, int y2, int x2, PointEmitter emitt /** * Calls the Bresenham algorithm but make it working for double values */ - public static void calcPoints( final double lat1, final double lon1, - final double lat2, final double lon2, - final PointEmitter emitter, - final double offsetLat, final double offsetLon, - final double deltaLat, final double deltaLon ) - { + public static void calcPoints(final double lat1, final double lon1, + final double lat2, final double lon2, + final PointEmitter emitter, + final double offsetLat, final double offsetLon, + final double deltaLat, final double deltaLon) { // round to make results of bresenham closer to correct solution int y1 = (int) ((lat1 - offsetLat) / deltaLat); int x1 = (int) ((lon1 - offsetLon) / deltaLon); int y2 = (int) ((lat2 - offsetLat) / deltaLat); int x2 = (int) ((lon2 - offsetLon) / deltaLon); - bresenham(y1, x1, y2, x2, new PointEmitter() - { + bresenham(y1, x1, y2, x2, new PointEmitter() { @Override - public void set( double lat, double lon ) - { + public void set(double lat, double lon) { // +.1 to move more near the center of the tile emitter.set((lat + .1) * deltaLat + offsetLat, (lon + .1) * deltaLon + offsetLon); } diff --git a/core/src/main/java/com/graphhopper/storage/index/Location2IDFullIndex.java b/core/src/main/java/com/graphhopper/storage/index/Location2IDFullIndex.java index 7190394a9e8..58aa139d31e 100644 --- a/core/src/main/java/com/graphhopper/storage/index/Location2IDFullIndex.java +++ b/core/src/main/java/com/graphhopper/storage/index/Location2IDFullIndex.java @@ -28,30 +28,27 @@ /** * Very slow O(n) LocationIndex but no RAM/disc required. *

    + * * @author Peter Karich */ -public class Location2IDFullIndex implements LocationIndex -{ - private DistanceCalc calc = Helper.DIST_PLANE; +public class Location2IDFullIndex implements LocationIndex { private final Graph graph; private final NodeAccess nodeAccess; + private DistanceCalc calc = Helper.DIST_PLANE; private boolean closed = false; - public Location2IDFullIndex( Graph g ) - { + public Location2IDFullIndex(Graph g) { this.graph = g; this.nodeAccess = g.getNodeAccess(); } @Override - public boolean loadExisting() - { + public boolean loadExisting() { return true; } @Override - public LocationIndex setApproximation( boolean approxDist ) - { + public LocationIndex setApproximation(boolean approxDist) { if (approxDist) calc = Helper.DIST_PLANE; else @@ -61,45 +58,37 @@ public LocationIndex setApproximation( boolean approxDist ) } @Override - public LocationIndex setResolution( int resolution ) - { + public LocationIndex setResolution(int resolution) { return this; } @Override - public LocationIndex prepareIndex() - { + public LocationIndex prepareIndex() { return this; } @Override - public QueryResult findClosest( double queryLat, double queryLon, EdgeFilter edgeFilter ) - { + public QueryResult findClosest(double queryLat, double queryLon, EdgeFilter edgeFilter) { if (isClosed()) throw new IllegalStateException("You need to create a new LocationIndex instance as it is already closed"); QueryResult res = new QueryResult(queryLat, queryLon); Circle circle = null; AllEdgesIterator iter = graph.getAllEdges(); - while (iter.next()) - { + while (iter.next()) { if (!edgeFilter.accept(iter)) continue; - for (int node, i = 0; i < 2; i++) - { - if (i == 0) - { + for (int node, i = 0; i < 2; i++) { + if (i == 0) { node = iter.getBaseNode(); - } else - { + } else { node = iter.getAdjNode(); } double tmpLat = nodeAccess.getLatitude(node); double tmpLon = nodeAccess.getLongitude(node); double dist = calc.calcDist(tmpLat, tmpLon, queryLat, queryLon); - if (circle == null || dist < calc.calcDist(circle.getLat(), circle.getLon(), queryLat, queryLon)) - { + if (circle == null || dist < calc.calcDist(circle.getLat(), circle.getLon(), queryLat, queryLon)) { res.setClosestEdge(iter.detach(false)); res.setClosestNode(node); res.setQueryDistance(dist); @@ -114,42 +103,35 @@ public QueryResult findClosest( double queryLat, double queryLon, EdgeFilter edg } @Override - public int findID( double lat, double lon ) - { + public int findID(double lat, double lon) { return findClosest(lat, lon, EdgeFilter.ALL_EDGES).getClosestNode(); } @Override - public LocationIndex create( long size ) - { + public LocationIndex create(long size) { return this; } @Override - public void flush() - { + public void flush() { } @Override - public void close() - { + public void close() { closed = true; } @Override - public boolean isClosed() - { + public boolean isClosed() { return closed; } @Override - public long getCapacity() - { + public long getCapacity() { return 0; } @Override - public void setSegmentSize( int bytes ) - { + public void setSegmentSize(int bytes) { } } diff --git a/core/src/main/java/com/graphhopper/storage/index/Location2IDFullWithEdgesIndex.java b/core/src/main/java/com/graphhopper/storage/index/Location2IDFullWithEdgesIndex.java index cc01e4964bb..880af5d4e60 100644 --- a/core/src/main/java/com/graphhopper/storage/index/Location2IDFullWithEdgesIndex.java +++ b/core/src/main/java/com/graphhopper/storage/index/Location2IDFullWithEdgesIndex.java @@ -27,80 +27,66 @@ /** * Same as full index but calculates distance to all edges too *

    + * * @author Peter Karich */ -public class Location2IDFullWithEdgesIndex implements LocationIndex -{ - private DistanceCalc calc = Helper.DIST_EARTH; +public class Location2IDFullWithEdgesIndex implements LocationIndex { private final Graph graph; private final NodeAccess nodeAccess; + private DistanceCalc calc = Helper.DIST_EARTH; private boolean closed = false; - public Location2IDFullWithEdgesIndex( Graph g ) - { + public Location2IDFullWithEdgesIndex(Graph g) { this.graph = g; this.nodeAccess = g.getNodeAccess(); } @Override - public boolean loadExisting() - { + public boolean loadExisting() { return true; } @Override - public LocationIndex setResolution( int resolution ) - { + public LocationIndex setResolution(int resolution) { return this; } @Override - public LocationIndex setApproximation( boolean approxDist ) - { - if (approxDist) - { + public LocationIndex setApproximation(boolean approxDist) { + if (approxDist) { calc = Helper.DIST_PLANE; - } else - { + } else { calc = Helper.DIST_EARTH; } return this; } @Override - public LocationIndex prepareIndex() - { + public LocationIndex prepareIndex() { return this; } @Override - public int findID( double lat, double lon ) - { + public int findID(double lat, double lon) { return findClosest(lat, lon, EdgeFilter.ALL_EDGES).getClosestNode(); } @Override - public QueryResult findClosest( double queryLat, double queryLon, EdgeFilter filter ) - { + public QueryResult findClosest(double queryLat, double queryLon, EdgeFilter filter) { if (isClosed()) throw new IllegalStateException("You need to create a new LocationIndex instance as it is already closed"); QueryResult res = new QueryResult(queryLat, queryLon); double foundDist = Double.MAX_VALUE; AllEdgesIterator iter = graph.getAllEdges(); - while (iter.next()) - { - if (!filter.accept(iter)) - { + while (iter.next()) { + if (!filter.accept(iter)) { continue; } - for (int i = 0, node; i < 2; i++) - { - if (i == 0) - { + for (int i = 0, node; i < 2; i++) { + if (i == 0) { node = iter.getBaseNode(); - } else - { + } else { node = iter.getAdjNode(); } @@ -110,8 +96,7 @@ public QueryResult findClosest( double queryLat, double queryLon, EdgeFilter fil if (fromDist < 0) continue; - if (fromDist < foundDist) - { + if (fromDist < foundDist) { res.setQueryDistance(fromDist); res.setClosestEdge(iter.detach(false)); res.setClosestNode(node); @@ -127,12 +112,10 @@ public QueryResult findClosest( double queryLat, double queryLon, EdgeFilter fil double toLon = nodeAccess.getLongitude(toNode); if (calc.validEdgeDistance(queryLat, queryLon, - fromLat, fromLon, toLat, toLon)) - { + fromLat, fromLon, toLat, toLon)) { double distEdge = calc.calcDenormalizedDist(calc.calcNormalizedEdgeDistance(queryLat, queryLon, fromLat, fromLon, toLat, toLon)); - if (distEdge < foundDist) - { + if (distEdge < foundDist) { res.setQueryDistance(distEdge); res.setClosestNode(node); res.setClosestEdge(iter); @@ -148,36 +131,30 @@ public QueryResult findClosest( double queryLat, double queryLon, EdgeFilter fil } @Override - public LocationIndex create( long size ) - { + public LocationIndex create(long size) { return this; } @Override - public void flush() - { + public void flush() { } @Override - public void close() - { + public void close() { closed = true; } @Override - public boolean isClosed() - { + public boolean isClosed() { return closed; } @Override - public long getCapacity() - { + public long getCapacity() { return 0; } @Override - public void setSegmentSize( int bytes ) - { + public void setSegmentSize(int bytes) { } } diff --git a/core/src/main/java/com/graphhopper/storage/index/Location2IDQuadtree.java b/core/src/main/java/com/graphhopper/storage/index/Location2IDQuadtree.java index 5a03ef07425..61573c55f38 100644 --- a/core/src/main/java/com/graphhopper/storage/index/Location2IDQuadtree.java +++ b/core/src/main/java/com/graphhopper/storage/index/Location2IDQuadtree.java @@ -24,39 +24,40 @@ import com.graphhopper.geohash.LinearKeyAlgo; import com.graphhopper.routing.util.EdgeFilter; import com.graphhopper.storage.*; -import com.graphhopper.util.*; +import com.graphhopper.util.BreadthFirstSearch; +import com.graphhopper.util.DistanceCalc; +import com.graphhopper.util.Helper; +import com.graphhopper.util.StopWatch; import com.graphhopper.util.shapes.BBox; import com.graphhopper.util.shapes.GHPoint; - -import java.util.Arrays; - import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.Arrays; + /** * This class implements map matching and returns a node index from lat,lon coordinate. This * implementation is the a very memory efficient representation for areas with lots of node and * edges, but lacks precision. No edge distances are measured. *

    + * * @author Peter Karich * @see LocationIndexTree which is more precise but more complicated and also slightly slower * implementation of LocationIndex. *

    */ -class Location2IDQuadtree implements LocationIndex -{ +class Location2IDQuadtree implements LocationIndex { private final static int MAGIC_INT = Integer.MAX_VALUE / 12306; private final Logger logger = LoggerFactory.getLogger(getClass()); - private KeyAlgo keyAlgo; - protected DistanceCalc distCalc = Helper.DIST_PLANE; private final DataAccess index; - private double maxRasterWidth2InMeterNormed; private final Graph graph; private final NodeAccess nodeAccess; + protected DistanceCalc distCalc = Helper.DIST_PLANE; + private KeyAlgo keyAlgo; + private double maxRasterWidth2InMeterNormed; private int lonSize, latSize; - public Location2IDQuadtree( Graph g, Directory dir ) - { + public Location2IDQuadtree(Graph g, Directory dir) { this.graph = g; this.nodeAccess = g.getNodeAccess(); index = dir.find("loc2id_index"); @@ -64,8 +65,7 @@ public Location2IDQuadtree( Graph g, Directory dir ) } @Override - public LocationIndex setApproximation( boolean approxDist ) - { + public LocationIndex setApproximation(boolean approxDist) { if (approxDist) distCalc = Helper.DIST_PLANE; else @@ -75,8 +75,7 @@ public LocationIndex setApproximation( boolean approxDist ) } @Override - public long getCapacity() - { + public long getCapacity() { return index.getCapacity() / 4; } @@ -84,11 +83,11 @@ public long getCapacity() * Loads the index from disc if exists. Make sure you are using the identical graph which was * used while flushing this index. *

    + * * @return if loading from file was successfully. */ @Override - public boolean loadExisting() - { + public boolean loadExisting() { if (!index.loadExisting()) return false; @@ -107,14 +106,12 @@ public boolean loadExisting() } @Override - public LocationIndex create( long size ) - { + public LocationIndex create(long size) { throw new UnsupportedOperationException("Not supported. Use prepareIndex instead."); } @Override - public LocationIndex setResolution( int resolution ) - { + public LocationIndex setResolution(int resolution) { initLatLonSize(resolution); return this; } @@ -126,8 +123,7 @@ public LocationIndex setResolution( int resolution ) * boundaries every 1.3km - IMO enough for EU or US networks. */ @Override - public LocationIndex prepareIndex() - { + public LocationIndex prepareIndex() { initBuffer(); initAlgo(latSize, lonSize); StopWatch sw = new StopWatch().start(); @@ -143,22 +139,19 @@ public LocationIndex prepareIndex() return this; } - private void initLatLonSize( int size ) - { + private void initLatLonSize(int size) { latSize = lonSize = (int) Math.sqrt(size); if (latSize * lonSize < size) lonSize++; } - private void initBuffer() - { + private void initBuffer() { // avoid default big segment size and use one segment only: index.setSegmentSize(latSize * lonSize * 4); index.create(latSize * lonSize * 4); } - void initAlgo( int lat, int lon ) - { + void initAlgo(int lat, int lon) { this.latSize = lat; this.lonSize = lon; BBox b = graph.getBounds(); @@ -171,29 +164,24 @@ void initAlgo( int lat, int lon ) // distances. because sin(x) is only monotonic increasing for x <= PI/2 (and positive for x >= 0) } - protected double getMaxRasterWidthMeter() - { + protected double getMaxRasterWidthMeter() { return distCalc.calcDenormalizedDist(maxRasterWidth2InMeterNormed) / 2; } - private GHBitSet fillQuadtree( int size ) - { + private GHBitSet fillQuadtree(int size) { int locs = graph.getNodes(); - if (locs <= 0) - { + if (locs <= 0) { throw new IllegalStateException("check your graph - it is empty!"); } GHBitSet filledIndices = new GHBitSetImpl(size); GHPoint coord = new GHPoint(); - for (int nodeId = 0; nodeId < locs; nodeId++) - { + for (int nodeId = 0; nodeId < locs; nodeId++) { double lat = nodeAccess.getLatitude(nodeId); double lon = nodeAccess.getLongitude(nodeId); int key = (int) keyAlgo.encode(lat, lon); long bytePos = (long) key * 4; - if (filledIndices.contains(key)) - { + if (filledIndices.contains(key)) { int oldNodeId = index.getInt(bytePos); keyAlgo.decode(key, coord); // decide which one is closer to 'key' @@ -202,12 +190,10 @@ private GHBitSet fillQuadtree( int size ) double oldLon = nodeAccess.getLongitude(oldNodeId); double distOld = distCalc.calcNormalizedDist(coord.lat, coord.lon, oldLat, oldLon); // new point is closer to quad tree point (key) so overwrite old - if (distNew < distOld) - { + if (distNew < distOld) { index.setInt(bytePos, nodeId); } - } else - { + } else { index.setInt(bytePos, nodeId); filledIndices.add(key); } @@ -215,8 +201,7 @@ private GHBitSet fillQuadtree( int size ) return filledIndices; } - private int fillEmptyIndices( GHBitSet filledIndices ) - { + private int fillEmptyIndices(GHBitSet filledIndices) { int len = latSize * lonSize; DataAccess indexCopy = new RAMDirectory().find("temp_index_copy"); indexCopy.setSegmentSize(index.getSegmentSize()).create(index.getCapacity()); @@ -227,60 +212,45 @@ private int fillEmptyIndices( GHBitSet filledIndices ) // and 2. use a taken-from array to decide which of the colliding should be preferred int[] takenFrom = new int[len]; Arrays.fill(takenFrom, -1); - for (int i = filledIndices.next(0); i >= 0; i = filledIndices.next(i + 1)) - { + for (int i = filledIndices.next(0); i >= 0; i = filledIndices.next(i + 1)) { takenFrom[i] = i; } - if (initializedCounter == 0) - { + if (initializedCounter == 0) { throw new IllegalStateException("at least one entry has to be != null, which should have happened in initIndex"); } int tmp = initializedCounter; - while (initializedCounter < len) - { + while (initializedCounter < len) { index.copyTo(indexCopy); filledIndices.copyTo(indicesCopy); initializedCounter = filledIndices.getCardinality(); - for (int i = 0; i < len; i++) - { + for (int i = 0; i < len; i++) { int to = -1, from = -1; - if (indicesCopy.contains(i)) - { + if (indicesCopy.contains(i)) { // check change "initialized to empty" - if ((i + 1) % lonSize != 0 && !indicesCopy.contains(i + 1)) - { + if ((i + 1) % lonSize != 0 && !indicesCopy.contains(i + 1)) { // set right from current from = i; to = i + 1; - } else if (i + lonSize < len && !indicesCopy.contains(i + lonSize)) - { + } else if (i + lonSize < len && !indicesCopy.contains(i + lonSize)) { // set below from current from = i; to = i + lonSize; } - } else - { - // check change "empty to initialized" - if ((i + 1) % lonSize != 0 && indicesCopy.contains(i + 1)) - { + } else // check change "empty to initialized" + if ((i + 1) % lonSize != 0 && indicesCopy.contains(i + 1)) { // set from right from = i + 1; to = i; - } else if (i + lonSize < len && indicesCopy.contains(i + lonSize)) - { + } else if (i + lonSize < len && indicesCopy.contains(i + lonSize)) { // set from below from = i + lonSize; to = i; } - } - if (to >= 0) - { - if (takenFrom[to] >= 0) - { + if (to >= 0) { + if (takenFrom[to] >= 0) { // takenFrom[to] == to -> special case for normedDist == 0 if (takenFrom[to] == to - || getNormedDist(from, to) >= getNormedDist(takenFrom[to], to)) - { + || getNormedDist(from, to) >= getNormedDist(takenFrom[to], to)) { continue; } } @@ -296,8 +266,7 @@ private int fillEmptyIndices( GHBitSet filledIndices ) return initializedCounter - tmp; } - double getNormedDist( int from, int to ) - { + double getNormedDist(int from, int to) { int fromX = from % lonSize; int fromY = from / lonSize; int toX = to % lonSize; @@ -311,15 +280,13 @@ private int fillEmptyIndices( GHBitSet filledIndices ) * @return the node id (corresponding to a coordinate) closest to the specified lat,lon. */ @Override - public int findID( final double lat, final double lon ) - { + public int findID(final double lat, final double lon) { return findClosest(lat, lon, EdgeFilter.ALL_EDGES).getClosestNode(); } @Override - public QueryResult findClosest( final double queryLat, final double queryLon, - final EdgeFilter edgeFilter ) - { + public QueryResult findClosest(final double queryLat, final double queryLon, + final EdgeFilter edgeFilter) { if (isClosed()) throw new IllegalStateException("You need to create a new LocationIndex instance as it is already closed"); @@ -348,17 +315,14 @@ public QueryResult findClosest( final double queryLat, final double queryLon, res.setClosestNode(id); res.setQueryDistance(distCalc.calcNormalizedDist(queryLat, queryLon, mainLat, mainLon)); goFurtherHook(id); - new BreadthFirstSearch() - { + new BreadthFirstSearch() { @Override - protected GHBitSet createBitSet() - { + protected GHBitSet createBitSet() { return new GHTBitSet(10); } @Override - protected boolean goFurther( int baseNode ) - { + protected boolean goFurther(int baseNode) { if (baseNode == id) return true; @@ -366,8 +330,7 @@ protected boolean goFurther( int baseNode ) double currLat = nodeAccess.getLatitude(baseNode); double currLon = nodeAccess.getLongitude(baseNode); double currNormedDist = distCalc.calcNormalizedDist(queryLat, queryLon, currLat, currLon); - if (currNormedDist < res.getQueryDistance()) - { + if (currNormedDist < res.getQueryDistance()) { res.setQueryDistance(currNormedDist); res.setClosestNode(baseNode); return true; @@ -382,13 +345,11 @@ protected boolean goFurther( int baseNode ) return res; } - public void goFurtherHook( int n ) - { + public void goFurtherHook(int n) { } @Override - public void flush() - { + public void flush() { index.setHeader(0, MAGIC_INT); index.setHeader(1 * 4, latSize); index.setHeader(2 * 4, lonSize); @@ -397,20 +358,17 @@ public void flush() } @Override - public void close() - { + public void close() { index.close(); } @Override - public boolean isClosed() - { + public boolean isClosed() { return index.isClosed(); } @Override - public void setSegmentSize( int bytes ) - { + public void setSegmentSize(int bytes) { index.setSegmentSize(bytes); } } diff --git a/core/src/main/java/com/graphhopper/storage/index/LocationIndex.java b/core/src/main/java/com/graphhopper/storage/index/LocationIndex.java index 801e36b9409..63676309602 100644 --- a/core/src/main/java/com/graphhopper/storage/index/LocationIndex.java +++ b/core/src/main/java/com/graphhopper/storage/index/LocationIndex.java @@ -26,15 +26,15 @@ *

    * The implementations of findID needs to be thread safe! *

    + * * @author Peter Karich */ -public interface LocationIndex extends Storable -{ +public interface LocationIndex extends Storable { /** * Integer value to specify the resolution of this location index. The higher the better the * resolution. */ - LocationIndex setResolution( int resolution ); + LocationIndex setResolution(int resolution); /** * Creates this index - to be called once before findID. @@ -42,31 +42,32 @@ public interface LocationIndex extends Storable LocationIndex prepareIndex(); /** - * @deprecated will be removed with 0.8 use 'QueryResult findClosest' instead * @return the closest node id for the specified geo location (latitude,longitude) + * @deprecated will be removed with 0.8 use 'QueryResult findClosest' instead */ - int findID( double lat, double lon ); + int findID(double lat, double lon); /** * This method returns the closest QueryResult for the specified location (lat, lon) and only if * the filter accepts the edge as valid candidate (e.g. filtering away car-only results for bike * search) *

    + * * @param edgeFilter if a graph supports multiple vehicles we have to make sure that the entry - * node into the graph is accessible from a selected vehicle. E.g. if you have a FOOT-query do:

    -     *   new DefaultEdgeFilter(footFlagEncoder);
    -     * 
    - *

    + * node into the graph is accessible from a selected vehicle. E.g. if you have a FOOT-query do:

    +     *                     new DefaultEdgeFilter(footFlagEncoder);
    +     *                   
    + * * @return An object containing the closest node and edge for the specified location. The node id * has at least one edge which is accepted from the specified edgeFilter. If nothing is found * the method QueryResult.isValid will return false. */ - QueryResult findClosest( double lat, double lon, EdgeFilter edgeFilter ); + QueryResult findClosest(double lat, double lon, EdgeFilter edgeFilter); /** * @param approxDist false if initialization and querying should be faster but less precise. */ - LocationIndex setApproximation( boolean approxDist ); + LocationIndex setApproximation(boolean approxDist); - void setSegmentSize( int bytes ); + void setSegmentSize(int bytes); } diff --git a/core/src/main/java/com/graphhopper/storage/index/LocationIndexTree.java b/core/src/main/java/com/graphhopper/storage/index/LocationIndexTree.java index 569b82bd147..9ac7f5e6fd9 100644 --- a/core/src/main/java/com/graphhopper/storage/index/LocationIndexTree.java +++ b/core/src/main/java/com/graphhopper/storage/index/LocationIndexTree.java @@ -21,11 +21,7 @@ import com.graphhopper.coll.GHTBitSet; import com.graphhopper.geohash.SpatialKeyAlgo; import com.graphhopper.routing.util.EdgeFilter; -import com.graphhopper.storage.DataAccess; -import com.graphhopper.storage.Directory; -import com.graphhopper.storage.Graph; -import com.graphhopper.storage.CHGraph; -import com.graphhopper.storage.NodeAccess; +import com.graphhopper.storage.*; import com.graphhopper.util.*; import com.graphhopper.util.shapes.BBox; import com.graphhopper.util.shapes.GHPoint; @@ -33,12 +29,14 @@ import gnu.trove.list.array.TIntArrayList; import gnu.trove.procedure.TIntProcedure; import gnu.trove.set.hash.TIntHashSet; - -import java.util.*; - import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.List; + /** * This implementation implements an n-tree to get the closest node or edge from GPS coordinates. *

    @@ -46,30 +44,30 @@ * line for different resolutions, especially if a leaf node could be split into a tree-node and * resolution changes. *

    + * * @author Peter Karich */ -public class LocationIndexTree implements LocationIndex -{ +public class LocationIndexTree implements LocationIndex { + // do not start with 0 as a positive value means leaf and a negative means "entry with subentries" + static final int START_POINTER = 1; + protected final Graph graph; + final DataAccess dataAccess; private final Logger logger = LoggerFactory.getLogger(getClass()); private final int MAGIC_INT; + private final NodeAccess nodeAccess; protected DistanceCalc distCalc = Helper.DIST_PLANE; + protected SpatialKeyAlgo keyAlgo; + int maxRegionSearch = 4; private DistanceCalc preciseDistCalc = Helper.DIST_EARTH; - protected final Graph graph; - private final NodeAccess nodeAccess; - final DataAccess dataAccess; private int[] entries; private byte[] shifts; // convert spatial key to index for subentry of current depth private long[] bitmasks; - protected SpatialKeyAlgo keyAlgo; private int minResolutionInMeter = 300; private double deltaLat; private double deltaLon; private int initSizeLeafEntries = 4; private boolean initialized = false; - // do not start with 0 as a positive value means leaf and a negative means "entry with subentries" - static final int START_POINTER = 1; - int maxRegionSearch = 4; /** * If normed distance is smaller than this value the node or edge is 'identical' and the * algorithm can stop search. @@ -79,8 +77,7 @@ public class LocationIndexTree implements LocationIndex /** * @param g the graph for which this index should do the lookup based on latitude,longitude. */ - public LocationIndexTree( Graph g, Directory dir ) - { + public LocationIndexTree(Graph g, Directory dir) { if (g instanceof CHGraph) throw new IllegalArgumentException("Use base graph for LocationIndexTree instead of CHGraph"); @@ -90,8 +87,7 @@ public LocationIndexTree( Graph g, Directory dir ) dataAccess = dir.find("location_index"); } - public int getMinResolutionInMeter() - { + public int getMinResolutionInMeter() { return minResolutionInMeter; } @@ -99,8 +95,7 @@ public int getMinResolutionInMeter() * Minimum width in meter of one tile. Decrease this if you need faster queries, but keep in * mind that then queries with different coordinates are more likely to fail. */ - public LocationIndexTree setMinResolutionInMeter( int minResolutionInMeter ) - { + public LocationIndexTree setMinResolutionInMeter(int minResolutionInMeter) { this.minResolutionInMeter = minResolutionInMeter; return this; } @@ -110,8 +105,7 @@ public LocationIndexTree setMinResolutionInMeter( int minResolutionInMeter ) * (minResolutionInMeter*regionAround). Set to 1 for to force avoiding a fall back, good if you * have strict performance and lookup-quality requirements. Default is 4. */ - public LocationIndexTree setMaxRegionSearch( int numTiles ) - { + public LocationIndexTree setMaxRegionSearch(int numTiles) { if (numTiles < 1) throw new IllegalArgumentException("Region of location index must be at least 1 but was " + numTiles); @@ -123,8 +117,7 @@ public LocationIndexTree setMaxRegionSearch( int numTiles ) return this; } - void prepareAlgo() - { + void prepareAlgo() { // 0.1 meter should count as 'equal' equalNormedDelta = distCalc.calcNormalizedDist(0.1); @@ -147,20 +140,15 @@ void prepareAlgo() TIntArrayList tmpEntries = new TIntArrayList(); // the last one is always 4 to reduce costs if only a single entry tmp /= 4; - while (tmp > 1) - { + while (tmp > 1) { int tmpNo; - if (tmp >= 64) - { + if (tmp >= 64) { tmpNo = 64; - } else if (tmp >= 16) - { + } else if (tmp >= 16) { tmpNo = 16; - } else if (tmp >= 4) - { + } else if (tmp >= 4) { tmpNo = 4; - } else - { + } else { break; } tmpEntries.add(tmpNo); @@ -170,8 +158,7 @@ void prepareAlgo() initEntries(tmpEntries.toArray()); int shiftSum = 0; long parts = 1; - for (int i = 0; i < shifts.length; i++) - { + for (int i = 0; i < shifts.length; i++) { shiftSum += shifts[i]; parts *= entries[i]; } @@ -184,11 +171,9 @@ void prepareAlgo() deltaLon = (bounds.maxLon - bounds.minLon) / parts; } - private LocationIndexTree initEntries( int[] entries ) - { - if (entries.length < 1) - // at least one depth should have been specified - { + private LocationIndexTree initEntries(int[] entries) { + if (entries.length < 1) { + // at least one depth should have been specified throw new IllegalStateException("depth needs to be at least 1"); } this.entries = entries; @@ -196,10 +181,8 @@ private LocationIndexTree initEntries( int[] entries ) shifts = new byte[depth]; bitmasks = new long[depth]; int lastEntry = entries[0]; - for (int i = 0; i < depth; i++) - { - if (lastEntry < entries[i]) - { + for (int i = 0; i < depth; i++) { + if (lastEntry < entries[i]) { throw new IllegalStateException("entries should decrease or stay but was:" + Arrays.toString(entries)); } @@ -210,8 +193,7 @@ private LocationIndexTree initEntries( int[] entries ) return this; } - private byte getShift( int entries ) - { + private byte getShift(int entries) { byte b = (byte) Math.round(Math.log(entries) / Math.log(2)); if (b <= 0) throw new IllegalStateException("invalid shift:" + b); @@ -219,26 +201,22 @@ private byte getShift( int entries ) return b; } - private long getBitmask( int shift ) - { + private long getBitmask(int shift) { long bm = (1L << shift) - 1; - if (bm <= 0) - { + if (bm <= 0) { throw new IllegalStateException("invalid bitmask:" + bm); } return bm; } - InMemConstructionIndex getPrepareInMemIndex() - { + InMemConstructionIndex getPrepareInMemIndex() { InMemConstructionIndex memIndex = new InMemConstructionIndex(entries[0]); memIndex.prepare(); return memIndex; } @Override - public int findID( double lat, double lon ) - { + public int findID(double lat, double lon) { QueryResult res = findClosest(lat, lon, EdgeFilter.ALL_EDGES); if (!res.isValid()) return -1; @@ -247,8 +225,7 @@ public int findID( double lat, double lon ) } @Override - public LocationIndex setResolution( int minResolutionInMeter ) - { + public LocationIndex setResolution(int minResolutionInMeter) { if (minResolutionInMeter <= 0) throw new IllegalStateException("Negative precision is not allowed!"); @@ -257,8 +234,7 @@ public LocationIndex setResolution( int minResolutionInMeter ) } @Override - public LocationIndex setApproximation( boolean approx ) - { + public LocationIndex setApproximation(boolean approx) { if (approx) distCalc = Helper.DIST_PLANE; else @@ -267,14 +243,12 @@ public LocationIndex setApproximation( boolean approx ) } @Override - public LocationIndexTree create( long size ) - { + public LocationIndexTree create(long size) { throw new UnsupportedOperationException("Not supported. Use prepareIndex instead."); } @Override - public boolean loadExisting() - { + public boolean loadExisting() { if (initialized) throw new IllegalStateException("Call loadExisting only once"); @@ -295,8 +269,7 @@ public boolean loadExisting() } @Override - public void flush() - { + public void flush() { dataAccess.setHeader(0, MAGIC_INT); dataAccess.setHeader(1 * 4, calcChecksum()); dataAccess.setHeader(2 * 4, minResolutionInMeter); @@ -306,8 +279,7 @@ public void flush() } @Override - public LocationIndex prepareIndex() - { + public LocationIndex prepareIndex() { if (initialized) throw new IllegalStateException("Call prepareIndex only once"); @@ -318,12 +290,10 @@ public LocationIndex prepareIndex() // compact & store to dataAccess dataAccess.create(64 * 1024); - try - { + try { inMem.store(inMem.root, START_POINTER); flush(); - } catch (Exception ex) - { + } catch (Exception ex) { throw new IllegalStateException("Problem while storing location index. " + Helper.getMemInfo(), ex); } float entriesPerLeaf = (float) inMem.size / inMem.leafs; @@ -340,271 +310,48 @@ public LocationIndex prepareIndex() return this; } - int calcChecksum() - { + int calcChecksum() { // do not include the edges as we could get problem with CHGraph due to shortcuts // ^ graph.getAllEdges().count(); return graph.getNodes(); } @Override - public void close() - { + public void close() { dataAccess.close(); } @Override - public boolean isClosed() - { + public boolean isClosed() { return dataAccess.isClosed(); } @Override - public long getCapacity() - { + public long getCapacity() { return dataAccess.getCapacity(); } @Override - public void setSegmentSize( int bytes ) - { + public void setSegmentSize(int bytes) { dataAccess.setSegmentSize(bytes); } - class InMemConstructionIndex - { - int size; - int leafs; - InMemTreeEntry root; - - public InMemConstructionIndex( int noOfSubEntries ) - { - root = new InMemTreeEntry(noOfSubEntries); - } - - void prepare() - { - final EdgeIterator allIter = graph.getAllEdges(); - try - { - while (allIter.next()) - { - int nodeA = allIter.getBaseNode(); - int nodeB = allIter.getAdjNode(); - double lat1 = nodeAccess.getLatitude(nodeA); - double lon1 = nodeAccess.getLongitude(nodeA); - double lat2; - double lon2; - PointList points = allIter.fetchWayGeometry(0); - int len = points.getSize(); - for (int i = 0; i < len; i++) - { - lat2 = points.getLatitude(i); - lon2 = points.getLongitude(i); - addNode(nodeA, nodeB, lat1, lon1, lat2, lon2); - lat1 = lat2; - lon1 = lon2; - } - lat2 = nodeAccess.getLatitude(nodeB); - lon2 = nodeAccess.getLongitude(nodeB); - addNode(nodeA, nodeB, lat1, lon1, lat2, lon2); - } - } catch (Exception ex) - { - logger.error("Problem! base:" + allIter.getBaseNode() + ", adj:" + allIter.getAdjNode() - + ", edge:" + allIter.getEdge(), ex); - } - } - - void addNode( final int nodeA, final int nodeB, - final double lat1, final double lon1, - final double lat2, final double lon2 ) - { - PointEmitter pointEmitter = new PointEmitter() - { - @Override - public void set( double lat, double lon ) - { - long key = keyAlgo.encode(lat, lon); - long keyPart = createReverseKey(key); - // no need to feed both nodes as we search neighbors in fillIDs - addNode(root, nodeA, 0, keyPart, key); - } - }; - - if (!distCalc.isCrossBoundary(lon1, lon2)) - { - BresenhamLine.calcPoints(lat1, lon1, lat2, lon2, pointEmitter, - graph.getBounds().minLat, graph.getBounds().minLon, - deltaLat, deltaLon); - } - } - - void addNode( InMemEntry entry, int nodeId, int depth, long keyPart, long key ) - { - if (entry.isLeaf()) - { - InMemLeafEntry leafEntry = (InMemLeafEntry) entry; - leafEntry.addNode(nodeId); - } else - { - int index = (int) (bitmasks[depth] & keyPart); - keyPart = keyPart >>> shifts[depth]; - InMemTreeEntry treeEntry = ((InMemTreeEntry) entry); - InMemEntry subentry = treeEntry.getSubEntry(index); - depth++; - if (subentry == null) - { - if (depth == entries.length) - { - subentry = new InMemLeafEntry(initSizeLeafEntries, key); - } else - { - subentry = new InMemTreeEntry(entries[depth]); - } - treeEntry.setSubEntry(index, subentry); - } - - addNode(subentry, nodeId, depth, keyPart, key); - } - } - - Collection getEntriesOf( int selectDepth ) - { - List list = new ArrayList(); - fillLayer(list, selectDepth, 0, ((InMemTreeEntry) root).getSubEntriesForDebug()); - return list; - } - - void fillLayer( Collection list, int selectDepth, int depth, Collection entries ) - { - for (InMemEntry entry : entries) - { - if (selectDepth == depth) - { - list.add(entry); - } else if (entry instanceof InMemTreeEntry) - { - fillLayer(list, selectDepth, depth + 1, ((InMemTreeEntry) entry).getSubEntriesForDebug()); - } - } - } - - String print() - { - StringBuilder sb = new StringBuilder(); - print(root, sb, 0, 0); - return sb.toString(); - } - - void print( InMemEntry e, StringBuilder sb, long key, int depth ) - { - if (e.isLeaf()) - { - InMemLeafEntry leaf = (InMemLeafEntry) e; - int bits = keyAlgo.getBits(); - // print reverse keys - sb.append(BitUtil.BIG.toBitString(BitUtil.BIG.reverse(key, bits), bits)).append(" "); - TIntArrayList entries = leaf.getResults(); - for (int i = 0; i < entries.size(); i++) - { - sb.append(leaf.get(i)).append(','); - } - sb.append('\n'); - } else - { - InMemTreeEntry tree = (InMemTreeEntry) e; - key = key << shifts[depth]; - for (int counter = 0; counter < tree.subEntries.length; counter++) - { - InMemEntry sube = tree.subEntries[counter]; - if (sube != null) - { - print(sube, sb, key | counter, depth + 1); - } - } - } - } - - // store and freezes tree - int store( InMemEntry entry, int intIndex ) - { - long refPointer = (long) intIndex * 4; - if (entry.isLeaf()) - { - InMemLeafEntry leaf = ((InMemLeafEntry) entry); - TIntArrayList entries = leaf.getResults(); - int len = entries.size(); - if (len == 0) - { - return intIndex; - } - size += len; - intIndex++; - leafs++; - dataAccess.ensureCapacity((long) (intIndex + len + 1) * 4); - if (len == 1) - { - // less disc space for single entries - dataAccess.setInt(refPointer, -entries.get(0) - 1); - } else - { - for (int index = 0; index < len; index++, intIndex++) - { - dataAccess.setInt((long) intIndex * 4, entries.get(index)); - } - dataAccess.setInt(refPointer, intIndex); - } - } else - { - InMemTreeEntry treeEntry = ((InMemTreeEntry) entry); - int len = treeEntry.subEntries.length; - intIndex += len; - for (int subCounter = 0; subCounter < len; subCounter++, refPointer += 4) - { - InMemEntry subEntry = treeEntry.subEntries[subCounter]; - if (subEntry == null) - { - continue; - } - dataAccess.ensureCapacity((long) (intIndex + 1) * 4); - int beforeIntIndex = intIndex; - intIndex = store(subEntry, beforeIntIndex); - if (intIndex == beforeIntIndex) - { - dataAccess.setInt(refPointer, 0); - } else - { - dataAccess.setInt(refPointer, beforeIntIndex); - } - } - } - return intIndex; - } - } - - TIntArrayList getEntries() - { + TIntArrayList getEntries() { return new TIntArrayList(entries); } // fillIDs according to how they are stored - final void fillIDs( long keyPart, int intIndex, TIntHashSet set, int depth ) - { + final void fillIDs(long keyPart, int intIndex, TIntHashSet set, int depth) { long pointer = (long) intIndex << 2; - if (depth == entries.length) - { + if (depth == entries.length) { int value = dataAccess.getInt(pointer); - if (value < 0) - // single data entries (less disc space) - { + if (value < 0) { + // single data entries (less disc space) set.add(-(value + 1)); - } else - { + } else { long max = (long) value * 4; // leaf entry => value is maxPointer - for (long leafIndex = pointer + 4; leafIndex < max; leafIndex += 4) - { + for (long leafIndex = pointer + 4; leafIndex < max; leafIndex += 4) { set.add(dataAccess.getInt(leafIndex)); } } @@ -612,21 +359,18 @@ final void fillIDs( long keyPart, int intIndex, TIntHashSet set, int depth ) } int offset = (int) (bitmasks[depth] & keyPart) << 2; int value = dataAccess.getInt(pointer + offset); - if (value > 0) - { + if (value > 0) { // tree entry => negative value points to subentries fillIDs(keyPart >>> shifts[depth], value, set, depth + 1); } } // this method returns the spatial key in reverse order for easier right-shifting - final long createReverseKey( double lat, double lon ) - { + final long createReverseKey(double lat, double lon) { return BitUtil.BIG.reverse(keyAlgo.encode(lat, lon), keyAlgo.getBits()); } - final long createReverseKey( long key ) - { + final long createReverseKey(long key) { return BitUtil.BIG.reverse(key, keyAlgo.getBits()); } @@ -635,8 +379,7 @@ final long createReverseKey( long key ) * context of a spatial key tile. *

    */ - final double calculateRMin( double lat, double lon ) - { + final double calculateRMin(double lat, double lon) { return calculateRMin(lat, lon, 0); } @@ -645,8 +388,7 @@ final double calculateRMin( double lat, double lon ) * region with dimension 2*paddingTiles + 1 and where the center tile contains the given lat/lon * coordinate */ - final double calculateRMin( double lat, double lon, int paddingTiles ) - { + final double calculateRMin(double lat, double lon, int paddingTiles) { GHPoint query = new GHPoint(lat, lon); long key = keyAlgo.encode(query); GHPoint center = new GHPoint(); @@ -665,19 +407,15 @@ final double calculateRMin( double lat, double lon, int paddingTiles ) // convert degree deltas into a radius in meter double dMinLat, dMinLon; - if (dSouthernLat < dNorthernLat) - { + if (dSouthernLat < dNorthernLat) { dMinLat = distCalc.calcDist(query.lat, query.lon, minLat, query.lon); - } else - { + } else { dMinLat = distCalc.calcDist(query.lat, query.lon, maxLat, query.lon); } - if (dWesternLon < dEasternLon) - { + if (dWesternLon < dEasternLon) { dMinLon = distCalc.calcDist(query.lat, query.lon, query.lat, minLon); - } else - { + } else { dMinLon = distCalc.calcDist(query.lat, query.lon, query.lat, maxLon); } @@ -688,18 +426,15 @@ final double calculateRMin( double lat, double lon, int paddingTiles ) /** * Provide info about tilesize for testing / visualization */ - double getDeltaLat() - { + double getDeltaLat() { return deltaLat; } - double getDeltaLon() - { + double getDeltaLon() { return deltaLon; } - GHPoint getCenter( double lat, double lon ) - { + GHPoint getCenter(double lat, double lon) { GHPoint query = new GHPoint(lat, lon); long key = keyAlgo.encode(query); GHPoint center = new GHPoint(); @@ -712,15 +447,14 @@ GHPoint getCenter( double lat, double lon ) * which makes sure not too many nodes are collected as well as no nodes will be missing. See * discussion at issue #221. *

    + * * @return true if no further call of this method is required. False otherwise, ie. a next * iteration is necessary and no early finish possible. */ - public final boolean findNetworkEntries( double queryLat, double queryLon, - TIntHashSet foundEntries, int iteration ) - { + public final boolean findNetworkEntries(double queryLat, double queryLon, + TIntHashSet foundEntries, int iteration) { // find entries in border of searchbox - for (int yreg = -iteration; yreg <= iteration; yreg++) - { + for (int yreg = -iteration; yreg <= iteration; yreg++) { double subqueryLat = queryLat + yreg * deltaLat; double subqueryLonA = queryLon - iteration * deltaLon; double subqueryLonB = queryLon + iteration * deltaLon; @@ -731,8 +465,7 @@ public final boolean findNetworkEntries( double queryLat, double queryLon, findNetworkEntriesSingleRegion(foundEntries, subqueryLat, subqueryLonB); } - for (int xreg = -iteration + 1; xreg <= iteration - 1; xreg++) - { + for (int xreg = -iteration + 1; xreg <= iteration - 1; xreg++) { double subqueryLon = queryLon + xreg * deltaLon; double subqueryLatA = queryLat - iteration * deltaLat; double subqueryLatB = queryLat + iteration * deltaLat; @@ -740,11 +473,9 @@ public final boolean findNetworkEntries( double queryLat, double queryLon, findNetworkEntriesSingleRegion(foundEntries, subqueryLatB, subqueryLon); } - if (iteration % 2 != 0) - { + if (iteration % 2 != 0) { // Check if something was found already... - if (!foundEntries.isEmpty()) - { + if (!foundEntries.isEmpty()) { double rMin = calculateRMin(queryLat, queryLon, iteration); double minDistance = calcMinDistance(queryLat, queryLon, foundEntries); @@ -761,40 +492,34 @@ public final boolean findNetworkEntries( double queryLat, double queryLon, return false; } - final double calcMinDistance( double queryLat, double queryLon, TIntHashSet pointset ) - { + final double calcMinDistance(double queryLat, double queryLon, TIntHashSet pointset) { double min = Double.MAX_VALUE; TIntIterator itr = pointset.iterator(); - while (itr.hasNext()) - { + while (itr.hasNext()) { int node = itr.next(); double lat = nodeAccess.getLat(node); double lon = nodeAccess.getLon(node); double dist = distCalc.calcDist(queryLat, queryLon, lat, lon); - if (dist < min) - { + if (dist < min) { min = dist; } } return min; } - final void findNetworkEntriesSingleRegion( TIntHashSet storedNetworkEntryIds, double queryLat, double queryLon ) - { + final void findNetworkEntriesSingleRegion(TIntHashSet storedNetworkEntryIds, double queryLat, double queryLon) { long keyPart = createReverseKey(queryLat, queryLon); fillIDs(keyPart, START_POINTER, storedNetworkEntryIds, 0); } @Override - public QueryResult findClosest( final double queryLat, final double queryLon, final EdgeFilter edgeFilter ) - { + public QueryResult findClosest(final double queryLat, final double queryLon, final EdgeFilter edgeFilter) { if (isClosed()) throw new IllegalStateException("You need to create a new LocationIndex instance as it is already closed"); TIntHashSet allCollectedEntryIds = new TIntHashSet(); final QueryResult closestMatch = new QueryResult(queryLat, queryLon); - for (int iteration = 0; iteration < maxRegionSearch; iteration++) - { + for (int iteration = 0; iteration < maxRegionSearch; iteration++) { TIntHashSet storedNetworkEntryIds = new TIntHashSet(); boolean earlyFinish = findNetworkEntries(queryLat, queryLon, storedNetworkEntryIds, iteration); storedNetworkEntryIds.removeAll(allCollectedEntryIds); @@ -804,24 +529,18 @@ public QueryResult findClosest( final double queryLat, final double queryLon, fi final GHBitSet checkBitset = new GHTBitSet(new TIntHashSet(storedNetworkEntryIds)); // find nodes from the network entries which are close to 'point' final EdgeExplorer explorer = graph.createEdgeExplorer(); - storedNetworkEntryIds.forEach(new TIntProcedure() - { + storedNetworkEntryIds.forEach(new TIntProcedure() { @Override - public boolean execute( int networkEntryNodeId ) - { - new XFirstSearchCheck(queryLat, queryLon, checkBitset, edgeFilter) - { + public boolean execute(int networkEntryNodeId) { + new XFirstSearchCheck(queryLat, queryLon, checkBitset, edgeFilter) { @Override - protected double getQueryDistance() - { + protected double getQueryDistance() { return closestMatch.getQueryDistance(); } @Override - protected boolean check( int node, double normedDist, int wayIndex, EdgeIteratorState edge, QueryResult.Position pos ) - { - if (normedDist < closestMatch.getQueryDistance()) - { + protected boolean check(int node, double normedDist, int wayIndex, EdgeIteratorState edge, QueryResult.Position pos) { + if (normedDist < closestMatch.getQueryDistance()) { closestMatch.setQueryDistance(normedDist); closestMatch.setClosestNode(node); closestMatch.setClosestEdge(edge.detach(false)); @@ -842,8 +561,7 @@ protected boolean check( int node, double normedDist, int wayIndex, EdgeIterator } // denormalize distance and calculate snapping point only if closed match was found - if (closestMatch.isValid()) - { + if (closestMatch.isValid()) { closestMatch.setQueryDistance(distCalc.calcDenormalizedDist(closestMatch.getQueryDistance())); closestMatch.calcSnappedPoint(distCalc); } @@ -851,23 +569,284 @@ protected boolean check( int node, double normedDist, int wayIndex, EdgeIterator return closestMatch; } + // make entries static as otherwise we get an additional reference to this class (memory waste) + interface InMemEntry { + boolean isLeaf(); + } + + static class InMemLeafEntry extends SortedIntSet implements InMemEntry { + // private long key; + + public InMemLeafEntry(int count, long key) { + super(count); + // this.key = key; + } + + public boolean addNode(int nodeId) { + return addOnce(nodeId); + } + + @Override + public final boolean isLeaf() { + return true; + } + + @Override + public String toString() { + return "LEAF " + /*key +*/ " " + super.toString(); + } + + TIntArrayList getResults() { + return this; + } + } + + // Space efficient sorted integer set. Suited for only a few entries. + static class SortedIntSet extends TIntArrayList { + public SortedIntSet() { + } + + public SortedIntSet(int capacity) { + super(capacity); + } + + /** + * Allow adding a value only once + */ + public boolean addOnce(int value) { + int foundIndex = binarySearch(value); + if (foundIndex >= 0) { + return false; + } + foundIndex = -foundIndex - 1; + insert(foundIndex, value); + return true; + } + } + + static class InMemTreeEntry implements InMemEntry { + InMemEntry[] subEntries; + + public InMemTreeEntry(int subEntryNo) { + subEntries = new InMemEntry[subEntryNo]; + } + + public InMemEntry getSubEntry(int index) { + return subEntries[index]; + } + + public void setSubEntry(int index, InMemEntry subEntry) { + this.subEntries[index] = subEntry; + } + + public Collection getSubEntriesForDebug() { + List list = new ArrayList(); + for (InMemEntry e : subEntries) { + if (e != null) { + list.add(e); + } + } + return list; + } + + @Override + public final boolean isLeaf() { + return false; + } + + @Override + public String toString() { + return "TREE"; + } + } + + class InMemConstructionIndex { + int size; + int leafs; + InMemTreeEntry root; + + public InMemConstructionIndex(int noOfSubEntries) { + root = new InMemTreeEntry(noOfSubEntries); + } + + void prepare() { + final EdgeIterator allIter = graph.getAllEdges(); + try { + while (allIter.next()) { + int nodeA = allIter.getBaseNode(); + int nodeB = allIter.getAdjNode(); + double lat1 = nodeAccess.getLatitude(nodeA); + double lon1 = nodeAccess.getLongitude(nodeA); + double lat2; + double lon2; + PointList points = allIter.fetchWayGeometry(0); + int len = points.getSize(); + for (int i = 0; i < len; i++) { + lat2 = points.getLatitude(i); + lon2 = points.getLongitude(i); + addNode(nodeA, nodeB, lat1, lon1, lat2, lon2); + lat1 = lat2; + lon1 = lon2; + } + lat2 = nodeAccess.getLatitude(nodeB); + lon2 = nodeAccess.getLongitude(nodeB); + addNode(nodeA, nodeB, lat1, lon1, lat2, lon2); + } + } catch (Exception ex) { + logger.error("Problem! base:" + allIter.getBaseNode() + ", adj:" + allIter.getAdjNode() + + ", edge:" + allIter.getEdge(), ex); + } + } + + void addNode(final int nodeA, final int nodeB, + final double lat1, final double lon1, + final double lat2, final double lon2) { + PointEmitter pointEmitter = new PointEmitter() { + @Override + public void set(double lat, double lon) { + long key = keyAlgo.encode(lat, lon); + long keyPart = createReverseKey(key); + // no need to feed both nodes as we search neighbors in fillIDs + addNode(root, nodeA, 0, keyPart, key); + } + }; + + if (!distCalc.isCrossBoundary(lon1, lon2)) { + BresenhamLine.calcPoints(lat1, lon1, lat2, lon2, pointEmitter, + graph.getBounds().minLat, graph.getBounds().minLon, + deltaLat, deltaLon); + } + } + + void addNode(InMemEntry entry, int nodeId, int depth, long keyPart, long key) { + if (entry.isLeaf()) { + InMemLeafEntry leafEntry = (InMemLeafEntry) entry; + leafEntry.addNode(nodeId); + } else { + int index = (int) (bitmasks[depth] & keyPart); + keyPart = keyPart >>> shifts[depth]; + InMemTreeEntry treeEntry = ((InMemTreeEntry) entry); + InMemEntry subentry = treeEntry.getSubEntry(index); + depth++; + if (subentry == null) { + if (depth == entries.length) { + subentry = new InMemLeafEntry(initSizeLeafEntries, key); + } else { + subentry = new InMemTreeEntry(entries[depth]); + } + treeEntry.setSubEntry(index, subentry); + } + + addNode(subentry, nodeId, depth, keyPart, key); + } + } + + Collection getEntriesOf(int selectDepth) { + List list = new ArrayList(); + fillLayer(list, selectDepth, 0, ((InMemTreeEntry) root).getSubEntriesForDebug()); + return list; + } + + void fillLayer(Collection list, int selectDepth, int depth, Collection entries) { + for (InMemEntry entry : entries) { + if (selectDepth == depth) { + list.add(entry); + } else if (entry instanceof InMemTreeEntry) { + fillLayer(list, selectDepth, depth + 1, ((InMemTreeEntry) entry).getSubEntriesForDebug()); + } + } + } + + String print() { + StringBuilder sb = new StringBuilder(); + print(root, sb, 0, 0); + return sb.toString(); + } + + void print(InMemEntry e, StringBuilder sb, long key, int depth) { + if (e.isLeaf()) { + InMemLeafEntry leaf = (InMemLeafEntry) e; + int bits = keyAlgo.getBits(); + // print reverse keys + sb.append(BitUtil.BIG.toBitString(BitUtil.BIG.reverse(key, bits), bits)).append(" "); + TIntArrayList entries = leaf.getResults(); + for (int i = 0; i < entries.size(); i++) { + sb.append(leaf.get(i)).append(','); + } + sb.append('\n'); + } else { + InMemTreeEntry tree = (InMemTreeEntry) e; + key = key << shifts[depth]; + for (int counter = 0; counter < tree.subEntries.length; counter++) { + InMemEntry sube = tree.subEntries[counter]; + if (sube != null) { + print(sube, sb, key | counter, depth + 1); + } + } + } + } + + // store and freezes tree + int store(InMemEntry entry, int intIndex) { + long refPointer = (long) intIndex * 4; + if (entry.isLeaf()) { + InMemLeafEntry leaf = ((InMemLeafEntry) entry); + TIntArrayList entries = leaf.getResults(); + int len = entries.size(); + if (len == 0) { + return intIndex; + } + size += len; + intIndex++; + leafs++; + dataAccess.ensureCapacity((long) (intIndex + len + 1) * 4); + if (len == 1) { + // less disc space for single entries + dataAccess.setInt(refPointer, -entries.get(0) - 1); + } else { + for (int index = 0; index < len; index++, intIndex++) { + dataAccess.setInt((long) intIndex * 4, entries.get(index)); + } + dataAccess.setInt(refPointer, intIndex); + } + } else { + InMemTreeEntry treeEntry = ((InMemTreeEntry) entry); + int len = treeEntry.subEntries.length; + intIndex += len; + for (int subCounter = 0; subCounter < len; subCounter++, refPointer += 4) { + InMemEntry subEntry = treeEntry.subEntries[subCounter]; + if (subEntry == null) { + continue; + } + dataAccess.ensureCapacity((long) (intIndex + 1) * 4); + int beforeIntIndex = intIndex; + intIndex = store(subEntry, beforeIntIndex); + if (intIndex == beforeIntIndex) { + dataAccess.setInt(refPointer, 0); + } else { + dataAccess.setInt(refPointer, beforeIntIndex); + } + } + } + return intIndex; + } + } + /** * Make it possible to collect nearby location also for other purposes. */ - protected abstract class XFirstSearchCheck extends BreadthFirstSearch - { + protected abstract class XFirstSearchCheck extends BreadthFirstSearch { + final double queryLat; + final double queryLon; + final GHBitSet checkBitset; + final EdgeFilter edgeFilter; boolean goFurther = true; double currNormedDist; double currLat; double currLon; int currNode; - final double queryLat; - final double queryLon; - final GHBitSet checkBitset; - final EdgeFilter edgeFilter; - public XFirstSearchCheck( double queryLat, double queryLon, GHBitSet checkBitset, EdgeFilter edgeFilter ) - { + public XFirstSearchCheck(double queryLat, double queryLon, GHBitSet checkBitset, EdgeFilter edgeFilter) { this.queryLat = queryLat; this.queryLon = queryLon; this.checkBitset = checkBitset; @@ -875,14 +854,12 @@ public XFirstSearchCheck( double queryLat, double queryLon, GHBitSet checkBitset } @Override - protected GHBitSet createBitSet() - { + protected GHBitSet createBitSet() { return checkBitset; } @Override - protected boolean goFurther( int baseNode ) - { + protected boolean goFurther(int baseNode) { currNode = baseNode; currLat = nodeAccess.getLatitude(baseNode); currLon = nodeAccess.getLongitude(baseNode); @@ -891,19 +868,16 @@ protected boolean goFurther( int baseNode ) } @Override - protected boolean checkAdjacent( EdgeIteratorState currEdge ) - { + protected boolean checkAdjacent(EdgeIteratorState currEdge) { goFurther = false; - if (!edgeFilter.accept(currEdge)) - { + if (!edgeFilter.accept(currEdge)) { // only limit the adjNode to a certain radius as currNode could be the wrong side of a valid edge // goFurther = currDist < minResolution2InMeterNormed; return true; } int tmpClosestNode = currNode; - if (check(tmpClosestNode, currNormedDist, 0, currEdge, QueryResult.Position.TOWER)) - { + if (check(tmpClosestNode, currNormedDist, 0, currEdge, QueryResult.Position.TOWER)) { if (currNormedDist <= equalNormedDelta) return false; } @@ -921,31 +895,25 @@ protected boolean checkAdjacent( EdgeIteratorState currEdge ) double tmpNormedDist; PointList pointList = currEdge.fetchWayGeometry(2); int len = pointList.getSize(); - for (int pointIndex = 0; pointIndex < len; pointIndex++) - { + for (int pointIndex = 0; pointIndex < len; pointIndex++) { double wayLat = pointList.getLatitude(pointIndex); double wayLon = pointList.getLongitude(pointIndex); QueryResult.Position pos = QueryResult.Position.EDGE; - if (distCalc.isCrossBoundary(tmpLon, wayLon)) - { + if (distCalc.isCrossBoundary(tmpLon, wayLon)) { tmpLat = wayLat; tmpLon = wayLon; continue; } - if (distCalc.validEdgeDistance(queryLat, queryLon, tmpLat, tmpLon, wayLat, wayLon)) - { + if (distCalc.validEdgeDistance(queryLat, queryLon, tmpLat, tmpLon, wayLat, wayLon)) { tmpNormedDist = distCalc.calcNormalizedEdgeDistance(queryLat, queryLon, tmpLat, tmpLon, wayLat, wayLon); check(tmpClosestNode, tmpNormedDist, pointIndex, currEdge, pos); - } else - { - if (pointIndex + 1 == len) - { + } else { + if (pointIndex + 1 == len) { tmpNormedDist = adjDist; pos = QueryResult.Position.TOWER; - } else - { + } else { tmpNormedDist = distCalc.calcNormalizedDist(queryLat, queryLon, wayLat, wayLon); pos = QueryResult.Position.PILLAR; } @@ -963,118 +931,6 @@ protected boolean checkAdjacent( EdgeIteratorState currEdge ) protected abstract double getQueryDistance(); - protected abstract boolean check( int node, double normedDist, int wayIndex, EdgeIteratorState iter, QueryResult.Position pos ); - } - - // make entries static as otherwise we get an additional reference to this class (memory waste) - interface InMemEntry - { - boolean isLeaf(); - } - - static class InMemLeafEntry extends SortedIntSet implements InMemEntry - { - // private long key; - - public InMemLeafEntry( int count, long key ) - { - super(count); - // this.key = key; - } - - public boolean addNode( int nodeId ) - { - return addOnce(nodeId); - } - - @Override - public final boolean isLeaf() - { - return true; - } - - @Override - public String toString() - { - return "LEAF " + /*key +*/ " " + super.toString(); - } - - TIntArrayList getResults() - { - return this; - } - } - - // Space efficient sorted integer set. Suited for only a few entries. - static class SortedIntSet extends TIntArrayList - { - public SortedIntSet() - { - } - - public SortedIntSet( int capacity ) - { - super(capacity); - } - - /** - * Allow adding a value only once - */ - public boolean addOnce( int value ) - { - int foundIndex = binarySearch(value); - if (foundIndex >= 0) - { - return false; - } - foundIndex = -foundIndex - 1; - insert(foundIndex, value); - return true; - } - } - - static class InMemTreeEntry implements InMemEntry - { - InMemEntry[] subEntries; - - public InMemTreeEntry( int subEntryNo ) - { - subEntries = new InMemEntry[subEntryNo]; - } - - public InMemEntry getSubEntry( int index ) - { - return subEntries[index]; - } - - public void setSubEntry( int index, InMemEntry subEntry ) - { - this.subEntries[index] = subEntry; - } - - public Collection getSubEntriesForDebug() - { - List list = new ArrayList(); - for (InMemEntry e : subEntries) - { - if (e != null) - { - list.add(e); - } - } - return list; - } - - @Override - public final boolean isLeaf() - { - return false; - } - - @Override - public String toString() - { - return "TREE"; - } + protected abstract boolean check(int node, double normedDist, int wayIndex, EdgeIteratorState iter, QueryResult.Position pos); } } diff --git a/core/src/main/java/com/graphhopper/storage/index/PointEmitter.java b/core/src/main/java/com/graphhopper/storage/index/PointEmitter.java index 55a9cf97618..9312ae7c0b2 100644 --- a/core/src/main/java/com/graphhopper/storage/index/PointEmitter.java +++ b/core/src/main/java/com/graphhopper/storage/index/PointEmitter.java @@ -20,7 +20,6 @@ /** * @author Peter Karich */ -public interface PointEmitter -{ - void set( double lat, double lon ); +public interface PointEmitter { + void set(double lat, double lon); } diff --git a/core/src/main/java/com/graphhopper/storage/index/QueryResult.java b/core/src/main/java/com/graphhopper/storage/index/QueryResult.java index 9025cf6620d..7c1c90bfc4e 100644 --- a/core/src/main/java/com/graphhopper/storage/index/QueryResult.java +++ b/core/src/main/java/com/graphhopper/storage/index/QueryResult.java @@ -32,66 +32,43 @@ * T--S----N * *

    + * * @author Peter Karich */ -public class QueryResult -{ +public class QueryResult { + private final GHPoint queryPoint; private double queryDistance = Double.MAX_VALUE; private int wayIndex = -1; private int closestNode = -1; private EdgeIteratorState closestEdge; - private final GHPoint queryPoint; private GHPoint3D snappedPoint; private Position snappedPosition; - /** - * Due to precision differences it is hard to define when something is exactly 90° or "on-node" - * like TOWER or PILLAR or if it is more "on-edge" (EDGE). The default mechanism is to prefer - * "on-edge" even if it could be 90°. To prefer "on-node" you could use e.g. GHPoint.equals with - * a default precision of 1e-6. - *

    - * @see DistanceCalc#validEdgeDistance - */ - public static enum Position - { - EDGE, TOWER, PILLAR - } - - public QueryResult( double queryLat, double queryLon ) - { + public QueryResult(double queryLat, double queryLon) { queryPoint = new GHPoint(queryLat, queryLon); } - public void setClosestNode( int node ) - { - closestNode = node; - } - /** * @return the closest matching node. -1 if nothing found, this should be avoided via a call of * 'isValid' */ - public int getClosestNode() - { + public int getClosestNode() { return closestNode; } - public void setQueryDistance( double dist ) - { - queryDistance = dist; + public void setClosestNode(int node) { + closestNode = node; } /** * @return the distance of the query to the snapped coordinates. In meter */ - public double getQueryDistance() - { + public double getQueryDistance() { return queryDistance; } - public void setWayIndex( int wayIndex ) - { - this.wayIndex = wayIndex; + public void setQueryDistance(double dist) { + queryDistance = dist; } /** @@ -100,47 +77,44 @@ public void setWayIndex( int wayIndex ) * wayGeometry indices (minus one) and L+1 to the *adjacent* node. Currently only initialized if * returned from Location2NodesNtree. */ - public int getWayIndex() - { + public int getWayIndex() { return wayIndex; } - public void setSnappedPosition( Position pos ) - { - this.snappedPosition = pos; + public void setWayIndex(int wayIndex) { + this.wayIndex = wayIndex; } /** * @return 0 if on edge. 1 if on pillar node and 2 if on tower node. */ - public Position getSnappedPosition() - { + public Position getSnappedPosition() { return snappedPosition; } + public void setSnappedPosition(Position pos) { + this.snappedPosition = pos; + } + /** * @return true if a close node was found */ - public boolean isValid() - { + public boolean isValid() { return closestNode >= 0; } - public void setClosestEdge( EdgeIteratorState detach ) - { - closestEdge = detach; - } - /** * @return the closest matching edge. Will be null if nothing found or call isValid before */ - public EdgeIteratorState getClosestEdge() - { + public EdgeIteratorState getClosestEdge() { return closestEdge; } - public GHPoint getQueryPoint() - { + public void setClosestEdge(EdgeIteratorState detach) { + closestEdge = detach; + } + + public GHPoint getQueryPoint() { return queryPoint; } @@ -148,8 +122,7 @@ public GHPoint getQueryPoint() * Calculates the position of the query point 'snapped' to a close road segment or node. Call * calcSnappedPoint before, if not, an IllegalStateException is thrown. */ - public GHPoint3D getSnappedPoint() - { + public GHPoint3D getSnappedPoint() { if (snappedPoint == null) throw new IllegalStateException("Calculate snapped point before!"); return snappedPoint; @@ -158,8 +131,7 @@ public GHPoint3D getSnappedPoint() /** * Calculates the closet point on the edge from the query point. */ - public void calcSnappedPoint( DistanceCalc distCalc ) - { + public void calcSnappedPoint(DistanceCalc distCalc) { if (closestEdge == null) throw new IllegalStateException("No closest edge?"); if (snappedPoint != null) @@ -169,16 +141,14 @@ public void calcSnappedPoint( DistanceCalc distCalc ) double tmpLat = fullPL.getLatitude(wayIndex); double tmpLon = fullPL.getLongitude(wayIndex); double tmpEle = fullPL.getElevation(wayIndex); - if (snappedPosition != Position.EDGE) - { + if (snappedPosition != Position.EDGE) { snappedPoint = new GHPoint3D(tmpLat, tmpLon, tmpEle); return; } double queryLat = getQueryPoint().lat, queryLon = getQueryPoint().lon; double adjLat = fullPL.getLatitude(wayIndex + 1), adjLon = fullPL.getLongitude(wayIndex + 1); - if (distCalc.validEdgeDistance(queryLat, queryLon, tmpLat, tmpLon, adjLat, adjLon)) - { + if (distCalc.validEdgeDistance(queryLat, queryLon, tmpLat, tmpLon, adjLat, adjLon)) { GHPoint tmpPoint = distCalc.calcCrossingPointToEdge(queryLat, queryLon, tmpLat, tmpLon, adjLat, adjLon); double adjEle = fullPL.getElevation(wayIndex + 1); snappedPoint = new GHPoint3D(tmpPoint.lat, tmpPoint.lon, (tmpEle + adjEle) / 2); @@ -188,10 +158,22 @@ public void calcSnappedPoint( DistanceCalc distCalc ) } @Override - public String toString() - { + public String toString() { if (closestEdge != null) return closestEdge.getBaseNode() + "-" + closestEdge.getAdjNode() + " " + snappedPoint; return closestNode + ", " + queryPoint + ", " + wayIndex; } + + /** + * Due to precision differences it is hard to define when something is exactly 90° or "on-node" + * like TOWER or PILLAR or if it is more "on-edge" (EDGE). The default mechanism is to prefer + * "on-edge" even if it could be 90°. To prefer "on-node" you could use e.g. GHPoint.equals with + * a default precision of 1e-6. + *

    + * + * @see DistanceCalc#validEdgeDistance + */ + public static enum Position { + EDGE, TOWER, PILLAR + } } diff --git a/core/src/main/java/com/graphhopper/util/AngleCalc.java b/core/src/main/java/com/graphhopper/util/AngleCalc.java index 1d10c36717c..7fa179882b6 100644 --- a/core/src/main/java/com/graphhopper/util/AngleCalc.java +++ b/core/src/main/java/com/graphhopper/util/AngleCalc.java @@ -24,26 +24,23 @@ * Calculates the angle of a turn, defined by three points. The fast atan2 method is from Jim Shima, * 1999, http://www.dspguru.com/dsp/tricks/fixed-point-atan2-with-self-normalization *

    + * * @author Johannes Pelzer * @author Peter Karich */ -public class AngleCalc -{ +public class AngleCalc { private final static double PI_4 = Math.PI / 4.0; private final static double PI_2 = Math.PI / 2.0; private final static double PI3_4 = 3.0 * Math.PI / 4.0; - static final double atan2( double y, double x ) - { + static final double atan2(double y, double x) { // kludge to prevent 0/0 condition double absY = Math.abs(y) + 1e-10; double r, angle; - if (x < 0.0) - { + if (x < 0.0) { r = (x + absY) / (absY - x); angle = PI3_4; - } else - { + } else { r = (x - absY) / (x + absY); angle = PI_4; } @@ -58,10 +55,10 @@ static final double atan2( double y, double x ) /** * Return orientation of line relative to east. *

    + * * @return Orientation in interval -pi to +pi where 0 is east */ - public double calcOrientation( double lat1, double lon1, double lat2, double lon2 ) - { + public double calcOrientation(double lat1, double lon1, double lat2, double lon2) { double shrinkFactor = cos(toRadians((lat1 + lat2) / 2)); return Math.atan2((lat2 - lat1), shrinkFactor * (lon2 - lon1)); } @@ -69,15 +66,15 @@ public double calcOrientation( double lat1, double lon1, double lat2, double lon /** * convert north based clockwise azimuth (0, 360) into x-axis/east based angle (-Pi, Pi) */ - public double convertAzimuth2xaxisAngle(double azimuth) - { - if (Double.compare(azimuth, 360)>0 || Double.compare(azimuth, 0)<0) - { + public double convertAzimuth2xaxisAngle(double azimuth) { + if (Double.compare(azimuth, 360) > 0 || Double.compare(azimuth, 0) < 0) { throw new IllegalArgumentException("Azimuth " + azimuth + " must be in (0, 360)"); } - double angleXY = PI_2 - azimuth/180.*Math.PI; - if (angleXY<-Math.PI) angleXY += 2*Math.PI; - if (angleXY>Math.PI) angleXY -= 2*Math.PI; + double angleXY = PI_2 - azimuth / 180. * Math.PI; + if (angleXY < -Math.PI) + angleXY += 2 * Math.PI; + if (angleXY > Math.PI) + angleXY -= 2 * Math.PI; return angleXY; } @@ -86,23 +83,18 @@ public double convertAzimuth2xaxisAngle(double azimuth) * will be smaller or equal to PI (180 degree). This is achieved by adding or substracting a * 2*PI, so the direction of the orientation will not be changed */ - public double alignOrientation( double baseOrientation, double orientation ) - { + public double alignOrientation(double baseOrientation, double orientation) { double resultOrientation; - if (baseOrientation >= 0) - { + if (baseOrientation >= 0) { if (orientation < -Math.PI + baseOrientation) resultOrientation = orientation + 2 * Math.PI; else resultOrientation = orientation; - } else - { - if (orientation > +Math.PI + baseOrientation) - resultOrientation = orientation - 2 * Math.PI; - else - resultOrientation = orientation; - } + } else if (orientation > +Math.PI + baseOrientation) + resultOrientation = orientation - 2 * Math.PI; + else + resultOrientation = orientation; return resultOrientation; } @@ -110,8 +102,7 @@ public double alignOrientation( double baseOrientation, double orientation ) * Calculate the azimuth in degree for a line given by two coordinates. Direction in 'degree' * where 0 is north, 90 is east, 180 is south and 270 is west. */ - double calcAzimuth( double lat1, double lon1, double lat2, double lon2 ) - { + double calcAzimuth(double lat1, double lon1, double lat2, double lon2) { double orientation = -calcOrientation(lat1, lon1, lat2, lon2); orientation = Helper.round4(orientation + Math.PI / 2); if (orientation < 0) @@ -120,37 +111,27 @@ public double alignOrientation( double baseOrientation, double orientation ) return Math.toDegrees(orientation); } - String azimuth2compassPoint( double azimuth ) - { + String azimuth2compassPoint(double azimuth) { String cp; double slice = 360.0 / 16; - if (azimuth < slice) - { + if (azimuth < slice) { cp = "N"; - } else if (azimuth < slice * 3) - { + } else if (azimuth < slice * 3) { cp = "NE"; - } else if (azimuth < slice * 5) - { + } else if (azimuth < slice * 5) { cp = "E"; - } else if (azimuth < slice * 7) - { + } else if (azimuth < slice * 7) { cp = "SE"; - } else if (azimuth < slice * 9) - { + } else if (azimuth < slice * 9) { cp = "S"; - } else if (azimuth < slice * 11) - { + } else if (azimuth < slice * 11) { cp = "SW"; - } else if (azimuth < slice * 13) - { + } else if (azimuth < slice * 13) { cp = "W"; - } else if (azimuth < slice * 15) - { + } else if (azimuth < slice * 15) { cp = "NW"; - } else - { + } else { cp = "N"; } return cp; diff --git a/core/src/main/java/com/graphhopper/util/BitUtil.java b/core/src/main/java/com/graphhopper/util/BitUtil.java index e38006621dd..75265c42a30 100644 --- a/core/src/main/java/com/graphhopper/util/BitUtil.java +++ b/core/src/main/java/com/graphhopper/util/BitUtil.java @@ -26,11 +26,11 @@ * 1=>1110 1011 * 2=>... * LITTLE endianess is default for GraphHopper and most microprocessors. - *

    + * + * * @author Peter Karich */ -public abstract class BitUtil -{ +public abstract class BitUtil { /** * Default for GraphHopper */ @@ -40,146 +40,124 @@ public abstract class BitUtil */ public static final BitUtil BIG = new BitUtilBig(); - public static BitUtil get( ByteOrder order ) - { + public static BitUtil get(ByteOrder order) { if (order.equals(ByteOrder.BIG_ENDIAN)) return BitUtil.BIG; else return BitUtil.LITTLE; } - public final double toDouble( byte[] bytes ) - { + public final double toDouble(byte[] bytes) { return toDouble(bytes, 0); } - public final double toDouble( byte[] bytes, int offset ) - { + public final double toDouble(byte[] bytes, int offset) { return Double.longBitsToDouble(toLong(bytes, offset)); } - public final byte[] fromDouble( double value ) - { + public final byte[] fromDouble(double value) { byte[] bytes = new byte[8]; fromDouble(bytes, value, 0); return bytes; } - public final void fromDouble( byte[] bytes, double value ) - { + public final void fromDouble(byte[] bytes, double value) { fromDouble(bytes, value, 0); } - public final void fromDouble( byte[] bytes, double value, int offset ) - { + public final void fromDouble(byte[] bytes, double value, int offset) { fromLong(bytes, Double.doubleToRawLongBits(value), offset); } - public final float toFloat( byte[] bytes ) - { + public final float toFloat(byte[] bytes) { return toFloat(bytes, 0); } - public final float toFloat( byte[] bytes, int offset ) - { + public final float toFloat(byte[] bytes, int offset) { return Float.intBitsToFloat(toInt(bytes, offset)); } - public final byte[] fromFloat( float value ) - { + public final byte[] fromFloat(float value) { byte[] bytes = new byte[4]; fromFloat(bytes, value, 0); return bytes; } - public final void fromFloat( byte[] bytes, float value ) - { + public final void fromFloat(byte[] bytes, float value) { fromFloat(bytes, value, 0); } - public final void fromFloat( byte[] bytes, float value, int offset ) - { + public final void fromFloat(byte[] bytes, float value, int offset) { fromInt(bytes, Float.floatToRawIntBits(value), offset); } - public final short toShort( byte[] b ) - { + public final short toShort(byte[] b) { return toShort(b, 0); } - public abstract short toShort( byte[] b, int offset ); + public abstract short toShort(byte[] b, int offset); - public final int toInt( byte[] b ) - { + public final int toInt(byte[] b) { return toInt(b, 0); } - public abstract int toInt( byte[] b, int offset ); + public abstract int toInt(byte[] b, int offset); - public final byte[] fromInt( int value ) - { + public final byte[] fromInt(int value) { byte[] bytes = new byte[4]; fromInt(bytes, value, 0); return bytes; } - public final void fromInt( byte[] bytes, int value ) - { + public final void fromInt(byte[] bytes, int value) { fromInt(bytes, value, 0); } - public final byte[] fromShort( short value ) - { + public final byte[] fromShort(short value) { byte[] bytes = new byte[4]; fromShort(bytes, value, 0); return bytes; } - public final void fromShort( byte[] bytes, short value ) - { + public final void fromShort(byte[] bytes, short value) { fromShort(bytes, value, 0); } - public abstract void fromShort( byte[] bytes, short value, int offset ); + public abstract void fromShort(byte[] bytes, short value, int offset); - public abstract void fromInt( byte[] bytes, int value, int offset ); + public abstract void fromInt(byte[] bytes, int value, int offset); - public final long toLong( byte[] b ) - { + public final long toLong(byte[] b) { return toLong(b, 0); } - public abstract long toLong( int high, int low ); + public abstract long toLong(int high, int low); - public abstract long toLong( byte[] b, int offset ); + public abstract long toLong(byte[] b, int offset); - public final byte[] fromLong( long value ) - { + public final byte[] fromLong(long value) { byte[] bytes = new byte[8]; fromLong(bytes, value, 0); return bytes; } - public final void fromLong( byte[] bytes, long value ) - { + public final void fromLong(byte[] bytes, long value) { fromLong(bytes, value, 0); } - public abstract void fromLong( byte[] bytes, long value, int offset ); + public abstract void fromLong(byte[] bytes, long value, int offset); /** * The only purpose of this method is to test 'reverse'. toBitString is the reverse and both are * indepentent of the endianness. */ - public final long fromBitString2Long( String str ) - { + public final long fromBitString2Long(String str) { if (str.length() > 64) throw new UnsupportedOperationException("Strings needs to fit into a 'long' but length was " + str.length()); long res = 0; int strLen = str.length(); - for (int charIndex = 0; charIndex < strLen; charIndex++) - { + for (int charIndex = 0; charIndex < strLen; charIndex++) { res <<= 1; if (str.charAt(charIndex) != '0') res |= 1; @@ -188,22 +166,19 @@ public final long fromBitString2Long( String str ) return res; } - public abstract byte[] fromBitString( String str ); + public abstract byte[] fromBitString(String str); /** * Similar to Long.toBinaryString */ - public final String toBitString( long value ) - { + public final String toBitString(long value) { return toBitString(value, 64); } - public String toLastBitString( long value, int bits ) - { + public String toLastBitString(long value, int bits) { StringBuilder sb = new StringBuilder(bits); long lastBit = 1L << bits - 1; - for (int i = 0; i < bits; i++) - { + for (int i = 0; i < bits; i++) { if ((value & lastBit) == 0) sb.append('0'); else @@ -217,14 +192,13 @@ public String toLastBitString( long value, int bits ) /** * Higher order bits comes first in the returned string. *

    + * * @param bits how many bits should be returned. */ - public String toBitString( long value, int bits ) - { + public String toBitString(long value, int bits) { StringBuilder sb = new StringBuilder(bits); long lastBit = 1L << 63; - for (int i = 0; i < bits; i++) - { + for (int i = 0; i < bits; i++) { if ((value & lastBit) == 0) sb.append('0'); else @@ -238,24 +212,22 @@ public String toBitString( long value, int bits ) /** * Higher order bits comes first in the returned string. */ - public abstract String toBitString( byte[] bytes ); + public abstract String toBitString(byte[] bytes); /** * Reverses the bits in the specified long value and it removes the remaining higher bits. See * also http://graphics.stanford.edu/~seander/bithacks.html#BitReverseObvious *

    + * * @param maxBits the maximum number of recognized bits for reversal */ - public final long reverse( long value, int maxBits ) - { + public final long reverse(long value, int maxBits) { long res = 0; - for (; maxBits > 0; value >>>= 1) - { + for (; maxBits > 0; value >>>= 1) { res <<= 1; res |= value & 1; maxBits--; - if (value == 0) - { + if (value == 0) { res <<= maxBits; break; } @@ -263,35 +235,29 @@ public final long reverse( long value, int maxBits ) return res; } - public final int getIntLow( long longValue ) - { + public final int getIntLow(long longValue) { return (int) (longValue & 0xFFFFFFFFL); } - public final int getIntHigh( long longValue ) - { + public final int getIntHigh(long longValue) { return (int) (longValue >> 32); } - public final long combineIntsToLong( int intLow, int intHigh ) - { + public final long combineIntsToLong(int intLow, int intHigh) { return ((long) intHigh << 32) | (intLow & 0xFFFFFFFFL); } - public final long reverseLeft( long value, int maxBits ) - { + public final long reverseLeft(long value, int maxBits) { long res = 0; int delta = 64 - maxBits; long maxBit = 1L << delta; - for (; maxBits > 0; res <<= 1) - { + for (; maxBits > 0; res <<= 1) { if ((value & maxBit) != 0) res |= 1; maxBit <<= 1; maxBits--; - if (maxBit == 0) - { + if (maxBit == 0) { res <<= delta; break; } diff --git a/core/src/main/java/com/graphhopper/util/BitUtilBig.java b/core/src/main/java/com/graphhopper/util/BitUtilBig.java index b46813c6020..1c263c14076 100644 --- a/core/src/main/java/com/graphhopper/util/BitUtilBig.java +++ b/core/src/main/java/com/graphhopper/util/BitUtilBig.java @@ -20,37 +20,32 @@ /** * Conversion between "the memory" (integer/long/float/double/string) to bytes via BIG endianess. *

    + * * @author Peter Karich */ -public class BitUtilBig extends BitUtil -{ - BitUtilBig() - { +public class BitUtilBig extends BitUtil { + BitUtilBig() { } @Override - public final short toShort( byte[] b, int offset ) - { + public final short toShort(byte[] b, int offset) { return (short) ((b[offset] & 0xFF) << 8 | (b[offset + 1] & 0xFF)); } @Override - public final int toInt( byte[] b, int offset ) - { + public final int toInt(byte[] b, int offset) { return (b[offset] & 0xFF) << 24 | (b[++offset] & 0xFF) << 16 | (b[++offset] & 0xFF) << 8 | (b[++offset] & 0xFF); } @Override - public void fromShort( byte[] bytes, short value, int offset ) - { + public void fromShort(byte[] bytes, short value, int offset) { bytes[offset] = (byte) (value >> 8); bytes[offset + 1] = (byte) (value); } @Override - public final void fromInt( byte[] bytes, int value, int offset ) - { + public final void fromInt(byte[] bytes, int value, int offset) { bytes[offset] = (byte) (value >> 24); bytes[++offset] = (byte) (value >> 16); bytes[++offset] = (byte) (value >> 8); @@ -58,20 +53,17 @@ public final void fromInt( byte[] bytes, int value, int offset ) } @Override - public final long toLong( int int0, int int1 ) - { + public final long toLong(int int0, int int1) { return ((long) int0 << 32) | (int1 & 0xFFFFFFFFL); } @Override - public final long toLong( byte[] b, int offset ) - { + public final long toLong(byte[] b, int offset) { return ((long) toInt(b, offset) << 32) | (toInt(b, offset + 4) & 0xFFFFFFFFL); } @Override - public final void fromLong( byte[] bytes, long value, int offset ) - { + public final void fromLong(byte[] bytes, long value, int offset) { bytes[offset] = (byte) (value >> 56); bytes[++offset] = (byte) (value >> 48); bytes[++offset] = (byte) (value >> 40); @@ -83,8 +75,7 @@ public final void fromLong( byte[] bytes, long value, int offset ) } @Override - public byte[] fromBitString( String str ) - { + public byte[] fromBitString(String str) { // no need for performance or memory tuning ... int strLen = str.length(); int bLen = str.length() / 8; @@ -93,11 +84,9 @@ public byte[] fromBitString( String str ) byte[] bytes = new byte[bLen]; int charI = 0; - for (int b = 0; b < bLen; b++) - { + for (int b = 0; b < bLen; b++) { byte res = 0; - for (int i = 0; i < 8; i++) - { + for (int i = 0; i < 8; i++) { res <<= 1; if (charI < strLen && str.charAt(charI) != '0') res |= 1; @@ -110,14 +99,11 @@ public byte[] fromBitString( String str ) } @Override - public String toBitString( byte[] bytes ) - { + public String toBitString(byte[] bytes) { StringBuilder sb = new StringBuilder(bytes.length * 8); byte lastBit = (byte) (1 << 7); - for (byte b : bytes) - { - for (int i = 0; i < 8; i++) - { + for (byte b : bytes) { + for (int i = 0; i < 8; i++) { if ((b & lastBit) == 0) sb.append('0'); else @@ -132,15 +118,13 @@ public String toBitString( byte[] bytes ) /** * Touches only the specified bits - it does not zero out the higher bits (like reverse does). */ - final long reversePart( long v, int maxBits ) - { + final long reversePart(long v, int maxBits) { long rest = v & (~((1L << maxBits) - 1)); return rest | reverse(v, maxBits); } @Override - public String toString() - { + public String toString() { return "big"; } } diff --git a/core/src/main/java/com/graphhopper/util/BitUtilLittle.java b/core/src/main/java/com/graphhopper/util/BitUtilLittle.java index 7e56b6b0c9f..5b4715551f5 100644 --- a/core/src/main/java/com/graphhopper/util/BitUtilLittle.java +++ b/core/src/main/java/com/graphhopper/util/BitUtilLittle.java @@ -20,37 +20,32 @@ /** * Conversion between "the memory" (integer/long/float/double/string) to bytes via little endianess. *

    + * * @author Peter Karich */ -public class BitUtilLittle extends BitUtil -{ - BitUtilLittle() - { +public class BitUtilLittle extends BitUtil { + BitUtilLittle() { } @Override - public final short toShort( byte[] b, int offset ) - { + public final short toShort(byte[] b, int offset) { return (short) ((b[offset + 1] & 0xFF) << 8 | (b[offset] & 0xFF)); } @Override - public final int toInt( byte[] b, int offset ) - { + public final int toInt(byte[] b, int offset) { return (b[offset + 3] & 0xFF) << 24 | (b[offset + 2] & 0xFF) << 16 | (b[offset + 1] & 0xFF) << 8 | (b[offset] & 0xFF); } @Override - public void fromShort( byte[] bytes, short value, int offset ) - { + public void fromShort(byte[] bytes, short value, int offset) { bytes[offset + 1] = (byte) (value >>> 8); bytes[offset] = (byte) (value); } @Override - public final void fromInt( byte[] bytes, int value, int offset ) - { + public final void fromInt(byte[] bytes, int value, int offset) { bytes[offset + 3] = (byte) (value >>> 24); bytes[offset + 2] = (byte) (value >>> 16); bytes[offset + 1] = (byte) (value >>> 8); @@ -58,20 +53,17 @@ public final void fromInt( byte[] bytes, int value, int offset ) } @Override - public final long toLong( int int0, int int1 ) - { + public final long toLong(int int0, int int1) { return ((long) int1 << 32) | (int0 & 0xFFFFFFFFL); } @Override - public final long toLong( byte[] b, int offset ) - { + public final long toLong(byte[] b, int offset) { return ((long) toInt(b, offset + 4) << 32) | (toInt(b, offset) & 0xFFFFFFFFL); } @Override - public final void fromLong( byte[] bytes, long value, int offset ) - { + public final void fromLong(byte[] bytes, long value, int offset) { bytes[offset + 7] = (byte) (value >> 56); bytes[offset + 6] = (byte) (value >> 48); bytes[offset + 5] = (byte) (value >> 40); @@ -83,8 +75,7 @@ public final void fromLong( byte[] bytes, long value, int offset ) } @Override - public byte[] fromBitString( String str ) - { + public byte[] fromBitString(String str) { // no need for performance or memory tuning ... int strLen = str.length(); int bLen = str.length() / 8; @@ -93,11 +84,9 @@ public byte[] fromBitString( String str ) byte[] bytes = new byte[bLen]; int charI = 0; - for (int b = bLen - 1; b >= 0; b--) - { + for (int b = bLen - 1; b >= 0; b--) { byte res = 0; - for (int i = 0; i < 8; i++) - { + for (int i = 0; i < 8; i++) { res <<= 1; if (charI < strLen && str.charAt(charI) != '0') res |= 1; @@ -110,15 +99,12 @@ public byte[] fromBitString( String str ) } @Override - public String toBitString( byte[] bytes ) - { + public String toBitString(byte[] bytes) { StringBuilder sb = new StringBuilder(bytes.length * 8); byte lastBit = (byte) (1 << 7); - for (int bIndex = bytes.length - 1; bIndex >= 0; bIndex--) - { + for (int bIndex = bytes.length - 1; bIndex >= 0; bIndex--) { byte b = bytes[bIndex]; - for (int i = 0; i < 8; i++) - { + for (int i = 0; i < 8; i++) { if ((b & lastBit) == 0) sb.append('0'); else @@ -131,8 +117,7 @@ public String toBitString( byte[] bytes ) } @Override - public String toString() - { + public String toString() { return "little"; } } diff --git a/core/src/main/java/com/graphhopper/util/BreadthFirstSearch.java b/core/src/main/java/com/graphhopper/util/BreadthFirstSearch.java index ce09a26394d..be5009a74b3 100644 --- a/core/src/main/java/com/graphhopper/util/BreadthFirstSearch.java +++ b/core/src/main/java/com/graphhopper/util/BreadthFirstSearch.java @@ -22,30 +22,26 @@ /** * Implementattion of breadth first search (BFS) *

    + * * @author Peter Karich */ -public class BreadthFirstSearch extends XFirstSearch -{ +public class BreadthFirstSearch extends XFirstSearch { @Override - public void start( EdgeExplorer explorer, int startNode ) - { + public void start(EdgeExplorer explorer, int startNode) { SimpleIntDeque fifo = new SimpleIntDeque(); GHBitSet visited = createBitSet(); visited.add(startNode); fifo.push(startNode); int current; - while (!fifo.isEmpty()) - { + while (!fifo.isEmpty()) { current = fifo.pop(); if (!goFurther(current)) continue; EdgeIterator iter = explorer.setBaseNode(current); - while (iter.next()) - { + while (iter.next()) { int connectedId = iter.getAdjNode(); - if (checkAdjacent(iter) && !visited.contains(connectedId)) - { + if (checkAdjacent(iter) && !visited.contains(connectedId)) { visited.add(connectedId); fifo.push(connectedId); } diff --git a/core/src/main/java/com/graphhopper/util/CHEdgeExplorer.java b/core/src/main/java/com/graphhopper/util/CHEdgeExplorer.java index 0183034f1ee..157d4c69dd0 100644 --- a/core/src/main/java/com/graphhopper/util/CHEdgeExplorer.java +++ b/core/src/main/java/com/graphhopper/util/CHEdgeExplorer.java @@ -22,11 +22,11 @@ /** * The edge explorer for CHGraph *

    + * * @author Peter Karich * @see CHGraph */ -public interface CHEdgeExplorer extends EdgeExplorer -{ +public interface CHEdgeExplorer extends EdgeExplorer { @Override - CHEdgeIterator setBaseNode( int baseNode ); + CHEdgeIterator setBaseNode(int baseNode); } diff --git a/core/src/main/java/com/graphhopper/util/CHEdgeIterator.java b/core/src/main/java/com/graphhopper/util/CHEdgeIterator.java index d06eebc0008..01dfc0504b1 100644 --- a/core/src/main/java/com/graphhopper/util/CHEdgeIterator.java +++ b/core/src/main/java/com/graphhopper/util/CHEdgeIterator.java @@ -22,9 +22,9 @@ /** * Support for CH edges *

    + * * @author Peter Karich * @see CHGraph */ -public interface CHEdgeIterator extends EdgeIterator, CHEdgeIteratorState -{ +public interface CHEdgeIterator extends EdgeIterator, CHEdgeIteratorState { } diff --git a/core/src/main/java/com/graphhopper/util/CHEdgeIteratorState.java b/core/src/main/java/com/graphhopper/util/CHEdgeIteratorState.java index a5d3fe8190b..c9651fb269c 100644 --- a/core/src/main/java/com/graphhopper/util/CHEdgeIteratorState.java +++ b/core/src/main/java/com/graphhopper/util/CHEdgeIteratorState.java @@ -23,12 +23,12 @@ /** * The state returned from the EdgeIterator of a CHGraph *

    + * * @author Peter Karich * @see CHGraph * @see CHEdgeIterator */ -public interface CHEdgeIteratorState extends EdgeIteratorState -{ +public interface CHEdgeIteratorState extends EdgeIteratorState { int getSkippedEdge1(); int getSkippedEdge2(); @@ -36,7 +36,7 @@ public interface CHEdgeIteratorState extends EdgeIteratorState /** * Sets the edges that this shortcut skips. Those skipped edges can be shortcuts too. */ - void setSkippedEdges( int edge1, int edge2 ); + void setSkippedEdges(int edge1, int edge2); /** * @return true if this edge is a shortcut, false otherwise. @@ -46,17 +46,18 @@ public interface CHEdgeIteratorState extends EdgeIteratorState /** * This method is only used on preparation. *

    + * * @see PrepareEncoder#canBeOverwritten(long, long) */ - boolean canBeOverwritten( long flags ); + boolean canBeOverwritten(long flags); /** - * Sets the weight calculated from Weighting.calcWeight, only applicable if isShortcut is true. + * Returns the weight of this shortcut. */ - CHEdgeIteratorState setWeight( double weight ); + double getWeight(); /** - * Returns the weight of this shortcut. + * Sets the weight calculated from Weighting.calcWeight, only applicable if isShortcut is true. */ - double getWeight(); + CHEdgeIteratorState setWeight(double weight); } diff --git a/core/src/main/java/com/graphhopper/util/CmdArgs.java b/core/src/main/java/com/graphhopper/util/CmdArgs.java index 0b8e654b1ae..39d13f65ba6 100644 --- a/core/src/main/java/com/graphhopper/util/CmdArgs.java +++ b/core/src/main/java/com/graphhopper/util/CmdArgs.java @@ -29,33 +29,23 @@ /** * Stores command line options in a map. The capitalization of the key is ignored. *

    + * * @author Peter Karich */ -public class CmdArgs extends PMap -{ +public class CmdArgs extends PMap { - public CmdArgs() - { + public CmdArgs() { } - public CmdArgs( Map map ) - { + public CmdArgs(Map map) { super(map); } - @Override - public CmdArgs put( String key, Object str ) - { - super.put(key, str); - return this; - } - /** - * @param fileStr the file name of config.properties + * @param fileStr the file name of config.properties * @param systemProperty the property name of the configuration. E.g. -Dgraphhopper.config */ - public static CmdArgs readFromConfig( String fileStr, String systemProperty ) throws IOException - { + public static CmdArgs readFromConfig(String fileStr, String systemProperty) throws IOException { if (systemProperty.startsWith("-D")) systemProperty = systemProperty.substring(2); @@ -71,12 +61,10 @@ public static CmdArgs readFromConfig( String fileStr, String systemProperty ) th // overwrite with system settings Properties props = System.getProperties(); - for (Entry e : props.entrySet()) - { + for (Entry e : props.entrySet()) { String k = ((String) e.getKey()); String v = ((String) e.getValue()); - if (k.startsWith("graphhopper.")) - { + if (k.startsWith("graphhopper.")) { k = k.substring("graphhopper.".length()); args.put(k, v); } @@ -84,25 +72,20 @@ public static CmdArgs readFromConfig( String fileStr, String systemProperty ) th return args; } - public static CmdArgs read( String[] args ) - { + public static CmdArgs read(String[] args) { Map map = new LinkedHashMap(); - for (String arg : args) - { + for (String arg : args) { int index = arg.indexOf("="); - if (index <= 0) - { + if (index <= 0) { continue; } String key = arg.substring(0, index); - if (key.startsWith("-")) - { + if (key.startsWith("-")) { key = key.substring(1); } - if (key.startsWith("-")) - { + if (key.startsWith("-")) { key = key.substring(1); } @@ -116,23 +99,26 @@ public static CmdArgs read( String[] args ) /** * Command line configuration overwrites the ones in the config file. *

    + * * @return a new CmdArgs object if necessary. */ - public static CmdArgs readFromConfigAndMerge( CmdArgs args, String configKey, String configSysAttr ) - { + public static CmdArgs readFromConfigAndMerge(CmdArgs args, String configKey, String configSysAttr) { String configVal = args.get(configKey, ""); - if (!Helper.isEmpty(configVal)) - { - try - { + if (!Helper.isEmpty(configVal)) { + try { CmdArgs tmp = CmdArgs.readFromConfig(configVal, configSysAttr); tmp.merge(args); return tmp; - } catch (Exception ex) - { + } catch (Exception ex) { throw new RuntimeException(ex); } } return args; } + + @Override + public CmdArgs put(String key, Object str) { + super.put(key, str); + return this; + } } diff --git a/core/src/main/java/com/graphhopper/util/ConfigMap.java b/core/src/main/java/com/graphhopper/util/ConfigMap.java index 69d3a068949..74c4c93b3ca 100644 --- a/core/src/main/java/com/graphhopper/util/ConfigMap.java +++ b/core/src/main/java/com/graphhopper/util/ConfigMap.java @@ -24,43 +24,37 @@ /** * A properties map (String to Object) with convenient accessors *

    - * @see PMap + * * @author Peter Karich + * @see PMap */ -public class ConfigMap -{ +public class ConfigMap { private final Map map; - public ConfigMap() - { + public ConfigMap() { this(5); } - public ConfigMap( int capacity ) - { + public ConfigMap(int capacity) { this(new HashMap(capacity)); } - public ConfigMap( Map map ) - { + public ConfigMap(Map map) { this.map = map; } - public ConfigMap put( ConfigMap map ) - { + public ConfigMap put(ConfigMap map) { this.map.putAll(map.map); return this; } - String checkKey( String key ) - { + String checkKey(String key) { if (!key.toLowerCase().equals(key)) throw new NullPointerException("keys have to be lower case but wasn't: " + key); return key; } - public ConfigMap put( String key, Object obj ) - { + public ConfigMap put(String key, Object obj) { if (obj == null) throw new NullPointerException("Value cannot be null for key " + key + ". Use remove instead."); @@ -68,80 +62,68 @@ public ConfigMap put( String key, Object obj ) return this; } - public ConfigMap remove( String key ) - { + public ConfigMap remove(String key) { map.remove(checkKey(key)); return this; } - public boolean has( String key ) - { + public boolean has(String key) { return map.containsKey(checkKey(key)); } - public long getLong( String key, long _default ) - { + public long getLong(String key, long _default) { Long t = (Long) map.get(checkKey(key)); if (t == null) return _default; return t; } - public int getInt( String key, int _default ) - { + public int getInt(String key, int _default) { Integer t = (Integer) map.get(checkKey(key)); if (t == null) return _default; return t; } - public boolean getBool( String key, boolean _default ) - { + public boolean getBool(String key, boolean _default) { Boolean t = (Boolean) map.get(checkKey(key)); if (t == null) return _default; return t; } - public double getDouble( String key, double _default ) - { + public double getDouble(String key, double _default) { Double t = (Double) map.get(checkKey(key)); if (t == null) return _default; return t; } - public T get( String key, T _default ) - { + public T get(String key, T _default) { T t = (T) map.get(checkKey(key)); if (t == null) return _default; return t; } - public Map getMap( String key, Class embed ) - { + public Map getMap(String key, Class embed) { return (Map) map.get(checkKey(key)); } - public Map getMap( String key ) - { + public Map getMap(String key) { return (Map) map.get(checkKey(key)); } - public List getList( String key ) - { + public List getList(String key) { return (List) map.get(checkKey(key)); } - public List getList( String key, Class embed ) - { + public List getList(String key, Class embed) { return (List) map.get(checkKey(key)); } @Override - public String toString() - { + public String toString() { return map.toString(); } } diff --git a/core/src/main/java/com/graphhopper/util/Constants.java b/core/src/main/java/com/graphhopper/util/Constants.java index 3ed9c6c3a05..4b4aab0ec6f 100644 --- a/core/src/main/java/com/graphhopper/util/Constants.java +++ b/core/src/main/java/com/graphhopper/util/Constants.java @@ -18,16 +18,16 @@ package com.graphhopper.util; import com.graphhopper.GraphHopper; -import static com.graphhopper.util.Helper.readFile; import java.io.InputStreamReader; import java.util.List; +import static com.graphhopper.util.Helper.readFile; + /** * Defining several important constants for GraphHopper. Partially taken from Lucene. */ -public class Constants -{ +public class Constants { /** * The value of System.getProperty("java.version"). * */ @@ -68,38 +68,26 @@ public class Constants public static final String BUILD_DATE; public static final boolean SNAPSHOT; - public static String getVersions() - { - return VERSION_NODE + "," + VERSION_EDGE + "," + VERSION_GEOMETRY + "," + VERSION_LOCATION_IDX - + "," + VERSION_NAME_IDX + "," + VERSION_SHORTCUT; - } - - static - { + static { String version = "0.0"; - try - { - // see com/graphhopper/version file in resources which is modified in the maven packaging process + try { + // see com/graphhopper/version file in resources which is modified in the maven packaging process // to contain the current version List v = readFile(new InputStreamReader(GraphHopper.class.getResourceAsStream("version"), Helper.UTF_CS)); version = v.get(0); - } catch (Exception ex) - { + } catch (Exception ex) { System.err.println("GraphHopper Initialization ERROR: cannot read version!? " + ex.getMessage()); } int indexM = version.indexOf("-"); - if ("${project.version}".equals(version)) - { + if ("${project.version}".equals(version)) { VERSION = "0.0"; SNAPSHOT = true; System.err.println("GraphHopper Initialization WARNING: maven did not preprocess the version file! Do not use the jar for a release!"); - } else if ("0.0".equals(version)) - { + } else if ("0.0".equals(version)) { VERSION = "0.0"; SNAPSHOT = true; System.err.println("GraphHopper Initialization WARNING: cannot get version!?"); - } else - { + } else { String tmp = version; // throw away the "-SNAPSHOT" if (indexM >= 0) @@ -109,22 +97,24 @@ public static String getVersions() VERSION = tmp; } String buildDate = ""; - try - { + try { List v = readFile(new InputStreamReader(GraphHopper.class.getResourceAsStream("builddate"), Helper.UTF_CS)); buildDate = v.get(0); - } catch (Exception ex) - { + } catch (Exception ex) { } BUILD_DATE = buildDate; } - public static String getMajorVersion() - { + public static String getVersions() { + return VERSION_NODE + "," + VERSION_EDGE + "," + VERSION_GEOMETRY + "," + VERSION_LOCATION_IDX + + "," + VERSION_NAME_IDX + "," + VERSION_SHORTCUT; + } + + public static String getMajorVersion() { int firstIdx = VERSION.indexOf("."); if (firstIdx < 0) throw new IllegalStateException("Cannot extract major version from version " + VERSION); - + int sndIdx = VERSION.indexOf(".", firstIdx + 1); if (sndIdx < 0) return VERSION; diff --git a/core/src/main/java/com/graphhopper/util/DepthFirstSearch.java b/core/src/main/java/com/graphhopper/util/DepthFirstSearch.java index 53b614c9fd7..e89f09678bb 100644 --- a/core/src/main/java/com/graphhopper/util/DepthFirstSearch.java +++ b/core/src/main/java/com/graphhopper/util/DepthFirstSearch.java @@ -23,34 +23,29 @@ /** * Implementation of depth first search (DFS) by LIFO queue *

    + * * @author Peter Karich * @author Jan Sölter */ -public class DepthFirstSearch extends XFirstSearch -{ +public class DepthFirstSearch extends XFirstSearch { /** * beginning with startNode add all following nodes to LIFO queue. If node has been already * explored before, skip reexploration. */ @Override - public void start( EdgeExplorer explorer, int startNode ) - { + public void start(EdgeExplorer explorer, int startNode) { TIntArrayStack stack = new TIntArrayStack(); GHBitSet explored = createBitSet(); stack.push(startNode); int current; - while (stack.size() > 0) - { + while (stack.size() > 0) { current = stack.pop(); - if (!explored.contains(current) && goFurther(current)) - { + if (!explored.contains(current) && goFurther(current)) { EdgeIterator iter = explorer.setBaseNode(current); - while (iter.next()) - { + while (iter.next()) { int connectedId = iter.getAdjNode(); - if (checkAdjacent(iter)) - { + if (checkAdjacent(iter)) { stack.push(connectedId); } } diff --git a/core/src/main/java/com/graphhopper/util/DistanceCalc.java b/core/src/main/java/com/graphhopper/util/DistanceCalc.java index dcb7029260c..b36bb97d90b 100644 --- a/core/src/main/java/com/graphhopper/util/DistanceCalc.java +++ b/core/src/main/java/com/graphhopper/util/DistanceCalc.java @@ -28,31 +28,30 @@ * * @author Peter Karich */ -public interface DistanceCalc -{ - BBox createBBox( double lat, double lon, double radiusInMeter ); +public interface DistanceCalc { + BBox createBBox(double lat, double lon, double radiusInMeter); - double calcCircumference( double lat ); + double calcCircumference(double lat); /** * Calculates distance of (from, to) in meter. */ - double calcDist( double fromLat, double fromLon, double toLat, double toLon ); + double calcDist(double fromLat, double fromLon, double toLat, double toLon); /** * Returns the specified length in normalized meter. */ - double calcNormalizedDist( double dist ); + double calcNormalizedDist(double dist); /** * Inverse to calcNormalizedDist. Returned the length in meter. */ - double calcDenormalizedDist( double normedDist ); + double calcDenormalizedDist(double normedDist); /** * Calculates in normalized meter */ - double calcNormalizedDist( double fromLat, double fromLon, double toLat, double toLon ); + double calcNormalizedDist(double fromLat, double fromLon, double toLat, double toLon); /** * This method decides for case 1: if we should use distance(r to edge) where r=(lat,lon) or @@ -73,36 +72,36 @@ public interface DistanceCalc * * @return true for case 1 which is "on edge" or the special case of 90° to the edge */ - boolean validEdgeDistance( double r_lat_deg, double r_lon_deg, - double a_lat_deg, double a_lon_deg, - double b_lat_deg, double b_lon_deg ); + boolean validEdgeDistance(double r_lat_deg, double r_lon_deg, + double a_lat_deg, double a_lon_deg, + double b_lat_deg, double b_lon_deg); /** * This method calculates the distance from r to edge (a, b) where the crossing point is c * * @return the distance in normalized meter */ - double calcNormalizedEdgeDistance( double r_lat_deg, double r_lon_deg, - double a_lat_deg, double a_lon_deg, - double b_lat_deg, double b_lon_deg ); + double calcNormalizedEdgeDistance(double r_lat_deg, double r_lon_deg, + double a_lat_deg, double a_lon_deg, + double b_lat_deg, double b_lon_deg); /** * @return the crossing point c of the vertical line from r to line (a, b) */ - GHPoint calcCrossingPointToEdge( double r_lat_deg, double r_lon_deg, - double a_lat_deg, double a_lon_deg, - double b_lat_deg, double b_lon_deg ); + GHPoint calcCrossingPointToEdge(double r_lat_deg, double r_lon_deg, + double a_lat_deg, double a_lon_deg, + double b_lat_deg, double b_lon_deg); /** * This methods projects a point given in lat and long (in degrees) into a direction, given as * heading, measured clockwise from north in degrees. The distance is passed in km. */ - public GHPoint projectCoordinate( double lat_deg, double lon_deg, - double distanceInMeter, double headingClockwiseFromNorth ); + public GHPoint projectCoordinate(double lat_deg, double lon_deg, + double distanceInMeter, double headingClockwiseFromNorth); /* * Simple heuristic to detect if the specified two points are crossing the boundary +-180°. See * #667 */ - boolean isCrossBoundary( double lon1, double lon2 ); + boolean isCrossBoundary(double lon1, double lon2); } diff --git a/core/src/main/java/com/graphhopper/util/DistanceCalc2D.java b/core/src/main/java/com/graphhopper/util/DistanceCalc2D.java index 22512905f1e..489aecce2f9 100644 --- a/core/src/main/java/com/graphhopper/util/DistanceCalc2D.java +++ b/core/src/main/java/com/graphhopper/util/DistanceCalc2D.java @@ -17,24 +17,22 @@ */ package com.graphhopper.util; -import static java.lang.Math.*; +import static java.lang.Math.sqrt; /** * Calculates the distance of two points or one point and an edge in euclidean space. *

    + * * @author Peter Karich */ -public class DistanceCalc2D extends DistanceCalcEarth -{ +public class DistanceCalc2D extends DistanceCalcEarth { @Override - public double calcDist( double fromY, double fromX, double toY, double toX ) - { + public double calcDist(double fromY, double fromX, double toY, double toX) { return sqrt(calcNormalizedDist(fromY, fromX, toY, toX)); } @Override - public double calcDenormalizedDist( double normedDist ) - { + public double calcDenormalizedDist(double normedDist) { return sqrt(normedDist); } @@ -42,8 +40,7 @@ public double calcDenormalizedDist( double normedDist ) * Returns the specified length in normalized meter. */ @Override - public double calcNormalizedDist( double dist ) - { + public double calcNormalizedDist(double dist) { return dist * dist; } @@ -51,16 +48,14 @@ public double calcNormalizedDist( double dist ) * Calculates in normalized meter */ @Override - public double calcNormalizedDist( double fromY, double fromX, double toY, double toX ) - { + public double calcNormalizedDist(double fromY, double fromX, double toY, double toX) { double dX = fromX - toX; double dY = fromY - toY; return dX * dX + dY * dY; } @Override - public String toString() - { + public String toString() { return "2D"; } } diff --git a/core/src/main/java/com/graphhopper/util/DistanceCalc3D.java b/core/src/main/java/com/graphhopper/util/DistanceCalc3D.java index 69948cc11f8..69759b70f91 100644 --- a/core/src/main/java/com/graphhopper/util/DistanceCalc3D.java +++ b/core/src/main/java/com/graphhopper/util/DistanceCalc3D.java @@ -23,25 +23,23 @@ * not so much sense as it is only important for large distances where then the rather smallish * heights would becomes neglectable. *

    + * * @author Peter Karich */ -public class DistanceCalc3D extends DistanceCalcEarth -{ +public class DistanceCalc3D extends DistanceCalcEarth { /** * @param fromHeight in meters above 0 - * @param toHeight in meters above 0 + * @param toHeight in meters above 0 */ - public double calcDist( double fromLat, double fromLon, double fromHeight, - double toLat, double toLon, double toHeight ) - { + public double calcDist(double fromLat, double fromLon, double fromHeight, + double toLat, double toLon, double toHeight) { double len = super.calcDist(fromLat, fromLon, toLat, toLon); double delta = Math.abs(toHeight - fromHeight); return Math.sqrt(delta * delta + len * len); } @Override - public String toString() - { + public String toString() { return "EXACT3D"; } } diff --git a/core/src/main/java/com/graphhopper/util/DistanceCalcEarth.java b/core/src/main/java/com/graphhopper/util/DistanceCalcEarth.java index 362952cc42c..49c1035f525 100644 --- a/core/src/main/java/com/graphhopper/util/DistanceCalcEarth.java +++ b/core/src/main/java/com/graphhopper/util/DistanceCalcEarth.java @@ -25,8 +25,7 @@ /** * @author Peter Karich */ -public class DistanceCalcEarth implements DistanceCalc -{ +public class DistanceCalcEarth implements DistanceCalc { /** * mean radius of the earth */ @@ -48,15 +47,13 @@ public class DistanceCalcEarth implements DistanceCalc * cos(lat1).cos(lat2).sin²(Δlong/2) c = 2.atan2(√a, √(1−a)) d = R.c */ @Override - public double calcDist( double fromLat, double fromLon, double toLat, double toLon ) - { + public double calcDist(double fromLat, double fromLon, double toLat, double toLon) { double normedDist = calcNormalizedDist(fromLat, fromLon, toLat, toLon); return R * 2 * asin(sqrt(normedDist)); } @Override - public double calcDenormalizedDist( double normedDist ) - { + public double calcDenormalizedDist(double normedDist) { return R * 2 * asin(sqrt(normedDist)); } @@ -64,15 +61,13 @@ public double calcDenormalizedDist( double normedDist ) * Returns the specified length in normalized meter. */ @Override - public double calcNormalizedDist( double dist ) - { + public double calcNormalizedDist(double dist) { double tmp = sin(dist / 2 / R); return tmp * tmp; } @Override - public double calcNormalizedDist( double fromLat, double fromLon, double toLat, double toLon ) - { + public double calcNormalizedDist(double fromLat, double fromLon, double toLat, double toLon) { double sinDeltaLat = sin(toRadians(toLat - fromLat) / 2); double sinDeltaLon = sin(toRadians(toLon - fromLon) / 2); return sinDeltaLat * sinDeltaLat @@ -82,19 +77,16 @@ public double calcNormalizedDist( double fromLat, double fromLon, double toLat, /** * Circumference of the earth at different latitudes (breitengrad) */ - public double calcCircumference( double lat ) - { + public double calcCircumference(double lat) { return 2 * PI * R * cos(toRadians(lat)); } - public boolean isDateLineCrossOver( double lon1, double lon2 ) - { + public boolean isDateLineCrossOver(double lon1, double lon2) { return abs(lon1 - lon2) > 180.0; } @Override - public BBox createBBox( double lat, double lon, double radiusInMeter ) - { + public BBox createBBox(double lat, double lon, double radiusInMeter) { if (radiusInMeter <= 0) throw new IllegalArgumentException("Distance must not be zero or negative! " + radiusInMeter + " lat,lon:" + lat + "," + lon); @@ -109,23 +101,22 @@ public BBox createBBox( double lat, double lon, double radiusInMeter ) } @Override - public double calcNormalizedEdgeDistance( double r_lat_deg, double r_lon_deg, - double a_lat_deg, double a_lon_deg, - double b_lat_deg, double b_lon_deg ) - { + public double calcNormalizedEdgeDistance(double r_lat_deg, double r_lon_deg, + double a_lat_deg, double a_lon_deg, + double b_lat_deg, double b_lon_deg) { return calcNormalizedEdgeDistanceNew(r_lat_deg, r_lon_deg, a_lat_deg, a_lon_deg, b_lat_deg, b_lon_deg, false); } /** * New edge distance calculation where no validEdgeDistance check would be necessary *

    + * * @return the normalized distance of the query point "r" to the project point "c" onto the line * segment a-b */ - public double calcNormalizedEdgeDistanceNew( double r_lat_deg, double r_lon_deg, - double a_lat_deg, double a_lon_deg, - double b_lat_deg, double b_lon_deg, boolean reduceToSegment ) - { + public double calcNormalizedEdgeDistanceNew(double r_lat_deg, double r_lon_deg, + double a_lat_deg, double a_lon_deg, + double b_lat_deg, double b_lon_deg, boolean reduceToSegment) { double shrinkFactor = calcShrinkFactor(a_lat_deg, b_lat_deg); double a_lat = a_lat_deg; @@ -152,8 +143,7 @@ public double calcNormalizedEdgeDistanceNew( double r_lat_deg, double r_lon_deg, double factor = ((r_lon - a_lon) * delta_lon + (r_lat - a_lat) * delta_lat) / norm; // make new calculation compatible to old - if (reduceToSegment) - { + if (reduceToSegment) { if (factor > 1) factor = 1; else if (factor < 0) @@ -165,16 +155,14 @@ else if (factor < 0) return calcNormalizedDist(c_lat, c_lon / shrinkFactor, r_lat_deg, r_lon_deg); } - private double calcShrinkFactor( double a_lat_deg, double b_lat_deg ) - { + private double calcShrinkFactor(double a_lat_deg, double b_lat_deg) { return cos(toRadians((a_lat_deg + b_lat_deg) / 2)); } @Override - public GHPoint calcCrossingPointToEdge( double r_lat_deg, double r_lon_deg, - double a_lat_deg, double a_lon_deg, - double b_lat_deg, double b_lon_deg ) - { + public GHPoint calcCrossingPointToEdge(double r_lat_deg, double r_lon_deg, + double a_lat_deg, double a_lon_deg, + double b_lat_deg, double b_lon_deg) { double shrinkFactor = calcShrinkFactor(a_lat_deg, b_lat_deg); double a_lat = a_lat_deg; double a_lon = a_lon_deg * shrinkFactor; @@ -206,10 +194,9 @@ public GHPoint calcCrossingPointToEdge( double r_lat_deg, double r_lon_deg, } @Override - public boolean validEdgeDistance( double r_lat_deg, double r_lon_deg, - double a_lat_deg, double a_lon_deg, - double b_lat_deg, double b_lon_deg ) - { + public boolean validEdgeDistance(double r_lat_deg, double r_lon_deg, + double a_lat_deg, double a_lon_deg, + double b_lat_deg, double b_lon_deg) { double shrinkFactor = calcShrinkFactor(a_lat_deg, b_lat_deg); double a_lat = a_lat_deg; double a_lon = a_lon_deg * shrinkFactor; @@ -238,8 +225,7 @@ public boolean validEdgeDistance( double r_lat_deg, double r_lon_deg, } @Override - public GHPoint projectCoordinate( double latInDeg, double lonInDeg, double distanceInMeter, double headingClockwiseFromNorth ) - { + public GHPoint projectCoordinate(double latInDeg, double lonInDeg, double distanceInMeter, double headingClockwiseFromNorth) { double angularDistance = distanceInMeter / R; double latInRadians = Math.toRadians(latInDeg); @@ -264,14 +250,12 @@ public GHPoint projectCoordinate( double latInDeg, double lonInDeg, double dista } @Override - public boolean isCrossBoundary( double lon1, double lon2 ) - { + public boolean isCrossBoundary(double lon1, double lon2) { return abs(lon1 - lon2) > 300; } @Override - public String toString() - { + public String toString() { return "EXACT"; } } diff --git a/core/src/main/java/com/graphhopper/util/DistancePlaneProjection.java b/core/src/main/java/com/graphhopper/util/DistancePlaneProjection.java index 4679bbbb207..e5b749a32ef 100644 --- a/core/src/main/java/com/graphhopper/util/DistancePlaneProjection.java +++ b/core/src/main/java/com/graphhopper/util/DistancePlaneProjection.java @@ -30,13 +30,12 @@ * http://en.wikipedia.org/wiki/Mercator_projection#Mathematics_of_the_Mercator_projection * http://gis.stackexchange.com/questions/4906/why-is-law-of-cosines-more-preferable-than-haversine-when-calculating-distance-b *

    + * * @author Peter Karich */ -public class DistancePlaneProjection extends DistanceCalcEarth -{ +public class DistancePlaneProjection extends DistanceCalcEarth { @Override - public double calcDist( double fromLat, double fromLon, double toLat, double toLon ) - { + public double calcDist(double fromLat, double fromLon, double toLat, double toLon) { double dLat = toRadians(toLat - fromLat); double dLon = toRadians(toLon - fromLon); // use mean latitude as reference point for delta_lon @@ -46,21 +45,18 @@ public double calcDist( double fromLat, double fromLon, double toLat, double toL } @Override - public double calcDenormalizedDist( double normedDist ) - { + public double calcDenormalizedDist(double normedDist) { return R * sqrt(normedDist); } @Override - public double calcNormalizedDist( double dist ) - { + public double calcNormalizedDist(double dist) { double tmp = dist / R; return tmp * tmp; } @Override - public double calcNormalizedDist( double fromLat, double fromLon, double toLat, double toLon ) - { + public double calcNormalizedDist(double fromLat, double fromLon, double toLat, double toLon) { double dLat = toRadians(toLat - fromLat); double dLon = toRadians(toLon - fromLon); double left = cos(toRadians((fromLat + toLat) / 2)) * dLon; @@ -68,8 +64,7 @@ public double calcNormalizedDist( double fromLat, double fromLon, double toLat, } @Override - public String toString() - { + public String toString() { return "PLANE_PROJ"; } } diff --git a/core/src/main/java/com/graphhopper/util/DouglasPeucker.java b/core/src/main/java/com/graphhopper/util/DouglasPeucker.java index e86eb7ababe..2a8040d8220 100644 --- a/core/src/main/java/com/graphhopper/util/DouglasPeucker.java +++ b/core/src/main/java/com/graphhopper/util/DouglasPeucker.java @@ -23,23 +23,21 @@ *

    * Calling simplify is thread safe. *

    + * * @author Peter Karich */ -public class DouglasPeucker -{ +public class DouglasPeucker { private double normedMaxDist; private DistanceCalc calc; private boolean approx; - public DouglasPeucker() - { + public DouglasPeucker() { setApproximation(true); // 1m setMaxDistance(1); } - public void setApproximation( boolean a ) - { + public void setApproximation(boolean a) { approx = a; if (approx) calc = Helper.DIST_PLANE; @@ -50,8 +48,7 @@ public void setApproximation( boolean a ) /** * maximum distance of discrepancy (from the normal way) in meter */ - public DouglasPeucker setMaxDistance( double dist ) - { + public DouglasPeucker setMaxDistance(double dist) { this.normedMaxDist = calc.calcNormalizedDist(dist); return this; } @@ -59,25 +56,22 @@ public DouglasPeucker setMaxDistance( double dist ) /** * This method removes points which are close to the line (defined by maxDist). *

    + * * @return removed nodes */ - public int simplify( PointList points ) - { + public int simplify(PointList points) { int removed = 0; int size = points.getSize(); - if (approx) - { + if (approx) { int delta = 500; int segments = size / delta + 1; int start = 0; - for (int i = 0; i < segments; i++) - { + for (int i = 0; i < segments; i++) { // start of next is end of last segment, except for the last removed += simplify(points, start, Math.min(size - 1, start + delta)); start += delta; } - } else - { + } else { removed = simplify(points, 0, size - 1); } @@ -88,19 +82,15 @@ public int simplify( PointList points ) /** * compress list: move points into EMPTY slots */ - void compressNew( PointList points, int removed ) - { + void compressNew(PointList points, int removed) { int freeIndex = -1; - for (int currentIndex = 0; currentIndex < points.getSize(); currentIndex++) - { - if (Double.isNaN(points.getLatitude(currentIndex))) - { + for (int currentIndex = 0; currentIndex < points.getSize(); currentIndex++) { + if (Double.isNaN(points.getLatitude(currentIndex))) { if (freeIndex < 0) freeIndex = currentIndex; continue; - } else if (freeIndex < 0) - { + } else if (freeIndex < 0) { continue; } @@ -110,10 +100,8 @@ void compressNew( PointList points, int removed ) int max = currentIndex; int searchIndex = freeIndex + 1; freeIndex = currentIndex; - for (; searchIndex < max; searchIndex++) - { - if (Double.isNaN(points.getLatitude(searchIndex))) - { + for (; searchIndex < max; searchIndex++) { + if (Double.isNaN(points.getLatitude(searchIndex))) { freeIndex = searchIndex; break; } @@ -123,10 +111,8 @@ void compressNew( PointList points, int removed ) } // keep the points of fromIndex and lastIndex - int simplify( PointList points, int fromIndex, int lastIndex ) - { - if (lastIndex - fromIndex < 2) - { + int simplify(PointList points, int fromIndex, int lastIndex) { + if (lastIndex - fromIndex < 2) { return 0; } int indexWithMaxDist = -1; @@ -135,42 +121,34 @@ int simplify( PointList points, int fromIndex, int lastIndex ) double firstLon = points.getLongitude(fromIndex); double lastLat = points.getLatitude(lastIndex); double lastLon = points.getLongitude(lastIndex); - for (int i = fromIndex + 1; i < lastIndex; i++) - { + for (int i = fromIndex + 1; i < lastIndex; i++) { double lat = points.getLatitude(i); - if (Double.isNaN(lat)) - { + if (Double.isNaN(lat)) { continue; } double lon = points.getLongitude(i); double dist = calc.calcNormalizedEdgeDistance(lat, lon, firstLat, firstLon, lastLat, lastLon); - if (maxDist < dist) - { + if (maxDist < dist) { indexWithMaxDist = i; maxDist = dist; } } - if (indexWithMaxDist < 0) - { + if (indexWithMaxDist < 0) { throw new IllegalStateException("maximum not found in [" + fromIndex + "," + lastIndex + "]"); } int counter = 0; - if (maxDist < normedMaxDist) - { - for (int i = fromIndex + 1; i < lastIndex; i++) - { + if (maxDist < normedMaxDist) { + for (int i = fromIndex + 1; i < lastIndex; i++) { points.set(i, Double.NaN, Double.NaN, Double.NaN); counter++; } - } else - { + } else { counter = simplify(points, fromIndex, indexWithMaxDist); counter += simplify(points, indexWithMaxDist, lastIndex); } return counter; } - } diff --git a/core/src/main/java/com/graphhopper/util/Downloader.java b/core/src/main/java/com/graphhopper/util/Downloader.java index 0a9ccb93d61..b64ed245fbb 100644 --- a/core/src/main/java/com/graphhopper/util/Downloader.java +++ b/core/src/main/java/com/graphhopper/util/Downloader.java @@ -27,39 +27,32 @@ /** * @author Peter Karich */ -public class Downloader -{ - public static void main( String[] args ) throws IOException - { - new Downloader("GraphHopper Downloader").downloadAndUnzip("http://graphhopper.com/public/maps/0.1/europe_germany_berlin.ghz", "somefolder", - new ProgressListener() - { - @Override - public void update( long val ) - { - System.out.println("progress:" + val); - } - }); - } - - private String referrer = "http://graphhopper.com"; +public class Downloader { private final String userAgent; + private String referrer = "http://graphhopper.com"; private String acceptEncoding = "gzip, deflate"; private int timeout = 4000; - public Downloader( String userAgent ) - { + public Downloader(String userAgent) { this.userAgent = userAgent; } - public Downloader setTimeout( int timeout ) - { + public static void main(String[] args) throws IOException { + new Downloader("GraphHopper Downloader").downloadAndUnzip("http://graphhopper.com/public/maps/0.1/europe_germany_berlin.ghz", "somefolder", + new ProgressListener() { + @Override + public void update(long val) { + System.out.println("progress:" + val); + } + }); + } + + public Downloader setTimeout(int timeout) { this.timeout = timeout; return this; } - public Downloader setReferrer( String referrer ) - { + public Downloader setReferrer(String referrer) { this.referrer = referrer; return this; } @@ -70,8 +63,7 @@ public Downloader setReferrer( String referrer ) * true otherwise it throws an IOException if an error happens. Furthermore it wraps the stream * to decompress it if the connection content encoding is specified. */ - public InputStream fetch( HttpURLConnection connection, boolean readErrorStreamNoException ) throws IOException - { + public InputStream fetch(HttpURLConnection connection, boolean readErrorStreamNoException) throws IOException { // create connection but before reading get the correct inputstream based on the compression and if error connection.connect(); @@ -85,27 +77,23 @@ public InputStream fetch( HttpURLConnection connection, boolean readErrorStreamN throw new IOException("Stream is null. Message:" + connection.getResponseMessage()); // wrap - try - { + try { String encoding = connection.getContentEncoding(); if (encoding != null && encoding.equalsIgnoreCase("gzip")) is = new GZIPInputStream(is); else if (encoding != null && encoding.equalsIgnoreCase("deflate")) is = new InflaterInputStream(is, new Inflater(true)); - } catch (IOException ex) - { + } catch (IOException ex) { } return is; } - public InputStream fetch( String url ) throws IOException - { + public InputStream fetch(String url) throws IOException { return fetch((HttpURLConnection) createConnection(url), false); } - public HttpURLConnection createConnection( String urlStr ) throws IOException - { + public HttpURLConnection createConnection(String urlStr) throws IOException { URL url = new URL(urlStr); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); // Will yield in a POST request: conn.setDoOutput(true); @@ -121,46 +109,38 @@ public HttpURLConnection createConnection( String urlStr ) throws IOException return conn; } - public void downloadFile( String url, String toFile ) throws IOException - { + public void downloadFile(String url, String toFile) throws IOException { HttpURLConnection conn = createConnection(url); InputStream iStream = fetch(conn, false); int size = 8 * 1024; BufferedOutputStream writer = new BufferedOutputStream(new FileOutputStream(toFile), size); InputStream in = new BufferedInputStream(iStream, size); - try - { + try { byte[] buffer = new byte[size]; int numRead; - while ((numRead = in.read(buffer)) != -1) - { + while ((numRead = in.read(buffer)) != -1) { writer.write(buffer, 0, numRead); } - } finally - { + } finally { Helper.close(writer); Helper.close(in); } } - public void downloadAndUnzip( String url, String toFolder, final ProgressListener progressListener ) throws IOException - { + public void downloadAndUnzip(String url, String toFolder, final ProgressListener progressListener) throws IOException { HttpURLConnection conn = createConnection(url); final int length = conn.getContentLength(); InputStream iStream = fetch(conn, false); - new Unzipper().unzip(iStream, new File(toFolder), new ProgressListener() - { + new Unzipper().unzip(iStream, new File(toFolder), new ProgressListener() { @Override - public void update( long sumBytes ) - { + public void update(long sumBytes) { progressListener.update((int) (100 * sumBytes / length)); } }); } - public String downloadAsString( String url, boolean readErrorStreamNoException ) throws IOException - { + public String downloadAsString(String url, boolean readErrorStreamNoException) throws IOException { return Helper.isToString(fetch((HttpURLConnection) createConnection(url), readErrorStreamNoException)); } } diff --git a/core/src/main/java/com/graphhopper/util/EdgeExplorer.java b/core/src/main/java/com/graphhopper/util/EdgeExplorer.java index dc17907bb19..cba67104590 100644 --- a/core/src/main/java/com/graphhopper/util/EdgeExplorer.java +++ b/core/src/main/java/com/graphhopper/util/EdgeExplorer.java @@ -21,18 +21,19 @@ * Class to get an EdgeIterator. Create it via graph.createEdgeExplorer() use one instance per * thread. *

    + * * @author Peter Karich * @see EdgeIterator * @see EdgeIteratorState */ -public interface EdgeExplorer -{ +public interface EdgeExplorer { /** * This method sets the base node for iteration through neighboring edges (EdgeIteratorStates). *

    + * * @return EdgeIterator around the specified baseNode. The resulting iterator can be a new * instance or a reused instance returned in a previous call. So be sure you do not use the * EdgeExplorer from multiple threads or in a nested loop. */ - EdgeIterator setBaseNode( int baseNode ); + EdgeIterator setBaseNode(int baseNode); } diff --git a/core/src/main/java/com/graphhopper/util/EdgeIterator.java b/core/src/main/java/com/graphhopper/util/EdgeIterator.java index adce7d73292..6c689bfaeda 100644 --- a/core/src/main/java/com/graphhopper/util/EdgeIterator.java +++ b/core/src/main/java/com/graphhopper/util/EdgeIterator.java @@ -33,29 +33,28 @@ * } * *

    + * * @author Peter Karich * @see EdgeIteratorState * @see EdgeExplorer */ -public interface EdgeIterator extends EdgeIteratorState -{ +public interface EdgeIterator extends EdgeIteratorState { + /** + * integer value to indicate if an edge is valid or not which then would be initialized with + * this value + */ + int NO_EDGE = -1; + /** * To be called to go to the next edge state. *

    + * * @return true if an edge state is available */ boolean next(); - /** - * integer value to indicate if an edge is valid or not which then would be initialized with - * this value - */ - int NO_EDGE = -1; - - class Edge - { - public static boolean isValid( int edgeId ) - { + class Edge { + public static boolean isValid(int edgeId) { return edgeId > NO_EDGE; } } diff --git a/core/src/main/java/com/graphhopper/util/EdgeIteratorState.java b/core/src/main/java/com/graphhopper/util/EdgeIteratorState.java index 3e847034ddb..3137e55f3a3 100644 --- a/core/src/main/java/com/graphhopper/util/EdgeIteratorState.java +++ b/core/src/main/java/com/graphhopper/util/EdgeIteratorState.java @@ -22,12 +22,12 @@ /** * This interface represents an edge and is one possible state of an EdgeIterator. *

    + * * @author Peter Karich * @see EdgeIterator * @see EdgeExplorer */ -public interface EdgeIteratorState -{ +public interface EdgeIteratorState { int K_UNFAVORED_EDGE = -1; /** @@ -41,6 +41,7 @@ public interface EdgeIteratorState * graph.getEdges(baseNode)". Often only used for convenience reasons. Do not confuse this with * a source node of a directed edge. *

    + * * @return the requested node itself * @see EdgeIterator */ @@ -59,75 +60,79 @@ public interface EdgeIteratorState * (docs/core/low-level-api.md#what-are-pillar-and-tower-nodes). Updates to the returned list * are not reflected in the graph, for that you've to use setWayGeometry. *

    + * * @param mode can be

    • 0 = only pillar nodes, no tower nodes
    • 1 = inclusive the - * base tower node only
    • 2 = inclusive the adjacent tower node only
    • 3 = - * inclusive the base and adjacent tower node
    + * base tower node only
  • 2 = inclusive the adjacent tower node only
  • 3 = + * inclusive the base and adjacent tower node
  • * @return pillar nodes */ - PointList fetchWayGeometry( int mode ); + PointList fetchWayGeometry(int mode); /** * @param list is a sorted collection of nodes between the baseNode and the current adjacent - * node. Specify the list without the adjacent and base nodes. + * node. Specify the list without the adjacent and base nodes. */ - EdgeIteratorState setWayGeometry( PointList list ); + EdgeIteratorState setWayGeometry(PointList list); /** * @return the distance of the current edge in meter */ double getDistance(); - EdgeIteratorState setDistance( double dist ); + EdgeIteratorState setDistance(double dist); long getFlags(); - EdgeIteratorState setFlags( long flags ); + EdgeIteratorState setFlags(long flags); /** * @return the additional field value for this edge */ int getAdditionalField(); + /** + * Updates the additional field value for this edge + */ + EdgeIteratorState setAdditionalField(int value); + /** * @see FlagEncoder#isForward(long) and #472 */ - boolean isForward( FlagEncoder encoder ); + boolean isForward(FlagEncoder encoder); /** * @see FlagEncoder#isBackward(long) and #472 */ - boolean isBackward( FlagEncoder encoder ); + boolean isBackward(FlagEncoder encoder); /** * Get additional boolean information of the edge. *

    - * @param key direction or vehicle dependent integer key + * + * @param key direction or vehicle dependent integer key * @param _default default value if key is not found */ - boolean getBool( int key, boolean _default ); - - /** - * Updates the additional field value for this edge - */ - EdgeIteratorState setAdditionalField( int value ); + boolean getBool(int key, boolean _default); String getName(); - EdgeIteratorState setName( String name ); + EdgeIteratorState setName(String name); /** * Clones this EdgeIteratorState. *

    + * * @param reverse if true a detached edgeState with reversed properties is created where base - * and adjacent nodes, flags and wayGeometry are in reversed order. See #162 for more details - * about why we need the new reverse parameter. + * and adjacent nodes, flags and wayGeometry are in reversed order. See #162 for more details + * about why we need the new reverse parameter. */ - EdgeIteratorState detach( boolean reverse ); + EdgeIteratorState detach(boolean reverse); /** * Copies the properties of this edge into the specified edge. Does not change nodes! *

    + * * @return the specified edge e */ - EdgeIteratorState copyPropertiesTo( EdgeIteratorState e ); + EdgeIteratorState copyPropertiesTo(EdgeIteratorState e); } diff --git a/core/src/main/java/com/graphhopper/util/FinishInstruction.java b/core/src/main/java/com/graphhopper/util/FinishInstruction.java index c5d1f4ad7af..3861b3a80e0 100644 --- a/core/src/main/java/com/graphhopper/util/FinishInstruction.java +++ b/core/src/main/java/com/graphhopper/util/FinishInstruction.java @@ -20,43 +20,35 @@ /** * @author Peter Karich */ -public class FinishInstruction extends Instruction -{ - public FinishInstruction( String name, final double lat, final double lon, final double ele ) - { - super(FINISH, name, InstructionAnnotation.EMPTY, new PointList(2, true) - { +public class FinishInstruction extends Instruction { + public FinishInstruction(String name, final double lat, final double lon, final double ele) { + super(FINISH, name, InstructionAnnotation.EMPTY, new PointList(2, true) { { add(lat, lon, ele); } }); } - public FinishInstruction(final double lat, final double lon, final double ele ) - { - super(FINISH, "", InstructionAnnotation.EMPTY, new PointList(2, true) - { + public FinishInstruction(final double lat, final double lon, final double ele) { + super(FINISH, "", InstructionAnnotation.EMPTY, new PointList(2, true) { { add(lat, lon, ele); } }); } - public FinishInstruction(String name, PointAccess pointAccess, int node ) - { + public FinishInstruction(String name, PointAccess pointAccess, int node) { this(name, pointAccess.getLatitude(node), pointAccess.getLongitude(node), pointAccess.is3D() ? pointAccess.getElevation(node) : 0); } - public FinishInstruction(PointAccess pointAccess, int node ) - { + public FinishInstruction(PointAccess pointAccess, int node) { this(pointAccess.getLatitude(node), pointAccess.getLongitude(node), pointAccess.is3D() ? pointAccess.getElevation(node) : 0); } @Override - public String getTurnDescription( Translation tr ) - { + public String getTurnDescription(Translation tr) { if (rawName) return getName(); diff --git a/core/src/main/java/com/graphhopper/util/GHUtility.java b/core/src/main/java/com/graphhopper/util/GHUtility.java index 1039e92b2c3..743757d752a 100644 --- a/core/src/main/java/com/graphhopper/util/GHUtility.java +++ b/core/src/main/java/com/graphhopper/util/GHUtility.java @@ -19,8 +19,8 @@ import com.graphhopper.coll.GHBitSet; import com.graphhopper.coll.GHBitSetImpl; -import com.graphhopper.routing.util.AllEdgesIterator; import com.graphhopper.routing.util.AllCHEdgesIterator; +import com.graphhopper.routing.util.AllEdgesIterator; import com.graphhopper.routing.util.EdgeFilter; import com.graphhopper.routing.util.FlagEncoder; import com.graphhopper.storage.*; @@ -34,24 +34,21 @@ * A helper class to avoid cluttering the Graph interface with all the common methods. Most of the * methods are useful for unit tests or debugging only. *

    + * * @author Peter Karich */ -public class GHUtility -{ +public class GHUtility { /** * This method could throw exception if uncatched problems like index out of bounds etc */ - public static List getProblems( Graph g ) - { + public static List getProblems(Graph g) { List problems = new ArrayList(); int nodes = g.getNodes(); int nodeIndex = 0; NodeAccess na = g.getNodeAccess(); - try - { + try { EdgeExplorer explorer = g.createEdgeExplorer(); - for (; nodeIndex < nodes; nodeIndex++) - { + for (; nodeIndex < nodes; nodeIndex++) { double lat = na.getLatitude(nodeIndex); if (lat > 90 || lat < -90) problems.add("latitude is not within its bounds " + lat); @@ -61,20 +58,16 @@ public static List getProblems( Graph g ) problems.add("longitude is not within its bounds " + lon); EdgeIterator iter = explorer.setBaseNode(nodeIndex); - while (iter.next()) - { - if (iter.getAdjNode() >= nodes) - { + while (iter.next()) { + if (iter.getAdjNode() >= nodes) { problems.add("edge of " + nodeIndex + " has a node " + iter.getAdjNode() + " greater or equal to getNodes"); } - if (iter.getAdjNode() < 0) - { + if (iter.getAdjNode() < 0) { problems.add("edge of " + nodeIndex + " has a negative node " + iter.getAdjNode()); } } } - } catch (Exception ex) - { + } catch (Exception ex) { throw new RuntimeException("problem with node " + nodeIndex, ex); } @@ -87,56 +80,45 @@ public static List getProblems( Graph g ) /** * Counts reachable edges. */ - public static int count( EdgeIterator iter ) - { + public static int count(EdgeIterator iter) { int counter = 0; - while (iter.next()) - { + while (iter.next()) { counter++; } return counter; } - public static Set asSet( int... values ) - { + public static Set asSet(int... values) { Set s = new HashSet(); - for (int v : values) - { + for (int v : values) { s.add(v); } return s; } - public static Set getNeighbors( EdgeIterator iter ) - { + public static Set getNeighbors(EdgeIterator iter) { // make iteration order over set static => linked Set list = new LinkedHashSet(); - while (iter.next()) - { + while (iter.next()) { list.add(iter.getAdjNode()); } return list; } - public static List getEdgeIds( EdgeIterator iter ) - { + public static List getEdgeIds(EdgeIterator iter) { List list = new ArrayList(); - while (iter.next()) - { + while (iter.next()) { list.add(iter.getEdge()); } return list; } - public static void printEdgeInfo( final Graph g, FlagEncoder encoder ) - { + public static void printEdgeInfo(final Graph g, FlagEncoder encoder) { System.out.println("-- Graph n:" + g.getNodes() + " e:" + g.getAllEdges().getMaxId() + " ---"); AllEdgesIterator iter = g.getAllEdges(); - while (iter.next()) - { + while (iter.next()) { String sc = ""; - if (iter instanceof AllCHEdgesIterator) - { + if (iter instanceof AllCHEdgesIterator) { AllCHEdgesIterator aeSkip = (AllCHEdgesIterator) iter; sc = aeSkip.isShortcut() ? "sc" : " "; } @@ -146,18 +128,14 @@ public static void printEdgeInfo( final Graph g, FlagEncoder encoder ) } } - public static void printInfo( final Graph g, int startNode, final int counts, final EdgeFilter filter ) - { - new BreadthFirstSearch() - { + public static void printInfo(final Graph g, int startNode, final int counts, final EdgeFilter filter) { + new BreadthFirstSearch() { int counter = 0; @Override - protected boolean goFurther( int nodeId ) - { + protected boolean goFurther(int nodeId) { System.out.println(getNodeInfo(g, nodeId, filter)); - if (counter++ > counts) - { + if (counter++ > counts) { return false; } return true; @@ -165,27 +143,23 @@ protected boolean goFurther( int nodeId ) }.start(g.createEdgeExplorer(), startNode); } - public static String getNodeInfo( CHGraph g, int nodeId, EdgeFilter filter ) - { + public static String getNodeInfo(CHGraph g, int nodeId, EdgeFilter filter) { CHEdgeExplorer ex = g.createEdgeExplorer(filter); CHEdgeIterator iter = ex.setBaseNode(nodeId); NodeAccess na = g.getNodeAccess(); String str = nodeId + ":" + na.getLatitude(nodeId) + "," + na.getLongitude(nodeId) + "\n"; - while (iter.next()) - { + while (iter.next()) { str += " ->" + iter.getAdjNode() + "(" + iter.getSkippedEdge1() + "," + iter.getSkippedEdge2() + ") " + iter.getEdge() + " \t" + BitUtil.BIG.toBitString(iter.getFlags(), 8) + "\n"; } return str; } - public static String getNodeInfo( Graph g, int nodeId, EdgeFilter filter ) - { + public static String getNodeInfo(Graph g, int nodeId, EdgeFilter filter) { EdgeIterator iter = g.createEdgeExplorer(filter).setBaseNode(nodeId); NodeAccess na = g.getNodeAccess(); String str = nodeId + ":" + na.getLatitude(nodeId) + "," + na.getLongitude(nodeId) + "\n"; - while (iter.next()) - { + while (iter.next()) { str += " ->" + iter.getAdjNode() + " (" + iter.getDistance() + ") pillars:" + iter.fetchWayGeometry(0).getSize() + ", edgeId:" + iter.getEdge() + "\t" + BitUtil.BIG.toBitString(iter.getFlags(), 8) + "\n"; @@ -193,13 +167,11 @@ public static String getNodeInfo( Graph g, int nodeId, EdgeFilter filter ) return str; } - public static Graph shuffle( Graph g, Graph sortedGraph ) - { + public static Graph shuffle(Graph g, Graph sortedGraph) { int len = g.getNodes(); TIntList list = new TIntArrayList(len, -1); list.fill(0, len, -1); - for (int i = 0; i < len; i++) - { + for (int i = 0; i < len; i++) { list.set(i, i); } list.shuffle(new Random()); @@ -210,8 +182,7 @@ public static Graph shuffle( Graph g, Graph sortedGraph ) * Sorts the graph according to depth-first search traversal. Other traversals have either no * significant difference (bfs) for querying or are worse (z-curve). */ - public static Graph sortDFS( Graph g, Graph sortedGraph ) - { + public static Graph sortDFS(Graph g, Graph sortedGraph) { final TIntList list = new TIntArrayList(g.getNodes(), -1); int nodes = g.getNodes(); list.fill(0, nodes, -1); @@ -219,19 +190,15 @@ public static Graph sortDFS( Graph g, Graph sortedGraph ) final AtomicInteger ref = new AtomicInteger(-1); EdgeExplorer explorer = g.createEdgeExplorer(); for (int startNode = 0; startNode >= 0 && startNode < nodes; - startNode = bitset.nextClear(startNode + 1)) - { - new DepthFirstSearch() - { + startNode = bitset.nextClear(startNode + 1)) { + new DepthFirstSearch() { @Override - protected GHBitSet createBitSet() - { + protected GHBitSet createBitSet() { return bitset; } @Override - protected boolean goFurther( int nodeId ) - { + protected boolean goFurther(int nodeId) { list.set(nodeId, ref.incrementAndGet()); return super.goFurther(nodeId); } @@ -240,11 +207,9 @@ protected boolean goFurther( int nodeId ) return createSortedGraph(g, sortedGraph, list); } - static Graph createSortedGraph( Graph fromGraph, Graph toSortedGraph, final TIntList oldToNewNodeList ) - { + static Graph createSortedGraph(Graph fromGraph, Graph toSortedGraph, final TIntList oldToNewNodeList) { AllEdgesIterator eIter = fromGraph.getAllEdges(); - while (eIter.next()) - { + while (eIter.next()) { int base = eIter.getBaseNode(); int newBaseIndex = oldToNewNodeList.get(base); int adj = eIter.getAdjNode(); @@ -260,8 +225,7 @@ static Graph createSortedGraph( Graph fromGraph, Graph toSortedGraph, final TInt int nodes = fromGraph.getNodes(); NodeAccess na = fromGraph.getNodeAccess(); NodeAccess sna = toSortedGraph.getNodeAccess(); - for (int old = 0; old < nodes; old++) - { + for (int old = 0; old < nodes; old++) { int newIndex = oldToNewNodeList.get(old); if (sna.is3D()) sna.setNode(newIndex, na.getLatitude(old), na.getLongitude(old), na.getElevation(old)); @@ -275,11 +239,9 @@ static Graph createSortedGraph( Graph fromGraph, Graph toSortedGraph, final TInt * @return the specified toGraph which is now filled with data from fromGraph */ // TODO very similar to createSortedGraph -> use a 'int map(int)' interface - public static Graph copyTo( Graph fromGraph, Graph toGraph ) - { + public static Graph copyTo(Graph fromGraph, Graph toGraph) { AllEdgesIterator eIter = fromGraph.getAllEdges(); - while (eIter.next()) - { + while (eIter.next()) { int base = eIter.getBaseNode(); int adj = eIter.getAdjNode(); eIter.copyPropertiesTo(toGraph.edge(base, adj)); @@ -288,8 +250,7 @@ public static Graph copyTo( Graph fromGraph, Graph toGraph ) NodeAccess fna = fromGraph.getNodeAccess(); NodeAccess tna = toGraph.getNodeAccess(); int nodes = fromGraph.getNodes(); - for (int node = 0; node < nodes; node++) - { + for (int node = 0; node < nodes; node++) { if (tna.is3D()) tna.setNode(node, fna.getLatitude(node), fna.getLongitude(node), fna.getElevation(node)); else @@ -298,15 +259,12 @@ public static Graph copyTo( Graph fromGraph, Graph toGraph ) return toGraph; } - static Directory guessDirectory( GraphStorage store ) - { + static Directory guessDirectory(GraphStorage store) { String location = store.getDirectory().getLocation(); Directory outdir; - if (store.getDirectory() instanceof MMapDirectory) - { + if (store.getDirectory() instanceof MMapDirectory) { throw new IllegalStateException("not supported yet: mmap will overwrite existing storage at the same location"); - } else - { + } else { boolean isStoring = ((GHDirectory) store.getDirectory()).isStoring(); outdir = new RAMDirectory(location, isStoring); } @@ -316,8 +274,7 @@ static Directory guessDirectory( GraphStorage store ) /** * Create a new storage from the specified one without copying the data. */ - public static GraphHopperStorage newStorage( GraphHopperStorage store ) - { + public static GraphHopperStorage newStorage(GraphHopperStorage store) { Directory outdir = guessDirectory(store); boolean is3D = store.getNodeAccess().is3D(); @@ -326,252 +283,214 @@ public static GraphHopperStorage newStorage( GraphHopperStorage store ) create(store.getNodes()); } - public static int getAdjNode( Graph g, int edge, int adjNode ) - { - if (EdgeIterator.Edge.isValid(edge)) - { + public static int getAdjNode(Graph g, int edge, int adjNode) { + if (EdgeIterator.Edge.isValid(edge)) { EdgeIteratorState iterTo = g.getEdgeIteratorState(edge, adjNode); return iterTo.getAdjNode(); } return adjNode; } + public static EdgeIteratorState createMockedEdgeIteratorState(final double distance, final long flags) { + return new GHUtility.DisabledEdgeIterator() { + @Override + public double getDistance() { + return distance; + } + + @Override + public long getFlags() { + return flags; + } + + @Override + public boolean getBool(int key, boolean _default) { + return _default; + } + }; + } + + ; + + /** + * @return the first edge containing the specified nodes base and adj. Returns null if + * not found. + */ + public static EdgeIteratorState getEdge(Graph graph, int base, int adj) { + EdgeIterator iter = graph.createEdgeExplorer().setBaseNode(base); + while (iter.next()) { + if (iter.getAdjNode() == adj) + return iter; + } + return null; + } + + /** + * Creates unique positive number for specified edgeId taking into account the direction defined + * by nodeA, nodeB and reverse. + */ + public static int createEdgeKey(int nodeA, int nodeB, int edgeId, boolean reverse) { + edgeId = edgeId << 1; + if (reverse) + return (nodeA > nodeB) ? edgeId : edgeId + 1; + return (nodeA > nodeB) ? edgeId + 1 : edgeId; + } + + /** + * Returns if the specified edgeKeys (created by createEdgeKey) are identical regardless of the + * direction. + */ + public static boolean isSameEdgeKeys(int edgeKey1, int edgeKey2) { + return edgeKey1 / 2 == edgeKey2 / 2; + } + + /** + * Returns the edgeKey of the opposite direction + */ + public static int reverseEdgeKey(int edgeKey) { + return edgeKey % 2 == 0 ? edgeKey + 1 : edgeKey - 1; + } + + /** + * @return edge ID for edgeKey + */ + public static int getEdgeFromEdgeKey(int edgeKey) { + return edgeKey / 2; + } + /** * This edge iterator can be used in tests to mock specific iterator behaviour via overloading * certain methods. */ - public static class DisabledEdgeIterator implements CHEdgeIterator - { + public static class DisabledEdgeIterator implements CHEdgeIterator { @Override - public EdgeIterator detach( boolean reverse ) - { + public EdgeIterator detach(boolean reverse) { throw new UnsupportedOperationException("Not supported. Edge is empty."); } @Override - public EdgeIteratorState setDistance( double dist ) - { + public EdgeIteratorState setDistance(double dist) { throw new UnsupportedOperationException("Not supported. Edge is empty."); } @Override - public EdgeIteratorState setFlags( long flags ) - { + public EdgeIteratorState setFlags(long flags) { throw new UnsupportedOperationException("Not supported. Edge is empty."); } @Override - public boolean next() - { + public boolean next() { throw new UnsupportedOperationException("Not supported. Edge is empty."); } @Override - public int getEdge() - { + public int getEdge() { throw new UnsupportedOperationException("Not supported. Edge is empty."); } @Override - public int getBaseNode() - { + public int getBaseNode() { throw new UnsupportedOperationException("Not supported. Edge is empty."); } @Override - public int getAdjNode() - { + public int getAdjNode() { throw new UnsupportedOperationException("Not supported. Edge is empty."); } @Override - public double getDistance() - { + public double getDistance() { throw new UnsupportedOperationException("Not supported. Edge is empty."); } @Override - public long getFlags() - { + public long getFlags() { throw new UnsupportedOperationException("Not supported. Edge is empty."); } @Override - public PointList fetchWayGeometry( int type ) - { + public PointList fetchWayGeometry(int type) { throw new UnsupportedOperationException("Not supported. Edge is empty."); } @Override - public EdgeIteratorState setWayGeometry( PointList list ) - { + public EdgeIteratorState setWayGeometry(PointList list) { throw new UnsupportedOperationException("Not supported. Edge is empty."); } @Override - public String getName() - { + public String getName() { throw new UnsupportedOperationException("Not supported. Edge is empty."); } @Override - public EdgeIteratorState setName( String name ) - { + public EdgeIteratorState setName(String name) { throw new UnsupportedOperationException("Not supported. Edge is empty."); } @Override - public boolean getBool( int key, boolean _default ) - { + public boolean getBool(int key, boolean _default) { throw new UnsupportedOperationException("Not supported. Edge is empty."); } @Override - public boolean isBackward( FlagEncoder encoder ) - { + public boolean isBackward(FlagEncoder encoder) { throw new UnsupportedOperationException("Not supported. Edge is empty."); } @Override - public boolean isForward( FlagEncoder encoder ) - { + public boolean isForward(FlagEncoder encoder) { throw new UnsupportedOperationException("Not supported. Edge is empty."); } @Override - public int getAdditionalField() - { + public int getAdditionalField() { throw new UnsupportedOperationException("Not supported. Edge is empty."); } @Override - public EdgeIteratorState setAdditionalField( int value ) - { + public EdgeIteratorState setAdditionalField(int value) { throw new UnsupportedOperationException("Not supported. Edge is empty."); } @Override - public EdgeIteratorState copyPropertiesTo( EdgeIteratorState edge ) - { + public EdgeIteratorState copyPropertiesTo(EdgeIteratorState edge) { throw new UnsupportedOperationException("Not supported. Edge is empty."); } @Override - public boolean isShortcut() - { + public boolean isShortcut() { return false; } @Override - public int getSkippedEdge1() - { + public int getSkippedEdge1() { throw new UnsupportedOperationException("Not supported. Edge is empty."); } @Override - public int getSkippedEdge2() - { + public int getSkippedEdge2() { throw new UnsupportedOperationException("Not supported. Edge is empty."); } @Override - public void setSkippedEdges( int edge1, int edge2 ) - { + public void setSkippedEdges(int edge1, int edge2) { throw new UnsupportedOperationException("Not supported. Edge is empty."); } @Override - public double getWeight() - { + public double getWeight() { throw new UnsupportedOperationException("Not supported. Edge is empty."); } @Override - public CHEdgeIteratorState setWeight( double weight ) - { + public CHEdgeIteratorState setWeight(double weight) { throw new UnsupportedOperationException("Not supported. Edge is empty."); } @Override - public boolean canBeOverwritten( long flags ) - { + public boolean canBeOverwritten(long flags) { throw new UnsupportedOperationException("Not supported. Edge is empty."); } - }; - - public static EdgeIteratorState createMockedEdgeIteratorState( final double distance, final long flags ) - { - return new GHUtility.DisabledEdgeIterator() - { - @Override - public double getDistance() - { - return distance; - } - - @Override - public long getFlags() - { - return flags; - } - - @Override - public boolean getBool( int key, boolean _default ) - { - return _default; - } - }; - } - - /** - * @return the first edge containing the specified nodes base and adj. Returns null if - * not found. - */ - public static EdgeIteratorState getEdge( Graph graph, int base, int adj ) - { - EdgeIterator iter = graph.createEdgeExplorer().setBaseNode(base); - while (iter.next()) - { - if (iter.getAdjNode() == adj) - return iter; - } - return null; - } - - /** - * Creates unique positive number for specified edgeId taking into account the direction defined - * by nodeA, nodeB and reverse. - */ - public static int createEdgeKey( int nodeA, int nodeB, int edgeId, boolean reverse ) - { - edgeId = edgeId << 1; - if (reverse) - return (nodeA > nodeB) ? edgeId : edgeId + 1; - return (nodeA > nodeB) ? edgeId + 1 : edgeId; - } - - /** - * Returns if the specified edgeKeys (created by createEdgeKey) are identical regardless of the - * direction. - */ - public static boolean isSameEdgeKeys( int edgeKey1, int edgeKey2 ) - { - return edgeKey1 / 2 == edgeKey2 / 2; - } - - /** - * Returns the edgeKey of the opposite direction - */ - public static int reverseEdgeKey( int edgeKey ) - { - return edgeKey % 2 == 0 ? edgeKey + 1 : edgeKey - 1; - } - - /** - * @return edge ID for edgeKey - */ - public static int getEdgeFromEdgeKey( int edgeKey ) - { - return edgeKey / 2; } } diff --git a/core/src/main/java/com/graphhopper/util/GPXEntry.java b/core/src/main/java/com/graphhopper/util/GPXEntry.java index bdd3009300d..1a921245d9d 100644 --- a/core/src/main/java/com/graphhopper/util/GPXEntry.java +++ b/core/src/main/java/com/graphhopper/util/GPXEntry.java @@ -23,54 +23,45 @@ /** * @author Peter Karich */ -public class GPXEntry extends GHPoint3D -{ +public class GPXEntry extends GHPoint3D { private long time; - public GPXEntry( GHPoint p, long millis ) - { + public GPXEntry(GHPoint p, long millis) { this(p.lat, p.lon, millis); } - public GPXEntry( double lat, double lon, long millis ) - { + public GPXEntry(double lat, double lon, long millis) { super(lat, lon, Double.NaN); this.time = millis; } - public GPXEntry( double lat, double lon, double ele, long millis ) - { + public GPXEntry(double lat, double lon, double ele, long millis) { super(lat, lon, ele); this.time = millis; } - boolean is3D() - { + boolean is3D() { return !Double.isNaN(ele); } /** * The time relative to the start time in milli seconds. */ - public long getTime() - { + public long getTime() { return time; } - public void setTime( long time ) - { + public void setTime(long time) { this.time = time; } @Override - public int hashCode() - { + public int hashCode() { return 59 * super.hashCode() + (int) (time ^ (time >>> 32)); } @Override - public boolean equals( Object obj ) - { + public boolean equals(Object obj) { if (obj == null) return false; @@ -79,8 +70,7 @@ public boolean equals( Object obj ) } @Override - public String toString() - { + public String toString() { return super.toString() + ", " + time; } } diff --git a/core/src/main/java/com/graphhopper/util/Helper.java b/core/src/main/java/com/graphhopper/util/Helper.java index 290555d8621..5587a0f2caa 100644 --- a/core/src/main/java/com/graphhopper/util/Helper.java +++ b/core/src/main/java/com/graphhopper/util/Helper.java @@ -20,6 +20,8 @@ import com.graphhopper.util.shapes.BBox; import gnu.trove.list.TIntList; import gnu.trove.list.array.TIntArrayList; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.io.*; import java.lang.reflect.Method; @@ -35,59 +37,57 @@ import java.util.*; import java.util.Map.Entry; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - /** * Several utility classes which are compatible with Java6 on Android. *

    + * * @author Peter Karich * @see Helper7 for none-Android compatible methods. */ -public class Helper -{ +public class Helper { public static final DistanceCalc DIST_EARTH = new DistanceCalcEarth(); public static final DistanceCalc3D DIST_3D = new DistanceCalc3D(); public static final DistancePlaneProjection DIST_PLANE = new DistancePlaneProjection(); public static final AngleCalc ANGLE_CALC = new AngleCalc(); - private static final Logger LOGGER = LoggerFactory.getLogger(Helper.class); public static final Charset UTF_CS = Charset.forName("UTF-8"); public static final TimeZone UTC = TimeZone.getTimeZone("UTC"); public static final long MB = 1L << 20; + private static final Logger LOGGER = LoggerFactory.getLogger(Helper.class); + // +- 180 and +-90 => let use use 400 + private static final float DEGREE_FACTOR = Integer.MAX_VALUE / 400f; + // milli meter is a bit extreme but we have integers + private static final float ELE_FACTOR = 1000f; - public static ArrayList tIntListToArrayList( TIntList from ) - { + private Helper() { + } + + public static ArrayList tIntListToArrayList(TIntList from) { int len = from.size(); ArrayList list = new ArrayList(len); - for (int i = 0; i < len; i++) - { + for (int i = 0; i < len; i++) { list.add(from.get(i)); } return list; } - public static Locale getLocale( String param ) - { + public static Locale getLocale(String param) { int pointIndex = param.indexOf('.'); if (pointIndex > 0) param = param.substring(0, pointIndex); param = param.replace("-", "_"); int index = param.indexOf("_"); - if (index < 0) - { + if (index < 0) { return new Locale(param); } return new Locale(param.substring(0, index), param.substring(index + 1)); } - static String packageToPath( Package pkg ) - { + static String packageToPath(Package pkg) { return pkg.getName().replaceAll("\\.", File.separator); } - public static int countBitValue( int maxTurnCosts ) - { + public static int countBitValue(int maxTurnCosts) { double val = Math.log(maxTurnCosts) / Math.log(2); int intVal = (int) val; if (val == intVal) @@ -95,31 +95,21 @@ public static int countBitValue( int maxTurnCosts ) return intVal + 1; } - private Helper() - { - } - - public static void loadProperties( Map map, Reader tmpReader ) throws IOException - { + public static void loadProperties(Map map, Reader tmpReader) throws IOException { BufferedReader reader = new BufferedReader(tmpReader); String line; - try - { - while ((line = reader.readLine()) != null) - { - if (line.startsWith("//") || line.startsWith("#")) - { + try { + while ((line = reader.readLine()) != null) { + if (line.startsWith("//") || line.startsWith("#")) { continue; } - if (Helper.isEmpty(line)) - { + if (Helper.isEmpty(line)) { continue; } int index = line.indexOf("="); - if (index < 0) - { + if (index < 0) { LOGGER.warn("Skipping configuration at line:" + line); continue; } @@ -128,102 +118,80 @@ public static void loadProperties( Map map, Reader tmpReader ) t String value = line.substring(index + 1); map.put(field.trim(), value.trim()); } - } finally - { + } finally { reader.close(); } } - public static void saveProperties( Map map, Writer tmpWriter ) throws IOException - { + public static void saveProperties(Map map, Writer tmpWriter) throws IOException { BufferedWriter writer = new BufferedWriter(tmpWriter); - try - { - for (Entry e : map.entrySet()) - { + try { + for (Entry e : map.entrySet()) { writer.append(e.getKey()); writer.append('='); writer.append(e.getValue()); writer.append('\n'); } - } finally - { + } finally { writer.close(); } } - public static List readFile( String file ) throws IOException - { + public static List readFile(String file) throws IOException { return readFile(new InputStreamReader(new FileInputStream(file), UTF_CS)); } - public static List readFile( Reader simpleReader ) throws IOException - { + public static List readFile(Reader simpleReader) throws IOException { BufferedReader reader = new BufferedReader(simpleReader); - try - { + try { List res = new ArrayList(); String line; - while ((line = reader.readLine()) != null) - { + while ((line = reader.readLine()) != null) { res.add(line); } return res; - } finally - { + } finally { reader.close(); } } - public static String isToString( InputStream inputStream ) throws IOException - { + public static String isToString(InputStream inputStream) throws IOException { int size = 1024 * 8; String encoding = "UTF-8"; InputStream in = new BufferedInputStream(inputStream, size); - try - { + try { byte[] buffer = new byte[size]; ByteArrayOutputStream output = new ByteArrayOutputStream(); int numRead; - while ((numRead = in.read(buffer)) != -1) - { + while ((numRead = in.read(buffer)) != -1) { output.write(buffer, 0, numRead); } return output.toString(encoding); - } finally - { + } finally { in.close(); } } - public static int idealIntArraySize( int need ) - { + public static int idealIntArraySize(int need) { return idealByteArraySize(need * 4) / 4; } - public static int idealByteArraySize( int need ) - { - for (int i = 4; i < 32; i++) - { - if (need <= (1 << i) - 12) - { + public static int idealByteArraySize(int need) { + for (int i = 4; i < 32; i++) { + if (need <= (1 << i) - 12) { return (1 << i) - 12; } } return need; } - public static boolean removeDir( File file ) - { - if (!file.exists()) - { + public static boolean removeDir(File file) { + if (!file.exists()) { return true; } - if (file.isDirectory()) - { - for (File f : file.listFiles()) - { + if (file.isDirectory()) { + for (File f : file.listFiles()) { removeDir(f); } } @@ -231,76 +199,61 @@ public static boolean removeDir( File file ) return file.delete(); } - public static long getTotalMB() - { + public static long getTotalMB() { return Runtime.getRuntime().totalMemory() / MB; } - public static long getUsedMB() - { + public static long getUsedMB() { return (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / MB; } - public static String getMemInfo() - { + public static String getMemInfo() { return "totalMB:" + getTotalMB() + ", usedMB:" + getUsedMB(); } - public static int getSizeOfObjectRef( int factor ) - { + public static int getSizeOfObjectRef(int factor) { // pointer to class, flags, lock return factor * (4 + 4 + 4); } - public static int getSizeOfLongArray( int length, int factor ) - { + public static int getSizeOfLongArray(int length, int factor) { // pointer to class, flags, lock, size return factor * (4 + 4 + 4 + 4) + 8 * length; } - public static int getSizeOfObjectArray( int length, int factor ) - { + public static int getSizeOfObjectArray(int length, int factor) { // improvements: add 4byte to make a multiple of 8 in some cases plus compressed oop return factor * (4 + 4 + 4 + 4) + 4 * length; } - public static void close( Closeable cl ) - { - try - { + public static void close(Closeable cl) { + try { if (cl != null) cl.close(); - } catch (IOException ex) - { + } catch (IOException ex) { throw new RuntimeException("Couldn't close resource", ex); } } - public static boolean isEmpty( String str ) - { + public static boolean isEmpty(String str) { return str == null || str.trim().length() == 0; } /** * Determines if the specified ByteBuffer is one which maps to a file! */ - public static boolean isFileMapped( ByteBuffer bb ) - { - if (bb instanceof MappedByteBuffer) - { - try - { + public static boolean isFileMapped(ByteBuffer bb) { + if (bb instanceof MappedByteBuffer) { + try { ((MappedByteBuffer) bb).isLoaded(); return true; - } catch (UnsupportedOperationException ex) - { + } catch (UnsupportedOperationException ex) { } } return false; } - public static int calcIndexSize( BBox graphBounds ) - { + public static int calcIndexSize(BBox graphBounds) { if (!graphBounds.isValid()) throw new IllegalArgumentException("Bounding box is not valid to calculate index size: " + graphBounds); @@ -311,57 +264,48 @@ public static int calcIndexSize( BBox graphBounds ) return Math.max(2000, (int) (dist * dist)); } - public static String pruneFileEnd( String file ) - { + public static String pruneFileEnd(String file) { int index = file.lastIndexOf("."); if (index < 0) return file; return file.substring(0, index); } - public static List createDoubleList( double[] values ) - { + public static List createDoubleList(double[] values) { List list = new ArrayList<>(); - for (double v : values) - { + for (double v : values) { list.add(v); } return list; } - public static TIntList createTList( int... list ) - { + public static TIntList createTList(int... list) { TIntList res = new TIntArrayList(list.length); - for (int val : list) - { + for (int val : list) { res.add(val); } return res; } - public static PointList createPointList( double... list ) - { + public static PointList createPointList(double... list) { if (list.length % 2 != 0) throw new IllegalArgumentException("list should consist of lat,lon pairs!"); int max = list.length / 2; PointList res = new PointList(max, false); - for (int i = 0; i < max; i++) - { + for (int i = 0; i < max; i++) { res.add(list[2 * i], list[2 * i + 1], Double.NaN); } return res; } - public static PointList createPointList3D( double... list ) - { + public static PointList createPointList3D(double... list) { if (list.length % 3 != 0) throw new IllegalArgumentException("list should consist of lat,lon,ele tuples!"); int max = list.length / 3; PointList res = new PointList(max, true); - for (int i = 0; i < max; i++) - { + for (int i = 0; i < max; i++) { res.add(list[3 * i], list[3 * i + 1], list[3 * i + 2]); } return res; @@ -372,10 +316,10 @@ public static PointList createPointList3D( double... list ) * only integer values). But this conversion also reduces memory consumption where the precision * loss is accceptable. As +- 180° and +-90° are assumed as maximum values. *

    + * * @return the integer of the specified degree */ - public static final int degreeToInt( double deg ) - { + public static final int degreeToInt(double deg) { if (deg >= Double.MAX_VALUE) return Integer.MAX_VALUE; if (deg <= -Double.MAX_VALUE) @@ -386,10 +330,10 @@ public static final int degreeToInt( double deg ) /** * Converts back the integer value. *

    + * * @return the degree value of the specified integer */ - public static final double intToDegree( int storedInt ) - { + public static final double intToDegree(int storedInt) { if (storedInt == Integer.MAX_VALUE) return Double.MAX_VALUE; if (storedInt == -Integer.MAX_VALUE) @@ -400,8 +344,7 @@ public static final double intToDegree( int storedInt ) /** * Converts elevation value (in meters) into integer for storage. */ - public static final int eleToInt( double ele ) - { + public static final int eleToInt(double ele) { if (ele >= Integer.MAX_VALUE) return Integer.MAX_VALUE; return (int) (ele * ELE_FACTOR); @@ -411,43 +354,30 @@ public static final int eleToInt( double ele ) * Converts the integer value retrieved from storage into elevation (in meters). Do not expect * more precision than meters although it currently is! */ - public static final double intToEle( int integEle ) - { + public static final double intToEle(int integEle) { if (integEle == Integer.MAX_VALUE) return Double.MAX_VALUE; return integEle / ELE_FACTOR; } - // +- 180 and +-90 => let use use 400 - private static final float DEGREE_FACTOR = Integer.MAX_VALUE / 400f; - // milli meter is a bit extreme but we have integers - private static final float ELE_FACTOR = 1000f; - - public static void cleanMappedByteBuffer( final ByteBuffer buffer ) - { - try - { - AccessController.doPrivileged(new PrivilegedExceptionAction() - { + public static void cleanMappedByteBuffer(final ByteBuffer buffer) { + try { + AccessController.doPrivileged(new PrivilegedExceptionAction() { @Override - public Object run() throws Exception - { - try - { + public Object run() throws Exception { + try { final Method getCleanerMethod = buffer.getClass().getMethod("cleaner"); getCleanerMethod.setAccessible(true); final Object cleaner = getCleanerMethod.invoke(buffer); if (cleaner != null) cleaner.getClass().getMethod("clean").invoke(cleaner); - } catch (NoSuchMethodException ex) - { + } catch (NoSuchMethodException ex) { // ignore if method cleaner or clean is not available, like on Android } return null; } }); - } catch (PrivilegedActionException e) - { + } catch (PrivilegedActionException e) { throw new RuntimeException("unable to unmap the mapped buffer", e); } } @@ -456,23 +386,19 @@ public Object run() throws Exception * Trying to force the release of the mapped ByteBuffer. See * http://stackoverflow.com/q/2972986/194609 and use only if you know what you are doing. */ - public static void cleanHack() - { + public static void cleanHack() { System.gc(); } - public static String nf( long no ) - { + public static String nf(long no) { // I like french localization the most: 123654 will be 123 654 instead // of comma vs. point confusion for english/german guys. // NumberFormat is not thread safe => but getInstance looks like it's cached return NumberFormat.getInstance(Locale.FRANCE).format(no); } - public static String firstBig( String sayText ) - { - if (sayText == null || sayText.length() <= 0) - { + public static String firstBig(String sayText) { + if (sayText == null || sayText.length() <= 0) { return sayText; } @@ -482,32 +408,27 @@ public static String firstBig( String sayText ) /** * This methods returns the value or min if too small or max if too big. */ - public static final double keepIn( double value, double min, double max ) - { + public static final double keepIn(double value, double min, double max) { return Math.max(min, Math.min(value, max)); } /** * Round the value to the specified exponent */ - public static double round( double value, int exponent ) - { + public static double round(double value, int exponent) { double factor = Math.pow(10, exponent); return Math.round(value * factor) / factor; } - public static final double round6( double value ) - { + public static final double round6(double value) { return Math.round(value * 1e6) / 1e6; } - public static final double round4( double value ) - { + public static final double round4(double value) { return Math.round(value * 1e4) / 1e4; } - public static final double round2( double value ) - { + public static final double round2(double value) { return Math.round(value * 100) / 100d; } @@ -515,16 +436,14 @@ public static final double round2( double value ) * This creates a date formatter for yyyy-MM-dd'T'HH:mm:ss'Z' which is has to be identical to * buildDate used in pom.xml */ - public static DateFormat createFormatter() - { + public static DateFormat createFormatter() { return createFormatter("yyyy-MM-dd'T'HH:mm:ss'Z'"); } /** * Creates a SimpleDateFormat with the UK locale. */ - public static DateFormat createFormatter( String str ) - { + public static DateFormat createFormatter(String str) { DateFormat df = new SimpleDateFormat(str, Locale.UK); df.setTimeZone(UTC); return df; @@ -534,27 +453,23 @@ public static DateFormat createFormatter( String str ) * This method handles the specified (potentially negative) int as unsigned bit representation * and returns the positive converted long. */ - public static final long toUnsignedLong( int x ) - { + public static final long toUnsignedLong(int x) { return ((long) x) & 0xFFFFffffL; } /** * Converts the specified long back into a signed int (reverse method for toUnsignedLong) */ - public static final int toSignedInt( long x ) - { + public static final int toSignedInt(long x) { return (int) x; } - public static final String camelCaseToUnderScore( String key ) - { + public static final String camelCaseToUnderScore(String key) { if (key.isEmpty()) return key; StringBuilder sb = new StringBuilder(key.length()); - for (int i = 0; i < key.length(); i++) - { + for (int i = 0; i < key.length(); i++) { char c = key.charAt(i); if (Character.isUpperCase(c)) sb.append("_").append(Character.toLowerCase(c)); @@ -565,17 +480,14 @@ public static final String camelCaseToUnderScore( String key ) return sb.toString(); } - public static final String underScoreToCamelCase( String key ) - { + public static final String underScoreToCamelCase(String key) { if (key.isEmpty()) return key; StringBuilder sb = new StringBuilder(key.length()); - for (int i = 0; i < key.length(); i++) - { + for (int i = 0; i < key.length(); i++) { char c = key.charAt(i); - if (c == '_') - { + if (c == '_') { i++; if (i < key.length()) sb.append(Character.toUpperCase(key.charAt(i))); diff --git a/core/src/main/java/com/graphhopper/util/Helper7.java b/core/src/main/java/com/graphhopper/util/Helper7.java index 77a927db933..f1807a1778b 100644 --- a/core/src/main/java/com/graphhopper/util/Helper7.java +++ b/core/src/main/java/com/graphhopper/util/Helper7.java @@ -24,34 +24,30 @@ * Put the usage of proprietary "sun" classes and after jdk6 classes into this class. To use Helper * class under Android as well. *

    + * * @author Peter Karich */ -public class Helper7 -{ +public class Helper7 { /** * true, if this platform supports unmapping mmapped files. */ public static final boolean UNMAP_SUPPORTED; - static - { + static { boolean v; - try - { + try { Class.forName("sun.misc.Cleaner"); Class.forName("java.nio.DirectByteBuffer") .getMethod("cleaner"); v = true; - } catch (Exception e) - { + } catch (Exception e) { v = false; } UNMAP_SUPPORTED = v; } - public static String getBeanMemInfo() - { + public static String getBeanMemInfo() { java.lang.management.OperatingSystemMXBean mxbean = java.lang.management.ManagementFactory.getOperatingSystemMXBean(); com.sun.management.OperatingSystemMXBean sunmxbean = (com.sun.management.OperatingSystemMXBean) mxbean; long freeMemory = sunmxbean.getFreePhysicalMemorySize(); @@ -60,16 +56,12 @@ public static String getBeanMemInfo() + ", rfree:" + Runtime.getRuntime().freeMemory() / Helper.MB; } - public static void close( XMLStreamReader r ) - { - try - { - if (r != null) - { + public static void close(XMLStreamReader r) { + try { + if (r != null) { r.close(); } - } catch (XMLStreamException ex) - { + } catch (XMLStreamException ex) { throw new RuntimeException("Couldn't close xml reader", ex); } } diff --git a/core/src/main/java/com/graphhopper/util/Instruction.java b/core/src/main/java/com/graphhopper/util/Instruction.java index 0f3e0991f3e..7d8c150fc7b 100644 --- a/core/src/main/java/com/graphhopper/util/Instruction.java +++ b/core/src/main/java/com/graphhopper/util/Instruction.java @@ -21,10 +21,7 @@ import java.util.List; import java.util.Map; -public class Instruction -{ - private static final AngleCalc AC = Helper.ANGLE_CALC; - +public class Instruction { public static final int LEAVE_ROUNDABOUT = -6; // for future use public static final int TURN_SHARP_LEFT = -3; public static final int TURN_LEFT = -2; @@ -36,21 +33,20 @@ public class Instruction public static final int FINISH = 4; public static final int REACHED_VIA = 5; public static final int USE_ROUNDABOUT = 6; - + private static final AngleCalc AC = Helper.ANGLE_CALC; + protected final PointList points; + protected final InstructionAnnotation annotation; protected boolean rawName; protected int sign; protected String name; protected double distance; protected long time; - protected final PointList points; - protected final InstructionAnnotation annotation; /** * The points, distances and times have exactly the same count. The last point of this * instruction is not duplicated here and should be in the next one. */ - public Instruction( int sign, String name, InstructionAnnotation ia, PointList pl ) - { + public Instruction(int sign, String name, InstructionAnnotation ia, PointList pl) { this.sign = sign; this.name = name; this.points = pl; @@ -61,95 +57,80 @@ public Instruction( int sign, String name, InstructionAnnotation ia, PointList p * This method does not perform translation or combination with the sign - it just uses the * provided name as instruction. */ - public void setUseRawName() - { + public void setUseRawName() { rawName = true; } - public InstructionAnnotation getAnnotation() - { + public InstructionAnnotation getAnnotation() { return annotation; } /** * The instruction for the person/driver to execute. */ - public int getSign() - { + public int getSign() { return sign; } - public String getName() - { + public String getName() { return name; } - public void setName( String name ) - { + public void setName(String name) { this.name = name; } - public Map getExtraInfoJSON() - { + public Map getExtraInfoJSON() { return Collections.emptyMap(); } - public void setExtraInfo( String key, Object value ) - { + public void setExtraInfo(String key, Object value) { throw new IllegalArgumentException("Key" + key + " is not a valid option"); } - public Instruction setDistance( double distance ) - { - this.distance = distance; - return this; - } - /** * Distance in meter until no new instruction */ - public double getDistance() - { + public double getDistance() { return distance; } - public Instruction setTime( long time ) - { - this.time = time; + public Instruction setDistance(double distance) { + this.distance = distance; return this; } /** * Time in time until no new instruction */ - public long getTime() - { + public long getTime() { return time; } + public Instruction setTime(long time) { + this.time = time; + return this; + } + /** * Latitude of the location where this instruction should take place. */ - double getFirstLat() - { + double getFirstLat() { return points.getLatitude(0); } /** * Longitude of the location where this instruction should take place. */ - double getFirstLon() - { + double getFirstLon() { return points.getLongitude(0); } - double getFirstEle() - { + double getFirstEle() { return points.getElevation(0); } - public PointList getPoints() - { + public PointList getPoints() { return points; } @@ -157,11 +138,11 @@ public PointList getPoints() * This method returns a list of gpx entries where the time (in time) is relative to the first * which is 0. It does NOT contain the last point which is the first of the next instruction. *

    + * * @return the time offset to add for the next instruction */ - long fillGPXList( List list, long time, - Instruction prevInstr, Instruction nextInstr, boolean firstInstr ) - { + long fillGPXList(List list, long time, + Instruction prevInstr, Instruction nextInstr, boolean firstInstr) { checkOne(); int len = points.size(); long prevTime = time; @@ -172,8 +153,7 @@ long fillGPXList( List list, long time, if (is3D) ele = points.getElevation(0); - for (int i = 0; i < len; i++) - { + for (int i = 0; i < len; i++) { list.add(new GPXEntry(lat, lon, ele, prevTime)); boolean last = i + 1 == len; @@ -193,8 +173,7 @@ long fillGPXList( List list, long time, } @Override - public String toString() - { + public String toString() { StringBuilder sb = new StringBuilder(); sb.append('('); sb.append(sign).append(','); @@ -209,8 +188,7 @@ public String toString() * Return the direction like 'NE' based on the first tracksegment of the instruction. If * Instruction does not contain enough coordinate points, an empty string will be returned. */ - String calcDirection( Instruction nextI ) - { + String calcDirection(Instruction nextI) { double azimuth = calcAzimuth(nextI); if (Double.isNaN(azimuth)) return ""; @@ -223,21 +201,17 @@ String calcDirection( Instruction nextI ) * instruction contains less than 2 points then NaN will be returned or the specified * instruction will be used if that is the finish instruction. */ - public double calcAzimuth( Instruction nextI ) - { + public double calcAzimuth(Instruction nextI) { double nextLat; double nextLon; - if (points.getSize() >= 2) - { + if (points.getSize() >= 2) { nextLat = points.getLatitude(1); nextLon = points.getLongitude(1); - } else if (nextI != null && points.getSize() == 1) - { + } else if (nextI != null && points.getSize() == 1) { nextLat = nextI.points.getLatitude(0); nextLon = nextI.points.getLongitude(0); - } else - { + } else { return Double.NaN; } @@ -246,28 +220,23 @@ public double calcAzimuth( Instruction nextI ) return AC.calcAzimuth(lat, lon, nextLat, nextLon); } - void checkOne() - { + void checkOne() { if (points.size() < 1) throw new IllegalStateException("Instruction must contain at least one point " + toString()); } - public String getTurnDescription( Translation tr ) - { + public String getTurnDescription(Translation tr) { if (rawName) return getName(); String str; String streetName = getName(); int indi = getSign(); - if (indi == Instruction.CONTINUE_ON_STREET) - { + if (indi == Instruction.CONTINUE_ON_STREET) { str = Helper.isEmpty(streetName) ? tr.tr("continue") : tr.tr("continue_onto", streetName); - } else - { + } else { String dir = null; - switch (indi) - { + switch (indi) { case Instruction.TURN_SHARP_LEFT: dir = tr.tr("turn_sharp_left"); break; diff --git a/core/src/main/java/com/graphhopper/util/InstructionAnnotation.java b/core/src/main/java/com/graphhopper/util/InstructionAnnotation.java index 848d98e079f..28fa7826aed 100644 --- a/core/src/main/java/com/graphhopper/util/InstructionAnnotation.java +++ b/core/src/main/java/com/graphhopper/util/InstructionAnnotation.java @@ -20,62 +20,51 @@ /** * @author Peter Karich */ -public class InstructionAnnotation -{ +public class InstructionAnnotation { public final static InstructionAnnotation EMPTY = new InstructionAnnotation(); private boolean empty; private int importance; private String message; - private InstructionAnnotation() - { + private InstructionAnnotation() { setEmpty(); } - public InstructionAnnotation( int importance, String message ) - { - if (message.isEmpty() && importance == 0) - { + public InstructionAnnotation(int importance, String message) { + if (message.isEmpty() && importance == 0) { setEmpty(); - } else - { + } else { this.empty = false; this.importance = importance; this.message = message; } } - private void setEmpty() - { + private void setEmpty() { this.empty = true; this.importance = 0; this.message = ""; } - public boolean isEmpty() - { + public boolean isEmpty() { return empty; } - public int getImportance() - { + public int getImportance() { return importance; } - public String getMessage() - { + public String getMessage() { return message; } @Override - public String toString() - { + public String toString() { return importance + ": " + getMessage(); } @Override - public int hashCode() - { + public int hashCode() { int hash = 3; hash = 83 * hash + this.importance; hash = 83 * hash + (this.message != null ? this.message.hashCode() : 0); @@ -83,8 +72,7 @@ public int hashCode() } @Override - public boolean equals( Object obj ) - { + public boolean equals(Object obj) { if (obj == null) return false; if (getClass() != obj.getClass()) diff --git a/core/src/main/java/com/graphhopper/util/InstructionList.java b/core/src/main/java/com/graphhopper/util/InstructionList.java index 51ff2d88479..8b244c89473 100644 --- a/core/src/main/java/com/graphhopper/util/InstructionList.java +++ b/core/src/main/java/com/graphhopper/util/InstructionList.java @@ -23,58 +23,55 @@ /** * List of instructions. */ -public class InstructionList implements Iterable -{ +public class InstructionList implements Iterable { public static final InstructionList EMPTY = new InstructionList(); private final List instructions; private final Translation tr; - private InstructionList() - { + private InstructionList() { this(0, null); } - public InstructionList( Translation tr ) - { + public InstructionList(Translation tr) { this(10, tr); } - public InstructionList( int cap, Translation tr ) - { + public InstructionList(int cap, Translation tr) { instructions = new ArrayList(cap); this.tr = tr; } - public void replaceLast( Instruction instr ) - { + static String simpleXMLEscape(String str) { + // We could even use the 'more flexible' CDATA section but for now do the following. The 'and' could be important sometimes: + return str.replaceAll("&", "&"). + // but do not care for: + replaceAll("[\\<\\>]", "_"); + } + + public void replaceLast(Instruction instr) { if (instructions.isEmpty()) throw new IllegalStateException("Cannot replace last instruction as list is empty"); instructions.set(instructions.size() - 1, instr); } - public void add( Instruction instr ) - { + public void add(Instruction instr) { instructions.add(instr); } - public int getSize() - { + public int getSize() { return instructions.size(); } - public int size() - { + public int size() { return instructions.size(); } - public List> createJson() - { + public List> createJson() { List> instrList = new ArrayList>(instructions.size()); int pointsIndex = 0; int counter = 0; - for (Instruction instruction : instructions) - { + for (Instruction instruction : instructions) { Map instrJson = new HashMap(); instrList.add(instrJson); @@ -83,8 +80,7 @@ public List> createJson() if (Helper.isEmpty(str)) str = ia.getMessage(); instrJson.put("text", Helper.firstBig(str)); - if (!ia.isEmpty()) - { + if (!ia.isEmpty()) { instrJson.put("annotation_text", ia.getMessage()); instrJson.put("annotation_importance", ia.getImportance()); } @@ -107,25 +103,21 @@ public List> createJson() return instrList; } - public boolean isEmpty() - { + public boolean isEmpty() { return instructions.isEmpty(); } @Override - public Iterator iterator() - { + public Iterator iterator() { return instructions.iterator(); } - public Instruction get( int index ) - { + public Instruction get(int index) { return instructions.get(index); } @Override - public String toString() - { + public String toString() { return instructions.toString(); } @@ -134,15 +126,13 @@ public String toString() * the first which is 0. *

    */ - public List createGPXList() - { + public List createGPXList() { if (isEmpty()) return Collections.emptyList(); List gpxList = new ArrayList(); long timeOffset = 0; - for (int i = 0; i < size() - 1; i++) - { + for (int i = 0; i < size() - 1; i++) { Instruction prevInstr = (i > 0) ? get(i - 1) : null; boolean instrIsFirst = prevInstr == null; Instruction nextInstr = get(i + 1); @@ -163,21 +153,19 @@ public List createGPXList() * Creates the standard GPX string out of the points according to the schema found here: * https://graphhopper.com/public/schema/gpx-1.1.xsd *

    + * * @return string to be stored as gpx file */ - public String createGPX() - { + public String createGPX() { return createGPX("GraphHopper", new Date().getTime()); } - public String createGPX( String trackName, long startTimeMillis ) - { + public String createGPX(String trackName, long startTimeMillis) { boolean includeElevation = getSize() > 0 ? get(0).getPoints().is3D() : false; return createGPX(trackName, startTimeMillis, includeElevation, true, true, true); } - private void createWayPointBlock( StringBuilder output, Instruction instruction ) - { + private void createWayPointBlock(StringBuilder output, Instruction instruction) { output.append("\n"); @@ -191,16 +179,7 @@ private void createWayPointBlock( StringBuilder output, Instruction instruction output.append(""); } - static String simpleXMLEscape( String str ) - { - // We could even use the 'more flexible' CDATA section but for now do the following. The 'and' could be important sometimes: - return str.replaceAll("&", "&"). - // but do not care for: - replaceAll("[\\<\\>]", "_"); - } - - public String createGPX( String trackName, long startTimeMillis, boolean includeElevation, boolean withRoute, boolean withTrack, boolean withWayPoints ) - { + public String createGPX(String trackName, long startTimeMillis, boolean includeElevation, boolean withRoute, boolean withTrack, boolean withWayPoints) { DateFormat formatter = Helper.createFormatter(); String header = "" @@ -217,26 +196,21 @@ public String createGPX( String trackName, long startTimeMillis, boolean include + "" + ""; StringBuilder gpxOutput = new StringBuilder(header); - if (!isEmpty()) - { - if (withWayPoints) - { + if (!isEmpty()) { + if (withWayPoints) { createWayPointBlock(gpxOutput, instructions.get(0)); // Start - for (Instruction currInstr : instructions) - { - if ((currInstr.getSign() == Instruction.REACHED_VIA) || // Via - (currInstr.getSign() == Instruction.FINISH)) // End + for (Instruction currInstr : instructions) { + if ((currInstr.getSign() == Instruction.REACHED_VIA) // Via + || (currInstr.getSign() == Instruction.FINISH)) // End { createWayPointBlock(gpxOutput, currInstr); } } } - if (withRoute) - { + if (withRoute) { gpxOutput.append("\n"); Instruction nextInstr = null; - for (Instruction currInstr : instructions) - { + for (Instruction currInstr : instructions) { if (null != nextInstr) createRteptBlock(gpxOutput, nextInstr, currInstr); @@ -246,13 +220,11 @@ public String createGPX( String trackName, long startTimeMillis, boolean include gpxOutput.append("\n"); } } - if (withTrack) - { + if (withTrack) { gpxOutput.append("\n").append(trackName).append(""); gpxOutput.append(""); - for (GPXEntry entry : createGPXList()) - { + for (GPXEntry entry : createGPXList()) { gpxOutput.append("\n"); if (includeElevation) @@ -269,8 +241,7 @@ public String createGPX( String trackName, long startTimeMillis, boolean include return gpxOutput.toString(); } - public void createRteptBlock( StringBuilder output, Instruction instruction, Instruction nextI ) - { + public void createRteptBlock(StringBuilder output, Instruction instruction, Instruction nextI) { output.append("\n"); @@ -297,11 +268,9 @@ public void createRteptBlock( StringBuilder output, Instruction instruction, Ins /** * @return list of lat lon */ - List> createStartPoints() - { + List> createStartPoints() { List> res = new ArrayList>(instructions.size()); - for (Instruction instruction : instructions) - { + for (Instruction instruction : instructions) { res.add(Arrays.asList(instruction.getFirstLat(), instruction.getFirstLon())); } return res; @@ -311,14 +280,13 @@ List> createStartPoints() * This method is useful for navigation devices to find the next instruction for the specified * coordinate (e.g. the current position). *

    + * * @param maxDistance the maximum acceptable distance to the instruction (in meter) * @return the next Instruction or null if too far away. */ - public Instruction find( double lat, double lon, double maxDistance ) - { + public Instruction find(double lat, double lon, double maxDistance) { // handle special cases - if (getSize() == 0) - { + if (getSize() == 0) { return null; } PointList points = get(0).getPoints(); @@ -329,35 +297,28 @@ public Instruction find( double lat, double lon, double maxDistance ) int foundInstruction = 0; // Search the closest edge to the query point - if (getSize() > 1) - { - for (int instructionIndex = 0; instructionIndex < getSize(); instructionIndex++) - { + if (getSize() > 1) { + for (int instructionIndex = 0; instructionIndex < getSize(); instructionIndex++) { points = get(instructionIndex).getPoints(); - for (int pointIndex = 0; pointIndex < points.size(); pointIndex++) - { + for (int pointIndex = 0; pointIndex < points.size(); pointIndex++) { double currLat = points.getLatitude(pointIndex); double currLon = points.getLongitude(pointIndex); - if (!(instructionIndex == 0 && pointIndex == 0)) - { + if (!(instructionIndex == 0 && pointIndex == 0)) { // calculate the distance from the point to the edge double distance; int index = instructionIndex; - if (distCalc.validEdgeDistance(lat, lon, currLat, currLon, prevLat, prevLon)) - { + if (distCalc.validEdgeDistance(lat, lon, currLat, currLon, prevLat, prevLon)) { distance = distCalc.calcNormalizedEdgeDistance(lat, lon, currLat, currLon, prevLat, prevLon); if (pointIndex > 0) index++; - } else - { + } else { distance = distCalc.calcNormalizedDist(lat, lon, currLat, currLon); if (pointIndex > 0) index++; } - if (distance < foundMinDistance) - { + if (distance < foundMinDistance) { foundMinDistance = distance; foundInstruction = index; } diff --git a/core/src/main/java/com/graphhopper/util/MiniPerfTest.java b/core/src/main/java/com/graphhopper/util/MiniPerfTest.java index 98a108056c5..3d9e9d361c3 100644 --- a/core/src/main/java/com/graphhopper/util/MiniPerfTest.java +++ b/core/src/main/java/com/graphhopper/util/MiniPerfTest.java @@ -17,16 +17,15 @@ */ package com.graphhopper.util; -import java.text.DecimalFormat; - import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.text.DecimalFormat; + /** * @author Peter Karich */ -public abstract class MiniPerfTest -{ +public abstract class MiniPerfTest { protected Logger logger = LoggerFactory.getLogger(getClass()); private int counts = 100; private double fullTime = 0; @@ -34,16 +33,13 @@ public abstract class MiniPerfTest private double min = Double.MAX_VALUE; private int dummySum; - public MiniPerfTest start() - { + public MiniPerfTest start() { int warmupCount = Math.max(1, counts / 3); - for (int i = 0; i < warmupCount; i++) - { + for (int i = 0; i < warmupCount; i++) { dummySum += doCalc(true, i); } long startFull = System.nanoTime(); - for (int i = 0; i < counts; i++) - { + for (int i = 0; i < counts; i++) { long start = System.nanoTime(); dummySum += doCalc(false, i); long time = System.nanoTime() - start; @@ -58,8 +54,7 @@ public MiniPerfTest start() return this; } - public MiniPerfTest setIterations( int counts ) - { + public MiniPerfTest setIterations(int counts) { this.counts = counts; return this; } @@ -67,42 +62,36 @@ public MiniPerfTest setIterations( int counts ) /** * @return minimum time of every call, in ms */ - public double getMin() - { + public double getMin() { return min / 1e6; } /** * @return maximum time of every calls, in ms */ - public double getMax() - { + public double getMax() { return max / 1e6; } /** * @return time for all calls accumulated, in ms */ - public double getSum() - { + public double getSum() { return fullTime / 1e6; } /** * @return mean time per call, in ms */ - public double getMean() - { + public double getMean() { return getSum() / counts; } - public String getReport() - { + public String getReport() { return "sum:" + nf(getSum() / 1000f) + "s, time/call:" + nf(getMean() / 1000f) + "s"; } - public String nf( Number num ) - { + public String nf(Number num) { return new DecimalFormat("#.#").format(num); } @@ -110,5 +99,5 @@ public String nf( Number num ) * @return return some integer as result from your processing to make sure that the JVM cannot * optimize (away) the call or within the call something. */ - public abstract int doCalc( boolean warmup, int run ); + public abstract int doCalc(boolean warmup, int run); } diff --git a/core/src/main/java/com/graphhopper/util/NotThreadSafe.java b/core/src/main/java/com/graphhopper/util/NotThreadSafe.java index 9884843facb..45d537920bd 100644 --- a/core/src/main/java/com/graphhopper/util/NotThreadSafe.java +++ b/core/src/main/java/com/graphhopper/util/NotThreadSafe.java @@ -20,8 +20,8 @@ /** * Marks classes or methods as none thread-safe *

    + * * @author Peter Karich */ -public @interface NotThreadSafe -{ +public @interface NotThreadSafe { } diff --git a/core/src/main/java/com/graphhopper/util/NumHelper.java b/core/src/main/java/com/graphhopper/util/NumHelper.java index 69d375d0ed5..13aea2a56e4 100644 --- a/core/src/main/java/com/graphhopper/util/NumHelper.java +++ b/core/src/main/java/com/graphhopper/util/NumHelper.java @@ -20,37 +20,30 @@ /** * @author Peter Karich */ -public class NumHelper -{ +public class NumHelper { private final static double DEFAULT_PRECISION = 1e-6; - public static boolean equalsEps( double d1, double d2 ) - { + public static boolean equalsEps(double d1, double d2) { return equalsEps(d1, d2, DEFAULT_PRECISION); } - public static boolean equalsEps( double d1, double d2, double epsilon ) - { + public static boolean equalsEps(double d1, double d2, double epsilon) { return Math.abs(d1 - d2) < epsilon; } - public static boolean equals( double d1, double d2 ) - { + public static boolean equals(double d1, double d2) { return Double.compare(d1, d2) == 0; } - public static int compare( double d1, double d2 ) - { + public static int compare(double d1, double d2) { return Double.compare(d1, d2); } - public static boolean equalsEps( float d1, float d2 ) - { + public static boolean equalsEps(float d1, float d2) { return equalsEps(d1, d2, DEFAULT_PRECISION); } - public static boolean equalsEps( float d1, float d2, float epsilon ) - { + public static boolean equalsEps(float d1, float d2, float epsilon) { return Math.abs(d1 - d2) < epsilon; } } diff --git a/core/src/main/java/com/graphhopper/util/PMap.java b/core/src/main/java/com/graphhopper/util/PMap.java index 7216768c132..e6edcbc0a10 100644 --- a/core/src/main/java/com/graphhopper/util/PMap.java +++ b/core/src/main/java/com/graphhopper/util/PMap.java @@ -23,35 +23,30 @@ /** * A properties map (String to String) with convenient accessors *

    - * @see ConfigMap + * * @author Peter Karich + * @see ConfigMap */ -public class PMap -{ +public class PMap { private final Map map; - public PMap() - { + public PMap() { this(5); } - public PMap( int capacity ) - { + public PMap(int capacity) { this(new HashMap(capacity)); } - public PMap( Map map ) - { + public PMap(Map map) { this.map = map; } - public PMap( String propertiesString ) - { + public PMap(String propertiesString) { // five chosen as arbitrary initial capacity this.map = new HashMap(5); - for (String s : propertiesString.split("\\|")) - { + for (String s : propertiesString.split("\\|")) { s = s.trim(); int index = s.indexOf("="); if (index < 0) @@ -61,14 +56,12 @@ public PMap( String propertiesString ) } } - public PMap put( PMap map ) - { + public PMap put(PMap map) { this.map.putAll(map.map); return this; } - public PMap put( String key, Object str ) - { + public PMap put(String key, Object str) { if (str == null) throw new NullPointerException("Value cannot be null. Use remove instead."); @@ -77,81 +70,62 @@ public PMap put( String key, Object str ) return this; } - public PMap remove( String key ) - { + public PMap remove(String key) { // query accepts camelCase and under_score map.remove(Helper.camelCaseToUnderScore(key)); return this; } - public boolean has( String key ) - { + public boolean has(String key) { // query accepts camelCase and under_score return map.containsKey(Helper.camelCaseToUnderScore(key)); } - public long getLong( String key, long _default ) - { + public long getLong(String key, long _default) { String str = get(key); - if (!Helper.isEmpty(str)) - { - try - { + if (!Helper.isEmpty(str)) { + try { return Long.parseLong(str); - } catch (Exception ex) - { + } catch (Exception ex) { } } return _default; } - public int getInt( String key, int _default ) - { + public int getInt(String key, int _default) { String str = get(key); - if (!Helper.isEmpty(str)) - { - try - { + if (!Helper.isEmpty(str)) { + try { return Integer.parseInt(str); - } catch (Exception ex) - { + } catch (Exception ex) { } } return _default; } - public boolean getBool( String key, boolean _default ) - { + public boolean getBool(String key, boolean _default) { String str = get(key); - if (!Helper.isEmpty(str)) - { - try - { + if (!Helper.isEmpty(str)) { + try { return Boolean.parseBoolean(str); - } catch (Exception ex) - { + } catch (Exception ex) { } } return _default; } - public double getDouble( String key, double _default ) - { + public double getDouble(String key, double _default) { String str = get(key); - if (!Helper.isEmpty(str)) - { - try - { + if (!Helper.isEmpty(str)) { + try { return Double.parseDouble(str); - } catch (Exception ex) - { + } catch (Exception ex) { } } return _default; } - public String get( String key, String _default ) - { + public String get(String key, String _default) { String str = get(key); if (Helper.isEmpty(str)) return _default; @@ -159,8 +133,7 @@ public String get( String key, String _default ) return str; } - String get( String key ) - { + String get(String key) { if (Helper.isEmpty(key)) return ""; @@ -175,25 +148,20 @@ String get( String key ) /** * This method copies the underlying structur into a new Map object */ - public Map toMap() - { + public Map toMap() { return new HashMap(map); } - private Map getMap() - { + private Map getMap() { return map; } - public PMap merge( PMap read ) - { + public PMap merge(PMap read) { return merge(read.getMap()); } - PMap merge( Map map ) - { - for (Map.Entry e : map.entrySet()) - { + PMap merge(Map map) { + for (Map.Entry e : map.entrySet()) { if (Helper.isEmpty(e.getKey())) continue; @@ -203,8 +171,7 @@ PMap merge( Map map ) } @Override - public String toString() - { + public String toString() { return getMap().toString(); } } diff --git a/core/src/main/java/com/graphhopper/util/Parameters.java b/core/src/main/java/com/graphhopper/util/Parameters.java index 23e451c7146..09c47efc370 100644 --- a/core/src/main/java/com/graphhopper/util/Parameters.java +++ b/core/src/main/java/com/graphhopper/util/Parameters.java @@ -18,19 +18,16 @@ package com.graphhopper.util; /** - * * @author Peter Karich */ -public class Parameters -{ +public class Parameters { /* Parameters with an 'INIT' prefix are used as defaults and/or are configured at start.*/ static final String ROUTING_INIT_PREFIX = "routing."; /** * Parameters that can be used for algorithm. */ - public static final class Algorithms - { + public static final class Algorithms { /** * Bidirectional Dijkstra */ @@ -63,8 +60,7 @@ public static final class Algorithms /** * All public properties for alternative routing. */ - public static final class AltRoute - { + public static final class AltRoute { public static final String MAX_PATHS = ALT_ROUTE + ".max_paths"; public static final String MAX_WEIGHT = ALT_ROUTE + ".max_weight_factor"; @@ -72,21 +68,18 @@ public static final class AltRoute public static final String MAX_SHARE = ALT_ROUTE + ".max_share_factor"; } - public static final class AStar - { + public static final class AStar { public static final String EPSILON = ASTAR + ".epsilon"; } - public static final class AStarBi - { + public static final class AStarBi { public static final String EPSILON = ASTAR_BI + ".epsilon"; } /** * All public properties for round trip calculation. */ - public static final class RoundTrip - { + public static final class RoundTrip { public static final String DISTANCE = ROUND_TRIP + ".distance"; public static final String SEED = ROUND_TRIP + ".seed"; public static final String HEADING = "heading"; @@ -97,8 +90,7 @@ public static final class RoundTrip /** * Parameters that can be passed as hints and influence routing per request. */ - public static final class Routing - { + public static final class Routing { public static final String EDGE_BASED = "edge_based"; public static final String MAX_VISITED_NODES = "max_visited_nodes"; public static final String INIT_MAX_VISITED_NODES = ROUTING_INIT_PREFIX + "max_visited_nodes"; @@ -122,8 +114,7 @@ public static final class Routing /** * Properties for CH routing */ - public static final class CH - { + public static final class CH { /** * This property name in HintsMap configures at runtime if CH routing should be ignored. */ diff --git a/core/src/main/java/com/graphhopper/util/PathMerger.java b/core/src/main/java/com/graphhopper/util/PathMerger.java index 0f8294dfd14..7f0da2a6861 100644 --- a/core/src/main/java/com/graphhopper/util/PathMerger.java +++ b/core/src/main/java/com/graphhopper/util/PathMerger.java @@ -19,49 +19,45 @@ import com.graphhopper.PathWrapper; import com.graphhopper.routing.Path; + import java.util.ArrayList; import java.util.List; /** * This class merges a list of points into one point recognizing the specified places. *

    + * * @author Peter Karich * @author ratrun */ -public class PathMerger -{ +public class PathMerger { private static final DouglasPeucker DP = new DouglasPeucker(); private boolean enableInstructions = true; private boolean simplifyResponse = true; private DouglasPeucker douglasPeucker = DP; private boolean calcPoints = true; - public PathMerger setCalcPoints( boolean calcPoints ) - { + public PathMerger setCalcPoints(boolean calcPoints) { this.calcPoints = calcPoints; return this; } - public PathMerger setDouglasPeucker( DouglasPeucker douglasPeucker ) - { + public PathMerger setDouglasPeucker(DouglasPeucker douglasPeucker) { this.douglasPeucker = douglasPeucker; return this; } - public PathMerger setSimplifyResponse( boolean simplifyRes ) - { + public PathMerger setSimplifyResponse(boolean simplifyRes) { this.simplifyResponse = simplifyRes; return this; } - public PathMerger setEnableInstructions( boolean enableInstructions ) - { + public PathMerger setEnableInstructions(boolean enableInstructions) { this.enableInstructions = enableInstructions; return this; } - public void doWork( PathWrapper altRsp, List paths, Translation tr ) - { + public void doWork(PathWrapper altRsp, List paths, Translation tr) { int origPoints = 0; long fullTimeInMillis = 0; double fullWeight = 0; @@ -71,30 +67,24 @@ public void doWork( PathWrapper altRsp, List paths, Translation tr ) InstructionList fullInstructions = new InstructionList(tr); PointList fullPoints = PointList.EMPTY; List description = new ArrayList(); - for (int pathIndex = 0; pathIndex < paths.size(); pathIndex++) - { + for (int pathIndex = 0; pathIndex < paths.size(); pathIndex++) { Path path = paths.get(pathIndex); description.addAll(path.getDescription()); fullTimeInMillis += path.getTime(); fullDistance += path.getDistance(); fullWeight += path.getWeight(); - if (enableInstructions) - { + if (enableInstructions) { InstructionList il = path.calcInstructions(tr); - if (!il.isEmpty()) - { - if (fullPoints.isEmpty()) - { + if (!il.isEmpty()) { + if (fullPoints.isEmpty()) { PointList pl = il.get(0).getPoints(); // do a wild guess about the total number of points to avoid reallocation a bit fullPoints = new PointList(il.size() * Math.min(10, pl.size()), pl.is3D()); } - for (Instruction i : il) - { - if (simplifyResponse) - { + for (Instruction i : il) { + if (simplifyResponse) { origPoints += i.getPoints().size(); douglasPeucker.simplify(i.getPoints()); } @@ -103,22 +93,19 @@ public void doWork( PathWrapper altRsp, List paths, Translation tr ) } // if not yet reached finish replace with 'reached via' - if (pathIndex + 1 < paths.size()) - { + if (pathIndex + 1 < paths.size()) { ViaInstruction newInstr = new ViaInstruction(fullInstructions.get(fullInstructions.size() - 1)); newInstr.setViaCount(pathIndex + 1); fullInstructions.replaceLast(newInstr); } } - } else if (calcPoints) - { + } else if (calcPoints) { PointList tmpPoints = path.calcPoints(); if (fullPoints.isEmpty()) fullPoints = new PointList(tmpPoints.size(), tmpPoints.is3D()); - if (simplifyResponse) - { + if (simplifyResponse) { origPoints = tmpPoints.getSize(); douglasPeucker.simplify(tmpPoints); } @@ -128,8 +115,7 @@ public void doWork( PathWrapper altRsp, List paths, Translation tr ) allFound = allFound && path.isFound(); } - if (!fullPoints.isEmpty()) - { + if (!fullPoints.isEmpty()) { String debug = altRsp.getDebugInfo() + ", simplify (" + origPoints + "->" + fullPoints.getSize() + ")"; altRsp.addDebugInfo(debug); if (fullPoints.is3D) @@ -149,13 +135,11 @@ public void doWork( PathWrapper altRsp, List paths, Translation tr ) setTime(fullTimeInMillis); } - private void calcAscendDescend( final PathWrapper rsp, final PointList pointList ) - { + private void calcAscendDescend(final PathWrapper rsp, final PointList pointList) { double ascendMeters = 0; double descendMeters = 0; double lastEle = pointList.getElevation(0); - for (int i = 1; i < pointList.size(); ++i) - { + for (int i = 1; i < pointList.size(); ++i) { double ele = pointList.getElevation(i); double diff = Math.abs(ele - lastEle); diff --git a/core/src/main/java/com/graphhopper/util/PointAccess.java b/core/src/main/java/com/graphhopper/util/PointAccess.java index 92b1567f879..368decb38b8 100644 --- a/core/src/main/java/com/graphhopper/util/PointAccess.java +++ b/core/src/main/java/com/graphhopper/util/PointAccess.java @@ -20,8 +20,7 @@ /** * @author Peter Karich */ -public interface PointAccess -{ +public interface PointAccess { /** * @return true if elevation data is stored and can be retrieved */ @@ -36,7 +35,7 @@ public interface PointAccess * This method ensures that the node with the specified index exists i.e. allocates space for * it. */ - void ensureNode( int nodeId ); + void ensureNode(int nodeId); /** * This method ensures that the node with the specified index exists and prepares access to it. @@ -44,7 +43,7 @@ public interface PointAccess *

    * This methods sets the latitude, longitude and elevation to the specified value. */ - void setNode( int nodeId, double lat, double lon ); + void setNode(int nodeId, double lat, double lon); /** * This method ensures that the node with the specified index exists and prepares access to it. @@ -52,26 +51,26 @@ public interface PointAccess *

    * This methods sets the latitude, longitude and elevation to the specified value. */ - void setNode( int nodeId, double lat, double lon, double ele ); + void setNode(int nodeId, double lat, double lon, double ele); /** * @return the latitude at the specified node index */ - double getLatitude( int nodeId ); + double getLatitude(int nodeId); - double getLat( int nodeId ); + double getLat(int nodeId); /** * @return the longitude at the specified node index */ - double getLongitude( int nodeId ); + double getLongitude(int nodeId); - double getLon( int nodeId ); + double getLon(int nodeId); /** * Returns the elevation of the specified nodeId. */ - double getElevation( int nodeId ); + double getElevation(int nodeId); - double getEle( int nodeId ); + double getEle(int nodeId); } diff --git a/core/src/main/java/com/graphhopper/util/PointList.java b/core/src/main/java/com/graphhopper/util/PointList.java index c391e667a89..d810e856ed4 100644 --- a/core/src/main/java/com/graphhopper/util/PointList.java +++ b/core/src/main/java/com/graphhopper/util/PointList.java @@ -25,25 +25,129 @@ /** * Slim list to store several points (without the need for a point object). *

    + * * @author Peter Karich */ -public class PointList implements Iterable, PointAccess -{ +public class PointList implements Iterable, PointAccess { + public static final PointList EMPTY = new PointList(0, true) { + @Override + public void set(int index, double lat, double lon, double ele) { + throw new RuntimeException("cannot change EMPTY PointList"); + } + + @Override + public void add(double lat, double lon, double ele) { + throw new RuntimeException("cannot change EMPTY PointList"); + } + + @Override + public double getLatitude(int index) { + throw new RuntimeException("cannot access EMPTY PointList"); + } + + @Override + public double getLongitude(int index) { + throw new RuntimeException("cannot access EMPTY PointList"); + } + + @Override + public boolean isEmpty() { + return true; + } + + @Override + public void clear() { + throw new RuntimeException("cannot change EMPTY PointList"); + } + + @Override + public void trimToSize(int newSize) { + throw new RuntimeException("cannot change EMPTY PointList"); + } + + @Override + public void parse2DJSON(String str) { + throw new RuntimeException("cannot change EMPTY PointList"); + } + + @Override + public double calcDistance(DistanceCalc calc) { + throw new UnsupportedOperationException("cannot access EMPTY PointList"); + } + + @Override + public PointList copy(int from, int end) { + throw new RuntimeException("cannot copy EMPTY PointList"); + } + + @Override + public PointList clone(boolean reverse) { + throw new UnsupportedOperationException("cannot access EMPTY PointList"); + } + + @Override + public double getElevation(int index) { + throw new UnsupportedOperationException("cannot access EMPTY PointList"); + } + + @Override + public double getLat(int index) { + throw new UnsupportedOperationException("cannot access EMPTY PointList"); + } + + @Override + public double getLon(int index) { + throw new UnsupportedOperationException("cannot access EMPTY PointList"); + } + + @Override + public double getEle(int index) { + throw new UnsupportedOperationException("cannot access EMPTY PointList"); + } + + @Override + public List toGeoJson() { + throw new UnsupportedOperationException("cannot access EMPTY PointList"); + } + + @Override + public void reverse() { + throw new UnsupportedOperationException("cannot change EMPTY PointList"); + } + + @Override + public int getSize() { + return 0; + } + + @Override + public int size() { + return 0; + } + + @Override + public GHPoint3D toGHPoint(int index) { + throw new UnsupportedOperationException("cannot access EMPTY PointList"); + } + + @Override + public boolean is3D() { + throw new UnsupportedOperationException("cannot access EMPTY PointList"); + } + }; private final static DistanceCalc3D distCalc3D = Helper.DIST_3D; private static String ERR_MSG = "Tried to access PointList with too big index!"; + protected int size = 0; + protected boolean is3D; private double[] latitudes; private double[] longitudes; private double[] elevations; - protected int size = 0; - protected boolean is3D; - public PointList() - { + public PointList() { this(10, false); } - public PointList( int cap, boolean is3D ) - { + public PointList(int cap, boolean is3D) { latitudes = new double[cap]; longitudes = new double[cap]; this.is3D = is3D; @@ -52,39 +156,33 @@ public PointList( int cap, boolean is3D ) } @Override - public boolean is3D() - { + public boolean is3D() { return is3D; } @Override - public int getDimension() - { + public int getDimension() { if (is3D) return 3; return 2; } @Override - public void ensureNode( int nodeId ) - { + public void ensureNode(int nodeId) { incCap(nodeId + 1); } @Override - public void setNode( int nodeId, double lat, double lon ) - { + public void setNode(int nodeId, double lat, double lon) { set(nodeId, lat, lon, Double.NaN); } @Override - public void setNode( int nodeId, double lat, double lon, double ele ) - { + public void setNode(int nodeId, double lat, double lon, double ele) { set(nodeId, lat, lon, ele); } - public void set( int index, double lat, double lon, double ele ) - { + public void set(int index, double lat, double lon, double ele) { if (index >= size) throw new ArrayIndexOutOfBoundsException("index has to be smaller than size " + size); @@ -96,8 +194,7 @@ else if (!Double.isNaN(ele)) throw new IllegalStateException("This is a 2D list we cannot store elevation: " + ele); } - private void incCap( int newSize ) - { + private void incCap(int newSize) { if (newSize <= latitudes.length) return; @@ -110,15 +207,13 @@ private void incCap( int newSize ) elevations = Arrays.copyOf(elevations, cap); } - public void add( double lat, double lon ) - { + public void add(double lat, double lon) { if (is3D) throw new IllegalStateException("Cannot add point without elevation data in 3D mode"); add(lat, lon, Double.NaN); } - public void add( double lat, double lon, double ele ) - { + public void add(double lat, double lon, double ele) { int newSize = size + 1; incCap(newSize); latitudes[size] = lat; @@ -130,28 +225,24 @@ else if (!Double.isNaN(ele)) size = newSize; } - public void add( PointAccess nodeAccess, int index ) - { + public void add(PointAccess nodeAccess, int index) { if (is3D) add(nodeAccess.getLatitude(index), nodeAccess.getLongitude(index), nodeAccess.getElevation(index)); else add(nodeAccess.getLatitude(index), nodeAccess.getLongitude(index)); } - public void add( GHPoint point ) - { + public void add(GHPoint point) { if (is3D) add(point.lat, point.lon, ((GHPoint3D) point).ele); else add(point.lat, point.lon); } - public void add( PointList points ) - { + public void add(PointList points) { int newSize = size + points.getSize(); incCap(newSize); - for (int i = 0; i < points.getSize(); i++) - { + for (int i = 0; i < points.getSize(); i++) { int tmp = size + i; latitudes[tmp] = points.getLatitude(i); longitudes[tmp] = points.getLongitude(i); @@ -161,30 +252,25 @@ public void add( PointList points ) size = newSize; } - public int size() - { + public int size() { return size; } - public int getSize() - { + public int getSize() { return size; } - public boolean isEmpty() - { + public boolean isEmpty() { return size == 0; } @Override - public double getLat( int index ) - { + public double getLat(int index) { return getLatitude(index); } @Override - public double getLatitude( int index ) - { + public double getLatitude(int index) { if (index >= size) throw new ArrayIndexOutOfBoundsException(ERR_MSG + " index:" + index + ", size:" + size); @@ -192,14 +278,12 @@ public double getLatitude( int index ) } @Override - public double getLon( int index ) - { + public double getLon(int index) { return getLongitude(index); } @Override - public double getLongitude( int index ) - { + public double getLongitude(int index) { if (index >= size) throw new ArrayIndexOutOfBoundsException(ERR_MSG + " index:" + index + ", size:" + size); @@ -207,8 +291,7 @@ public double getLongitude( int index ) } @Override - public double getElevation( int index ) - { + public double getElevation(int index) { if (index >= size) throw new ArrayIndexOutOfBoundsException(ERR_MSG + " index:" + index + ", size:" + size); if (!is3D) @@ -218,17 +301,14 @@ public double getElevation( int index ) } @Override - public double getEle( int index ) - { + public double getEle(int index) { return getElevation(index); } - public void reverse() - { + public void reverse() { // in-place reverse int max = size / 2; - for (int i = 0; i < max; i++) - { + for (int i = 0; i < max; i++) { int swapIndex = size - i - 1; double tmp = latitudes[i]; @@ -239,8 +319,7 @@ public void reverse() longitudes[i] = longitudes[swapIndex]; longitudes[swapIndex] = tmp; - if (is3D) - { + if (is3D) { tmp = elevations[i]; elevations[i] = elevations[swapIndex]; elevations[swapIndex] = tmp; @@ -248,13 +327,11 @@ public void reverse() } } - public void clear() - { + public void clear() { size = 0; } - public void trimToSize( int newSize ) - { + public void trimToSize(int newSize) { if (newSize > size) throw new IllegalArgumentException("new size needs be smaller than old size"); @@ -262,11 +339,9 @@ public void trimToSize( int newSize ) } @Override - public String toString() - { + public String toString() { StringBuilder sb = new StringBuilder(); - for (int i = 0; i < size; i++) - { + for (int i = 0; i < size; i++) { if (i > 0) sb.append(", "); @@ -274,8 +349,7 @@ public String toString() sb.append(latitudes[i]); sb.append(','); sb.append(longitudes[i]); - if (is3D) - { + if (is3D) { sb.append(','); sb.append(elevations[i]); } @@ -287,26 +361,21 @@ public String toString() /** * Attention: geoJson is LON,LAT or LON,LAT,ELE */ - public List toGeoJson() - { + public List toGeoJson() { return toGeoJson(is3D); } - public List toGeoJson( boolean includeElevation ) - { + public List toGeoJson(boolean includeElevation) { ArrayList points = new ArrayList(size); - for (int i = 0; i < size; i++) - { + for (int i = 0; i < size; i++) { if (includeElevation) - points.add(new Double[] - { + points.add(new Double[]{ Helper.round6(getLongitude(i)), Helper.round6(getLatitude(i)), Helper.round2(getElevation(i)) }); else - points.add(new Double[] - { + points.add(new Double[]{ Helper.round6(getLongitude(i)), Helper.round6(getLatitude(i)) }); } @@ -314,8 +383,7 @@ public List toGeoJson( boolean includeElevation ) } @Override - public boolean equals( Object obj ) - { + public boolean equals(Object obj) { if (obj == null) return false; @@ -326,8 +394,7 @@ public boolean equals( Object obj ) if (this.getSize() != other.getSize() || this.is3D() != other.is3D()) return false; - for (int i = 0; i < size; i++) - { + for (int i = 0; i < size; i++) { if (!NumHelper.equalsEps(latitudes[i], other.latitudes[i])) return false; @@ -340,17 +407,14 @@ public boolean equals( Object obj ) return true; } - public PointList clone( boolean reverse ) - { + public PointList clone(boolean reverse) { PointList clonePL = new PointList(size, is3D); if (is3D) - for (int i = 0; i < size; i++) - { + for (int i = 0; i < size; i++) { clonePL.add(latitudes[i], longitudes[i], elevations[i]); } else - for (int i = 0; i < size; i++) - { + for (int i = 0; i < size; i++) { clonePL.add(latitudes[i], longitudes[i]); } if (reverse) @@ -358,8 +422,7 @@ public PointList clone( boolean reverse ) return clonePL; } - public PointList copy( int from, int end ) - { + public PointList copy(int from, int end) { if (from > end) throw new IllegalArgumentException("from must be smaller or equals to end"); if (from < 0 || end > size) @@ -367,13 +430,11 @@ public PointList copy( int from, int end ) PointList copyPL = new PointList(size, is3D); if (is3D) - for (int i = from; i < end; i++) - { + for (int i = from; i < end; i++) { copyPL.add(latitudes[i], longitudes[i], elevations[i]); } else - for (int i = from; i < end; i++) - { + for (int i = from; i < end; i++) { copyPL.add(latitudes[i], longitudes[i], Double.NaN); } @@ -381,11 +442,9 @@ public PointList copy( int from, int end ) } @Override - public int hashCode() - { + public int hashCode() { int hash = 5; - for (int i = 0; i < latitudes.length; i++) - { + for (int i = 0; i < latitudes.length; i++) { hash = 73 * hash + (int) Math.round(latitudes[i] * 1000000); hash = 73 * hash + (int) Math.round(longitudes[i] * 1000000); } @@ -393,16 +452,13 @@ public int hashCode() return hash; } - public double calcDistance( DistanceCalc calc ) - { + public double calcDistance(DistanceCalc calc) { double prevLat = Double.NaN; double prevLon = Double.NaN; double prevEle = Double.NaN; double dist = 0; - for (int i = 0; i < size; i++) - { - if (i > 0) - { + for (int i = 0; i < size; i++) { + if (i > 0) { if (is3D) dist += distCalc3D.calcDist(prevLat, prevLon, prevEle, latitudes[i], longitudes[i], elevations[i]); else @@ -421,10 +477,8 @@ public double calcDistance( DistanceCalc calc ) * Takes the string from a json array ala [lon1,lat1], [lon2,lat2], ... and fills the list from * it. */ - public void parse2DJSON( String str ) - { - for (String latlon : str.split("\\[")) - { + public void parse2DJSON(String str) { + for (String latlon : str.split("\\[")) { if (latlon.trim().length() == 0) continue; @@ -434,161 +488,26 @@ public void parse2DJSON( String str ) } } - public GHPoint3D toGHPoint( int index ) - { + public GHPoint3D toGHPoint(int index) { return new GHPoint3D(getLatitude(index), getLongitude(index), getElevation(index)); } - public static final PointList EMPTY = new PointList(0, true) - { - @Override - public void set( int index, double lat, double lon, double ele ) - { - throw new RuntimeException("cannot change EMPTY PointList"); - } - - @Override - public void add( double lat, double lon, double ele ) - { - throw new RuntimeException("cannot change EMPTY PointList"); - } - - @Override - public double getLatitude( int index ) - { - throw new RuntimeException("cannot access EMPTY PointList"); - } - - @Override - public double getLongitude( int index ) - { - throw new RuntimeException("cannot access EMPTY PointList"); - } - - @Override - public boolean isEmpty() - { - return true; - } - - @Override - public void clear() - { - throw new RuntimeException("cannot change EMPTY PointList"); - } - - @Override - public void trimToSize( int newSize ) - { - throw new RuntimeException("cannot change EMPTY PointList"); - } - - @Override - public void parse2DJSON( String str ) - { - throw new RuntimeException("cannot change EMPTY PointList"); - } - - @Override - public double calcDistance( DistanceCalc calc ) - { - throw new UnsupportedOperationException("cannot access EMPTY PointList"); - } - - @Override - public PointList copy( int from, int end ) - { - throw new RuntimeException("cannot copy EMPTY PointList"); - } - - @Override - public PointList clone( boolean reverse ) - { - throw new UnsupportedOperationException("cannot access EMPTY PointList"); - } - - @Override - public double getElevation( int index ) - { - throw new UnsupportedOperationException("cannot access EMPTY PointList"); - } - - @Override - public double getLat( int index ) - { - throw new UnsupportedOperationException("cannot access EMPTY PointList"); - } - - @Override - public double getLon( int index ) - { - throw new UnsupportedOperationException("cannot access EMPTY PointList"); - } - - @Override - public double getEle( int index ) - { - throw new UnsupportedOperationException("cannot access EMPTY PointList"); - } - - @Override - public List toGeoJson() - { - throw new UnsupportedOperationException("cannot access EMPTY PointList"); - } - - @Override - public void reverse() - { - throw new UnsupportedOperationException("cannot change EMPTY PointList"); - } - - @Override - public int getSize() - { - return 0; - } - - @Override - public int size() - { - return 0; - } - - @Override - public GHPoint3D toGHPoint( int index ) - { - throw new UnsupportedOperationException("cannot access EMPTY PointList"); - } - - @Override - public boolean is3D() - { - throw new UnsupportedOperationException("cannot access EMPTY PointList"); - } - }; - - int getCapacity() - { + int getCapacity() { return latitudes.length; } @Override - public Iterator iterator() - { - return new Iterator() - { + public Iterator iterator() { + return new Iterator() { int counter = 0; @Override - public boolean hasNext() - { + public boolean hasNext() { return counter < getSize(); } @Override - public GHPoint3D next() - { + public GHPoint3D next() { if (counter >= getSize()) throw new NoSuchElementException(); @@ -598,8 +517,7 @@ public GHPoint3D next() } @Override - public void remove() - { + public void remove() { throw new UnsupportedOperationException("Not supported."); } }; diff --git a/core/src/main/java/com/graphhopper/util/ProgressListener.java b/core/src/main/java/com/graphhopper/util/ProgressListener.java index d47604bb4e1..1016aca2d61 100644 --- a/core/src/main/java/com/graphhopper/util/ProgressListener.java +++ b/core/src/main/java/com/graphhopper/util/ProgressListener.java @@ -20,7 +20,6 @@ /** * @author Peter Karich */ -public interface ProgressListener -{ - void update( long val ); +public interface ProgressListener { + void update(long val); } diff --git a/core/src/main/java/com/graphhopper/util/RoundaboutInstruction.java b/core/src/main/java/com/graphhopper/util/RoundaboutInstruction.java index e711cc460c5..9daf81b5837 100644 --- a/core/src/main/java/com/graphhopper/util/RoundaboutInstruction.java +++ b/core/src/main/java/com/graphhopper/util/RoundaboutInstruction.java @@ -23,67 +23,55 @@ /** * @author jansoe */ -public class RoundaboutInstruction extends Instruction -{ +public class RoundaboutInstruction extends Instruction { private int exitNumber = 0; // 0 undetermined, 1 clockwise, -1 counterclockwise, 2 inconsistent private int clockwise = 0; private boolean exited = false; private double radian = Double.NaN; - public RoundaboutInstruction( int sign, String name, InstructionAnnotation ia, PointList pl ) - { + public RoundaboutInstruction(int sign, String name, InstructionAnnotation ia, PointList pl) { super(sign, name, ia, pl); } - public RoundaboutInstruction increaseExitNumber() - { + public RoundaboutInstruction increaseExitNumber() { this.exitNumber += 1; return this; } - public RoundaboutInstruction setExitNumber( int exitNumber ) - { - this.exitNumber = exitNumber; - return this; - } - - public RoundaboutInstruction setDirOfRotation( double deltaIn ) - { - if (clockwise == 0) - { + public RoundaboutInstruction setDirOfRotation(double deltaIn) { + if (clockwise == 0) { clockwise = deltaIn > 0 ? 1 : -1; - } else - { + } else { int clockwise2 = deltaIn > 0 ? 1 : -1; - if (clockwise != clockwise2) - { + if (clockwise != clockwise2) { clockwise = 2; } } return this; } - public RoundaboutInstruction setExited() - { + public RoundaboutInstruction setExited() { exited = true; return this; } - public boolean isExited() - { + public boolean isExited() { return exited; } - public int getExitNumber() - { - if (exited && exitNumber == 0) - { + public int getExitNumber() { + if (exited && exitNumber == 0) { throw new IllegalStateException("RoundaboutInstruction must contain exitNumber>0"); } return exitNumber; } + public RoundaboutInstruction setExitNumber(int exitNumber) { + this.exitNumber = exitNumber; + return this; + } + /** * @return radian of angle -2PI < x < 2PI between roundabout entrance and exit values *

      @@ -92,8 +80,7 @@ public int getExitNumber() *
    • NaN if direction of rotation is unclear
    • *
    */ - public double getTurnAngle() - { + public double getTurnAngle() { if (Math.abs(clockwise) != 1) return Double.NaN; else @@ -103,15 +90,13 @@ public double getTurnAngle() /** * The radian value between entrance (in) and exit (out) of this roundabout. */ - public RoundaboutInstruction setRadian( double radian ) - { + public RoundaboutInstruction setRadian(double radian) { this.radian = radian; return this; } @Override - public Map getExtraInfoJSON() - { + public Map getExtraInfoJSON() { Map tmpMap = new HashMap(2); tmpMap.put("exit_number", getExitNumber()); double tmpAngle = getTurnAngle(); @@ -123,26 +108,21 @@ public Map getExtraInfoJSON() } @Override - public String getTurnDescription( Translation tr ) - { + public String getTurnDescription(Translation tr) { if (rawName) return getName(); String str; String streetName = getName(); int indi = getSign(); - if (indi == Instruction.USE_ROUNDABOUT) - { - if (!exited) - { + if (indi == Instruction.USE_ROUNDABOUT) { + if (!exited) { str = tr.tr("roundabout_enter"); - } else - { + } else { str = Helper.isEmpty(streetName) ? tr.tr("roundabout_exit", getExitNumber()) : tr.tr("roundabout_exit_onto", getExitNumber(), streetName); } - } else - { + } else { throw new IllegalStateException(indi + "no roundabout indication"); } return str; diff --git a/core/src/main/java/com/graphhopper/util/SimpleIntDeque.java b/core/src/main/java/com/graphhopper/util/SimpleIntDeque.java index 369d08905a3..3c7409fb58c 100644 --- a/core/src/main/java/com/graphhopper/util/SimpleIntDeque.java +++ b/core/src/main/java/com/graphhopper/util/SimpleIntDeque.java @@ -22,29 +22,25 @@ /** * push to end, pop from beginning *

    + * * @author Peter Karich */ -public class SimpleIntDeque -{ +public class SimpleIntDeque { private int[] arr; private float growFactor; private int frontIndex; private int endIndexPlusOne; - public SimpleIntDeque() - { + public SimpleIntDeque() { this(100, 2); } - public SimpleIntDeque( int initSize ) - { + public SimpleIntDeque(int initSize) { this(initSize, 2); } - public SimpleIntDeque( int initSize, float growFactor ) - { - if ((int) (initSize * growFactor) <= initSize) - { + public SimpleIntDeque(int initSize, float growFactor) { + if ((int) (initSize * growFactor) <= initSize) { throw new RuntimeException("initial size or increasing grow-factor too low!"); } @@ -52,30 +48,25 @@ public SimpleIntDeque( int initSize, float growFactor ) this.arr = new int[initSize]; } - int getCapacity() - { + int getCapacity() { return arr.length; } - public void setGrowFactor( float factor ) - { + public void setGrowFactor(float factor) { this.growFactor = factor; } - public boolean isEmpty() - { + public boolean isEmpty() { return frontIndex >= endIndexPlusOne; } - public int pop() - { + public int pop() { int tmp = arr[frontIndex]; frontIndex++; // removing the empty space of the front if too much is unused int smallerSize = (int) (arr.length / growFactor); - if (frontIndex > smallerSize) - { + if (frontIndex > smallerSize) { endIndexPlusOne = getSize(); // ensure that there are at least 10 entries int[] newArr = new int[endIndexPlusOne + 10]; @@ -87,15 +78,12 @@ public int pop() return tmp; } - public int getSize() - { + public int getSize() { return endIndexPlusOne - frontIndex; } - public void push( int v ) - { - if (endIndexPlusOne >= arr.length) - { + public void push(int v) { + if (endIndexPlusOne >= arr.length) { arr = Arrays.copyOf(arr, (int) (arr.length * growFactor)); } @@ -104,13 +92,10 @@ public void push( int v ) } @Override - public String toString() - { + public String toString() { StringBuilder sb = new StringBuilder(); - for (int i = frontIndex; i < endIndexPlusOne; i++) - { - if (i > frontIndex) - { + for (int i = frontIndex; i < endIndexPlusOne; i++) { + if (i > frontIndex) { sb.append(", "); } sb.append(arr[i]); diff --git a/core/src/main/java/com/graphhopper/util/StopWatch.java b/core/src/main/java/com/graphhopper/util/StopWatch.java index 784e9ca91fc..f32034478bd 100644 --- a/core/src/main/java/com/graphhopper/util/StopWatch.java +++ b/core/src/main/java/com/graphhopper/util/StopWatch.java @@ -20,37 +20,32 @@ /** * Make simple speed measurements possible. *

    + * * @author Peter Karich */ -public class StopWatch -{ +public class StopWatch { private long lastTime; private long nanoTime; private String name = ""; - public StopWatch( String name ) - { + public StopWatch(String name) { this.name = name; } - public StopWatch() - { + public StopWatch() { } - public StopWatch setName( String name ) - { + public StopWatch setName(String name) { this.name = name; return this; } - public StopWatch start() - { + public StopWatch start() { lastTime = System.nanoTime(); return this; } - public StopWatch stop() - { + public StopWatch stop() { if (lastTime < 0) return this; @@ -62,30 +57,25 @@ public StopWatch stop() /** * @return the time delta in milliseconds */ - public long getTime() - { + public long getTime() { return nanoTime / 1000000; } - public long getNanos() - { + public long getNanos() { return nanoTime; } @Override - public String toString() - { + public String toString() { String str = ""; - if (!Helper.isEmpty(name)) - { + if (!Helper.isEmpty(name)) { str += name + " "; } return str + "time:" + getSeconds(); } - public float getSeconds() - { + public float getSeconds() { return nanoTime / 1e9f; } } diff --git a/core/src/main/java/com/graphhopper/util/Translation.java b/core/src/main/java/com/graphhopper/util/Translation.java index 9717f82ad8d..002af95cdd3 100644 --- a/core/src/main/java/com/graphhopper/util/Translation.java +++ b/core/src/main/java/com/graphhopper/util/Translation.java @@ -23,10 +23,9 @@ /** * @author Peter Karich */ -public interface Translation -{ +public interface Translation { - String tr( String key, Object... params ); + String tr(String key, Object... params); Map asMap(); diff --git a/core/src/main/java/com/graphhopper/util/TranslationMap.java b/core/src/main/java/com/graphhopper/util/TranslationMap.java index eb8b13f5995..5d6360c9425 100644 --- a/core/src/main/java/com/graphhopper/util/TranslationMap.java +++ b/core/src/main/java/com/graphhopper/util/TranslationMap.java @@ -25,10 +25,10 @@ * A class which manages the translations in-memory. See here for more information: * ./docs/core/translations.md *

    + * * @author Peter Karich */ -public class TranslationMap -{ +public class TranslationMap { // ISO codes (639-1), use 'en_US' as reference private static final List LOCALES = Arrays.asList("ar", "ast", "bg", "ca", "cs_CZ", "da_DK", "de_DE", "el", "en_US", "es", "fa", "fil", "fi", @@ -37,23 +37,25 @@ public class TranslationMap "vi_VI", "zh_CN", "zh_HK"); private final Map translations = new HashMap(); + public static int countOccurence(String phrase, String splitter) { + if (Helper.isEmpty(phrase)) + return 0; + return phrase.trim().split(splitter).length; + } + /** * This loads the translation files from the specified folder. */ - public TranslationMap doImport( File folder ) - { - try - { - for (String locale : LOCALES) - { + public TranslationMap doImport(File folder) { + try { + for (String locale : LOCALES) { TranslationHashMap trMap = new TranslationHashMap(Helper.getLocale(locale)); trMap.doImport(new FileInputStream(new File(folder, locale + ".txt"))); add(trMap); } postImportHook(); return this; - } catch (Exception ex) - { + } catch (Exception ex) { throw new RuntimeException(ex); } } @@ -61,26 +63,21 @@ public TranslationMap doImport( File folder ) /** * This loads the translation files from classpath. */ - public TranslationMap doImport() - { - try - { - for (String locale : LOCALES) - { + public TranslationMap doImport() { + try { + for (String locale : LOCALES) { TranslationHashMap trMap = new TranslationHashMap(Helper.getLocale(locale)); trMap.doImport(TranslationMap.class.getResourceAsStream(locale + ".txt")); add(trMap); } postImportHook(); return this; - } catch (Exception ex) - { + } catch (Exception ex) { throw new RuntimeException(ex); } } - public void add( Translation tr ) - { + public void add(Translation tr) { Locale locale = tr.getLocale(); translations.put(locale.toString(), tr); if (!locale.getCountry().isEmpty() && !translations.containsKey(tr.getLanguage())) @@ -100,11 +97,9 @@ public void add( Translation tr ) * Returns the Translation object for the specified locale and falls back to english if the * locale was not found. */ - public Translation getWithFallBack( Locale locale ) - { + public Translation getWithFallBack(Locale locale) { Translation tr = get(locale.toString()); - if (tr == null) - { + if (tr == null) { tr = get(locale.getLanguage()); if (tr == null) tr = get("en"); @@ -115,8 +110,7 @@ public Translation getWithFallBack( Locale locale ) /** * Returns the Translation object for the specified locale and returns null if not found. */ - public Translation get( String locale ) - { + public Translation get(String locale) { locale = locale.replace("-", "_"); Translation tr = translations.get(locale); if (locale.contains("_") && tr == null) @@ -125,48 +119,33 @@ public Translation get( String locale ) return tr; } - public static int countOccurence( String phrase, String splitter ) - { - if (Helper.isEmpty(phrase)) - return 0; - return phrase.trim().split(splitter).length; - } - /** * This method does some checks and fills missing translation from en */ - private void postImportHook() - { + private void postImportHook() { Map enMap = get("en").asMap(); StringBuilder sb = new StringBuilder(); - for (Translation tr : translations.values()) - { + for (Translation tr : translations.values()) { Map trMap = tr.asMap(); - for (Entry enEntry : enMap.entrySet()) - { + for (Entry enEntry : enMap.entrySet()) { String value = trMap.get(enEntry.getKey()); - if (Helper.isEmpty(value)) - { + if (Helper.isEmpty(value)) { trMap.put(enEntry.getKey(), enEntry.getValue()); continue; } int expectedCount = countOccurence(enEntry.getValue(), "\\%"); - if (expectedCount != countOccurence(value, "\\%")) - { + if (expectedCount != countOccurence(value, "\\%")) { sb.append(tr.getLocale()).append(" - error in "). append(enEntry.getKey()).append("->"). append(value).append("\n"); - } else - { + } else { // try if formatting works, many times e.g. '%1$' instead of '%1$s' Object[] strs = new String[expectedCount]; Arrays.fill(strs, "tmp"); - try - { + try { String.format(value, strs); - } catch (Exception ex) - { + } catch (Exception ex) { sb.append(tr.getLocale()).append(" - error ").append(ex.getMessage()).append("in "). append(enEntry.getKey()).append("->"). append(value).append("\n"); @@ -175,43 +154,41 @@ private void postImportHook() } } - if (sb.length() > 0) - { + if (sb.length() > 0) { System.out.println(sb); throw new IllegalStateException(sb.toString()); } } - public static class TranslationHashMap implements Translation - { - private final Map map = new HashMap(); + @Override + public String toString() { + return translations.toString(); + } + + public static class TranslationHashMap implements Translation { final Locale locale; + private final Map map = new HashMap(); - public TranslationHashMap( Locale locale ) - { + public TranslationHashMap(Locale locale) { this.locale = locale; } - public void clear() - { + public void clear() { map.clear(); } @Override - public Locale getLocale() - { + public Locale getLocale() { return locale; } @Override - public String getLanguage() - { + public String getLanguage() { return locale.getLanguage(); } @Override - public String tr( String key, Object... params ) - { + public String tr(String key, Object... params) { String val = map.get(key.toLowerCase()); if (Helper.isEmpty(val)) return key; @@ -219,8 +196,7 @@ public String tr( String key, Object... params ) return String.format(val, params); } - public TranslationHashMap put( String key, String val ) - { + public TranslationHashMap put(String key, String val) { String existing = map.put(key.toLowerCase(), val); if (existing != null) throw new IllegalStateException("Cannot overwrite key " + key + " with " + val + ", was: " + existing); @@ -228,25 +204,20 @@ public TranslationHashMap put( String key, String val ) } @Override - public String toString() - { + public String toString() { return map.toString(); } @Override - public Map asMap() - { + public Map asMap() { return map; } - public TranslationHashMap doImport( InputStream is ) - { + public TranslationHashMap doImport(InputStream is) { if (is == null) throw new IllegalStateException("No input stream found in class path!?"); - try - { - for (String line : Helper.readFile(new InputStreamReader(is, Helper.UTF_CS))) - { + try { + for (String line : Helper.readFile(new InputStreamReader(is, Helper.UTF_CS))) { if (line.isEmpty() || line.startsWith("//") || line.startsWith("#")) continue; @@ -262,17 +233,10 @@ public TranslationHashMap doImport( InputStream is ) put(key, value); } - } catch (IOException ex) - { + } catch (IOException ex) { throw new RuntimeException(ex); } return this; } } - - @Override - public String toString() - { - return translations.toString(); - } } diff --git a/core/src/main/java/com/graphhopper/util/Unzipper.java b/core/src/main/java/com/graphhopper/util/Unzipper.java index 0d3f68f6626..0e3fccc8f08 100644 --- a/core/src/main/java/com/graphhopper/util/Unzipper.java +++ b/core/src/main/java/com/graphhopper/util/Unzipper.java @@ -24,16 +24,13 @@ /** * @author Peter Karich */ -public class Unzipper -{ - public void unzip( String from, boolean remove ) throws IOException - { +public class Unzipper { + public void unzip(String from, boolean remove) throws IOException { String to = Helper.pruneFileEnd(from); unzip(from, to, remove); } - public boolean unzip( String fromStr, String toStr, boolean remove ) throws IOException - { + public boolean unzip(String fromStr, String toStr, boolean remove) throws IOException { File from = new File(fromStr); if (!from.exists() || fromStr.equals(toStr)) return false; @@ -48,42 +45,34 @@ public boolean unzip( String fromStr, String toStr, boolean remove ) throws IOEx /** * @param progressListener updates not in percentage but the number of bytes already read. */ - public void unzip( InputStream fromIs, File toFolder, ProgressListener progressListener ) throws IOException - { + public void unzip(InputStream fromIs, File toFolder, ProgressListener progressListener) throws IOException { if (!toFolder.exists()) toFolder.mkdirs(); long sumBytes = 0; ZipInputStream zis = new ZipInputStream(fromIs); - try - { + try { ZipEntry ze = zis.getNextEntry(); byte[] buffer = new byte[8 * 1024]; - while (ze != null) - { - if (ze.isDirectory()) - { + while (ze != null) { + if (ze.isDirectory()) { new File(toFolder, ze.getName()).mkdir(); - } else - { + } else { double factor = 1; if (ze.getCompressedSize() > 0 && ze.getSize() > 0) factor = (double) ze.getCompressedSize() / ze.getSize(); File newFile = new File(toFolder, ze.getName()); FileOutputStream fos = new FileOutputStream(newFile); - try - { + try { int len; - while ((len = zis.read(buffer)) > 0) - { + while ((len = zis.read(buffer)) > 0) { fos.write(buffer, 0, len); sumBytes += len * factor; if (progressListener != null) progressListener.update(sumBytes); } - } finally - { + } finally { fos.close(); } } @@ -91,8 +80,7 @@ public void unzip( InputStream fromIs, File toFolder, ProgressListener progressL ze = zis.getNextEntry(); } zis.closeEntry(); - } finally - { + } finally { zis.close(); } } diff --git a/core/src/main/java/com/graphhopper/util/ViaInstruction.java b/core/src/main/java/com/graphhopper/util/ViaInstruction.java index 6ce7e55eea5..c2c45104dc7 100644 --- a/core/src/main/java/com/graphhopper/util/ViaInstruction.java +++ b/core/src/main/java/com/graphhopper/util/ViaInstruction.java @@ -20,38 +20,32 @@ /** * @author Peter Karich */ -public class ViaInstruction extends Instruction -{ +public class ViaInstruction extends Instruction { private int viaPosition = -1; - public ViaInstruction( String name, InstructionAnnotation ia, PointList pl ) - { + public ViaInstruction(String name, InstructionAnnotation ia, PointList pl) { super(REACHED_VIA, name, ia, pl); } - public ViaInstruction( Instruction instr ) - { + public ViaInstruction(Instruction instr) { this(instr.getName(), instr.getAnnotation(), instr.getPoints()); setDistance(instr.getDistance()); setTime(instr.getTime()); } - public void setViaCount( int count ) - { - this.viaPosition = count; - } - - public int getViaCount() - { + public int getViaCount() { if (viaPosition < 0) throw new IllegalStateException("Uninitialized via count in instruction " + getName()); return viaPosition; } + public void setViaCount(int count) { + this.viaPosition = count; + } + @Override - public String getTurnDescription( Translation tr ) - { + public String getTurnDescription(Translation tr) { if (rawName) return getName(); diff --git a/core/src/main/java/com/graphhopper/util/XFirstSearch.java b/core/src/main/java/com/graphhopper/util/XFirstSearch.java index 465852afa72..d0fa5af64ca 100644 --- a/core/src/main/java/com/graphhopper/util/XFirstSearch.java +++ b/core/src/main/java/com/graphhopper/util/XFirstSearch.java @@ -23,24 +23,21 @@ /** * This abstract class defines commonalities for BFS and DFS *

    + * * @author Jan Sölter */ -public abstract class XFirstSearch -{ - protected GHBitSet createBitSet() - { +public abstract class XFirstSearch { + protected GHBitSet createBitSet() { return new GHBitSetImpl(); } - public abstract void start( EdgeExplorer explorer, int startNode ); + public abstract void start(EdgeExplorer explorer, int startNode); - protected boolean goFurther( int nodeId ) - { + protected boolean goFurther(int nodeId) { return true; } - protected boolean checkAdjacent( EdgeIteratorState edge ) - { + protected boolean checkAdjacent(EdgeIteratorState edge) { return true; } } diff --git a/core/src/main/java/com/graphhopper/util/exceptions/CannotFindPointException.java b/core/src/main/java/com/graphhopper/util/exceptions/CannotFindPointException.java index 2d9e2c2d2aa..96307ed8e4f 100644 --- a/core/src/main/java/com/graphhopper/util/exceptions/CannotFindPointException.java +++ b/core/src/main/java/com/graphhopper/util/exceptions/CannotFindPointException.java @@ -18,7 +18,6 @@ package com.graphhopper.util.exceptions; import java.util.Collections; -import java.util.HashMap; import java.util.Map; /** @@ -27,25 +26,21 @@ * * @author Robin Boldt */ -public class CannotFindPointException extends IllegalArgumentException implements GHException -{ +public class CannotFindPointException extends IllegalArgumentException implements GHException { protected final int pointIndex; - public CannotFindPointException( String var1, int pointIndex ) - { + public CannotFindPointException(String var1, int pointIndex) { super(var1); this.pointIndex = pointIndex; } - public int getPointIndex() - { + public int getPointIndex() { return this.pointIndex; } @Override - public Map getDetails() - { + public Map getDetails() { return Collections.singletonMap("point_index", String.valueOf(pointIndex)); } } diff --git a/core/src/main/java/com/graphhopper/util/exceptions/GHException.java b/core/src/main/java/com/graphhopper/util/exceptions/GHException.java index c2d6bdd913c..b510c8fd2b5 100644 --- a/core/src/main/java/com/graphhopper/util/exceptions/GHException.java +++ b/core/src/main/java/com/graphhopper/util/exceptions/GHException.java @@ -24,7 +24,6 @@ * * @author Robin Boldt */ -public interface GHException -{ +public interface GHException { public abstract Map getDetails(); } diff --git a/core/src/main/java/com/graphhopper/util/exceptions/PointOutOfBoundsException.java b/core/src/main/java/com/graphhopper/util/exceptions/PointOutOfBoundsException.java index 56707b1b3c4..c7f1b37bae1 100644 --- a/core/src/main/java/com/graphhopper/util/exceptions/PointOutOfBoundsException.java +++ b/core/src/main/java/com/graphhopper/util/exceptions/PointOutOfBoundsException.java @@ -1,17 +1,12 @@ package com.graphhopper.util.exceptions; -import java.util.HashMap; -import java.util.Map; - /** * Refinement of the CannotFindPointException that indicates that a point is placed out of the graphs bounds * * @author Robin Boldt */ -public class PointOutOfBoundsException extends CannotFindPointException -{ - public PointOutOfBoundsException( String var1, int pointIndex ) - { +public class PointOutOfBoundsException extends CannotFindPointException { + public PointOutOfBoundsException(String var1, int pointIndex) { super(var1, pointIndex); } } diff --git a/core/src/main/java/com/graphhopper/util/shapes/BBox.java b/core/src/main/java/com/graphhopper/util/shapes/BBox.java index 3ce4f550f81..977c0162bbe 100644 --- a/core/src/main/java/com/graphhopper/util/shapes/BBox.java +++ b/core/src/main/java/com/graphhopper/util/shapes/BBox.java @@ -31,11 +31,12 @@ * Nice German overview: * http://www.geoinf.uni-jena.de/fileadmin/Geoinformatik/Lehre/Diplomarbeiten/DA_Andres.pdf *

    + * * @author Peter Karich */ -public class BBox implements Shape, Cloneable -{ +public class BBox implements Shape, Cloneable { + private final boolean elevation; // longitude (theta) = x, latitude (phi) = y, elevation = z public double minLon; public double maxLon; @@ -43,20 +44,16 @@ public class BBox implements Shape, Cloneable public double maxLat; public double minEle; public double maxEle; - private final boolean elevation; - public BBox( double minLon, double maxLon, double minLat, double maxLat ) - { + public BBox(double minLon, double maxLon, double minLat, double maxLat) { this(minLon, maxLon, minLat, maxLat, Double.NaN, Double.NaN, false); } - public BBox( double minLon, double maxLon, double minLat, double maxLat, double minEle, double maxEle ) - { + public BBox(double minLon, double maxLon, double minLat, double maxLat, double minEle, double maxEle) { this(minLon, maxLon, minLat, maxLat, minEle, maxEle, true); } - public BBox( double minLon, double maxLon, double minLat, double maxLat, double minEle, double maxEle, boolean elevation ) - { + public BBox(double minLon, double maxLon, double minLat, double maxLat, double minEle, double maxEle, boolean elevation) { this.elevation = elevation; this.maxLat = maxLat; this.minLon = minLon; @@ -66,63 +63,49 @@ public BBox( double minLon, double maxLon, double minLat, double maxLat, double this.maxEle = maxEle; } - public boolean hasElevation() - { - return elevation; - } - /** * Prefills BBox with minimum values so that it can increase. */ - public static BBox createInverse( boolean elevation ) - { - if (elevation) - { + public static BBox createInverse(boolean elevation) { + if (elevation) { return new BBox(Double.MAX_VALUE, -Double.MAX_VALUE, Double.MAX_VALUE, -Double.MAX_VALUE, Double.MAX_VALUE, -Double.MAX_VALUE, true); - } else - { + } else { return new BBox(Double.MAX_VALUE, -Double.MAX_VALUE, Double.MAX_VALUE, -Double.MAX_VALUE, Double.NaN, Double.NaN, false); } } - public void update( double lat, double lon ) - { - if (lat > maxLat) - { + public boolean hasElevation() { + return elevation; + } + + public void update(double lat, double lon) { + if (lat > maxLat) { maxLat = lat; } - if (lat < minLat) - { + if (lat < minLat) { minLat = lat; } - if (lon > maxLon) - { + if (lon > maxLon) { maxLon = lon; } - if (lon < minLon) - { + if (lon < minLon) { minLon = lon; } } - public void update( double lat, double lon, double elev ) - { - if (elevation) - { - if (elev > maxEle) - { + public void update(double lat, double lon, double elev) { + if (elevation) { + if (elev > maxEle) { maxEle = elev; } - if (elev < minEle) - { + if (elev < minEle) { minEle = elev; } - } else - { + } else { throw new IllegalStateException("No BBox with elevation to update"); } update(lat, lon); @@ -130,19 +113,15 @@ public void update( double lat, double lon, double elev ) } @Override - public BBox clone() - { + public BBox clone() { return new BBox(minLon, maxLon, minLat, maxLat, minEle, maxEle, elevation); } @Override - public boolean intersect( Shape s ) - { - if (s instanceof BBox) - { + public boolean intersect(Shape s) { + if (s instanceof BBox) { return intersect((BBox) s); - } else if (s instanceof Circle) - { + } else if (s instanceof Circle) { return ((Circle) s).intersect(this); } @@ -150,50 +129,41 @@ public boolean intersect( Shape s ) } @Override - public boolean contains( Shape s ) - { - if (s instanceof BBox) - { + public boolean contains(Shape s) { + if (s instanceof BBox) { return contains((BBox) s); - } else if (s instanceof Circle) - { + } else if (s instanceof Circle) { return contains((Circle) s); } throw new UnsupportedOperationException("unsupported shape"); } - public boolean intersect( Circle s ) - { + public boolean intersect(Circle s) { return ((Circle) s).intersect(this); } - public boolean intersect( BBox o ) - { + public boolean intersect(BBox o) { // return (o.minLon < minLon && o.maxLon > minLon || o.minLon < maxLon && o.minLon >= minLon) // && (o.maxLat < maxLat && o.maxLat >= minLat || o.maxLat >= maxLat && o.minLat < maxLat); return minLon < o.maxLon && minLat < o.maxLat && o.minLon < maxLon && o.minLat < maxLat; } @Override - public boolean contains( double lat, double lon ) - { + public boolean contains(double lat, double lon) { return lat <= maxLat && lat >= minLat && lon <= maxLon && lon >= minLon; } - public boolean contains( BBox b ) - { + public boolean contains(BBox b) { return maxLat >= b.maxLat && minLat <= b.minLat && maxLon >= b.maxLon && minLon <= b.minLon; } - public boolean contains( Circle c ) - { + public boolean contains(Circle c) { return contains(c.getBounds()); } @Override - public String toString() - { + public String toString() { String str = minLon + "," + maxLon + "," + minLat + "," + maxLat; if (elevation) str += "," + minEle + "," + maxEle; @@ -201,20 +171,17 @@ public String toString() return str; } - public String toLessPrecisionString() - { + public String toLessPrecisionString() { return (float) minLon + "," + (float) maxLon + "," + (float) minLat + "," + (float) maxLat; } @Override - public BBox getBounds() - { + public BBox getBounds() { return this; } @Override - public boolean equals( Object obj ) - { + public boolean equals(Object obj) { if (obj == null) return false; @@ -225,8 +192,7 @@ public boolean equals( Object obj ) } @Override - public int hashCode() - { + public int hashCode() { int hash = 3; hash = 17 * hash + (int) (Double.doubleToLongBits(this.minLon) ^ (Double.doubleToLongBits(this.minLon) >>> 32)); hash = 17 * hash + (int) (Double.doubleToLongBits(this.maxLon) ^ (Double.doubleToLongBits(this.maxLon) >>> 32)); @@ -235,8 +201,7 @@ public int hashCode() return hash; } - public boolean isValid() - { + public boolean isValid() { // second longitude should be bigger than the first if (minLon >= maxLon) return false; @@ -245,8 +210,7 @@ public boolean isValid() if (minLat >= maxLat) return false; - if (elevation) - { + if (elevation) { // equal elevation is okay if (minEle > maxEle) return false; @@ -266,8 +230,7 @@ public boolean isValid() * @return array containing this bounding box. Attention: GeoJson is lon,lat! If 3D is gets even * worse: lon,lat,ele */ - public List toGeoJson() - { + public List toGeoJson() { List list = new ArrayList(4); list.add(Helper.round6(minLon)); list.add(Helper.round6(minLat)); diff --git a/core/src/main/java/com/graphhopper/util/shapes/Circle.java b/core/src/main/java/com/graphhopper/util/shapes/Circle.java index 83e1bdbe0f1..f538b3fae62 100644 --- a/core/src/main/java/com/graphhopper/util/shapes/Circle.java +++ b/core/src/main/java/com/graphhopper/util/shapes/Circle.java @@ -23,22 +23,19 @@ /** * @author Peter Karich */ -public class Circle implements Shape -{ - private DistanceCalc calc = Helper.DIST_EARTH; +public class Circle implements Shape { private final double radiusInKm; private final double lat; private final double lon; private final double normedDist; private final BBox bbox; + private DistanceCalc calc = Helper.DIST_EARTH; - public Circle( double lat, double lon, double radiusInMeter ) - { + public Circle(double lat, double lon, double radiusInMeter) { this(lat, lon, radiusInMeter, Helper.DIST_EARTH); } - public Circle( double lat, double lon, double radiusInMeter, DistanceCalc calc ) - { + public Circle(double lat, double lon, double radiusInMeter, DistanceCalc calc) { this.calc = calc; this.lat = lat; this.lon = lon; @@ -47,41 +44,33 @@ public Circle( double lat, double lon, double radiusInMeter, DistanceCalc calc ) bbox = calc.createBBox(lat, lon, radiusInMeter); } - public double getLat() - { + public double getLat() { return lat; } - public double getLon() - { + public double getLon() { return lon; } @Override - public boolean contains( double lat1, double lon1 ) - { + public boolean contains(double lat1, double lon1) { return normDist(lat1, lon1) <= normedDist; } @Override - public BBox getBounds() - { + public BBox getBounds() { return bbox; } - private double normDist( double lat1, double lon1 ) - { + private double normDist(double lat1, double lon1) { return calc.calcNormalizedDist(lat, lon, lat1, lon1); } @Override - public boolean intersect( Shape o ) - { - if (o instanceof Circle) - { + public boolean intersect(Shape o) { + if (o instanceof Circle) { return intersect((Circle) o); - } else if (o instanceof BBox) - { + } else if (o instanceof BBox) { return intersect((BBox) o); } @@ -89,76 +78,60 @@ public boolean intersect( Shape o ) } @Override - public boolean contains( Shape o ) - { - if (o instanceof Circle) - { + public boolean contains(Shape o) { + if (o instanceof Circle) { return contains((Circle) o); - } else if (o instanceof BBox) - { + } else if (o instanceof BBox) { return contains((BBox) o); } throw new UnsupportedOperationException("unsupported shape"); } - public boolean intersect( BBox b ) - { + public boolean intersect(BBox b) { // test top intersect - if (lat > b.maxLat) - { - if (lon < b.minLon) - { + if (lat > b.maxLat) { + if (lon < b.minLon) { return normDist(b.maxLat, b.minLon) <= normedDist; } - if (lon > b.maxLon) - { + if (lon > b.maxLon) { return normDist(b.maxLat, b.maxLon) <= normedDist; } return b.maxLat - bbox.minLat > 0; } // test bottom intersect - if (lat < b.minLat) - { - if (lon < b.minLon) - { + if (lat < b.minLat) { + if (lon < b.minLon) { return normDist(b.minLat, b.minLon) <= normedDist; } - if (lon > b.maxLon) - { + if (lon > b.maxLon) { return normDist(b.minLat, b.maxLon) <= normedDist; } return bbox.maxLat - b.minLat > 0; } // test middle intersect - if (lon < b.minLon) - { + if (lon < b.minLon) { return bbox.maxLon - b.minLon > 0; } - if (lon > b.maxLon) - { + if (lon > b.maxLon) { return b.maxLon - bbox.minLon > 0; } return true; } - public boolean intersect( Circle c ) - { + public boolean intersect(Circle c) { // necessary to improve speed? - if (!getBounds().intersect(c.getBounds())) - { + if (!getBounds().intersect(c.getBounds())) { return false; } return normDist(c.lat, c.lon) <= calc.calcNormalizedDist(radiusInKm + c.radiusInKm); } - public boolean contains( BBox b ) - { - if (bbox.contains(b)) - { + public boolean contains(BBox b) { + if (bbox.contains(b)) { return contains(b.maxLat, b.minLon) && contains(b.minLat, b.minLon) && contains(b.maxLat, b.maxLon) && contains(b.minLat, b.maxLon); } @@ -166,11 +139,9 @@ public boolean contains( BBox b ) return false; } - public boolean contains( Circle c ) - { + public boolean contains(Circle c) { double res = radiusInKm - c.radiusInKm; - if (res < 0) - { + if (res < 0) { return false; } @@ -178,8 +149,7 @@ public boolean contains( Circle c ) } @Override - public String toString() - { + public String toString() { return lat + "," + lon + ", radius:" + radiusInKm; } } diff --git a/core/src/main/java/com/graphhopper/util/shapes/GHPlace.java b/core/src/main/java/com/graphhopper/util/shapes/GHPlace.java index f3038a0f168..8d13b1a595f 100644 --- a/core/src/main/java/com/graphhopper/util/shapes/GHPlace.java +++ b/core/src/main/java/com/graphhopper/util/shapes/GHPlace.java @@ -22,50 +22,42 @@ /** * Specifies a place by its coordinates or name *

    + * * @author Peter Karich */ -public class GHPlace extends GHPoint -{ +public class GHPlace extends GHPoint { private String name = ""; - public GHPlace() - { + public GHPlace() { } - public GHPlace( String name ) - { + public GHPlace(String name) { setName(name); } - public GHPlace( double lat, double lon ) - { + public GHPlace(double lat, double lon) { super(lat, lon); } - public void setValue( String t ) - { + public void setValue(String t) { setName(t); } - public GHPlace setName( String name ) - { - this.name = name; - return this; + public String getName() { + return name; } - public String getName() - { - return name; + public GHPlace setName(String name) { + this.name = name; + return this; } - public boolean isValidName() - { + public boolean isValidName() { return !Helper.isEmpty(name); } @Override - public String toString() - { + public String toString() { String str = ""; if (isValidName()) str += name; diff --git a/core/src/main/java/com/graphhopper/util/shapes/GHPoint.java b/core/src/main/java/com/graphhopper/util/shapes/GHPoint.java index 4c280f492be..af751414d98 100644 --- a/core/src/main/java/com/graphhopper/util/shapes/GHPoint.java +++ b/core/src/main/java/com/graphhopper/util/shapes/GHPoint.java @@ -22,39 +22,46 @@ /** * @author Peter Karich */ -public class GHPoint -{ +public class GHPoint { public double lat = Double.NaN; public double lon = Double.NaN; - public GHPoint() - { + public GHPoint() { } - public GHPoint( double lat, double lon ) - { + public GHPoint(double lat, double lon) { this.lat = lat; this.lon = lon; } - public double getLon() - { + public static GHPoint parse(String str) { + // if the point is in the format of lat,lon we don't need to call geocoding service + String[] fromStrs = str.split(","); + if (fromStrs.length == 2) { + try { + double fromLat = Double.parseDouble(fromStrs[0]); + double fromLon = Double.parseDouble(fromStrs[1]); + return new GHPoint(fromLat, fromLon); + } catch (Exception ex) { + } + } + return null; + } + + public double getLon() { return lon; } - public double getLat() - { + public double getLat() { return lat; } - public boolean isValid() - { + public boolean isValid() { return !Double.isNaN(lat) && !Double.isNaN(lon); } @Override - public int hashCode() - { + public int hashCode() { int hash = 7; hash = 83 * hash + (int) (Double.doubleToLongBits(this.lat) ^ (Double.doubleToLongBits(this.lat) >>> 32)); hash = 83 * hash + (int) (Double.doubleToLongBits(this.lon) ^ (Double.doubleToLongBits(this.lon) >>> 32)); @@ -62,8 +69,7 @@ public int hashCode() } @Override - public boolean equals( Object obj ) - { + public boolean equals(Object obj) { if (obj == null) return false; @@ -73,37 +79,14 @@ public boolean equals( Object obj ) } @Override - public String toString() - { + public String toString() { return lat + "," + lon; } /** * Attention: geoJson is LON,LAT */ - public Double[] toGeoJson() - { - return new Double[] - { - lon, lat - }; - } - - public static GHPoint parse( String str ) - { - // if the point is in the format of lat,lon we don't need to call geocoding service - String[] fromStrs = str.split(","); - if (fromStrs.length == 2) - { - try - { - double fromLat = Double.parseDouble(fromStrs[0]); - double fromLon = Double.parseDouble(fromStrs[1]); - return new GHPoint(fromLat, fromLon); - } catch (Exception ex) - { - } - } - return null; + public Double[] toGeoJson() { + return new Double[]{lon, lat}; } } diff --git a/core/src/main/java/com/graphhopper/util/shapes/GHPoint3D.java b/core/src/main/java/com/graphhopper/util/shapes/GHPoint3D.java index 3b4f1aa1f7d..f138baf6619 100644 --- a/core/src/main/java/com/graphhopper/util/shapes/GHPoint3D.java +++ b/core/src/main/java/com/graphhopper/util/shapes/GHPoint3D.java @@ -22,37 +22,31 @@ /** * @author Peter Karich */ -public class GHPoint3D extends GHPoint -{ +public class GHPoint3D extends GHPoint { public double ele; - public GHPoint3D( double lat, double lon, double elevation ) - { + public GHPoint3D(double lat, double lon, double elevation) { super(lat, lon); this.ele = elevation; } - public double getElevation() - { + public double getElevation() { return ele; } - public double getEle() - { + public double getEle() { return ele; } @Override - public int hashCode() - { + public int hashCode() { int hash = 59 * super.hashCode() + (int) (Double.doubleToLongBits(this.ele) ^ (Double.doubleToLongBits(this.ele) >>> 32)); return hash; } @Override - public boolean equals( Object obj ) - { + public boolean equals(Object obj) { if (obj == null) return false; @@ -67,17 +61,12 @@ public boolean equals( Object obj ) } @Override - public String toString() - { + public String toString() { return super.toString() + "," + ele; } @Override - public Double[] toGeoJson() - { - return new Double[] - { - lon, lat, ele - }; + public Double[] toGeoJson() { + return new Double[]{lon, lat, ele}; } } diff --git a/core/src/main/java/com/graphhopper/util/shapes/Shape.java b/core/src/main/java/com/graphhopper/util/shapes/Shape.java index 5fdc9046424..1dace9efd84 100644 --- a/core/src/main/java/com/graphhopper/util/shapes/Shape.java +++ b/core/src/main/java/com/graphhopper/util/shapes/Shape.java @@ -20,25 +20,25 @@ /** * A shape interface to implement circles or rectangles. *

    + * * @author Peter Karich */ -public interface Shape -{ +public interface Shape { /** * @return true if edges or areas of this and the specified shapes overlap */ - boolean intersect( Shape o ); + boolean intersect(Shape o); /** * @return true only if lat and lon are inside (or on the edge) of this shape */ - boolean contains( double lat, double lon ); + boolean contains(double lat, double lon); /** * @return true if the specified shape is fully contained in this shape. Only iff *

     s1.contains(s2) && s2.contains(s1) 
    then s1 is equal to s2 */ - boolean contains( Shape s ); + boolean contains(Shape s); /** * @return the minimal rectangular bounding box of this shape diff --git a/core/src/test/java/com/graphhopper/GHRequestTest.java b/core/src/test/java/com/graphhopper/GHRequestTest.java index 7aa8382f5e4..0a0c6d64d93 100644 --- a/core/src/test/java/com/graphhopper/GHRequestTest.java +++ b/core/src/test/java/com/graphhopper/GHRequestTest.java @@ -29,11 +29,9 @@ /** * @author Peter Karich */ -public class GHRequestTest -{ +public class GHRequestTest { @Test - public void testGetHint() - { + public void testGetHint() { GHRequest instance = new GHRequest(10, 12, 12, 10); instance.getHints().put("something", "1"); assertEquals(1, instance.getHints().getInt("something", 2)); @@ -42,8 +40,7 @@ public void testGetHint() } @Test - public void testCorrectInit() - { + public void testCorrectInit() { double lat0 = 51, lon0 = 1, lat1 = 52, lon1 = 2, lat2 = 53, lon2 = 3; ArrayList points = new ArrayList(3); @@ -97,10 +94,8 @@ public void testCorrectInit() compareFavoredHeadings(instance, emptyHeadings); } - private void compareFavoredHeadings( GHRequest request, List expected ) - { - for (int ind = 0; ind < expected.size(); ind++) - { + private void compareFavoredHeadings(GHRequest request, List expected) { + for (int ind = 0; ind < expected.size(); ind++) { double favoredHeading = request.getFavoredHeading(ind); assertEquals(ind + " favored Heading does not match" + expected.get(ind) + " vs ." + favoredHeading, expected.get(ind), favoredHeading, 0.01); diff --git a/core/src/test/java/com/graphhopper/GHResponseTest.java b/core/src/test/java/com/graphhopper/GHResponseTest.java index 3c0e088ce7d..ec3b23c37cf 100644 --- a/core/src/test/java/com/graphhopper/GHResponseTest.java +++ b/core/src/test/java/com/graphhopper/GHResponseTest.java @@ -17,20 +17,19 @@ */ package com.graphhopper; -import static org.junit.Assert.*; import org.junit.Test; -public class GHResponseTest -{ +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; + +public class GHResponseTest { @Test - public void testToString() throws Exception - { + public void testToString() throws Exception { assertEquals("no paths", new GHResponse().toString()); } @Test - public void testHasNoErrorIfEmpty() throws Exception - { + public void testHasNoErrorIfEmpty() throws Exception { assertFalse(new GHResponse().hasErrors()); GHResponse rsp = new GHResponse(); rsp.add(new PathWrapper()); diff --git a/core/src/test/java/com/graphhopper/coll/AbstractBinHeapTest.java b/core/src/test/java/com/graphhopper/coll/AbstractBinHeapTest.java index 29b39acfb6e..8e66d12f936 100644 --- a/core/src/test/java/com/graphhopper/coll/AbstractBinHeapTest.java +++ b/core/src/test/java/com/graphhopper/coll/AbstractBinHeapTest.java @@ -19,24 +19,21 @@ import com.graphhopper.storage.SPTEntry; import com.graphhopper.util.EdgeIterator; +import org.junit.Test; import java.util.PriorityQueue; import java.util.Random; -import org.junit.Test; - -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; /** * @author Peter Karich */ -public abstract class AbstractBinHeapTest -{ - public abstract BinHeapWrapper createHeap( int capacity ); +public abstract class AbstractBinHeapTest { + public abstract BinHeapWrapper createHeap(int capacity); @Test - public void test0() - { + public void test0() { BinHeapWrapper binHeap = createHeap(100); binHeap.insert(123, 0); assertEquals(123, binHeap.peekKey().intValue()); @@ -48,8 +45,7 @@ public void test0() } @Test - public void testBasic() - { + public void testBasic() { BinHeapWrapper binHeap = createHeap(100); binHeap.insert(20, 1); binHeap.insert(123, 2); @@ -63,8 +59,7 @@ public void testBasic() } @Test - public void testClear() - { + public void testClear() { BinHeapWrapper binHeap = createHeap(100); binHeap.insert(20, 1); binHeap.insert(123, 2); @@ -78,15 +73,13 @@ public void testClear() } @Test - public void testSpreading() - { + public void testSpreading() { BinHeapWrapper binHeap = createHeap(100); binHeap.insert(100, 101); binHeap.insert(49, 51); binHeap.insert(71, 71); binHeap.insert(29, 31); - for (int i = 0; i < 20; i++) - { + for (int i = 0; i < 20; i++) { binHeap.insert(i * 10, i * 11); } binHeap.insert(59, 61); @@ -108,8 +101,7 @@ public void testSpreading() } @Test - public void testRekey() - { + public void testRekey() { BinHeapWrapper binHeap = createHeap(100); binHeap.insert(20, 1); binHeap.insert(123, 2); @@ -123,15 +115,13 @@ public void testRekey() } @Test - public void testSize() - { + public void testSize() { PriorityQueue juQueue = new PriorityQueue(100); BinHeapWrapper binHeap = createHeap(100); Random rand = new Random(1); int N = 1000; - for (int i = 0; i < N; i++) - { + for (int i = 0; i < N; i++) { int val = rand.nextInt(); binHeap.insert(val, i); juQueue.add(new SPTEntry(EdgeIterator.NO_EDGE, i, val)); @@ -139,8 +129,7 @@ public void testSize() assertEquals(juQueue.size(), binHeap.getSize()); - for (int i = 0; i < N; i++) - { + for (int i = 0; i < N; i++) { assertEquals(juQueue.poll().adjNode, binHeap.pollElement(), 1e-5); } diff --git a/core/src/test/java/com/graphhopper/coll/AbstractMyBitSetTest.java b/core/src/test/java/com/graphhopper/coll/AbstractMyBitSetTest.java index 6dcdb64eb0f..c23638cd985 100644 --- a/core/src/test/java/com/graphhopper/coll/AbstractMyBitSetTest.java +++ b/core/src/test/java/com/graphhopper/coll/AbstractMyBitSetTest.java @@ -17,20 +17,18 @@ */ package com.graphhopper.coll; -import static org.junit.Assert.*; - import org.junit.Test; +import static org.junit.Assert.*; + /** * @author Peter Karich */ -public abstract class AbstractMyBitSetTest -{ - public abstract GHBitSet createBitSet( int no ); +public abstract class AbstractMyBitSetTest { + public abstract GHBitSet createBitSet(int no); @Test - public void testCopy() - { + public void testCopy() { GHBitSet bs = createBitSet(100); bs.add(100); bs.add(70); @@ -57,8 +55,7 @@ public void testCopy() } @Test - public void testToString() - { + public void testToString() { GHBitSet bs = createBitSet(100); bs.add(12); bs.add(1); @@ -66,8 +63,7 @@ public void testToString() } @Test - public void testNext() - { + public void testNext() { GHBitSet bs = createBitSet(100); bs.add(7); bs.add(90); @@ -78,18 +74,15 @@ public void testNext() } @Test - public void testEnsureCapacity() - { + public void testEnsureCapacity() { GHBitSet bs = createBitSet(8); bs.add(7); - try - { + try { bs.add(8); assertTrue(false); - } catch (Throwable ex) - { + } catch (Throwable ex) { } - + bs.add(8); bs.add(9); assertFalse(bs.contains(6)); @@ -98,8 +91,7 @@ public void testEnsureCapacity() } @Test - public void testClear() - { + public void testClear() { GHBitSet bs = createBitSet(100); bs.add(12); bs.add(1); diff --git a/core/src/test/java/com/graphhopper/coll/BitSetImplTest.java b/core/src/test/java/com/graphhopper/coll/BitSetImplTest.java index 36858c71e5f..38ccabf19b3 100644 --- a/core/src/test/java/com/graphhopper/coll/BitSetImplTest.java +++ b/core/src/test/java/com/graphhopper/coll/BitSetImplTest.java @@ -20,11 +20,9 @@ /** * @author Peter Karich */ -public class BitSetImplTest extends AbstractMyBitSetTest -{ +public class BitSetImplTest extends AbstractMyBitSetTest { @Override - public GHBitSet createBitSet( int no ) - { + public GHBitSet createBitSet(int no) { return new GHBitSetImpl(no); } } diff --git a/core/src/test/java/com/graphhopper/coll/CompressedArrayTest.java b/core/src/test/java/com/graphhopper/coll/CompressedArrayTest.java index 2dddafae091..1f447f9c7a6 100644 --- a/core/src/test/java/com/graphhopper/coll/CompressedArrayTest.java +++ b/core/src/test/java/com/graphhopper/coll/CompressedArrayTest.java @@ -18,21 +18,19 @@ package com.graphhopper.coll; import com.graphhopper.util.shapes.GHPoint; +import org.junit.Test; import java.util.Random; -import org.junit.Test; - -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; /** * @author Peter Karich */ -public class CompressedArrayTest -{ +public class CompressedArrayTest { @Test - public void testCompress() throws Exception - { + public void testCompress() throws Exception { CompressedArray arr = new CompressedArray(); arr.write(10, 1); arr.write(11, 2); @@ -56,12 +54,10 @@ public void testCompress() throws Exception } @Test - public void testCompress2() throws Exception - { + public void testCompress2() throws Exception { CompressedArray arr = new CompressedArray(); Random rand = new Random(0); - for (int i = 0; i < 10000; i++) - { + for (int i = 0; i < 10000; i++) { arr.write(i / 1000.0, rand.nextDouble() * 90); } diff --git a/core/src/test/java/com/graphhopper/coll/GHLongIntBTreeTest.java b/core/src/test/java/com/graphhopper/coll/GHLongIntBTreeTest.java index 7a0b23069ad..797f2a63898 100644 --- a/core/src/test/java/com/graphhopper/coll/GHLongIntBTreeTest.java +++ b/core/src/test/java/com/graphhopper/coll/GHLongIntBTreeTest.java @@ -17,35 +17,30 @@ */ package com.graphhopper.coll; +import org.junit.Test; + import java.util.LinkedHashSet; import java.util.Random; import java.util.Set; -import org.junit.Test; - import static org.junit.Assert.*; /** * @author Peter Karich */ -public class GHLongIntBTreeTest -{ +public class GHLongIntBTreeTest { @Test - public void testThrowException_IfPutting_NoNumber() - { + public void testThrowException_IfPutting_NoNumber() { GHLongIntBTree instance = new GHLongIntBTree(2); - try - { + try { instance.put(-1, 1); assertTrue(false); - } catch (Exception ex) - { + } catch (Exception ex) { } } @Test - public void testTwoSplits() - { + public void testTwoSplits() { GHLongIntBTree instance = new GHLongIntBTree(3); instance.put(1, 2); instance.put(2, 4); @@ -73,8 +68,7 @@ public void testTwoSplits() } @Test - public void testSplitAndOverwrite() - { + public void testSplitAndOverwrite() { GHLongIntBTree instance = new GHLongIntBTree(3); instance.put(1, 2); instance.put(2, 4); @@ -88,17 +82,14 @@ public void testSplitAndOverwrite() assertEquals(6, instance.get(3)); } - void check( GHLongIntBTree instance, int from ) - { - for (int i = from; i < instance.getSize(); i++) - { + void check(GHLongIntBTree instance, int from) { + for (int i = from; i < instance.getSize(); i++) { assertEquals(i * 2, instance.get(i)); } } @Test - public void testPut() - { + public void testPut() { GHLongIntBTree instance = new GHLongIntBTree(3); instance.put(2, 4); instance.put(7, 14); @@ -125,8 +116,7 @@ public void testPut() } @Test - public void testUpdate() - { + public void testUpdate() { GHLongIntBTree instance = new GHLongIntBTree(2); int result = instance.put(100, 10); assertEquals(instance.getNoNumberValue(), result); @@ -142,26 +132,21 @@ public void testUpdate() } @Test - public void testRandom() - { - for (int j = 3; j < 12; j += 4) - { + public void testRandom() { + for (int j = 3; j < 12; j += 4) { GHLongIntBTree instance = new GHLongIntBTree(j); int size = 500; Random rand = new Random(123); Set addedValues = new LinkedHashSet(size); - for (int i = 0; i < size; i++) - { + for (int i = 0; i < size; i++) { int val = rand.nextInt(size); addedValues.add(val); - try - { + try { instance.put(val, val); // System.out.println(i + "--------------" + val); // instance.print(); // System.out.println("\n\n"); - } catch (Exception ex) - { + } catch (Exception ex) { ex.printStackTrace(); assertFalse(j + "| Problem with " + i + " " + ex, true); } @@ -169,15 +154,13 @@ public void testRandom() assertEquals(j + "| Size not equal to set! In " + i + " added " + val, addedValues.size(), instance.getSize()); } int i = 0; - for (int val : addedValues) - { + for (int val : addedValues) { assertEquals(j + "| Problem with " + i, val, instance.get(val)); i++; } instance.optimize(); i = 0; - for (int val : addedValues) - { + for (int val : addedValues) { assertEquals(j + "| Problem with " + i, val, instance.get(val)); i++; } diff --git a/core/src/test/java/com/graphhopper/coll/GHSortedCollectionTest.java b/core/src/test/java/com/graphhopper/coll/GHSortedCollectionTest.java index d9f6bbc0c9e..1d046db5e14 100644 --- a/core/src/test/java/com/graphhopper/coll/GHSortedCollectionTest.java +++ b/core/src/test/java/com/graphhopper/coll/GHSortedCollectionTest.java @@ -19,16 +19,15 @@ import org.junit.Test; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; /** * @author Peter Karich */ -public class GHSortedCollectionTest -{ +public class GHSortedCollectionTest { @Test - public void testPoll() - { + public void testPoll() { GHSortedCollection instance = new GHSortedCollection(); assertTrue(instance.isEmpty()); instance.insert(0, 10); @@ -42,8 +41,7 @@ public void testPoll() } @Test - public void testInsert() - { + public void testInsert() { GHSortedCollection instance = new GHSortedCollection(); assertTrue(instance.isEmpty()); instance.insert(0, 10); @@ -62,8 +60,7 @@ public void testInsert() } @Test - public void testUpdate() - { + public void testUpdate() { GHSortedCollection instance = new GHSortedCollection(); assertTrue(instance.isEmpty()); instance.insert(0, 10); diff --git a/core/src/test/java/com/graphhopper/coll/GHTBitSetTest.java b/core/src/test/java/com/graphhopper/coll/GHTBitSetTest.java index 3709ebf94fa..d88bbbc2c89 100644 --- a/core/src/test/java/com/graphhopper/coll/GHTBitSetTest.java +++ b/core/src/test/java/com/graphhopper/coll/GHTBitSetTest.java @@ -17,28 +17,24 @@ */ package com.graphhopper.coll; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; /** * @author Peter Karich */ -public class GHTBitSetTest extends AbstractMyBitSetTest -{ +public class GHTBitSetTest extends AbstractMyBitSetTest { @Override - public GHBitSet createBitSet( int no ) - { + public GHBitSet createBitSet(int no) { return new GHTBitSet(no); } @Override - public void testNext() - { + public void testNext() { // not supported (yet) -> due to sorting } @Override - public void testToString() - { + public void testToString() { // unsorted output! GHBitSet bs = createBitSet(100); bs.add(12); diff --git a/core/src/test/java/com/graphhopper/coll/GHTreeMapComposedTest.java b/core/src/test/java/com/graphhopper/coll/GHTreeMapComposedTest.java index 8ec1f90ef61..6da12df2b20 100644 --- a/core/src/test/java/com/graphhopper/coll/GHTreeMapComposedTest.java +++ b/core/src/test/java/com/graphhopper/coll/GHTreeMapComposedTest.java @@ -19,16 +19,14 @@ import org.junit.Test; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; /** * @author Peter Karich */ -public class GHTreeMapComposedTest -{ +public class GHTreeMapComposedTest { @Test - public void testInsert() - { + public void testInsert() { GHTreeMapComposed instance = new GHTreeMapComposed(); instance.insert(1, 100); assertEquals(1, instance.peekKey()); diff --git a/core/src/test/java/com/graphhopper/coll/IntDoubleBinHeapTest.java b/core/src/test/java/com/graphhopper/coll/IntDoubleBinHeapTest.java index 7aafdd93ccd..2c17655895e 100644 --- a/core/src/test/java/com/graphhopper/coll/IntDoubleBinHeapTest.java +++ b/core/src/test/java/com/graphhopper/coll/IntDoubleBinHeapTest.java @@ -20,11 +20,9 @@ /** * @author Peter Karich */ -public class IntDoubleBinHeapTest extends AbstractBinHeapTest -{ +public class IntDoubleBinHeapTest extends AbstractBinHeapTest { @Override - public BinHeapWrapper createHeap( int capacity ) - { + public BinHeapWrapper createHeap(int capacity) { return new IntDoubleBinHeap(capacity); } } diff --git a/core/src/test/java/com/graphhopper/coll/OSMIDMapTest.java b/core/src/test/java/com/graphhopper/coll/OSMIDMapTest.java index 3bb8f5299c1..a0d9e269f0f 100644 --- a/core/src/test/java/com/graphhopper/coll/OSMIDMapTest.java +++ b/core/src/test/java/com/graphhopper/coll/OSMIDMapTest.java @@ -21,16 +21,14 @@ import com.graphhopper.storage.RAMDirectory; import org.junit.Test; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; /** * @author Peter Karich */ -public class OSMIDMapTest -{ +public class OSMIDMapTest { @Test - public void testGet() - { + public void testGet() { OSMIDMap map = new OSMIDMap(new RAMDirectory()); map.put(9, 0); map.put(10, -50); @@ -53,16 +51,14 @@ public void testGet() assertEquals(2, map.get(31)); assertEquals(-1, map.get(32)); - for (int i = 0; i < 50; i++) - { + for (int i = 0; i < 50; i++) { map.put(i + 50, i + 7); } assertEquals(57, map.getSize()); } @Test - public void testBinSearch() - { + public void testBinSearch() { DataAccess da = new RAMDirectory().find(""); da.create(100); @@ -89,8 +85,7 @@ public void testBinSearch() } @Test - public void testGetLong() - { + public void testGetLong() { OSMIDMap map = new OSMIDMap(new RAMDirectory()); map.put(12, 0); map.put(Long.MAX_VALUE / 10, 1); @@ -103,8 +98,7 @@ public void testGetLong() } @Test - public void testGet2() - { + public void testGet2() { OSMIDMap map = new OSMIDMap(new RAMDirectory()); map.put(9, 0); map.put(10, 1); @@ -131,8 +125,7 @@ public void testGet2() } @Test - public void testUpdateOfLowerKeys() - { + public void testUpdateOfLowerKeys() { OSMIDMap map = new OSMIDMap(new RAMDirectory()); map.put(9, 0); map.put(10, 1); diff --git a/core/src/test/java/com/graphhopper/geohash/LinearKeyAlgoTest.java b/core/src/test/java/com/graphhopper/geohash/LinearKeyAlgoTest.java index 70b43ed7268..b9aae20fe73 100644 --- a/core/src/test/java/com/graphhopper/geohash/LinearKeyAlgoTest.java +++ b/core/src/test/java/com/graphhopper/geohash/LinearKeyAlgoTest.java @@ -19,19 +19,16 @@ import com.graphhopper.util.shapes.BBox; import com.graphhopper.util.shapes.GHPoint; - -import static org.junit.Assert.*; - import org.junit.Test; +import static org.junit.Assert.assertEquals; + /** * @author Peter Karich */ -public class LinearKeyAlgoTest -{ +public class LinearKeyAlgoTest { @Test - public void testEncode() - { + public void testEncode() { KeyAlgo algo = new LinearKeyAlgo(3, 4).setBounds(-1, 9, -2, 20); assertEquals(2L, algo.encode(-1, 5)); assertEquals(11L, algo.encode(14, 7)); @@ -48,8 +45,7 @@ public void testEncode() } @Test - public void testDecode() - { + public void testDecode() { KeyAlgo algo = new LinearKeyAlgo(3, 4).setBounds(-1, 9, -2, 20); GHPoint latLon = new GHPoint(); @@ -75,8 +71,7 @@ public void testDecode() * Test if different constructors yield same results */ @Test - public void testInstantiation() - { + public void testInstantiation() { double minLon = 0; double minLat = 2; double maxLat = 6; diff --git a/core/src/test/java/com/graphhopper/geohash/SpatialKeyAlgoTest.java b/core/src/test/java/com/graphhopper/geohash/SpatialKeyAlgoTest.java index 9b7f91eb4b4..2f6b9c04f34 100644 --- a/core/src/test/java/com/graphhopper/geohash/SpatialKeyAlgoTest.java +++ b/core/src/test/java/com/graphhopper/geohash/SpatialKeyAlgoTest.java @@ -22,24 +22,22 @@ import com.graphhopper.util.shapes.GHPoint; import org.junit.Test; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; /** * @author Peter Karich */ -public class SpatialKeyAlgoTest -{ +public class SpatialKeyAlgoTest { @Test - public void testEncode() - { + public void testEncode() { SpatialKeyAlgo algo = new SpatialKeyAlgo(32); long val = algo.encode(-24.235345f, 47.234234f); assertEquals("01100110101000111100000110010100", BitUtil.BIG.toLastBitString(val, 32)); } @Test - public void testEncode3BytesPrecision() - { + public void testEncode3BytesPrecision() { // 3 bytes => c / 1^12 = ~10km int bits = 3 * 8; SpatialKeyAlgo algo = new SpatialKeyAlgo(bits); @@ -60,8 +58,7 @@ public void testEncode3BytesPrecision() } @Test - public void testEncode4BytesPrecision() - { + public void testEncode4BytesPrecision() { int bits = 4 * 8; SpatialKeyAlgo algo = new SpatialKeyAlgo(bits); float lat = 24.235345f; @@ -80,8 +77,7 @@ public void testEncode4BytesPrecision() } @Test - public void testEncode6BytesPrecision() - { + public void testEncode6BytesPrecision() { int bits = 6 * 8; SpatialKeyAlgo algo = new SpatialKeyAlgo(bits); float lat = 24.235345f; @@ -100,10 +96,8 @@ public void testEncode6BytesPrecision() } @Test - public void testBijectionBug2() - { - for (long i = 4; i <= 64; i += 4) - { + public void testBijectionBug2() { + for (long i = 4; i <= 64; i += 4) { SpatialKeyAlgo algo = new SpatialKeyAlgo((int) i); long keyX = algo.encode(1, 1); @@ -123,8 +117,7 @@ public void testBijectionBug2() } @Test - public void testBijection() - { + public void testBijection() { // fix bijection precision problem! // // the latitude encoding "10" would result in 1.0 but a rounding error could lead to e.g. 0.99 @@ -142,8 +135,7 @@ public void testBijection() testBijection(8 * 8); } - public void testBijection( int bits ) - { + public void testBijection(int bits) { SpatialKeyAlgo algo = new SpatialKeyAlgo(bits); GHPoint coord11 = new GHPoint(); long key = algo.encode(1, 1); @@ -190,8 +182,7 @@ public void testBijection( int bits ) } @Test - public void testNoFurtherIterationIfBitsIs1() - { + public void testNoFurtherIterationIfBitsIs1() { SpatialKeyAlgo algo = new SpatialKeyAlgo(4).setBounds(0, 5, 0, 5); // 1001 GHPoint coord = new GHPoint(); @@ -201,8 +192,7 @@ public void testNoFurtherIterationIfBitsIs1() } @Test - public void testOddBits() - { + public void testOddBits() { GHPoint coord = new GHPoint(); SpatialKeyAlgo algo = new SpatialKeyAlgo(8); long key = algo.encode(5, 30); @@ -219,8 +209,7 @@ public void testOddBits() } @Test - public void testDifferentInitialBounds() - { + public void testDifferentInitialBounds() { SpatialKeyAlgo algo = new SpatialKeyAlgo(8).setBounds(0, 5, 0, 5); assertEquals(1, algo.encode(0, 0.5)); assertEquals(5, algo.encode(0, 1)); @@ -234,8 +223,7 @@ public void testDifferentInitialBounds() } @Test - public void testEdgeCases() - { + public void testEdgeCases() { double minLon = -1, maxLon = 1.6; double minLat = -1, maxLat = 0.5; int parts = 4; diff --git a/core/src/test/java/com/graphhopper/reader/OSMElementTest.java b/core/src/test/java/com/graphhopper/reader/OSMElementTest.java index 98ce93374bf..b6aa5458b03 100644 --- a/core/src/test/java/com/graphhopper/reader/OSMElementTest.java +++ b/core/src/test/java/com/graphhopper/reader/OSMElementTest.java @@ -17,23 +17,20 @@ */ package com.graphhopper.reader; -import com.graphhopper.reader.ReaderWay; -import com.graphhopper.reader.ReaderElement; +import org.junit.Test; + import java.util.HashMap; import java.util.Map; -import org.junit.Test; - -import static org.junit.Assert.*; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; /** * @author Peter Karich */ -public class OSMElementTest -{ +public class OSMElementTest { @Test - public void testHasTag() - { + public void testHasTag() { ReaderElement instance = new ReaderWay(1); instance.setTag("surface", "something"); assertTrue(instance.hasTag("surface", "now", "something")); @@ -41,8 +38,7 @@ public void testHasTag() } @Test - public void testSetTags() - { + public void testSetTags() { ReaderElement instance = new ReaderWay(1); Map map = new HashMap(); map.put("test", "xy"); diff --git a/core/src/test/java/com/graphhopper/reader/OSMNodeTest.java b/core/src/test/java/com/graphhopper/reader/OSMNodeTest.java index 5a53432b146..fe792c128dd 100644 --- a/core/src/test/java/com/graphhopper/reader/OSMNodeTest.java +++ b/core/src/test/java/com/graphhopper/reader/OSMNodeTest.java @@ -17,19 +17,17 @@ */ package com.graphhopper.reader; -import com.graphhopper.reader.ReaderNode; import org.junit.Test; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; /** * @author Peter Karich */ -public class OSMNodeTest -{ +public class OSMNodeTest { @Test - public void testSetTags() - { + public void testSetTags() { ReaderNode instance = new ReaderNode(0, 10, 10); assertTrue(Double.isNaN(instance.getEle())); diff --git a/core/src/test/java/com/graphhopper/reader/PrinctonReader.java b/core/src/test/java/com/graphhopper/reader/PrinctonReader.java index 1a359932c31..13f30841577 100644 --- a/core/src/test/java/com/graphhopper/reader/PrinctonReader.java +++ b/core/src/test/java/com/graphhopper/reader/PrinctonReader.java @@ -27,78 +27,65 @@ /** * Data taken from here http://algs4.cs.princeton.edu/44sp/ *

    + * * @author Peter Karich */ -public class PrinctonReader -{ +public class PrinctonReader { private Graph g; private InputStream is; - public PrinctonReader( Graph graph ) - { + public PrinctonReader(Graph graph) { g = graph; } - public PrinctonReader setStream( InputStream is ) - { + public PrinctonReader setStream(InputStream is) { this.is = is; return this; } - public void read() - { + public void read() { BufferedReader reader = new BufferedReader(new InputStreamReader(is, Helper.UTF_CS), 8 * (1 << 10)); int lineNo = 0; - try - { + try { lineNo++; int nodes = Integer.parseInt(reader.readLine()); lineNo++; int edges = Integer.parseInt(reader.readLine()); - for (int i = 0; i < edges; i++) - { + for (int i = 0; i < edges; i++) { lineNo++; String line = reader.readLine(); if (line == null) throw new IllegalStateException("Cannot read line " + lineNo); - + String args[] = line.split(" "); int from = -1; int to = -1; double dist = -1; int counter = 0; - for (int j = 0; j < args.length; j++) - { - if (Helper.isEmpty(args[j])) - { + for (int j = 0; j < args.length; j++) { + if (Helper.isEmpty(args[j])) { continue; } - if (counter == 0) - { + if (counter == 0) { from = Integer.parseInt(args[j]); - } else if (counter == 1) - { + } else if (counter == 1) { to = Integer.parseInt(args[j]); - } else - { + } else { dist = Double.parseDouble(args[j]); } counter++; } - if (counter != 3) - { + if (counter != 3) { throw new RuntimeException("incorrect read!? from:" + from + ", to:" + to + ", dist:" + dist); } g.edge(from, to, dist, false); } - } catch (Exception ex) - { + } catch (Exception ex) { throw new RuntimeException("Problem in line " + lineNo, ex); - } finally - { + } finally { Helper.close(reader); } } diff --git a/core/src/test/java/com/graphhopper/reader/PrinctonReaderTest.java b/core/src/test/java/com/graphhopper/reader/PrinctonReaderTest.java index 6fb1539e660..ac6d083f4b8 100644 --- a/core/src/test/java/com/graphhopper/reader/PrinctonReaderTest.java +++ b/core/src/test/java/com/graphhopper/reader/PrinctonReaderTest.java @@ -21,30 +21,25 @@ import com.graphhopper.routing.util.EdgeFilter; import com.graphhopper.routing.util.EncodingManager; import com.graphhopper.storage.Graph; - -import static com.graphhopper.util.GHUtility.*; - import com.graphhopper.storage.GraphBuilder; import com.graphhopper.util.EdgeExplorer; +import org.junit.Test; import java.io.IOException; import java.util.zip.GZIPInputStream; -import org.junit.Test; - -import static org.junit.Assert.*; +import static com.graphhopper.util.GHUtility.count; +import static org.junit.Assert.assertEquals; /** * @author Peter Karich */ -public class PrinctonReaderTest -{ +public class PrinctonReaderTest { private EncodingManager encodingManager = new EncodingManager("car"); private EdgeFilter carOutEdges = new DefaultEdgeFilter(encodingManager.getEncoder("car"), false, true); @Test - public void testRead() - { + public void testRead() { Graph graph = new GraphBuilder(encodingManager).create(); new PrinctonReader(graph).setStream(PrinctonReader.class.getResourceAsStream("tinyEWD.txt")).read(); assertEquals(8, graph.getNodes()); @@ -54,8 +49,7 @@ public void testRead() } @Test - public void testMediumRead() throws IOException - { + public void testMediumRead() throws IOException { Graph graph = new GraphBuilder(encodingManager).create(); new PrinctonReader(graph).setStream(new GZIPInputStream(PrinctonReader.class.getResourceAsStream("mediumEWD.txt.gz"))).read(); assertEquals(250, graph.getNodes()); diff --git a/core/src/test/java/com/graphhopper/reader/dem/CGIARProviderTest.java b/core/src/test/java/com/graphhopper/reader/dem/CGIARProviderTest.java index 1be9231c8f0..d39bc7647d2 100644 --- a/core/src/test/java/com/graphhopper/reader/dem/CGIARProviderTest.java +++ b/core/src/test/java/com/graphhopper/reader/dem/CGIARProviderTest.java @@ -18,31 +18,30 @@ package com.graphhopper.reader.dem; import com.graphhopper.util.Downloader; +import org.junit.Before; +import org.junit.Test; + import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.net.SocketTimeoutException; -import org.junit.Test; -import static org.junit.Assert.*; -import org.junit.Before; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; /** * @author Peter Karich */ -public class CGIARProviderTest -{ +public class CGIARProviderTest { CGIARProvider instance; @Before - public void setUp() - { + public void setUp() { instance = new CGIARProvider(); } @Test - public void testDown() - { + public void testDown() { assertEquals(50, instance.down(52.5)); assertEquals(0, instance.down(0.1)); assertEquals(0, instance.down(0.01)); @@ -54,8 +53,7 @@ public void testDown() } @Test - public void testFileName() - { + public void testFileName() { assertEquals("srtm_36_02", instance.getFileName(52, -0.1)); assertEquals("srtm_35_02", instance.getFileName(50, -10)); @@ -70,18 +68,15 @@ public void testFileName() } @Test - public void testFileNotFound() - { + public void testFileNotFound() { File file = new File(instance.getCacheDir(), instance.getFileName(46, -20) + ".gh"); File zipFile = new File(instance.getCacheDir(), instance.getFileName(46, -20) + ".zip"); file.delete(); zipFile.delete(); - instance.setDownloader(new Downloader("test GH") - { + instance.setDownloader(new Downloader("test GH") { @Override - public void downloadFile( String url, String toFile ) throws IOException - { + public void downloadFile(String url, String toFile) throws IOException { throw new FileNotFoundException("xyz"); } }); @@ -91,22 +86,18 @@ public void downloadFile( String url, String toFile ) throws IOException assertTrue(file.exists()); assertEquals(228, file.length()); - instance.setDownloader(new Downloader("test GH") - { + instance.setDownloader(new Downloader("test GH") { @Override - public void downloadFile( String url, String toFile ) throws IOException - { + public void downloadFile(String url, String toFile) throws IOException { throw new SocketTimeoutException("xyz"); } }); - try - { + try { instance.setSleep(30); instance.getEle(16, -20); assertTrue(false); - } catch (Exception ex) - { + } catch (Exception ex) { } file.delete(); diff --git a/core/src/test/java/com/graphhopper/reader/dem/HeightTileTest.java b/core/src/test/java/com/graphhopper/reader/dem/HeightTileTest.java index 552fdf6f298..046ff8d5e8d 100644 --- a/core/src/test/java/com/graphhopper/reader/dem/HeightTileTest.java +++ b/core/src/test/java/com/graphhopper/reader/dem/HeightTileTest.java @@ -21,16 +21,14 @@ import com.graphhopper.storage.RAMDirectory; import org.junit.Test; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; /** * @author Peter Karich */ -public class HeightTileTest -{ +public class HeightTileTest { @Test - public void testGetHeight() - { + public void testGetHeight() { // data access has same coordinate system as graphical or UI systems have (or the original DEM data has). // But HeightTile has lat,lon system ('mathematically') int width = 10; @@ -75,8 +73,7 @@ public void testGetHeight() } @Test - public void testGetHeightForNegativeTile() - { + public void testGetHeightForNegativeTile() { int width = 10; HeightTile instance = new HeightTile(-20, -20, width, 1e-6, 10); DataAccess heights = new RAMDirectory().find("tmp"); @@ -98,8 +95,7 @@ public void testGetHeightForNegativeTile() } @Test - public void testCalcMean() - { + public void testCalcMean() { int width = 10; HeightTile instance = new HeightTile(0, 0, width, 1e-6, 10).setCalcMean(true); DataAccess heights = new RAMDirectory().find("tmp"); @@ -125,12 +121,9 @@ public void testCalcMean() assertEquals((10 + 2) / 3d, instance.getHeight(-0.5, -0.5), 1e-3); } - private void init( DataAccess da, int width, int i ) - { - for (int x = 0; x < width; x++) - { - for (int y = 0; y < width; y++) - { + private void init(DataAccess da, int width, int i) { + for (int x = 0; x < width; x++) { + for (int y = 0; y < width; y++) { da.setShort(2 * (y * width + x), (short) 1); } } diff --git a/core/src/test/java/com/graphhopper/reader/dem/SRTMProviderTest.java b/core/src/test/java/com/graphhopper/reader/dem/SRTMProviderTest.java index 6bff4de61ef..9d277b0a4d5 100644 --- a/core/src/test/java/com/graphhopper/reader/dem/SRTMProviderTest.java +++ b/core/src/test/java/com/graphhopper/reader/dem/SRTMProviderTest.java @@ -18,39 +18,33 @@ package com.graphhopper.reader.dem; import com.graphhopper.storage.DAType; - -import java.io.File; -import java.io.IOException; - import org.junit.After; +import org.junit.Before; import org.junit.Test; -import static org.junit.Assert.*; +import java.io.File; +import java.io.IOException; -import org.junit.Before; +import static org.junit.Assert.assertEquals; /** * @author Peter Karich */ -public class SRTMProviderTest -{ +public class SRTMProviderTest { SRTMProvider instance; @Before - public void setUp() - { + public void setUp() { instance = new SRTMProvider(); } @After - public void tearDown() - { + public void tearDown() { instance.release(); } @Test - public void testGetFileString() - { + public void testGetFileString() { assertEquals("Eurasia/N49E011", instance.getFileString(49, 11)); assertEquals("Eurasia/N52W002", instance.getFileString(52.268157, -1.230469)); assertEquals("Africa/S06E034", instance.getFileString(-5.965754, 34.804687)); @@ -63,8 +57,7 @@ public void testGetFileString() } @Test - public void testGetHeight() throws IOException - { + public void testGetHeight() throws IOException { instance.setCacheDir(new File("./files/")); // easy to verify orientation of tile: // instance.getEle(43, 13); @@ -93,8 +86,7 @@ public void testGetHeight() throws IOException } @Test - public void testGetHeight_issue545() throws IOException - { + public void testGetHeight_issue545() throws IOException { instance.setCacheDir(new File("./files/")); // test different precision of the elevation file (3600) @@ -102,8 +94,7 @@ public void testGetHeight_issue545() throws IOException } @Test - public void testGetHeightMMap() throws IOException - { + public void testGetHeightMMap() throws IOException { instance.setCacheDir(new File("./files/")); instance.setDAType(DAType.MMAP); assertEquals(161, instance.getEle(55.8943144, -3), 1e-1); diff --git a/core/src/test/java/com/graphhopper/reader/osm/conditional/CalendarBasedTest.java b/core/src/test/java/com/graphhopper/reader/osm/conditional/CalendarBasedTest.java index c24e1114b34..7db159ab40a 100644 --- a/core/src/test/java/com/graphhopper/reader/osm/conditional/CalendarBasedTest.java +++ b/core/src/test/java/com/graphhopper/reader/osm/conditional/CalendarBasedTest.java @@ -22,12 +22,11 @@ /** * Base Test for calendar based tasks. *

    + * * @author Robin Boldt */ -public abstract class CalendarBasedTest -{ - protected Calendar getCalendar( int year, int month, int day ) - { +public abstract class CalendarBasedTest { + protected Calendar getCalendar(int year, int month, int day) { Calendar calendar = DateRangeParser.createCalendar(); calendar.set(Calendar.YEAR, year); calendar.set(Calendar.MONTH, month); diff --git a/core/src/test/java/com/graphhopper/reader/osm/conditional/ConditionalOSMTagInspectorTest.java b/core/src/test/java/com/graphhopper/reader/osm/conditional/ConditionalOSMTagInspectorTest.java index 1186d102981..a0b163bf947 100644 --- a/core/src/test/java/com/graphhopper/reader/osm/conditional/ConditionalOSMTagInspectorTest.java +++ b/core/src/test/java/com/graphhopper/reader/osm/conditional/ConditionalOSMTagInspectorTest.java @@ -23,16 +23,42 @@ import java.util.*; -import static org.junit.Assert.*; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; /** * @author Robin Boldt */ -public class ConditionalOSMTagInspectorTest extends CalendarBasedTest -{ +public class ConditionalOSMTagInspectorTest extends CalendarBasedTest { + private static Set getSampleRestrictedValues() { + Set restrictedValues = new HashSet(); + restrictedValues.add("private"); + restrictedValues.add("agricultural"); + restrictedValues.add("forestry"); + restrictedValues.add("no"); + restrictedValues.add("restricted"); + restrictedValues.add("delivery"); + restrictedValues.add("military"); + restrictedValues.add("emergency"); + return restrictedValues; + } + + private static Set getSamplePermissiveValues() { + Set restrictedValues = new HashSet(); + restrictedValues.add("yes"); + restrictedValues.add("permissive"); + return restrictedValues; + } + + private static List getSampleConditionalTags() { + List conditionalTags = new ArrayList(); + conditionalTags.add("vehicle"); + conditionalTags.add("access"); + return conditionalTags; + } + @Test - public void testConditionalAccept() - { + public void testConditionalAccept() { Calendar cal = getCalendar(2014, Calendar.MARCH, 10); ConditionalTagInspector acceptor = new ConditionalOSMTagInspector(cal, getSampleConditionalTags(), getSampleRestrictedValues(), getSamplePermissiveValues()); ReaderWay way = new ReaderWay(1); @@ -41,8 +67,7 @@ public void testConditionalAccept() } @Test - public void testConditionalAcceptNextYear() - { + public void testConditionalAcceptNextYear() { Calendar cal = getCalendar(2014, Calendar.MARCH, 10); ConditionalTagInspector acceptor = new ConditionalOSMTagInspector(cal, getSampleConditionalTags(), getSampleRestrictedValues(), getSamplePermissiveValues()); ReaderWay way = new ReaderWay(1); @@ -51,8 +76,7 @@ public void testConditionalAcceptNextYear() } @Test - public void testConditionalReject() - { + public void testConditionalReject() { Calendar cal = getCalendar(2014, Calendar.MARCH, 10); ConditionalTagInspector acceptor = new ConditionalOSMTagInspector(cal, getSampleConditionalTags(), getSampleRestrictedValues(), getSamplePermissiveValues()); ReaderWay way = new ReaderWay(1); @@ -61,8 +85,7 @@ public void testConditionalReject() } @Test - public void testConditionalAllowance() - { + public void testConditionalAllowance() { Calendar cal = getCalendar(2014, Calendar.MARCH, 10); ConditionalTagInspector acceptor = new ConditionalOSMTagInspector(cal, getSampleConditionalTags(), getSampleRestrictedValues(), getSamplePermissiveValues()); ReaderWay way = new ReaderWay(1); @@ -71,8 +94,7 @@ public void testConditionalAllowance() } @Test - public void testConditionalAllowanceReject() - { + public void testConditionalAllowanceReject() { Calendar cal = getCalendar(2014, Calendar.MARCH, 10); ConditionalTagInspector acceptor = new ConditionalOSMTagInspector(cal, getSampleConditionalTags(), getSampleRestrictedValues(), getSamplePermissiveValues()); ReaderWay way = new ReaderWay(1); @@ -81,8 +103,7 @@ public void testConditionalAllowanceReject() } @Test - public void testConditionalSingleDay() - { + public void testConditionalSingleDay() { Calendar cal = getCalendar(2015, Calendar.DECEMBER, 27); ConditionalTagInspector acceptor = new ConditionalOSMTagInspector(cal, getSampleConditionalTags(), getSampleRestrictedValues(), getSamplePermissiveValues()); ReaderWay way = new ReaderWay(1); @@ -91,8 +112,7 @@ public void testConditionalSingleDay() } @Test - public void testConditionalAllowanceSingleDay() - { + public void testConditionalAllowanceSingleDay() { Calendar cal = getCalendar(2015, Calendar.DECEMBER, 27); ConditionalTagInspector acceptor = new ConditionalOSMTagInspector(cal, getSampleConditionalTags(), getSampleRestrictedValues(), getSamplePermissiveValues()); ReaderWay way = new ReaderWay(1); @@ -100,34 +120,4 @@ public void testConditionalAllowanceSingleDay() assertTrue(acceptor.isRestrictedWayConditionallyPermitted(way)); } - private static Set getSampleRestrictedValues() - { - Set restrictedValues = new HashSet(); - restrictedValues.add("private"); - restrictedValues.add("agricultural"); - restrictedValues.add("forestry"); - restrictedValues.add("no"); - restrictedValues.add("restricted"); - restrictedValues.add("delivery"); - restrictedValues.add("military"); - restrictedValues.add("emergency"); - return restrictedValues; - } - - private static Set getSamplePermissiveValues() - { - Set restrictedValues = new HashSet(); - restrictedValues.add("yes"); - restrictedValues.add("permissive"); - return restrictedValues; - } - - private static List getSampleConditionalTags() - { - List conditionalTags = new ArrayList(); - conditionalTags.add("vehicle"); - conditionalTags.add("access"); - return conditionalTags; - } - } diff --git a/core/src/test/java/com/graphhopper/reader/osm/conditional/ConditionalParserTest.java b/core/src/test/java/com/graphhopper/reader/osm/conditional/ConditionalParserTest.java index 8a2ab74a3f4..d78a2380ab4 100644 --- a/core/src/test/java/com/graphhopper/reader/osm/conditional/ConditionalParserTest.java +++ b/core/src/test/java/com/graphhopper/reader/osm/conditional/ConditionalParserTest.java @@ -30,13 +30,11 @@ /** * @author Robin Boldt */ -public class ConditionalParserTest extends CalendarBasedTest -{ +public class ConditionalParserTest extends CalendarBasedTest { ConditionalParser parser; @Before - public void setup() - { + public void setup() { HashSet restrictedValues = new HashSet(); restrictedValues.add("private"); restrictedValues.add("agricultural"); @@ -51,23 +49,20 @@ public void setup() } @Test - public void testParseConditional() throws ParseException - { + public void testParseConditional() throws ParseException { ValueRange dateRange = parser.getRange("no @ (2015 Sep 1-2015 Sep 30)"); assertFalse(dateRange.isInRange(getCalendar(2015, Calendar.AUGUST, 31))); assertTrue(dateRange.isInRange(getCalendar(2015, Calendar.SEPTEMBER, 30))); } @Test - public void testParseAllowingCondition() throws ParseException - { + public void testParseAllowingCondition() throws ParseException { ValueRange dateRange = parser.getRange("yes @ (2015 Sep 1-2015 Sep 30)"); assertNull(dateRange); } @Test - public void testParsingOfLeading0() throws ParseException - { + public void testParsingOfLeading0() throws ParseException { ValueRange dateRange = parser.getRange("no @ (01.11. - 31.03.)"); assertTrue(dateRange.isInRange(getCalendar(2015, Calendar.DECEMBER, 2))); @@ -76,8 +71,7 @@ public void testParsingOfLeading0() throws ParseException } @Test - public void testGetRange() throws Exception - { + public void testGetRange() throws Exception { Set set = new HashSet<>(); set.add("no"); ConditionalParser instance = new ConditionalParser(set); @@ -124,8 +118,7 @@ public void testGetRange() throws Exception } @Test - public void parseNumber() - { + public void parseNumber() { // TODO currently no unit conversation is done which can be required if a different one is passed in isInRange Set set = new HashSet<>(); ConditionalParser p = new ConditionalParser(set); diff --git a/core/src/test/java/com/graphhopper/reader/osm/conditional/DateRangeParserTest.java b/core/src/test/java/com/graphhopper/reader/osm/conditional/DateRangeParserTest.java index 484c7866716..92c5284fe7d 100644 --- a/core/src/test/java/com/graphhopper/reader/osm/conditional/DateRangeParserTest.java +++ b/core/src/test/java/com/graphhopper/reader/osm/conditional/DateRangeParserTest.java @@ -27,11 +27,9 @@ /** * @author Robin Boldt */ -public class DateRangeParserTest extends CalendarBasedTest -{ +public class DateRangeParserTest extends CalendarBasedTest { @Test - public void testParseConditional() throws ParseException - { + public void testParseConditional() throws ParseException { assertSameDate(2014, Calendar.DECEMBER, 15, "2014 Dec 15"); assertSameDate(2015, Calendar.MARCH, 2, "2015 Mar 2"); assertSameDate(2015, Calendar.MARCH, 1, "2015 Mar"); @@ -40,15 +38,13 @@ public void testParseConditional() throws ParseException } @Test - public void testToString() throws ParseException - { + public void testToString() throws ParseException { DateRange instance = DateRangeParser.parseDateRange("Mar-Oct"); assertEquals("yearless:true, dayOnly:false, reverse:false, from:1970-03-01T00:00:00Z, to:1970-10-31T23:59:59Z", instance.toString()); } @Test - public void testParseSimpleDateRange() throws ParseException - { + public void testParseSimpleDateRange() throws ParseException { DateRange dateRange = DateRangeParser.parseDateRange("2014 Aug 10-2014 Aug 14"); assertFalse(dateRange.isInRange(getCalendar(2014, Calendar.AUGUST, 9))); assertTrue(dateRange.isInRange(getCalendar(2014, Calendar.AUGUST, 10))); @@ -58,8 +54,7 @@ public void testParseSimpleDateRange() throws ParseException } @Test - public void testParseSimpleDateRangeWithoutYear() throws ParseException - { + public void testParseSimpleDateRangeWithoutYear() throws ParseException { DateRange dateRange = DateRangeParser.parseDateRange("Aug 10-Aug 14"); assertFalse(dateRange.isInRange(getCalendar(2014, Calendar.AUGUST, 9))); assertTrue(dateRange.isInRange(getCalendar(2014, Calendar.AUGUST, 10))); @@ -69,8 +64,7 @@ public void testParseSimpleDateRangeWithoutYear() throws ParseException } @Test - public void testParseSimpleDateRangeWithoutYearAndDay() throws ParseException - { + public void testParseSimpleDateRangeWithoutYearAndDay() throws ParseException { DateRange dateRange = DateRangeParser.parseDateRange("Jul-Aug"); assertFalse(dateRange.isInRange(getCalendar(2014, Calendar.JUNE, 9))); assertTrue(dateRange.isInRange(getCalendar(2014, Calendar.JULY, 10))); @@ -79,8 +73,7 @@ public void testParseSimpleDateRangeWithoutYearAndDay() throws ParseException } @Test - public void testParseSimpleDateRangeWithoutYearAndDay2() throws ParseException - { + public void testParseSimpleDateRangeWithoutYearAndDay2() throws ParseException { DateRange dateRange = DateRangeParser.parseDateRange("Mar-Sep"); assertFalse(dateRange.isInRange(getCalendar(2014, Calendar.FEBRUARY, 25))); assertTrue(dateRange.isInRange(getCalendar(2014, Calendar.MARCH, 1))); @@ -89,8 +82,7 @@ public void testParseSimpleDateRangeWithoutYearAndDay2() throws ParseException } @Test - public void testParseReverseDateRange() throws ParseException - { + public void testParseReverseDateRange() throws ParseException { DateRange dateRange = DateRangeParser.parseDateRange("2014 Aug 14-2015 Mar 10"); assertFalse(dateRange.isInRange(getCalendar(2014, Calendar.AUGUST, 13))); assertTrue(dateRange.isInRange(getCalendar(2014, Calendar.AUGUST, 14))); @@ -99,8 +91,7 @@ public void testParseReverseDateRange() throws ParseException } @Test - public void testParseReverseDateRangeWithoutYear() throws ParseException - { + public void testParseReverseDateRangeWithoutYear() throws ParseException { DateRange dateRange = DateRangeParser.parseDateRange("Aug 14-Aug 10"); assertTrue(dateRange.isInRange(getCalendar(2014, Calendar.JANUARY, 9))); assertTrue(dateRange.isInRange(getCalendar(2014, Calendar.AUGUST, 9))); @@ -112,8 +103,7 @@ public void testParseReverseDateRangeWithoutYear() throws ParseException } @Test - public void testParseReverseDateRangeWithoutYearAndDay() throws ParseException - { + public void testParseReverseDateRangeWithoutYearAndDay() throws ParseException { DateRange dateRange = DateRangeParser.parseDateRange("Sep-Mar"); assertFalse(dateRange.isInRange(getCalendar(2014, Calendar.AUGUST, 31))); assertTrue(dateRange.isInRange(getCalendar(2014, Calendar.SEPTEMBER, 1))); @@ -124,8 +114,7 @@ public void testParseReverseDateRangeWithoutYearAndDay() throws ParseException } @Test - public void testParseReverseDateRangeWithoutYearAndDay_645() throws ParseException - { + public void testParseReverseDateRangeWithoutYearAndDay_645() throws ParseException { DateRange dateRange = DateRangeParser.parseDateRange("Aug 10-Jan"); assertFalse(dateRange.isInRange(getCalendar(2016, Calendar.AUGUST, 9))); assertTrue(dateRange.isInRange(getCalendar(2016, Calendar.AUGUST, 10))); @@ -136,8 +125,7 @@ public void testParseReverseDateRangeWithoutYearAndDay_645() throws ParseExcepti } @Test - public void testParseSingleDateRange() throws ParseException - { + public void testParseSingleDateRange() throws ParseException { DateRange dateRange = DateRangeParser.parseDateRange("2014 Sep 1"); assertFalse(dateRange.isInRange(getCalendar(2014, Calendar.AUGUST, 31))); assertTrue(dateRange.isInRange(getCalendar(2014, Calendar.SEPTEMBER, 1))); @@ -147,8 +135,7 @@ public void testParseSingleDateRange() throws ParseException } @Test - public void testParseSingleDateRangeWithoutDay() throws ParseException - { + public void testParseSingleDateRangeWithoutDay() throws ParseException { DateRange dateRange = DateRangeParser.parseDateRange("2014 Sep"); assertFalse(dateRange.isInRange(getCalendar(2014, Calendar.AUGUST, 31))); assertTrue(dateRange.isInRange(getCalendar(2014, Calendar.SEPTEMBER, 1))); @@ -158,8 +145,7 @@ public void testParseSingleDateRangeWithoutDay() throws ParseException } @Test - public void testParseSingleDateRangeWithoutYearAndDay() throws ParseException - { + public void testParseSingleDateRangeWithoutYearAndDay() throws ParseException { DateRange dateRange = DateRangeParser.parseDateRange("Sep"); assertFalse(dateRange.isInRange(getCalendar(2014, Calendar.AUGUST, 31))); assertTrue(dateRange.isInRange(getCalendar(2014, Calendar.SEPTEMBER, 1))); @@ -168,8 +154,7 @@ public void testParseSingleDateRangeWithoutYearAndDay() throws ParseException } @Test - public void testParseSingleDateRangeOneDayOnly() throws ParseException - { + public void testParseSingleDateRangeOneDayOnly() throws ParseException { DateRange dateRange = DateRangeParser.parseDateRange("Sa"); assertFalse(dateRange.isInRange(getCalendar(2015, Calendar.DECEMBER, 25))); assertTrue(dateRange.isInRange(getCalendar(2015, Calendar.DECEMBER, 26))); @@ -177,8 +162,7 @@ public void testParseSingleDateRangeOneDayOnly() throws ParseException } @Test - public void testParseSingleDateRangeOneDayOnlyIncludingPh() throws ParseException - { + public void testParseSingleDateRangeOneDayOnlyIncludingPh() throws ParseException { DateRange dateRange = DateRangeParser.parseDateRange("Su, PH"); assertFalse(dateRange.isInRange(getCalendar(2015, Calendar.DECEMBER, 26))); assertTrue(dateRange.isInRange(getCalendar(2015, Calendar.DECEMBER, 27))); @@ -186,8 +170,7 @@ public void testParseSingleDateRangeOneDayOnlyIncludingPh() throws ParseExceptio } @Test - public void testParseSingleDateRangeDayOnly() throws ParseException - { + public void testParseSingleDateRangeDayOnly() throws ParseException { DateRange dateRange = DateRangeParser.parseDateRange("Mo-Fr"); assertTrue(dateRange.dayOnly); assertFalse(dateRange.reverse); @@ -199,8 +182,7 @@ public void testParseSingleDateRangeDayOnly() throws ParseException } @Test - public void testParseReverseDateRangeDayOnly() throws ParseException - { + public void testParseReverseDateRangeDayOnly() throws ParseException { // This is reverse since Su=7 and Mo=1 // Note: If we use Locale.Germany or Locale.UK for calendar creation // then cal.set(DAY_OF_WEEK, 7) results in a "time in millis" after Saturday leading to reverse=false @@ -214,14 +196,12 @@ public void testParseReverseDateRangeDayOnly() throws ParseException } @Test(expected = ParseException.class) - public void testParseUnparsableDate() throws ParseException - { + public void testParseUnparsableDate() throws ParseException { DateRangeParser.parseDateRange("Sat"); fail(); } - private void assertSameDate( int year, int month, int day, String dateString ) throws ParseException - { + private void assertSameDate(int year, int month, int day, String dateString) throws ParseException { Calendar expected = getCalendar(year, month, day); ParsedCalendar actualParsed = DateRangeParser.parseDateString(dateString); Calendar actual = actualParsed.parsedCalendar; diff --git a/core/src/test/java/com/graphhopper/routing/AStarBidirectionTest.java b/core/src/test/java/com/graphhopper/routing/AStarBidirectionTest.java index d4500fcab55..95b4ee9cd28 100644 --- a/core/src/test/java/com/graphhopper/routing/AStarBidirectionTest.java +++ b/core/src/test/java/com/graphhopper/routing/AStarBidirectionTest.java @@ -17,77 +17,57 @@ */ package com.graphhopper.routing; +import com.graphhopper.routing.util.TraversalMode; import com.graphhopper.routing.weighting.ShortestWeighting; - -import java.util.Arrays; -import java.util.Collection; - +import com.graphhopper.storage.Graph; +import com.graphhopper.storage.GraphHopperStorage; +import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import org.junit.runners.Parameterized.Parameters; -import com.graphhopper.routing.util.TraversalMode; -import com.graphhopper.storage.Graph; -import com.graphhopper.storage.GraphHopperStorage; - +import java.util.Arrays; +import java.util.Collection; import java.util.concurrent.atomic.AtomicReference; import static org.junit.Assert.assertEquals; -import org.junit.Test; - /** * @author Peter Karich */ @RunWith(Parameterized.class) -public class AStarBidirectionTest extends AbstractRoutingAlgorithmTester -{ +public class AStarBidirectionTest extends AbstractRoutingAlgorithmTester { + private final TraversalMode traversalMode; + + public AStarBidirectionTest(TraversalMode tMode) { + this.traversalMode = tMode; + } + /** * Runs the same test with each of the supported traversal modes */ @Parameters(name = "{0}") - public static Collection configs() - { - return Arrays.asList(new Object[][] - { - { - TraversalMode.NODE_BASED - }, - { - TraversalMode.EDGE_BASED_1DIR - }, - { - TraversalMode.EDGE_BASED_2DIR - }, - { - TraversalMode.EDGE_BASED_2DIR_UTURN - } - }); - } - - private final TraversalMode traversalMode; - - public AStarBidirectionTest( TraversalMode tMode ) - { - this.traversalMode = tMode; + public static Collection configs() { + return Arrays.asList(new Object[][]{ + {TraversalMode.NODE_BASED}, + {TraversalMode.EDGE_BASED_1DIR}, + {TraversalMode.EDGE_BASED_2DIR}, + {TraversalMode.EDGE_BASED_2DIR_UTURN} + }); } @Override - public RoutingAlgorithmFactory createFactory( GraphHopperStorage prepareGraph, AlgorithmOptions prepareOpts ) - { - return new RoutingAlgorithmFactory() - { + public RoutingAlgorithmFactory createFactory(GraphHopperStorage prepareGraph, AlgorithmOptions prepareOpts) { + return new RoutingAlgorithmFactory() { @Override - public RoutingAlgorithm createAlgo( Graph g, AlgorithmOptions opts ) - { + public RoutingAlgorithm createAlgo(Graph g, AlgorithmOptions opts) { return new AStarBidirection(g, opts.getFlagEncoder(), opts.getWeighting(), traversalMode); } }; } @Test - public void testInitFromAndTo() - { + public void testInitFromAndTo() { Graph g = createGHStorage(false); g.edge(0, 1, 1, true); updateDistancesFor(g, 0, 0.00, 0.00); @@ -95,18 +75,15 @@ public void testInitFromAndTo() final AtomicReference fromRef = new AtomicReference(); final AtomicReference toRef = new AtomicReference(); - AStarBidirection astar = new AStarBidirection(g, carEncoder, new ShortestWeighting(carEncoder), traversalMode) - { + AStarBidirection astar = new AStarBidirection(g, carEncoder, new ShortestWeighting(carEncoder), traversalMode) { @Override - public void initFrom( int from, double weight ) - { + public void initFrom(int from, double weight) { super.initFrom(from, weight); fromRef.set(currFrom); } @Override - public void initTo( int to, double weight ) - { + public void initTo(int to, double weight) { super.initTo(to, weight); toRef.set(currTo); } diff --git a/core/src/test/java/com/graphhopper/routing/AStarTest.java b/core/src/test/java/com/graphhopper/routing/AStarTest.java index 08b08ea923f..f720a9078d7 100644 --- a/core/src/test/java/com/graphhopper/routing/AStarTest.java +++ b/core/src/test/java/com/graphhopper/routing/AStarTest.java @@ -17,54 +17,45 @@ */ package com.graphhopper.routing; -import com.graphhopper.routing.util.*; - -import java.util.Arrays; -import java.util.Collection; - +import com.graphhopper.routing.util.TraversalMode; +import com.graphhopper.storage.Graph; +import com.graphhopper.storage.GraphHopperStorage; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import org.junit.runners.Parameterized.Parameters; -import com.graphhopper.storage.Graph; -import com.graphhopper.storage.GraphHopperStorage; +import java.util.Arrays; +import java.util.Collection; /** * @author Peter Karich */ @RunWith(Parameterized.class) -public class AStarTest extends AbstractRoutingAlgorithmTester -{ +public class AStarTest extends AbstractRoutingAlgorithmTester { + private final TraversalMode traversalMode; + + public AStarTest(TraversalMode tMode) { + this.traversalMode = tMode; + } + /** * Runs the same test with each of the supported traversal modes */ @Parameters(name = "{0}") - public static Collection configs() - { - return Arrays.asList(new Object[][] - { - {TraversalMode.NODE_BASED}, - {TraversalMode.EDGE_BASED_1DIR}, - {TraversalMode.EDGE_BASED_2DIR}, - {TraversalMode.EDGE_BASED_2DIR_UTURN} - }); - } - - private final TraversalMode traversalMode; - - public AStarTest( TraversalMode tMode ) - { - this.traversalMode = tMode; + public static Collection configs() { + return Arrays.asList(new Object[][]{ + {TraversalMode.NODE_BASED}, + {TraversalMode.EDGE_BASED_1DIR}, + {TraversalMode.EDGE_BASED_2DIR}, + {TraversalMode.EDGE_BASED_2DIR_UTURN} + }); } @Override - public RoutingAlgorithmFactory createFactory( GraphHopperStorage prepareGraph, AlgorithmOptions prepareOpts ) - { - return new RoutingAlgorithmFactory() - { + public RoutingAlgorithmFactory createFactory(GraphHopperStorage prepareGraph, AlgorithmOptions prepareOpts) { + return new RoutingAlgorithmFactory() { @Override - public RoutingAlgorithm createAlgo( Graph g, AlgorithmOptions opts ) - { + public RoutingAlgorithm createAlgo(Graph g, AlgorithmOptions opts) { return new AStar(g, opts.getFlagEncoder(), opts.getWeighting(), traversalMode); } }; diff --git a/core/src/test/java/com/graphhopper/routing/AbstractRoutingAlgorithmTester.java b/core/src/test/java/com/graphhopper/routing/AbstractRoutingAlgorithmTester.java index 783962f5cf7..ac693800984 100644 --- a/core/src/test/java/com/graphhopper/routing/AbstractRoutingAlgorithmTester.java +++ b/core/src/test/java/com/graphhopper/routing/AbstractRoutingAlgorithmTester.java @@ -17,73 +17,164 @@ */ package com.graphhopper.routing; +import com.graphhopper.routing.util.*; +import com.graphhopper.routing.weighting.FastestWeighting; import com.graphhopper.routing.weighting.ShortestWeighting; import com.graphhopper.routing.weighting.Weighting; -import com.graphhopper.routing.weighting.FastestWeighting; -import com.graphhopper.routing.util.*; import com.graphhopper.storage.*; import com.graphhopper.storage.index.LocationIndex; import com.graphhopper.storage.index.LocationIndexTree; import com.graphhopper.storage.index.QueryResult; import com.graphhopper.util.*; -import static com.graphhopper.util.Parameters.Algorithms.DIJKSTRA_BI; import gnu.trove.list.TIntList; -import java.util.*; - -import static org.junit.Assert.*; - import org.junit.Before; import org.junit.Test; +import java.util.Arrays; +import java.util.List; +import java.util.Random; + +import static com.graphhopper.util.Parameters.Algorithms.DIJKSTRA_BI; +import static org.junit.Assert.*; + /** * @author Peter Karich */ -public abstract class AbstractRoutingAlgorithmTester -{ +public abstract class AbstractRoutingAlgorithmTester { protected static final EncodingManager encodingManager = new EncodingManager("car,foot"); + private static final DistanceCalc distCalc = new DistanceCalcEarth(); protected FlagEncoder carEncoder; protected FlagEncoder footEncoder; protected AlgorithmOptions defaultOpts; + // 0-1-2-3-4 + // | / | + // | 8 | + // \ / | + // 7-6----5 + public static Graph initBiGraph(Graph graph) { + // distance will be overwritten in second step as we need to calculate it from lat,lon + graph.edge(0, 1, 1, true); + graph.edge(1, 2, 1, true); + graph.edge(2, 3, 1, true); + graph.edge(3, 4, 1, true); + graph.edge(4, 5, 1, true); + graph.edge(5, 6, 1, true); + graph.edge(6, 7, 1, true); + graph.edge(7, 0, 1, true); + graph.edge(3, 8, 1, true); + graph.edge(8, 6, 1, true); + + // we need lat,lon for edge precise queries because the distances of snapped point + // to adjacent nodes is calculated from lat,lon of the necessary points + updateDistancesFor(graph, 0, 0.001, 0); + updateDistancesFor(graph, 1, 0.100, 0.0005); + updateDistancesFor(graph, 2, 0.010, 0.0010); + updateDistancesFor(graph, 3, 0.001, 0.0011); + updateDistancesFor(graph, 4, 0.001, 0.00111); + + updateDistancesFor(graph, 8, 0.0005, 0.0011); + + updateDistancesFor(graph, 7, 0, 0); + updateDistancesFor(graph, 6, 0, 0.001); + updateDistancesFor(graph, 5, 0, 0.004); + return graph; + } + + public static void updateDistancesFor(Graph g, int node, double lat, double lon) { + NodeAccess na = g.getNodeAccess(); + na.setNode(node, lat, lon); + EdgeIterator iter = g.createEdgeExplorer().setBaseNode(node); + while (iter.next()) { + iter.setDistance(iter.fetchWayGeometry(3).calcDistance(distCalc)); + // System.out.println(node + "->" + adj + ": " + iter.getDistance()); + } + } + + protected static GraphHopperStorage createMatrixAlikeGraph(GraphHopperStorage tmpGraph) { + int WIDTH = 10; + int HEIGHT = 15; + int[][] matrix = new int[WIDTH][HEIGHT]; + int counter = 0; + Random rand = new Random(12); + boolean print = false; + for (int h = 0; h < HEIGHT; h++) { + if (print) { + for (int w = 0; w < WIDTH; w++) { + System.out.print(" |\t "); + } + System.out.println(); + } + + for (int w = 0; w < WIDTH; w++) { + matrix[w][h] = counter++; + if (h > 0) { + float dist = 5 + Math.abs(rand.nextInt(5)); + if (print) + System.out.print(" " + (int) dist + "\t "); + + tmpGraph.edge(matrix[w][h], matrix[w][h - 1], dist, true); + } + } + if (print) { + System.out.println(); + if (h > 0) { + for (int w = 0; w < WIDTH; w++) { + System.out.print(" |\t "); + } + System.out.println(); + } + } + + for (int w = 0; w < WIDTH; w++) { + if (w > 0) { + float dist = 5 + Math.abs(rand.nextInt(5)); + if (print) + System.out.print("-- " + (int) dist + "\t-- "); + tmpGraph.edge(matrix[w][h], matrix[w - 1][h], dist, true); + } + if (print) + System.out.print("(" + matrix[w][h] + ")\t"); + } + if (print) + System.out.println(); + } + + return tmpGraph; + } + @Before - public void setUp() - { + public void setUp() { carEncoder = (CarFlagEncoder) encodingManager.getEncoder("car"); footEncoder = (FootFlagEncoder) encodingManager.getEncoder("foot"); defaultOpts = AlgorithmOptions.start().flagEncoder(carEncoder). weighting(new ShortestWeighting(carEncoder)).build(); } - protected Graph getGraph( GraphHopperStorage ghStorage, Weighting weighting ) - { + protected Graph getGraph(GraphHopperStorage ghStorage, Weighting weighting) { return ghStorage.getGraph(Graph.class, weighting); } - protected GraphHopperStorage createGHStorage( EncodingManager em, List weightings, boolean is3D ) - { + protected GraphHopperStorage createGHStorage(EncodingManager em, List weightings, boolean is3D) { return new GraphBuilder(em).set3D(is3D).create(); } - protected GraphHopperStorage createGHStorage( boolean is3D ) - { + protected GraphHopperStorage createGHStorage(boolean is3D) { return createGHStorage(encodingManager, Arrays.asList(defaultOpts.getWeighting()), is3D); } - protected final RoutingAlgorithm createAlgo( GraphHopperStorage g ) - { + protected final RoutingAlgorithm createAlgo(GraphHopperStorage g) { return createAlgo(g, defaultOpts); } - protected final RoutingAlgorithm createAlgo( GraphHopperStorage ghStorage, AlgorithmOptions opts ) - { + protected final RoutingAlgorithm createAlgo(GraphHopperStorage ghStorage, AlgorithmOptions opts) { return createFactory(ghStorage, opts).createAlgo(getGraph(ghStorage, opts.getWeighting()), opts); } - public abstract RoutingAlgorithmFactory createFactory( GraphHopperStorage ghStorage, AlgorithmOptions opts ); + public abstract RoutingAlgorithmFactory createFactory(GraphHopperStorage ghStorage, AlgorithmOptions opts); @Test - public void testCalcShortestPath() - { + public void testCalcShortestPath() { GraphHopperStorage ghStorage = createTestStorage(); RoutingAlgorithm algo = createAlgo(ghStorage); Path p = algo.calcPath(0, 7); @@ -93,8 +184,7 @@ public void testCalcShortestPath() // see calc-fastest-graph.svg @Test - public void testCalcFastestPath() - { + public void testCalcFastestPath() { GraphHopperStorage graphShortest = createGHStorage(false); initDirectedAndDiffSpeed(graphShortest, carEncoder); Path p1 = createAlgo(graphShortest, defaultOpts). @@ -119,8 +209,7 @@ public void testCalcFastestPath() // 4-5-- | // |/ \--7 // 6----/ - protected void initDirectedAndDiffSpeed( Graph graph, FlagEncoder enc ) - { + protected void initDirectedAndDiffSpeed(Graph graph, FlagEncoder enc) { graph.edge(0, 1).setFlags(enc.setProperties(10, true, false)); graph.edge(0, 4).setFlags(enc.setProperties(100, true, false)); @@ -156,8 +245,7 @@ protected void initDirectedAndDiffSpeed( Graph graph, FlagEncoder enc ) } @Test - public void testCalcFootPath() - { + public void testCalcFootPath() { AlgorithmOptions opts = AlgorithmOptions.start().flagEncoder(footEncoder). weighting(new ShortestWeighting(footEncoder)).build(); GraphHopperStorage ghStorage = createGHStorage(encodingManager, Arrays.asList(opts.getWeighting()), false); @@ -169,8 +257,7 @@ public void testCalcFootPath() assertEquals(Helper.createTList(0, 4, 5, 7), p1.calcNodes()); } - protected void initFootVsCar( Graph graph ) - { + protected void initFootVsCar(Graph graph) { graph.edge(0, 1).setDistance(7000).setFlags(footEncoder.setProperties(5, true, true) | carEncoder.setProperties(10, true, false)); graph.edge(0, 4).setDistance(5000).setFlags(footEncoder.setProperties(5, true, true) | carEncoder.setProperties(20, true, false)); @@ -194,8 +281,7 @@ protected void initFootVsCar( Graph graph ) } // see test-graph.svg ! - protected GraphHopperStorage createTestStorage() - { + protected GraphHopperStorage createTestStorage() { GraphHopperStorage graph = createGHStorage(false); graph.edge(0, 1, 7, true); @@ -233,8 +319,7 @@ protected GraphHopperStorage createTestStorage() } @Test - public void testNoPathFound() - { + public void testNoPathFound() { GraphHopperStorage graph = createGHStorage(false); graph.edge(100, 101); assertFalse(createAlgo(graph).calcPath(0, 1).isFound()); @@ -261,8 +346,7 @@ public void testNoPathFound() } @Test - public void testWikipediaShortestPath() - { + public void testWikipediaShortestPath() { GraphHopperStorage ghStorage = createWikipediaTestGraph(); Path p = createAlgo(ghStorage).calcPath(0, 4); assertEquals(p.toString(), 20, p.getDistance(), 1e-4); @@ -270,16 +354,14 @@ public void testWikipediaShortestPath() } @Test - public void testCalcIf1EdgeAway() - { + public void testCalcIf1EdgeAway() { Path p = createAlgo(createTestStorage()).calcPath(1, 2); assertEquals(Helper.createTList(1, 2), p.calcNodes()); assertEquals(p.toString(), 35.1, p.getDistance(), .1); } // see wikipedia-graph.svg ! - protected GraphHopperStorage createWikipediaTestGraph() - { + protected GraphHopperStorage createWikipediaTestGraph() { GraphHopperStorage graph = createGHStorage(false); graph.edge(0, 1, 7, true); graph.edge(0, 2, 9, true); @@ -293,58 +375,8 @@ protected GraphHopperStorage createWikipediaTestGraph() return graph; } - // 0-1-2-3-4 - // | / | - // | 8 | - // \ / | - // 7-6----5 - public static Graph initBiGraph( Graph graph ) - { - // distance will be overwritten in second step as we need to calculate it from lat,lon - graph.edge(0, 1, 1, true); - graph.edge(1, 2, 1, true); - graph.edge(2, 3, 1, true); - graph.edge(3, 4, 1, true); - graph.edge(4, 5, 1, true); - graph.edge(5, 6, 1, true); - graph.edge(6, 7, 1, true); - graph.edge(7, 0, 1, true); - graph.edge(3, 8, 1, true); - graph.edge(8, 6, 1, true); - - // we need lat,lon for edge precise queries because the distances of snapped point - // to adjacent nodes is calculated from lat,lon of the necessary points - updateDistancesFor(graph, 0, 0.001, 0); - updateDistancesFor(graph, 1, 0.100, 0.0005); - updateDistancesFor(graph, 2, 0.010, 0.0010); - updateDistancesFor(graph, 3, 0.001, 0.0011); - updateDistancesFor(graph, 4, 0.001, 0.00111); - - updateDistancesFor(graph, 8, 0.0005, 0.0011); - - updateDistancesFor(graph, 7, 0, 0); - updateDistancesFor(graph, 6, 0, 0.001); - updateDistancesFor(graph, 5, 0, 0.004); - return graph; - } - - private static final DistanceCalc distCalc = new DistanceCalcEarth(); - - public static void updateDistancesFor( Graph g, int node, double lat, double lon ) - { - NodeAccess na = g.getNodeAccess(); - na.setNode(node, lat, lon); - EdgeIterator iter = g.createEdgeExplorer().setBaseNode(node); - while (iter.next()) - { - iter.setDistance(iter.fetchWayGeometry(3).calcDistance(distCalc)); - // System.out.println(node + "->" + adj + ": " + iter.getDistance()); - } - } - @Test - public void testBidirectional() - { + public void testBidirectional() { GraphHopperStorage graph = createGHStorage(false); initBiGraph(graph); @@ -361,8 +393,7 @@ public void testBidirectional() } @Test - public void testMaxVisitedNodes() - { + public void testMaxVisitedNodes() { GraphHopperStorage graph = createGHStorage(false); initBiGraph(graph); @@ -382,8 +413,7 @@ public void testMaxVisitedNodes() // \ / / // 8-7-6-/ @Test - public void testBidirectional2() - { + public void testBidirectional2() { GraphHopperStorage graph = createGHStorage(false); graph.edge(0, 1, 100, true); @@ -404,8 +434,7 @@ public void testBidirectional2() } @Test - public void testRekeyBugOfIntBinHeap() - { + public void testRekeyBugOfIntBinHeap() { // using Dijkstra + IntBinHeap then rekey loops endlessly GraphHopperStorage matrixGraph = createMatrixGraph(); Path p = createAlgo(matrixGraph).calcPath(36, 91); @@ -413,8 +442,7 @@ public void testRekeyBugOfIntBinHeap() TIntList list = p.calcNodes(); if (!Helper.createTList(36, 46, 56, 66, 76, 86, 85, 84, 94, 93, 92, 91).equals(list) - && !Helper.createTList(36, 46, 56, 66, 76, 86, 85, 84, 83, 82, 92, 91).equals(list)) - { + && !Helper.createTList(36, 46, 56, 66, 76, 86, 85, 84, 83, 82, 92, 91).equals(list)) { assertTrue("wrong locations: " + list.toString(), false); } assertEquals(66f, p.getDistance(), 1e-3); @@ -423,24 +451,21 @@ public void testRekeyBugOfIntBinHeap() testCorrectWeight(matrixGraph); } - public void testBug1( GraphHopperStorage g ) - { + public void testBug1(GraphHopperStorage g) { Path p = createAlgo(g).calcPath(34, 36); assertEquals(Helper.createTList(34, 35, 36), p.calcNodes()); assertEquals(3, p.calcNodes().size()); assertEquals(17, p.getDistance(), 1e-5); } - public void testCorrectWeight( GraphHopperStorage g ) - { + public void testCorrectWeight(GraphHopperStorage g) { Path p = createAlgo(g).calcPath(45, 72); assertEquals(Helper.createTList(45, 44, 54, 64, 74, 73, 72), p.calcNodes()); assertEquals(38f, p.getDistance(), 1e-3); } @Test - public void testCannotCalculateSP() - { + public void testCannotCalculateSP() { GraphHopperStorage graph = createGHStorage(false); graph.edge(0, 1, 1, false); graph.edge(1, 2, 1, false); @@ -450,8 +475,7 @@ public void testCannotCalculateSP() } @Test - public void testDirectedGraphBug1() - { + public void testDirectedGraphBug1() { GraphHopperStorage graph = createGHStorage(false); graph.edge(0, 1, 3, false); graph.edge(1, 2, 2.99, false); @@ -467,8 +491,7 @@ public void testDirectedGraphBug1() } @Test - public void testDirectedGraphBug2() - { + public void testDirectedGraphBug2() { GraphHopperStorage graph = createGHStorage(false); graph.edge(0, 1, 1, false); graph.edge(1, 2, 1, false); @@ -485,8 +508,7 @@ public void testDirectedGraphBug2() // | / / | // d-2--3-e-4 @Test - public void testWithCoordinates() - { + public void testWithCoordinates() { Weighting weighting = new ShortestWeighting(carEncoder); GraphHopperStorage graph = createGHStorage(encodingManager, Arrays.asList(weighting), false); @@ -521,16 +543,14 @@ public void testWithCoordinates() } @Test - public void testCalcIfEmptyWay() - { + public void testCalcIfEmptyWay() { Path p = createAlgo(createTestStorage()).calcPath(0, 0); assertEquals(p.calcNodes().toString(), 1, p.calcNodes().size()); assertEquals(p.toString(), 0, p.getDistance(), 1e-4); } @Test - public void testViaEdges_FromEqualsTo() - { + public void testViaEdges_FromEqualsTo() { GraphHopperStorage ghStorage = createTestStorage(); // identical tower nodes Path p = calcPathViaQuery(ghStorage, 0.001, 0.000, 0.001, 0.000); @@ -553,12 +573,11 @@ public void testViaEdges_FromEqualsTo() } @Test - public void testViaEdges_BiGraph() - { + public void testViaEdges_BiGraph() { GraphHopperStorage graph = createGHStorage(false); initBiGraph(graph); - // 0-7 to 4-3 + // 0-7 to 4-3 Path p = calcPathViaQuery(graph, 0.0009, 0, 0.001, 0.001105); assertEquals(p.toString(), Helper.createTList(10, 7, 6, 8, 3, 9), p.calcNodes()); assertEquals(p.toString(), 324.11, p.getDistance(), 0.01); @@ -570,8 +589,7 @@ public void testViaEdges_BiGraph() } @Test - public void testViaEdges_WithCoordinates() - { + public void testViaEdges_WithCoordinates() { GraphHopperStorage ghStorage = createTestStorage(); Path p = calcPath(ghStorage, 0, 1, 2, 3); assertEquals(Helper.createTList(9, 1, 2, 8), p.calcNodes()); @@ -579,8 +597,7 @@ public void testViaEdges_WithCoordinates() } @Test - public void testViaEdges_SpecialCases() - { + public void testViaEdges_SpecialCases() { GraphHopperStorage graph = createGHStorage(false); // 0->1\ // | 2 @@ -614,8 +631,7 @@ public void testViaEdges_SpecialCases() } @Test - public void testQueryGraphAndFastest() - { + public void testQueryGraphAndFastest() { Weighting weighting = new FastestWeighting(carEncoder); GraphHopperStorage graph = createGHStorage(encodingManager, Arrays.asList(weighting), false); initDirectedAndDiffSpeed(graph, carEncoder); @@ -624,13 +640,11 @@ public void testQueryGraphAndFastest() assertEquals(602.98, p.getDistance(), 1e-1); } - Path calcPathViaQuery( GraphHopperStorage ghStorage, double fromLat, double fromLon, double toLat, double toLon ) - { + Path calcPathViaQuery(GraphHopperStorage ghStorage, double fromLat, double fromLon, double toLat, double toLon) { return calcPathViaQuery(defaultOpts.getWeighting(), ghStorage, fromLat, fromLon, toLat, toLon); } - Path calcPathViaQuery( Weighting weighting, GraphHopperStorage ghStorage, double fromLat, double fromLon, double toLat, double toLon ) - { + Path calcPathViaQuery(Weighting weighting, GraphHopperStorage ghStorage, double fromLat, double fromLon, double toLat, double toLon) { LocationIndex index = new LocationIndexTree(ghStorage, new RAMDirectory()); index.prepareIndex(); QueryResult from = index.findClosest(fromLat, fromLon, EdgeFilter.ALL_EDGES); @@ -644,9 +658,8 @@ Path calcPathViaQuery( Weighting weighting, GraphHopperStorage ghStorage, double calcPath(from.getClosestNode(), to.getClosestNode()); } - Path calcPath( GraphHopperStorage ghStorage, int fromNode1, int fromNode2, int toNode1, int toNode2 ) - { - // lookup two edges: fromNode1-fromNode2 and toNode1-toNode2 + Path calcPath(GraphHopperStorage ghStorage, int fromNode1, int fromNode2, int toNode1, int toNode2) { + // lookup two edges: fromNode1-fromNode2 and toNode1-toNode2 QueryResult from = newQR(ghStorage, fromNode1, fromNode2); QueryResult to = newQR(ghStorage, toNode1, toNode2); @@ -658,8 +671,7 @@ Path calcPath( GraphHopperStorage ghStorage, int fromNode1, int fromNode2, int t /** * Creates query result on edge (node1-node2) very close to node1. */ - QueryResult newQR( Graph graph, int node1, int node2 ) - { + QueryResult newQR(Graph graph, int node1, int node2) { EdgeIteratorState edge = GHUtility.getEdge(graph, node1, node2); if (edge == null) throw new IllegalStateException("edge not found? " + node1 + "-" + node2); @@ -680,8 +692,7 @@ QueryResult newQR( Graph graph, int node1, int node2 ) } @Test - public void testTwoWeightsPerEdge() - { + public void testTwoWeightsPerEdge() { FlagEncoder encoder = new Bike2WeightFlagEncoder(); EncodingManager em = new EncodingManager(encoder); AlgorithmOptions opts = AlgorithmOptions.start(). @@ -692,7 +703,7 @@ public void testTwoWeightsPerEdge() // force the other path GHUtility.getEdge(graph, 0, 3).setFlags(encoder.setProperties(10, false, true)); - // for two weights per edge it happened that Path (and also the Weighting) read the wrong side + // for two weights per edge it happened that Path (and also the Weighting) read the wrong side // of the speed and read 0 => infinity weight => overflow of millis => negative millis! Path p = createAlgo(graph, opts). calcPath(0, 10); @@ -702,8 +713,7 @@ public void testTwoWeightsPerEdge() } @Test - public void test0SpeedButUnblocked_Issue242() - { + public void test0SpeedButUnblocked_Issue242() { GraphHopperStorage graph = createGHStorage(false); long flags = carEncoder.setAccess(carEncoder.setSpeed(0, 0), true, true); @@ -711,41 +721,33 @@ public void test0SpeedButUnblocked_Issue242() graph.edge(1, 2).setFlags(flags).setDistance(10); RoutingAlgorithm algo = createAlgo(graph); - try - { + try { Path p = algo.calcPath(0, 2); assertTrue(false); - } catch (Exception ex) - { + } catch (Exception ex) { assertTrue(ex.getMessage(), ex.getMessage().startsWith("Speed cannot be 0")); } } @Test - public void testTwoWeightsPerEdge2() - { + public void testTwoWeightsPerEdge2() { // other direction should be different! - Weighting fakeWeighting = new Weighting() - { + Weighting fakeWeighting = new Weighting() { @Override - public FlagEncoder getFlagEncoder() - { + public FlagEncoder getFlagEncoder() { return carEncoder; } @Override - public double getMinWeight( double distance ) - { + public double getMinWeight(double distance) { return 0.8 * distance; } @Override - public double calcWeight( EdgeIteratorState edgeState, boolean reverse, int prevOrNextEdgeId ) - { + public double calcWeight(EdgeIteratorState edgeState, boolean reverse, int prevOrNextEdgeId) { int adj = edgeState.getAdjNode(); int base = edgeState.getBaseNode(); - if (reverse) - { + if (reverse) { int tmp = base; base = adj; adj = tmp; @@ -763,14 +765,12 @@ else if (adj == 4) } @Override - public boolean matches( HintsMap map ) - { + public boolean matches(HintsMap map) { throw new UnsupportedOperationException("Not supported"); } @Override - public String getName() - { + public String getName() { return "custom"; } }; @@ -798,8 +798,7 @@ public String getName() } @Test - public void testMultipleVehicles_issue548() - { + public void testMultipleVehicles_issue548() { FastestWeighting footWeighting = new FastestWeighting(footEncoder); AlgorithmOptions footOptions = AlgorithmOptions.start().flagEncoder(footEncoder). weighting(footWeighting).build(); @@ -832,8 +831,7 @@ public void testMultipleVehicles_issue548() // 5-6-7 // | |\| // 8-9-10 - Graph initEleGraph( Graph g ) - { + Graph initEleGraph(Graph g) { g.edge(0, 1, 10, true); g.edge(0, 4, 12, true); g.edge(0, 3, 5, true); @@ -868,71 +866,7 @@ Graph initEleGraph( Graph g ) return g; } - protected GraphHopperStorage createMatrixGraph() - { + protected GraphHopperStorage createMatrixGraph() { return createMatrixAlikeGraph(createGHStorage(false)); } - - protected static GraphHopperStorage createMatrixAlikeGraph( GraphHopperStorage tmpGraph ) - { - int WIDTH = 10; - int HEIGHT = 15; - int[][] matrix = new int[WIDTH][HEIGHT]; - int counter = 0; - Random rand = new Random(12); - boolean print = false; - for (int h = 0; h < HEIGHT; h++) - { - if (print) - { - for (int w = 0; w < WIDTH; w++) - { - System.out.print(" |\t "); - } - System.out.println(); - } - - for (int w = 0; w < WIDTH; w++) - { - matrix[w][h] = counter++; - if (h > 0) - { - float dist = 5 + Math.abs(rand.nextInt(5)); - if (print) - System.out.print(" " + (int) dist + "\t "); - - tmpGraph.edge(matrix[w][h], matrix[w][h - 1], dist, true); - } - } - if (print) - { - System.out.println(); - if (h > 0) - { - for (int w = 0; w < WIDTH; w++) - { - System.out.print(" |\t "); - } - System.out.println(); - } - } - - for (int w = 0; w < WIDTH; w++) - { - if (w > 0) - { - float dist = 5 + Math.abs(rand.nextInt(5)); - if (print) - System.out.print("-- " + (int) dist + "\t-- "); - tmpGraph.edge(matrix[w][h], matrix[w - 1][h], dist, true); - } - if (print) - System.out.print("(" + matrix[w][h] + ")\t"); - } - if (print) - System.out.println(); - } - - return tmpGraph; - } } diff --git a/core/src/test/java/com/graphhopper/routing/AlternativeRouteTest.java b/core/src/test/java/com/graphhopper/routing/AlternativeRouteTest.java index 6cd3b00d908..7dbac45f9cc 100644 --- a/core/src/test/java/com/graphhopper/routing/AlternativeRouteTest.java +++ b/core/src/test/java/com/graphhopper/routing/AlternativeRouteTest.java @@ -17,61 +17,51 @@ */ package com.graphhopper.routing; +import com.graphhopper.routing.AlternativeRoute.AlternativeBidirSearch; import com.graphhopper.routing.util.CarFlagEncoder; import com.graphhopper.routing.util.EncodingManager; -import com.graphhopper.routing.weighting.FastestWeighting; import com.graphhopper.routing.util.FlagEncoder; import com.graphhopper.routing.util.TraversalMode; +import com.graphhopper.routing.weighting.FastestWeighting; import com.graphhopper.routing.weighting.Weighting; import com.graphhopper.storage.Graph; +import com.graphhopper.storage.GraphExtension; import com.graphhopper.storage.GraphHopperStorage; import com.graphhopper.storage.RAMDirectory; import com.graphhopper.util.Helper; - import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import java.util.Arrays; +import java.util.Collection; import java.util.List; import static com.graphhopper.routing.AbstractRoutingAlgorithmTester.updateDistancesFor; -import com.graphhopper.routing.AlternativeRoute.AlternativeBidirSearch; -import com.graphhopper.storage.*; -import java.util.Arrays; -import java.util.Collection; import static org.junit.Assert.*; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; @RunWith(Parameterized.class) -public class AlternativeRouteTest -{ +public class AlternativeRouteTest { private final FlagEncoder carFE = new CarFlagEncoder(); private final EncodingManager em = new EncodingManager(carFE); private final TraversalMode traversalMode; + public AlternativeRouteTest(TraversalMode tMode) { + this.traversalMode = tMode; + } + /** * Runs the same test with each of the supported traversal modes */ @Parameterized.Parameters(name = "{0}") - public static Collection configs() - { - return Arrays.asList(new Object[][] - { - { - TraversalMode.NODE_BASED - }, - { - TraversalMode.EDGE_BASED_2DIR - } + public static Collection configs() { + return Arrays.asList(new Object[][]{ + {TraversalMode.NODE_BASED}, + {TraversalMode.EDGE_BASED_2DIR} }); } - public AlternativeRouteTest( TraversalMode tMode ) - { - this.traversalMode = tMode; - } - - public GraphHopperStorage createTestGraph( boolean fullGraph, EncodingManager tmpEM ) - { + public GraphHopperStorage createTestGraph(boolean fullGraph, EncodingManager tmpEM) { GraphHopperStorage graph = new GraphHopperStorage(new RAMDirectory(), tmpEM, false, new GraphExtension.NoOpExtension()); graph.create(1000); @@ -114,8 +104,7 @@ public GraphHopperStorage createTestGraph( boolean fullGraph, EncodingManager tm } @Test - public void testCalcAlternatives() throws Exception - { + public void testCalcAlternatives() throws Exception { Weighting weighting = new FastestWeighting(carFE); GraphHopperStorage g = createTestGraph(true, em); AlternativeRoute altDijkstra = new AlternativeRoute(g, carFE, weighting, traversalMode); @@ -144,8 +133,7 @@ public void testCalcAlternatives() throws Exception } @Test - public void testCalcAlternatives2() throws Exception - { + public void testCalcAlternatives2() throws Exception { Weighting weighting = new FastestWeighting(carFE); Graph g = createTestGraph(true, em); AlternativeRoute altDijkstra = new AlternativeRoute(g, carFE, weighting, traversalMode); @@ -167,12 +155,10 @@ public void testCalcAlternatives2() throws Exception assertEquals(2416.0, pathInfos.get(2).getPath().getWeight(), .1); } - void checkAlternatives( List alternativeInfos ) - { + void checkAlternatives(List alternativeInfos) { assertFalse("alternativeInfos should contain alternatives", alternativeInfos.isEmpty()); AlternativeRoute.AlternativeInfo bestInfo = alternativeInfos.get(0); - for (int i = 1; i < alternativeInfos.size(); i++) - { + for (int i = 1; i < alternativeInfos.size(); i++) { AlternativeRoute.AlternativeInfo a = alternativeInfos.get(i); if (a.getPath().getWeight() < bestInfo.getPath().getWeight()) assertTrue("alternative is not longer -> " + a + " vs " + bestInfo, false); @@ -184,8 +170,7 @@ void checkAlternatives( List alternativeInfos } @Test - public void testDisconnectedAreas() - { + public void testDisconnectedAreas() { Graph g = createTestGraph(true, em); // one single disconnected node diff --git a/core/src/test/java/com/graphhopper/routing/DijkstraBidirectionRefTest.java b/core/src/test/java/com/graphhopper/routing/DijkstraBidirectionRefTest.java index e5681782c20..fdc8dc61cf1 100644 --- a/core/src/test/java/com/graphhopper/routing/DijkstraBidirectionRefTest.java +++ b/core/src/test/java/com/graphhopper/routing/DijkstraBidirectionRefTest.java @@ -17,54 +17,45 @@ */ package com.graphhopper.routing; -import com.graphhopper.routing.util.*; - -import java.util.Arrays; -import java.util.Collection; - +import com.graphhopper.routing.util.TraversalMode; +import com.graphhopper.storage.Graph; +import com.graphhopper.storage.GraphHopperStorage; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import org.junit.runners.Parameterized.Parameters; -import com.graphhopper.storage.Graph; -import com.graphhopper.storage.GraphHopperStorage; +import java.util.Arrays; +import java.util.Collection; /** * @author Peter Karich */ @RunWith(Parameterized.class) -public class DijkstraBidirectionRefTest extends AbstractRoutingAlgorithmTester -{ +public class DijkstraBidirectionRefTest extends AbstractRoutingAlgorithmTester { + private final TraversalMode traversalMode; + + public DijkstraBidirectionRefTest(TraversalMode tMode) { + this.traversalMode = tMode; + } + /** * Runs the same test with each of the supported traversal modes */ @Parameters(name = "{0}") - public static Collection configs() - { - return Arrays.asList(new Object[][] - { - {TraversalMode.NODE_BASED}, - {TraversalMode.EDGE_BASED_1DIR}, - {TraversalMode.EDGE_BASED_2DIR}, - {TraversalMode.EDGE_BASED_2DIR_UTURN} - }); - } - - private final TraversalMode traversalMode; - - public DijkstraBidirectionRefTest( TraversalMode tMode ) - { - this.traversalMode = tMode; + public static Collection configs() { + return Arrays.asList(new Object[][]{ + {TraversalMode.NODE_BASED}, + {TraversalMode.EDGE_BASED_1DIR}, + {TraversalMode.EDGE_BASED_2DIR}, + {TraversalMode.EDGE_BASED_2DIR_UTURN} + }); } @Override - public RoutingAlgorithmFactory createFactory( GraphHopperStorage prepareGraph, AlgorithmOptions prepareOpts ) - { - return new RoutingAlgorithmFactory() - { + public RoutingAlgorithmFactory createFactory(GraphHopperStorage prepareGraph, AlgorithmOptions prepareOpts) { + return new RoutingAlgorithmFactory() { @Override - public RoutingAlgorithm createAlgo( Graph g, AlgorithmOptions opts ) - { + public RoutingAlgorithm createAlgo(Graph g, AlgorithmOptions opts) { return new DijkstraBidirectionRef(g, opts.getFlagEncoder(), opts.getWeighting(), traversalMode); } }; diff --git a/core/src/test/java/com/graphhopper/routing/DijkstraOneToManyTest.java b/core/src/test/java/com/graphhopper/routing/DijkstraOneToManyTest.java index 561c98c7856..4e76b03e5a1 100644 --- a/core/src/test/java/com/graphhopper/routing/DijkstraOneToManyTest.java +++ b/core/src/test/java/com/graphhopper/routing/DijkstraOneToManyTest.java @@ -17,106 +17,115 @@ */ package com.graphhopper.routing; +import com.graphhopper.routing.util.EdgeFilter; +import com.graphhopper.routing.util.TraversalMode; import com.graphhopper.routing.weighting.FastestWeighting; -import com.graphhopper.routing.util.*; import com.graphhopper.storage.Graph; import com.graphhopper.storage.GraphBuilder; import com.graphhopper.storage.GraphHopperStorage; import com.graphhopper.util.EdgeIteratorState; import com.graphhopper.util.Helper; - -import java.util.Arrays; -import java.util.Collection; - -import static org.junit.Assert.*; - import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import org.junit.runners.Parameterized.Parameters; +import java.util.Arrays; +import java.util.Collection; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + /** * @author Peter Karich */ @RunWith(Parameterized.class) -public class DijkstraOneToManyTest extends AbstractRoutingAlgorithmTester -{ +public class DijkstraOneToManyTest extends AbstractRoutingAlgorithmTester { + private final TraversalMode traversalMode; + + public DijkstraOneToManyTest(TraversalMode tMode) { + this.traversalMode = tMode; + } + /** * Runs the same test with each of the supported traversal modes */ @Parameters(name = "{0}") - public static Collection configs() - { - return Arrays.asList(new Object[][] - { + public static Collection configs() { + return Arrays.asList(new Object[][]{ { TraversalMode.NODE_BASED - }, -// TODO { TraversalMode.EDGE_BASED_1DIR }, -// TODO { TraversalMode.EDGE_BASED_2DIR }, -// TODO { TraversalMode.EDGE_BASED_2DIR_UTURN } + }, // TODO { TraversalMode.EDGE_BASED_1DIR }, + // TODO { TraversalMode.EDGE_BASED_2DIR }, + // TODO { TraversalMode.EDGE_BASED_2DIR_UTURN } }); } - private final TraversalMode traversalMode; + public static Graph initGraphWeightLimit(Graph g) { + // 0----1 + // / | + // 7-- | + // / | | + // 6---5 | + // | | | + // 4---3---2 - public DijkstraOneToManyTest( TraversalMode tMode ) - { - this.traversalMode = tMode; + g.edge(0, 1, 1, true); + g.edge(1, 2, 1, true); + + g.edge(3, 2, 1, true); + g.edge(3, 5, 1, true); + g.edge(5, 7, 1, true); + g.edge(3, 4, 1, true); + g.edge(4, 6, 1, true); + g.edge(6, 7, 1, true); + g.edge(6, 5, 1, true); + g.edge(0, 7, 1, true); + return g; } @Override - public RoutingAlgorithmFactory createFactory( GraphHopperStorage prepareGraph, AlgorithmOptions prepareOpts ) - { - return new RoutingAlgorithmFactory() - { + public RoutingAlgorithmFactory createFactory(GraphHopperStorage prepareGraph, AlgorithmOptions prepareOpts) { + return new RoutingAlgorithmFactory() { @Override - public RoutingAlgorithm createAlgo( Graph g, AlgorithmOptions opts ) - { + public RoutingAlgorithm createAlgo(Graph g, AlgorithmOptions opts) { return new DijkstraOneToMany(g, opts.getFlagEncoder(), opts.getWeighting(), traversalMode); } }; } @Override - public void testViaEdges_BiGraph() - { + public void testViaEdges_BiGraph() { // calcPath with QueryResult not supported } @Override - public void testViaEdges_SpecialCases() - { + public void testViaEdges_SpecialCases() { // calcPath with QueryResult not supported } @Override - public void testViaEdges_FromEqualsTo() - { + public void testViaEdges_FromEqualsTo() { // calcPath with QueryResult not supported } @Override - public void testViaEdges_WithCoordinates() - { + public void testViaEdges_WithCoordinates() { // calcPath with QueryResult not supported } @Override - public void testQueryGraphAndFastest() - { + public void testQueryGraphAndFastest() { // calcPath with QueryResult not supported } @Override - public void testTwoWeightsPerEdge2() - { + public void testTwoWeightsPerEdge2() { // calcPath with QueryResult not supported } @Test - public void testIssue182() - { + public void testIssue182() { GraphHopperStorage storage = createGHStorage(false); initGraph(storage); RoutingAlgorithm algo = createAlgo(storage); @@ -129,8 +138,7 @@ public void testIssue182() } @Test - public void testIssue239_and362() - { + public void testIssue239_and362() { GraphHopperStorage g = createGHStorage(false); g.edge(0, 1, 1, true); g.edge(1, 2, 1, true); @@ -149,8 +157,7 @@ public void testIssue239_and362() } @Test - public void testUseCache() - { + public void testUseCache() { RoutingAlgorithm algo = createAlgo(createTestStorage()); Path p = algo.calcPath(0, 4); assertEquals(Helper.createTList(0, 4), p.calcNodes()); @@ -165,8 +172,7 @@ public void testUseCache() } @Test - public void testDifferentEdgeFilter() - { + public void testDifferentEdgeFilter() { GraphHopperStorage g = new GraphBuilder(encodingManager).setCHGraph(new FastestWeighting(carEncoder)).create(); g.edge(4, 3, 10, true); g.edge(3, 6, 10, true); @@ -175,11 +181,9 @@ public void testDifferentEdgeFilter() g.edge(5, 6, 10, true); DijkstraOneToMany algo = (DijkstraOneToMany) createAlgo(g); - algo.setEdgeFilter(new EdgeFilter() - { + algo.setEdgeFilter(new EdgeFilter() { @Override - public boolean accept( EdgeIteratorState iter ) - { + public boolean accept(EdgeIteratorState iter) { return iter.getAdjNode() != 5; } }); @@ -188,11 +192,9 @@ public boolean accept( EdgeIteratorState iter ) // important call! algo.clear(); - algo.setEdgeFilter(new EdgeFilter() - { + algo.setEdgeFilter(new EdgeFilter() { @Override - public boolean accept( EdgeIteratorState iter ) - { + public boolean accept(EdgeIteratorState iter) { return iter.getAdjNode() != 3; } }); @@ -200,8 +202,7 @@ public boolean accept( EdgeIteratorState iter ) assertEquals(Helper.createTList(4, 5, 6), p.calcNodes()); } - private Graph initGraph( Graph g ) - { + private Graph initGraph(Graph g) { // 0-1-2-3-4 // | / // 7-10---- @@ -219,8 +220,7 @@ private Graph initGraph( Graph g ) } @Test - public void testWeightLimit_issue380() - { + public void testWeightLimit_issue380() { GraphHopperStorage graph = createGHStorage(false); initGraphWeightLimit(graph); @@ -237,8 +237,7 @@ public void testWeightLimit_issue380() } @Test - public void testUseCacheZeroPath_issue707() - { + public void testUseCacheZeroPath_issue707() { RoutingAlgorithm algo = createAlgo(createTestStorage()); Path p = algo.calcPath(0, 0); @@ -256,28 +255,4 @@ public void testUseCacheZeroPath_issue707() assertEquals(Helper.createTList(0, 1, 2), p.calcNodes()); } - public static Graph initGraphWeightLimit( Graph g ) - { - // 0----1 - // / | - // 7-- | - // / | | - // 6---5 | - // | | | - // 4---3---2 - - g.edge(0, 1, 1, true); - g.edge(1, 2, 1, true); - - g.edge(3, 2, 1, true); - g.edge(3, 5, 1, true); - g.edge(5, 7, 1, true); - g.edge(3, 4, 1, true); - g.edge(4, 6, 1, true); - g.edge(6, 7, 1, true); - g.edge(6, 5, 1, true); - g.edge(0, 7, 1, true); - return g; - } - } diff --git a/core/src/test/java/com/graphhopper/routing/DijkstraTest.java b/core/src/test/java/com/graphhopper/routing/DijkstraTest.java index 13c070ef5c4..1f3de7e22e5 100644 --- a/core/src/test/java/com/graphhopper/routing/DijkstraTest.java +++ b/core/src/test/java/com/graphhopper/routing/DijkstraTest.java @@ -17,54 +17,45 @@ */ package com.graphhopper.routing; -import com.graphhopper.routing.util.*; - -import java.util.Arrays; -import java.util.Collection; - +import com.graphhopper.routing.util.TraversalMode; +import com.graphhopper.storage.Graph; +import com.graphhopper.storage.GraphHopperStorage; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import org.junit.runners.Parameterized.Parameters; -import com.graphhopper.storage.Graph; -import com.graphhopper.storage.GraphHopperStorage; +import java.util.Arrays; +import java.util.Collection; /** * @author Peter Karich */ @RunWith(Parameterized.class) -public class DijkstraTest extends AbstractRoutingAlgorithmTester -{ +public class DijkstraTest extends AbstractRoutingAlgorithmTester { + private final TraversalMode traversalMode; + + public DijkstraTest(TraversalMode tMode) { + this.traversalMode = tMode; + } + /** * Runs the same test with each of the supported traversal modes */ @Parameters(name = "{0}") - public static Collection configs() - { - return Arrays.asList(new Object[][] - { - {TraversalMode.NODE_BASED}, - {TraversalMode.EDGE_BASED_1DIR}, - {TraversalMode.EDGE_BASED_2DIR}, - {TraversalMode.EDGE_BASED_2DIR_UTURN} - }); - } - - private final TraversalMode traversalMode; - - public DijkstraTest( TraversalMode tMode ) - { - this.traversalMode = tMode; + public static Collection configs() { + return Arrays.asList(new Object[][]{ + {TraversalMode.NODE_BASED}, + {TraversalMode.EDGE_BASED_1DIR}, + {TraversalMode.EDGE_BASED_2DIR}, + {TraversalMode.EDGE_BASED_2DIR_UTURN} + }); } @Override - public RoutingAlgorithmFactory createFactory( GraphHopperStorage prepareGraph, AlgorithmOptions prepareOpts ) - { - return new RoutingAlgorithmFactory() - { + public RoutingAlgorithmFactory createFactory(GraphHopperStorage prepareGraph, AlgorithmOptions prepareOpts) { + return new RoutingAlgorithmFactory() { @Override - public RoutingAlgorithm createAlgo( Graph g, AlgorithmOptions opts ) - { + public RoutingAlgorithm createAlgo(Graph g, AlgorithmOptions opts) { return new Dijkstra(g, opts.getFlagEncoder(), opts.getWeighting(), traversalMode); } }; diff --git a/core/src/test/java/com/graphhopper/routing/EdgeBasedRoutingAlgorithmTest.java b/core/src/test/java/com/graphhopper/routing/EdgeBasedRoutingAlgorithmTest.java index 8bcf16e04d7..fc9c19c70a5 100644 --- a/core/src/test/java/com/graphhopper/routing/EdgeBasedRoutingAlgorithmTest.java +++ b/core/src/test/java/com/graphhopper/routing/EdgeBasedRoutingAlgorithmTest.java @@ -17,73 +17,49 @@ */ package com.graphhopper.routing; +import com.graphhopper.routing.util.*; +import com.graphhopper.routing.weighting.FastestWeighting; import com.graphhopper.routing.weighting.TurnWeighting; import com.graphhopper.routing.weighting.Weighting; -import com.graphhopper.routing.weighting.FastestWeighting; -import com.graphhopper.routing.util.*; -import com.graphhopper.storage.*; +import com.graphhopper.storage.Graph; +import com.graphhopper.storage.GraphBuilder; +import com.graphhopper.storage.GraphHopperStorage; +import com.graphhopper.storage.TurnCostExtension; import com.graphhopper.util.EdgeIteratorState; import com.graphhopper.util.Helper; - -import static org.junit.Assert.*; -import static com.graphhopper.util.GHUtility.*; -import static com.graphhopper.util.Parameters.Algorithms.*; - -import java.util.Arrays; -import java.util.Collection; - import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import org.junit.runners.Parameterized.Parameters; +import java.util.Arrays; +import java.util.Collection; + +import static com.graphhopper.util.GHUtility.getEdge; +import static com.graphhopper.util.Parameters.Algorithms.*; +import static org.junit.Assert.assertEquals; + /** * @author Peter Karich */ @RunWith(Parameterized.class) -public class EdgeBasedRoutingAlgorithmTest -{ - private FlagEncoder carEncoder; - - EncodingManager createEncodingManager( boolean restrictedOnly ) - { - if (restrictedOnly) - carEncoder = new CarFlagEncoder(5, 5, 1); - else - // allow for basic costs too - carEncoder = new CarFlagEncoder(5, 5, 3); - return new EncodingManager(carEncoder); - } - - @Parameters(name = "{0}") - public static Collection configs() - { - return Arrays.asList(new Object[][] - { - {DIJKSTRA}, - {DIJKSTRA_BI}, - {ASTAR}, - {ASTAR_BI} - // TODO { AlgorithmOptions.DIJKSTRA_ONE_TO_MANY } - }); - } - +public class EdgeBasedRoutingAlgorithmTest { private final String algoStr; + private FlagEncoder carEncoder; - public EdgeBasedRoutingAlgorithmTest( String algo ) - { + public EdgeBasedRoutingAlgorithmTest(String algo) { this.algoStr = algo; } - public RoutingAlgorithm createAlgo( Graph g, AlgorithmOptions opts ) - { - opts = AlgorithmOptions.start(opts).algorithm(algoStr).build(); - return new RoutingAlgorithmFactorySimple().createAlgo(g, opts); - } - - protected GraphHopperStorage createStorage( EncodingManager em ) - { - return new GraphBuilder(em).create(); + @Parameters(name = "{0}") + public static Collection configs() { + return Arrays.asList(new Object[][]{ + {DIJKSTRA}, + {DIJKSTRA_BI}, + {ASTAR}, + {ASTAR_BI} + // TODO { AlgorithmOptions.DIJKSTRA_ONE_TO_MANY } + }); } // 0---1 @@ -91,8 +67,7 @@ protected GraphHopperStorage createStorage( EncodingManager em ) // 2--3--4 // | | | // 5--6--7 - public static void initGraph( Graph g ) - { + public static void initGraph(Graph g) { g.edge(0, 1, 3, true); g.edge(0, 2, 1, true); g.edge(1, 3, 1, true); @@ -105,8 +80,25 @@ public static void initGraph( Graph g ) g.edge(6, 7, 1, true); } - private void initTurnRestrictions( Graph g, TurnCostExtension tcs, TurnCostEncoder tEncoder ) - { + EncodingManager createEncodingManager(boolean restrictedOnly) { + if (restrictedOnly) + carEncoder = new CarFlagEncoder(5, 5, 1); + else + // allow for basic costs too + carEncoder = new CarFlagEncoder(5, 5, 3); + return new EncodingManager(carEncoder); + } + + public RoutingAlgorithm createAlgo(Graph g, AlgorithmOptions opts) { + opts = AlgorithmOptions.start(opts).algorithm(algoStr).build(); + return new RoutingAlgorithmFactorySimple().createAlgo(g, opts); + } + + protected GraphHopperStorage createStorage(EncodingManager em) { + return new GraphBuilder(em).create(); + } + + private void initTurnRestrictions(Graph g, TurnCostExtension tcs, TurnCostEncoder tEncoder) { long tflags = tEncoder.getTurnFlags(true, 0); // only forward from 2-3 to 3-4 => limit 2,3->3,6 and 2,3->3,1 @@ -133,14 +125,12 @@ private void initTurnRestrictions( Graph g, TurnCostExtension tcs, TurnCostEncod tcs.addTurnInfo(getEdge(g, 3, 6).getEdge(), 6, getEdge(g, 6, 3).getEdge(), tflags); } - Weighting createWeighting( FlagEncoder encoder, TurnCostExtension tcs, double turnCosts ) - { + Weighting createWeighting(FlagEncoder encoder, TurnCostExtension tcs, double turnCosts) { return new TurnWeighting(new FastestWeighting(encoder), encoder, tcs).setDefaultUTurnCost(turnCosts); } @Test - public void testBasicTurnRestriction() - { + public void testBasicTurnRestriction() { GraphHopperStorage g = createStorage(createEncodingManager(true)); initGraph(g); TurnCostExtension tcs = (TurnCostExtension) g.getExtension(); @@ -169,8 +159,7 @@ public void testBasicTurnRestriction() } @Test - public void testUTurns() - { + public void testUTurns() { GraphHopperStorage g = createStorage(createEncodingManager(true)); initGraph(g); TurnCostExtension tcs = (TurnCostExtension) g.getExtension(); @@ -205,8 +194,7 @@ public void testUTurns() } @Test - public void testBasicTurnCosts() - { + public void testBasicTurnCosts() { GraphHopperStorage g = createStorage(createEncodingManager(false)); initGraph(g); TurnCostExtension tcs = (TurnCostExtension) g.getExtension(); diff --git a/core/src/test/java/com/graphhopper/routing/PathBidirRefTest.java b/core/src/test/java/com/graphhopper/routing/PathBidirRefTest.java index 8043a220fb5..45209e8113d 100644 --- a/core/src/test/java/com/graphhopper/routing/PathBidirRefTest.java +++ b/core/src/test/java/com/graphhopper/routing/PathBidirRefTest.java @@ -17,35 +17,34 @@ */ package com.graphhopper.routing; -import com.graphhopper.routing.util.*; -import com.graphhopper.storage.SPTEntry; +import com.graphhopper.routing.util.DefaultEdgeFilter; +import com.graphhopper.routing.util.EdgeFilter; +import com.graphhopper.routing.util.EncodingManager; +import com.graphhopper.routing.util.FlagEncoder; import com.graphhopper.storage.Graph; import com.graphhopper.storage.GraphBuilder; +import com.graphhopper.storage.SPTEntry; import com.graphhopper.util.EdgeExplorer; import com.graphhopper.util.EdgeIterator; import com.graphhopper.util.Helper; - -import static org.junit.Assert.*; - import org.junit.Test; +import static org.junit.Assert.assertEquals; + /** * @author Peter Karich */ -public class PathBidirRefTest -{ +public class PathBidirRefTest { private final EncodingManager encodingManager = new EncodingManager("car"); private FlagEncoder carEncoder = encodingManager.getEncoder("car"); private EdgeFilter carOutEdges = new DefaultEdgeFilter(carEncoder, false, true); - Graph createGraph() - { + Graph createGraph() { return new GraphBuilder(encodingManager).create(); } @Test - public void testExtract() - { + public void testExtract() { Graph g = createGraph(); g.edge(1, 2, 10, true); PathBidirRef pw = new PathBidirRef(g, carEncoder); @@ -61,8 +60,7 @@ public void testExtract() } @Test - public void testExtract2() - { + public void testExtract2() { Graph g = createGraph(); g.edge(1, 2, 10, false); g.edge(2, 3, 20, false); diff --git a/core/src/test/java/com/graphhopper/routing/PathTest.java b/core/src/test/java/com/graphhopper/routing/PathTest.java index 7788d4c49b1..85b7c07c65e 100644 --- a/core/src/test/java/com/graphhopper/routing/PathTest.java +++ b/core/src/test/java/com/graphhopper/routing/PathTest.java @@ -17,30 +17,21 @@ */ package com.graphhopper.routing; -import com.graphhopper.routing.weighting.ShortestWeighting; import com.graphhopper.routing.util.*; +import com.graphhopper.routing.weighting.ShortestWeighting; import com.graphhopper.storage.*; -import com.graphhopper.util.Helper; - -import static com.graphhopper.storage.AbstractGraphStorageTester.*; - -import com.graphhopper.util.EdgeIteratorState; -import com.graphhopper.util.Instruction; -import com.graphhopper.util.InstructionList; -import com.graphhopper.storage.SPTEntry; import com.graphhopper.util.*; +import org.junit.Test; import java.util.*; -import org.junit.Test; - +import static com.graphhopper.storage.AbstractGraphStorageTester.assertPList; import static org.junit.Assert.*; /** * @author Peter Karich */ -public class PathTest -{ +public class PathTest { private final FlagEncoder encoder = new CarFlagEncoder(); private final EncodingManager carManager = new EncodingManager(encoder); private final EncodingManager mixedEncoders = new EncodingManager(new CarFlagEncoder()); @@ -49,8 +40,7 @@ public class PathTest private final RoundaboutGraph roundaboutGraph = new RoundaboutGraph(); @Test - public void testFound() - { + public void testFound() { GraphHopperStorage g = new GraphBuilder(carManager).create(); Path p = new Path(g, encoder); assertFalse(p.isFound()); @@ -60,8 +50,7 @@ public void testFound() } @Test - public void testTime() - { + public void testTime() { FlagEncoder tmpEnc = new Bike2WeightFlagEncoder(); GraphHopperStorage g = new GraphBuilder(new EncodingManager(tmpEnc)).create(); Path p = new Path(g, tmpEnc); @@ -73,8 +62,7 @@ public void testTime() } @Test - public void testWayList() - { + public void testWayList() { GraphHopperStorage g = new GraphBuilder(carManager).create(); NodeAccess na = g.getNodeAccess(); na.setNode(0, 0.0, 0.1); @@ -163,8 +151,7 @@ public void testWayList() } @Test - public void testFindInstruction() - { + public void testFindInstruction() { Graph g = new GraphBuilder(carManager).create(); NodeAccess na = g.getNodeAccess(); na.setNode(0, 0.0, 0.0); @@ -216,106 +203,12 @@ public void testFindInstruction() assertNull(il.find(50.8, 50.25, 1000)); } - private class RoundaboutGraph - { - private final EdgeIteratorState edge3to6, edge3to9; - boolean clockwise = false; - final public Graph g = new GraphBuilder(mixedEncoders).create(); - final public NodeAccess na = g.getNodeAccess(); - List roundaboutEdges = new LinkedList(); - - private RoundaboutGraph() - { - // - // 8 - // \ - // 5 - // / \ - // 1 - 2 4 - 7 - // \ / - // 3 - // | \ - // 6 [ 9 ] edge 9 is turned off in default mode - - na.setNode(1, 52.514, 13.348); - na.setNode(2, 52.514, 13.349); - na.setNode(3, 52.5135, 13.35); - na.setNode(4, 52.514, 13.351); - na.setNode(5, 52.5145, 13.351); - na.setNode(6, 52.513, 13.35); - na.setNode(7, 52.514, 13.352); - na.setNode(8, 52.515, 13.351); - na.setNode(9, 52.513, 13.351); - - g.edge(1, 2, 5, true).setName("MainStreet 1 2"); - - // roundabout - roundaboutEdges.add(g.edge(3, 2, 5, false).setName("2-3")); - roundaboutEdges.add(g.edge(4, 3, 5, false).setName("3-4")); - roundaboutEdges.add(g.edge(5, 4, 5, false).setName("4-5")); - roundaboutEdges.add(g.edge(2, 5, 5, false).setName("5-2")); - - g.edge(4, 7, 5, true).setName("MainStreet 4 7"); - g.edge(5, 8, 5, true).setName("5-8"); - - edge3to6 = g.edge(3, 6, 5, true).setName("3-6"); - edge3to9 = g.edge(3, 9, 5, false).setName("3-9"); - - setRoundabout(clockwise); - inverse3to9(); - - } - - public void setRoundabout( boolean clockwise ) - { - for (FlagEncoder encoder : mixedEncoders.fetchEdgeEncoders()) - { - for (EdgeIteratorState edge : roundaboutEdges) - { - edge.setFlags(encoder.setAccess(edge.getFlags(), clockwise, !clockwise)); - edge.setFlags(encoder.setBool(edge.getFlags(), FlagEncoder.K_ROUNDABOUT, true)); - } - } - this.clockwise = clockwise; - } - - public void inverse3to9() - { - for (FlagEncoder encoder : mixedEncoders.fetchEdgeEncoders()) - { - long flags = edge3to9.getFlags(); - edge3to9.setFlags(encoder.setAccess(flags, !edge3to9.isForward(encoder), false)); - } - } - - public void inverse3to6() - { - for (FlagEncoder encoder : mixedEncoders.fetchEdgeEncoders()) - { - long flags = edge3to6.getFlags(); - edge3to6.setFlags(encoder.setAccess(flags, !edge3to6.isForward(encoder), true)); - } - } - - private double getAngle( int n1, int n2, int n3, int n4 ) - { - double inOrientation = Helper.ANGLE_CALC.calcOrientation(na.getLat(n1), na.getLon(n1), na.getLat(n2), na.getLon(n2)); - double outOrientation = Helper.ANGLE_CALC.calcOrientation(na.getLat(n3), na.getLon(n3), na.getLat(n4), na.getLon(n4)); - outOrientation = Helper.ANGLE_CALC.alignOrientation(inOrientation, outOrientation); - double delta = (inOrientation - outOrientation); - delta = clockwise ? (Math.PI + delta) : -1 * (Math.PI - delta); - return delta; - } - } - /** * Test roundabout instructions for different profiles */ @Test - public void testCalcInstructionsRoundabout() - { - for (FlagEncoder encoder : mixedEncoders.fetchEdgeEncoders()) - { + public void testCalcInstructionsRoundabout() { + for (FlagEncoder encoder : mixedEncoders.fetchEdgeEncoders()) { Path p = new Dijkstra(roundaboutGraph.g, encoder, new ShortestWeighting(encoder), TraversalMode.NODE_BASED) .calcPath(1, 8); assertTrue(p.isFound()); @@ -351,8 +244,7 @@ public void testCalcInstructionsRoundabout() * case starting in Roundabout */ @Test - public void testCalcInstructionsRoundaboutBegin() - { + public void testCalcInstructionsRoundaboutBegin() { Path p = new Dijkstra(roundaboutGraph.g, encoder, new ShortestWeighting(encoder), TraversalMode.NODE_BASED) .calcPath(2, 8); assertTrue(p.isFound()); @@ -367,8 +259,7 @@ public void testCalcInstructionsRoundaboutBegin() * case with one node being containig already exit */ @Test - public void testCalcInstructionsRoundaboutDirectExit() - { + public void testCalcInstructionsRoundaboutDirectExit() { roundaboutGraph.inverse3to9(); Path p = new Dijkstra(roundaboutGraph.g, encoder, new ShortestWeighting(encoder), TraversalMode.NODE_BASED) .calcPath(6, 8); @@ -386,8 +277,7 @@ public void testCalcInstructionsRoundaboutDirectExit() * case with one edge being not an exit */ @Test - public void testCalcInstructionsRoundabout2() - { + public void testCalcInstructionsRoundabout2() { roundaboutGraph.inverse3to6(); Path p = new Dijkstra(roundaboutGraph.g, encoder, new ShortestWeighting(encoder), TraversalMode.NODE_BASED) .calcPath(1, 8); @@ -406,8 +296,7 @@ public void testCalcInstructionsRoundabout2() } @Test - public void testCalcInstructionsRoundaboutIssue353() - { + public void testCalcInstructionsRoundaboutIssue353() { final Graph g = new GraphBuilder(carManager).create(); final NodeAccess na = g.getNodeAccess(); @@ -429,14 +318,14 @@ public void testCalcInstructionsRoundaboutIssue353() na.setNode(6, 52.513, 13.35); na.setNode(7, 52.514, 13.352); na.setNode(8, 52.515, 13.351); - + na.setNode(9, 52.5135, 13.349); na.setNode(10, 52.5135, 13.348); na.setNode(11, 52.514, 13.347); - + g.edge(2, 1, 5, false).setName("MainStreet 2 1"); g.edge(1, 11, 5, false).setName("MainStreet 1 11"); - + // roundabout EdgeIteratorState tmpEdge; tmpEdge = g.edge(3, 9, 2, false).setName("3-9"); @@ -474,8 +363,7 @@ public void testCalcInstructionsRoundaboutIssue353() * clockwise roundabout */ @Test - public void testCalcInstructionsRoundaboutClockwise() - { + public void testCalcInstructionsRoundaboutClockwise() { roundaboutGraph.setRoundabout(true); Path p = new Dijkstra(roundaboutGraph.g, encoder, new ShortestWeighting(encoder), TraversalMode.NODE_BASED) .calcPath(1, 8); @@ -492,15 +380,95 @@ public void testCalcInstructionsRoundaboutClockwise() assertEquals(delta, instr.getTurnAngle(), 0.01); } - List pick( String key, List> instructionJson ) - { + List pick(String key, List> instructionJson) { List list = new ArrayList(); - for (Map json : instructionJson) - { + for (Map json : instructionJson) { list.add(json.get(key).toString()); } return list; } + private class RoundaboutGraph { + final public Graph g = new GraphBuilder(mixedEncoders).create(); + final public NodeAccess na = g.getNodeAccess(); + private final EdgeIteratorState edge3to6, edge3to9; + boolean clockwise = false; + List roundaboutEdges = new LinkedList(); + + private RoundaboutGraph() { + // + // 8 + // \ + // 5 + // / \ + // 1 - 2 4 - 7 + // \ / + // 3 + // | \ + // 6 [ 9 ] edge 9 is turned off in default mode + + na.setNode(1, 52.514, 13.348); + na.setNode(2, 52.514, 13.349); + na.setNode(3, 52.5135, 13.35); + na.setNode(4, 52.514, 13.351); + na.setNode(5, 52.5145, 13.351); + na.setNode(6, 52.513, 13.35); + na.setNode(7, 52.514, 13.352); + na.setNode(8, 52.515, 13.351); + na.setNode(9, 52.513, 13.351); + + g.edge(1, 2, 5, true).setName("MainStreet 1 2"); + + // roundabout + roundaboutEdges.add(g.edge(3, 2, 5, false).setName("2-3")); + roundaboutEdges.add(g.edge(4, 3, 5, false).setName("3-4")); + roundaboutEdges.add(g.edge(5, 4, 5, false).setName("4-5")); + roundaboutEdges.add(g.edge(2, 5, 5, false).setName("5-2")); + + g.edge(4, 7, 5, true).setName("MainStreet 4 7"); + g.edge(5, 8, 5, true).setName("5-8"); + + edge3to6 = g.edge(3, 6, 5, true).setName("3-6"); + edge3to9 = g.edge(3, 9, 5, false).setName("3-9"); + + setRoundabout(clockwise); + inverse3to9(); + + } + + public void setRoundabout(boolean clockwise) { + for (FlagEncoder encoder : mixedEncoders.fetchEdgeEncoders()) { + for (EdgeIteratorState edge : roundaboutEdges) { + edge.setFlags(encoder.setAccess(edge.getFlags(), clockwise, !clockwise)); + edge.setFlags(encoder.setBool(edge.getFlags(), FlagEncoder.K_ROUNDABOUT, true)); + } + } + this.clockwise = clockwise; + } + + public void inverse3to9() { + for (FlagEncoder encoder : mixedEncoders.fetchEdgeEncoders()) { + long flags = edge3to9.getFlags(); + edge3to9.setFlags(encoder.setAccess(flags, !edge3to9.isForward(encoder), false)); + } + } + + public void inverse3to6() { + for (FlagEncoder encoder : mixedEncoders.fetchEdgeEncoders()) { + long flags = edge3to6.getFlags(); + edge3to6.setFlags(encoder.setAccess(flags, !edge3to6.isForward(encoder), true)); + } + } + + private double getAngle(int n1, int n2, int n3, int n4) { + double inOrientation = Helper.ANGLE_CALC.calcOrientation(na.getLat(n1), na.getLon(n1), na.getLat(n2), na.getLon(n2)); + double outOrientation = Helper.ANGLE_CALC.calcOrientation(na.getLat(n3), na.getLon(n3), na.getLat(n4), na.getLon(n4)); + outOrientation = Helper.ANGLE_CALC.alignOrientation(inOrientation, outOrientation); + double delta = (inOrientation - outOrientation); + delta = clockwise ? (Math.PI + delta) : -1 * (Math.PI - delta); + return delta; + } + } + } diff --git a/core/src/test/java/com/graphhopper/routing/QueryGraphTest.java b/core/src/test/java/com/graphhopper/routing/QueryGraphTest.java index 7bd42e2da33..193d21686d3 100644 --- a/core/src/test/java/com/graphhopper/routing/QueryGraphTest.java +++ b/core/src/test/java/com/graphhopper/routing/QueryGraphTest.java @@ -17,53 +17,44 @@ */ package com.graphhopper.routing; -import com.graphhopper.routing.weighting.TurnWeighting; -import com.graphhopper.routing.weighting.FastestWeighting; import com.graphhopper.routing.util.*; +import com.graphhopper.routing.weighting.FastestWeighting; +import com.graphhopper.routing.weighting.TurnWeighting; import com.graphhopper.storage.*; import com.graphhopper.storage.index.QueryResult; - -import static com.graphhopper.storage.index.QueryResult.Position.*; - import com.graphhopper.util.*; import com.graphhopper.util.shapes.GHPoint; import gnu.trove.map.TIntObjectMap; - -import java.util.Arrays; -import java.util.Collections; - import org.junit.After; +import org.junit.Before; import org.junit.Test; -import static org.junit.Assert.*; +import java.util.Arrays; -import org.junit.Before; +import static com.graphhopper.storage.index.QueryResult.Position.*; +import static org.junit.Assert.*; /** * @author Peter Karich */ -public class QueryGraphTest -{ +public class QueryGraphTest { private EncodingManager encodingManager; private FlagEncoder carEncoder; private GraphHopperStorage g; @Before - public void setUp() - { + public void setUp() { carEncoder = new CarFlagEncoder(); encodingManager = new EncodingManager(carEncoder); g = new GraphHopperStorage(new RAMDirectory(), encodingManager, false, new GraphExtension.NoOpExtension()).create(100); } @After - public void tearDown() - { + public void tearDown() { g.close(); } - void initGraph( Graph g ) - { + void initGraph(Graph g) { // // /*-*\ // 0 1 @@ -78,8 +69,7 @@ void initGraph( Graph g ) } @Test - public void testOneVirtualNode() - { + public void testOneVirtualNode() { initGraph(g); EdgeExplorer expl = g.createEdgeExplorer(); @@ -150,8 +140,7 @@ public void testOneVirtualNode() } @Test - public void testFillVirtualEdges() - { + public void testFillVirtualEdges() { initGraph(g); g.getNodeAccess().setNode(3, 0, 1); g.edge(1, 3); @@ -160,12 +149,10 @@ public void testFillVirtualEdges() EdgeIterator iter = g.createEdgeExplorer().setBaseNode(baseNode); iter.next(); QueryResult res1 = createLocationResult(2, 1.7, iter, 1, PILLAR); - QueryGraph queryGraph = new QueryGraph(g) - { + QueryGraph queryGraph = new QueryGraph(g) { @Override - void fillVirtualEdges( TIntObjectMap node2Edge, int towerNode, EdgeExplorer mainExpl ) - { + void fillVirtualEdges(TIntObjectMap node2Edge, int towerNode, EdgeExplorer mainExpl) { super.fillVirtualEdges(node2Edge, towerNode, mainExpl); // ignore nodes should include baseNode == 1 if (towerNode == 3) @@ -186,8 +173,7 @@ else if (towerNode == 1) } @Test - public void testMultipleVirtualNodes() - { + public void testMultipleVirtualNodes() { initGraph(g); // snap to edge which has pillar nodes @@ -232,8 +218,7 @@ public void testMultipleVirtualNodes() } @Test - public void testOneWay() - { + public void testOneWay() { NodeAccess na = g.getNodeAccess(); na.setNode(0, 0, 0); na.setNode(1, 0, 1); @@ -257,8 +242,7 @@ public void testOneWay() } @Test - public void testVirtEdges() - { + public void testVirtEdges() { initGraph(g); EdgeIterator iter = g.createEdgeExplorer().setBaseNode(0); @@ -271,8 +255,7 @@ public void testVirtEdges() } @Test - public void testUseMeanElevation() - { + public void testUseMeanElevation() { g.close(); g = new GraphHopperStorage(new RAMDirectory(), encodingManager, true, new GraphExtension.NoOpExtension()).create(100); NodeAccess na = g.getNodeAccess(); @@ -298,8 +281,7 @@ public void testUseMeanElevation() } @Test - public void testLoopStreet_Issue151() - { + public void testLoopStreet_Issue151() { // do query at x should result in ignoring only the bottom edge 1-3 not the upper one => getNeighbors are 0, 5, 3 and not only 0, 5 // // 0--1--3--4 @@ -328,8 +310,7 @@ public void testLoopStreet_Issue151() } @Test - public void testOneWayLoop_Issue162() - { + public void testOneWayLoop_Issue162() { // do query at x, where edge is oneway // // |\ @@ -368,8 +349,7 @@ public void testOneWayLoop_Issue162() } @Test - public void testEdgesShareOneNode() - { + public void testEdgesShareOneNode() { initGraph(g); EdgeIteratorState iter = GHUtility.getEdge(g, 0, 2); @@ -385,8 +365,7 @@ public void testEdgesShareOneNode() } @Test - public void testAvoidDuplicateVirtualNodesIfIdentical() - { + public void testAvoidDuplicateVirtualNodesIfIdentical() { initGraph(g); EdgeIteratorState edgeState = GHUtility.getEdge(g, 0, 2); @@ -414,8 +393,7 @@ public void testAvoidDuplicateVirtualNodesIfIdentical() } @Test - public void testGetEdgeProps() - { + public void testGetEdgeProps() { initGraph(g); EdgeIteratorState e1 = GHUtility.getEdge(g, 0, 2); QueryGraph queryGraph = new QueryGraph(g); @@ -427,17 +405,15 @@ public void testGetEdgeProps() assertEquals(e1.getEdge(), e2.getEdge()); } - PointList getPoints( Graph g, int base, int adj ) - { + PointList getPoints(Graph g, int base, int adj) { EdgeIteratorState edge = GHUtility.getEdge(g, base, adj); if (edge == null) throw new IllegalStateException("edge " + base + "-" + adj + " not found"); return edge.fetchWayGeometry(3); } - public QueryResult createLocationResult( double lat, double lon, - EdgeIteratorState edge, int wayIndex, QueryResult.Position pos ) - { + public QueryResult createLocationResult(double lat, double lon, + EdgeIteratorState edge, int wayIndex, QueryResult.Position pos) { if (edge == null) throw new IllegalStateException("Specify edge != null"); QueryResult tmp = new QueryResult(lat, lon); @@ -449,8 +425,7 @@ public QueryResult createLocationResult( double lat, double lon, } @Test - public void testIteration_Issue163() - { + public void testIteration_Issue163() { EdgeFilter outEdgeFilter = new DefaultEdgeFilter(encodingManager.getEncoder("car"), false, true); EdgeFilter inEdgeFilter = new DefaultEdgeFilter(encodingManager.getEncoder("car"), true, false); EdgeExplorer inExplorer = g.createEdgeExplorer(inEdgeFilter); @@ -491,8 +466,7 @@ public void testIteration_Issue163() assertEdgeIdsStayingEqual(inExplorer, outExplorer, nodeD, nodeB); } - private void assertEdgeIdsStayingEqual( EdgeExplorer inExplorer, EdgeExplorer outExplorer, int startNode, int endNode ) - { + private void assertEdgeIdsStayingEqual(EdgeExplorer inExplorer, EdgeExplorer outExplorer, int startNode, int endNode) { EdgeIterator it = outExplorer.setBaseNode(startNode); it.next(); assertEquals(startNode, it.getBaseNode()); @@ -511,8 +485,7 @@ private void assertEdgeIdsStayingEqual( EdgeExplorer inExplorer, EdgeExplorer ou } @Test - public void testTurnCostsProperlyPropagated_Issue282() - { + public void testTurnCostsProperlyPropagated_Issue282() { TurnCostExtension turnExt = new TurnCostExtension(); FlagEncoder encoder = new CarFlagEncoder(5, 5, 15); @@ -550,8 +523,7 @@ public void testTurnCostsProperlyPropagated_Issue282() graphWithTurnCosts.close(); } - private void initHorseshoeGraph( Graph g ) - { + private void initHorseshoeGraph(Graph g) { // setup graph // ____ // | | @@ -563,8 +535,7 @@ private void initHorseshoeGraph( Graph g ) g.edge(0, 1, 10, true).setWayGeometry(Helper.createPointList(2, 0, 2, 2)); } - private QueryResult fakeEdgeQueryResult( EdgeIteratorState edge, double lat, double lon, int wayIndex ) - { + private QueryResult fakeEdgeQueryResult(EdgeIteratorState edge, double lat, double lon, int wayIndex) { QueryResult qr = new QueryResult(lat, lon); qr.setClosestEdge(edge); qr.setWayIndex(wayIndex); @@ -573,15 +544,13 @@ private QueryResult fakeEdgeQueryResult( EdgeIteratorState edge, double lat, dou return qr; } - private boolean isAvoidEdge( QueryGraph queryGraph, int virtualEdgeTypeId, boolean _default ) - { + private boolean isAvoidEdge(QueryGraph queryGraph, int virtualEdgeTypeId, boolean _default) { boolean avoidEdge = queryGraph.virtualEdges.get(virtualEdgeTypeId).getBool(EdgeIteratorState.K_UNFAVORED_EDGE, _default); return avoidEdge; } @Test - public void testEnforceHeading() - { + public void testEnforceHeading() { initHorseshoeGraph(g); EdgeIteratorState edge = GHUtility.getEdge(g, 0, 1); @@ -633,8 +602,7 @@ public void testEnforceHeading() } @Test - public void testEnforceHeadingByEdgeId() - { + public void testEnforceHeadingByEdgeId() { initHorseshoeGraph(g); EdgeIteratorState edge = GHUtility.getEdge(g, 0, 1); @@ -664,8 +632,7 @@ public void testEnforceHeadingByEdgeId() } @Test - public void testInternalAPIOriginalTraversalKey() - { + public void testInternalAPIOriginalTraversalKey() { initGraph(g); EdgeExplorer explorer = g.createEdgeExplorer(); @@ -693,17 +660,16 @@ public void testInternalAPIOriginalTraversalKey() } @Test - public void useEECache() - { - initGraph(g); - EdgeExplorer explorer = g.createEdgeExplorer(); + public void useEECache() { + initGraph(g); + EdgeExplorer explorer = g.createEdgeExplorer(); EdgeIterator iter = explorer.setBaseNode(1); - assertTrue(iter.next()); + assertTrue(iter.next()); QueryResult res = createLocationResult(2, 1.5, iter, 1, PILLAR); - - QueryGraph queryGraph = new QueryGraph(g).setUseEdgeExplorerCache(true); + + QueryGraph queryGraph = new QueryGraph(g).setUseEdgeExplorerCache(true); queryGraph.lookup(Arrays.asList(res)); - + EdgeExplorer edgeExplorer = queryGraph.createEdgeExplorer(); // using cache means same reference assertTrue(edgeExplorer == queryGraph.createEdgeExplorer()); diff --git a/core/src/test/java/com/graphhopper/routing/RoutingAlgorithmIT.java b/core/src/test/java/com/graphhopper/routing/RoutingAlgorithmIT.java index e51bfa28eb7..9bd37f8109e 100644 --- a/core/src/test/java/com/graphhopper/routing/RoutingAlgorithmIT.java +++ b/core/src/test/java/com/graphhopper/routing/RoutingAlgorithmIT.java @@ -17,37 +17,82 @@ */ package com.graphhopper.routing; -import com.graphhopper.routing.weighting.ShortestWeighting; -import com.graphhopper.routing.weighting.Weighting; import com.graphhopper.reader.PrinctonReader; import com.graphhopper.routing.ch.PrepareContractionHierarchies; -import com.graphhopper.routing.util.*; import com.graphhopper.routing.util.EncodingManager; +import com.graphhopper.routing.util.FlagEncoder; import com.graphhopper.routing.util.TestAlgoCollector.AlgoHelperEntry; +import com.graphhopper.routing.util.TraversalMode; +import com.graphhopper.routing.weighting.ShortestWeighting; +import com.graphhopper.routing.weighting.Weighting; import com.graphhopper.storage.*; import com.graphhopper.storage.index.LocationIndex; import com.graphhopper.storage.index.LocationIndexTree; -import static com.graphhopper.util.Parameters.Algorithms.*; import com.graphhopper.util.StopWatch; +import org.junit.Test; import java.io.IOException; -import java.util.*; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.Random; import java.util.zip.GZIPInputStream; -import static org.junit.Assert.*; - -import org.junit.Test; +import static com.graphhopper.util.Parameters.Algorithms.*; +import static org.junit.Assert.assertTrue; /** * Try algorithms, indices and graph storages with real data *

    + * * @author Peter Karich */ -public class RoutingAlgorithmIT -{ +public class RoutingAlgorithmIT { + public static List createAlgos(GraphHopperStorage ghStorage, + LocationIndex idx, final FlagEncoder encoder, boolean withCh, + final TraversalMode tMode, final Weighting weighting, + final EncodingManager manager) { + List prepare = new ArrayList(); + prepare.add(new AlgoHelperEntry(ghStorage, ghStorage, new AlgorithmOptions(ASTAR, encoder, weighting, tMode), idx)); + // later: include dijkstraOneToMany + prepare.add(new AlgoHelperEntry(ghStorage, ghStorage, new AlgorithmOptions(DIJKSTRA, encoder, weighting, tMode), idx)); + + final AlgorithmOptions astarbiOpts = new AlgorithmOptions(ASTAR_BI, encoder, weighting, tMode); + astarbiOpts.getHints().put(ASTAR_BI + ".approximation", "BeelineSimplification"); + final AlgorithmOptions dijkstrabiOpts = new AlgorithmOptions(DIJKSTRA_BI, encoder, weighting, tMode); + prepare.add(new AlgoHelperEntry(ghStorage, ghStorage, astarbiOpts, idx)); + prepare.add(new AlgoHelperEntry(ghStorage, ghStorage, dijkstrabiOpts, idx)); + + if (withCh) { + GraphHopperStorage storageCopy = new GraphBuilder(manager). + set3D(ghStorage.getNodeAccess().is3D()).setCHGraph(weighting). + create(); + ghStorage.copyTo(storageCopy); + storageCopy.freeze(); + final CHGraph graphCH = storageCopy.getGraph(CHGraph.class, weighting); + final PrepareContractionHierarchies prepareCH = new PrepareContractionHierarchies( + new GHDirectory("", DAType.RAM_INT), storageCopy, graphCH, encoder, weighting, tMode); + prepareCH.doWork(); + LocationIndex idxCH = new LocationIndexTree(storageCopy, new RAMDirectory()).prepareIndex(); + prepare.add(new AlgoHelperEntry(graphCH, storageCopy, dijkstrabiOpts, idxCH) { + @Override + public RoutingAlgorithm createAlgo(Graph qGraph) { + return prepareCH.createAlgo(qGraph, dijkstrabiOpts); + } + }); + + prepare.add(new AlgoHelperEntry(graphCH, storageCopy, astarbiOpts, idxCH) { + @Override + public RoutingAlgorithm createAlgo(Graph qGraph) { + return prepareCH.createAlgo(qGraph, astarbiOpts); + } + }); + } + return prepare; + } + @Test - public void testPerformance() throws IOException - { + public void testPerformance() throws IOException { int N = 10; int noJvmWarming = N / 4; @@ -60,11 +105,9 @@ public void testPerformance() throws IOException new PrinctonReader(graph).setStream(new GZIPInputStream(PrinctonReader.class.getResourceAsStream(bigFile))).read(); Collection prepares = createAlgos(graph, null, encoder, false, TraversalMode.NODE_BASED, new ShortestWeighting(encoder), eManager); - for (AlgoHelperEntry entry : prepares) - { + for (AlgoHelperEntry entry : prepares) { StopWatch sw = new StopWatch(); - for (int i = 0; i < N; i++) - { + for (int i = 0; i < N; i++) { int node1 = Math.abs(rand.nextInt(graph.getNodes())); int node2 = Math.abs(rand.nextInt(graph.getNodes())); RoutingAlgorithm d = entry.createAlgo(graph); @@ -85,53 +128,4 @@ public void testPerformance() throws IOException assertTrue("speed to low!? " + perRun + " per run", perRun < 0.08); } } - - public static List createAlgos( GraphHopperStorage ghStorage, - LocationIndex idx, final FlagEncoder encoder, boolean withCh, - final TraversalMode tMode, final Weighting weighting, - final EncodingManager manager ) - { - List prepare = new ArrayList(); - prepare.add(new AlgoHelperEntry(ghStorage, ghStorage, new AlgorithmOptions(ASTAR, encoder, weighting, tMode), idx)); - // later: include dijkstraOneToMany - prepare.add(new AlgoHelperEntry(ghStorage, ghStorage, new AlgorithmOptions(DIJKSTRA, encoder, weighting, tMode), idx)); - - final AlgorithmOptions astarbiOpts = new AlgorithmOptions(ASTAR_BI, encoder, weighting, tMode); - astarbiOpts.getHints().put(ASTAR_BI + ".approximation", "BeelineSimplification"); - final AlgorithmOptions dijkstrabiOpts = new AlgorithmOptions(DIJKSTRA_BI, encoder, weighting, tMode); - prepare.add(new AlgoHelperEntry(ghStorage, ghStorage, astarbiOpts, idx)); - prepare.add(new AlgoHelperEntry(ghStorage, ghStorage, dijkstrabiOpts, idx)); - - if (withCh) - { - GraphHopperStorage storageCopy = new GraphBuilder(manager). - set3D(ghStorage.getNodeAccess().is3D()).setCHGraph(weighting). - create(); - ghStorage.copyTo(storageCopy); - storageCopy.freeze(); - final CHGraph graphCH = storageCopy.getGraph(CHGraph.class, weighting); - final PrepareContractionHierarchies prepareCH = new PrepareContractionHierarchies( - new GHDirectory("", DAType.RAM_INT), storageCopy, graphCH, encoder, weighting, tMode); - prepareCH.doWork(); - LocationIndex idxCH = new LocationIndexTree(storageCopy, new RAMDirectory()).prepareIndex(); - prepare.add(new AlgoHelperEntry(graphCH, storageCopy, dijkstrabiOpts, idxCH) - { - @Override - public RoutingAlgorithm createAlgo( Graph qGraph ) - { - return prepareCH.createAlgo(qGraph, dijkstrabiOpts); - } - }); - - prepare.add(new AlgoHelperEntry(graphCH, storageCopy, astarbiOpts, idxCH) - { - @Override - public RoutingAlgorithm createAlgo( Graph qGraph ) - { - return prepareCH.createAlgo(qGraph, astarbiOpts); - } - }); - } - return prepare; - } } diff --git a/core/src/test/java/com/graphhopper/routing/ch/CHAlgoFactoryDecoratorTest.java b/core/src/test/java/com/graphhopper/routing/ch/CHAlgoFactoryDecoratorTest.java index ac68fba513e..ec94968a9be 100644 --- a/core/src/test/java/com/graphhopper/routing/ch/CHAlgoFactoryDecoratorTest.java +++ b/core/src/test/java/com/graphhopper/routing/ch/CHAlgoFactoryDecoratorTest.java @@ -18,20 +18,19 @@ package com.graphhopper.routing.ch; import org.junit.Test; -import static org.junit.Assert.*; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; /** - * * @author Peter Karich */ -public class CHAlgoFactoryDecoratorTest -{ +public class CHAlgoFactoryDecoratorTest { @Test - public void testCreatePreparations() - { + public void testCreatePreparations() { CHAlgoFactoryDecorator instance = new CHAlgoFactoryDecorator(); assertFalse(instance.isDisablingAllowed()); - + instance.setEnabled(false); assertTrue(instance.isDisablingAllowed()); } diff --git a/core/src/test/java/com/graphhopper/routing/ch/DijkstraBidirectionCHTest.java b/core/src/test/java/com/graphhopper/routing/ch/DijkstraBidirectionCHTest.java index f298d5f5c4b..eee4e46e228 100644 --- a/core/src/test/java/com/graphhopper/routing/ch/DijkstraBidirectionCHTest.java +++ b/core/src/test/java/com/graphhopper/routing/ch/DijkstraBidirectionCHTest.java @@ -17,49 +17,46 @@ */ package com.graphhopper.routing.ch; -import com.graphhopper.routing.weighting.ShortestWeighting; -import com.graphhopper.routing.weighting.Weighting; -import com.graphhopper.routing.weighting.FastestWeighting; import com.graphhopper.routing.*; import com.graphhopper.routing.util.*; +import com.graphhopper.routing.weighting.FastestWeighting; +import com.graphhopper.routing.weighting.ShortestWeighting; +import com.graphhopper.routing.weighting.Weighting; import com.graphhopper.storage.*; -import com.graphhopper.util.EdgeIteratorState; import com.graphhopper.util.CHEdgeIteratorState; +import com.graphhopper.util.EdgeIteratorState; import com.graphhopper.util.Helper; import com.graphhopper.util.Parameters; +import org.junit.Test; + import java.util.Arrays; import java.util.List; -import static org.junit.Assert.*; - -import org.junit.Test; +import static org.junit.Assert.assertEquals; /** * Tests if a graph optimized by contraction hierarchies returns the same results as a none * optimized one. Additionally fine grained path unpacking is tested. *

    + * * @author Peter Karich */ -public class DijkstraBidirectionCHTest extends AbstractRoutingAlgorithmTester -{ +public class DijkstraBidirectionCHTest extends AbstractRoutingAlgorithmTester { @Override - protected CHGraph getGraph( GraphHopperStorage ghStorage, Weighting weighting ) - { + protected CHGraph getGraph(GraphHopperStorage ghStorage, Weighting weighting) { return ghStorage.getGraph(CHGraph.class, weighting); } @Override - protected GraphHopperStorage createGHStorage( EncodingManager em, - List weightings, boolean is3D ) - { + protected GraphHopperStorage createGHStorage(EncodingManager em, + List weightings, boolean is3D) { return new GraphHopperStorage(weightings, new RAMDirectory(), em, is3D, new GraphExtension.NoOpExtension()). create(1000); } @Override - public RoutingAlgorithmFactory createFactory( GraphHopperStorage ghStorage, AlgorithmOptions opts ) - { + public RoutingAlgorithmFactory createFactory(GraphHopperStorage ghStorage, AlgorithmOptions opts) { PrepareContractionHierarchies ch = new PrepareContractionHierarchies(new GHDirectory("", DAType.RAM_INT), ghStorage, getGraph(ghStorage, opts.getWeighting()), opts.getFlagEncoder(), opts.getWeighting(), TraversalMode.NODE_BASED); @@ -68,8 +65,7 @@ ghStorage, getGraph(ghStorage, opts.getWeighting()), } @Test - public void testPathRecursiveUnpacking() - { + public void testPathRecursiveUnpacking() { // use an encoder where it is possible to store 2 weights per edge FlagEncoder encoder = new Bike2WeightFlagEncoder(); EncodingManager em = new EncodingManager(encoder); @@ -119,8 +115,7 @@ public void testPathRecursiveUnpacking() } @Test - public void testBaseGraph() - { + public void testBaseGraph() { CarFlagEncoder carFE = new CarFlagEncoder(); EncodingManager em = new EncodingManager(carFE); AlgorithmOptions opts = AlgorithmOptions.start().flagEncoder(carFE). @@ -141,8 +136,7 @@ public void testBaseGraph() } @Test - public void testBaseGraphMultipleVehicles() - { + public void testBaseGraphMultipleVehicles() { AlgorithmOptions footOptions = AlgorithmOptions.start().flagEncoder(footEncoder). weighting(new FastestWeighting(footEncoder)).build(); AlgorithmOptions carOptions = AlgorithmOptions.start().flagEncoder(carEncoder). diff --git a/core/src/test/java/com/graphhopper/routing/ch/PrepareContractionHierarchiesTest.java b/core/src/test/java/com/graphhopper/routing/ch/PrepareContractionHierarchiesTest.java index 16b59882736..1fe97d29928 100644 --- a/core/src/test/java/com/graphhopper/routing/ch/PrepareContractionHierarchiesTest.java +++ b/core/src/test/java/com/graphhopper/routing/ch/PrepareContractionHierarchiesTest.java @@ -17,45 +17,118 @@ */ package com.graphhopper.routing.ch; -import com.graphhopper.routing.weighting.ShortestWeighting; -import com.graphhopper.routing.weighting.Weighting; -import com.graphhopper.routing.weighting.FastestWeighting; import com.graphhopper.routing.*; import com.graphhopper.routing.ch.PrepareContractionHierarchies.Shortcut; -import com.graphhopper.routing.util.*; +import com.graphhopper.routing.util.BikeFlagEncoder; +import com.graphhopper.routing.util.CarFlagEncoder; +import com.graphhopper.routing.util.EncodingManager; +import com.graphhopper.routing.util.TraversalMode; +import com.graphhopper.routing.weighting.FastestWeighting; +import com.graphhopper.routing.weighting.ShortestWeighting; +import com.graphhopper.routing.weighting.Weighting; import com.graphhopper.storage.*; import com.graphhopper.util.*; -import static com.graphhopper.util.Parameters.Algorithms.DIJKSTRA_BI; import gnu.trove.list.TIntList; -import java.util.Arrays; +import org.junit.Before; +import org.junit.Test; +import java.util.Arrays; import java.util.Collection; import java.util.Iterator; import java.util.List; +import static com.graphhopper.util.Parameters.Algorithms.DIJKSTRA_BI; import static org.junit.Assert.*; -import org.junit.Before; -import org.junit.Test; - /** * @author Peter Karich */ -public class PrepareContractionHierarchiesTest -{ +public class PrepareContractionHierarchiesTest { private final CarFlagEncoder carEncoder = new CarFlagEncoder(); private final EncodingManager encodingManager = new EncodingManager(carEncoder); private final Weighting weighting = new ShortestWeighting(carEncoder); private final TraversalMode tMode = TraversalMode.NODE_BASED; private Directory dir; - GraphHopperStorage createGHStorage() - { + // 0-1-.....-9-10 + // | ^ \ + // | | | + // 17-16-...-11<-/ + public static void initDirected2(Graph g) { + g.edge(0, 1, 1, true); + g.edge(1, 2, 1, true); + g.edge(2, 3, 1, true); + g.edge(3, 4, 1, true); + g.edge(4, 5, 1, true); + g.edge(5, 6, 1, true); + g.edge(6, 7, 1, true); + g.edge(7, 8, 1, true); + g.edge(8, 9, 1, true); + g.edge(9, 10, 1, true); + g.edge(10, 11, 1, false); + g.edge(11, 12, 1, true); + g.edge(11, 9, 3, false); + g.edge(12, 13, 1, true); + g.edge(13, 14, 1, true); + g.edge(14, 15, 1, true); + g.edge(15, 16, 1, true); + g.edge(16, 17, 1, true); + g.edge(17, 0, 1, true); + } + + // 8 + // | + // 6->0->1->3->7 + // | | + // | v + //10<-2---4<---5 + // 9 + public static void initDirected1(Graph g) { + g.edge(0, 8, 1, true); + g.edge(0, 1, 1, false); + g.edge(1, 3, 1, false); + g.edge(3, 7, 1, false); + g.edge(3, 5, 1, false); + g.edge(5, 4, 1, false); + g.edge(4, 2, 1, true); + g.edge(2, 9, 1, false); + g.edge(2, 10, 1, false); + g.edge(2, 6, 1, true); + g.edge(6, 0, 1, false); + } + + // prepare-routing.svg + public static Graph initShortcutsGraph(Graph g) { + g.edge(0, 1, 1, true); + g.edge(0, 2, 1, true); + g.edge(1, 2, 1, true); + g.edge(2, 3, 1.5, true); + g.edge(1, 4, 1, true); + g.edge(2, 9, 1, true); + g.edge(9, 3, 1, true); + g.edge(10, 3, 1, true); + g.edge(4, 5, 1, true); + g.edge(5, 6, 1, true); + g.edge(6, 7, 1, true); + g.edge(7, 8, 1, true); + g.edge(8, 9, 1, true); + g.edge(4, 11, 1, true); + g.edge(9, 14, 1, true); + g.edge(10, 14, 1, true); + g.edge(11, 12, 1, true); + g.edge(12, 15, 1, true); + g.edge(12, 13, 1, true); + g.edge(13, 16, 1, true); + g.edge(15, 16, 2, true); + g.edge(14, 16, 1, true); + return g; + } + + GraphHopperStorage createGHStorage() { return new GraphBuilder(encodingManager).setCHGraph(weighting).create(); } - GraphHopperStorage createExampleGraph() - { + GraphHopperStorage createExampleGraph() { GraphHopperStorage g = createGHStorage(); //5-1-----2 @@ -75,14 +148,12 @@ GraphHopperStorage createExampleGraph() } @Before - public void setUp() - { + public void setUp() { dir = new GHDirectory("", DAType.RAM_INT); } @Test - public void testShortestPathSkipNode() - { + public void testShortestPathSkipNode() { GraphHopperStorage g = createExampleGraph(); double normalDist = new Dijkstra(g, carEncoder, weighting, tMode).calcPath(4, 2).getDistance(); DijkstraOneToMany algo = new DijkstraOneToMany(g, carEncoder, weighting, tMode); @@ -102,8 +173,7 @@ public void testShortestPathSkipNode() } @Test - public void testShortestPathSkipNode2() - { + public void testShortestPathSkipNode2() { GraphHopperStorage g = createExampleGraph(); CHGraph lg = g.getGraph(CHGraph.class); double normalDist = new Dijkstra(g, carEncoder, weighting, tMode).calcPath(4, 2).getDistance(); @@ -121,8 +191,7 @@ public void testShortestPathSkipNode2() } @Test - public void testShortestPathLimit() - { + public void testShortestPathLimit() { GraphHopperStorage g = createExampleGraph(); CHGraph lg = g.getGraph(CHGraph.class); @@ -137,8 +206,7 @@ public void testShortestPathLimit() } @Test - public void testAddShortcuts() - { + public void testAddShortcuts() { GraphHopperStorage g = createExampleGraph(); CHGraph lg = g.getGraph(CHGraph.class); int old = lg.getAllEdges().getMaxId(); @@ -148,8 +216,7 @@ public void testAddShortcuts() } @Test - public void testMoreComplexGraph() - { + public void testMoreComplexGraph() { GraphHopperStorage g = createGHStorage(); CHGraph lg = g.getGraph(CHGraph.class); initShortcutsGraph(lg); @@ -161,8 +228,7 @@ public void testMoreComplexGraph() } @Test - public void testDirectedGraph() - { + public void testDirectedGraph() { GraphHopperStorage g = createGHStorage(); CHGraph lg = g.getGraph(CHGraph.class); g.edge(5, 4, 3, false); @@ -185,8 +251,7 @@ public void testDirectedGraph() } @Test - public void testDirectedGraph2() - { + public void testDirectedGraph2() { GraphHopperStorage g = createGHStorage(); CHGraph lg = g.getGraph(CHGraph.class); initDirected2(g); @@ -208,8 +273,7 @@ public void testDirectedGraph2() } @Test - public void testDirectedGraph3() - { + public void testDirectedGraph3() { GraphHopperStorage g = createGHStorage(); CHGraphImpl lg = (CHGraphImpl) g.getGraph(CHGraph.class); //5 6 7 @@ -221,7 +285,7 @@ public void testDirectedGraph3() g.edge(0, 2, 2, true); g.edge(10, 2, 2, true); g.edge(11, 2, 2, true); - // create a longer one directional edge => no longish one-dir shortcut should be created + // create a longer one directional edge => no longish one-dir shortcut should be created g.edge(2, 1, 2, true); g.edge(2, 1, 10, false); @@ -241,8 +305,7 @@ public void testDirectedGraph3() Iterator iter = scs.iterator(); Shortcut sc1 = iter.next(); Shortcut sc2 = iter.next(); - if (sc1.weight > sc2.weight) - { + if (sc1.weight > sc2.weight) { Shortcut tmp = sc1; sc1 = sc2; sc2 = tmp; @@ -260,8 +323,7 @@ public void testDirectedGraph3() assertEquals(sc2.toString(), 12, sc2.weight, 1e-4); } - void initRoundaboutGraph( Graph g ) - { + void initRoundaboutGraph(Graph g) { // roundabout: //16-0-9-10--11 12<-13 // \ \ / \ @@ -313,8 +375,7 @@ void initRoundaboutGraph( Graph g ) } @Test - public void testRoundaboutUnpacking() - { + public void testRoundaboutUnpacking() { GraphHopperStorage g = createGHStorage(); CHGraph lg = g.getGraph(CHGraph.class); initRoundaboutGraph(g); @@ -329,8 +390,7 @@ public void testRoundaboutUnpacking() } @Test - public void testFindShortcuts_Roundabout() - { + public void testFindShortcuts_Roundabout() { GraphHopperStorage ghStorage = createGHStorage(); CHGraph lg = ghStorage.getGraph(CHGraph.class); EdgeIteratorState iter1_3 = ghStorage.edge(1, 3, 1, true); @@ -371,8 +431,7 @@ public void testFindShortcuts_Roundabout() Iterator iter = sc.iterator(); Shortcut sc1 = iter.next(); Shortcut sc2 = iter.next(); - if (sc1.from > sc2.from) - { + if (sc1.from > sc2.from) { Shortcut tmpSc = sc1; sc1 = sc2; sc2 = tmpSc; @@ -382,8 +441,7 @@ public void testFindShortcuts_Roundabout() assertEquals("6->1, weight:5.0 (9,7)", sc2.toString()); } - void initUnpackingGraph( GraphHopperStorage ghStorage, CHGraph g, Weighting w ) - { + void initUnpackingGraph(GraphHopperStorage ghStorage, CHGraph g, Weighting w) { final long flags = carEncoder.setProperties(30, true, false); double dist = 1; g.edge(10, 0).setDistance(dist).setFlags(flags); @@ -429,8 +487,7 @@ void initUnpackingGraph( GraphHopperStorage ghStorage, CHGraph g, Weighting w ) } @Test - public void testUnpackingOrder() - { + public void testUnpackingOrder() { GraphHopperStorage ghStorage = createGHStorage(); CHGraph lg = ghStorage.getGraph(CHGraph.class); initUnpackingGraph(ghStorage, lg, weighting); @@ -442,8 +499,7 @@ public void testUnpackingOrder() } @Test - public void testUnpackingOrder_Fastest() - { + public void testUnpackingOrder_Fastest() { GraphHopperStorage ghStorage = createGHStorage(); CHGraph lg = ghStorage.getGraph(CHGraph.class); Weighting w = new FastestWeighting(carEncoder); @@ -457,8 +513,7 @@ public void testUnpackingOrder_Fastest() } @Test - public void testCircleBug() - { + public void testCircleBug() { GraphHopperStorage g = createGHStorage(); CHGraph lg = g.getGraph(CHGraph.class); // /--1 @@ -474,8 +529,7 @@ public void testCircleBug() } @Test - public void testBug178() - { + public void testBug178() { // 5--------6__ // | | \ // 0-1->-2--3--4 @@ -503,8 +557,7 @@ public void testBug178() // | 8 | // \ / / // 7-6-5-/ - void initBiGraph( Graph graph ) - { + void initBiGraph(Graph graph) { graph.edge(0, 1, 100, true); graph.edge(1, 2, 1, true); graph.edge(2, 3, 1, true); @@ -517,83 +570,6 @@ void initBiGraph( Graph graph ) graph.edge(8, 6, 20, true); } - // 0-1-.....-9-10 - // | ^ \ - // | | | - // 17-16-...-11<-/ - public static void initDirected2( Graph g ) - { - g.edge(0, 1, 1, true); - g.edge(1, 2, 1, true); - g.edge(2, 3, 1, true); - g.edge(3, 4, 1, true); - g.edge(4, 5, 1, true); - g.edge(5, 6, 1, true); - g.edge(6, 7, 1, true); - g.edge(7, 8, 1, true); - g.edge(8, 9, 1, true); - g.edge(9, 10, 1, true); - g.edge(10, 11, 1, false); - g.edge(11, 12, 1, true); - g.edge(11, 9, 3, false); - g.edge(12, 13, 1, true); - g.edge(13, 14, 1, true); - g.edge(14, 15, 1, true); - g.edge(15, 16, 1, true); - g.edge(16, 17, 1, true); - g.edge(17, 0, 1, true); - } - - // 8 - // | - // 6->0->1->3->7 - // | | - // | v - //10<-2---4<---5 - // 9 - public static void initDirected1( Graph g ) - { - g.edge(0, 8, 1, true); - g.edge(0, 1, 1, false); - g.edge(1, 3, 1, false); - g.edge(3, 7, 1, false); - g.edge(3, 5, 1, false); - g.edge(5, 4, 1, false); - g.edge(4, 2, 1, true); - g.edge(2, 9, 1, false); - g.edge(2, 10, 1, false); - g.edge(2, 6, 1, true); - g.edge(6, 0, 1, false); - } - - // prepare-routing.svg - public static Graph initShortcutsGraph( Graph g ) - { - g.edge(0, 1, 1, true); - g.edge(0, 2, 1, true); - g.edge(1, 2, 1, true); - g.edge(2, 3, 1.5, true); - g.edge(1, 4, 1, true); - g.edge(2, 9, 1, true); - g.edge(9, 3, 1, true); - g.edge(10, 3, 1, true); - g.edge(4, 5, 1, true); - g.edge(5, 6, 1, true); - g.edge(6, 7, 1, true); - g.edge(7, 8, 1, true); - g.edge(8, 9, 1, true); - g.edge(4, 11, 1, true); - g.edge(9, 14, 1, true); - g.edge(10, 14, 1, true); - g.edge(11, 12, 1, true); - g.edge(12, 15, 1, true); - g.edge(12, 13, 1, true); - g.edge(13, 16, 1, true); - g.edge(15, 16, 2, true); - g.edge(14, 16, 1, true); - return g; - } - // public static void printEdges(CHGraph g) { // RawEdgeIterator iter = g.getAllEdges(); // while (iter.next()) { @@ -607,8 +583,7 @@ public static Graph initShortcutsGraph( Graph g ) // System.out.println("---"); // } @Test - public void testBits() - { + public void testBits() { int fromNode = Integer.MAX_VALUE / 3 * 2; int endNode = Integer.MAX_VALUE / 37 * 17; @@ -618,8 +593,7 @@ public void testBits() } @Test - public void testMultiplePreparationsIdenticalView() - { + public void testMultiplePreparationsIdenticalView() { CarFlagEncoder tmpCarEncoder = new CarFlagEncoder(); BikeFlagEncoder tmpBikeEncoder = new BikeFlagEncoder(); EncodingManager tmpEncodingManager = new EncodingManager(tmpCarEncoder, tmpBikeEncoder); @@ -634,15 +608,13 @@ public void testMultiplePreparationsIdenticalView() ghStorage.freeze(); - for (Weighting w : chWeightings) - { + for (Weighting w : chWeightings) { checkPath(ghStorage, w, 7, 5, Helper.createTList(3, 9, 14, 16, 13, 12)); } } @Test - public void testMultiplePreparationsDifferentView() - { + public void testMultiplePreparationsDifferentView() { CarFlagEncoder tmpCarEncoder = new CarFlagEncoder(); BikeFlagEncoder tmpBikeEncoder = new BikeFlagEncoder(); EncodingManager tmpEncodingManager = new EncodingManager(tmpCarEncoder, tmpBikeEncoder); @@ -663,8 +635,7 @@ public void testMultiplePreparationsDifferentView() checkPath(ghStorage, bikeWeighting, 9, 5, Helper.createTList(3, 10, 14, 16, 13, 12)); } - void checkPath( GraphHopperStorage ghStorage, Weighting w, int expShortcuts, double expDistance, TIntList expNodes ) - { + void checkPath(GraphHopperStorage ghStorage, Weighting w, int expShortcuts, double expDistance, TIntList expNodes) { CHGraph lg = ghStorage.getGraph(CHGraph.class, w); PrepareContractionHierarchies prepare = new PrepareContractionHierarchies(dir, ghStorage, lg, w.getFlagEncoder(), w, tMode); prepare.doWork(); diff --git a/core/src/test/java/com/graphhopper/routing/ch/PrepareEncoderTest.java b/core/src/test/java/com/graphhopper/routing/ch/PrepareEncoderTest.java index 181f4c5804e..d0caee0f510 100644 --- a/core/src/test/java/com/graphhopper/routing/ch/PrepareEncoderTest.java +++ b/core/src/test/java/com/graphhopper/routing/ch/PrepareEncoderTest.java @@ -19,17 +19,16 @@ import org.junit.Test; -import static org.junit.Assert.*; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; /** * @author Peter Karich */ -public class PrepareEncoderTest -{ +public class PrepareEncoderTest { @Test - public void testOverwrite() - { + public void testOverwrite() { long forward = PrepareEncoder.getScFwdDir(); long backward = PrepareEncoder.getScFwdDir() ^ PrepareEncoder.getScDirMask(); long both = PrepareEncoder.getScDirMask(); diff --git a/core/src/test/java/com/graphhopper/routing/subnetwork/PrepareRoutingSubnetworksTest.java b/core/src/test/java/com/graphhopper/routing/subnetwork/PrepareRoutingSubnetworksTest.java index a7efb1d6c00..e4cf85c8bba 100644 --- a/core/src/test/java/com/graphhopper/routing/subnetwork/PrepareRoutingSubnetworksTest.java +++ b/core/src/test/java/com/graphhopper/routing/subnetwork/PrepareRoutingSubnetworksTest.java @@ -17,48 +17,36 @@ */ package com.graphhopper.routing.subnetwork; -import com.graphhopper.routing.subnetwork.TarjansSCCAlgorithm; -import com.graphhopper.routing.subnetwork.PrepareRoutingSubnetworks; import com.graphhopper.coll.GHBitSetImpl; import com.graphhopper.routing.subnetwork.PrepareRoutingSubnetworks.PrepEdgeFilter; -import com.graphhopper.routing.util.BikeFlagEncoder; -import com.graphhopper.routing.util.CarFlagEncoder; -import com.graphhopper.routing.util.DefaultEdgeFilter; -import com.graphhopper.routing.util.EdgeFilter; -import com.graphhopper.routing.util.EncodingManager; -import com.graphhopper.routing.util.FlagEncoder; +import com.graphhopper.routing.util.*; import com.graphhopper.storage.GraphBuilder; import com.graphhopper.storage.GraphHopperStorage; import com.graphhopper.util.EdgeExplorer; import com.graphhopper.util.EdgeIteratorState; import com.graphhopper.util.GHUtility; import com.graphhopper.util.Helper; - import gnu.trove.list.array.TIntArrayList; +import org.junit.Test; import java.util.Arrays; import java.util.Collections; import java.util.List; -import org.junit.*; - import static org.junit.Assert.*; /** * @author Peter Karich */ -public class PrepareRoutingSubnetworksTest -{ +public class PrepareRoutingSubnetworksTest { private final FlagEncoder carFlagEncoder = new CarFlagEncoder(); private final EncodingManager em = new EncodingManager(carFlagEncoder); - GraphHopperStorage createStorage( EncodingManager eman ) - { + GraphHopperStorage createStorage(EncodingManager eman) { return new GraphBuilder(eman).create(); } - GraphHopperStorage createSubnetworkTestStorage() - { + GraphHopperStorage createSubnetworkTestStorage() { GraphHopperStorage g = createStorage(em); // big network g.edge(1, 2, 1, true); @@ -85,8 +73,7 @@ GraphHopperStorage createSubnetworkTestStorage() return g; } - GraphHopperStorage createSubnetworkTestStorage2( EncodingManager em ) - { + GraphHopperStorage createSubnetworkTestStorage2(EncodingManager em) { GraphHopperStorage g = createStorage(em); // large network g.edge(0, 1, 1, true); @@ -107,8 +94,7 @@ GraphHopperStorage createSubnetworkTestStorage2( EncodingManager em ) } @Test - public void testFindSubnetworks() - { + public void testFindSubnetworks() { GraphHopperStorage g = createSubnetworkTestStorage(); PrepEdgeFilter filter = new PrepEdgeFilter(carFlagEncoder); PrepareRoutingSubnetworks instance = new PrepareRoutingSubnetworks(g, Collections.singletonList(carFlagEncoder)); @@ -124,8 +110,7 @@ public void testFindSubnetworks() } @Test - public void testKeepLargestNetworks() - { + public void testKeepLargestNetworks() { GraphHopperStorage g = createSubnetworkTestStorage(); PrepEdgeFilter filter = new PrepEdgeFilter(carFlagEncoder); PrepareRoutingSubnetworks instance = new PrepareRoutingSubnetworks(g, Collections.singletonList(carFlagEncoder)); @@ -144,8 +129,7 @@ public void testKeepLargestNetworks() } @Test - public void testRemoveSubnetworkIfOnlyOneVehicle() - { + public void testRemoveSubnetworkIfOnlyOneVehicle() { GraphHopperStorage g = createSubnetworkTestStorage2(em); PrepareRoutingSubnetworks instance = new PrepareRoutingSubnetworks(g, em.fetchEdgeEncoders()); instance.setMinNetworkSize(4); @@ -166,8 +150,7 @@ public void testRemoveSubnetworkIfOnlyOneVehicle() } @Test - public void testRemoveNode() - { + public void testRemoveNode() { FlagEncoder carEncoder = new CarFlagEncoder(); BikeFlagEncoder bikeEncoder = new BikeFlagEncoder(); EncodingManager em2 = new EncodingManager(carEncoder, bikeEncoder); @@ -180,10 +163,8 @@ public void testRemoveNode() assertFalse(instance.detectNodeRemovedForAllEncoders(edgeExplorer, 6)); // mark certain edges inaccessible for all encoders - for (EdgeIteratorState edge : Arrays.asList(GHUtility.getEdge(g, 5, 6), GHUtility.getEdge(g, 4, 5), GHUtility.getEdge(g, 4, 6))) - { - for (FlagEncoder encoders : em2.fetchEdgeEncoders()) - { + for (EdgeIteratorState edge : Arrays.asList(GHUtility.getEdge(g, 5, 6), GHUtility.getEdge(g, 4, 5), GHUtility.getEdge(g, 4, 6))) { + for (FlagEncoder encoders : em2.fetchEdgeEncoders()) { edge.setFlags(encoders.setAccess(0, false, false)); } } @@ -194,8 +175,7 @@ public void testRemoveNode() } @Test - public void testRemoveSubnetworkWhenMultipleVehicles() - { + public void testRemoveSubnetworkWhenMultipleVehicles() { FlagEncoder carEncoder = new CarFlagEncoder(); BikeFlagEncoder bikeEncoder = new BikeFlagEncoder(); EncodingManager em2 = new EncodingManager(carEncoder, bikeEncoder); @@ -221,8 +201,7 @@ public void testRemoveSubnetworkWhenMultipleVehicles() assertEquals(6, g.getNodes()); } - GraphHopperStorage createDeadEndUnvisitedNetworkStorage( EncodingManager em ) - { + GraphHopperStorage createDeadEndUnvisitedNetworkStorage(EncodingManager em) { GraphHopperStorage g = createStorage(em); // 0 <-> 1 <-> 2 <-> 3 <-> 4 <- 5 <-> 6 g.edge(0, 1, 1, true); @@ -240,8 +219,7 @@ GraphHopperStorage createDeadEndUnvisitedNetworkStorage( EncodingManager em ) return g; } - GraphHopperStorage createTarjanTestStorage() - { + GraphHopperStorage createTarjanTestStorage() { GraphHopperStorage g = createStorage(em); g.edge(1, 2, 1, false); @@ -264,8 +242,7 @@ GraphHopperStorage createTarjanTestStorage() } @Test - public void testRemoveDeadEndUnvisitedNetworks() - { + public void testRemoveDeadEndUnvisitedNetworks() { GraphHopperStorage g = createDeadEndUnvisitedNetworkStorage(em); assertEquals(11, g.getNodes()); @@ -281,8 +258,7 @@ public void testRemoveDeadEndUnvisitedNetworks() } @Test - public void testTarjan() - { + public void testTarjan() { GraphHopperStorage g = createSubnetworkTestStorage(); // Requires a single vehicle type, otherwise we throw. @@ -294,28 +270,15 @@ public void testTarjan() List components = tarjan.findComponents(); assertEquals(4, components.size()); - assertEquals(new TIntArrayList(new int[] - { - 13, 5, 3, 7, 0 - }), components.get(0)); - assertEquals(new TIntArrayList(new int[] - { - 2, 4, 12, 11, 8, 1 - }), components.get(1)); - assertEquals(new TIntArrayList(new int[] - { - 10, 14, 6 - }), components.get(2)); - assertEquals(new TIntArrayList(new int[] - { - 15, 9 - }), components.get(3)); + assertEquals(new TIntArrayList(new int[]{13, 5, 3, 7, 0}), components.get(0)); + assertEquals(new TIntArrayList(new int[]{2, 4, 12, 11, 8, 1}), components.get(1)); + assertEquals(new TIntArrayList(new int[]{10, 14, 6}), components.get(2)); + assertEquals(new TIntArrayList(new int[]{15, 9}), components.get(3)); } // Previous two-pass implementation failed on 1 -> 2 -> 0 @Test - public void testNodeOrderingRegression() - { + public void testNodeOrderingRegression() { // 1 -> 2 -> 0 GraphHopperStorage g = createStorage(em); g.edge(1, 2, 1, false); @@ -328,8 +291,7 @@ public void testNodeOrderingRegression() } @Test - public void test481() - { + public void test481() { // 0->1->3->4->5->6 // 2 7<--/ GraphHopperStorage g = createStorage(em); diff --git a/core/src/test/java/com/graphhopper/routing/template/RoundTripRoutingTemplateTest.java b/core/src/test/java/com/graphhopper/routing/template/RoundTripRoutingTemplateTest.java index 9990d865887..e4d6f9271b9 100644 --- a/core/src/test/java/com/graphhopper/routing/template/RoundTripRoutingTemplateTest.java +++ b/core/src/test/java/com/graphhopper/routing/template/RoundTripRoutingTemplateTest.java @@ -17,38 +17,37 @@ */ package com.graphhopper.routing.template; -import com.graphhopper.routing.weighting.Weighting; -import com.graphhopper.routing.weighting.FastestWeighting; import com.graphhopper.GHRequest; import com.graphhopper.GHResponse; import com.graphhopper.routing.*; import com.graphhopper.routing.util.*; +import com.graphhopper.routing.weighting.FastestWeighting; +import com.graphhopper.routing.weighting.Weighting; import com.graphhopper.storage.Graph; import com.graphhopper.storage.RAMDirectory; import com.graphhopper.storage.index.LocationIndex; import com.graphhopper.storage.index.LocationIndexTree; import com.graphhopper.storage.index.QueryResult; import com.graphhopper.util.Helper; -import static com.graphhopper.util.Parameters.Algorithms.DIJKSTRA_BI; +import org.junit.Test; + import java.util.Arrays; import java.util.List; -import org.junit.Test; -import static org.junit.Assert.*; + +import static com.graphhopper.util.Parameters.Algorithms.DIJKSTRA_BI; +import static org.junit.Assert.assertEquals; /** - * * @author Peter Karich */ -public class RoundTripRoutingTemplateTest -{ +public class RoundTripRoutingTemplateTest { private final FlagEncoder carFE = new CarFlagEncoder(); private final EncodingManager em = new EncodingManager(carFE); // TODO private final TraversalMode tMode = TraversalMode.EDGE_BASED_2DIR; private final TraversalMode tMode = TraversalMode.NODE_BASED; @Test - public void testCalcRoundTrip() throws Exception - { + public void testCalcRoundTrip() throws Exception { Weighting weighting = new FastestWeighting(carFE); Graph g = createTestGraph(true); @@ -79,8 +78,7 @@ public void testCalcRoundTrip() throws Exception assertEquals(Helper.createTList(4, 8, 7, 6), paths.get(1).calcNodes()); } - private Graph createTestGraph( boolean fullGraph ) - { + private Graph createTestGraph(boolean fullGraph) { return new AlternativeRouteTest(tMode).createTestGraph(fullGraph, em); } } diff --git a/core/src/test/java/com/graphhopper/routing/util/AbstractBikeFlagEncoderTester.java b/core/src/test/java/com/graphhopper/routing/util/AbstractBikeFlagEncoderTester.java index 8a0d710e4e0..b1bc39cd019 100644 --- a/core/src/test/java/com/graphhopper/routing/util/AbstractBikeFlagEncoderTester.java +++ b/core/src/test/java/com/graphhopper/routing/util/AbstractBikeFlagEncoderTester.java @@ -17,62 +17,55 @@ */ package com.graphhopper.routing.util; -import com.graphhopper.routing.weighting.PriorityWeighting; import com.graphhopper.reader.ReaderNode; import com.graphhopper.reader.ReaderWay; +import com.graphhopper.routing.weighting.PriorityWeighting; +import com.graphhopper.util.Helper; import com.graphhopper.util.Translation; import org.junit.Before; import org.junit.Test; +import java.text.DateFormat; +import java.util.Date; import java.util.Locale; import static com.graphhopper.routing.util.PriorityCode.*; -import com.graphhopper.util.Helper; import static com.graphhopper.util.TranslationMapTest.SINGLETON; -import java.text.DateFormat; -import java.util.Date; import static org.junit.Assert.*; /** * @author Peter Karich * @author ratrun */ -public abstract class AbstractBikeFlagEncoderTester -{ +public abstract class AbstractBikeFlagEncoderTester { protected BikeCommonFlagEncoder encoder; @Before - public void setUp() - { + public void setUp() { encoder = createBikeEncoder(); } protected abstract BikeCommonFlagEncoder createBikeEncoder(); - protected void assertPriority( int expectedPrio, ReaderWay way ) - { + protected void assertPriority(int expectedPrio, ReaderWay way) { assertPriority(expectedPrio, way, 0); } - protected void assertPriority( int expectedPrio, ReaderWay way, long relationFlags ) - { + protected void assertPriority(int expectedPrio, ReaderWay way, long relationFlags) { assertEquals(expectedPrio, encoder.handlePriority(way, 18, (int) encoder.relationCodeEncoder.getValue(relationFlags))); } - protected double getSpeedFromFlags( ReaderWay way ) - { + protected double getSpeedFromFlags(ReaderWay way) { long allowed = encoder.acceptBit; long flags = encoder.handleWayTags(way, allowed, 0); return encoder.getSpeed(flags); } - protected String getWayTypeFromFlags( ReaderWay way ) - { + protected String getWayTypeFromFlags(ReaderWay way) { return getWayTypeFromFlags(way, 0); } - protected String getWayTypeFromFlags( ReaderWay way, long relationFlags ) - { + protected String getWayTypeFromFlags(ReaderWay way, long relationFlags) { long allowed = encoder.acceptBit; long flags = encoder.handleWayTags(way, allowed, relationFlags); Translation enMap = SINGLETON.getWithFallBack(Locale.UK); @@ -80,8 +73,7 @@ protected String getWayTypeFromFlags( ReaderWay way, long relationFlags ) } @Test - public void testAccess() - { + public void testAccess() { ReaderWay way = new ReaderWay(1); way.setTag("highway", "motorway"); @@ -204,11 +196,10 @@ public void testAccess() } @Test - public void testTramStations() - { + public void testTramStations() { ReaderWay way = new ReaderWay(1); way.setTag("highway", "secondary"); - way.setTag("railway", "rail"); + way.setTag("railway", "rail"); assertTrue(encoder.acceptWay(way) > 0); way = new ReaderWay(1); @@ -219,10 +210,10 @@ public void testTramStations() way = new ReaderWay(1); way.setTag("highway", "secondary"); way.setTag("railway", "station"); - way.setTag("bicycle", "yes"); + way.setTag("bicycle", "yes"); assertTrue(encoder.acceptWay(way) > 0); - - way.setTag("bicycle", "no"); + + way.setTag("bicycle", "no"); assertTrue(encoder.acceptWay(way) == 0); way = new ReaderWay(1); @@ -245,8 +236,7 @@ public void testTramStations() } @Test - public void testAvoidTunnel() - { + public void testAvoidTunnel() { ReaderWay osmWay = new ReaderWay(1); osmWay.setTag("highway", "residential"); assertPriority(PREFER.getValue(), osmWay); @@ -263,8 +253,7 @@ public void testAvoidTunnel() } @Test - public void testTram() - { + public void testTram() { ReaderWay way = new ReaderWay(1); // very dangerous way.setTag("highway", "secondary"); @@ -277,8 +266,7 @@ public void testTram() } @Test - public void testHandleCommonWayTags() - { + public void testHandleCommonWayTags() { ReaderWay way = new ReaderWay(1); String wayType; @@ -370,8 +358,7 @@ public void testHandleCommonWayTags() } @Test - public void testService() - { + public void testService() { ReaderWay way = new ReaderWay(1); way.setTag("highway", "service"); assertEquals(14, encoder.getSpeed(way)); @@ -383,8 +370,7 @@ public void testService() } @Test - public void testSacScale() - { + public void testSacScale() { ReaderWay way = new ReaderWay(1); way.setTag("highway", "service"); way.setTag("sac_scale", "hiking"); @@ -396,16 +382,14 @@ public void testSacScale() } @Test - public void testReduceToMaxSpeed() - { + public void testReduceToMaxSpeed() { ReaderWay way = new ReaderWay(12); way.setTag("maxspeed", "90"); assertEquals(12, encoder.applyMaxSpeed(way, 12), 1e-2); } @Test - public void testPreferenceForSlowSpeed() - { + public void testPreferenceForSlowSpeed() { ReaderWay osmWay = new ReaderWay(1); osmWay.setTag("highway", "tertiary"); assertEquals(30, encoder.getSpeed(encoder.setSpeed(0, encoder.applyMaxSpeed(osmWay, 49))), 1e-1); @@ -413,8 +397,7 @@ public void testPreferenceForSlowSpeed() } @Test - public void testHandleWayTagsCallsHandlePriority() - { + public void testHandleWayTagsCallsHandlePriority() { ReaderWay osmWay = new ReaderWay(1); osmWay.setTag("highway", "cycleway"); long encoded = encoder.handleWayTags(osmWay, encoder.acceptBit, 0); @@ -422,8 +405,7 @@ public void testHandleWayTagsCallsHandlePriority() } @Test - public void testAvoidMotorway() - { + public void testAvoidMotorway() { ReaderWay osmWay = new ReaderWay(1); osmWay.setTag("highway", "motorway"); osmWay.setTag("bicycle", "yes"); @@ -431,8 +413,7 @@ public void testAvoidMotorway() } @Test - public void testPriority() - { + public void testPriority() { long flags = encoder.priorityWayEncoder.setValue(0, PriorityCode.BEST.getValue()); assertEquals(1, encoder.getDouble(flags, PriorityWeighting.KEY), 1e-3); @@ -441,8 +422,7 @@ public void testPriority() } @Test - public void testBarrierAccess() - { + public void testBarrierAccess() { // by default allow access through the gate for bike & foot! ReaderNode node = new ReaderNode(1, -1, -1); node.setTag("barrier", "gate"); @@ -482,8 +462,7 @@ public void testBarrierAccess() } @Test - public void testBarrierAccessFord() - { + public void testBarrierAccessFord() { ReaderNode node = new ReaderNode(1, -1, -1); node.setTag("ford", "yes"); // barrier! diff --git a/core/src/test/java/com/graphhopper/routing/util/AbstractFlagEncoderTest.java b/core/src/test/java/com/graphhopper/routing/util/AbstractFlagEncoderTest.java index e96e9e8d25c..6754e17dab3 100644 --- a/core/src/test/java/com/graphhopper/routing/util/AbstractFlagEncoderTest.java +++ b/core/src/test/java/com/graphhopper/routing/util/AbstractFlagEncoderTest.java @@ -19,16 +19,14 @@ import org.junit.Test; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; /** * @author Peter Karich */ -public class AbstractFlagEncoderTest -{ +public class AbstractFlagEncoderTest { @Test - public void testAcceptsCar() - { + public void testAcceptsCar() { CarFlagEncoder encoder = new CarFlagEncoder(5, 5, 0); assertEquals(40, encoder.parseSpeed("40 km/h"), 1e-3); assertEquals(40, encoder.parseSpeed("40km/h"), 1e-3); diff --git a/core/src/test/java/com/graphhopper/routing/util/Bike2WeightFlagEncoderTest.java b/core/src/test/java/com/graphhopper/routing/util/Bike2WeightFlagEncoderTest.java index c3027ea9466..819080dde1e 100644 --- a/core/src/test/java/com/graphhopper/routing/util/Bike2WeightFlagEncoderTest.java +++ b/core/src/test/java/com/graphhopper/routing/util/Bike2WeightFlagEncoderTest.java @@ -27,18 +27,15 @@ /** * @author Peter Karich */ -public class Bike2WeightFlagEncoderTest extends BikeFlagEncoderTest -{ +public class Bike2WeightFlagEncoderTest extends BikeFlagEncoderTest { private final EncodingManager em = new EncodingManager("bike,bike2"); @Override - protected BikeCommonFlagEncoder createBikeEncoder() - { + protected BikeCommonFlagEncoder createBikeEncoder() { return (BikeCommonFlagEncoder) em.getEncoder("bike2"); } - private Graph initExampleGraph() - { + private Graph initExampleGraph() { GraphHopperStorage gs = new GraphHopperStorage(new RAMDirectory(), em, true, new GraphExtension.NoOpExtension()).create(1000); NodeAccess na = gs.getNodeAccess(); // 50--(0.0001)-->49--(0.0004)-->55--(0.0005)-->60 @@ -53,8 +50,7 @@ private Graph initExampleGraph() } @Test - public void testApplyWayTags() - { + public void testApplyWayTags() { Graph graph = initExampleGraph(); EdgeIteratorState edge = GHUtility.getEdge(graph, 0, 1); ReaderWay way = new ReaderWay(1); @@ -68,8 +64,7 @@ public void testApplyWayTags() } @Test - public void testUnchangedForStepsBridgeAndTunnel() - { + public void testUnchangedForStepsBridgeAndTunnel() { Graph graph = initExampleGraph(); EdgeIteratorState edge = GHUtility.getEdge(graph, 0, 1); long oldFlags = edge.getFlags(); @@ -81,8 +76,7 @@ public void testUnchangedForStepsBridgeAndTunnel() } @Test - public void testSetSpeed0_issue367() - { + public void testSetSpeed0_issue367() { long flags = encoder.setProperties(10, true, true); flags = encoder.setSpeed(flags, 0); @@ -93,8 +87,7 @@ public void testSetSpeed0_issue367() } @Test - public void testRoutingFailsWithInvalidGraph_issue665() - { + public void testRoutingFailsWithInvalidGraph_issue665() { GraphHopperStorage graph = new GraphHopperStorage( new RAMDirectory(), em, true, new GraphExtension.NoOpExtension()); graph.create(100); @@ -110,8 +103,7 @@ public void testRoutingFailsWithInvalidGraph_issue665() assertTrue(isGraphValid(graph, encoder)); } - private boolean isGraphValid( Graph graph, FlagEncoder encoder ) - { + private boolean isGraphValid(Graph graph, FlagEncoder encoder) { EdgeExplorer explorer = graph.createEdgeExplorer(); // iterator at node 0 considers the edge 0-1 to be undirected diff --git a/core/src/test/java/com/graphhopper/routing/util/BikeFlagEncoderTest.java b/core/src/test/java/com/graphhopper/routing/util/BikeFlagEncoderTest.java index 4efa8b4cc8a..09a644128de 100644 --- a/core/src/test/java/com/graphhopper/routing/util/BikeFlagEncoderTest.java +++ b/core/src/test/java/com/graphhopper/routing/util/BikeFlagEncoderTest.java @@ -17,33 +17,28 @@ */ package com.graphhopper.routing.util; -import com.graphhopper.routing.weighting.PriorityWeighting; import com.graphhopper.reader.ReaderNode; import com.graphhopper.reader.ReaderRelation; import com.graphhopper.reader.ReaderWay; +import com.graphhopper.routing.weighting.PriorityWeighting; +import org.junit.Test; import static com.graphhopper.routing.util.BikeCommonFlagEncoder.PUSHING_SECTION_SPEED; import static com.graphhopper.routing.util.PriorityCode.*; - -import org.junit.Test; - import static org.junit.Assert.*; /** * @author Peter Karich * @author ratrun */ -public class BikeFlagEncoderTest extends AbstractBikeFlagEncoderTester -{ +public class BikeFlagEncoderTest extends AbstractBikeFlagEncoderTester { @Override - protected BikeCommonFlagEncoder createBikeEncoder() - { + protected BikeCommonFlagEncoder createBikeEncoder() { return (BikeCommonFlagEncoder) new EncodingManager("bike,mtb").getEncoder("bike"); } @Test - public void testGetSpeed() - { + public void testGetSpeed() { long result = encoder.setProperties(10, true, true); assertEquals(10, encoder.getSpeed(result), 1e-1); ReaderWay way = new ReaderWay(1); @@ -207,8 +202,7 @@ public void testGetSpeed() } @Test - public void testHandleWayTags() - { + public void testHandleWayTags() { ReaderWay way = new ReaderWay(1); String wayType; way.setTag("highway", "track"); @@ -252,8 +246,7 @@ public void testHandleWayTags() } @Test - public void testOneway() - { + public void testOneway() { ReaderWay way = new ReaderWay(1); way.setTag("highway", "tertiary"); long flags = encoder.handleWayTags(way, encoder.acceptWay(way), 0); @@ -329,8 +322,7 @@ public void testOneway() } @Test - public void testHandleWayTagsInfluencedByRelation() - { + public void testHandleWayTagsInfluencedByRelation() { ReaderWay osmWay = new ReaderWay(1); osmWay.setTag("highway", "road"); long allowed = encoder.acceptBit; @@ -396,8 +388,7 @@ public void testHandleWayTagsInfluencedByRelation() } @Test - public void testUnchangedRelationShouldNotInfluencePriority() - { + public void testUnchangedRelationShouldNotInfluencePriority() { ReaderWay osmWay = new ReaderWay(1); osmWay.setTag("highway", "secondary"); @@ -409,8 +400,7 @@ public void testUnchangedRelationShouldNotInfluencePriority() @Test @Override - public void testSacScale() - { + public void testSacScale() { ReaderWay way = new ReaderWay(1); way.setTag("highway", "path"); way.setTag("sac_scale", "hiking"); @@ -434,8 +424,7 @@ public void testSacScale() } @Test - public void testCalcPriority() - { + public void testCalcPriority() { long allowed = encoder.acceptBit; ReaderWay osmWay = new ReaderWay(1); ReaderRelation osmRel = new ReaderRelation(1); @@ -453,8 +442,7 @@ public void testCalcPriority() } @Test - public void testMaxSpeed() - { + public void testMaxSpeed() { ReaderWay way = new ReaderWay(1); way.setTag("highway", "secondary"); way.setTag("maxspeed", "10"); @@ -487,8 +475,7 @@ public void testMaxSpeed() } @Test - public void testTurnFlagEncoding_DefaultNoRestrictionsAndNoCosts() - { + public void testTurnFlagEncoding_DefaultNoRestrictionsAndNoCosts() { // default is disabled turn costs and no restrictions long flags_r0 = encoder.getTurnFlags(true, 0); long flags_0 = encoder.getTurnFlags(false, 0); @@ -510,8 +497,7 @@ public void testTurnFlagEncoding_DefaultNoRestrictionsAndNoCosts() } @Test - public void testTurnFlagEncoding_withCosts() - { + public void testTurnFlagEncoding_withCosts() { encoder = new BikeFlagEncoder(4, 2, 127); new EncodingManager(encoder); @@ -530,12 +516,10 @@ public void testTurnFlagEncoding_withCosts() assertFalse(encoder.isTurnRestricted(flags_20)); long flags_r220 = encoder.getTurnFlags(true, 0); - try - { + try { encoder.getTurnFlags(false, 220); assertTrue(false); - } catch (Exception ex) - { + } catch (Exception ex) { } long flags_126 = encoder.getTurnFlags(false, 126); assertTrue(Double.isInfinite(encoder.getTurnCost(flags_r220))); @@ -548,8 +532,7 @@ public void testTurnFlagEncoding_withCosts() // Issue 407 : Always block kissing_gate execpt for mountainbikes @Test @Override - public void testBarrierAccess() - { + public void testBarrierAccess() { // kissing_gate without bicycle tag ReaderNode node = new ReaderNode(1, -1, -1); node.setTag("barrier", "kissing_gate"); @@ -565,8 +548,7 @@ public void testBarrierAccess() } @Test - public void testClassBicycle() - { + public void testClassBicycle() { ReaderWay way = new ReaderWay(1); way.setTag("highway", "tertiary"); way.setTag("class:bicycle", "3"); diff --git a/core/src/test/java/com/graphhopper/routing/util/CarFlagEncoderTest.java b/core/src/test/java/com/graphhopper/routing/util/CarFlagEncoderTest.java index 222edcf2dde..d3c7f817119 100644 --- a/core/src/test/java/com/graphhopper/routing/util/CarFlagEncoderTest.java +++ b/core/src/test/java/com/graphhopper/routing/util/CarFlagEncoderTest.java @@ -20,9 +20,9 @@ import com.graphhopper.reader.ReaderNode; import com.graphhopper.reader.ReaderWay; import com.graphhopper.util.Helper; -import java.text.DateFormat; import org.junit.Test; +import java.text.DateFormat; import java.util.Date; import static org.junit.Assert.*; @@ -30,14 +30,12 @@ /** * @author Peter Karich */ -public class CarFlagEncoderTest -{ +public class CarFlagEncoderTest { private final EncodingManager em = new EncodingManager("car,bike,foot"); private final CarFlagEncoder encoder = (CarFlagEncoder) em.getEncoder("car"); @Test - public void testAccess() - { + public void testAccess() { ReaderWay way = new ReaderWay(1); assertFalse(encoder.acceptWay(way) > 0); way.setTag("highway", "service"); @@ -127,8 +125,7 @@ public void testAccess() } @Test - public void testMilitaryAccess() - { + public void testMilitaryAccess() { ReaderWay way = new ReaderWay(1); way.setTag("highway", "track"); way.setTag("access", "military"); @@ -136,8 +133,7 @@ public void testMilitaryAccess() } @Test - public void testFordAccess() - { + public void testFordAccess() { ReaderNode node = new ReaderNode(0, 0.0, 0.0); node.setTag("ford", "yes"); @@ -150,21 +146,18 @@ public void testFordAccess() assertFalse(encoder.acceptWay(way) > 0); assertTrue(encoder.handleNodeTags(node) > 0); - try - { + try { // Now they are passable encoder.setBlockFords(false); assertTrue(encoder.acceptWay(way) > 0); assertFalse(encoder.handleNodeTags(node) > 0); - } finally - { + } finally { encoder.setBlockFords(true); } } @Test - public void testOneway() - { + public void testOneway() { ReaderWay way = new ReaderWay(1); way.setTag("highway", "primary"); long flags = encoder.handleWayTags(way, encoder.acceptWay(way), 0); @@ -198,8 +191,7 @@ public void testOneway() } @Test - public void testSetAccess() - { + public void testSetAccess() { assertTrue(encoder.isForward(encoder.setProperties(0, true, true))); assertTrue(encoder.isBackward(encoder.setProperties(0, true, true))); @@ -222,8 +214,7 @@ public void testSetAccess() } @Test - public void testMaxSpeed() - { + public void testMaxSpeed() { ReaderWay way = new ReaderWay(1); way.setTag("highway", "trunk"); way.setTag("maxspeed", "500"); @@ -258,8 +249,7 @@ public void testMaxSpeed() } @Test - public void testSpeed() - { + public void testSpeed() { // limit bigger than default road speed ReaderWay way = new ReaderWay(1); way.setTag("highway", "trunk"); @@ -288,24 +278,20 @@ public void testSpeed() encoded = encoder.handleWayTags(way, allowed, 0); assertEquals(20, encoder.getSpeed(encoded), 1e-1); - try - { + try { encoder.setSpeed(0, -1); assertTrue(false); - } catch (IllegalArgumentException ex) - { + } catch (IllegalArgumentException ex) { } } @Test - public void testSetSpeed() - { + public void testSetSpeed() { assertEquals(10, encoder.getSpeed(encoder.setSpeed(0, 10)), 1e-1); } @Test - public void testSetSpeed0_issue367() - { + public void testSetSpeed0_issue367() { long flags = encoder.setProperties(10, true, true); flags = encoder.setSpeed(flags, encoder.speedFactor * 0.49); @@ -316,8 +302,7 @@ public void testSetSpeed0_issue367() } @Test - public void testRoundabout() - { + public void testRoundabout() { long flags = encoder.setAccess(0, true, true); long resFlags = encoder.setBool(flags, FlagEncoder.K_ROUNDABOUT, true); assertTrue(encoder.isBool(resFlags, FlagEncoder.K_ROUNDABOUT)); @@ -344,11 +329,10 @@ public void testRoundabout() } @Test - public void testRailway() - { + public void testRailway() { ReaderWay way = new ReaderWay(1); way.setTag("highway", "secondary"); - way.setTag("railway", "rail"); + way.setTag("railway", "rail"); assertTrue(encoder.acceptWay(way) > 0); way.clearTags(); @@ -397,7 +381,7 @@ public void testRailway() // We can't store 0.5km/h, but we expect the lowest possible speed (5km/h) assertEquals(2.5, encoder.getFerrySpeed(way, 20, 30, 40), 1e-1); assertEquals(5, encoder.getSpeed(encoder.setSpeed(0, 2.5)), 1e-1); - + //Test for an unrealisitic long duration way = new ReaderWay(1); way.setTag("route", "ferry"); @@ -412,8 +396,7 @@ public void testRailway() } @Test - public void testSwapDir() - { + public void testSwapDir() { long swappedFlags = encoder.reverseFlags(encoder.flagsDefault(true, true)); assertTrue(encoder.isForward(swappedFlags)); assertTrue(encoder.isBackward(swappedFlags)); @@ -427,8 +410,7 @@ public void testSwapDir() } @Test - public void testBarrierAccess() - { + public void testBarrierAccess() { ReaderNode node = new ReaderNode(1, -1, -1); node.setTag("barrier", "lift_gate"); node.setTag("access", "yes"); @@ -467,8 +449,7 @@ public void testBarrierAccess() } @Test - public void testTurnFlagEncoding_noCosts() - { + public void testTurnFlagEncoding_noCosts() { FlagEncoder tmpEnc = new CarFlagEncoder(8, 5, 0); EncodingManager em = new EncodingManager(tmpEnc); @@ -492,8 +473,7 @@ public void testTurnFlagEncoding_noCosts() } @Test - public void testTurnFlagEncoding_withCosts() - { + public void testTurnFlagEncoding_withCosts() { FlagEncoder tmpEncoder = new CarFlagEncoder(8, 5, 127); EncodingManager em = new EncodingManager(tmpEncoder); @@ -512,20 +492,17 @@ public void testTurnFlagEncoding_withCosts() assertFalse(tmpEncoder.isTurnRestricted(flags_20)); long flags_r220 = tmpEncoder.getTurnFlags(true, 0); - try - { + try { tmpEncoder.getTurnFlags(false, 220); assertTrue(false); - } catch (Exception ex) - { + } catch (Exception ex) { } assertTrue(Double.isInfinite(tmpEncoder.getTurnCost(flags_r220))); assertTrue(tmpEncoder.isTurnRestricted(flags_r220)); } @Test - public void testMaxValue() - { + public void testMaxValue() { CarFlagEncoder instance = new CarFlagEncoder(10, 0.5, 0); EncodingManager em = new EncodingManager(instance); ReaderWay way = new ReaderWay(1); @@ -548,30 +525,25 @@ public void testMaxValue() } @Test - public void testRegisterOnlyOnceAllowed() - { + public void testRegisterOnlyOnceAllowed() { CarFlagEncoder instance = new CarFlagEncoder(10, 0.5, 0); EncodingManager tmpEM = new EncodingManager(instance); - try - { + try { tmpEM = new EncodingManager(instance); assertTrue(false); - } catch (IllegalStateException ex) - { + } catch (IllegalStateException ex) { } } @Test - public void testSetToMaxSpeed() - { + public void testSetToMaxSpeed() { ReaderWay way = new ReaderWay(12); way.setTag("maxspeed", "90"); assertEquals(90, encoder.getMaxSpeed(way), 1e-2); } @Test - public void testCombination() - { + public void testCombination() { ReaderWay way = new ReaderWay(123); way.setTag("highway", "cycleway"); way.setTag("sac_scale", "hiking"); diff --git a/core/src/test/java/com/graphhopper/routing/util/DataFlagEncoderTest.java b/core/src/test/java/com/graphhopper/routing/util/DataFlagEncoderTest.java index 8ebb37ed502..2a8945b9826 100644 --- a/core/src/test/java/com/graphhopper/routing/util/DataFlagEncoderTest.java +++ b/core/src/test/java/com/graphhopper/routing/util/DataFlagEncoderTest.java @@ -6,22 +6,22 @@ import com.graphhopper.util.EdgeIteratorState; import com.graphhopper.util.GHUtility; import com.graphhopper.util.Helper; -import java.util.*; -import static org.junit.Assert.*; import org.junit.Test; +import java.util.HashMap; +import java.util.Map; + +import static org.junit.Assert.*; + /** - * * @author Peter Karich */ -public class DataFlagEncoderTest -{ +public class DataFlagEncoderTest { private final DataFlagEncoder encoder; private final EncodingManager encodingManager; private final int motorVehicleInt; - public DataFlagEncoderTest() - { + public DataFlagEncoderTest() { encoder = new DataFlagEncoder(); encodingManager = new EncodingManager(encoder); @@ -29,8 +29,7 @@ public DataFlagEncoderTest() } @Test - public void testHighway() - { + public void testHighway() { ReaderWay osmWay = new ReaderWay(0); osmWay.setTag("highway", "primary"); osmWay.setTag("surface", "sand"); @@ -59,8 +58,7 @@ public void testHighway() } @Test - public void testHighwaySpeed() - { + public void testHighwaySpeed() { Map map = new HashMap<>(); map.put("motorway", 100d); map.put("motorway_link", 100d); @@ -72,8 +70,7 @@ public void testHighwaySpeed() } @Test - public void testMaxspeed() - { + public void testMaxspeed() { ReaderWay osmWay = new ReaderWay(0); osmWay.setTag("highway", "primary"); osmWay.setTag("maxspeed", "10"); @@ -92,8 +89,7 @@ public void testMaxspeed() } @Test - public void reverseEdge() - { + public void reverseEdge() { Graph graph = new GraphBuilder(encodingManager).create(); EdgeIteratorState edge = graph.edge(0, 1); ReaderWay osmWay = new ReaderWay(0); diff --git a/core/src/test/java/com/graphhopper/routing/util/EncodedDoubleValueTest.java b/core/src/test/java/com/graphhopper/routing/util/EncodedDoubleValueTest.java index 44d34bd3efd..dc9b365962d 100644 --- a/core/src/test/java/com/graphhopper/routing/util/EncodedDoubleValueTest.java +++ b/core/src/test/java/com/graphhopper/routing/util/EncodedDoubleValueTest.java @@ -20,27 +20,24 @@ import com.graphhopper.reader.ReaderWay; import org.junit.Test; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; /** * @author Peter Karich */ -public class EncodedDoubleValueTest -{ +public class EncodedDoubleValueTest { @Test - public void testSetDoubleValue() - { + public void testSetDoubleValue() { EncodedDoubleValue instance = new EncodedDoubleValue("test", 6, 10, 0.01, 5, 10); assertEquals(10.12, instance.getDoubleValue(instance.setDoubleValue(0, 10.12)), 1e-4); } @Test - public void testMaxValue() - { + public void testMaxValue() { EncodedDoubleValue instance1 = new EncodedDoubleValue("test1", 0, 8, 0.5, 60, 100); long flags = instance1.setDoubleValue(0, instance1.getMaxValue()); assertEquals(100, instance1.getDoubleValue(flags), 1e-1); - + CarFlagEncoder carEncoder = new CarFlagEncoder(10, 0.5, 0); new EncodingManager(carEncoder); ReaderWay way = new ReaderWay(1); @@ -54,8 +51,7 @@ public void testMaxValue() } @Test - public void testUnsignedRightShift_issue417() - { + public void testUnsignedRightShift_issue417() { EncodedDoubleValue speedEncoder = new EncodedDoubleValue("Speed", 56, 8, 1, 30, 255); Long flags = -72057594037927936L; assertEquals(255, speedEncoder.getDoubleValue(flags), 0.01); diff --git a/core/src/test/java/com/graphhopper/routing/util/EncodedValueTest.java b/core/src/test/java/com/graphhopper/routing/util/EncodedValueTest.java index 66f15396f26..28c5bdb897e 100644 --- a/core/src/test/java/com/graphhopper/routing/util/EncodedValueTest.java +++ b/core/src/test/java/com/graphhopper/routing/util/EncodedValueTest.java @@ -19,16 +19,14 @@ import org.junit.Test; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; /** * @author Peter Karich */ -public class EncodedValueTest -{ +public class EncodedValueTest { @Test - public void testSetValue() - { + public void testSetValue() { EncodedValue instance = new EncodedValue("test", 6, 4, 1, 5, 10); assertEquals(10, instance.getValue(instance.setValue(0, 10))); @@ -40,8 +38,7 @@ public void testSetValue() } @Test - public void testSwap() - { + public void testSwap() { EncodedValue instance1 = new EncodedValue("test1", 0, 10, 1, 5, 1000); EncodedValue instance2 = new EncodedValue("test2", 10, 10, 1, 5, 1000); long flags = instance2.setValue(instance1.setValue(0, 13), 874); diff --git a/core/src/test/java/com/graphhopper/routing/util/EncodingManagerTest.java b/core/src/test/java/com/graphhopper/routing/util/EncodingManagerTest.java index 54b3df65c3c..69a5f691bad 100644 --- a/core/src/test/java/com/graphhopper/routing/util/EncodingManagerTest.java +++ b/core/src/test/java/com/graphhopper/routing/util/EncodingManagerTest.java @@ -17,31 +17,25 @@ */ package com.graphhopper.routing.util; -import com.graphhopper.routing.weighting.PriorityWeighting; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotEquals; -import static org.junit.Assert.assertTrue; - -import org.junit.Rule; -import org.junit.Test; - import com.graphhopper.reader.ReaderRelation; import com.graphhopper.reader.ReaderWay; +import com.graphhopper.routing.weighting.PriorityWeighting; import com.graphhopper.util.BitUtil; +import org.junit.Rule; +import org.junit.Test; import org.junit.rules.ExpectedException; +import static org.junit.Assert.*; + /** * @author Peter Karich */ -public class EncodingManagerTest -{ +public class EncodingManagerTest { @Rule public ExpectedException thrown = ExpectedException.none(); @Test - public void testCompatibility() - { + public void testCompatibility() { EncodingManager manager = new EncodingManager("car,bike,foot"); BikeFlagEncoder bike = (BikeFlagEncoder) manager.getEncoder("bike"); CarFlagEncoder car = (CarFlagEncoder) manager.getEncoder("car"); @@ -61,91 +55,74 @@ public void testCompatibility() assertEquals(foot3, foot2); assertEquals(foot3.hashCode(), foot2.hashCode()); - try - { + try { new EncodingManager("car,car"); assertTrue("do not allow duplicate flag encoders", false); - } catch (Exception ex) - { + } catch (Exception ex) { } } @Test - public void testEncoderAcceptNoException() - { + public void testEncoderAcceptNoException() { EncodingManager manager = new EncodingManager("car"); assertTrue(manager.supports("car")); assertFalse(manager.supports("foot")); } @Test - public void testEncoderWithWrongVersionIsRejected() - { + public void testEncoderWithWrongVersionIsRejected() { thrown.expect(IllegalArgumentException.class); EncodingManager manager = new EncodingManager("car|version=0"); } @Test - public void testWrongEncoders() - { - try - { + public void testWrongEncoders() { + try { FootFlagEncoder foot = new FootFlagEncoder(); new EncodingManager(foot, foot); assertTrue(false); - } catch (Exception ex) - { + } catch (Exception ex) { assertEquals("You must not register a FlagEncoder (foot) twice!", ex.getMessage()); } - try - { + try { new EncodingManager(new FootFlagEncoder(), new CarFlagEncoder(), new BikeFlagEncoder(), new MountainBikeFlagEncoder(), new RacingBikeFlagEncoder()); assertTrue(false); - } catch (Exception ex) - { + } catch (Exception ex) { assertTrue(ex.getMessage(), ex.getMessage().startsWith("Encoders are requesting more than 32 bits of way flags. Decrease the")); } } @Test - public void testToDetailsStringIncludesEncoderVersionNumber() - { - FlagEncoder encoder = new AbstractFlagEncoder(1, 2.0, 3) - { + public void testToDetailsStringIncludesEncoderVersionNumber() { + FlagEncoder encoder = new AbstractFlagEncoder(1, 2.0, 3) { @Override - public int getVersion() - { + public int getVersion() { return 10; } @Override - public String toString() - { + public String toString() { return "new_encoder"; } @Override - protected String getPropertiesString() - { + protected String getPropertiesString() { return "my_properties"; } @Override - public long handleRelationTags( ReaderRelation relation, long oldRelationFlags ) - { + public long handleRelationTags(ReaderRelation relation, long oldRelationFlags) { return 0; } @Override - public long acceptWay( ReaderWay way ) - { + public long acceptWay(ReaderWay way) { return 0; } @Override - public long handleWayTags( ReaderWay way, long allowed, long relationFlags ) - { + public long handleWayTags(ReaderWay way, long allowed, long relationFlags) { return 0; } }; @@ -156,39 +133,33 @@ public long handleWayTags( ReaderWay way, long allowed, long relationFlags ) } @Test - public void testCombineRelations() - { + public void testCombineRelations() { ReaderWay osmWay = new ReaderWay(1); osmWay.setTag("highway", "track"); ReaderRelation osmRel = new ReaderRelation(1); BikeFlagEncoder defaultBike = new BikeFlagEncoder(); - BikeFlagEncoder lessRelationCodes = new BikeFlagEncoder() - { + BikeFlagEncoder lessRelationCodes = new BikeFlagEncoder() { @Override - public int defineRelationBits( int index, int shift ) - { + public int defineRelationBits(int index, int shift) { relationCodeEncoder = new EncodedValue("RelationCode2", shift, 2, 1, 0, 3); return shift + 2; } @Override - public long handleRelationTags( ReaderRelation relation, long oldRelFlags ) - { + public long handleRelationTags(ReaderRelation relation, long oldRelFlags) { if (relation.hasTag("route", "bicycle")) return relationCodeEncoder.setValue(0, 2); return relationCodeEncoder.setValue(0, 0); } @Override - protected int handlePriority( ReaderWay way, double wayTypeSpeed, int priorityFromRelation ) - { + protected int handlePriority(ReaderWay way, double wayTypeSpeed, int priorityFromRelation) { return priorityFromRelation; } @Override - public String toString() - { + public String toString() { return "less_relations_bits"; } }; @@ -206,8 +177,7 @@ public String toString() } @Test - public void testMixBikeTypesAndRelationCombination() - { + public void testMixBikeTypesAndRelationCombination() { ReaderWay osmWay = new ReaderWay(1); osmWay.setTag("highway", "track"); osmWay.setTag("tracktype", "grade1"); @@ -231,8 +201,7 @@ public void testMixBikeTypesAndRelationCombination() > mtbEncoder.getDouble(flags, PriorityWeighting.KEY)); } - public void testFullBitMask() - { + public void testFullBitMask() { BitUtil bitUtil = BitUtil.LITTLE; EncodingManager manager = new EncodingManager("car,foot"); AbstractFlagEncoder carr = (AbstractFlagEncoder) manager.getEncoder("car"); @@ -243,15 +212,13 @@ public void testFullBitMask() } @Test - public void testFixWayName() - { + public void testFixWayName() { assertEquals("B8, B12", EncodingManager.fixWayName("B8;B12")); assertEquals("B8, B12", EncodingManager.fixWayName("B8; B12")); } @Test - public void testCompatibilityBug() - { + public void testCompatibilityBug() { EncodingManager manager2 = new EncodingManager(FlagEncoderFactory.DEFAULT, "bike2", 8); ReaderWay osmWay = new ReaderWay(1); osmWay.setTag("highway", "footway"); @@ -277,8 +244,7 @@ public void testCompatibilityBug() } @Test - public void testSupportFords() - { + public void testSupportFords() { // 1) no encoder crossing fords String flagEncodersStr = "car,bike,foot"; EncodingManager manager = new EncodingManager(FlagEncoderFactory.DEFAULT, flagEncodersStr, 8); diff --git a/core/src/test/java/com/graphhopper/routing/util/FootFlagEncoderTest.java b/core/src/test/java/com/graphhopper/routing/util/FootFlagEncoderTest.java index 6d9bd3d70c9..10d8cfe9ff2 100644 --- a/core/src/test/java/com/graphhopper/routing/util/FootFlagEncoderTest.java +++ b/core/src/test/java/com/graphhopper/routing/util/FootFlagEncoderTest.java @@ -24,10 +24,9 @@ import com.graphhopper.util.EdgeExplorer; import com.graphhopper.util.GHUtility; import com.graphhopper.util.Helper; -import java.text.DateFormat; - import org.junit.Test; +import java.text.DateFormat; import java.util.Date; import static org.junit.Assert.*; @@ -35,21 +34,18 @@ /** * @author Peter Karich */ -public class FootFlagEncoderTest -{ +public class FootFlagEncoderTest { private final EncodingManager encodingManager = new EncodingManager("car,bike,foot"); private final FootFlagEncoder footEncoder = (FootFlagEncoder) encodingManager.getEncoder("foot"); @Test - public void testGetSpeed() - { + public void testGetSpeed() { long fl = footEncoder.setProperties(10, true, true); assertEquals(10, footEncoder.getSpeed(fl), 1e-1); } @Test - public void testBasics() - { + public void testBasics() { long fl = footEncoder.flagsDefault(true, true); assertEquals(FootFlagEncoder.MEAN_SPEED, footEncoder.getSpeed(fl), 1e-1); @@ -59,8 +55,7 @@ public void testBasics() } @Test - public void testCombined() - { + public void testCombined() { FlagEncoder carEncoder = encodingManager.getEncoder("car"); long fl = footEncoder.setProperties(10, true, true) | carEncoder.setProperties(100, true, false); assertEquals(10, footEncoder.getSpeed(fl), 1e-1); @@ -75,8 +70,7 @@ public void testCombined() } @Test - public void testGraph() - { + public void testGraph() { Graph g = new GraphBuilder(encodingManager).create(); g.edge(0, 1).setDistance(10).setFlags(footEncoder.setProperties(10, true, true)); g.edge(0, 2).setDistance(10).setFlags(footEncoder.setProperties(5, true, true)); @@ -88,8 +82,7 @@ public void testGraph() } @Test - public void testAccess() - { + public void testAccess() { ReaderWay way = new ReaderWay(1); way.setTag("highway", "motorway"); @@ -183,8 +176,7 @@ public void testAccess() } @Test - public void testRailPlatformIssue366() - { + public void testRailPlatformIssue366() { ReaderWay way = new ReaderWay(1); way.setTag("railway", "platform"); long flags = footEncoder.handleWayTags(way, footEncoder.acceptWay(way), 0); @@ -204,8 +196,7 @@ public void testRailPlatformIssue366() } @Test - public void testMixSpeedAndSafe() - { + public void testMixSpeedAndSafe() { ReaderWay way = new ReaderWay(1); way.setTag("highway", "motorway"); long flags = footEncoder.handleWayTags(way, footEncoder.acceptWay(way), 0); @@ -222,8 +213,7 @@ public void testMixSpeedAndSafe() } @Test - public void testPriority() - { + public void testPriority() { ReaderWay way = new ReaderWay(1); way.setTag("highway", "cycleway"); assertEquals(PriorityCode.UNCHANGED.getValue(), footEncoder.handlePriority(way, 0)); @@ -274,8 +264,7 @@ public void testPriority() } @Test - public void testSlowHiking() - { + public void testSlowHiking() { ReaderWay way = new ReaderWay(1); way.setTag("highway", "track"); way.setTag("sac_scale", "hiking"); @@ -289,8 +278,7 @@ public void testSlowHiking() } @Test - public void testTurnFlagEncoding_noCostsAndRestrictions() - { + public void testTurnFlagEncoding_noCostsAndRestrictions() { long flags_r0 = footEncoder.getTurnFlags(true, 0); long flags_0 = footEncoder.getTurnFlags(false, 0); @@ -311,8 +299,7 @@ public void testTurnFlagEncoding_noCostsAndRestrictions() } @Test - public void testBarrierAccess() - { + public void testBarrierAccess() { // by default allow access through the gate for bike & foot! ReaderNode node = new ReaderNode(1, -1, -1); node.setTag("barrier", "gate"); @@ -348,8 +335,7 @@ public void testBarrierAccess() } @Test - public void handleWayTagsRoundabout() - { + public void handleWayTagsRoundabout() { ReaderWay way = new ReaderWay(1); way.setTag("junction", "roundabout"); way.setTag("highway", "tertiary"); @@ -357,8 +343,7 @@ public void handleWayTagsRoundabout() assertTrue(footEncoder.isBool(flags, FlagEncoder.K_ROUNDABOUT)); } - public void testFord() - { + public void testFord() { // by default deny access through fords! ReaderNode node = new ReaderNode(1, -1, -1); node.setTag("ford", "no"); diff --git a/core/src/test/java/com/graphhopper/routing/util/HikeFlagEncoderTest.java b/core/src/test/java/com/graphhopper/routing/util/HikeFlagEncoderTest.java index f9b5f31e2ab..d79363ce4fc 100644 --- a/core/src/test/java/com/graphhopper/routing/util/HikeFlagEncoderTest.java +++ b/core/src/test/java/com/graphhopper/routing/util/HikeFlagEncoderTest.java @@ -19,19 +19,18 @@ import com.graphhopper.reader.ReaderWay; import org.junit.Test; -import static org.junit.Assert.*; + +import static org.junit.Assert.assertEquals; /** * @author Peter Karich */ -public class HikeFlagEncoderTest -{ +public class HikeFlagEncoderTest { private final EncodingManager encodingManager = new EncodingManager("car,hike"); private final HikeFlagEncoder hikeEncoder = (HikeFlagEncoder) encodingManager.getEncoder("hike"); @Test - public void testPriority() - { + public void testPriority() { ReaderWay way = new ReaderWay(1); way.setTag("highway", "cycleway"); assertEquals(PriorityCode.UNCHANGED.getValue(), hikeEncoder.handlePriority(way, 0)); diff --git a/core/src/test/java/com/graphhopper/routing/util/MotorcycleFlagEncoderTest.java b/core/src/test/java/com/graphhopper/routing/util/MotorcycleFlagEncoderTest.java index 5915d444544..8e84e1cd614 100644 --- a/core/src/test/java/com/graphhopper/routing/util/MotorcycleFlagEncoderTest.java +++ b/core/src/test/java/com/graphhopper/routing/util/MotorcycleFlagEncoderTest.java @@ -22,9 +22,9 @@ import com.graphhopper.util.EdgeIteratorState; import com.graphhopper.util.GHUtility; import com.graphhopper.util.Helper; -import java.text.DateFormat; import org.junit.Test; +import java.text.DateFormat; import java.util.Date; import static org.junit.Assert.*; @@ -32,13 +32,11 @@ /** * @author Peter Karich */ -public class MotorcycleFlagEncoderTest -{ +public class MotorcycleFlagEncoderTest { private final EncodingManager em = new EncodingManager("motorcycle,foot"); private final MotorcycleFlagEncoder encoder = (MotorcycleFlagEncoder) em.getEncoder("motorcycle"); - private Graph initExampleGraph() - { + private Graph initExampleGraph() { GraphHopperStorage gs = new GraphHopperStorage(new RAMDirectory(), em, true, new GraphExtension.NoOpExtension()).create(1000); NodeAccess na = gs.getNodeAccess(); // 50--(0.0001)-->49--(0.0004)-->55--(0.0005)-->60 @@ -53,8 +51,7 @@ private Graph initExampleGraph() } @Test - public void testAccess() - { + public void testAccess() { ReaderWay way = new ReaderWay(1); assertFalse(encoder.acceptWay(way) > 0); way.setTag("highway", "service"); @@ -127,8 +124,7 @@ public void testAccess() } @Test - public void testHandleWayTags() - { + public void testHandleWayTags() { ReaderWay way = new ReaderWay(1); way.setTag("highway", "service"); long flags = encoder.acceptWay(way); @@ -139,8 +135,7 @@ public void testHandleWayTags() } @Test - public void testSetSpeed0_issue367() - { + public void testSetSpeed0_issue367() { long flags = encoder.setProperties(10, true, true); flags = encoder.setSpeed(flags, 0); @@ -151,8 +146,7 @@ public void testSetSpeed0_issue367() } @Test - public void testCurvature() - { + public void testCurvature() { Graph graph = initExampleGraph(); EdgeIteratorState edge = GHUtility.getEdge(graph, 0, 1); @@ -162,8 +156,7 @@ public void testCurvature() assertTrue("The bendiness of the straight road is smaller than the one of the curvy road", bendinessOfCurvyWay < bendinessOfStraightWay); } - private double getBendiness( EdgeIteratorState edge, double estimatedDistance ) - { + private double getBendiness(EdgeIteratorState edge, double estimatedDistance) { ReaderWay way = new ReaderWay(1); way.setTag("highway", "primary"); way.setTag("estimated_distance", estimatedDistance); diff --git a/core/src/test/java/com/graphhopper/routing/util/MountainBikeFlagEncoderTest.java b/core/src/test/java/com/graphhopper/routing/util/MountainBikeFlagEncoderTest.java index 1b7c427edff..92bb345d81a 100644 --- a/core/src/test/java/com/graphhopper/routing/util/MountainBikeFlagEncoderTest.java +++ b/core/src/test/java/com/graphhopper/routing/util/MountainBikeFlagEncoderTest.java @@ -20,24 +20,19 @@ import com.graphhopper.reader.ReaderNode; import com.graphhopper.reader.ReaderRelation; import com.graphhopper.reader.ReaderWay; - -import static com.graphhopper.routing.util.PriorityCode.*; - import org.junit.Test; +import static com.graphhopper.routing.util.PriorityCode.*; import static org.junit.Assert.*; -public class MountainBikeFlagEncoderTest extends AbstractBikeFlagEncoderTester -{ +public class MountainBikeFlagEncoderTest extends AbstractBikeFlagEncoderTester { @Override - protected BikeCommonFlagEncoder createBikeEncoder() - { + protected BikeCommonFlagEncoder createBikeEncoder() { return (BikeCommonFlagEncoder) new EncodingManager("bike,mtb").getEncoder("mtb"); } @Test - public void testGetSpeed() - { + public void testGetSpeed() { long result = encoder.setProperties(10, true, true); assertEquals(10, encoder.getSpeed(result), 1e-1); ReaderWay way = new ReaderWay(1); @@ -87,8 +82,7 @@ public void testGetSpeed() @Test @Override - public void testSacScale() - { + public void testSacScale() { ReaderWay way = new ReaderWay(1); way.setTag("highway", "service"); way.setTag("sac_scale", "hiking"); @@ -106,8 +100,7 @@ public void testSacScale() } @Test - public void testHandleWayTags() - { + public void testHandleWayTags() { ReaderWay way = new ReaderWay(1); String wayType; @@ -155,8 +148,7 @@ public void testHandleWayTags() } @Test - public void testHandleWayTagsInfluencedByRelation() - { + public void testHandleWayTagsInfluencedByRelation() { ReaderWay osmWay = new ReaderWay(1); osmWay.setTag("highway", "track"); long allowed = encoder.acceptBit; @@ -209,8 +201,7 @@ public void testHandleWayTagsInfluencedByRelation() // Issue 407 : Always block kissing_gate execpt for mountainbikes @Test @Override - public void testBarrierAccess() - { + public void testBarrierAccess() { // kissing_gate without bicycle tag ReaderNode node = new ReaderNode(1, -1, -1); node.setTag("barrier", "kissing_gate"); diff --git a/core/src/test/java/com/graphhopper/routing/util/RacingBikeFlagEncoderTest.java b/core/src/test/java/com/graphhopper/routing/util/RacingBikeFlagEncoderTest.java index 22e34ebd024..e620b5c0eef 100644 --- a/core/src/test/java/com/graphhopper/routing/util/RacingBikeFlagEncoderTest.java +++ b/core/src/test/java/com/graphhopper/routing/util/RacingBikeFlagEncoderTest.java @@ -19,29 +19,25 @@ import com.graphhopper.reader.ReaderRelation; import com.graphhopper.reader.ReaderWay; +import org.junit.Test; import static com.graphhopper.routing.util.BikeCommonFlagEncoder.PUSHING_SECTION_SPEED; import static com.graphhopper.routing.util.PriorityCode.*; - -import org.junit.Test; - -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; /** * @author ratrun */ -public class RacingBikeFlagEncoderTest extends AbstractBikeFlagEncoderTester -{ +public class RacingBikeFlagEncoderTest extends AbstractBikeFlagEncoderTester { @Override - protected BikeCommonFlagEncoder createBikeEncoder() - { + protected BikeCommonFlagEncoder createBikeEncoder() { return (BikeCommonFlagEncoder) new EncodingManager("bike,racingbike").getEncoder("racingbike"); } @Test @Override - public void testAvoidTunnel() - { + public void testAvoidTunnel() { // tunnel is not that bad for racing bike ReaderWay osmWay = new ReaderWay(1); osmWay.setTag("highway", "residential"); @@ -58,8 +54,7 @@ public void testAvoidTunnel() @Test @Override - public void testService() - { + public void testService() { ReaderWay way = new ReaderWay(1); way.setTag("highway", "service"); assertEquals(12, encoder.getSpeed(way)); @@ -72,8 +67,7 @@ public void testService() @Test @Override - public void testSacScale() - { + public void testSacScale() { ReaderWay way = new ReaderWay(1); way.setTag("highway", "service"); way.setTag("sac_scale", "mountain_hiking"); @@ -98,8 +92,7 @@ public void testSacScale() } @Test - public void testGetSpeed() - { + public void testGetSpeed() { long result = encoder.setProperties(10, true, true); assertEquals(10, encoder.getSpeed(result), 1e-1); ReaderWay way = new ReaderWay(1); @@ -133,8 +126,7 @@ public void testGetSpeed() } @Test - public void testHandleWayTagsInfluencedByRelation() - { + public void testHandleWayTagsInfluencedByRelation() { ReaderWay osmWay = new ReaderWay(1); osmWay.setTag("highway", "track"); assertEquals(PUSHING_SECTION_SPEED / 2, getSpeedFromFlags(osmWay), 1e-1); @@ -205,8 +197,7 @@ public void testHandleWayTagsInfluencedByRelation() } @Test - public void testAvoidanceOfHighMaxSpeed() - { + public void testAvoidanceOfHighMaxSpeed() { ReaderWay osmWay = new ReaderWay(1); osmWay.setTag("highway", "tertiary"); osmWay.setTag("maxspeed", "50"); @@ -266,8 +257,7 @@ public void testAvoidanceOfHighMaxSpeed() } @Test - public void testClassBicycle() - { + public void testClassBicycle() { ReaderWay way = new ReaderWay(1); way.setTag("highway", "tertiary"); way.setTag("class:bicycle:roadcycling", "3"); diff --git a/core/src/test/java/com/graphhopper/routing/util/tour/SinglePointTourTest.java b/core/src/test/java/com/graphhopper/routing/util/tour/SinglePointTourTest.java index 39a271611f2..f9b9a4bcbec 100644 --- a/core/src/test/java/com/graphhopper/routing/util/tour/SinglePointTourTest.java +++ b/core/src/test/java/com/graphhopper/routing/util/tour/SinglePointTourTest.java @@ -17,20 +17,19 @@ */ package com.graphhopper.routing.util.tour; -import java.util.Random; import org.junit.Test; +import java.util.Random; + import static junit.framework.TestCase.assertEquals; import static junit.framework.TestCase.assertTrue; /** * @author Robin Boldt */ -public class SinglePointTourTest -{ +public class SinglePointTourTest { @Test - public void testBasics() - { + public void testBasics() { SinglePointTour tour = new SinglePointTour(new Random(0), 100); assertEquals(1, tour.getNumberOfGeneratedPoints()); diff --git a/core/src/test/java/com/graphhopper/routing/weighting/AbstractWeightingTest.java b/core/src/test/java/com/graphhopper/routing/weighting/AbstractWeightingTest.java index edb12ce39b7..5587d168e68 100644 --- a/core/src/test/java/com/graphhopper/routing/weighting/AbstractWeightingTest.java +++ b/core/src/test/java/com/graphhopper/routing/weighting/AbstractWeightingTest.java @@ -17,19 +17,17 @@ */ package com.graphhopper.routing.weighting; -import com.graphhopper.routing.weighting.AbstractWeighting; import org.junit.Test; -import static org.junit.Assert.*; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; /** - * * @author Peter Karich */ -public class AbstractWeightingTest -{ +public class AbstractWeightingTest { @Test - public void testToString() - { + public void testToString() { assertTrue(AbstractWeighting.isValidName("blup")); assertTrue(AbstractWeighting.isValidName("blup_a")); assertTrue(AbstractWeighting.isValidName("blup|a")); diff --git a/core/src/test/java/com/graphhopper/routing/weighting/FastestWeightingTest.java b/core/src/test/java/com/graphhopper/routing/weighting/FastestWeightingTest.java index da3ac0d3808..8d8836275cd 100644 --- a/core/src/test/java/com/graphhopper/routing/weighting/FastestWeightingTest.java +++ b/core/src/test/java/com/graphhopper/routing/weighting/FastestWeightingTest.java @@ -20,31 +20,31 @@ import com.graphhopper.routing.VirtualEdgeIteratorState; import com.graphhopper.routing.util.EncodingManager; import com.graphhopper.routing.util.FlagEncoder; -import com.graphhopper.util.*; -import static com.graphhopper.util.GHUtility.*; +import com.graphhopper.util.EdgeIterator; +import com.graphhopper.util.Helper; +import com.graphhopper.util.PMap; +import com.graphhopper.util.Parameters; import com.graphhopper.util.Parameters.Routing; import org.junit.Test; +import static com.graphhopper.util.GHUtility.createMockedEdgeIteratorState; import static org.junit.Assert.assertEquals; /** * @author Peter Karich */ -public class FastestWeightingTest -{ +public class FastestWeightingTest { private final FlagEncoder encoder = new EncodingManager("car").getEncoder("car"); @Test - public void testMinWeightHasSameUnitAs_getWeight() - { + public void testMinWeightHasSameUnitAs_getWeight() { Weighting instance = new FastestWeighting(encoder); long flags = encoder.setProperties(encoder.getMaxSpeed(), true, true); assertEquals(instance.getMinWeight(10), instance.calcWeight(createMockedEdgeIteratorState(10, flags), false, EdgeIterator.NO_EDGE), 1e-8); } @Test - public void testWeightWrongHeading() - { + public void testWeightWrongHeading() { Weighting instance = new FastestWeighting(encoder, new PMap().put(Parameters.Routing.HEADING_PENALTY, "100")); VirtualEdgeIteratorState virtEdge = new VirtualEdgeIteratorState(0, 1, 1, 2, 10, encoder.setProperties(10, true, true), "test", Helper.createPointList(51, 0, 51, 1)); @@ -67,13 +67,12 @@ public void testWeightWrongHeading() } @Test - public void testSpeed0() - { + public void testSpeed0() { Weighting instance = new FastestWeighting(encoder); assertEquals(1.0 / 0, instance.calcWeight(createMockedEdgeIteratorState(10, encoder.setProperties(0, true, true)), false, EdgeIterator.NO_EDGE), 1e-8); // 0 / 0 returns NaN but calcWeight should not return NaN! assertEquals(1.0 / 0, instance.calcWeight(createMockedEdgeIteratorState(0, encoder.setProperties(0, true, true)), false, EdgeIterator.NO_EDGE), 1e-8); - } + } } diff --git a/core/src/test/java/com/graphhopper/routing/weighting/ShortFastestWeightingTest.java b/core/src/test/java/com/graphhopper/routing/weighting/ShortFastestWeightingTest.java index edfdf3f6601..5022920a0d9 100644 --- a/core/src/test/java/com/graphhopper/routing/weighting/ShortFastestWeightingTest.java +++ b/core/src/test/java/com/graphhopper/routing/weighting/ShortFastestWeightingTest.java @@ -19,9 +19,10 @@ import com.graphhopper.routing.util.EncodingManager; import com.graphhopper.routing.util.FlagEncoder; -import com.graphhopper.routing.weighting.Weighting; -import com.graphhopper.routing.weighting.ShortFastestWeighting; -import com.graphhopper.util.*; +import com.graphhopper.util.EdgeIterator; +import com.graphhopper.util.EdgeIteratorState; +import com.graphhopper.util.GHUtility; +import com.graphhopper.util.PMap; import org.junit.Test; import static org.junit.Assert.assertEquals; @@ -30,13 +31,11 @@ /** * @author Peter Karich */ -public class ShortFastestWeightingTest -{ +public class ShortFastestWeightingTest { private final FlagEncoder encoder = new EncodingManager("car").getEncoder("car"); @Test - public void testShort() - { + public void testShort() { EdgeIteratorState edge = createEdge(10, encoder.setProperties(50, true, true)); Weighting instance = new ShortFastestWeighting(encoder, 0.03); assertEquals(1.02, instance.calcWeight(edge, false, EdgeIterator.NO_EDGE), 1e-8); @@ -47,36 +46,28 @@ public void testShort() } @Test - public void testTooSmall() - { - try - { + public void testTooSmall() { + try { new ShortFastestWeighting(encoder, new PMap("short_fastest.distance_factor=0|short_fastest.time_factor=0")); assertTrue(false); - } catch (Exception ex) - { + } catch (Exception ex) { } } - EdgeIterator createEdge( final double distance, final long flags ) - { - return new GHUtility.DisabledEdgeIterator() - { + EdgeIterator createEdge(final double distance, final long flags) { + return new GHUtility.DisabledEdgeIterator() { @Override - public double getDistance() - { + public double getDistance() { return distance; } @Override - public long getFlags() - { + public long getFlags() { return flags; } @Override - public boolean getBool( int key, boolean _default ) - { + public boolean getBool(int key, boolean _default) { return _default; } }; diff --git a/core/src/test/java/com/graphhopper/search/NameIndexTest.java b/core/src/test/java/com/graphhopper/search/NameIndexTest.java index 693417d76a7..da7ff836213 100644 --- a/core/src/test/java/com/graphhopper/search/NameIndexTest.java +++ b/core/src/test/java/com/graphhopper/search/NameIndexTest.java @@ -19,24 +19,23 @@ import com.graphhopper.storage.RAMDirectory; import com.graphhopper.util.Helper; -import java.io.File; import org.junit.Test; -import static org.junit.Assert.*; +import java.io.File; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; /** * @author Peter Karich */ -public class NameIndexTest -{ +public class NameIndexTest { @Test - public void testNoErrorOnLargeName() - { + public void testNoErrorOnLargeName() { NameIndex index = new NameIndex(new RAMDirectory()).create(1000); // 127 => bytes.length == 254 String str = ""; - for (int i = 0; i < 127; i++) - { + for (int i = 0; i < 127; i++) { str += "ß"; } long result = index.put(str); @@ -44,8 +43,7 @@ public void testNoErrorOnLargeName() } @Test - public void testPut() - { + public void testPut() { NameIndex index = new NameIndex(new RAMDirectory()).create(1000); long result = index.put("Something Streetä"); assertEquals("Something Streetä", index.get(result)); @@ -63,8 +61,7 @@ public void testPut() } @Test - public void testCreate() - { + public void testCreate() { NameIndex index = new NameIndex(new RAMDirectory()).create(1000); String str1 = "nice"; long pointer1 = index.put(str1); @@ -78,15 +75,13 @@ public void testCreate() } @Test - public void testTooLongNameNoError() - { + public void testTooLongNameNoError() { NameIndex index = new NameIndex(new RAMDirectory()).create(1000); // WTH are they doing in OSM? There are exactly two names in the full planet export which violates this limitation! index.put("Бухарестская улица (http://ru.wikipedia.org/wiki/%D0%91%D1%83%D1%85%D0%B0%D1%80%D0%B5%D1%81%D1%82%D1%81%D0%BA%D0%B0%D1%8F_%D1%83%D0%BB%D0%B8%D1%86%D0%B0_(%D0%A1%D0%B0%D0%BD%D0%BA%D1%82-%D0%9F%D0%B5%D1%82%D0%B5%D1%80%D0%B1%D1%83%D1%80%D0%B3))"); String str = "sdfsdfds"; - for (int i = 0; i < 256 * 3; i++) - { + for (int i = 0; i < 256 * 3; i++) { str += "Б"; } index.put(str); @@ -94,8 +89,7 @@ public void testTooLongNameNoError() } @Test - public void testFlush() - { + public void testFlush() { String location = "./target/nameindex-store"; Helper.removeDir(new File(location)); diff --git a/core/src/test/java/com/graphhopper/storage/AbstractDirectoryTester.java b/core/src/test/java/com/graphhopper/storage/AbstractDirectoryTester.java index 77baf39bb10..61472da135f 100644 --- a/core/src/test/java/com/graphhopper/storage/AbstractDirectoryTester.java +++ b/core/src/test/java/com/graphhopper/storage/AbstractDirectoryTester.java @@ -18,56 +18,49 @@ package com.graphhopper.storage; import com.graphhopper.util.Helper; - -import java.io.File; - import org.junit.After; import org.junit.Before; import org.junit.Test; -import static org.junit.Assert.*; +import java.io.File; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; /** * @author Peter Karich */ -public abstract class AbstractDirectoryTester -{ +public abstract class AbstractDirectoryTester { protected String location = "./target/tmp/dir"; private DataAccess da; abstract Directory createDir(); @After - public void tearDown() - { + public void tearDown() { if (da != null) da.close(); Helper.removeDir(new File(location)); } @Before - public void setUp() - { + public void setUp() { Helper.removeDir(new File(location)); } @Test - public void testRequestedDataAccessHasToBeTheIdenticalType() - { + public void testRequestedDataAccessHasToBeTheIdenticalType() { Directory dir = createDir(); da = dir.find("testing", new DAType(DAType.MemRef.HEAP, false, false, false, false)); - try - { + try { dir.find("testing", new DAType(DAType.MemRef.HEAP, false, false, false, true)); assertFalse(true); - } catch (Exception ex) - { + } catch (Exception ex) { } } @Test - public void testSynched() - { + public void testSynched() { Directory dir = createDir(); DataAccess da1 = dir.find("testing", new DAType(DAType.MemRef.HEAP, false, false, true, false)); da = dir.find("testing_synched", new DAType(DAType.MemRef.HEAP, false, false, true, true)); @@ -76,8 +69,7 @@ public void testSynched() } @Test - public void testNoDuplicates() - { + public void testNoDuplicates() { Directory dir = createDir(); DataAccess da1 = dir.find("testing"); DataAccess da2 = dir.find("testing"); @@ -87,8 +79,7 @@ public void testNoDuplicates() } @Test - public void testNoErrorForDACreate() - { + public void testNoErrorForDACreate() { Directory dir = createDir(); da = dir.find("testing"); da.create(100); diff --git a/core/src/test/java/com/graphhopper/storage/AbstractGraphStorageTester.java b/core/src/test/java/com/graphhopper/storage/AbstractGraphStorageTester.java index aaaf78e77f6..0a2e98f8df2 100644 --- a/core/src/test/java/com/graphhopper/storage/AbstractGraphStorageTester.java +++ b/core/src/test/java/com/graphhopper/storage/AbstractGraphStorageTester.java @@ -17,47 +17,75 @@ */ package com.graphhopper.storage; -import static com.graphhopper.util.GHUtility.count; -import static org.junit.Assert.*; +import com.graphhopper.routing.util.*; +import com.graphhopper.util.*; +import com.graphhopper.util.shapes.BBox; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; import java.io.Closeable; import java.io.File; +import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.List; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - -import com.graphhopper.routing.util.*; -import com.graphhopper.util.*; -import com.graphhopper.util.shapes.BBox; -import java.io.IOException; +import static com.graphhopper.util.GHUtility.count; +import static org.junit.Assert.*; /** * Abstract test class to be extended for implementations of the Graph interface. Graphs * implementing GraphStorage should extend GraphStorageTest instead. *

    + * * @author Peter Karich */ -public abstract class AbstractGraphStorageTester -{ +public abstract class AbstractGraphStorageTester { private final String locationParent = "./target/graphstorage"; protected int defaultSize = 100; protected String defaultGraphLoc = "./target/graphstorage/default"; protected EncodingManager encodingManager = new EncodingManager("car,foot"); protected CarFlagEncoder carEncoder = (CarFlagEncoder) encodingManager.getEncoder("car"); protected FootFlagEncoder footEncoder = (FootFlagEncoder) encodingManager.getEncoder("foot"); + protected GraphHopperStorage graph; EdgeFilter carOutFilter = new DefaultEdgeFilter(carEncoder, false, true); EdgeFilter carInFilter = new DefaultEdgeFilter(carEncoder, true, false); EdgeExplorer carOutExplorer; EdgeExplorer carInExplorer; EdgeExplorer carAllExplorer; - protected GraphHopperStorage graph; - protected GraphHopperStorage createGHStorage() - { + 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 static int getIdOf(Graph g, double latitude) { + int s = g.getNodes(); + NodeAccess na = g.getNodeAccess(); + for (int i = 0; i < s; i++) { + if (Math.abs(na.getLatitude(i) - latitude) < 1e-4) { + return i; + } + } + return -1; + } + + public static int getIdOf(Graph g, double latitude, double longitude) { + int s = g.getNodes(); + NodeAccess na = g.getNodeAccess(); + for (int i = 0; i < s; i++) { + if (Math.abs(na.getLatitude(i) - latitude) < 1e-4 && Math.abs(na.getLongitude(i) - longitude) < 1e-4) { + return i; + } + } + throw new IllegalArgumentException("did not find node with location " + (float) latitude + "," + (float) longitude); + } + + protected GraphHopperStorage createGHStorage() { GraphHopperStorage g = createGHStorage(defaultGraphLoc, false); carOutExplorer = g.createEdgeExplorer(carOutFilter); carInExplorer = g.createEdgeExplorer(carInFilter); @@ -65,29 +93,25 @@ protected GraphHopperStorage createGHStorage() return g; } - abstract GraphHopperStorage createGHStorage( String location, boolean is3D ); + abstract GraphHopperStorage createGHStorage(String location, boolean is3D); - protected final GraphHopperStorage newRAMGHStorage() - { + protected final GraphHopperStorage newRAMGHStorage() { return new GraphHopperStorage(new RAMDirectory(), encodingManager, false, new GraphExtension.NoOpExtension()); } @Before - public void setUp() - { + public void setUp() { Helper.removeDir(new File(locationParent)); } @After - public void tearDown() - { + public void tearDown() { Helper.close((Closeable) graph); Helper.removeDir(new File(locationParent)); } @Test - public void testSetTooBigDistance_435() - { + public void testSetTooBigDistance_435() { graph = createGHStorage(); double maxDist = EdgeAccess.MAX_DIST; @@ -95,22 +119,18 @@ public void testSetTooBigDistance_435() assertEquals(maxDist, edge1.getDistance(), 1); // max out should NOT lead to infinity as this leads fast to NaN! - try - { + try { graph.edge(0, 2, maxDist + 1, true); - } catch (Exception ex) - { + } catch (Exception ex) { assertTrue(ex.getMessage(), ex.getMessage().startsWith("Distance too large")); } } @Test - public void testSetNodes() - { + public void testSetNodes() { graph = createGHStorage(); NodeAccess na = graph.getNodeAccess(); - for (int i = 0; i < defaultSize * 2; i++) - { + for (int i = 0; i < defaultSize * 2; i++) { na.setNode(i, 2 * i, 3 * i); } graph.edge(defaultSize + 1, defaultSize + 2, 10, true); @@ -119,16 +139,14 @@ public void testSetNodes() } @Test - public void testPropertiesWithNoInit() - { + public void testPropertiesWithNoInit() { graph = createGHStorage(); assertEquals(0, graph.edge(0, 1).getFlags()); assertEquals(0, graph.edge(0, 2).getDistance(), 1e-6); } @Test - public void testCreateLocation() - { + public void testCreateLocation() { graph = createGHStorage(); graph.edge(3, 1, 50, true); assertEquals(1, count(carOutExplorer.setBaseNode(1))); @@ -138,8 +156,7 @@ public void testCreateLocation() } @Test - public void testEdges() - { + public void testEdges() { graph = createGHStorage(); graph.edge(2, 1, 12, true); assertEquals(1, count(carOutExplorer.setBaseNode(2))); @@ -151,8 +168,7 @@ public void testEdges() } @Test - public void testUnidirectional() - { + public void testUnidirectional() { graph = createGHStorage(); graph.edge(1, 2, 12, false); @@ -185,8 +201,7 @@ public void testUnidirectional() } @Test - public void testUnidirectionalEdgeFilter() - { + public void testUnidirectionalEdgeFilter() { graph = createGHStorage(); graph.edge(1, 2, 12, false); @@ -221,8 +236,7 @@ public void testUnidirectionalEdgeFilter() } @Test - public void testUpdateUnidirectional() - { + public void testUpdateUnidirectional() { graph = createGHStorage(); graph.edge(1, 2, 12, false); @@ -245,8 +259,7 @@ public void testUpdateUnidirectional() } @Test - public void testClone() - { + public void testClone() { graph = createGHStorage(); graph.edge(1, 2, 10, true); NodeAccess na = graph.getNodeAccess(); @@ -266,8 +279,7 @@ public void testClone() } @Test - public void testCopyProperties() - { + public void testCopyProperties() { graph = createGHStorage(); EdgeIteratorState edge = graph.edge(1, 3, 10, false).setName("testing").setWayGeometry(Helper.createPointList(1, 2)); @@ -280,8 +292,7 @@ public void testCopyProperties() } @Test - public void testGetLocations() - { + public void testGetLocations() { graph = createGHStorage(); NodeAccess na = graph.getNodeAccess(); na.setNode(0, 12, 23); @@ -300,31 +311,26 @@ public void testGetLocations() } @Test - public void testCopyTo() - { + public void testCopyTo() { graph = createGHStorage(); initExampleGraph(graph); GraphHopperStorage gs = newRAMGHStorage(); gs.setSegmentSize(8000); gs.create(10); - try - { + try { graph.copyTo(gs); checkExampleGraph(gs); - } catch (Exception ex) - { + } catch (Exception ex) { ex.printStackTrace(); assertTrue(ex.toString(), false); } - try - { + try { Helper.close((Closeable) graph); graph = createGHStorage(); gs.copyTo(graph); checkExampleGraph(graph); - } catch (Exception ex) - { + } catch (Exception ex) { ex.printStackTrace(); assertTrue(ex.toString(), false); } @@ -332,15 +338,13 @@ public void testCopyTo() } @Test - public void testAddLocation() - { + public void testAddLocation() { graph = createGHStorage(); initExampleGraph(graph); checkExampleGraph(graph); } - protected void initExampleGraph( Graph g ) - { + protected void initExampleGraph(Graph g) { NodeAccess na = g.getNodeAccess(); na.setNode(0, 12, 23); na.setNode(1, 38.33f, 135.3f); @@ -355,8 +359,7 @@ protected void initExampleGraph( Graph g ) g.edge(0, 5, 212, true); } - private void checkExampleGraph( Graph graph ) - { + private void checkExampleGraph(Graph graph) { NodeAccess na = graph.getNodeAccess(); assertEquals(12f, na.getLatitude(0), 1e-6); assertEquals(23f, na.getLongitude(0), 1e-6); @@ -372,19 +375,16 @@ private void checkExampleGraph( Graph graph ) assertEquals(GHUtility.asSet(0), GHUtility.getNeighbors(carOutExplorer.setBaseNode((1)))); assertEquals(GHUtility.asSet(5, 4, 3, 2, 1), GHUtility.getNeighbors(carOutExplorer.setBaseNode(0))); - try - { + try { assertEquals(0, count(carOutExplorer.setBaseNode(6))); // for now return empty iterator // assertFalse(true); - } catch (Exception ex) - { + } catch (Exception ex) { } } @Test - public void testDirectional() - { + public void testDirectional() { graph = createGHStorage(); graph.edge(1, 2, 12, true); graph.edge(2, 3, 12, false); @@ -414,8 +414,7 @@ public void testDirectional() } @Test - public void testDozendEdges() - { + public void testDozendEdges() { graph = createGHStorage(); graph.edge(1, 2, 12, true); assertEquals(1, count(carAllExplorer.setBaseNode(1))); @@ -447,8 +446,7 @@ public void testDozendEdges() } @Test - public void testCheckFirstNode() - { + public void testCheckFirstNode() { graph = createGHStorage(); assertEquals(0, count(carAllExplorer.setBaseNode(1))); @@ -457,8 +455,7 @@ public void testCheckFirstNode() } @Test - public void testDeleteNodeForUnidir() - { + public void testDeleteNodeForUnidir() { graph = createGHStorage(); NodeAccess na = graph.getNodeAccess(); na.setNode(10, 10, 1); @@ -489,19 +486,16 @@ public void testDeleteNodeForUnidir() } @Test - public void testComplexDeleteNode() - { + public void testComplexDeleteNode() { testDeleteNodes(21); } @Test - public void testComplexDeleteNode2() - { + public void testComplexDeleteNode2() { testDeleteNodes(6); } - public void testDeleteNodes( int fillToSize ) - { + public void testDeleteNodes(int fillToSize) { graph = createGHStorage(); NodeAccess na = graph.getNodeAccess(); na.setNode(0, 12, 23); @@ -512,15 +506,12 @@ public void testDeleteNodes( int fillToSize ) na.setNode(5, 2.5f, 1); int deleted = 2; - for (int i = 6; i < fillToSize; i++) - { + for (int i = 6; i < fillToSize; i++) { na.setNode(i, i * 1.5, i * 1.6); - if (i % 3 == 0) - { + if (i % 3 == 0) { graph.markNodeRemoved(i); deleted++; - } else - { + } else { // connect to // ... a deleted node graph.edge(i, 0, 10 * i, true); @@ -560,11 +551,9 @@ public void testDeleteNodes( int fillToSize ) assertFalse(containsLatitude(graph, carAllExplorer.setBaseNode(id3), 12)); } - public boolean containsLatitude( Graph g, EdgeIterator iter, double latitude ) - { + public boolean containsLatitude(Graph g, EdgeIterator iter, double latitude) { NodeAccess na = g.getNodeAccess(); - while (iter.next()) - { + while (iter.next()) { if (Math.abs(na.getLatitude(iter.getAdjNode()) - latitude) < 1e-4) return true; } @@ -572,8 +561,7 @@ public boolean containsLatitude( Graph g, EdgeIterator iter, double latitude ) } @Test - public void testSimpleDelete() - { + public void testSimpleDelete() { graph = createGHStorage(); NodeAccess na = graph.getNodeAccess(); na.setNode(0, 12, 23); @@ -603,8 +591,7 @@ public void testSimpleDelete() } @Test - public void testSimpleDelete2() - { + public void testSimpleDelete2() { graph = createGHStorage(); NodeAccess na = graph.getNodeAccess(); assertEquals(-1, getIdOf(graph, 12)); @@ -652,8 +639,7 @@ public void testSimpleDelete2() } @Test - public void testSimpleDelete3() - { + public void testSimpleDelete3() { graph = createGHStorage(); NodeAccess na = graph.getNodeAccess(); na.setNode(7, 7, 1); @@ -684,8 +670,7 @@ public void testSimpleDelete3() } @Test - public void testDeleteAndOptimize() - { + public void testDeleteAndOptimize() { graph = createGHStorage(); NodeAccess na = graph.getNodeAccess(); na.setNode(20, 10, 10); @@ -696,8 +681,7 @@ public void testDeleteAndOptimize() } @Test - public void testBounds() - { + public void testBounds() { graph = createGHStorage(); BBox b = graph.getBounds(); assertEquals(BBox.createInverse(false).maxLat, b.maxLat, 1e-6); @@ -715,8 +699,7 @@ public void testBounds() } @Test - public void testFlags() - { + public void testFlags() { graph = createGHStorage(); graph.edge(0, 1).setDistance(10).setFlags(carEncoder.setProperties(100, true, true)); graph.edge(2, 3).setDistance(10).setFlags(carEncoder.setProperties(10, true, false)); @@ -729,18 +712,15 @@ public void testFlags() assertTrue(iter.next()); assertEquals(carEncoder.setProperties(10, true, false), iter.getFlags()); - try - { + try { graph.edge(0, 1).setDistance(-1); assertTrue(false); - } catch (IllegalArgumentException ex) - { + } catch (IllegalArgumentException ex) { } } @Test - public void testEdgeProperties() - { + public void testEdgeProperties() { graph = createGHStorage(); EdgeIteratorState iter1 = graph.edge(0, 1, 10, true); EdgeIteratorState iter2 = graph.edge(0, 2, 20, true); @@ -772,41 +752,34 @@ public void testEdgeProperties() graph.optimize(); // throw exception if accessing deleted edge - try - { + try { graph.getEdgeIteratorState(iter1.getEdge(), -1); assertTrue(false); - } catch (Exception ex) - { + } catch (Exception ex) { } } @Test - public void testCreateDuplicateEdges() - { + public void testCreateDuplicateEdges() { graph = createGHStorage(); graph.edge(2, 1, 12, true); graph.edge(2, 3, 12, true); graph.edge(2, 3, 13, false); assertEquals(3, GHUtility.count(carOutExplorer.setBaseNode(2))); - // no exception + // no exception graph.getEdgeIteratorState(1, 3); // raise exception - try - { + try { graph.getEdgeIteratorState(4, 3); assertTrue(false); - } catch (Exception ex) - { + } catch (Exception ex) { } - try - { + try { graph.getEdgeIteratorState(-1, 3); assertTrue(false); - } catch (Exception ex) - { + } catch (Exception ex) { } EdgeIterator iter = carOutExplorer.setBaseNode(2); @@ -828,16 +801,14 @@ public void testCreateDuplicateEdges() } @Test - public void testIdenticalNodes() - { + public void testIdenticalNodes() { graph = createGHStorage(); graph.edge(0, 0, 100, true); assertEquals(1, GHUtility.count(carAllExplorer.setBaseNode(0))); } @Test - public void testIdenticalNodes2() - { + public void testIdenticalNodes2() { graph = createGHStorage(); graph.edge(0, 0, 100, false); graph.edge(0, 0, 100, false); @@ -845,8 +816,7 @@ public void testIdenticalNodes2() } @Test - public void testEdgeReturn() - { + public void testEdgeReturn() { graph = createGHStorage(); EdgeIteratorState iter = graph.edge(4, 10).setDistance(100).setFlags(carEncoder.setProperties(10, true, false)); assertEquals(4, iter.getBaseNode()); @@ -857,8 +827,7 @@ public void testEdgeReturn() } @Test - public void testPillarNodes() - { + public void testPillarNodes() { graph = createGHStorage(); NodeAccess na = graph.getNodeAccess(); na.setNode(0, 0.01, 0.01); @@ -907,8 +876,7 @@ public void testPillarNodes() } @Test - public void testFootMix() - { + public void testFootMix() { graph = createGHStorage(); graph.edge(0, 1).setDistance(10).setFlags(footEncoder.setProperties(10, true, true)); graph.edge(0, 2).setDistance(10).setFlags(carEncoder.setProperties(10, true, true)); @@ -919,8 +887,7 @@ public void testFootMix() } @Test - public void testGetAllEdges() - { + public void testGetAllEdges() { graph = createGHStorage(); graph.edge(0, 1, 2, true); graph.edge(3, 1, 1, false); @@ -947,8 +914,7 @@ public void testGetAllEdges() } @Test - public void testGetAllEdgesWithDelete() - { + public void testGetAllEdgesWithDelete() { graph = createGHStorage(); NodeAccess na = graph.getNodeAccess(); na.setNode(0, 0, 5); @@ -983,47 +949,8 @@ public void testGetAllEdgesWithDelete() assertFalse(iter.next()); } - 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 static int getIdOf( Graph g, double latitude ) - { - int s = g.getNodes(); - NodeAccess na = g.getNodeAccess(); - for (int i = 0; i < s; i++) - { - if (Math.abs(na.getLatitude(i) - latitude) < 1e-4) - { - return i; - } - } - return -1; - } - - public static int getIdOf( Graph g, double latitude, double longitude ) - { - int s = g.getNodes(); - NodeAccess na = g.getNodeAccess(); - for (int i = 0; i < s; i++) - { - if (Math.abs(na.getLatitude(i) - latitude) < 1e-4 && Math.abs(na.getLongitude(i) - longitude) < 1e-4) - { - return i; - } - } - throw new IllegalArgumentException("did not find node with location " + (float) latitude + "," + (float) longitude); - } - @Test - public void testNameIndex() - { + public void testNameIndex() { graph = createGHStorage(); EdgeIteratorState iter1 = graph.edge(0, 1, 10, true); iter1.setName("named street1"); @@ -1036,15 +963,12 @@ public void testNameIndex() } @Test - public void test8BytesFlags() - { + public void test8BytesFlags() { Directory dir = new RAMDirectory(); List list = new ArrayList(); - list.add(new TmpCarFlagEncoder(29, 0.001, 0) - { + list.add(new TmpCarFlagEncoder(29, 0.001, 0) { @Override - public String toString() - { + public String toString() { return "car2"; } }); @@ -1081,8 +1005,7 @@ public String toString() } @Test - public void testEnabledElevation() - { + public void testEnabledElevation() { graph = createGHStorage(defaultGraphLoc, true); NodeAccess na = graph.getNodeAccess(); assertTrue(na.is3D()); @@ -1098,8 +1021,7 @@ public void testEnabledElevation() } @Test - public void testDontGrowOnUpdate() throws IOException - { + public void testDontGrowOnUpdate() throws IOException { graph = createGHStorage(defaultGraphLoc, true); NodeAccess na = graph.getNodeAccess(); assertTrue(na.is3D()); @@ -1126,8 +1048,7 @@ public void testDontGrowOnUpdate() throws IOException } @Test - public void testDetachEdge() - { + public void testDetachEdge() { graph = createGHStorage(); graph.edge(0, 1, 2, true); long flags = carEncoder.setProperties(10, true, false); @@ -1135,13 +1056,11 @@ public void testDetachEdge() graph.edge(1, 2, 2, true); EdgeIterator iter = graph.createEdgeExplorer().setBaseNode(0); - try - { + try { // currently not possible to detach without next, without introducing a new property inside EdgeIterable iter.detach(false); assertTrue(false); - } catch (Exception ex) - { + } catch (Exception ex) { } iter.next(); @@ -1178,10 +1097,8 @@ public void testDetachEdge() assertEquals(edgeState20.getFlags(), edgeState33.detach(true).getFlags()); } - static class TmpCarFlagEncoder extends CarFlagEncoder - { - public TmpCarFlagEncoder( int speedBits, double speedFactor, int maxTurnCosts ) - { + static class TmpCarFlagEncoder extends CarFlagEncoder { + public TmpCarFlagEncoder(int speedBits, double speedFactor, int maxTurnCosts) { super(speedBits, speedFactor, maxTurnCosts); } } diff --git a/core/src/test/java/com/graphhopper/storage/AbstractLockFactoryTester.java b/core/src/test/java/com/graphhopper/storage/AbstractLockFactoryTester.java index 59ad2825340..b8816282d37 100644 --- a/core/src/test/java/com/graphhopper/storage/AbstractLockFactoryTester.java +++ b/core/src/test/java/com/graphhopper/storage/AbstractLockFactoryTester.java @@ -19,41 +19,35 @@ import com.graphhopper.util.Constants; import com.graphhopper.util.Helper; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; import java.io.File; -import org.junit.After; - import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; -import org.junit.Before; -import org.junit.Test; - /** * @author Peter Karich */ -public abstract class AbstractLockFactoryTester -{ +public abstract class AbstractLockFactoryTester { protected final File lockDir = new File("./target/lockingtest/"); protected abstract LockFactory createLockFactory(); @Before - public void setUp() - { + public void setUp() { lockDir.mkdirs(); } @After - public void tearDown() - { + public void tearDown() { Helper.removeDir(lockDir); } @Test - public void testObtain() - { + public void testObtain() { LockFactory instance = createLockFactory(); instance.setLockDir(lockDir); Lock lock = instance.create("test", true); @@ -70,14 +64,12 @@ public void testObtain() // although it is locked do not allow release: // lock2.release(); // assertTrue(lock.isLocked()); - lock.release(); assertFalse(lock.isLocked()); } @Test - public void testForceDelete() - { + public void testForceDelete() { LockFactory instance = createLockFactory(); instance.setLockDir(lockDir); Lock lock = instance.create("testlock", true); diff --git a/core/src/test/java/com/graphhopper/storage/DataAccessTest.java b/core/src/test/java/com/graphhopper/storage/DataAccessTest.java index a9fae439f4f..1c79a66532b 100644 --- a/core/src/test/java/com/graphhopper/storage/DataAccessTest.java +++ b/core/src/test/java/com/graphhopper/storage/DataAccessTest.java @@ -19,32 +19,28 @@ import com.graphhopper.util.BitUtil; import com.graphhopper.util.Helper; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; import java.io.File; import java.nio.ByteOrder; -import org.junit.After; - import static org.junit.Assert.*; -import org.junit.Before; -import org.junit.Test; - /** * @author Peter Karich */ -public abstract class DataAccessTest -{ - protected ByteOrder defaultOrder = ByteOrder.LITTLE_ENDIAN; +public abstract class DataAccessTest { private final File folder = new File("./target/tmp/da"); + protected ByteOrder defaultOrder = ByteOrder.LITTLE_ENDIAN; protected String directory; protected String name = "dataacess"; - public abstract DataAccess createDataAccess( String location ); + public abstract DataAccess createDataAccess(String location); @Before - public void setUp() - { + public void setUp() { if (!Helper.removeDir(folder)) throw new IllegalStateException("cannot delete folder " + folder); @@ -53,14 +49,12 @@ public void setUp() } @After - public void tearDown() - { + public void tearDown() { Helper.removeDir(folder); } @Test - public void testLoadFlush() - { + public void testLoadFlush() { DataAccess da = createDataAccess(name); assertFalse(da.loadExisting()); da.create(300); @@ -78,12 +72,10 @@ public void testLoadFlush() da.close(); // cannot load data if already closed - try - { + try { da.loadExisting(); assertTrue(false); - } catch (Exception ex) - { + } catch (Exception ex) { assertEquals("already closed", ex.getMessage()); } @@ -94,23 +86,19 @@ public void testLoadFlush() } @Test - public void testExceptionIfNoEnsureCapacityWasCalled() - { + public void testExceptionIfNoEnsureCapacityWasCalled() { DataAccess da = createDataAccess(name); assertFalse(da.loadExisting()); // throw some undefined exception if no ensureCapacity was called - try - { + try { da.setInt(2 * 4, 321); assertTrue(false); - } catch (Exception ex) - { + } catch (Exception ex) { } } @Test - public void testLoadClose() - { + public void testLoadClose() { DataAccess da = createDataAccess(name); da.create(300); da.setInt(2 * 4, 321); @@ -124,8 +112,7 @@ public void testLoadClose() } @Test - public void testHeader() - { + public void testHeader() { DataAccess da = createDataAccess(name); da.create(300); da.setHeader(7 * 4, 123); @@ -145,8 +132,7 @@ public void testHeader() } @Test - public void testEnsureCapacity() - { + public void testEnsureCapacity() { DataAccess da = createDataAccess(name); da.create(128); da.setInt(31 * 4, 200); @@ -167,8 +153,7 @@ public void testEnsureCapacity() } @Test - public void testCopy() - { + public void testCopy() { DataAccess da1 = createDataAccess(name); da1.create(1001 * 4); da1.setHeader(4, 12); @@ -195,8 +180,7 @@ public void testCopy() } @Test - public void testSegments() - { + public void testSegments() { DataAccess da = createDataAccess(name); da.setSegmentSize(128); da.create(10); @@ -217,8 +201,7 @@ public void testSegments() } @Test - public void testTrimTo() - { + public void testTrimTo() { DataAccess da = createDataAccess(name); da.setSegmentSize(128); da.create(128 * 11); @@ -254,8 +237,7 @@ public void testTrimTo() } @Test - public void testBoundsCheck() - { + public void testBoundsCheck() { DataAccess da = createDataAccess(name); da.setSegmentSize(128); da.create(128 * 11); @@ -263,32 +245,27 @@ public void testBoundsCheck() // make smaller da.trimTo(128 * 1); - try - { + try { assertEquals(302, da.getInt(32 * 4)); assertTrue(false); - } catch (Exception ex) - { + } catch (Exception ex) { } da.close(); da = createDataAccess(name); da.create(128); da.setInt(31 * 4, 200); - try - { + try { // this should fail with an index out of bounds exception da.setInt(32 * 4, 220); assertFalse(true); - } catch (Exception ex) - { + } catch (Exception ex) { } da.close(); } @Test - public void testSegmentSize() - { + public void testSegmentSize() { DataAccess da = createDataAccess(name); da.setSegmentSize(20); assertEquals(128, da.getSegmentSize()); @@ -296,24 +273,20 @@ public void testSegmentSize() } @Test - public void testRenameNoFlush() - { + public void testRenameNoFlush() { DataAccess da = createDataAccess(name); da.create(100); da.setInt(17 * 4, 17); - try - { + try { da.rename(name + "wow"); assertTrue(false); - } catch (Exception ex) - { + } catch (Exception ex) { } da.close(); } @Test - public void testRenameFlush() - { + public void testRenameFlush() { DataAccess da = createDataAccess(name); da.create(100); da.setInt(17 * 4, 17); @@ -332,8 +305,7 @@ public void testRenameFlush() } @Test - public void testSet_GetBytes() - { + public void testSet_GetBytes() { DataAccess da = createDataAccess(name); da.create(300); assertEquals(128, da.getSegmentSize()); @@ -360,8 +332,7 @@ public void testSet_GetBytes() } @Test - public void testSet_Get_Short_Long() - { + public void testSet_Get_Short_Long() { DataAccess da = createDataAccess(name); da.create(300); da.setShort(6, (short) (Short.MAX_VALUE / 5)); @@ -371,8 +342,7 @@ public void testSet_Get_Short_Long() assertEquals(Short.MAX_VALUE / 7, da.getShort(8)); // currently RAMIntDA does not support arbitrary byte positions - if (!(da instanceof RAMIntDataAccess)) - { + if (!(da instanceof RAMIntDataAccess)) { da.setShort(7, (short) (Short.MAX_VALUE / 3)); assertEquals(Short.MAX_VALUE / 3, da.getShort(7)); // should be overwritten diff --git a/core/src/test/java/com/graphhopper/storage/EdgeTest.java b/core/src/test/java/com/graphhopper/storage/EdgeTest.java index 806bc5dd8fc..4e79999d231 100644 --- a/core/src/test/java/com/graphhopper/storage/EdgeTest.java +++ b/core/src/test/java/com/graphhopper/storage/EdgeTest.java @@ -25,11 +25,9 @@ /** * @author Peter Karich */ -public class EdgeTest -{ +public class EdgeTest { @Test - public void testCloneFull() - { + public void testCloneFull() { SPTEntry de = new SPTEntry(EdgeIterator.NO_EDGE, 1, 10); SPTEntry de2 = de.parent = new SPTEntry(EdgeIterator.NO_EDGE, -2, 20); SPTEntry de3 = de2.parent = new SPTEntry(EdgeIterator.NO_EDGE, 3, 30); @@ -39,8 +37,7 @@ public void testCloneFull() SPTEntry tmp2 = cloning; assertNotNull(tmp1); - while (tmp1 != null) - { + while (tmp1 != null) { assertFalse(tmp1 == tmp2); assertEquals(tmp1.edge, tmp2.edge); tmp1 = tmp1.parent; diff --git a/core/src/test/java/com/graphhopper/storage/GraphHopperStorageCHTest.java b/core/src/test/java/com/graphhopper/storage/GraphHopperStorageCHTest.java index a19f0c88b7d..87d665f63e1 100644 --- a/core/src/test/java/com/graphhopper/storage/GraphHopperStorageCHTest.java +++ b/core/src/test/java/com/graphhopper/storage/GraphHopperStorageCHTest.java @@ -17,54 +17,47 @@ */ package com.graphhopper.storage; -import com.graphhopper.routing.weighting.Weighting; -import com.graphhopper.routing.weighting.FastestWeighting; import com.graphhopper.routing.QueryGraph; import com.graphhopper.routing.ch.PrepareEncoder; import com.graphhopper.routing.util.*; +import com.graphhopper.routing.weighting.FastestWeighting; +import com.graphhopper.routing.weighting.Weighting; import com.graphhopper.storage.index.QueryResult; import com.graphhopper.util.*; import com.graphhopper.util.shapes.BBox; +import org.junit.Test; + import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; -import java.util.Collections; import java.util.List; import static org.junit.Assert.*; -import org.junit.Test; - /** * @author Peter Karich */ -public class GraphHopperStorageCHTest extends GraphHopperStorageTest -{ - protected CHGraph getGraph( GraphHopperStorage ghStorage ) - { +public class GraphHopperStorageCHTest extends GraphHopperStorageTest { + protected CHGraph getGraph(GraphHopperStorage ghStorage) { return ghStorage.getGraph(CHGraph.class); } @Override - public GraphHopperStorage newGHStorage( Directory dir, boolean is3D ) - { + public GraphHopperStorage newGHStorage(Directory dir, boolean is3D) { return new GraphHopperStorage(Arrays.asList(new FastestWeighting(carEncoder)), dir, encodingManager, is3D, new GraphExtension.NoOpExtension()); } @Test - public void testCannotBeLoadedWithNormalGraphHopperStorageClass() - { + public void testCannotBeLoadedWithNormalGraphHopperStorageClass() { graph = newGHStorage(new RAMDirectory(defaultGraphLoc, true), false).create(defaultSize); graph.flush(); graph.close(); graph = new GraphBuilder(encodingManager).setLocation(defaultGraphLoc).setMmap(false).setStore(true).create(); - try - { + try { graph.loadExisting(); assertTrue(false); - } catch (Exception ex) - { + } catch (Exception ex) { } graph = newGHStorage(new RAMDirectory(defaultGraphLoc, true), false); @@ -74,8 +67,7 @@ public void testCannotBeLoadedWithNormalGraphHopperStorageClass() } @Test - public void testPrios() - { + public void testPrios() { graph = createGHStorage(); CHGraph g = getGraph(graph); g.getNodeAccess().ensureNode(30); @@ -91,8 +83,7 @@ public void testPrios() } @Test - public void testEdgeFilter() - { + public void testEdgeFilter() { graph = createGHStorage(); CHGraph g = getGraph(graph); g.edge(0, 1, 10, true); @@ -122,8 +113,7 @@ public void testEdgeFilter() } @Test - public void testDisconnectEdge() - { + public void testDisconnectEdge() { graph = createGHStorage(); CHGraphImpl lg = (CHGraphImpl) getGraph(graph); @@ -174,8 +164,7 @@ public void testDisconnectEdge() } @Test - public void testGetWeight() - { + public void testGetWeight() { graph = createGHStorage(); CHGraphImpl g = (CHGraphImpl) getGraph(graph); assertFalse(g.edge(0, 1).isShortcut()); @@ -209,8 +198,7 @@ public void testGetWeight() } @Test - public void testGetWeightIfAdvancedEncoder() - { + public void testGetWeightIfAdvancedEncoder() { FlagEncoder customEncoder = new Bike2WeightFlagEncoder(); EncodingManager em = new EncodingManager(customEncoder); FastestWeighting weighting = new FastestWeighting(customEncoder); @@ -237,8 +225,7 @@ public void testGetWeightIfAdvancedEncoder() } @Test - public void testQueryGraph() - { + public void testQueryGraph() { graph = createGHStorage(); CHGraph chGraph = getGraph(graph); NodeAccess na = chGraph.getNodeAccess(); @@ -271,8 +258,7 @@ public void testQueryGraph() assertEquals(GHUtility.asSet(1, fromRes.getClosestNode()), GHUtility.getNeighbors(explorer.setBaseNode(toRes.getClosestNode()))); } - QueryResult createQR( double lat, double lon, int wayIndex, EdgeIteratorState edge ) - { + QueryResult createQR(double lat, double lon, int wayIndex, EdgeIteratorState edge) { QueryResult res = new QueryResult(lat, lon); res.setClosestEdge(edge); res.setWayIndex(wayIndex); @@ -283,8 +269,7 @@ QueryResult createQR( double lat, double lon, int wayIndex, EdgeIteratorState ed @Test @Override - public void testSave_and_Freeze() throws IOException - { + public void testSave_and_Freeze() throws IOException { // belongs to each other super.testSave_and_Freeze(); graph.close(); @@ -339,8 +324,7 @@ public void testSave_and_Freeze() throws IOException } @Test - public void testSimpleShortcutCreationAndTraversal() - { + public void testSimpleShortcutCreationAndTraversal() { graph = createGHStorage(); graph.edge(1, 3, 10, true); graph.edge(3, 4, 10, true); @@ -356,8 +340,7 @@ public void testSimpleShortcutCreationAndTraversal() } @Test - public void testShortcutCreationAndAccessForManyVehicles() - { + public void testShortcutCreationAndAccessForManyVehicles() { FlagEncoder tmpCar = new CarFlagEncoder(); FlagEncoder tmpBike = new Bike2WeightFlagEncoder(); EncodingManager em = new EncodingManager(tmpCar, tmpBike); @@ -384,12 +367,10 @@ public void testShortcutCreationAndAccessForManyVehicles() assertFalse(carCHGraph.getEdgeIteratorState(carSC02.getEdge(), 2).isBackward(tmpCar)); // throw exception for wrong encoder - try - { + try { assertFalse(carCHGraph.getEdgeIteratorState(carSC02.getEdge(), 2).isForward(tmpBike)); assertTrue(false); - } catch (AssertionError ex) - { + } catch (AssertionError ex) { } // assert bike CH graph @@ -397,12 +378,10 @@ public void testShortcutCreationAndAccessForManyVehicles() assertTrue(bikeCHGraph.getEdgeIteratorState(bikeSC02.getEdge(), 2).isBackward(tmpBike)); // throw exception for wrong encoder - try - { + try { assertFalse(bikeCHGraph.getEdgeIteratorState(bikeSC02.getEdge(), 2).isBackward(tmpCar)); assertTrue(false); - } catch (AssertionError ex) - { + } catch (AssertionError ex) { } } } diff --git a/core/src/test/java/com/graphhopper/storage/GraphHopperStorageTest.java b/core/src/test/java/com/graphhopper/storage/GraphHopperStorageTest.java index 21c74e41bfb..ea33fd0d767 100644 --- a/core/src/test/java/com/graphhopper/storage/GraphHopperStorageTest.java +++ b/core/src/test/java/com/graphhopper/storage/GraphHopperStorageTest.java @@ -19,21 +19,18 @@ import com.graphhopper.util.*; import com.graphhopper.util.shapes.BBox; +import org.junit.Test; import java.io.IOException; import static org.junit.Assert.*; -import org.junit.Test; - /** * @author Peter Karich */ -public class GraphHopperStorageTest extends AbstractGraphStorageTester -{ +public class GraphHopperStorageTest extends AbstractGraphStorageTester { @Override - public GraphHopperStorage createGHStorage( String location, boolean enabled3D ) - { + public GraphHopperStorage createGHStorage(String location, boolean enabled3D) { // reduce segment size in order to test the case where multiple segments come into the game GraphHopperStorage gs = newGHStorage(new RAMDirectory(location), enabled3D); gs.setSegmentSize(defaultSize / 2); @@ -41,34 +38,27 @@ public GraphHopperStorage createGHStorage( String location, boolean enabled3D ) return gs; } - protected GraphHopperStorage newGHStorage( Directory dir, boolean enabled3D ) - { + protected GraphHopperStorage newGHStorage(Directory dir, boolean enabled3D) { return new GraphHopperStorage(dir, encodingManager, enabled3D, new GraphExtension.NoOpExtension()); } @Test - public void testNoCreateCalled() throws IOException - { + public void testNoCreateCalled() throws IOException { GraphHopperStorage gs = new GraphBuilder(encodingManager).build(); - try - { + try { ((BaseGraph) gs.getGraph(Graph.class)).ensureNodeIndex(123); assertFalse("AssertionError should be raised", true); - } catch (AssertionError err) - { + } catch (AssertionError err) { assertTrue(true); - } catch (Exception ex) - { + } catch (Exception ex) { assertFalse("AssertionError should be raised, but was " + ex.toString(), true); - } finally - { + } finally { gs.close(); } } @Test - public void testSave_and_fileFormat() throws IOException - { + public void testSave_and_fileFormat() throws IOException { graph = newGHStorage(new RAMDirectory(defaultGraphLoc, true), true).create(defaultSize); NodeAccess na = graph.getNodeAccess(); assertTrue(na.is3D()); @@ -104,8 +94,7 @@ public void testSave_and_fileFormat() throws IOException } @Test - public void testSave_and_Freeze() throws IOException - { + public void testSave_and_Freeze() throws IOException { graph = newGHStorage(new RAMDirectory(defaultGraphLoc, true), true).create(defaultSize); graph.edge(1, 0); graph.freeze(); @@ -119,8 +108,7 @@ public void testSave_and_Freeze() throws IOException assertTrue(graph.isFrozen()); } - protected void checkGraph( Graph g ) - { + protected void checkGraph(Graph g) { NodeAccess na = g.getNodeAccess(); assertTrue(na.is3D()); assertTrue(g.getBounds().isValid()); @@ -160,8 +148,7 @@ protected void checkGraph( Graph g ) } @Test - public void internalDisconnect() - { + public void internalDisconnect() { GraphHopperStorage storage = createGHStorage(); BaseGraph graph = (BaseGraph) storage.getGraph(Graph.class); EdgeIteratorState iter0 = graph.edge(0, 1, 10, true); @@ -190,8 +177,7 @@ public void internalDisconnect() } @Test - public void testEnsureSize() - { + public void testEnsureSize() { Directory dir = new RAMDirectory(); graph = newGHStorage(dir, false).create(defaultSize); int testIndex = dir.find("edges").getSegmentSize() * 3; @@ -202,8 +188,7 @@ public void testEnsureSize() } @Test - public void testBigDataEdge() - { + public void testBigDataEdge() { Directory dir = new RAMDirectory(); GraphHopperStorage graph = new GraphHopperStorage(dir, encodingManager, false, new GraphExtension.NoOpExtension()); graph.create(defaultSize); @@ -213,107 +198,88 @@ public void testBigDataEdge() } @Test - public void testDoThrowExceptionIfDimDoesNotMatch() - { + public void testDoThrowExceptionIfDimDoesNotMatch() { graph = newGHStorage(new RAMDirectory(defaultGraphLoc, true), false); graph.create(1000); graph.flush(); graph.close(); graph = newGHStorage(new RAMDirectory(defaultGraphLoc, true), true); - try - { + try { graph.loadExisting(); assertTrue(false); - } catch (Exception ex) - { + } catch (Exception ex) { } } @Test - public void testIdentical() - { + public void testIdentical() { GraphHopperStorage store = new GraphHopperStorage(new RAMDirectory(), encodingManager, true, new GraphExtension.NoOpExtension()); assertEquals(store.getNodes(), store.getGraph(Graph.class).getNodes()); assertEquals(store.getAllEdges().getMaxId(), store.getGraph(Graph.class).getAllEdges().getMaxId()); } - public void testAdditionalEdgeField() - { - GraphExtension extStorage = new GraphExtension() - { + public void testAdditionalEdgeField() { + GraphExtension extStorage = new GraphExtension() { @Override - public boolean isRequireNodeField() - { + public boolean isRequireNodeField() { return false; } @Override - public boolean isRequireEdgeField() - { + public boolean isRequireEdgeField() { return true; } @Override - public int getDefaultNodeFieldValue() - { + public int getDefaultNodeFieldValue() { throw new UnsupportedOperationException("Not supported."); } @Override - public int getDefaultEdgeFieldValue() - { + public int getDefaultEdgeFieldValue() { return 2; } @Override - public void init( Graph graph, Directory dir ) - { + public void init(Graph graph, Directory dir) { } @Override - public void setSegmentSize( int bytes ) - { + public void setSegmentSize(int bytes) { } @Override - public GraphExtension copyTo( GraphExtension extStorage ) - { + public GraphExtension copyTo(GraphExtension extStorage) { return this; } @Override - public boolean loadExisting() - { + public boolean loadExisting() { return true; } @Override - public GraphExtension create( long byteCount ) - { + public GraphExtension create(long byteCount) { return this; } @Override - public void flush() - { + public void flush() { } @Override - public void close() - { + public void close() { } @Override - public boolean isClosed() - { + public boolean isClosed() { return false; } @Override - public long getCapacity() - { + public long getCapacity() { return 0; } }; diff --git a/core/src/test/java/com/graphhopper/storage/GraphHopperStorageWithTurnCostsTest.java b/core/src/test/java/com/graphhopper/storage/GraphHopperStorageWithTurnCostsTest.java index 0caf0589680..2ab54ed0230 100644 --- a/core/src/test/java/com/graphhopper/storage/GraphHopperStorageWithTurnCostsTest.java +++ b/core/src/test/java/com/graphhopper/storage/GraphHopperStorageWithTurnCostsTest.java @@ -17,34 +17,31 @@ */ package com.graphhopper.storage; -import java.io.IOException; -import java.util.Random; - import com.graphhopper.util.EdgeIteratorState; import com.graphhopper.util.Helper; import org.junit.Test; +import java.io.IOException; +import java.util.Random; + import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; /** * @author Karl Hübner */ -public class GraphHopperStorageWithTurnCostsTest extends GraphHopperStorageTest -{ +public class GraphHopperStorageWithTurnCostsTest extends GraphHopperStorageTest { private TurnCostExtension turnCostStorage; @Override - protected GraphHopperStorage newGHStorage( Directory dir, boolean is3D ) - { + protected GraphHopperStorage newGHStorage(Directory dir, boolean is3D) { turnCostStorage = new TurnCostExtension(); return new GraphHopperStorage(dir, encodingManager, is3D, turnCostStorage); } @Override @Test - public void testSave_and_fileFormat() throws IOException - { + public void testSave_and_fileFormat() throws IOException { graph = newGHStorage(new RAMDirectory(defaultGraphLoc, true), true).create(defaultSize); NodeAccess na = graph.getNodeAccess(); assertTrue(na.is3D()); @@ -90,8 +87,7 @@ public void testSave_and_fileFormat() throws IOException } @Test - public void testEnsureCapacity() throws IOException - { + public void testEnsureCapacity() throws IOException { graph = newGHStorage(new MMapDirectory(defaultGraphLoc), false); graph.setSegmentSize(128); graph.create(100); // 100 is the minimum size @@ -102,8 +98,7 @@ public void testEnsureCapacity() throws IOException Random r = new Random(); NodeAccess na = graph.getNodeAccess(); - for (int i = 0; i < 100; i++) - { + for (int i = 0; i < 100; i++) { double randomLat = 90 * r.nextDouble(); double randomLon = 180 * r.nextDouble(); @@ -111,18 +106,15 @@ public void testEnsureCapacity() throws IOException } // Make node 50 the 'center' node - for (int nodeId = 51; nodeId < 100; nodeId++) - { + for (int nodeId = 51; nodeId < 100; nodeId++) { graph.edge(50, nodeId, r.nextDouble(), true); } - for (int nodeId = 0; nodeId < 50; nodeId++) - { + for (int nodeId = 0; nodeId < 50; nodeId++) { graph.edge(nodeId, 50, r.nextDouble(), true); } // add 100 turn cost entries around node 50 - for (int edgeId = 0; edgeId < 50; edgeId++) - { + for (int edgeId = 0; edgeId < 50; edgeId++) { turnCostStorage.addTurnInfo(edgeId, 50, edgeId + 50, 1337); turnCostStorage.addTurnInfo(edgeId + 50, 50, edgeId, 1337); } diff --git a/core/src/test/java/com/graphhopper/storage/GraphStorageViaMMapTest.java b/core/src/test/java/com/graphhopper/storage/GraphStorageViaMMapTest.java index b85f39495f6..96be23ed265 100644 --- a/core/src/test/java/com/graphhopper/storage/GraphStorageViaMMapTest.java +++ b/core/src/test/java/com/graphhopper/storage/GraphStorageViaMMapTest.java @@ -20,11 +20,9 @@ /** * @author Peter Karich */ -public class GraphStorageViaMMapTest extends AbstractGraphStorageTester -{ +public class GraphStorageViaMMapTest extends AbstractGraphStorageTester { @Override - public GraphHopperStorage createGHStorage( String location, boolean is3D ) - { + public GraphHopperStorage createGHStorage(String location, boolean is3D) { GraphHopperStorage gs = new GraphBuilder(encodingManager).set3D(is3D).setLocation(location).setMmap(true).build(); gs.setSegmentSize(defaultSize / 2); gs.create(defaultSize); diff --git a/core/src/test/java/com/graphhopper/storage/MMapDataAccessTest.java b/core/src/test/java/com/graphhopper/storage/MMapDataAccessTest.java index d0c21b5725d..442f573d964 100644 --- a/core/src/test/java/com/graphhopper/storage/MMapDataAccessTest.java +++ b/core/src/test/java/com/graphhopper/storage/MMapDataAccessTest.java @@ -17,24 +17,21 @@ */ package com.graphhopper.storage; -import static org.junit.Assert.*; - import org.junit.Test; +import static org.junit.Assert.*; + /** * @author Peter Karich */ -public class MMapDataAccessTest extends DataAccessTest -{ +public class MMapDataAccessTest extends DataAccessTest { @Override - public DataAccess createDataAccess( String name ) - { + public DataAccess createDataAccess(String name) { return new MMapDataAccess(name, directory, defaultOrder, true).setSegmentSize(128); } @Test - public void textMixRAM2MMAP() - { + public void textMixRAM2MMAP() { DataAccess da = new RAMDataAccess(name, directory, true, defaultOrder); assertFalse(da.loadExisting()); da.create(100); @@ -48,8 +45,7 @@ public void textMixRAM2MMAP() } @Test - public void textMixMMAP2RAM() - { + public void textMixMMAP2RAM() { DataAccess da = createDataAccess(name); assertFalse(da.loadExisting()); da.create(100); diff --git a/core/src/test/java/com/graphhopper/storage/MMapDirectoryTest.java b/core/src/test/java/com/graphhopper/storage/MMapDirectoryTest.java index fb7eeecf6ca..e6751ef9397 100644 --- a/core/src/test/java/com/graphhopper/storage/MMapDirectoryTest.java +++ b/core/src/test/java/com/graphhopper/storage/MMapDirectoryTest.java @@ -20,11 +20,9 @@ /** * @author Peter Karich */ -public class MMapDirectoryTest extends AbstractDirectoryTester -{ +public class MMapDirectoryTest extends AbstractDirectoryTester { @Override - Directory createDir() - { + Directory createDir() { return new MMapDirectory(location).create(); } } diff --git a/core/src/test/java/com/graphhopper/storage/NativeFSLockFactoryTest.java b/core/src/test/java/com/graphhopper/storage/NativeFSLockFactoryTest.java index 82d09736493..9e608b24099 100644 --- a/core/src/test/java/com/graphhopper/storage/NativeFSLockFactoryTest.java +++ b/core/src/test/java/com/graphhopper/storage/NativeFSLockFactoryTest.java @@ -17,26 +17,23 @@ */ package com.graphhopper.storage; -import java.nio.channels.OverlappingFileLockException; - import org.junit.Test; +import java.nio.channels.OverlappingFileLockException; + import static org.junit.Assert.*; /** * @author Peter Karich */ -public class NativeFSLockFactoryTest extends AbstractLockFactoryTester -{ +public class NativeFSLockFactoryTest extends AbstractLockFactoryTester { @Override - protected LockFactory createLockFactory() - { + protected LockFactory createLockFactory() { return new NativeFSLockFactory(lockDir); } @Test - public void testMultiReadObtain() - { + public void testMultiReadObtain() { LockFactory instance = createLockFactory(); instance.setLockDir(lockDir); Lock writeLock1 = instance.create("test", true); diff --git a/core/src/test/java/com/graphhopper/storage/RAMDataAccessTest.java b/core/src/test/java/com/graphhopper/storage/RAMDataAccessTest.java index 2e1581256bb..d23df73fb4f 100644 --- a/core/src/test/java/com/graphhopper/storage/RAMDataAccessTest.java +++ b/core/src/test/java/com/graphhopper/storage/RAMDataAccessTest.java @@ -20,11 +20,9 @@ /** * @author Peter Karich */ -public class RAMDataAccessTest extends DataAccessTest -{ +public class RAMDataAccessTest extends DataAccessTest { @Override - public DataAccess createDataAccess( String name ) - { + public DataAccess createDataAccess(String name) { return new RAMDataAccess(name, directory, true, defaultOrder).setSegmentSize(128); } } diff --git a/core/src/test/java/com/graphhopper/storage/RAMDirectoryTest.java b/core/src/test/java/com/graphhopper/storage/RAMDirectoryTest.java index 710b507d952..3e277ab754a 100644 --- a/core/src/test/java/com/graphhopper/storage/RAMDirectoryTest.java +++ b/core/src/test/java/com/graphhopper/storage/RAMDirectoryTest.java @@ -20,11 +20,9 @@ /** * @author Peter Karich */ -public class RAMDirectoryTest extends AbstractDirectoryTester -{ +public class RAMDirectoryTest extends AbstractDirectoryTester { @Override - Directory createDir() - { + Directory createDir() { return new RAMDirectory(location, true).create(); } } diff --git a/core/src/test/java/com/graphhopper/storage/RAMIntDataAccessTest.java b/core/src/test/java/com/graphhopper/storage/RAMIntDataAccessTest.java index aa8c6d81dbc..90f81e0456f 100644 --- a/core/src/test/java/com/graphhopper/storage/RAMIntDataAccessTest.java +++ b/core/src/test/java/com/graphhopper/storage/RAMIntDataAccessTest.java @@ -20,17 +20,14 @@ /** * @author Peter Karich */ -public class RAMIntDataAccessTest extends DataAccessTest -{ +public class RAMIntDataAccessTest extends DataAccessTest { @Override - public DataAccess createDataAccess( String name ) - { + public DataAccess createDataAccess(String name) { return new RAMIntDataAccess(name, directory, true, defaultOrder).setSegmentSize(128); } @Override - public void testSet_GetBytes() - { + public void testSet_GetBytes() { // should we implement this? } } diff --git a/core/src/test/java/com/graphhopper/storage/SimpleFSLockFactoryTest.java b/core/src/test/java/com/graphhopper/storage/SimpleFSLockFactoryTest.java index fb7fa00dbe2..2e620bb847f 100644 --- a/core/src/test/java/com/graphhopper/storage/SimpleFSLockFactoryTest.java +++ b/core/src/test/java/com/graphhopper/storage/SimpleFSLockFactoryTest.java @@ -20,11 +20,9 @@ /** * @author Peter Karich */ -public class SimpleFSLockFactoryTest extends AbstractLockFactoryTester -{ +public class SimpleFSLockFactoryTest extends AbstractLockFactoryTester { @Override - protected LockFactory createLockFactory() - { + protected LockFactory createLockFactory() { return new SimpleFSLockFactory(lockDir); } } diff --git a/core/src/test/java/com/graphhopper/storage/StorablePropertiesTest.java b/core/src/test/java/com/graphhopper/storage/StorablePropertiesTest.java index a15a904fe9d..0874232e4df 100644 --- a/core/src/test/java/com/graphhopper/storage/StorablePropertiesTest.java +++ b/core/src/test/java/com/graphhopper/storage/StorablePropertiesTest.java @@ -18,26 +18,22 @@ package com.graphhopper.storage; import com.graphhopper.util.Helper; +import org.junit.Test; import java.io.File; -import org.junit.Test; - import static org.junit.Assert.*; /** * @author Peter Karich */ -public class StorablePropertiesTest -{ - Directory createDir( String location, boolean store ) - { +public class StorablePropertiesTest { + Directory createDir(String location, boolean store) { return new RAMDirectory(location, store).create(); } @Test - public void testLoad() - { + public void testLoad() { StorableProperties instance = new StorableProperties(createDir("", false)); // an in-memory storage does not load anything assertFalse(instance.loadExisting()); @@ -48,8 +44,7 @@ public void testLoad() } @Test - public void testVersionCheck() - { + public void testVersionCheck() { StorableProperties instance = new StorableProperties(createDir("", false)); instance.putCurrentVersions(); assertTrue(instance.checkVersions(true)); @@ -57,19 +52,16 @@ public void testVersionCheck() instance.put("nodes.version", 0); assertFalse(instance.checkVersions(true)); - try - { + try { instance.checkVersions(false); assertTrue(false); - } catch (Exception ex) - { + } catch (Exception ex) { } instance.close(); } @Test - public void testStore() - { + public void testStore() { String dir = "./target/test"; Helper.removeDir(new File(dir)); StorableProperties instance = new StorableProperties(createDir(dir, true)); diff --git a/core/src/test/java/com/graphhopper/storage/SynchedDAWrapperTest.java b/core/src/test/java/com/graphhopper/storage/SynchedDAWrapperTest.java index bbd739bcabc..93e07a58bf0 100644 --- a/core/src/test/java/com/graphhopper/storage/SynchedDAWrapperTest.java +++ b/core/src/test/java/com/graphhopper/storage/SynchedDAWrapperTest.java @@ -20,11 +20,9 @@ /** * @author Peter Karich */ -public class SynchedDAWrapperTest extends DataAccessTest -{ +public class SynchedDAWrapperTest extends DataAccessTest { @Override - public DataAccess createDataAccess( String name ) - { + public DataAccess createDataAccess(String name) { return new SynchedDAWrapper(new RAMDataAccess(name, directory, true, defaultOrder)).setSegmentSize(128); } } diff --git a/core/src/test/java/com/graphhopper/storage/UnsafeDataAccessTest.java b/core/src/test/java/com/graphhopper/storage/UnsafeDataAccessTest.java index a7689c6e2c2..21b253bfd08 100644 --- a/core/src/test/java/com/graphhopper/storage/UnsafeDataAccessTest.java +++ b/core/src/test/java/com/graphhopper/storage/UnsafeDataAccessTest.java @@ -18,39 +18,33 @@ package com.graphhopper.storage; import com.graphhopper.util.BitUtil; +import org.junit.Test; import java.nio.ByteOrder; -import org.junit.Test; - -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; /** * @author Peter Karich */ -public class UnsafeDataAccessTest extends DataAccessTest -{ +public class UnsafeDataAccessTest extends DataAccessTest { @Override - public DataAccess createDataAccess( String name ) - { + public DataAccess createDataAccess(String name) { return new UnsafeDataAccess(name, directory, defaultOrder).setSegmentSize(128); } @Override - public void testExceptionIfNoEnsureCapacityWasCalled() - { + public void testExceptionIfNoEnsureCapacityWasCalled() { // SKIP as unsafe failes with SIGSEGV and not with an exception! } @Override - public void testBoundsCheck() - { + public void testBoundsCheck() { // SKIP as unsafe has no bounds checks } @Test - public void testNativeOrder() - { + public void testNativeOrder() { BitUtil bitUtil = BitUtil.get(ByteOrder.nativeOrder()); long address = UnsafeDataAccess.UNSAFE.allocateMemory(8); long val = 123123123123L * 123L; @@ -58,17 +52,13 @@ public void testNativeOrder() byte[] bytes = new byte[8]; bitUtil.fromLong(bytes, val); - if (ByteOrder.nativeOrder().equals(ByteOrder.LITTLE_ENDIAN)) - { - for (int i = 7; i >= 0; i--) - { + if (ByteOrder.nativeOrder().equals(ByteOrder.LITTLE_ENDIAN)) { + for (int i = 7; i >= 0; i--) { UnsafeDataAccess.UNSAFE.putByte(address + i, bytes[i]); } - } else - { + } else { // not tested: - for (int i = 0; i < 8; i++) - { + for (int i = 0; i < 8; i++) { UnsafeDataAccess.UNSAFE.putByte(address + i, bytes[i]); } } diff --git a/core/src/test/java/com/graphhopper/storage/VLongStorageTest.java b/core/src/test/java/com/graphhopper/storage/VLongStorageTest.java index 5eaa6ada8f0..e3a914828a2 100644 --- a/core/src/test/java/com/graphhopper/storage/VLongStorageTest.java +++ b/core/src/test/java/com/graphhopper/storage/VLongStorageTest.java @@ -19,16 +19,14 @@ import org.junit.Test; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; /** * @author Peter Karich */ -public class VLongStorageTest -{ +public class VLongStorageTest { @Test - public void testWrite() - { + public void testWrite() { VLongStorage store = new VLongStorage(); store.seek(0); store.writeVLong(1); @@ -44,8 +42,7 @@ public void testWrite() } @Test - public void testWriteWithTrim() - { + public void testWriteWithTrim() { VLongStorage store = new VLongStorage(); store.seek(0); store.writeVLong(1); diff --git a/core/src/test/java/com/graphhopper/storage/index/AbstractLocationIndexTester.java b/core/src/test/java/com/graphhopper/storage/index/AbstractLocationIndexTester.java index 2482ab4f109..976d1bbd85f 100644 --- a/core/src/test/java/com/graphhopper/storage/index/AbstractLocationIndexTester.java +++ b/core/src/test/java/com/graphhopper/storage/index/AbstractLocationIndexTester.java @@ -23,35 +23,31 @@ import com.graphhopper.util.DistanceCalcEarth; import com.graphhopper.util.EdgeIterator; import com.graphhopper.util.Helper; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; import java.io.Closeable; import java.io.File; import java.util.Random; -import org.junit.After; - -import static org.junit.Assert.*; - -import org.junit.Before; -import org.junit.Test; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; /** * @author Peter Karich */ -public abstract class AbstractLocationIndexTester -{ +public abstract class AbstractLocationIndexTester { String location = "./target/tmp/"; LocationIndex idx; - public abstract LocationIndex createIndex( Graph g, int resolution ); + public abstract LocationIndex createIndex(Graph g, int resolution); - GraphHopperStorage createGHStorage( EncodingManager encodingManager ) - { + GraphHopperStorage createGHStorage(EncodingManager encodingManager) { return AbstractLocationIndexTester.this.createGHStorage(new RAMDirectory(), encodingManager, false); } - GraphHopperStorage createGHStorage( Directory dir, EncodingManager encodingManager, boolean is3D ) - { + GraphHopperStorage createGHStorage(Directory dir, EncodingManager encodingManager, boolean is3D) { return new GraphHopperStorage(dir, encodingManager, is3D, new GraphExtension.NoOpExtension()).create(100); } @@ -59,28 +55,24 @@ protected int findID(LocationIndex index, double lat, double lon) { return index.findClosest(lat, lon, EdgeFilter.ALL_EDGES).getClosestNode(); } - public boolean hasEdgeSupport() - { + public boolean hasEdgeSupport() { return false; } @Before - public void setUp() - { + public void setUp() { Helper.removeDir(new File(location)); } @After - public void tearDown() - { + public void tearDown() { if (idx != null) idx.close(); Helper.removeDir(new File(location)); } @Test - public void testSimpleGraph() - { + public void testSimpleGraph() { Graph g = AbstractLocationIndexTester.this.createGHStorage(new EncodingManager("car")); initSimpleGraph(g); @@ -89,19 +81,16 @@ public void testSimpleGraph() assertEquals(3, findID(idx, 1.5, 2)); assertEquals(0, findID(idx, -1, -1)); - if (hasEdgeSupport()) - // now get the edge 1-4 and not node 6 + if (hasEdgeSupport()) // now get the edge 1-4 and not node 6 { assertEquals(4, findID(idx, 4, 0)); - } else - { + } else { assertEquals(6, findID(idx, 4, 0)); } Helper.close((Closeable) g); } - public void initSimpleGraph( Graph g ) - { + public void initSimpleGraph(Graph g) { // 6 | 4 // 5 | // | 6 @@ -133,8 +122,7 @@ public void initSimpleGraph( Graph g ) } @Test - public void testSimpleGraph2() - { + public void testSimpleGraph2() { Graph g = AbstractLocationIndexTester.this.createGHStorage(new EncodingManager("car")); initSimpleGraph(g); @@ -143,12 +131,10 @@ public void testSimpleGraph2() assertEquals(3, findID(idx, 1.5, 2)); assertEquals(0, findID(idx, -1, -1)); assertEquals(6, findID(idx, 4.5, -0.5)); - if (hasEdgeSupport()) - { + if (hasEdgeSupport()) { assertEquals(4, findID(idx, 4, 1)); assertEquals(4, findID(idx, 4, 0)); - } else - { + } else { assertEquals(6, findID(idx, 4, 1)); assertEquals(6, findID(idx, 4, 0)); } @@ -158,8 +144,7 @@ public void testSimpleGraph2() } @Test - public void testGrid() - { + public void testGrid() { Graph g = createSampleGraph(new EncodingManager("car")); int locs = g.getNodes(); @@ -168,8 +153,7 @@ public void testGrid() // e.g. for 16 we get "expected 6 but was 9" i.e 6 was overwritten by node j9 which is a bit closer to the grid center // go through every point of the graph if all points are reachable NodeAccess na = g.getNodeAccess(); - for (int i = 0; i < locs; i++) - { + for (int i = 0; i < locs; i++) { double lat = na.getLatitude(i); double lon = na.getLongitude(i); assertEquals("nodeId:" + i + " " + (float) lat + "," + (float) lon, i, findID(idx, lat, lon)); @@ -184,8 +168,7 @@ public void testGrid() fullIndex = new Location2IDFullIndex(g); DistanceCalc dist = new DistanceCalcEarth(); - for (int i = 0; i < 100; i++) - { + for (int i = 0; i < 100; i++) { double lat = rand.nextDouble() * 5; double lon = rand.nextDouble() * 5; int fullId = findID(fullIndex, lat, lon); @@ -197,8 +180,7 @@ public void testGrid() double newLon = na.getLongitude(newId); float newDist = (float) dist.calcDist(lat, lon, newLat, newLon); - if (testGridIgnore(i)) - { + if (testGridIgnore(i)) { continue; } @@ -212,14 +194,12 @@ public void testGrid() } // our simple index has only one node per tile => problems if multiple subnetworks - boolean testGridIgnore( int i ) - { + boolean testGridIgnore(int i) { return false; } @Test - public void testSinglePoints120() - { + public void testSinglePoints120() { Graph g = createSampleGraph(new EncodingManager("car")); idx = createIndex(g, -1); @@ -234,19 +214,16 @@ public void testSinglePoints120() } @Test - public void testSinglePoints32() - { + public void testSinglePoints32() { Graph g = createSampleGraph(new EncodingManager("car")); idx = createIndex(g, -1); // 10 or 6 assertEquals(10, findID(idx, 3.649, 1.375)); assertEquals(10, findID(idx, 3.8465748, 0.021762699)); - if (hasEdgeSupport()) - { + if (hasEdgeSupport()) { assertEquals(4, findID(idx, 2.485, 1.373)); - } else - { + } else { assertEquals(6, findID(idx, 2.485, 1.373)); } assertEquals(0, findID(idx, 0.64628404, 0.53006625)); @@ -254,23 +231,20 @@ public void testSinglePoints32() } @Test - public void testNoErrorOnEdgeCase_lastIndex() - { + public void testNoErrorOnEdgeCase_lastIndex() { final EncodingManager encodingManager = new EncodingManager("car"); int locs = 10000; Graph g = AbstractLocationIndexTester.this.createGHStorage(new MMapDirectory(location), encodingManager, false); NodeAccess na = g.getNodeAccess(); Random rand = new Random(12); - for (int i = 0; i < locs; i++) - { + for (int i = 0; i < locs; i++) { na.setNode(i, (float) rand.nextDouble() * 10 + 10, (float) rand.nextDouble() * 10 + 10); } idx = createIndex(g, 200); Helper.close((Closeable) g); } - public Graph createSampleGraph( EncodingManager encodingManager ) - { + public Graph createSampleGraph(EncodingManager encodingManager) { Graph graph = AbstractLocationIndexTester.this.createGHStorage(encodingManager); // length does not matter here but lat,lon and outgoing edges do! @@ -350,8 +324,7 @@ public Graph createSampleGraph( EncodingManager encodingManager ) } @Test - public void testDifferentVehicles() - { + public void testDifferentVehicles() { final EncodingManager encodingManager = new EncodingManager("car,foot"); Graph g = AbstractLocationIndexTester.this.createGHStorage(encodingManager); initSimpleGraph(g); @@ -361,8 +334,7 @@ public void testDifferentVehicles() // now make all edges from node 1 accessible for CAR only EdgeIterator iter = g.createEdgeExplorer().setBaseNode(1); CarFlagEncoder carEncoder = (CarFlagEncoder) encodingManager.getEncoder("car"); - while (iter.next()) - { + while (iter.next()) { iter.setFlags(carEncoder.setProperties(50, true, true)); } idx.close(); diff --git a/core/src/test/java/com/graphhopper/storage/index/BresenhamLineTest.java b/core/src/test/java/com/graphhopper/storage/index/BresenhamLineTest.java index 6621d1d2fc4..b0224ef3c07 100644 --- a/core/src/test/java/com/graphhopper/storage/index/BresenhamLineTest.java +++ b/core/src/test/java/com/graphhopper/storage/index/BresenhamLineTest.java @@ -21,56 +21,47 @@ import com.graphhopper.geohash.SpatialKeyAlgo; import com.graphhopper.util.Helper; import com.graphhopper.util.PointList; +import org.junit.Before; +import org.junit.Test; import java.util.ArrayList; import java.util.Arrays; -import org.junit.Test; - -import static org.junit.Assert.*; - -import org.junit.Before; +import static org.junit.Assert.assertEquals; /** * @author Peter Karich */ -public class BresenhamLineTest -{ +public class BresenhamLineTest { final PointList points = new PointList(10, false); - PointEmitter emitter = new PointEmitter() - { + PointEmitter emitter = new PointEmitter() { @Override - public void set( double lat, double lon ) - { + public void set(double lat, double lon) { points.add(lat, lon); } }; @Before - public void setUp() - { + public void setUp() { points.clear(); } @Test - public void testBresenhamLineLeftDown() - { + public void testBresenhamLineLeftDown() { BresenhamLine.calcPoints(5, 2, 0, 0, emitter); // 5,2, 4,2, 3,2, 3,1, 2,1, 1,1, 0,0 assertEquals(Helper.createPointList(5, 2, 4, 2, 3, 1, 2, 1, 1, 0, 0, 0), points); } @Test - public void testBresenhamLineRightDown() - { + public void testBresenhamLineRightDown() { BresenhamLine.calcPoints(3, 1, 0, 3, emitter); // 3,1, 2,1, 1,1, 1,2, 0,2, 0,3 assertEquals(Helper.createPointList(3, 1, 2, 2, 1, 2, 0, 3), points); } @Test - public void testBresenhamLineLeftUp() - { + public void testBresenhamLineLeftUp() { BresenhamLine.calcPoints(2, 2, 3, 0, emitter); // 2,2, 2,1, 2,0, 3,0 @@ -78,37 +69,32 @@ public void testBresenhamLineLeftUp() } @Test - public void testBresenhamLineRightUp() - { + public void testBresenhamLineRightUp() { BresenhamLine.calcPoints(0, 0, 2, 3, emitter); // 0,0, 0,1, 1,1, 1,2, 2,2, 2,3 assertEquals(Helper.createPointList(0, 0, 1, 1, 1, 2, 2, 3), points); } @Test - public void testBresenhamBug() - { + public void testBresenhamBug() { BresenhamLine.calcPoints(0.5, -0.5, -0.6, 1.6, emitter, -1, -1, 0.75, 1.3); assertEquals(Helper.createPointList(0.575, -0.87, -0.175, 0.43, -0.925, 1.73), points); } @Test - public void testBresenhamHorizontal() - { + public void testBresenhamHorizontal() { BresenhamLine.calcPoints(.5, -.5, .5, 1, emitter, -1, -1, 0.6, 0.4); assertEquals(Helper.createPointList(.26, -.56, .26, -0.16, .26, .24, .26, .64, .26, 1.04), points); } @Test - public void testBresenhamVertical() - { + public void testBresenhamVertical() { BresenhamLine.calcPoints(-.5, .5, 1, 0.5, emitter, 0, 0, 0.4, 0.6); assertEquals(Helper.createPointList(-0.36, .06, 0.04, 0.06, 0.44, 0.06, 0.84, 0.06), points); } @Test - public void testRealBresenham() - { + public void testRealBresenham() { int parts = 4; int bits = (int) (Math.log(parts * parts) / Math.log(2)); double minLon = -1, maxLon = 1.6; @@ -117,11 +103,9 @@ public void testRealBresenham() double deltaLat = (maxLat - minLat) / parts; double deltaLon = (maxLon - minLon) / parts; final ArrayList keys = new ArrayList(); - PointEmitter tmpEmitter = new PointEmitter() - { + PointEmitter tmpEmitter = new PointEmitter() { @Override - public void set( double lat, double lon ) - { + public void set(double lat, double lon) { keys.add(keyAlgo.encode(lat, lon)); } }; @@ -145,8 +129,7 @@ public void set( double lat, double lon ) } @Test - public void testBresenhamToLeft() - { + public void testBresenhamToLeft() { BresenhamLine.calcPoints( 47.57383, 9.61984, 47.57382, 9.61890, emitter, 47, 9, 0.00647, 0.00964); diff --git a/core/src/test/java/com/graphhopper/storage/index/Location2IDFullIndexTest.java b/core/src/test/java/com/graphhopper/storage/index/Location2IDFullIndexTest.java index 64d2f6d061e..07678466dd4 100644 --- a/core/src/test/java/com/graphhopper/storage/index/Location2IDFullIndexTest.java +++ b/core/src/test/java/com/graphhopper/storage/index/Location2IDFullIndexTest.java @@ -19,30 +19,26 @@ import com.graphhopper.routing.util.EncodingManager; import com.graphhopper.storage.Graph; -import org.junit.*; +import org.junit.Test; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; /** * @author Peter Karich */ -public class Location2IDFullIndexTest extends AbstractLocationIndexTester -{ +public class Location2IDFullIndexTest extends AbstractLocationIndexTester { @Override - public LocationIndex createIndex( Graph g, int resolution ) - { + public LocationIndex createIndex(Graph g, int resolution) { return new Location2IDFullIndex(g); } @Override - public void testGrid() - { + public void testGrid() { // do not test against itself } @Test - public void testFullIndex() - { + public void testFullIndex() { LocationIndex tmpIdx = new Location2IDFullIndex(createSampleGraph(new EncodingManager("car"))); assertEquals(5, findID(tmpIdx, 2, 3)); assertEquals(10, findID(tmpIdx, 4, 1)); diff --git a/core/src/test/java/com/graphhopper/storage/index/Location2IDFullWithEdgesIndexTest.java b/core/src/test/java/com/graphhopper/storage/index/Location2IDFullWithEdgesIndexTest.java index bc84de9283a..9f0f434ca07 100644 --- a/core/src/test/java/com/graphhopper/storage/index/Location2IDFullWithEdgesIndexTest.java +++ b/core/src/test/java/com/graphhopper/storage/index/Location2IDFullWithEdgesIndexTest.java @@ -21,34 +21,29 @@ import com.graphhopper.storage.Graph; import org.junit.Test; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; /** * @author Peter Karich */ -public class Location2IDFullWithEdgesIndexTest extends AbstractLocationIndexTester -{ +public class Location2IDFullWithEdgesIndexTest extends AbstractLocationIndexTester { @Override - public LocationIndex createIndex( Graph g, int resolution ) - { + public LocationIndex createIndex(Graph g, int resolution) { return new Location2IDFullWithEdgesIndex(g); } @Override - public boolean hasEdgeSupport() - { + public boolean hasEdgeSupport() { return true; } @Override - public void testGrid() - { + public void testGrid() { // do not test against itself } @Test - public void testFullIndex() - { + public void testFullIndex() { LocationIndex tmpIdx = new Location2IDFullWithEdgesIndex(createSampleGraph(new EncodingManager("car"))); assertEquals(5, findID(tmpIdx, 2, 3)); assertEquals(10, findID(tmpIdx, 4, 1)); diff --git a/core/src/test/java/com/graphhopper/storage/index/Location2IDQuadtreeTest.java b/core/src/test/java/com/graphhopper/storage/index/Location2IDQuadtreeTest.java index 71a4860384f..a170efc4d44 100644 --- a/core/src/test/java/com/graphhopper/storage/index/Location2IDQuadtreeTest.java +++ b/core/src/test/java/com/graphhopper/storage/index/Location2IDQuadtreeTest.java @@ -21,19 +21,16 @@ import com.graphhopper.storage.Graph; import com.graphhopper.storage.MMapDirectory; import com.graphhopper.storage.RAMDirectory; - -import static org.junit.Assert.*; - import org.junit.Test; +import static org.junit.Assert.assertEquals; + /** * @author Peter Karich */ -public class Location2IDQuadtreeTest extends AbstractLocationIndexTester -{ +public class Location2IDQuadtreeTest extends AbstractLocationIndexTester { @Override - public LocationIndex createIndex( Graph g, int resolution ) - { + public LocationIndex createIndex(Graph g, int resolution) { if (resolution < 0) resolution = 120; return new Location2IDQuadtree(g, new MMapDirectory(location + "loc2idIndex").create()). @@ -41,8 +38,7 @@ public LocationIndex createIndex( Graph g, int resolution ) } @Test - public void testNormedDist() - { + public void testNormedDist() { Location2IDQuadtree index = new Location2IDQuadtree(createGHStorage(new EncodingManager("car")), new RAMDirectory()); index.initAlgo(5, 6); assertEquals(1, index.getNormedDist(0, 1), 1e-6); @@ -54,20 +50,17 @@ public void testNormedDist() } @Override - boolean testGridIgnore( int i ) - { + boolean testGridIgnore(int i) { // conceptual limitation where we are stuck in a blind alley limited // to the current tile - if (i == 6 || i == 36 || i == 90 || i == 96) - { + if (i == 6 || i == 36 || i == 90 || i == 96) { return true; } return false; } @Override - public void testDifferentVehicles() - { + public void testDifferentVehicles() { // currently unsupported } } diff --git a/core/src/test/java/com/graphhopper/storage/index/LocationIndexTreeCHTest.java b/core/src/test/java/com/graphhopper/storage/index/LocationIndexTreeCHTest.java index 218ad88db5b..9e55a157174 100644 --- a/core/src/test/java/com/graphhopper/storage/index/LocationIndexTreeCHTest.java +++ b/core/src/test/java/com/graphhopper/storage/index/LocationIndexTreeCHTest.java @@ -17,43 +17,38 @@ */ package com.graphhopper.storage.index; -import com.graphhopper.routing.util.EdgeFilter; import com.graphhopper.routing.util.EncodingManager; -import com.graphhopper.routing.weighting.FastestWeighting; import com.graphhopper.routing.util.FlagEncoder; +import com.graphhopper.routing.weighting.FastestWeighting; import com.graphhopper.storage.*; -import com.graphhopper.util.EdgeIteratorState; import com.graphhopper.util.CHEdgeIteratorState; +import com.graphhopper.util.EdgeIteratorState; import com.graphhopper.util.Helper; import gnu.trove.list.TIntList; import gnu.trove.set.TIntSet; import gnu.trove.set.hash.TIntHashSet; +import org.junit.Test; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; -import org.junit.Test; - -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; /** * @author Peter Karich */ -public class LocationIndexTreeCHTest extends LocationIndexTreeTest -{ +public class LocationIndexTreeCHTest extends LocationIndexTreeTest { @Override - public LocationIndexTree createIndex( Graph g, int resolution ) - { + public LocationIndexTree createIndex(Graph g, int resolution) { if (resolution < 0) resolution = 500000; return (LocationIndexTree) createIndexNoPrepare(g, resolution).prepareIndex(); } @Override - public LocationIndexTree createIndexNoPrepare( Graph g, int resolution ) - { + public LocationIndexTree createIndexNoPrepare(Graph g, int resolution) { Directory dir = new RAMDirectory(location); LocationIndexTree tmpIdx = new LocationIndexTree(g, dir); tmpIdx.setResolution(resolution); @@ -61,15 +56,13 @@ public LocationIndexTree createIndexNoPrepare( Graph g, int resolution ) } @Override - GraphHopperStorage createGHStorage( Directory dir, EncodingManager encodingManager, boolean is3D ) - { + GraphHopperStorage createGHStorage(Directory dir, EncodingManager encodingManager, boolean is3D) { return new GraphHopperStorage(Arrays.asList(new FastestWeighting(encodingManager.getEncoder("car"))), dir, encodingManager, is3D, new GraphExtension.NoOpExtension()). create(100); } @Test - public void testCHGraph() - { + public void testCHGraph() { GraphHopperStorage ghStorage = createGHStorage(new RAMDirectory(), encodingManager, false); CHGraph lg = ghStorage.getGraph(CHGraph.class); // 0 @@ -108,8 +101,7 @@ public void testCHGraph() } @Test - public void testSortHighLevelFirst() - { + public void testSortHighLevelFirst() { GraphHopperStorage g = createGHStorage(new RAMDirectory(), encodingManager, false); final CHGraph lg = g.getGraph(CHGraph.class); lg.getNodeAccess().ensureNode(4); @@ -120,11 +112,9 @@ public void testSortHighLevelFirst() // nodes with high level should come first to be covered by lower level nodes ArrayList list = Helper.tIntListToArrayList(tlist); - Collections.sort(list, new Comparator() - { + Collections.sort(list, new Comparator() { @Override - public int compare( Integer o1, Integer o2 ) - { + public int compare(Integer o1, Integer o2) { return lg.getLevel(o2) - lg.getLevel(o1); } }); @@ -134,8 +124,7 @@ public int compare( Integer o1, Integer o2 ) } @Test - public void testCHGraphBug() - { + public void testCHGraphBug() { // 0 // | // | X 2--3 diff --git a/core/src/test/java/com/graphhopper/storage/index/LocationIndexTreeTest.java b/core/src/test/java/com/graphhopper/storage/index/LocationIndexTreeTest.java index b57d83ceb2e..9f2b7440c76 100644 --- a/core/src/test/java/com/graphhopper/storage/index/LocationIndexTreeTest.java +++ b/core/src/test/java/com/graphhopper/storage/index/LocationIndexTreeTest.java @@ -25,30 +25,27 @@ import com.graphhopper.util.*; import com.graphhopper.util.shapes.GHPoint; import gnu.trove.set.hash.TIntHashSet; +import org.junit.Test; import java.util.Arrays; -import org.junit.Test; - -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; /** * @author Peter Karich */ -public class LocationIndexTreeTest extends AbstractLocationIndexTester -{ +public class LocationIndexTreeTest extends AbstractLocationIndexTester { protected final EncodingManager encodingManager = new EncodingManager("car"); @Override - public LocationIndexTree createIndex( Graph g, int resolution ) - { + public LocationIndexTree createIndex(Graph g, int resolution) { if (resolution < 0) resolution = 500000; return (LocationIndexTree) createIndexNoPrepare(g, resolution).prepareIndex(); } - public LocationIndexTree createIndexNoPrepare( Graph g, int resolution ) - { + public LocationIndexTree createIndexNoPrepare(Graph g, int resolution) { Directory dir = new RAMDirectory(location); LocationIndexTree tmpIDX = new LocationIndexTree(g, dir); tmpIDX.setResolution(resolution); @@ -56,8 +53,7 @@ public LocationIndexTree createIndexNoPrepare( Graph g, int resolution ) } @Override - public boolean hasEdgeSupport() - { + public boolean hasEdgeSupport() { return true; } @@ -66,8 +62,7 @@ public boolean hasEdgeSupport() // |1----3-\| // |____/ 4 // 2-------/ - Graph createTestGraph( EncodingManager em ) - { + Graph createTestGraph(EncodingManager em) { Graph graph = createGHStorage(new RAMDirectory(), em, false); NodeAccess na = graph.getNodeAccess(); na.setNode(0, 0.5, -0.5); @@ -86,8 +81,7 @@ Graph createTestGraph( EncodingManager em ) } @Test - public void testSnappedPointAndGeometry() - { + public void testSnappedPointAndGeometry() { Graph graph = createTestGraph(encodingManager); LocationIndex index = createIndex(graph, -1); // query directly the tower node @@ -106,8 +100,7 @@ public void testSnappedPointAndGeometry() } @Test - public void testInMemIndex() - { + public void testInMemIndex() { Graph graph = createTestGraph(encodingManager); LocationIndexTree index = createIndexNoPrepare(graph, 50000); index.prepareAlgo(); @@ -154,8 +147,7 @@ public void testInMemIndex() } @Test - public void testInMemIndex2() - { + public void testInMemIndex2() { Graph graph = createTestGraph2(); LocationIndexTree index = createIndexNoPrepare(graph, 500); index.prepareAlgo(); @@ -195,8 +187,7 @@ public void testInMemIndex2() } @Test - public void testInMemIndex3() - { + public void testInMemIndex3() { LocationIndexTree index = createIndexNoPrepare(createTestGraph(encodingManager), 10000); index.prepareAlgo(); LocationIndexTree.InMemConstructionIndex inMemIndex = index.getPrepareInMemIndex(); @@ -215,8 +206,7 @@ public void testInMemIndex3() } @Test - public void testReverseSpatialKey() - { + public void testReverseSpatialKey() { LocationIndexTree index = createIndex(createTestGraph(encodingManager), 200); assertEquals(Helper.createTList(64, 64, 64, 4), index.getEntries()); @@ -226,8 +216,7 @@ public void testReverseSpatialKey() } @Test - public void testMoreReal() - { + public void testMoreReal() { Graph graph = createGHStorage(new EncodingManager("car")); NodeAccess na = graph.getNodeAccess(); na.setNode(1, 51.2492152, 9.4317166); @@ -251,8 +240,7 @@ public void testMoreReal() // | |/------/ / //-1| 2---------/ // | - private Graph createTestGraphWithWayGeometry() - { + private Graph createTestGraphWithWayGeometry() { Graph graph = createGHStorage(encodingManager); NodeAccess na = graph.getNodeAccess(); na.setNode(0, 0.5, -0.5); @@ -272,8 +260,7 @@ private Graph createTestGraphWithWayGeometry() } @Test - public void testWayGeometry() - { + public void testWayGeometry() { Graph g = createTestGraphWithWayGeometry(); LocationIndex index = createIndex(g, -1); assertEquals(1, findID(index, 0, 0)); @@ -283,8 +270,7 @@ public void testWayGeometry() } @Test - public void testFindingWayGeometry() - { + public void testFindingWayGeometry() { Graph g = createGHStorage(encodingManager); NodeAccess na = g.getNodeAccess(); na.setNode(10, 51.2492152, 9.4317166); @@ -300,25 +286,21 @@ public void testFindingWayGeometry() } @Test - public void testEdgeFilter() - { + public void testEdgeFilter() { Graph graph = createTestGraph(encodingManager); LocationIndexTree index = createIndex(graph, -1); assertEquals(1, index.findClosest(-.6, -.6, EdgeFilter.ALL_EDGES).getClosestNode()); - assertEquals(2, index.findClosest(-.6, -.6, new EdgeFilter() - { + assertEquals(2, index.findClosest(-.6, -.6, new EdgeFilter() { @Override - public boolean accept( EdgeIteratorState iter ) - { + public boolean accept(EdgeIteratorState iter) { return iter.getBaseNode() == 2 || iter.getAdjNode() == 2; } }).getClosestNode()); } // see testgraph2.jpg - Graph createTestGraph2() - { + Graph createTestGraph2() { Graph graph = createGHStorage(new RAMDirectory(), encodingManager, false); NodeAccess na = graph.getNodeAccess(); na.setNode(8, 49.94553, 11.57214); @@ -413,8 +395,7 @@ Graph createTestGraph2() } @Test - public void testRMin() - { + public void testRMin() { Graph graph = createTestGraph(encodingManager); LocationIndexTree index = createIndex(graph, 50000); @@ -445,8 +426,7 @@ public void testRMin() } @Test - public void testSearchWithFilter_issue318() - { + public void testSearchWithFilter_issue318() { CarFlagEncoder carEncoder = new CarFlagEncoder(); BikeFlagEncoder bikeEncoder = new BikeFlagEncoder(); @@ -456,10 +436,8 @@ public void testSearchWithFilter_issue318() // distance from point to point is roughly 1 km int MAX = 5; - for (int latIdx = 0; latIdx < MAX; latIdx++) - { - for (int lonIdx = 0; lonIdx < MAX; lonIdx++) - { + for (int latIdx = 0; latIdx < MAX; latIdx++) { + for (int lonIdx = 0; lonIdx < MAX; lonIdx++) { int index = lonIdx * 10 + latIdx; na.setNode(index, 0.01 * latIdx, 0.01 * lonIdx); if (latIdx < MAX - 1) @@ -472,12 +450,10 @@ public void testSearchWithFilter_issue318() // reduce access for bike to two edges only AllEdgesIterator iter = graph.getAllEdges(); - while (iter.next()) - { + while (iter.next()) { iter.setFlags(bikeEncoder.setAccess(iter.getFlags(), false, false)); } - for (EdgeIteratorState edge : Arrays.asList(GHUtility.getEdge(graph, 0, 1), GHUtility.getEdge(graph, 1, 2))) - { + for (EdgeIteratorState edge : Arrays.asList(GHUtility.getEdge(graph, 0, 1), GHUtility.getEdge(graph, 1, 2))) { edge.setFlags(bikeEncoder.setAccess(edge.getFlags(), true, true)); } @@ -500,8 +476,7 @@ public void testSearchWithFilter_issue318() // | | | | // 4--5--6--7 @Test - public void testCrossBoundaryNetwork_issue667() - { + public void testCrossBoundaryNetwork_issue667() { Graph graph = createGHStorage(new RAMDirectory(), encodingManager, false); NodeAccess na = graph.getNodeAccess(); na.setNode(0, 0.1, 179.5); @@ -535,8 +510,7 @@ public void testCrossBoundaryNetwork_issue667() index.prepareIndex(); assertTrue(graph.getNodes() > 0); - for (int i = 0; i < graph.getNodes(); i++) - { + for (int i = 0; i < graph.getNodes(); i++) { QueryResult qr = index.findClosest(na.getLat(i), na.getLon(i), EdgeFilter.ALL_EDGES); assertEquals(i, qr.getClosestNode()); } diff --git a/core/src/test/java/com/graphhopper/util/AbstractBitUtilTester.java b/core/src/test/java/com/graphhopper/util/AbstractBitUtilTester.java index cf96326da5f..1b51abbccc2 100644 --- a/core/src/test/java/com/graphhopper/util/AbstractBitUtilTester.java +++ b/core/src/test/java/com/graphhopper/util/AbstractBitUtilTester.java @@ -17,22 +17,20 @@ */ package com.graphhopper.util; -import static org.junit.Assert.assertEquals; - import org.junit.Test; +import static org.junit.Assert.assertEquals; + /** * @author Peter Karich */ -public abstract class AbstractBitUtilTester -{ +public abstract class AbstractBitUtilTester { protected BitUtil bitUtil = getBitUtil(); abstract BitUtil getBitUtil(); @Test - public void testToFloat() - { + public void testToFloat() { byte[] bytes = bitUtil.fromFloat(Float.MAX_VALUE); assertEquals(Float.MAX_VALUE, bitUtil.toFloat(bytes), 1e-9); @@ -41,8 +39,7 @@ public void testToFloat() } @Test - public void testToDouble() - { + public void testToDouble() { byte[] bytes = bitUtil.fromDouble(Double.MAX_VALUE); assertEquals(Double.MAX_VALUE, bitUtil.toDouble(bytes), 1e-9); @@ -51,8 +48,7 @@ public void testToDouble() } @Test - public void testToInt() - { + public void testToInt() { byte[] bytes = bitUtil.fromInt(Integer.MAX_VALUE); assertEquals(Integer.MAX_VALUE, bitUtil.toInt(bytes)); @@ -61,8 +57,7 @@ public void testToInt() } @Test - public void testToShort() - { + public void testToShort() { byte[] bytes = bitUtil.fromShort(Short.MAX_VALUE); assertEquals(Short.MAX_VALUE, bitUtil.toShort(bytes)); @@ -77,8 +72,7 @@ public void testToShort() } @Test - public void testToLong() - { + public void testToLong() { byte[] bytes = bitUtil.fromLong(Long.MAX_VALUE); assertEquals(Long.MAX_VALUE, bitUtil.toLong(bytes)); @@ -87,8 +81,7 @@ public void testToLong() } @Test - public void testToLastBitString() - { + public void testToLastBitString() { assertEquals("1", bitUtil.toLastBitString(1L, 1)); assertEquals("01", bitUtil.toLastBitString(1L, 2)); assertEquals("001", bitUtil.toLastBitString(1L, 3)); @@ -97,16 +90,14 @@ public void testToLastBitString() } @Test - public void testBitString2Long() - { + public void testBitString2Long() { String str = "01000000000110000011100000011110"; assertEquals(str + "00000000000000000000000000000000", bitUtil.toBitString(bitUtil.fromBitString2Long(str))); assertEquals("1000000000000000000000000000000000000000000000000000000000000000", bitUtil.toBitString(1L << 63)); } @Test - public void testReverse() - { + public void testReverse() { String str48 = "000000000000000000000000000000000000000000000000"; long ret = bitUtil.reverse(bitUtil.fromBitString2Long(str48 + "0111000000000101"), 16); assertEquals(str48 + "1010000000001110", bitUtil.toBitString(ret, 64)); diff --git a/core/src/test/java/com/graphhopper/util/AngleCalcTest.java b/core/src/test/java/com/graphhopper/util/AngleCalcTest.java index e7deb70b61c..485b5759400 100644 --- a/core/src/test/java/com/graphhopper/util/AngleCalcTest.java +++ b/core/src/test/java/com/graphhopper/util/AngleCalcTest.java @@ -17,21 +17,19 @@ */ package com.graphhopper.util; -import static org.junit.Assert.*; - import org.junit.Test; +import static org.junit.Assert.assertEquals; + /** * @author Johannes Pelzer * @author Peter Karich */ -public class AngleCalcTest -{ +public class AngleCalcTest { private final AngleCalc AC = Helper.ANGLE_CALC; @Test - public void testOrientation() - { + public void testOrientation() { assertEquals(90.0, Math.toDegrees(AC.calcOrientation(0, 0, 1, 0)), 0.01); assertEquals(45.0, Math.toDegrees(AC.calcOrientation(0, 0, 1, 1)), 0.01); assertEquals(0.0, Math.toDegrees(AC.calcOrientation(0, 0, 0, 1)), 0.01); @@ -44,8 +42,7 @@ public void testOrientation() } @Test - public void testAlignOrientation() - { + public void testAlignOrientation() { assertEquals(90.0, Math.toDegrees(AC.alignOrientation(Math.toRadians(90), Math.toRadians(90))), 0.001); assertEquals(225.0, Math.toDegrees(AC.alignOrientation(Math.toRadians(90), Math.toRadians(-135))), 0.001); assertEquals(-45.0, Math.toDegrees(AC.alignOrientation(Math.toRadians(-135), Math.toRadians(-45))), 0.001); @@ -53,8 +50,7 @@ public void testAlignOrientation() } @Test - public void testCombined() - { + public void testCombined() { double orientation = AC.calcOrientation(52.414918, 13.244221, 52.415333, 13.243595); assertEquals(132.7, Math.toDegrees(AC.alignOrientation(0, orientation)), 1); @@ -63,8 +59,7 @@ public void testCombined() } @Test - public void testCalcAzimuth() - { + public void testCalcAzimuth() { assertEquals(45.0, AC.calcAzimuth(0, 0, 1, 1), 0.001); assertEquals(90.0, AC.calcAzimuth(0, 0, 0, 1), 0.001); assertEquals(180.0, AC.calcAzimuth(0, 0, -1, 0), 0.001); @@ -73,14 +68,12 @@ public void testCalcAzimuth() } @Test - public void testAzimuthCompassPoint() - { + public void testAzimuthCompassPoint() { assertEquals("S", AC.azimuth2compassPoint(199)); } @Test - public void testAtan2() - { + public void testAtan2() { // assertEquals(0, AngleCalc.atan2(0, 0), 1e-4); // assertEquals(0, AngleCalc.atan2(-0.002, 0), 1e-4); assertEquals(45, AngleCalc.atan2(5, 5) * 180 / Math.PI, 1e-2); @@ -92,28 +85,26 @@ public void testAtan2() assertEquals(90, Math.atan2(1, 0) * 180 / Math.PI, 1e-2); assertEquals(90, AngleCalc.atan2(1, 0) * 180 / Math.PI, 1e-2); } - + @Test - public void testConvertAzimuth2xaxisAngle() - { - assertEquals(Math.PI/2, AC.convertAzimuth2xaxisAngle(0), 1E-6); - assertEquals(Math.PI/2, Math.abs(AC.convertAzimuth2xaxisAngle(360)), 1E-6); + public void testConvertAzimuth2xaxisAngle() { + assertEquals(Math.PI / 2, AC.convertAzimuth2xaxisAngle(0), 1E-6); + assertEquals(Math.PI / 2, Math.abs(AC.convertAzimuth2xaxisAngle(360)), 1E-6); assertEquals(0, AC.convertAzimuth2xaxisAngle(90), 1E-6); - assertEquals(-Math.PI/2, AC.convertAzimuth2xaxisAngle(180), 1E-6); + assertEquals(-Math.PI / 2, AC.convertAzimuth2xaxisAngle(180), 1E-6); assertEquals(Math.PI, Math.abs(AC.convertAzimuth2xaxisAngle(270)), 1E-6); - assertEquals(-3*Math.PI/4, AC.convertAzimuth2xaxisAngle(225), 1E-6); - assertEquals(3*Math.PI/4, AC.convertAzimuth2xaxisAngle(315), 1E-6); + assertEquals(-3 * Math.PI / 4, AC.convertAzimuth2xaxisAngle(225), 1E-6); + assertEquals(3 * Math.PI / 4, AC.convertAzimuth2xaxisAngle(315), 1E-6); } - + @Test - public void checkAzimuthConsitency() - { - double azimuthDegree = AC.calcAzimuth(0, 0, 1, 1); + public void checkAzimuthConsitency() { + double azimuthDegree = AC.calcAzimuth(0, 0, 1, 1); double radianXY = AC.calcOrientation(0, 0, 1, 1); double radian2 = AC.convertAzimuth2xaxisAngle(azimuthDegree); assertEquals(radianXY, radian2, 1E-3); - azimuthDegree = AC.calcAzimuth(0, 4, 1, 3); + azimuthDegree = AC.calcAzimuth(0, 4, 1, 3); radianXY = AC.calcOrientation(0, 4, 1, 3); radian2 = AC.convertAzimuth2xaxisAngle(azimuthDegree); assertEquals(radianXY, radian2, 1E-3); diff --git a/core/src/test/java/com/graphhopper/util/BitUtilBigTest.java b/core/src/test/java/com/graphhopper/util/BitUtilBigTest.java index 627aff29eb1..1ae9f588781 100644 --- a/core/src/test/java/com/graphhopper/util/BitUtilBigTest.java +++ b/core/src/test/java/com/graphhopper/util/BitUtilBigTest.java @@ -19,22 +19,19 @@ import org.junit.Test; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; /** * @author Peter Karich */ -public class BitUtilBigTest extends AbstractBitUtilTester -{ +public class BitUtilBigTest extends AbstractBitUtilTester { @Override - BitUtil getBitUtil() - { + BitUtil getBitUtil() { return BitUtil.BIG; } @Test - public void testToBitString() - { + public void testToBitString() { assertEquals("0010101010101010101010101010101010101010101010101010101010101010", bitUtil.toBitString(Long.MAX_VALUE / 3)); assertEquals("0111111111111111111111111111111111111111111111111111111111111111", bitUtil.toBitString(Long.MAX_VALUE)); @@ -45,8 +42,7 @@ public void testToBitString() } @Test - public void testFromBitString() - { + public void testFromBitString() { String str = "011011100"; assertEquals(str + "0000000", bitUtil.toBitString(bitUtil.fromBitString(str))); diff --git a/core/src/test/java/com/graphhopper/util/BitUtilLittleTest.java b/core/src/test/java/com/graphhopper/util/BitUtilLittleTest.java index aacb5198dac..091b69b3779 100644 --- a/core/src/test/java/com/graphhopper/util/BitUtilLittleTest.java +++ b/core/src/test/java/com/graphhopper/util/BitUtilLittleTest.java @@ -19,22 +19,19 @@ import org.junit.Test; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; /** * @author Peter Karich */ -public class BitUtilLittleTest extends AbstractBitUtilTester -{ +public class BitUtilLittleTest extends AbstractBitUtilTester { @Override - BitUtil getBitUtil() - { + BitUtil getBitUtil() { return BitUtil.LITTLE; } @Test - public void testToBitString() - { + public void testToBitString() { assertEquals("0010101010101010101010101010101010101010101010101010101010101010", bitUtil.toBitString(Long.MAX_VALUE / 3)); assertEquals("0111111111111111111111111111111111111111111111111111111111111111", bitUtil.toBitString(Long.MAX_VALUE)); @@ -45,8 +42,7 @@ public void testToBitString() } @Test - public void testFromBitString() - { + public void testFromBitString() { String str = "001110110"; assertEquals(str + "0000000", bitUtil.toBitString(bitUtil.fromBitString(str))); diff --git a/core/src/test/java/com/graphhopper/util/BreadthFirstSearchTest.java b/core/src/test/java/com/graphhopper/util/BreadthFirstSearchTest.java index cb6fd3cb8a3..ab77565e81e 100644 --- a/core/src/test/java/com/graphhopper/util/BreadthFirstSearchTest.java +++ b/core/src/test/java/com/graphhopper/util/BreadthFirstSearchTest.java @@ -18,39 +18,35 @@ package com.graphhopper.util; import com.graphhopper.routing.util.EncodingManager; -import com.graphhopper.storage.GraphBuilder; import com.graphhopper.storage.Graph; +import com.graphhopper.storage.GraphBuilder; import gnu.trove.list.TIntList; import gnu.trove.list.array.TIntArrayList; import gnu.trove.set.hash.TIntHashSet; import org.junit.Before; import org.junit.Test; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; /** * @author Peter Karich */ -public class BreadthFirstSearchTest -{ +public class BreadthFirstSearchTest { int counter; TIntHashSet set = new TIntHashSet(); TIntList list = new TIntArrayList(); @Before - public void setup() - { + public void setup() { counter = 0; } @Test - public void testBFS() - { - BreadthFirstSearch bfs = new BreadthFirstSearch() - { + public void testBFS() { + BreadthFirstSearch bfs = new BreadthFirstSearch() { @Override - public boolean goFurther( int v ) - { + public boolean goFurther(int v) { counter++; assertTrue("v " + v + " is already contained in set. iteration:" + counter, !set.contains(v)); set.add(v); @@ -81,13 +77,10 @@ public boolean goFurther( int v ) } @Test - public void testBFS2() - { - BreadthFirstSearch bfs = new BreadthFirstSearch() - { + public void testBFS2() { + BreadthFirstSearch bfs = new BreadthFirstSearch() { @Override - public boolean goFurther( int v ) - { + public boolean goFurther(int v) { counter++; assertTrue("v " + v + " is already contained in set. iteration:" + counter, !set.contains(v)); set.add(v); @@ -110,5 +103,4 @@ public boolean goFurther( int v ) assertEquals("{1, 5, 2, 6, 3, 4}", list.toString()); } - } diff --git a/core/src/test/java/com/graphhopper/util/CHEdgeIteratorTest.java b/core/src/test/java/com/graphhopper/util/CHEdgeIteratorTest.java index fd258151528..78f26bf9d96 100644 --- a/core/src/test/java/com/graphhopper/util/CHEdgeIteratorTest.java +++ b/core/src/test/java/com/graphhopper/util/CHEdgeIteratorTest.java @@ -17,24 +17,24 @@ */ package com.graphhopper.util; +import com.graphhopper.routing.util.CarFlagEncoder; +import com.graphhopper.routing.util.DefaultEdgeFilter; +import com.graphhopper.routing.util.EdgeFilter; +import com.graphhopper.routing.util.EncodingManager; import com.graphhopper.routing.weighting.FastestWeighting; -import com.graphhopper.routing.util.*; -import com.graphhopper.storage.GraphBuilder; import com.graphhopper.storage.CHGraph; +import com.graphhopper.storage.GraphBuilder; import com.graphhopper.storage.GraphHopperStorage; - -import static org.junit.Assert.*; - import org.junit.Test; +import static org.junit.Assert.assertEquals; + /** * @author Peter Karich */ -public class CHEdgeIteratorTest -{ +public class CHEdgeIteratorTest { @Test - public void testUpdateFlags() - { + public void testUpdateFlags() { CarFlagEncoder carFlagEncoder = new CarFlagEncoder(); EncodingManager encodingManager = new EncodingManager(carFlagEncoder); FastestWeighting weighting = new FastestWeighting(carFlagEncoder); diff --git a/core/src/test/java/com/graphhopper/util/ConfigMapTest.java b/core/src/test/java/com/graphhopper/util/ConfigMapTest.java index bd560434856..4307fc92b12 100644 --- a/core/src/test/java/com/graphhopper/util/ConfigMapTest.java +++ b/core/src/test/java/com/graphhopper/util/ConfigMapTest.java @@ -18,17 +18,15 @@ package com.graphhopper.util; import org.junit.Test; + import static org.junit.Assert.*; /** - * * @author Peter Karich */ -public class ConfigMapTest -{ +public class ConfigMapTest { @Test - public void testPut() - { + public void testPut() { ConfigMap instance = new ConfigMap(); instance.put("int_val", 1); instance.put("test_pest", true); diff --git a/core/src/test/java/com/graphhopper/util/DepthFirstSearchTest.java b/core/src/test/java/com/graphhopper/util/DepthFirstSearchTest.java index 98acd7d338b..645b38411b2 100644 --- a/core/src/test/java/com/graphhopper/util/DepthFirstSearchTest.java +++ b/core/src/test/java/com/graphhopper/util/DepthFirstSearchTest.java @@ -28,32 +28,28 @@ import org.junit.Before; import org.junit.Test; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; /** * @author jansoe */ -public class DepthFirstSearchTest -{ +public class DepthFirstSearchTest { int counter; TIntHashSet set = new TIntHashSet(); TIntList list = new TIntArrayList(); @Before - public void setup() - { + public void setup() { counter = 0; } @Test - public void testDFS1() - { - DepthFirstSearch dfs = new DepthFirstSearch() - { + public void testDFS1() { + DepthFirstSearch dfs = new DepthFirstSearch() { @Override - public boolean goFurther( int v ) - { + public boolean goFurther(int v) { counter++; assertTrue("v " + v + " is already contained in set. iteration:" + counter, !set.contains(v)); set.add(v); @@ -80,13 +76,10 @@ public boolean goFurther( int v ) } @Test - public void testDFS2() - { - DepthFirstSearch dfs = new DepthFirstSearch() - { + public void testDFS2() { + DepthFirstSearch dfs = new DepthFirstSearch() { @Override - public boolean goFurther( int v ) - { + public boolean goFurther(int v) { counter++; assertTrue("v " + v + " is already contained in set. iteration:" + counter, !set.contains(v)); set.add(v); diff --git a/core/src/test/java/com/graphhopper/util/DistanceCalcEarthTest.java b/core/src/test/java/com/graphhopper/util/DistanceCalcEarthTest.java index 285b458cc58..001d0d2c842 100644 --- a/core/src/test/java/com/graphhopper/util/DistanceCalcEarthTest.java +++ b/core/src/test/java/com/graphhopper/util/DistanceCalcEarthTest.java @@ -18,27 +18,23 @@ package com.graphhopper.util; import com.graphhopper.util.shapes.GHPoint; +import org.junit.Test; import static org.junit.Assert.*; -import org.junit.Test; - /** * @author Peter Karich */ -public class DistanceCalcEarthTest -{ +public class DistanceCalcEarthTest { private DistanceCalc dc = new DistanceCalcEarth(); @Test - public void testCalcCircumference() - { + public void testCalcCircumference() { assertEquals(DistanceCalcEarth.C, dc.calcCircumference(0), 1e-7); } @Test - public void testDistance() - { + public void testDistance() { float lat = 24.235f; float lon = 47.234f; DistanceCalc approxDist = new DistancePlaneProjection(); @@ -86,8 +82,7 @@ public void testDistance() } @Test - public void testEdgeDistance() - { + public void testEdgeDistance() { double dist = dc.calcNormalizedEdgeDistance(49.94241, 11.544356, 49.937964, 11.541824, 49.942272, 11.555643); @@ -105,8 +100,7 @@ public void testEdgeDistance() } @Test - public void testValidEdgeDistance() - { + public void testValidEdgeDistance() { assertTrue(dc.validEdgeDistance(49.94241, 11.544356, 49.937964, 11.541824, 49.942272, 11.555643)); assertTrue(dc.validEdgeDistance(49.936624, 11.547636, 49.937964, 11.541824, 49.942272, 11.555643)); assertTrue(dc.validEdgeDistance(49.940712, 11.556069, 49.937964, 11.541824, 49.942272, 11.555643)); @@ -125,8 +119,7 @@ public void testValidEdgeDistance() } @Test - public void testPrecisionBug() - { + public void testPrecisionBug() { DistanceCalc dist = new DistancePlaneProjection(); // DistanceCalc dist = new DistanceCalc(); double queryLat = 42.56819, queryLon = 1.603231; @@ -152,8 +145,7 @@ public void testPrecisionBug() } @Test - public void testPrecisionBug2() - { + public void testPrecisionBug2() { DistanceCalc distCalc = new DistancePlaneProjection(); double queryLat = 55.818994, queryLon = 37.595354; double tmpLat = 55.81777239183573, tmpLon = 37.59598350366913; diff --git a/core/src/test/java/com/graphhopper/util/DouglasPeuckerTest.java b/core/src/test/java/com/graphhopper/util/DouglasPeuckerTest.java index 9224698f5d4..d3a6fb9ce28 100644 --- a/core/src/test/java/com/graphhopper/util/DouglasPeuckerTest.java +++ b/core/src/test/java/com/graphhopper/util/DouglasPeuckerTest.java @@ -19,13 +19,13 @@ import org.junit.Test; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; /** * @author Peter Karich */ -public class DouglasPeuckerTest -{ +public class DouglasPeuckerTest { // get some real life points from graphhopper API // http://217.92.216.224:8080/?point=49.945642,11.571436&point=49.946001,11.580706 @@ -35,10 +35,11 @@ public class DouglasPeuckerTest + "[11.576840167720023,49.94727782787258],[11.576961425921949,49.94725827009808],[11.577226852861648,49.947215242994176],[11.577394863457863,49.94717668623872],[11.577511092517772,49.94715005041249],[11.577635517216523,49.947112238715114]," + "[11.577917149169382,49.94702655703634],[11.577969116970207,49.947010724552214],[11.578816061738493,49.94673523932849],[11.579533552666014,49.94648974269233],[11.580073719771365,49.946299007824784],[11.580253092503245,49.946237913062525]," + "[11.580604946179799,49.94608871518274],[11.580740546749693,49.94603041438826]]"; + String points2 = "[[9.961074440801317,50.203764443183644],[9.96106605889796,50.20365789987872],[9.960999562464645,50.20318963087774],[9.96094144793469,50.202952888673984],[9.96223002587773,50.20267889356641],[9.962200968612752,50.20262022024289]," + + "[9.961859918278305,50.201853928011374],[9.961668810881722,50.20138565901039],[9.96216874485095,50.20128507617008],[9.961953795595925,50.20088553877664],[9.961899033827313,50.200686794534775],[9.961716680863127,50.20014066696481],[9.961588158344957,50.199798499043254]]"; @Test - public void testParse() - { + public void testParse() { PointList pointList = new PointList(); pointList.parse2DJSON("[[11.571499218899739,49.945605917549265],[11.571664621792689,49.94570668665409]]"); assertEquals(49.945605917549265, pointList.getLatitude(0), 1e-6); @@ -48,8 +49,7 @@ public void testParse() } @Test - public void testPathSimplify() - { + public void testPathSimplify() { PointList pointList = new PointList(); pointList.parse2DJSON(points1); assertEquals(32, pointList.getSize()); @@ -59,8 +59,7 @@ public void testPathSimplify() } @Test - public void testSimplifyCheckPointCount() - { + public void testSimplifyCheckPointCount() { PointList pointList = new PointList(); pointList.parse2DJSON(points1); DouglasPeucker dp = new DouglasPeucker().setMaxDistance(.5); @@ -70,12 +69,8 @@ public void testSimplifyCheckPointCount() assertFalse(pointList.toString(), pointList.toString().contains("NaN")); } - String points2 = "[[9.961074440801317,50.203764443183644],[9.96106605889796,50.20365789987872],[9.960999562464645,50.20318963087774],[9.96094144793469,50.202952888673984],[9.96223002587773,50.20267889356641],[9.962200968612752,50.20262022024289]," - + "[9.961859918278305,50.201853928011374],[9.961668810881722,50.20138565901039],[9.96216874485095,50.20128507617008],[9.961953795595925,50.20088553877664],[9.961899033827313,50.200686794534775],[9.961716680863127,50.20014066696481],[9.961588158344957,50.199798499043254]]"; - @Test - public void testSimplifyCheckPointOrder() - { + public void testSimplifyCheckPointOrder() { PointList pointList = new PointList(); pointList.parse2DJSON(points2); assertEquals(13, pointList.getSize()); @@ -83,7 +78,7 @@ public void testSimplifyCheckPointOrder() assertEquals(11, pointList.getSize()); assertFalse(pointList.toString(), pointList.toString().contains("NaN")); assertEquals("(50.203764443183644,9.961074440801317), (50.20318963087774,9.960999562464645), (50.202952888673984,9.96094144793469), (50.20267889356641,9.96223002587773), (50.201853928011374,9.961859918278305), " - + "(50.20138565901039,9.961668810881722), (50.20128507617008,9.96216874485095), (50.20088553877664,9.961953795595925), (50.200686794534775,9.961899033827313), (50.20014066696481,9.961716680863127), (50.199798499043254,9.961588158344957)", + + "(50.20138565901039,9.961668810881722), (50.20128507617008,9.96216874485095), (50.20088553877664,9.961953795595925), (50.200686794534775,9.961899033827313), (50.20014066696481,9.961716680863127), (50.199798499043254,9.961588158344957)", pointList.toString()); } } diff --git a/core/src/test/java/com/graphhopper/util/GHUtilityTest.java b/core/src/test/java/com/graphhopper/util/GHUtilityTest.java index be8329a12b0..cf431375e07 100644 --- a/core/src/test/java/com/graphhopper/util/GHUtilityTest.java +++ b/core/src/test/java/com/graphhopper/util/GHUtilityTest.java @@ -19,24 +19,21 @@ import com.graphhopper.routing.util.CarFlagEncoder; import com.graphhopper.routing.util.EncodingManager; -import com.graphhopper.routing.weighting.FastestWeighting; import com.graphhopper.routing.util.FlagEncoder; +import com.graphhopper.routing.weighting.FastestWeighting; import com.graphhopper.storage.*; +import org.junit.Test; import static org.junit.Assert.*; -import org.junit.Test; - /** * @author Peter Karich */ -public class GHUtilityTest -{ +public class GHUtilityTest { private final FlagEncoder carEncoder = new CarFlagEncoder(); private final EncodingManager encodingManager = new EncodingManager(carEncoder); - Graph createGraph() - { + Graph createGraph() { return new GraphBuilder(encodingManager).create(); } @@ -47,8 +44,7 @@ Graph createGraph() // 6 \1 // ______/ // 0/ - Graph initUnsorted( Graph g ) - { + Graph initUnsorted(Graph g) { NodeAccess na = g.getNodeAccess(); na.setNode(0, 0, 1); na.setNode(1, 2.5, 4.5); @@ -69,8 +65,7 @@ Graph initUnsorted( Graph g ) } @Test - public void testSort() - { + public void testSort() { Graph g = initUnsorted(createGraph()); Graph newG = GHUtility.sortDFS(g, createGraph()); assertEquals(g.getNodes(), newG.getNodes()); @@ -85,8 +80,7 @@ public void testSort() } @Test - public void testSort2() - { + public void testSort2() { Graph g = initUnsorted(createGraph()); Graph newG = GHUtility.sortDFS(g, createGraph()); assertEquals(g.getNodes(), newG.getNodes()); @@ -98,8 +92,7 @@ public void testSort2() } @Test - public void testSortDirected() - { + public void testSortDirected() { Graph g = createGraph(); NodeAccess na = g.getNodeAccess(); na.setNode(0, 0, 1); @@ -111,8 +104,7 @@ public void testSortDirected() } @Test - public void testCopyWithSelfRef() - { + public void testCopyWithSelfRef() { Graph g = initUnsorted(createGraph()); EdgeIteratorState eb = g.edge(0, 0, 11, true); @@ -123,8 +115,7 @@ public void testCopyWithSelfRef() } @Test - public void testCopy() - { + public void testCopy() { Graph g = initUnsorted(createGraph()); EdgeIteratorState edgeState = g.edge(6, 5, 11, true); edgeState.setWayGeometry(Helper.createPointList(12, 10, -1, 3)); @@ -167,8 +158,7 @@ public void testCopy() } @Test - public void testEdgeStuff() - { + public void testEdgeStuff() { assertEquals(6, GHUtility.createEdgeKey(1, 2, 3, false)); assertEquals(7, GHUtility.createEdgeKey(2, 1, 3, false)); assertEquals(7, GHUtility.createEdgeKey(1, 2, 3, true)); diff --git a/core/src/test/java/com/graphhopper/util/HelperTest.java b/core/src/test/java/com/graphhopper/util/HelperTest.java index 6a674ef42bf..c3768c5cedd 100644 --- a/core/src/test/java/com/graphhopper/util/HelperTest.java +++ b/core/src/test/java/com/graphhopper/util/HelperTest.java @@ -17,6 +17,10 @@ */ package com.graphhopper.util; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + import java.io.File; import java.io.IOException; import java.io.StringReader; @@ -24,40 +28,31 @@ import java.util.Locale; import java.util.Map; -import org.junit.After; -import org.junit.Test; - -import static org.junit.Assert.*; - -import org.junit.Before; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; /** * @author Peter Karich */ -public class HelperTest -{ +public class HelperTest { @Before - public void setUp() - { + public void setUp() { Helper.removeDir(new File("test")); } @After - public void tearDown() - { + public void tearDown() { Helper.removeDir(new File("test")); } @Test - public void testCountBitValue() throws Exception - { + public void testCountBitValue() throws Exception { assertEquals(2, Helper.countBitValue(4)); assertEquals(5, Helper.countBitValue(20)); } @Test - public void testUnzip() throws Exception - { + public void testUnzip() throws Exception { String to = "./target/tmp/test"; Helper.removeDir(new File(to)); new Unzipper().unzip("./src/test/resources/com/graphhopper/util/test.zip", to, false); @@ -68,8 +63,7 @@ public void testUnzip() throws Exception } @Test - public void testGetLocale() throws Exception - { + public void testGetLocale() throws Exception { assertEquals(Locale.GERMAN, Helper.getLocale("de")); assertEquals(Locale.GERMANY, Helper.getLocale("de_DE")); assertEquals(Locale.GERMANY, Helper.getLocale("de-DE")); @@ -79,24 +73,21 @@ public void testGetLocale() throws Exception } @Test - public void testRound() - { + public void testRound() { assertEquals(100.94, Helper.round(100.94, 2), 1e-7); assertEquals(100.9, Helper.round(100.94, 1), 1e-7); assertEquals(101.0, Helper.round(100.95, 1), 1e-7); } @Test - public void testKeepIn() - { + public void testKeepIn() { assertEquals(2, Helper.keepIn(2, 1, 4), 1e-2); assertEquals(3, Helper.keepIn(2, 3, 4), 1e-2); assertEquals(3, Helper.keepIn(-2, 3, 4), 1e-2); } @Test - public void testLoadProperties() throws IOException - { + public void testLoadProperties() throws IOException { Map map = new HashMap(); Helper.loadProperties(map, new StringReader("blup=test\n blup2 = xy")); assertEquals("test", map.get("blup")); @@ -104,8 +95,7 @@ public void testLoadProperties() throws IOException } @Test - public void testUnsignedConversions() - { + public void testUnsignedConversions() { long l = Helper.toUnsignedLong(-1); assertEquals(4294967295L, l); assertEquals(-1, Helper.toSignedInt(l)); @@ -127,18 +117,16 @@ public void testUnsignedConversions() } @Test - public void testCamelCaseToUnderscore() - { + public void testCamelCaseToUnderscore() { assertEquals("test_case", Helper.camelCaseToUnderScore("testCase")); assertEquals("test_case_t_b_d", Helper.camelCaseToUnderScore("testCaseTBD")); assertEquals("_test_case", Helper.camelCaseToUnderScore("TestCase")); - + assertEquals("_test_case", Helper.camelCaseToUnderScore("_test_case")); } @Test - public void testUnderscoreToCamelCase() - { + public void testUnderscoreToCamelCase() { assertEquals("testCase", Helper.underScoreToCamelCase("test_case")); assertEquals("testCaseTBD", Helper.underScoreToCamelCase("test_case_t_b_d")); assertEquals("TestCase_", Helper.underScoreToCamelCase("_test_case_")); diff --git a/core/src/test/java/com/graphhopper/util/InstructionListTest.java b/core/src/test/java/com/graphhopper/util/InstructionListTest.java index feb7a1a4a4a..282cd39f0ae 100644 --- a/core/src/test/java/com/graphhopper/util/InstructionListTest.java +++ b/core/src/test/java/com/graphhopper/util/InstructionListTest.java @@ -17,40 +17,37 @@ */ package com.graphhopper.util; -import com.graphhopper.routing.weighting.ShortestWeighting; import com.graphhopper.reader.ReaderWay; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; -import java.util.Locale; - import com.graphhopper.routing.Dijkstra; import com.graphhopper.routing.Path; -import com.graphhopper.routing.util.*; -import com.graphhopper.storage.*; +import com.graphhopper.routing.util.CarFlagEncoder; +import com.graphhopper.routing.util.EncodingManager; +import com.graphhopper.routing.util.FlagEncoder; +import com.graphhopper.routing.util.TraversalMode; +import com.graphhopper.routing.weighting.ShortestWeighting; +import com.graphhopper.storage.Graph; +import com.graphhopper.storage.GraphBuilder; +import com.graphhopper.storage.NodeAccess; +import org.json.JSONObject; +import org.junit.Before; +import org.junit.Test; +import org.xml.sax.SAXException; -import java.io.*; -import java.util.*; import javax.xml.XMLConstants; import javax.xml.transform.Source; import javax.xml.transform.stream.StreamSource; import javax.xml.validation.Schema; import javax.xml.validation.SchemaFactory; import javax.xml.validation.Validator; - -import org.json.JSONObject; -import org.junit.Test; +import java.io.StringReader; +import java.util.*; import static org.junit.Assert.*; -import org.junit.Before; -import org.xml.sax.SAXException; - /** * @author Peter Karich */ -public class InstructionListTest -{ +public class InstructionListTest { private final TranslationMap trMap = TranslationMapTest.SINGLETON; private final Translation usTR = trMap.getWithFallBack(Locale.US); private final TraversalMode tMode = TraversalMode.NODE_BASED; @@ -58,16 +55,14 @@ public class InstructionListTest private FlagEncoder carEncoder; @Before - public void setUp() - { + public void setUp() { carEncoder = new CarFlagEncoder(); carManager = new EncodingManager(carEncoder); } @SuppressWarnings("unchecked") @Test - public void testWayList() - { + public void testWayList() { Graph g = new GraphBuilder(carManager).create(); // 0-1-2 // | | | @@ -162,22 +157,18 @@ public void testWayList() assertEquals("Finish!", wayList.get(0).getTurnDescription(usTR)); } - List pick( String key, List> instructionJson ) - { + List pick(String key, List> instructionJson) { List list = new ArrayList(); - for (Map json : instructionJson) - { + for (Map json : instructionJson) { list.add(json.get(key).toString()); } return list; } - List> createList( PointList pl, List integs ) - { + List> createList(PointList pl, List integs) { List> list = new ArrayList>(); - for (int i : integs) - { + for (int i : integs) { List entryList = new ArrayList(2); entryList.add(pl.getLatitude(i)); entryList.add(pl.getLongitude(i)); @@ -186,37 +177,30 @@ List> createList( PointList pl, List integs ) return list; } - void compare( List> expected, List> was ) - { - for (int i = 0; i < expected.size(); i++) - { + void compare(List> expected, List> was) { + for (int i = 0; i < expected.size(); i++) { List e = expected.get(i); List wasE = was.get(i); - for (int j = 0; j < e.size(); j++) - { + for (int j = 0; j < e.size(); j++) { assertEquals("at " + j + " value " + e + " vs " + wasE, e.get(j), wasE.get(j), 1e-5d); } } } - List asL( Double... list ) - { + List asL(Double... list) { return Arrays.asList(list); } - double sumDistances( InstructionList il ) - { + double sumDistances(InstructionList il) { double val = 0; - for (Instruction i : il) - { + for (Instruction i : il) { val += i.getDistance(); } return val; } @Test - public void testWayList2() - { + public void testWayList2() { Graph g = new GraphBuilder(carManager).create(); // 2 // \. 5 @@ -254,8 +238,7 @@ public void testWayList2() // problem: we normally don't want instructions if streetname stays but here it is suboptimal: @Test - public void testNoInstructionIfSameStreet() - { + public void testNoInstructionIfSameStreet() { Graph g = new GraphBuilder(carManager).create(); // 2 // \. 5 @@ -284,8 +267,7 @@ public void testNoInstructionIfSameStreet() } @Test - public void testInstructionsWithTimeAndPlace() - { + public void testInstructionsWithTimeAndPlace() { Graph g = new GraphBuilder(carManager).create(); // 4-5 // | @@ -348,8 +330,7 @@ public void testInstructionsWithTimeAndPlace() } @Test - public void testRoundaboutJsonIntegrity() - { + public void testRoundaboutJsonIntegrity() { InstructionList il = new InstructionList(usTR); PointList pl = new PointList(); @@ -375,8 +356,7 @@ public void testRoundaboutJsonIntegrity() // Roundabout with unknown dir of rotation @Test - public void testRoundaboutJsonNaN() - { + public void testRoundaboutJsonNaN() { InstructionList il = new InstructionList(usTR); PointList pl = new PointList(); @@ -398,16 +378,13 @@ public void testRoundaboutJsonNaN() } @Test - public void testCreateGPXWithEle() - { + public void testCreateGPXWithEle() { final List fakeList = new ArrayList(); fakeList.add(new GPXEntry(12, 13, 0)); fakeList.add(new GPXEntry(12.5, 13, 1000)); - InstructionList il = new InstructionList(usTR) - { + InstructionList il = new InstructionList(usTR) { @Override - public List createGPXList() - { + public List createGPXList() { return fakeList; } }; @@ -426,8 +403,7 @@ public List createGPXList() } @Test - public void testCreateGPX() - { + public void testCreateGPX() { InstructionAnnotation ea = InstructionAnnotation.EMPTY; InstructionList instructions = new InstructionList(usTR); PointList pl = new PointList(); @@ -456,8 +432,7 @@ public void testCreateGPX() verifyGPX(instructions.createGPX()); } - private long flagsForSpeed( EncodingManager encodingManager, int speedKmPerHour ) - { + private long flagsForSpeed(EncodingManager encodingManager, int speedKmPerHour) { ReaderWay way = new ReaderWay(1); way.setTag("highway", "motorway"); way.setTag("maxspeed", String.format("%d km/h", speedKmPerHour)); @@ -465,8 +440,7 @@ private long flagsForSpeed( EncodingManager encodingManager, int speedKmPerHour } @Test - public void testEmptyList() - { + public void testEmptyList() { Graph g = new GraphBuilder(carManager).create(); Path p = new Dijkstra(g, carEncoder, new ShortestWeighting(carEncoder), tMode).calcPath(0, 1); InstructionList il = p.calcInstructions(usTR); @@ -474,33 +448,27 @@ public void testEmptyList() assertEquals(0, il.createStartPoints().size()); } - public void verifyGPX( String gpx ) - { + public void verifyGPX(String gpx) { SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); Schema schema = null; - try - { + try { Source schemaFile = new StreamSource(getClass().getResourceAsStream("gpx-schema.xsd")); schema = schemaFactory.newSchema(schemaFile); // using more schemas: http://stackoverflow.com/q/1094893/194609 - } catch (SAXException e1) - { + } catch (SAXException e1) { throw new IllegalStateException("There was a problem with the schema supplied for validation. Message:" + e1.getMessage()); } Validator validator = schema.newValidator(); - try - { + try { validator.validate(new StreamSource(new StringReader(gpx))); - } catch (Exception e) - { + } catch (Exception e) { throw new RuntimeException(e); } } @Test - public void testFind() - { + public void testFind() { Graph g = new GraphBuilder(carManager).create(); // n-4-5 (n: pillar node) // | @@ -538,8 +506,7 @@ public void testFind() } @Test - public void testXMLEscape_issue572() - { + public void testXMLEscape_issue572() { assertEquals("_", InstructionList.simpleXMLEscape("<")); assertEquals("_blup_", InstructionList.simpleXMLEscape("")); assertEquals("a&b", InstructionList.simpleXMLEscape("a&b")); diff --git a/core/src/test/java/com/graphhopper/util/InstructionTest.java b/core/src/test/java/com/graphhopper/util/InstructionTest.java index 5033d515afe..964568ab119 100644 --- a/core/src/test/java/com/graphhopper/util/InstructionTest.java +++ b/core/src/test/java/com/graphhopper/util/InstructionTest.java @@ -19,16 +19,15 @@ import org.junit.Test; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; /** * @author Johannes Pelzer */ -public class InstructionTest -{ +public class InstructionTest { @Test - public void testCalcAzimuthAndGetDirection() - { + public void testCalcAzimuthAndGetDirection() { InstructionAnnotation ea = InstructionAnnotation.EMPTY; PointList pl = new PointList(); pl.add(49.942, 11.584); diff --git a/core/src/test/java/com/graphhopper/util/PMapTest.java b/core/src/test/java/com/graphhopper/util/PMapTest.java index 0372f4b2344..1487894e967 100644 --- a/core/src/test/java/com/graphhopper/util/PMapTest.java +++ b/core/src/test/java/com/graphhopper/util/PMapTest.java @@ -20,24 +20,19 @@ import org.junit.Assert; import org.junit.Test; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; +import static org.junit.Assert.*; -public class PMapTest -{ +public class PMapTest { @Test - public void singleStringPropertyCanBeRetrieved() - { + public void singleStringPropertyCanBeRetrieved() { PMap subject = new PMap("foo=bar"); Assert.assertEquals("bar", subject.get("foo")); } @Test - public void propertyFromStringWithMultiplePropertiesCanBeRetrieved() - { + public void propertyFromStringWithMultiplePropertiesCanBeRetrieved() { PMap subject = new PMap("foo=valueA|bar=valueB"); Assert.assertEquals("valueA", subject.get("foo", "")); @@ -45,8 +40,7 @@ public void propertyFromStringWithMultiplePropertiesCanBeRetrieved() } @Test - public void keyCannotHaveAnyCasing() - { + public void keyCannotHaveAnyCasing() { PMap subject = new PMap("foo=valueA|bar=valueB"); assertEquals("valueA", subject.get("foo", "")); @@ -54,24 +48,21 @@ public void keyCannotHaveAnyCasing() } @Test - public void numericPropertyCanBeRetrievedAsLong() - { + public void numericPropertyCanBeRetrievedAsLong() { PMap subject = new PMap("foo=1234|bar=5678"); assertEquals(1234L, subject.getLong("foo", 0)); } @Test - public void numericPropertyCanBeRetrievedAsDouble() - { + public void numericPropertyCanBeRetrievedAsDouble() { PMap subject = new PMap("foo=123.45|bar=56.78"); assertEquals(123.45, subject.getDouble("foo", 0), 1e-4); } @Test - public void hasReturnsCorrectResult() - { + public void hasReturnsCorrectResult() { PMap subject = new PMap("foo=123.45|bar=56.78"); assertTrue(subject.has("foo")); diff --git a/core/src/test/java/com/graphhopper/util/PointListTest.java b/core/src/test/java/com/graphhopper/util/PointListTest.java index 18e603e033c..fa1c91c7adf 100644 --- a/core/src/test/java/com/graphhopper/util/PointListTest.java +++ b/core/src/test/java/com/graphhopper/util/PointListTest.java @@ -20,16 +20,14 @@ import com.graphhopper.util.shapes.GHPoint; import org.junit.Test; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; /** * @author Peter Karich */ -public class PointListTest -{ +public class PointListTest { @Test - public void testEquals() - { + public void testEquals() { assertEquals(Helper.createPointList(), PointList.EMPTY); PointList list1 = Helper.createPointList(38.5, -120.2, 43.252, -126.453, 40.7, -120.95, 50.3139, 10.612793, 50.04303, 9.497681); @@ -39,8 +37,7 @@ public void testEquals() } @Test - public void testReverse() - { + public void testReverse() { PointList instance = new PointList(); instance.add(1, 1); instance.reverse(); @@ -58,11 +55,9 @@ public void testReverse() } @Test - public void testAddPL() - { + public void testAddPL() { PointList instance = new PointList(); - for (int i = 0; i < 7; i++) - { + for (int i = 0; i < 7; i++) { instance.add(0, 0); } assertEquals(7, instance.getSize()); @@ -83,22 +78,19 @@ public void testAddPL() assertEquals(12, instance.getSize()); assertEquals(24, instance.getCapacity()); - for (int i = 0; i < toAdd.size(); i++) - { + for (int i = 0; i < toAdd.size(); i++) { assertEquals(toAdd.getLatitude(i), instance.getLatitude(7 + i), 1e-1); } } @Test - public void testIterable() - { + public void testIterable() { PointList toAdd = new PointList(); toAdd.add(1, 1); toAdd.add(2, 2); toAdd.add(3, 3); int counter = 0; - for (GHPoint point : toAdd) - { + for (GHPoint point : toAdd) { counter++; assertEquals(counter, point.getLat(), 0.1); } diff --git a/core/src/test/java/com/graphhopper/util/SimpleIntDequeTest.java b/core/src/test/java/com/graphhopper/util/SimpleIntDequeTest.java index 68b16f0cc2c..3e2fc1655f9 100644 --- a/core/src/test/java/com/graphhopper/util/SimpleIntDequeTest.java +++ b/core/src/test/java/com/graphhopper/util/SimpleIntDequeTest.java @@ -19,24 +19,22 @@ import org.junit.Test; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; /** * @author Peter Karich */ -public class SimpleIntDequeTest -{ +public class SimpleIntDequeTest { @Test - public void testSmall() - { + public void testSmall() { SimpleIntDeque deque = new SimpleIntDeque(8, 2f); assertTrue(deque.isEmpty()); assertEquals(0, deque.getSize()); } @Test - public void testEmpty() - { + public void testEmpty() { SimpleIntDeque deque = new SimpleIntDeque(1, 2f); deque.push(1); assertEquals(1, deque.getSize()); @@ -47,12 +45,10 @@ public void testEmpty() } @Test - public void testPush() - { + public void testPush() { SimpleIntDeque deque = new SimpleIntDeque(8, 2f); - for (int i = 0; i < 60; i++) - { + for (int i = 0; i < 60; i++) { deque.push(i); assertEquals(i + 1, deque.getSize()); } @@ -70,8 +66,7 @@ public void testPush() deque.push(3); assertEquals(60, deque.getSize()); - for (int i = 0; i < 50; i++) - { + for (int i = 0; i < 50; i++) { assertEquals(i + 2, deque.pop()); } diff --git a/core/src/test/java/com/graphhopper/util/TranslationMapTest.java b/core/src/test/java/com/graphhopper/util/TranslationMapTest.java index bdc0f86faed..fe60923295d 100644 --- a/core/src/test/java/com/graphhopper/util/TranslationMapTest.java +++ b/core/src/test/java/com/graphhopper/util/TranslationMapTest.java @@ -17,23 +17,22 @@ */ package com.graphhopper.util; -import java.util.Locale; - import org.junit.Test; -import static org.junit.Assert.*; +import java.util.Locale; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; /** * @author Peter Karich */ -public class TranslationMapTest -{ +public class TranslationMapTest { // use a static singleton to parse the I18N files only once per test execution public final static TranslationMap SINGLETON = new TranslationMap().doImport(); @Test - public void testToString() - { + public void testToString() { Translation enMap = SINGLETON.getWithFallBack(Locale.UK); assertEquals("continue onto blp street", enMap.tr("continue_onto", "blp street")); @@ -64,8 +63,7 @@ public void testToString() } @Test - public void testToRoundaboutString() - { + public void testToRoundaboutString() { Translation ptMap = SINGLETON.get("pt"); assertTrue(ptMap.tr("roundabout_exit_onto", "1", "somestreet").contains("somestreet")); } diff --git a/core/src/test/java/com/graphhopper/util/shapes/BBoxTest.java b/core/src/test/java/com/graphhopper/util/shapes/BBoxTest.java index 0551aa3f9db..81dbd4f8c9a 100644 --- a/core/src/test/java/com/graphhopper/util/shapes/BBoxTest.java +++ b/core/src/test/java/com/graphhopper/util/shapes/BBoxTest.java @@ -19,18 +19,16 @@ import com.graphhopper.util.DistanceCalc; import com.graphhopper.util.DistanceCalcEarth; -import org.junit.*; +import org.junit.Test; import static org.junit.Assert.*; /** * @author Peter Karich */ -public class BBoxTest -{ +public class BBoxTest { @Test - public void testCreate() - { + public void testCreate() { DistanceCalc c = new DistanceCalcEarth(); BBox b = c.createBBox(52, 10, 100000); @@ -48,8 +46,7 @@ public void testCreate() } @Test - public void testContains() - { + public void testContains() { assertTrue(new BBox(1, 2, 0, 1).contains(new BBox(1, 2, 0, 1))); assertTrue(new BBox(1, 2, 0, 1).contains(new BBox(1.5, 2, 0.5, 1))); assertFalse(new BBox(1, 2, 0, 0.5).contains(new BBox(1.5, 2, 0.5, 1))); @@ -60,8 +57,7 @@ public void testContains() } @Test - public void testIntersect() - { + public void testIntersect() { // --- // | | // --------- @@ -86,49 +82,40 @@ public void testIntersect() } @Test - public void testBasicJavaOverload() - { - new BBox(2, 4, 0, 1) - { + public void testBasicJavaOverload() { + new BBox(2, 4, 0, 1) { @Override - public boolean intersect( Circle c ) - { + public boolean intersect(Circle c) { assertTrue(true); return super.intersect(c); } @Override - public boolean intersect( Shape c ) - { + public boolean intersect(Shape c) { assertTrue(false); return true; } @Override - public boolean intersect( BBox c ) - { + public boolean intersect(BBox c) { assertTrue(false); return true; } - }.intersect(new Circle(1, 2, 3) - { + }.intersect(new Circle(1, 2, 3) { @Override - public boolean intersect( Circle c ) - { + public boolean intersect(Circle c) { assertTrue(false); return true; } @Override - public boolean intersect( Shape b ) - { + public boolean intersect(Shape b) { assertTrue(false); return true; } @Override - public boolean intersect( BBox b ) - { + public boolean intersect(BBox b) { assertTrue(true); return true; } diff --git a/core/src/test/java/com/graphhopper/util/shapes/CircleTest.java b/core/src/test/java/com/graphhopper/util/shapes/CircleTest.java index 0a4161f6c5f..ed63c1708d0 100644 --- a/core/src/test/java/com/graphhopper/util/shapes/CircleTest.java +++ b/core/src/test/java/com/graphhopper/util/shapes/CircleTest.java @@ -17,25 +17,23 @@ */ package com.graphhopper.util.shapes; -import org.junit.*; +import org.junit.Test; -import static org.junit.Assert.*; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; /** * @author Peter Karich */ -public class CircleTest -{ +public class CircleTest { @Test - public void testIntersectCircleCircle() - { + public void testIntersectCircleCircle() { assertTrue(new Circle(0, 0, 80000).intersect(new Circle(1, 1, 80000))); assertFalse(new Circle(0, 0, 75000).intersect(new Circle(1, 1, 80000))); } @Test - public void testIntersectCircleBBox() - { + public void testIntersectCircleBBox() { assertTrue(new Circle(10, 10, 120000).intersect(new BBox(9, 11, 8, 9))); assertTrue(new BBox(9, 11, 8, 9).intersect(new Circle(10, 10, 120000))); @@ -44,8 +42,7 @@ public void testIntersectCircleBBox() } @Test - public void testContains() - { + public void testContains() { Circle c = new Circle(10, 10, 120000); assertTrue(c.contains(new BBox(9, 11, 10, 10.1))); assertFalse(c.contains(new BBox(9, 11, 8, 9))); @@ -53,8 +50,7 @@ public void testContains() } @Test - public void testContainsCircle() - { + public void testContainsCircle() { Circle c = new Circle(10, 10, 120000); assertTrue(c.contains(new Circle(9.9, 10.2, 90000))); assertFalse(c.contains(new Circle(10, 10.4, 90000))); diff --git a/core/src/test/java/com/graphhopper/util/shapes/CoordTrigTest.java b/core/src/test/java/com/graphhopper/util/shapes/CoordTrigTest.java index 774499a3d33..f21c6be4501 100644 --- a/core/src/test/java/com/graphhopper/util/shapes/CoordTrigTest.java +++ b/core/src/test/java/com/graphhopper/util/shapes/CoordTrigTest.java @@ -17,14 +17,9 @@ */ package com.graphhopper.util.shapes; -import org.junit.Test; - -import static org.junit.Assert.*; - /** * @author Peter Karich */ -public class CoordTrigTest -{ +public class CoordTrigTest { } diff --git a/core/src/test/java/com/graphhopper/util/shapes/GHPoint3DTest.java b/core/src/test/java/com/graphhopper/util/shapes/GHPoint3DTest.java index a54231519e2..f423a4388d3 100644 --- a/core/src/test/java/com/graphhopper/util/shapes/GHPoint3DTest.java +++ b/core/src/test/java/com/graphhopper/util/shapes/GHPoint3DTest.java @@ -19,16 +19,15 @@ import org.junit.Test; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; /** * @author Peter Karich */ -public class GHPoint3DTest -{ +public class GHPoint3DTest { @Test - public void testEquals() - { + public void testEquals() { GHPoint3D point1 = new GHPoint3D(1, 2, Double.NaN); GHPoint3D point2 = new GHPoint3D(1, 2, Double.NaN); assertEquals(point1, point2); diff --git a/core/src/test/java/com/graphhopper/util/shapes/GHPointTest.java b/core/src/test/java/com/graphhopper/util/shapes/GHPointTest.java index 276a49aa180..278251f37fb 100644 --- a/core/src/test/java/com/graphhopper/util/shapes/GHPointTest.java +++ b/core/src/test/java/com/graphhopper/util/shapes/GHPointTest.java @@ -19,16 +19,15 @@ import org.junit.Test; -import static org.junit.Assert.*; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; /** * @author Peter Karich */ -public class GHPointTest -{ +public class GHPointTest { @Test - public void testIsValid() - { + public void testIsValid() { GHPoint instance = new GHPoint(); assertFalse(instance.isValid()); instance.lat = 1; diff --git a/pom.xml b/pom.xml index 4e76163466c..825d5534663 100644 --- a/pom.xml +++ b/pom.xml @@ -17,28 +17,23 @@ UTF-8 1.7.21 1.2.17 - 20160212 - - - project + 20160212 + + 4 4 8 - 4 - true 100 - none - 0 - true - NEW_LINE + true + project LEAVE_ALONE - NEW_LINE - NEW_LINE - true - true - 2 - * + true + true + 0 + false + false true - true + true + true diff --git a/reader-osm/src/main/java/com/graphhopper/reader/osm/GraphHopperOSM.java b/reader-osm/src/main/java/com/graphhopper/reader/osm/GraphHopperOSM.java index 6f5491ff068..0c8e47ff70d 100644 --- a/reader-osm/src/main/java/com/graphhopper/reader/osm/GraphHopperOSM.java +++ b/reader-osm/src/main/java/com/graphhopper/reader/osm/GraphHopperOSM.java @@ -26,26 +26,22 @@ * * @author Peter Karich */ -public class GraphHopperOSM extends GraphHopper -{ +public class GraphHopperOSM extends GraphHopper { @Override - protected DataReader createReader( GraphHopperStorage ghStorage ) - { + protected DataReader createReader(GraphHopperStorage ghStorage) { return initDataReader(new OSMReader(ghStorage)); } + public String getOSMFile() { + return getDataReaderFile(); + } + /** * This file can be an osm xml (.osm), a compressed xml (.osm.zip or .osm.gz) or a protobuf file * (.pbf). */ - public GraphHopperOSM setOSMFile( String osmFileStr ) - { + public GraphHopperOSM setOSMFile(String osmFileStr) { super.setDataReaderFile(osmFileStr); return this; } - - public String getOSMFile() - { - return getDataReaderFile(); - } } diff --git a/reader-osm/src/main/java/com/graphhopper/reader/osm/OSMFileHeader.java b/reader-osm/src/main/java/com/graphhopper/reader/osm/OSMFileHeader.java index 23eeed03b59..1445416080d 100644 --- a/reader-osm/src/main/java/com/graphhopper/reader/osm/OSMFileHeader.java +++ b/reader-osm/src/main/java/com/graphhopper/reader/osm/OSMFileHeader.java @@ -18,6 +18,7 @@ package com.graphhopper.reader.osm; import com.graphhopper.reader.ReaderElement; + import javax.xml.stream.XMLStreamConstants; import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamReader; @@ -25,37 +26,32 @@ /** * Represents an OSM file header *

    + * * @author ratrun */ -public class OSMFileHeader extends ReaderElement -{ +public class OSMFileHeader extends ReaderElement { + public OSMFileHeader() { + super(0, FILEHEADER); + } + /** * Constructor for XML Parser */ - public static OSMFileHeader create( long id, XMLStreamReader parser ) throws XMLStreamException - { + public static OSMFileHeader create(long id, XMLStreamReader parser) throws XMLStreamException { OSMFileHeader header = new OSMFileHeader(); parser.nextTag(); return header; } - public OSMFileHeader() - { - super(0, FILEHEADER); - } - - protected void readFileHeader( XMLStreamReader parser ) throws XMLStreamException - { + protected void readFileHeader(XMLStreamReader parser) throws XMLStreamException { int event = parser.getEventType(); - while (event != XMLStreamConstants.END_DOCUMENT && parser.getLocalName().equals("osm")) - { + while (event != XMLStreamConstants.END_DOCUMENT && parser.getLocalName().equals("osm")) { event = parser.nextTag(); } } @Override - public String toString() - { + public String toString() { return "OSM File header:" + super.toString(); } } diff --git a/reader-osm/src/main/java/com/graphhopper/reader/osm/OSMInputFile.java b/reader-osm/src/main/java/com/graphhopper/reader/osm/OSMInputFile.java index c00483155b2..b62830f9c44 100644 --- a/reader-osm/src/main/java/com/graphhopper/reader/osm/OSMInputFile.java +++ b/reader-osm/src/main/java/com/graphhopper/reader/osm/OSMInputFile.java @@ -17,9 +17,9 @@ */ package com.graphhopper.reader.osm; -import com.graphhopper.reader.*; -import com.graphhopper.reader.osm.pbf.Sink; +import com.graphhopper.reader.ReaderElement; import com.graphhopper.reader.osm.pbf.PbfReader; +import com.graphhopper.reader.osm.pbf.Sink; import javax.xml.stream.XMLInputFactory; import javax.xml.stream.XMLStreamConstants; @@ -36,34 +36,31 @@ /** * A readable OSM file. *

    + * * @author Nop */ -public class OSMInputFile implements Sink, Closeable -{ - private boolean eof; +public class OSMInputFile implements Sink, Closeable { private final InputStream bis; + private final BlockingQueue itemQueue; + Thread pbfReaderThread; + private boolean eof; // for xml parsing private XMLStreamReader parser; // for pbf parsing private boolean binary = false; - private final BlockingQueue itemQueue; private boolean hasIncomingData; private int workerThreads = -1; private OSMFileHeader fileheader; - public OSMInputFile( File file ) throws IOException - { + public OSMInputFile(File file) throws IOException { bis = decode(file); itemQueue = new LinkedBlockingQueue(50000); } - public OSMInputFile open() throws XMLStreamException - { - if (binary) - { + public OSMInputFile open() throws XMLStreamException { + if (binary) { openPBFReader(bis); - } else - { + } else { openXMLStream(bis); } return this; @@ -72,23 +69,19 @@ public OSMInputFile open() throws XMLStreamException /** * Currently on for pbf format. Default is number of cores. */ - public OSMInputFile setWorkerThreads( int num ) - { + public OSMInputFile setWorkerThreads(int num) { workerThreads = num; return this; } @SuppressWarnings("unchecked") - private InputStream decode( File file ) throws IOException - { + private InputStream decode(File file) throws IOException { final String name = file.getName(); InputStream ips = null; - try - { + try { ips = new BufferedInputStream(new FileInputStream(file), 50000); - } catch (FileNotFoundException e) - { + } catch (FileNotFoundException e) { throw new RuntimeException(e); } ips.mark(10); @@ -104,56 +97,46 @@ private InputStream decode( File file ) throws IOException return new CBZip2InputStream(ips); } */ - if (header[0] == 31 && header[1] == -117) - { + if (header[0] == 31 && header[1] == -117) { ips.reset(); return new GZIPInputStream(ips, 50000); } else if (header[0] == 0 && header[1] == 0 && header[2] == 0 && header[4] == 10 && header[5] == 9 - && (header[3] == 13 || header[3] == 14)) - { + && (header[3] == 13 || header[3] == 14)) { ips.reset(); binary = true; return ips; - } else if (header[0] == 'P' && header[1] == 'K') - { + } else if (header[0] == 'P' && header[1] == 'K') { ips.reset(); ZipInputStream zip = new ZipInputStream(ips); zip.getNextEntry(); return zip; - } else if (name.endsWith(".osm") || name.endsWith(".xml")) - { + } else if (name.endsWith(".osm") || name.endsWith(".xml")) { ips.reset(); return ips; - } else if (name.endsWith(".bz2") || name.endsWith(".bzip2")) - { + } else if (name.endsWith(".bz2") || name.endsWith(".bzip2")) { String clName = "org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream"; - try - { + try { Class clazz = Class.forName(clName); ips.reset(); Constructor ctor = clazz.getConstructor(InputStream.class, boolean.class); return ctor.newInstance(ips, true); - } catch (Exception e) - { + } catch (Exception e) { throw new IllegalArgumentException("Cannot instantiate " + clName, e); } - } else - { + } else { throw new IllegalArgumentException("Input file is not of valid type " + file.getPath()); } } - private void openXMLStream( InputStream in ) - throws XMLStreamException - { + private void openXMLStream(InputStream in) + throws XMLStreamException { XMLInputFactory factory = XMLInputFactory.newInstance(); parser = factory.createXMLStreamReader(in, "UTF-8"); int event = parser.next(); - if (event != XMLStreamConstants.START_ELEMENT || !parser.getLocalName().equalsIgnoreCase("osm")) - { + if (event != XMLStreamConstants.START_ELEMENT || !parser.getLocalName().equalsIgnoreCase("osm")) { throw new IllegalArgumentException("File is not a valid OSM stream"); } // See https://wiki.openstreetmap.org/wiki/PBF_Format#Definition_of_the_OSMHeader_fileblock @@ -162,22 +145,18 @@ private void openXMLStream( InputStream in ) if (timestamp == null) timestamp = parser.getAttributeValue(null, "timestamp"); - if (timestamp != null) - { - try - { + if (timestamp != null) { + try { fileheader = new OSMFileHeader(); fileheader.setTag("timestamp", timestamp); - } catch (Exception ex) - { + } catch (Exception ex) { } } eof = false; } - public ReaderElement getNext() throws XMLStreamException - { + public ReaderElement getNext() throws XMLStreamException { if (eof) throw new IllegalStateException("EOF reached"); @@ -194,39 +173,31 @@ public ReaderElement getNext() throws XMLStreamException return null; } - private ReaderElement getNextXML() throws XMLStreamException - { + private ReaderElement getNextXML() throws XMLStreamException { int event = parser.next(); - if (fileheader != null) - { + if (fileheader != null) { ReaderElement copyfileheader = fileheader; fileheader = null; return copyfileheader; } - while (event != XMLStreamConstants.END_DOCUMENT) - { - if (event == XMLStreamConstants.START_ELEMENT) - { + while (event != XMLStreamConstants.END_DOCUMENT) { + if (event == XMLStreamConstants.START_ELEMENT) { String idStr = parser.getAttributeValue(null, "id"); - if (idStr != null) - { + if (idStr != null) { String name = parser.getLocalName(); long id = 0; - switch (name.charAt(0)) - { + switch (name.charAt(0)) { case 'n': // note vs. node - if ("node".equals(name)) - { + if ("node".equals(name)) { id = Long.parseLong(idStr); return OSMXMLHelper.createNode(id, parser); } break; - case 'w': - { + case 'w': { id = Long.parseLong(idStr); return OSMXMLHelper.createWay(id, parser); } @@ -242,23 +213,18 @@ private ReaderElement getNextXML() throws XMLStreamException return null; } - public boolean isEOF() - { + public boolean isEOF() { return eof; } @Override - public void close() throws IOException - { - try - { + public void close() throws IOException { + try { if (!binary) parser.close(); - } catch (XMLStreamException ex) - { + } catch (XMLStreamException ex) { throw new IOException(ex); - } finally - { + } finally { eof = true; bis.close(); // if exception happend on OSMInputFile-thread we need to shutdown the pbf handling @@ -267,10 +233,7 @@ public void close() throws IOException } } - Thread pbfReaderThread; - - private void openPBFReader( InputStream stream ) - { + private void openPBFReader(InputStream stream) { hasIncomingData = true; if (workerThreads <= 0) workerThreads = 2; @@ -281,14 +244,11 @@ private void openPBFReader( InputStream stream ) } @Override - public void process( ReaderElement item ) - { - try - { + public void process(ReaderElement item) { + try { // blocks if full itemQueue.put(item); - } catch (InterruptedException ex) - { + } catch (InterruptedException ex) { throw new RuntimeException(ex); } @@ -297,29 +257,23 @@ public void process( ReaderElement item ) } @Override - public void complete() - { + public void complete() { hasIncomingData = false; } - private ReaderElement getNextPBF() - { + private ReaderElement getNextPBF() { ReaderElement next = null; - while (next == null) - { - if (!hasIncomingData && itemQueue.isEmpty()) - { + while (next == null) { + if (!hasIncomingData && itemQueue.isEmpty()) { // we are done, stop polling eof = true; break; } - try - { + try { // we cannot use "itemQueue.take()" as it blocks and hasIncomingData can change next = itemQueue.poll(10, TimeUnit.MILLISECONDS); - } catch (InterruptedException ex) - { + } catch (InterruptedException ex) { eof = true; break; } diff --git a/reader-osm/src/main/java/com/graphhopper/reader/osm/OSMReader.java b/reader-osm/src/main/java/com/graphhopper/reader/osm/OSMReader.java index 6f57482fce0..5b1027461fd 100644 --- a/reader-osm/src/main/java/com/graphhopper/reader/osm/OSMReader.java +++ b/reader-osm/src/main/java/com/graphhopper/reader/osm/OSMReader.java @@ -17,45 +17,38 @@ */ package com.graphhopper.reader.osm; -import com.graphhopper.reader.ReaderRelation; -import com.graphhopper.reader.ReaderWay; -import com.graphhopper.reader.ReaderNode; -import com.graphhopper.reader.ReaderElement; -import static com.graphhopper.util.Helper.nf; - +import com.graphhopper.coll.GHLongIntBTree; +import com.graphhopper.coll.LongIntMap; +import com.graphhopper.reader.*; +import com.graphhopper.reader.dem.ElevationProvider; +import com.graphhopper.reader.osm.OSMTurnRelation.TurnCostTableEntry; +import com.graphhopper.routing.util.DefaultEdgeFilter; +import com.graphhopper.routing.util.EncodingManager; +import com.graphhopper.routing.util.FlagEncoder; +import com.graphhopper.routing.weighting.TurnWeighting; +import com.graphhopper.storage.*; +import com.graphhopper.util.*; +import com.graphhopper.util.shapes.GHPoint; import gnu.trove.list.TLongList; import gnu.trove.list.array.TLongArrayList; import gnu.trove.map.TIntLongMap; import gnu.trove.map.TLongLongMap; +import gnu.trove.map.TLongObjectMap; import gnu.trove.map.hash.TIntLongHashMap; import gnu.trove.map.hash.TLongLongHashMap; +import gnu.trove.map.hash.TLongObjectHashMap; import gnu.trove.set.TLongSet; import gnu.trove.set.hash.TLongHashSet; - -import java.io.File; -import java.io.IOException; - -import javax.xml.stream.XMLStreamException; - import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.graphhopper.coll.GHLongIntBTree; -import com.graphhopper.coll.LongIntMap; -import com.graphhopper.reader.DataReader; -import com.graphhopper.reader.osm.OSMTurnRelation.TurnCostTableEntry; -import com.graphhopper.reader.PillarInfo; -import com.graphhopper.reader.dem.ElevationProvider; -import com.graphhopper.routing.util.*; -import com.graphhopper.routing.weighting.TurnWeighting; -import com.graphhopper.storage.*; -import com.graphhopper.util.*; -import com.graphhopper.util.shapes.GHPoint; -import gnu.trove.map.TLongObjectMap; -import gnu.trove.map.hash.TLongObjectHashMap; - +import javax.xml.stream.XMLStreamException; +import java.io.File; +import java.io.IOException; import java.util.*; +import static com.graphhopper.util.Helper.nf; + /** * This class parses an OSM xml or pbf file and creates a graph from it. It does so in a two phase * parsing processes in order to reduce memory usage compared to a single parsing processing. @@ -77,24 +70,32 @@ * When creating an edge the pillar node information from the intermediate datastructure will be * stored in the way geometry of that edge. *

    + * * @author Peter Karich */ -public class OSMReader implements DataReader -{ +public class OSMReader implements DataReader { protected static final int EMPTY = -1; // pillar node is >= 3 protected static final int PILLAR_NODE = 1; // tower node is <= -3 protected static final int TOWER_NODE = -2; private static final Logger LOGGER = LoggerFactory.getLogger(OSMReader.class); - private long locations; - private long skippedLocations; private final GraphStorage ghStorage; private final Graph graph; private final NodeAccess nodeAccess; + private final TLongList barrierNodeIds = new TLongArrayList(); + private final DistanceCalc distCalc = Helper.DIST_EARTH; + private final DistanceCalc3D distCalc3D = Helper.DIST_3D; + private final DouglasPeucker simplifyAlgo = new DouglasPeucker(); + private final boolean exitOnlyPillarNodeException = true; + private final Map outExplorerMap = new HashMap(); + private final Map inExplorerMap = new HashMap(); + protected long zeroCounter = 0; + protected PillarInfo pillarInfo; + private long locations; + private long skippedLocations; private EncodingManager encodingManager = null; private int workerThreads = -1; - protected long zeroCounter = 0; // Using the correct Map is hard. We need a memory efficient and fast solution for big data sets! // // very slow: new SparseLongLongArray @@ -110,25 +111,16 @@ public class OSMReader implements DataReader // stores osm way ids used by relations to identify which edge ids needs to be mapped later private TLongHashSet osmWayIdSet = new TLongHashSet(); private TIntLongMap edgeIdToOsmWayIdMap; - private final TLongList barrierNodeIds = new TLongArrayList(); - protected PillarInfo pillarInfo; - private final DistanceCalc distCalc = Helper.DIST_EARTH; - private final DistanceCalc3D distCalc3D = Helper.DIST_3D; - private final DouglasPeucker simplifyAlgo = new DouglasPeucker(); private boolean doSimplify = true; private int nextTowerId = 0; private int nextPillarId = 0; // negative but increasing to avoid clash with custom created OSM files private long newUniqueOsmId = -Long.MAX_VALUE; private ElevationProvider eleProvider = ElevationProvider.NOOP; - private final boolean exitOnlyPillarNodeException = true; private File osmFile; private Date osmDataDate; - private final Map outExplorerMap = new HashMap(); - private final Map inExplorerMap = new HashMap(); - public OSMReader( GraphHopperStorage ghStorage ) - { + public OSMReader(GraphHopperStorage ghStorage) { this.ghStorage = ghStorage; this.graph = ghStorage; this.nodeAccess = graph.getNodeAccess(); @@ -140,8 +132,7 @@ public OSMReader( GraphHopperStorage ghStorage ) } @Override - public void readGraph() throws IOException - { + public void readGraph() throws IOException { if (encodingManager == null) throw new IllegalStateException("Encoding manager was not set."); @@ -167,39 +158,31 @@ public void readGraph() throws IOException * Preprocessing of OSM file to select nodes which are used for highways. This allows a more * compact graph data structure. */ - void preProcess( File osmFile ) - { + void preProcess(File osmFile) { OSMInputFile in = null; - try - { + try { in = new OSMInputFile(osmFile).setWorkerThreads(workerThreads).open(); long tmpWayCounter = 1; long tmpRelationCounter = 1; ReaderElement item; - while ((item = in.getNext()) != null) - { - if (item.isType(ReaderElement.WAY)) - { + while ((item = in.getNext()) != null) { + if (item.isType(ReaderElement.WAY)) { final ReaderWay way = (ReaderWay) item; boolean valid = filterWay(way); - if (valid) - { + if (valid) { TLongList wayNodes = way.getNodes(); int s = wayNodes.size(); - for (int index = 0; index < s; index++) - { + for (int index = 0; index < s; index++) { prepareHighwayNode(wayNodes.get(index)); } - if (++tmpWayCounter % 5000000 == 0) - { + if (++tmpWayCounter % 5000000 == 0) { LOGGER.info(nf(tmpWayCounter) + " (preprocess), osmIdMap:" + nf(getNodeMap().getSize()) + " (" + getNodeMap().getMemoryUsage() + "MB) " + Helper.getMemInfo()); } } - } else if (item.isType(ReaderElement.RELATION)) - { + } else if (item.isType(ReaderElement.RELATION)) { final ReaderRelation relation = (ReaderRelation) item; if (!relation.isMetaRelation() && relation.hasTag("type", "route")) prepareWaysWithRelationInfo(relation); @@ -207,32 +190,26 @@ void preProcess( File osmFile ) if (relation.hasTag("type", "restriction")) prepareRestrictionRelation(relation); - if (++tmpRelationCounter % 50000 == 0) - { + if (++tmpRelationCounter % 50000 == 0) { LOGGER.info(nf(tmpRelationCounter) + " (preprocess), osmWayMap:" + nf(getRelFlagsMap().size()) + " " + Helper.getMemInfo()); } - } else if (item.isType(ReaderElement.FILEHEADER)) - { + } else if (item.isType(ReaderElement.FILEHEADER)) { final OSMFileHeader fileHeader = (OSMFileHeader) item; osmDataDate = Helper.createFormatter().parse(fileHeader.getTag("timestamp")); } } - } catch (Exception ex) - { + } catch (Exception ex) { throw new RuntimeException("Problem while parsing file", ex); - } finally - { + } finally { Helper.close(in); } } - private void prepareRestrictionRelation( ReaderRelation relation ) - { + private void prepareRestrictionRelation(ReaderRelation relation) { OSMTurnRelation turnRelation = createTurnRelation(relation); - if (turnRelation != null) - { + if (turnRelation != null) { getOsmWayIdSet().add(turnRelation.getOsmIdFrom()); getOsmWayIdSet().add(turnRelation.getOsmIdTo()); } @@ -241,13 +218,11 @@ private void prepareRestrictionRelation( ReaderRelation relation ) /** * @return all required osmWayIds to process e.g. relations. */ - private TLongSet getOsmWayIdSet() - { + private TLongSet getOsmWayIdSet() { return osmWayIdSet; } - private TIntLongMap getEdgeIdToOsmWayIdMap() - { + private TIntLongMap getEdgeIdToOsmWayIdMap() { if (edgeIdToOsmWayIdMap == null) edgeIdToOsmWayIdMap = new TIntLongHashMap(getOsmWayIdSet().size(), 0.5f, -1, -1); @@ -258,10 +233,10 @@ private TIntLongMap getEdgeIdToOsmWayIdMap() * Filter ways but do not analyze properties wayNodes will be filled with participating node * ids. *

    + * * @return true the current xml entry is a way entry and has nodes */ - boolean filterWay( ReaderWay item ) - { + boolean filterWay(ReaderWay item) { // ignore broken geometry if (item.getNodes().size() < 2) return false; @@ -276,8 +251,7 @@ boolean filterWay( ReaderWay item ) /** * Creates the edges and nodes files from the specified osm file. */ - private void writeOsm2Graph( File osmFile ) - { + private void writeOsm2Graph(File osmFile) { int tmp = (int) Math.max(getNodeMap().getSize() / 50, 100); LOGGER.info("creating graph. Found nodes (pillar+tower):" + nf(getNodeMap().getSize()) + ", " + Helper.getMemInfo()); ghStorage.create(tmp); @@ -285,34 +259,28 @@ private void writeOsm2Graph( File osmFile ) long relationStart = -1; long counter = 1; OSMInputFile in = null; - try - { + try { in = new OSMInputFile(osmFile).setWorkerThreads(workerThreads).open(); LongIntMap nodeFilter = getNodeMap(); ReaderElement item; - while ((item = in.getNext()) != null) - { - switch (item.getType()) - { + while ((item = in.getNext()) != null) { + switch (item.getType()) { case ReaderElement.NODE: - if (nodeFilter.get(item.getId()) != -1) - { + if (nodeFilter.get(item.getId()) != -1) { processNode((ReaderNode) item); } break; case ReaderElement.WAY: - if (wayStart < 0) - { + if (wayStart < 0) { LOGGER.info(nf(counter) + ", now parsing ways"); wayStart = counter; } processWay((ReaderWay) item); break; case ReaderElement.RELATION: - if (relationStart < 0) - { + if (relationStart < 0) { LOGGER.info(nf(counter) + ", now parsing relations"); relationStart = counter; } @@ -323,18 +291,15 @@ private void writeOsm2Graph( File osmFile ) default: throw new IllegalStateException("Unknown type " + item.getType()); } - if (++counter % 100000000 == 0) - { + if (++counter % 100000000 == 0) { LOGGER.info(nf(counter) + ", locs:" + nf(locations) + " (" + skippedLocations + ") " + Helper.getMemInfo()); } } // logger.info("storage nodes:" + storage.nodes() + " vs. graph nodes:" + storage.getGraph().nodes()); - } catch (Exception ex) - { + } catch (Exception ex) { throw new RuntimeException("Couldn't process file " + osmFile + ", error: " + ex.getMessage(), ex); - } finally - { + } finally { Helper.close(in); } @@ -346,8 +311,7 @@ private void writeOsm2Graph( File osmFile ) /** * Process properties, encode flags and create edges for the way. */ - void processWay( ReaderWay way ) - { + void processWay(ReaderWay way) { if (way.getNodes().size() < 2) return; @@ -366,14 +330,12 @@ void processWay( ReaderWay way ) // TODO move this after we have created the edge and know the coordinates => encodingManager.applyWayTags TLongList osmNodeIds = way.getNodes(); // Estimate length of ways containing a route tag e.g. for ferry speed calculation - if (osmNodeIds.size() > 1) - { + if (osmNodeIds.size() > 1) { int first = getNodeMap().get(osmNodeIds.get(0)); int last = getNodeMap().get(osmNodeIds.get(osmNodeIds.size() - 1)); double firstLat = getTmpLatitude(first), firstLon = getTmpLongitude(first); double lastLat = getTmpLatitude(last), lastLon = getTmpLongitude(last); - if (!Double.isNaN(firstLat) && !Double.isNaN(firstLon) && !Double.isNaN(lastLat) && !Double.isNaN(lastLon)) - { + if (!Double.isNaN(firstLat) && !Double.isNaN(firstLon) && !Double.isNaN(lastLat) && !Double.isNaN(lastLon)) { double estimatedDist = distCalc.calcDist(firstLat, firstLon, lastLat, lastLon); // Add artificial tag for the estimated distance and center way.setTag("estimated_distance", estimatedDist); @@ -381,15 +343,12 @@ void processWay( ReaderWay way ) } } - if (way.getTag("duration") != null) - { - try - { + if (way.getTag("duration") != null) { + try { long dur = OSMTagParser.parseDuration(way.getTag("duration")); // Provide the duration value in seconds in an artificial graphhopper specific tag: way.setTag("duration:seconds", Long.toString(dur)); - } catch (Exception ex) - { + } catch (Exception ex) { LOGGER.warn("Parsing error in way with OSMID=" + way.getId() + " : " + ex.getMessage()); } } @@ -402,22 +361,18 @@ void processWay( ReaderWay way ) // look for barriers along the way final int size = osmNodeIds.size(); int lastBarrier = -1; - for (int i = 0; i < size; i++) - { + for (int i = 0; i < size; i++) { long nodeId = osmNodeIds.get(i); long nodeFlags = getNodeFlagsMap().get(nodeId); // barrier was spotted and way is otherwise passable for that mode of travel - if (nodeFlags > 0) - { - if ((nodeFlags & wayFlags) > 0) - { + if (nodeFlags > 0) { + if ((nodeFlags & wayFlags) > 0) { // remove barrier to avoid duplicates getNodeFlagsMap().put(nodeId, 0); // create shadow node copy for zero length edge long newNodeId = addBarrierNode(nodeId); - if (i > 0) - { + if (i > 0) { // start at beginning of array if there was no previous barrier if (lastBarrier < 0) lastBarrier = 0; @@ -430,8 +385,7 @@ void processWay( ReaderWay way ) // create zero length edge for barrier createdEdges.addAll(addBarrierEdge(newNodeId, nodeId, wayFlags, nodeFlags, wayOsmId)); - } else - { + } else { // run edge from real first node to shadow node createdEdges.addAll(addBarrierEdge(nodeId, newNodeId, wayFlags, nodeFlags, wayOsmId)); @@ -445,40 +399,31 @@ void processWay( ReaderWay way ) } // just add remainder of way to graph if barrier was not the last node - if (lastBarrier >= 0) - { - if (lastBarrier < size - 1) - { + if (lastBarrier >= 0) { + if (lastBarrier < size - 1) { long transfer[] = osmNodeIds.toArray(lastBarrier, size - lastBarrier); TLongList partNodeIds = new TLongArrayList(transfer); createdEdges.addAll(addOSMWay(partNodeIds, wayFlags, wayOsmId)); } - } else - { + } else { // no barriers - simply add the whole way createdEdges.addAll(addOSMWay(way.getNodes(), wayFlags, wayOsmId)); } - for (EdgeIteratorState edge : createdEdges) - { + for (EdgeIteratorState edge : createdEdges) { encodingManager.applyWayTags(way, edge); } } - public void processRelation( ReaderRelation relation ) throws XMLStreamException - { - if (relation.hasTag("type", "restriction")) - { + public void processRelation(ReaderRelation relation) throws XMLStreamException { + if (relation.hasTag("type", "restriction")) { OSMTurnRelation turnRelation = createTurnRelation(relation); - if (turnRelation != null) - { + if (turnRelation != null) { GraphExtension extendedStorage = graph.getExtension(); - if (extendedStorage instanceof TurnCostExtension) - { + if (extendedStorage instanceof TurnCostExtension) { TurnCostExtension tcs = (TurnCostExtension) extendedStorage; Collection entries = analyzeTurnRelation(turnRelation); - for (TurnCostTableEntry entry : entries) - { + for (TurnCostTableEntry entry : entries) { tcs.addTurnInfo(entry.edgeFrom, entry.nodeVia, entry.edgeTo, entry.flags); } } @@ -486,21 +431,16 @@ public void processRelation( ReaderRelation relation ) throws XMLStreamException } } - public Collection analyzeTurnRelation( OSMTurnRelation turnRelation ) - { + public Collection analyzeTurnRelation(OSMTurnRelation turnRelation) { TLongObjectMap entries = new TLongObjectHashMap(); - for (FlagEncoder encoder : encodingManager.fetchEdgeEncoders()) - { - for (TurnCostTableEntry entry : analyzeTurnRelation(encoder, turnRelation)) - { + for (FlagEncoder encoder : encodingManager.fetchEdgeEncoders()) { + for (TurnCostTableEntry entry : analyzeTurnRelation(encoder, turnRelation)) { TurnCostTableEntry oldEntry = entries.get(entry.getItemId()); - if (oldEntry != null) - { + if (oldEntry != null) { // merging different encoders oldEntry.flags |= entry.flags; - } else - { + } else { entries.put(entry.getItemId(), entry); } } @@ -509,16 +449,14 @@ public Collection analyzeTurnRelation( OSMTurnRelation turnR return entries.valueCollection(); } - public Collection analyzeTurnRelation( FlagEncoder encoder, OSMTurnRelation turnRelation ) - { + public Collection analyzeTurnRelation(FlagEncoder encoder, OSMTurnRelation turnRelation) { if (!encoder.supports(TurnWeighting.class)) return Collections.emptyList(); EdgeExplorer edgeOutExplorer = outExplorerMap.get(encoder); EdgeExplorer edgeInExplorer = inExplorerMap.get(encoder); - if (edgeOutExplorer == null || edgeInExplorer == null) - { + if (edgeOutExplorer == null || edgeInExplorer == null) { edgeOutExplorer = graph.createEdgeExplorer(new DefaultEdgeFilter(encoder, false, true)); outExplorerMap.put(encoder, edgeOutExplorer); @@ -532,13 +470,11 @@ public Collection analyzeTurnRelation( FlagEncoder encoder, * @return OSM way ID from specified edgeId. Only previously stored OSM-way-IDs are returned in * order to reduce memory overhead. */ - public long getOsmIdOfInternalEdge( int edgeId ) - { + public long getOsmIdOfInternalEdge(int edgeId) { return getEdgeIdToOsmWayIdMap().get(edgeId); } - public int getInternalNodeIdOfOsmNode( long nodeOsmId ) - { + public int getInternalNodeIdOfOsmNode(long nodeOsmId) { int id = getNodeMap().get(nodeOsmId); if (id < TOWER_NODE) return -id - 3; @@ -547,17 +483,14 @@ public int getInternalNodeIdOfOsmNode( long nodeOsmId ) } // TODO remove this ugly stuff via better preparsing phase! E.g. putting every tags etc into a helper file! - double getTmpLatitude( int id ) - { + double getTmpLatitude(int id) { if (id == EMPTY) return Double.NaN; - if (id < TOWER_NODE) - { + if (id < TOWER_NODE) { // tower node id = -id - 3; return nodeAccess.getLatitude(id); - } else if (id > -TOWER_NODE) - { + } else if (id > -TOWER_NODE) { // pillar node id = id - 3; return pillarInfo.getLatitude(id); @@ -566,17 +499,14 @@ public int getInternalNodeIdOfOsmNode( long nodeOsmId ) return Double.NaN; } - double getTmpLongitude( int id ) - { + double getTmpLongitude(int id) { if (id == EMPTY) return Double.NaN; - if (id < TOWER_NODE) - { + if (id < TOWER_NODE) { // tower node id = -id - 3; return nodeAccess.getLongitude(id); - } else if (id > -TOWER_NODE) - { + } else if (id > -TOWER_NODE) { // pillar node id = id - 3; return pillarInfo.getLon(id); @@ -585,29 +515,24 @@ public int getInternalNodeIdOfOsmNode( long nodeOsmId ) return Double.NaN; } - private void processNode( ReaderNode node ) - { - if (isInBounds(node)) - { + private void processNode(ReaderNode node) { + if (isInBounds(node)) { addNode(node); // analyze node tags for barriers - if (node.hasTags()) - { + if (node.hasTags()) { long nodeFlags = encodingManager.handleNodeTags(node); if (nodeFlags != 0) getNodeFlagsMap().put(node.getId(), nodeFlags); } locations++; - } else - { + } else { skippedLocations++; } } - boolean addNode( ReaderNode node ) - { + boolean addNode(ReaderNode node) { int nodeType = getNodeMap().get(node.getId()); if (nodeType == EMPTY) return false; @@ -615,11 +540,9 @@ boolean addNode( ReaderNode node ) double lat = node.getLat(); double lon = node.getLon(); double ele = getElevation(node); - if (nodeType == TOWER_NODE) - { + if (nodeType == TOWER_NODE) { addTowerNode(node.getId(), lat, lon, ele); - } else if (nodeType == PILLAR_NODE) - { + } else if (nodeType == PILLAR_NODE) { pillarInfo.setNode(nextPillarId, lat, lon, ele); getNodeMap().put(node.getId(), nextPillarId + 3); nextPillarId++; @@ -627,20 +550,17 @@ boolean addNode( ReaderNode node ) return true; } - protected double getElevation( ReaderNode node ) - { + protected double getElevation(ReaderNode node) { return eleProvider.getEle(node.getLat(), node.getLon()); } - void prepareWaysWithRelationInfo( ReaderRelation osmRelation ) - { + void prepareWaysWithRelationInfo(ReaderRelation osmRelation) { // is there at least one tag interesting for the registed encoders? if (encodingManager.handleRelationTags(osmRelation, 0) == 0) return; int size = osmRelation.getMembers().size(); - for (int index = 0; index < size; index++) - { + for (int index = 0; index < size; index++) { ReaderRelation.Member member = osmRelation.getMembers().get(index); if (member.getType() != ReaderRelation.Member.WAY) continue; @@ -655,25 +575,20 @@ void prepareWaysWithRelationInfo( ReaderRelation osmRelation ) } } - void prepareHighwayNode( long osmId ) - { + void prepareHighwayNode(long osmId) { int tmpIndex = getNodeMap().get(osmId); - if (tmpIndex == EMPTY) - { + if (tmpIndex == EMPTY) { // osmId is used exactly once getNodeMap().put(osmId, PILLAR_NODE); - } else if (tmpIndex > EMPTY) - { + } else if (tmpIndex > EMPTY) { // mark node as tower node as it occured at least twice times getNodeMap().put(osmId, TOWER_NODE); - } else - { + } else { // tmpIndex is already negative (already tower node) } } - int addTowerNode( long osmId, double lat, double lon, double ele ) - { + int addTowerNode(long osmId, double lat, double lon, double ele) { if (nodeAccess.is3D()) nodeAccess.setNode(nextTowerId, lat, lon, ele); else @@ -688,17 +603,14 @@ int addTowerNode( long osmId, double lat, double lon, double ele ) /** * This method creates from an OSM way (via the osm ids) one or more edges in the graph. */ - Collection addOSMWay( final TLongList osmNodeIds, final long flags, final long wayOsmId ) - { + Collection addOSMWay(final TLongList osmNodeIds, final long flags, final long wayOsmId) { PointList pointList = new PointList(osmNodeIds.size(), nodeAccess.is3D()); List newEdges = new ArrayList(5); int firstNode = -1; int lastIndex = osmNodeIds.size() - 1; int lastInBoundsPillarNode = -1; - try - { - for (int i = 0; i < osmNodeIds.size(); i++) - { + try { + for (int i = 0; i < osmNodeIds.size(); i++) { long osmId = osmNodeIds.get(i); int tmpNode = getNodeMap().get(osmId); if (tmpNode == EMPTY) @@ -708,19 +620,16 @@ Collection addOSMWay( final TLongList osmNodeIds, final long if (tmpNode == TOWER_NODE) continue; - if (tmpNode == PILLAR_NODE) - { + if (tmpNode == PILLAR_NODE) { // In some cases no node information is saved for the specified osmId. // ie. a way references a which does not exist in the current file. // => if the node before was a pillar node then convert into to tower node (as it is also end-standing). - if (!pointList.isEmpty() && lastInBoundsPillarNode > -TOWER_NODE) - { + if (!pointList.isEmpty() && lastInBoundsPillarNode > -TOWER_NODE) { // transform the pillar node to a tower node tmpNode = lastInBoundsPillarNode; tmpNode = handlePillarNode(tmpNode, osmId, null, true); tmpNode = -tmpNode - 3; - if (pointList.getSize() > 1 && firstNode >= 0) - { + if (pointList.getSize() > 1 && firstNode >= 0) { // TOWER node newEdges.add(addEdge(firstNode, tmpNode, pointList, flags, wayOsmId)); pointList.clear(); @@ -735,11 +644,9 @@ Collection addOSMWay( final TLongList osmNodeIds, final long if (tmpNode <= -TOWER_NODE && tmpNode >= TOWER_NODE) throw new AssertionError("Mapped index not in correct bounds " + tmpNode + ", " + osmId); - if (tmpNode > -TOWER_NODE) - { + if (tmpNode > -TOWER_NODE) { boolean convertToTowerNode = i == 0 || i == lastIndex; - if (!convertToTowerNode) - { + if (!convertToTowerNode) { lastInBoundsPillarNode = tmpNode; } @@ -747,13 +654,11 @@ Collection addOSMWay( final TLongList osmNodeIds, final long tmpNode = handlePillarNode(tmpNode, osmId, pointList, convertToTowerNode); } - if (tmpNode < TOWER_NODE) - { + if (tmpNode < TOWER_NODE) { // TOWER node tmpNode = -tmpNode - 3; pointList.add(nodeAccess, tmpNode); - if (firstNode >= 0) - { + if (firstNode >= 0) { newEdges.add(addEdge(firstNode, tmpNode, pointList, flags, wayOsmId)); pointList.clear(); pointList.add(nodeAccess, tmpNode); @@ -761,8 +666,7 @@ Collection addOSMWay( final TLongList osmNodeIds, final long firstNode = tmpNode; } } - } catch (RuntimeException ex) - { + } catch (RuntimeException ex) { LOGGER.error("Couldn't properly add edge with osm ids:" + osmNodeIds, ex); if (exitOnlyPillarNodeException) throw ex; @@ -770,8 +674,7 @@ Collection addOSMWay( final TLongList osmNodeIds, final long return newEdges; } - EdgeIteratorState addEdge( int fromIndex, int toIndex, PointList pointList, long flags, long wayOsmId ) - { + EdgeIteratorState addEdge(int fromIndex, int toIndex, PointList pointList, long flags, long wayOsmId) { // sanity checks if (fromIndex < 0 || toIndex < 0) throw new AssertionError("to or from index is invalid for this edge " + fromIndex + "->" + toIndex + ", points:" + pointList); @@ -785,13 +688,11 @@ EdgeIteratorState addEdge( int fromIndex, int toIndex, PointList pointList, long double lat, lon, ele = Double.NaN; PointList pillarNodes = new PointList(pointList.getSize() - 2, nodeAccess.is3D()); int nodes = pointList.getSize(); - for (int i = 1; i < nodes; i++) - { + for (int i = 1; i < nodes; i++) { // we could save some lines if we would use pointList.calcDistance(distCalc); lat = pointList.getLatitude(i); lon = pointList.getLongitude(i); - if (pointList.is3D()) - { + if (pointList.is3D()) { ele = pointList.getElevation(i); if (!distCalc.isCrossBoundary(lon, prevLon)) towerNodeDistance += distCalc3D.calcDist(prevLat, prevLon, prevEle, lat, lon, ele); @@ -801,16 +702,14 @@ EdgeIteratorState addEdge( int fromIndex, int toIndex, PointList pointList, long prevLat = lat; prevLon = lon; - if (nodes > 2 && i < nodes - 1) - { + if (nodes > 2 && i < nodes - 1) { if (pillarNodes.is3D()) pillarNodes.add(lat, lon, ele); else pillarNodes.add(lat, lon); } } - if (towerNodeDistance < 0.0001) - { + if (towerNodeDistance < 0.0001) { // As investigation shows often two paths should have crossed via one identical point // but end up in two very close points. zeroCounter++; @@ -818,14 +717,12 @@ EdgeIteratorState addEdge( int fromIndex, int toIndex, PointList pointList, long } double maxDistance = (Integer.MAX_VALUE - 1) / 1000d; - if (Double.isNaN(towerNodeDistance)) - { + if (Double.isNaN(towerNodeDistance)) { LOGGER.warn("Bug in OSM or GraphHopper. Illegal tower node distance " + towerNodeDistance + " reset to 1m, osm way " + wayOsmId); towerNodeDistance = 1; } - if (Double.isInfinite(towerNodeDistance) || towerNodeDistance > maxDistance) - { + if (Double.isInfinite(towerNodeDistance) || towerNodeDistance > maxDistance) { // Too large is very rare and often the wrong tagging. See #435 // so we can avoid the complexity of splitting the way for now (new towernodes would be required, splitting up geometry etc) LOGGER.warn("Bug in OSM or GraphHopper. Too big tower node distance " + towerNodeDistance + " reset to large value, osm way " + wayOsmId); @@ -834,8 +731,7 @@ EdgeIteratorState addEdge( int fromIndex, int toIndex, PointList pointList, long EdgeIteratorState iter = graph.edge(fromIndex, toIndex).setDistance(towerNodeDistance).setFlags(flags); - if (nodes > 2) - { + if (nodes > 2) { if (doSimplify) simplifyAlgo.simplify(pillarNodes); @@ -848,10 +744,8 @@ EdgeIteratorState addEdge( int fromIndex, int toIndex, PointList pointList, long /** * Stores only osmWayIds which are required for relations */ - protected void storeOsmWayID( int edgeId, long osmWayId ) - { - if (getOsmWayIdSet().contains(osmWayId)) - { + protected void storeOsmWayID(int edgeId, long osmWayId) { + if (getOsmWayIdSet().contains(osmWayId)) { getEdgeIdToOsmWayIdMap().put(edgeId, osmWayId); } } @@ -859,8 +753,7 @@ protected void storeOsmWayID( int edgeId, long osmWayId ) /** * @return converted tower node */ - private int handlePillarNode( int tmpNode, long osmId, PointList pointList, boolean convertToTowerNode ) - { + private int handlePillarNode(int tmpNode, long osmId, PointList pointList, boolean convertToTowerNode) { tmpNode = tmpNode - 3; double lat = pillarInfo.getLatitude(tmpNode); double lon = pillarInfo.getLongitude(tmpNode); @@ -869,8 +762,7 @@ private int handlePillarNode( int tmpNode, long osmId, PointList pointList, bool throw new RuntimeException("Conversion pillarNode to towerNode already happended!? " + "osmId:" + osmId + " pillarIndex:" + tmpNode); - if (convertToTowerNode) - { + if (convertToTowerNode) { // convert pillarNode type to towerNode, make pillar values invalid pillarInfo.setNode(tmpNode, Double.MAX_VALUE, Double.MAX_VALUE, Double.MAX_VALUE); tmpNode = addTowerNode(osmId, lat, lon, ele); @@ -882,8 +774,7 @@ private int handlePillarNode( int tmpNode, long osmId, PointList pointList, bool return (int) tmpNode; } - protected void finishedReading() - { + protected void finishedReading() { printInfo("way"); pillarInfo.clear(); eleProvider.release(); @@ -897,16 +788,13 @@ protected void finishedReading() /** * Create a copy of the barrier node */ - long addBarrierNode( long nodeId ) - { + long addBarrierNode(long nodeId) { ReaderNode newNode; int graphIndex = getNodeMap().get(nodeId); - if (graphIndex < TOWER_NODE) - { + if (graphIndex < TOWER_NODE) { graphIndex = -graphIndex - 3; newNode = new ReaderNode(createNewNodeId(), nodeAccess, graphIndex); - } else - { + } else { graphIndex = graphIndex - 3; newNode = new ReaderNode(createNewNodeId(), pillarInfo, graphIndex); } @@ -917,16 +805,14 @@ long addBarrierNode( long nodeId ) return id; } - private long createNewNodeId() - { + private long createNewNodeId() { return newUniqueOsmId++; } /** * Add a zero length edge with reduced routing options to the graph. */ - Collection addBarrierEdge( long fromId, long toId, long flags, long nodeFlags, long wayOsmId ) - { + Collection addBarrierEdge(long fromId, long toId, long flags, long nodeFlags, long wayOsmId) { // clear barred directions from routing flags flags &= ~nodeFlags; // add edge @@ -939,35 +825,28 @@ Collection addBarrierEdge( long fromId, long toId, long flags /** * Creates an OSM turn relation out of an unspecified OSM relation *

    + * * @return the OSM turn relation, null, if unsupported turn relation */ - OSMTurnRelation createTurnRelation( ReaderRelation relation ) - { + OSMTurnRelation createTurnRelation(ReaderRelation relation) { OSMTurnRelation.Type type = OSMTurnRelation.Type.getRestrictionType(relation.getTag("restriction")); - if (type != OSMTurnRelation.Type.UNSUPPORTED) - { + if (type != OSMTurnRelation.Type.UNSUPPORTED) { long fromWayID = -1; long viaNodeID = -1; long toWayID = -1; - for (ReaderRelation.Member member : relation.getMembers()) - { - if (ReaderElement.WAY == member.getType()) - { - if ("from".equals(member.getRole())) - { + for (ReaderRelation.Member member : relation.getMembers()) { + if (ReaderElement.WAY == member.getType()) { + if ("from".equals(member.getRole())) { fromWayID = member.getRef(); - } else if ("to".equals(member.getRole())) - { + } else if ("to".equals(member.getRole())) { toWayID = member.getRef(); } - } else if (ReaderElement.NODE == member.getType() && "via".equals(member.getRole())) - { + } else if (ReaderElement.NODE == member.getType() && "via".equals(member.getRole())) { viaNodeID = member.getRef(); } } - if (fromWayID >= 0 && toWayID >= 0 && viaNodeID >= 0) - { + if (fromWayID >= 0 && toWayID >= 0 && viaNodeID >= 0) { return new OSMTurnRelation(fromWayID, viaNodeID, toWayID, type); } } @@ -977,26 +856,22 @@ OSMTurnRelation createTurnRelation( ReaderRelation relation ) /** * Filter method, override in subclass */ - boolean isInBounds( ReaderNode node ) - { + boolean isInBounds(ReaderNode node) { return true; } /** * Maps OSM IDs (long) to internal node IDs (int) */ - protected LongIntMap getNodeMap() - { + protected LongIntMap getNodeMap() { return osmNodeIdToInternalNodeMap; } - protected TLongLongMap getNodeFlagsMap() - { + protected TLongLongMap getNodeFlagsMap() { return osmNodeIdToNodeFlagsMap; } - TLongLongHashMap getRelFlagsMap() - { + TLongLongHashMap getRelFlagsMap() { return osmWayIdToRouteWeightMap; } @@ -1004,30 +879,26 @@ TLongLongHashMap getRelFlagsMap() * Specify the type of the path calculation (car, bike, ...). */ @Override - public OSMReader setEncodingManager( EncodingManager em ) - { + public OSMReader setEncodingManager(EncodingManager em) { this.encodingManager = em; return this; } @Override - public OSMReader setWayPointMaxDistance( double maxDist ) - { + public OSMReader setWayPointMaxDistance(double maxDist) { doSimplify = maxDist > 0; simplifyAlgo.setMaxDistance(maxDist); return this; } @Override - public OSMReader setWorkerThreads( int numOfWorkers ) - { + public OSMReader setWorkerThreads(int numOfWorkers) { this.workerThreads = numOfWorkers; return this; } @Override - public OSMReader setElevationProvider( ElevationProvider eleProvider ) - { + public OSMReader setElevationProvider(ElevationProvider eleProvider) { if (eleProvider == null) throw new IllegalStateException("Use the NOOP elevation provider instead of null or don't call setElevationProvider"); @@ -1039,14 +910,12 @@ public OSMReader setElevationProvider( ElevationProvider eleProvider ) } @Override - public DataReader setFile( File osmFile ) - { + public DataReader setFile(File osmFile) { this.osmFile = osmFile; return this; } - private void printInfo( String str ) - { + private void printInfo(String str) { LOGGER.info("finished " + str + " processing." + " nodes: " + graph.getNodes() + ", osmIdMap.size:" + getNodeMap().getSize() + ", osmIdMap:" + getNodeMap().getMemoryUsage() + "MB" + ", nodeFlagsMap.size:" + getNodeFlagsMap().size() + ", relFlagsMap.size:" + getRelFlagsMap().size() @@ -1055,14 +924,12 @@ private void printInfo( String str ) } @Override - public Date getDataDate() - { + public Date getDataDate() { return osmDataDate; } @Override - public String toString() - { + public String toString() { return getClass().getSimpleName(); } } diff --git a/reader-osm/src/main/java/com/graphhopper/reader/osm/OSMTagParser.java b/reader-osm/src/main/java/com/graphhopper/reader/osm/OSMTagParser.java index 0fde8a96f2e..55a97d53f87 100644 --- a/reader-osm/src/main/java/com/graphhopper/reader/osm/OSMTagParser.java +++ b/reader-osm/src/main/java/com/graphhopper/reader/osm/OSMTagParser.java @@ -17,17 +17,16 @@ */ package com.graphhopper.reader.osm; -import java.util.Date; import javax.xml.datatype.DatatypeFactory; import javax.xml.datatype.Duration; +import java.util.Date; /** * This class currently parses the duration tag only. * * @author ratrun */ -public class OSMTagParser -{ +public class OSMTagParser { // use a day somewhere within July 1970 which then makes two identical long months ala 31 days, see #588 private final static Date STATIC_DATE = new Date((31 * 6) * 24 * 3600 * 1000); @@ -36,42 +35,36 @@ public class OSMTagParser * string ala 'hh:mm', format for hours and minutes 'mm', 'hh:mm' or 'hh:mm:ss', or * alternatively ISO_8601 duration *

    + * * @return duration value in seconds */ - public static long parseDuration( String str ) throws IllegalArgumentException - { + public static long parseDuration(String str) throws IllegalArgumentException { long seconds = 0; if (str == null) return 0; // Check for ISO_8601 format - if (str.startsWith("P")) - { + if (str.startsWith("P")) { // A common mistake is when the minutes format is intended but the month format is specified // e.g. one month "P1M" is set, but on minute "PT1M" is meant. Duration dur; - try - { + try { dur = DatatypeFactory.newInstance().newDuration(str); seconds = dur.getTimeInMillis(STATIC_DATE) / 1000; - } catch (Exception ex) - { + } catch (Exception ex) { throw new IllegalArgumentException("Cannot parse duration tag value: " + str, ex); } return seconds; } - try - { + try { int index = str.indexOf(":"); - if (index > 0) - { + if (index > 0) { String hourStr = str.substring(0, index); String minStr = str.substring(index + 1); String secondsStr = "0"; index = minStr.indexOf(":"); - if (index > 0) - { + if (index > 0) { secondsStr = minStr.substring(index + 1, index + 3); minStr = minStr.substring(0, index); } @@ -80,13 +73,11 @@ public static long parseDuration( String str ) throws IllegalArgumentException seconds += Integer.parseInt(minStr) * 60L; seconds += Integer.parseInt(secondsStr); return seconds; - } else - { + } else { // value contains minutes seconds = Integer.parseInt(str) * 60; } - } catch (Exception ex) - { + } catch (Exception ex) { throw new IllegalArgumentException("Cannot parse duration tag value: " + str, ex); } return seconds; diff --git a/reader-osm/src/main/java/com/graphhopper/reader/osm/OSMTurnRelation.java b/reader-osm/src/main/java/com/graphhopper/reader/osm/OSMTurnRelation.java index 5663c0c625b..62a19948860 100644 --- a/reader-osm/src/main/java/com/graphhopper/reader/osm/OSMTurnRelation.java +++ b/reader-osm/src/main/java/com/graphhopper/reader/osm/OSMTurnRelation.java @@ -26,75 +26,43 @@ /** * Helper object which gives node cost entries for a given OSM-relation of type "restriction" *

    + * * @author Karl Hübner */ -public class OSMTurnRelation -{ - enum Type - { - UNSUPPORTED, NOT, ONLY; - - private static final Map tags = new HashMap(); - - static - { - tags.put("no_left_turn", NOT); - tags.put("no_right_turn", NOT); - tags.put("no_straight_on", NOT); - tags.put("no_u_turn", NOT); - tags.put("only_right_turn", ONLY); - tags.put("only_left_turn", ONLY); - tags.put("only_straight_on", ONLY); - } - - public static Type getRestrictionType( String tag ) - { - Type result = null; - if (tag != null) - { - result = tags.get(tag); - } - return (result != null) ? result : UNSUPPORTED; - } - } - +public class OSMTurnRelation { private final long fromOsmWayId; private final long viaOsmNodeId; private final long toOsmWayId; private final Type restriction; - OSMTurnRelation( long fromWayID, long viaNodeID, long toWayID, Type restrictionType ) - { + OSMTurnRelation(long fromWayID, long viaNodeID, long toWayID, Type restrictionType) { this.fromOsmWayId = fromWayID; this.viaOsmNodeId = viaNodeID; this.toOsmWayId = toWayID; this.restriction = restrictionType; } - long getOsmIdFrom() - { + long getOsmIdFrom() { return fromOsmWayId; } - long getOsmIdTo() - { + long getOsmIdTo() { return toOsmWayId; } /** * Transforms this relation into a collection of turn cost entries *

    + * * @param edgeOutExplorer an edge filter which only allows outgoing edges - * @param edgeInExplorer an edge filter which only allows incoming edges + * @param edgeInExplorer an edge filter which only allows incoming edges * @return a collection of node cost entries which can be added to the graph later */ - public Collection getRestrictionAsEntries( TurnCostEncoder encoder, - EdgeExplorer edgeOutExplorer, EdgeExplorer edgeInExplorer, OSMReader osmReader ) - { + public Collection getRestrictionAsEntries(TurnCostEncoder encoder, + EdgeExplorer edgeOutExplorer, EdgeExplorer edgeInExplorer, OSMReader osmReader) { int nodeVia = osmReader.getInternalNodeIdOfOsmNode(this.viaOsmNodeId); - try - { + try { // street with restriction was not included (access or tag limits etc) if (nodeVia == OSMReader.EMPTY) return Collections.emptyList(); @@ -104,10 +72,8 @@ public Collection getRestrictionAsEntries( TurnCostEncoder e // get all incoming edges and receive the edge which is defined by fromOsm EdgeIterator iter = edgeInExplorer.setBaseNode(nodeVia); - while (iter.next()) - { - if (osmReader.getOsmIdOfInternalEdge(iter.getEdge()) == this.fromOsmWayId) - { + while (iter.next()) { + if (osmReader.getOsmIdOfInternalEdge(iter.getEdge()) == this.fromOsmWayId) { edgeIdFrom = iter.getEdge(); break; } @@ -117,17 +83,15 @@ public Collection getRestrictionAsEntries( TurnCostEncoder e return Collections.emptyList(); final Collection entries = new ArrayList(); - // get all outgoing edges of the via node + // get all outgoing edges of the via node iter = edgeOutExplorer.setBaseNode(nodeVia); // for TYPE_ONLY_* we add ALL restrictions (from, via, * ) EXCEPT the given turn // for TYPE_NOT_* we add ONE restriction (from, via, to) - while (iter.next()) - { + while (iter.next()) { int edgeId = iter.getEdge(); long wayId = osmReader.getOsmIdOfInternalEdge(edgeId); if (edgeId != edgeIdFrom && this.restriction == Type.ONLY && wayId != this.toOsmWayId - || this.restriction == Type.NOT && wayId == this.toOsmWayId && wayId >= 0) - { + || this.restriction == Type.NOT && wayId == this.toOsmWayId && wayId >= 0) { final TurnCostTableEntry entry = new TurnCostTableEntry(); entry.nodeVia = nodeVia; entry.edgeFrom = edgeIdFrom; @@ -140,23 +104,44 @@ public Collection getRestrictionAsEntries( TurnCostEncoder e } } return entries; - } catch (Exception e) - { + } catch (Exception e) { throw new IllegalStateException("Could not built turn table entry for relation of node with osmId:" + this.viaOsmNodeId, e); } } @Override - public String toString() - { + public String toString() { return "*-(" + fromOsmWayId + ")->" + viaOsmNodeId + "-(" + toOsmWayId + ")->*"; } + enum Type { + UNSUPPORTED, NOT, ONLY; + + private static final Map tags = new HashMap(); + + static { + tags.put("no_left_turn", NOT); + tags.put("no_right_turn", NOT); + tags.put("no_straight_on", NOT); + tags.put("no_u_turn", NOT); + tags.put("only_right_turn", ONLY); + tags.put("only_left_turn", ONLY); + tags.put("only_straight_on", ONLY); + } + + public static Type getRestrictionType(String tag) { + Type result = null; + if (tag != null) { + result = tags.get(tag); + } + return (result != null) ? result : UNSUPPORTED; + } + } + /** * Helper class to processing purposes only */ - public static class TurnCostTableEntry - { + public static class TurnCostTableEntry { public int edgeFrom; public int nodeVia; public int edgeTo; @@ -166,14 +151,12 @@ public static class TurnCostTableEntry * @return an unique id (edgeFrom, edgeTo) to avoid duplicate entries if multiple encoders * are involved. */ - public long getItemId() - { + public long getItemId() { return ((long) edgeFrom) << 32 | ((long) edgeTo); } @Override - public String toString() - { + public String toString() { return "*-(" + edgeFrom + ")->" + nodeVia + "-(" + edgeTo + ")->*"; } } diff --git a/reader-osm/src/main/java/com/graphhopper/reader/osm/OSMXMLHelper.java b/reader-osm/src/main/java/com/graphhopper/reader/osm/OSMXMLHelper.java index d5058cad871..9c859537d41 100644 --- a/reader-osm/src/main/java/com/graphhopper/reader/osm/OSMXMLHelper.java +++ b/reader-osm/src/main/java/com/graphhopper/reader/osm/OSMXMLHelper.java @@ -22,19 +22,19 @@ import com.graphhopper.reader.ReaderRelation; import com.graphhopper.reader.ReaderRelation.Member; import com.graphhopper.reader.ReaderWay; -import java.util.List; + import javax.xml.stream.XMLStreamConstants; import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamReader; +import java.util.List; /** - * * @author Peter Karich */ -public class OSMXMLHelper -{ - public static ReaderNode createNode( long id, XMLStreamReader parser ) throws XMLStreamException - { +public class OSMXMLHelper { + private static final String TYPE_DECODE = "nwr"; + + public static ReaderNode createNode(long id, XMLStreamReader parser) throws XMLStreamException { ReaderNode node = new ReaderNode(id, Double.parseDouble(parser.getAttributeValue(null, "lat")), Double.parseDouble(parser.getAttributeValue(null, "lon"))); @@ -44,8 +44,7 @@ public static ReaderNode createNode( long id, XMLStreamReader parser ) throws XM return node; } - public static ReaderWay createWay( long id, XMLStreamReader parser ) throws XMLStreamException - { + public static ReaderWay createWay(long id, XMLStreamReader parser) throws XMLStreamException { ReaderWay way = new ReaderWay(id); parser.nextTag(); readNodes(way, parser); @@ -53,13 +52,10 @@ public static ReaderWay createWay( long id, XMLStreamReader parser ) throws XMLS return way; } - private static void readNodes( ReaderWay way, XMLStreamReader parser ) throws XMLStreamException - { + private static void readNodes(ReaderWay way, XMLStreamReader parser) throws XMLStreamException { int event = parser.getEventType(); - while (event != XMLStreamConstants.END_DOCUMENT && parser.getLocalName().equals("nd")) - { - if (event == XMLStreamConstants.START_ELEMENT) - { + while (event != XMLStreamConstants.END_DOCUMENT && parser.getLocalName().equals("nd")) { + if (event == XMLStreamConstants.START_ELEMENT) { // read node reference String ref = parser.getAttributeValue(null, "ref"); way.getNodes().add(Long.parseLong(ref)); @@ -69,13 +65,10 @@ private static void readNodes( ReaderWay way, XMLStreamReader parser ) throws XM } } - private static void readTags( ReaderElement re, XMLStreamReader parser ) throws XMLStreamException - { + private static void readTags(ReaderElement re, XMLStreamReader parser) throws XMLStreamException { int event = parser.getEventType(); - while (event != XMLStreamConstants.END_DOCUMENT && parser.getLocalName().equals("tag")) - { - if (event == XMLStreamConstants.START_ELEMENT) - { + while (event != XMLStreamConstants.END_DOCUMENT && parser.getLocalName().equals("tag")) { + if (event == XMLStreamConstants.START_ELEMENT) { // read tag String key = parser.getAttributeValue(null, "k"); String value = parser.getAttributeValue(null, "v"); @@ -88,8 +81,7 @@ private static void readTags( ReaderElement re, XMLStreamReader parser ) throws } } - public static ReaderRelation createRelation( long id, XMLStreamReader parser ) throws XMLStreamException - { + public static ReaderRelation createRelation(long id, XMLStreamReader parser) throws XMLStreamException { ReaderRelation rel = new ReaderRelation(id); parser.nextTag(); @@ -98,13 +90,10 @@ public static ReaderRelation createRelation( long id, XMLStreamReader parser ) t return rel; } - private static void readMembers( List members, XMLStreamReader parser ) throws XMLStreamException - { + private static void readMembers(List members, XMLStreamReader parser) throws XMLStreamException { int event = parser.getEventType(); - while (event != XMLStreamConstants.END_DOCUMENT && parser.getLocalName().equalsIgnoreCase("member")) - { - if (event == XMLStreamConstants.START_ELEMENT) - { + while (event != XMLStreamConstants.END_DOCUMENT && parser.getLocalName().equalsIgnoreCase("member")) { + if (event == XMLStreamConstants.START_ELEMENT) { // read member members.add(createMember(parser)); } @@ -114,10 +103,7 @@ private static void readMembers( List members, XMLStreamReader parser ) } - private static final String TYPE_DECODE = "nwr"; - - public static Member createMember( XMLStreamReader parser ) - { + public static Member createMember(XMLStreamReader parser) { String typeName = parser.getAttributeValue(null, "type"); int type = TYPE_DECODE.indexOf(typeName.charAt(0)); long ref = Long.parseLong(parser.getAttributeValue(null, "ref")); diff --git a/reader-osm/src/main/java/com/graphhopper/reader/osm/pbf/PbfBlobDecoder.java b/reader-osm/src/main/java/com/graphhopper/reader/osm/pbf/PbfBlobDecoder.java index d5a6a89922e..9f289ca2d50 100644 --- a/reader-osm/src/main/java/com/graphhopper/reader/osm/pbf/PbfBlobDecoder.java +++ b/reader-osm/src/main/java/com/graphhopper/reader/osm/pbf/PbfBlobDecoder.java @@ -8,26 +8,25 @@ import com.graphhopper.reader.ReaderWay; import com.graphhopper.reader.osm.OSMFileHeader; import com.graphhopper.util.Helper; +import gnu.trove.list.TLongList; import org.openstreetmap.osmosis.osmbinary.Fileformat; import org.openstreetmap.osmosis.osmbinary.Osmformat; -import gnu.trove.list.TLongList; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.io.IOException; import java.util.*; import java.util.zip.DataFormatException; import java.util.zip.Inflater; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - /** * Converts PBF block data into decoded entities ready to be passed into an Osmosis pipeline. This * class is designed to be passed into a pool of worker threads to allow multi-threaded decoding. *

    + * * @author Brett Henderson */ -public class PbfBlobDecoder implements Runnable -{ +public class PbfBlobDecoder implements Runnable { private static final Logger log = LoggerFactory.getLogger(PbfBlobDecoder.class); private final boolean checkData = false; private final String blobType; @@ -38,62 +37,51 @@ public class PbfBlobDecoder implements Runnable /** * Creates a new instance. *

    + * * @param blobType The type of blob. - * @param rawBlob The raw data of the blob. + * @param rawBlob The raw data of the blob. * @param listener The listener for receiving decoding results. */ - public PbfBlobDecoder( String blobType, byte[] rawBlob, PbfBlobDecoderListener listener ) - { + public PbfBlobDecoder(String blobType, byte[] rawBlob, PbfBlobDecoderListener listener) { this.blobType = blobType; this.rawBlob = rawBlob; this.listener = listener; } - private byte[] readBlobContent() throws IOException - { + private byte[] readBlobContent() throws IOException { Fileformat.Blob blob = Fileformat.Blob.parseFrom(rawBlob); byte[] blobData; - if (blob.hasRaw()) - { + if (blob.hasRaw()) { blobData = blob.getRaw().toByteArray(); - } else if (blob.hasZlibData()) - { + } else if (blob.hasZlibData()) { Inflater inflater = new Inflater(); inflater.setInput(blob.getZlibData().toByteArray()); blobData = new byte[blob.getRawSize()]; - try - { + try { inflater.inflate(blobData); - } catch (DataFormatException e) - { + } catch (DataFormatException e) { throw new RuntimeException("Unable to decompress PBF blob.", e); } - if (!inflater.finished()) - { + if (!inflater.finished()) { throw new RuntimeException("PBF blob contains incomplete compressed data."); } - } else - { + } else { throw new RuntimeException("PBF blob uses unsupported compression, only raw or zlib may be used."); } return blobData; } - private void processOsmHeader( byte[] data ) throws InvalidProtocolBufferException - { + private void processOsmHeader(byte[] data) throws InvalidProtocolBufferException { Osmformat.HeaderBlock header = Osmformat.HeaderBlock.parseFrom(data); // Build the list of active and unsupported features in the file. List supportedFeatures = Arrays.asList("OsmSchema-V0.6", "DenseNodes"); List unsupportedFeatures = new ArrayList(); - for (String feature : header.getRequiredFeaturesList()) - { - if (supportedFeatures.contains(feature)) - { - } else - { + for (String feature : header.getRequiredFeaturesList()) { + if (supportedFeatures.contains(feature)) { + } else { unsupportedFeatures.add(feature); } } @@ -101,8 +89,7 @@ private void processOsmHeader( byte[] data ) throws InvalidProtocolBufferExcepti // We can't continue if there are any unsupported features. We wait // until now so that we can display all unsupported features instead of // just the first one we encounter. - if (unsupportedFeatures.size() > 0) - { + if (unsupportedFeatures.size() > 0) { throw new RuntimeException("PBF file contains unsupported features " + unsupportedFeatures); } @@ -128,14 +115,11 @@ private void processOsmHeader( byte[] data ) throws InvalidProtocolBufferExcepti */ } - private Map buildTags( List keys, List values, PbfFieldDecoder fieldDecoder ) - { + private Map buildTags(List keys, List values, PbfFieldDecoder fieldDecoder) { // Ensure parallel lists are of equal size. - if (checkData) - { - if (keys.size() != values.size()) - { + if (checkData) { + if (keys.size() != values.size()) { throw new RuntimeException("Number of tag keys (" + keys.size() + ") and tag values (" + values.size() + ") don't match"); } @@ -143,11 +127,9 @@ private Map buildTags( List keys, List values, Iterator keyIterator = keys.iterator(); Iterator valueIterator = values.iterator(); - if (keyIterator.hasNext()) - { + if (keyIterator.hasNext()) { Map tags = new HashMap(keys.size()); - while (keyIterator.hasNext()) - { + while (keyIterator.hasNext()) { String key = fieldDecoder.decodeString(keyIterator.next()); String value = fieldDecoder.decodeString(valueIterator.next()); tags.put(key, value); @@ -157,10 +139,8 @@ private Map buildTags( List keys, List values, return null; } - private void processNodes( List nodes, PbfFieldDecoder fieldDecoder ) - { - for (Osmformat.Node node : nodes) - { + private void processNodes(List nodes, PbfFieldDecoder fieldDecoder) { + for (Osmformat.Node node : nodes) { Map tags = buildTags(node.getKeysList(), node.getValsList(), fieldDecoder); ReaderNode osmNode = new ReaderNode(node.getId(), fieldDecoder.decodeLatitude(node @@ -172,17 +152,14 @@ private void processNodes( List nodes, PbfFieldDecoder fieldDeco } } - private void processNodes( Osmformat.DenseNodes nodes, PbfFieldDecoder fieldDecoder ) - { + private void processNodes(Osmformat.DenseNodes nodes, PbfFieldDecoder fieldDecoder) { List idList = nodes.getIdList(); List latList = nodes.getLatList(); List lonList = nodes.getLonList(); // Ensure parallel lists are of equal size. - if (checkData) - { - if ((idList.size() != latList.size()) || (idList.size() != lonList.size())) - { + if (checkData) { + if ((idList.size() != latList.size()) || (idList.size() != lonList.size())) { throw new RuntimeException("Number of ids (" + idList.size() + "), latitudes (" + latList.size() + "), and longitudes (" + lonList.size() + ") don't match"); } @@ -205,8 +182,7 @@ private void processNodes( Osmformat.DenseNodes nodes, PbfFieldDecoder fieldDeco // int userSid = 0; // long timestamp = 0; // long changesetId = 0; - for (int i = 0; i < idList.size(); i++) - { + for (int i = 0; i < idList.size(); i++) { // Delta decode node fields. nodeId += idList.get(i); latitude += latList.get(i); @@ -239,25 +215,20 @@ private void processNodes( Osmformat.DenseNodes nodes, PbfFieldDecoder fieldDeco // in the same PBF array. Each set of tags is delimited by an index // with a value of 0. Map tags = null; - while (keysValuesIterator.hasNext()) - { + while (keysValuesIterator.hasNext()) { int keyIndex = keysValuesIterator.next(); - if (keyIndex == 0) - { + if (keyIndex == 0) { break; } - if (checkData) - { - if (!keysValuesIterator.hasNext()) - { + if (checkData) { + if (!keysValuesIterator.hasNext()) { throw new RuntimeException( "The PBF DenseInfo keys/values list contains a key with no corresponding value."); } } int valueIndex = keysValuesIterator.next(); - if (tags == null) - { + if (tags == null) { tags = new HashMap(); } @@ -272,10 +243,8 @@ private void processNodes( Osmformat.DenseNodes nodes, PbfFieldDecoder fieldDeco } } - private void processWays( List ways, PbfFieldDecoder fieldDecoder ) - { - for (Osmformat.Way way : ways) - { + private void processWays(List ways, PbfFieldDecoder fieldDecoder) { + for (Osmformat.Way way : ways) { Map tags = buildTags(way.getKeysList(), way.getValsList(), fieldDecoder); ReaderWay osmWay = new ReaderWay(way.getId()); osmWay.setTags(tags); @@ -285,8 +254,7 @@ private void processWays( List ways, PbfFieldDecoder fieldDecoder // the previous one. long nodeId = 0; TLongList wayNodes = osmWay.getNodes(); - for (long nodeIdOffset : way.getRefsList()) - { + for (long nodeIdOffset : way.getRefsList()) { nodeId += nodeIdOffset; wayNodes.add(nodeId); } @@ -295,18 +263,15 @@ private void processWays( List ways, PbfFieldDecoder fieldDecoder } } - private void buildRelationMembers( ReaderRelation relation, - List memberIds, List memberRoles, List memberTypes, - PbfFieldDecoder fieldDecoder ) - { + private void buildRelationMembers(ReaderRelation relation, + List memberIds, List memberRoles, List memberTypes, + PbfFieldDecoder fieldDecoder) { List members = relation.getMembers(); // Ensure parallel lists are of equal size. - if (checkData) - { - if ((memberIds.size() != memberRoles.size()) || (memberIds.size() != memberTypes.size())) - { + if (checkData) { + if ((memberIds.size() != memberRoles.size()) || (memberIds.size() != memberTypes.size())) { throw new RuntimeException("Number of member ids (" + memberIds.size() + "), member roles (" + memberRoles.size() + "), and member types (" + memberTypes.size() + ") don't match"); } @@ -320,23 +285,18 @@ private void buildRelationMembers( ReaderRelation relation, // delta encoded meaning that each id is stored as a delta against // the previous one. long refId = 0; - while (memberIdIterator.hasNext()) - { + while (memberIdIterator.hasNext()) { Osmformat.Relation.MemberType memberType = memberTypeIterator.next(); refId += memberIdIterator.next(); int entityType = ReaderRelation.Member.NODE; - if (memberType == Osmformat.Relation.MemberType.WAY) - { + if (memberType == Osmformat.Relation.MemberType.WAY) { entityType = ReaderRelation.Member.WAY; - } else if (memberType == Osmformat.Relation.MemberType.RELATION) - { + } else if (memberType == Osmformat.Relation.MemberType.RELATION) { entityType = ReaderRelation.Member.RELATION; } - if (checkData) - { - if (entityType == ReaderRelation.Member.NODE && memberType != Osmformat.Relation.MemberType.NODE) - { + if (checkData) { + if (entityType == ReaderRelation.Member.NODE && memberType != Osmformat.Relation.MemberType.NODE) { throw new RuntimeException("Member type of " + memberType + " is not supported."); } } @@ -347,10 +307,8 @@ private void buildRelationMembers( ReaderRelation relation, } } - private void processRelations( List relations, PbfFieldDecoder fieldDecoder ) - { - for (Osmformat.Relation relation : relations) - { + private void processRelations(List relations, PbfFieldDecoder fieldDecoder) { + for (Osmformat.Relation relation : relations) { Map tags = buildTags(relation.getKeysList(), relation.getValsList(), fieldDecoder); ReaderRelation osmRelation = new ReaderRelation(relation.getId()); @@ -364,13 +322,11 @@ private void processRelations( List relations, PbfFieldDecod } } - private void processOsmPrimitives( byte[] data ) throws InvalidProtocolBufferException - { + private void processOsmPrimitives(byte[] data) throws InvalidProtocolBufferException { Osmformat.PrimitiveBlock block = Osmformat.PrimitiveBlock.parseFrom(data); PbfFieldDecoder fieldDecoder = new PbfFieldDecoder(block); - for (Osmformat.PrimitiveGroup primitiveGroup : block.getPrimitivegroupList()) - { + for (Osmformat.PrimitiveGroup primitiveGroup : block.getPrimitivegroupList()) { log.debug("Processing OSM primitive group."); processNodes(primitiveGroup.getDense(), fieldDecoder); processNodes(primitiveGroup.getNodesList(), fieldDecoder); @@ -379,37 +335,29 @@ private void processOsmPrimitives( byte[] data ) throws InvalidProtocolBufferExc } } - private void runAndTrapExceptions() - { - try - { + private void runAndTrapExceptions() { + try { decodedEntities = new ArrayList(); - if ("OSMHeader".equals(blobType)) - { + if ("OSMHeader".equals(blobType)) { processOsmHeader(readBlobContent()); - } else if ("OSMData".equals(blobType)) - { + } else if ("OSMData".equals(blobType)) { processOsmPrimitives(readBlobContent()); } else if (log.isDebugEnabled()) log.debug("Skipping unrecognised blob type " + blobType); - } catch (IOException e) - { + } catch (IOException e) { throw new RuntimeException("Unable to process PBF blob", e); } } @Override - public void run() - { - try - { + public void run() { + try { runAndTrapExceptions(); listener.complete(decodedEntities); - } catch (RuntimeException e) - { + } catch (RuntimeException e) { listener.error(e); } } diff --git a/reader-osm/src/main/java/com/graphhopper/reader/osm/pbf/PbfBlobDecoderListener.java b/reader-osm/src/main/java/com/graphhopper/reader/osm/pbf/PbfBlobDecoderListener.java index 0d64a7dc374..423608e9c50 100644 --- a/reader-osm/src/main/java/com/graphhopper/reader/osm/pbf/PbfBlobDecoderListener.java +++ b/reader-osm/src/main/java/com/graphhopper/reader/osm/pbf/PbfBlobDecoderListener.java @@ -8,19 +8,20 @@ /** * Instances of this interface are used to receive results from PBFBlobDecoder. *

    + * * @author Brett Henderson */ -public interface PbfBlobDecoderListener -{ +public interface PbfBlobDecoderListener { /** * Provides the listener with the list of decoded entities. *

    + * * @param decodedEntities The decoded entities. */ - void complete( List decodedEntities ); + void complete(List decodedEntities); /** * Notifies the listener that an error occurred during processing. */ - void error( Exception ex ); + void error(Exception ex); } diff --git a/reader-osm/src/main/java/com/graphhopper/reader/osm/pbf/PbfBlobResult.java b/reader-osm/src/main/java/com/graphhopper/reader/osm/pbf/PbfBlobResult.java index 704e537a75e..78f98a27cd7 100644 --- a/reader-osm/src/main/java/com/graphhopper/reader/osm/pbf/PbfBlobResult.java +++ b/reader-osm/src/main/java/com/graphhopper/reader/osm/pbf/PbfBlobResult.java @@ -1,17 +1,17 @@ // This software is released into the Public Domain. See copying.txt for details. package com.graphhopper.reader.osm.pbf; -import java.util.List; - import com.graphhopper.reader.ReaderElement; +import java.util.List; + /** * Stores the results for a decoded Blob. *

    + * * @author Brett Henderson */ -public class PbfBlobResult -{ +public class PbfBlobResult { private List entities; private boolean complete; private boolean success; @@ -20,8 +20,7 @@ public class PbfBlobResult /** * Creates a new instance. */ - public PbfBlobResult() - { + public PbfBlobResult() { complete = false; success = false; ex = new RuntimeException("no success result stored"); @@ -30,10 +29,10 @@ public PbfBlobResult() /** * Stores the results of a successful blob decoding operation. *

    + * * @param decodedEntities The entities from the blob. */ - public void storeSuccessResult( List decodedEntities ) - { + public void storeSuccessResult(List decodedEntities) { entities = decodedEntities; complete = true; success = true; @@ -42,8 +41,7 @@ public void storeSuccessResult( List decodedEntities ) /** * Stores a failure result for a blob decoding operation. */ - public void storeFailureResult( Exception ex ) - { + public void storeFailureResult(Exception ex) { complete = true; success = false; this.ex = ex; @@ -52,25 +50,24 @@ public void storeFailureResult( Exception ex ) /** * Gets the complete flag. *

    + * * @return True if complete. */ - public boolean isComplete() - { + public boolean isComplete() { return complete; } /** * Gets the success flag. This is only valid after complete becomes true. *

    + * * @return True if successful. */ - public boolean isSuccess() - { + public boolean isSuccess() { return success; } - public Exception getException() - { + public Exception getException() { return ex; } @@ -78,10 +75,10 @@ public Exception getException() * Gets the entities decoded from the blob. This is only valid after complete becomes true, and * if success is true. *

    + * * @return The list of decoded entities. */ - public List getEntities() - { + public List getEntities() { return entities; } } diff --git a/reader-osm/src/main/java/com/graphhopper/reader/osm/pbf/PbfDecoder.java b/reader-osm/src/main/java/com/graphhopper/reader/osm/pbf/PbfDecoder.java index 70319ef91ae..fc2e7f6aeb0 100644 --- a/reader-osm/src/main/java/com/graphhopper/reader/osm/pbf/PbfDecoder.java +++ b/reader-osm/src/main/java/com/graphhopper/reader/osm/pbf/PbfDecoder.java @@ -3,8 +3,6 @@ import com.graphhopper.reader.ReaderElement; -import java.util.Date; - import java.util.LinkedList; import java.util.List; import java.util.Queue; @@ -17,10 +15,10 @@ * Decodes all blocks from a PBF stream using worker threads, and passes the results to the * downstream sink. *

    + * * @author Brett Henderson */ -public class PbfDecoder implements Runnable -{ +public class PbfDecoder implements Runnable { private final PbfStreamSplitter streamSplitter; private final ExecutorService executorService; private final int maxPendingBlobs; @@ -32,14 +30,14 @@ public class PbfDecoder implements Runnable /** * Creates a new instance. *

    - * @param streamSplitter The PBF stream splitter providing the source of blobs to be decoded. + * + * @param streamSplitter The PBF stream splitter providing the source of blobs to be decoded. * @param executorService The executor service managing the thread pool. * @param maxPendingBlobs The maximum number of blobs to have in progress at any point in time. - * @param sink The sink to send all decoded entities to. + * @param sink The sink to send all decoded entities to. */ - public PbfDecoder( PbfStreamSplitter streamSplitter, ExecutorService executorService, int maxPendingBlobs, - Sink sink ) - { + public PbfDecoder(PbfStreamSplitter streamSplitter, ExecutorService executorService, int maxPendingBlobs, + Sink sink) { this.streamSplitter = streamSplitter; this.executorService = executorService; this.maxPendingBlobs = maxPendingBlobs; @@ -57,14 +55,11 @@ public PbfDecoder( PbfStreamSplitter streamSplitter, ExecutorService executorSer * Any thread can call this method when they wish to wait until an update has been performed by * another thread. */ - private void waitForUpdate() - { - try - { + private void waitForUpdate() { + try { dataWaitCondition.await(); - } catch (InterruptedException e) - { + } catch (InterruptedException e) { throw new RuntimeException("Thread was interrupted.", e); } } @@ -73,26 +68,21 @@ private void waitForUpdate() * Any thread can call this method when they wish to signal another thread that an update has * occurred. */ - private void signalUpdate() - { + private void signalUpdate() { dataWaitCondition.signal(); } - private void sendResultsToSink( int targetQueueSize ) - { - while (blobResults.size() > targetQueueSize) - { + private void sendResultsToSink(int targetQueueSize) { + while (blobResults.size() > targetQueueSize) { // Get the next result from the queue and wait for it to complete. PbfBlobResult blobResult = blobResults.remove(); - while (!blobResult.isComplete()) - { + while (!blobResult.isComplete()) { // The thread hasn't finished processing yet so wait for an // update from another thread before checking again. waitForUpdate(); } - if (!blobResult.isSuccess()) - { + if (!blobResult.isSuccess()) { throw new RuntimeException("A PBF decoding worker thread failed, aborting.", blobResult.getException()); } @@ -100,24 +90,19 @@ private void sendResultsToSink( int targetQueueSize ) // for the duration of processing to allow worker threads to post // their results. lock.unlock(); - try - { - for (ReaderElement entity : blobResult.getEntities()) - { + try { + for (ReaderElement entity : blobResult.getEntities()) { sink.process(entity); } - } finally - { + } finally { lock.lock(); } } } - private void processBlobs() - { + private void processBlobs() { // Process until the PBF stream is exhausted. - while (streamSplitter.hasNext()) - { + while (streamSplitter.hasNext()) { // Obtain the next raw blob from the PBF stream. PbfRawBlob rawBlob = streamSplitter.next(); @@ -128,35 +113,28 @@ private void processBlobs() // Create the listener object that will update the blob results // based on an event fired by the blob decoder. - PbfBlobDecoderListener decoderListener = new PbfBlobDecoderListener() - { + PbfBlobDecoderListener decoderListener = new PbfBlobDecoderListener() { @Override - public void error( Exception ex ) - { + public void error(Exception ex) { lock.lock(); - try - { + try { // System.out.println("ERROR: " + new Date()); blobResult.storeFailureResult(ex); signalUpdate(); - } finally - { + } finally { lock.unlock(); } } @Override - public void complete( List decodedEntities ) - { + public void complete(List decodedEntities) { lock.lock(); - try - { + try { blobResult.storeSuccessResult(decodedEntities); signalUpdate(); - } finally - { + } finally { lock.unlock(); } } @@ -177,15 +155,12 @@ public void complete( List decodedEntities ) } @Override - public void run() - { + public void run() { lock.lock(); - try - { + try { processBlobs(); - } finally - { + } finally { lock.unlock(); } } diff --git a/reader-osm/src/main/java/com/graphhopper/reader/osm/pbf/PbfFieldDecoder.java b/reader-osm/src/main/java/com/graphhopper/reader/osm/pbf/PbfFieldDecoder.java index 37798885a38..925c3c9bc94 100644 --- a/reader-osm/src/main/java/com/graphhopper/reader/osm/pbf/PbfFieldDecoder.java +++ b/reader-osm/src/main/java/com/graphhopper/reader/osm/pbf/PbfFieldDecoder.java @@ -1,18 +1,18 @@ // This software is released into the Public Domain. See copying.txt for details. package com.graphhopper.reader.osm.pbf; -import java.util.Date; - import org.openstreetmap.osmosis.osmbinary.Osmformat; +import java.util.Date; + /** * Manages decoding of the lower level PBF data structures. *

    + * * @author Brett Henderson - *

    + *

    */ -public class PbfFieldDecoder -{ +public class PbfFieldDecoder { private static final double COORDINATE_SCALING_FACTOR = 0.000000001; private String[] strings; private int coordGranularity; @@ -23,10 +23,10 @@ public class PbfFieldDecoder /** * Creates a new instance. *

    + * * @param primitiveBlock The primitive block containing the fields to be decoded. */ - public PbfFieldDecoder( Osmformat.PrimitiveBlock primitiveBlock ) - { + public PbfFieldDecoder(Osmformat.PrimitiveBlock primitiveBlock) { this.coordGranularity = primitiveBlock.getGranularity(); this.coordLatitudeOffset = primitiveBlock.getLatOffset(); this.coordLongitudeOffset = primitiveBlock.getLonOffset(); @@ -34,8 +34,7 @@ public PbfFieldDecoder( Osmformat.PrimitiveBlock primitiveBlock ) Osmformat.StringTable stringTable = primitiveBlock.getStringtable(); strings = new String[stringTable.getSCount()]; - for (int i = 0; i < strings.length; i++) - { + for (int i = 0; i < strings.length; i++) { strings[i] = stringTable.getS(i).toStringUtf8(); } } @@ -43,44 +42,44 @@ public PbfFieldDecoder( Osmformat.PrimitiveBlock primitiveBlock ) /** * Decodes a raw latitude value into degrees. *

    + * * @param rawLatitude The PBF encoded value. * @return The latitude in degrees. */ - public double decodeLatitude( long rawLatitude ) - { + public double decodeLatitude(long rawLatitude) { return COORDINATE_SCALING_FACTOR * (coordLatitudeOffset + (coordGranularity * rawLatitude)); } /** * Decodes a raw longitude value into degrees. *

    + * * @param rawLongitude The PBF encoded value. * @return The longitude in degrees. */ - public double decodeLongitude( long rawLongitude ) - { + public double decodeLongitude(long rawLongitude) { return COORDINATE_SCALING_FACTOR * (coordLongitudeOffset + (coordGranularity * rawLongitude)); } /** * Decodes a raw timestamp value into a Date. *

    + * * @param rawTimestamp The PBF encoded timestamp. * @return The timestamp as a Date. */ - public Date decodeTimestamp( long rawTimestamp ) - { + public Date decodeTimestamp(long rawTimestamp) { return new Date(dateGranularity * rawTimestamp); } /** * Decodes a raw string into a String. *

    + * * @param rawString The PBF encoding string. * @return The string as a String. */ - public String decodeString( int rawString ) - { + public String decodeString(int rawString) { return strings[rawString]; } } diff --git a/reader-osm/src/main/java/com/graphhopper/reader/osm/pbf/PbfRawBlob.java b/reader-osm/src/main/java/com/graphhopper/reader/osm/pbf/PbfRawBlob.java index 360fa81340e..fd1c127a2cf 100644 --- a/reader-osm/src/main/java/com/graphhopper/reader/osm/pbf/PbfRawBlob.java +++ b/reader-osm/src/main/java/com/graphhopper/reader/osm/pbf/PbfRawBlob.java @@ -5,22 +5,22 @@ * Represents a single piece of raw blob data extracted from the PBF stream. It has not yet been * decoded into a PBF blob object. *

    + * * @author Brett Henderson */ -public class PbfRawBlob -{ +public class PbfRawBlob { private String type; private byte[] data; /** * Creates a new instance. *

    + * * @param type The type of data represented by this blob. This corresponds to the type field in - * the blob header. + * the blob header. * @param data The raw contents of the blob in binary undecoded form. */ - public PbfRawBlob( String type, byte[] data ) - { + public PbfRawBlob(String type, byte[] data) { this.type = type; this.data = data; } @@ -29,20 +29,20 @@ public PbfRawBlob( String type, byte[] data ) * Gets the type of data represented by this blob. This corresponds to the type field in the * blob header. *

    + * * @return The blob type. */ - public String getType() - { + public String getType() { return type; } /** * Gets the raw contents of the blob in binary undecoded form. *

    + * * @return The raw blob data. */ - public byte[] getData() - { + public byte[] getData() { return data; } } diff --git a/reader-osm/src/main/java/com/graphhopper/reader/osm/pbf/PbfReader.java b/reader-osm/src/main/java/com/graphhopper/reader/osm/pbf/PbfReader.java index 26f2e66a67d..7663c0ac038 100644 --- a/reader-osm/src/main/java/com/graphhopper/reader/osm/pbf/PbfReader.java +++ b/reader-osm/src/main/java/com/graphhopper/reader/osm/pbf/PbfReader.java @@ -9,10 +9,10 @@ /** * An OSM data source reading from a PBF file. The entire contents of the file are read. *

    + * * @author Brett Henderson */ -public class PbfReader implements Runnable -{ +public class PbfReader implements Runnable { private InputStream inputStream; private Sink sink; private int workers; @@ -20,22 +20,20 @@ public class PbfReader implements Runnable /** * Creates a new instance. *

    - * @param in The file to read. + * + * @param in The file to read. * @param workers The number of worker threads for decoding PBF blocks. */ - public PbfReader( InputStream in, Sink sink, int workers ) - { + public PbfReader(InputStream in, Sink sink, int workers) { this.inputStream = in; this.sink = sink; this.workers = workers; } @Override - public void run() - { + public void run() { ExecutorService executorService = Executors.newFixedThreadPool(workers); - try - { + try { // Create a stream splitter to break the PBF stream into blobs. PbfStreamSplitter streamSplitter = new PbfStreamSplitter(new DataInputStream(inputStream)); @@ -48,11 +46,9 @@ public void run() PbfDecoder pbfDecoder = new PbfDecoder(streamSplitter, executorService, workers + 1, sink); pbfDecoder.run(); - } catch (Exception e) - { + } catch (Exception e) { throw new RuntimeException("Unable to read PBF file.", e); - } finally - { + } finally { sink.complete(); executorService.shutdownNow(); } diff --git a/reader-osm/src/main/java/com/graphhopper/reader/osm/pbf/PbfStreamSplitter.java b/reader-osm/src/main/java/com/graphhopper/reader/osm/pbf/PbfStreamSplitter.java index f2a7120d286..8c73ac02197 100644 --- a/reader-osm/src/main/java/com/graphhopper/reader/osm/pbf/PbfStreamSplitter.java +++ b/reader-osm/src/main/java/com/graphhopper/reader/osm/pbf/PbfStreamSplitter.java @@ -14,10 +14,10 @@ * Parses a PBF data stream and extracts the raw data of each blob in sequence until the end of the * stream is reached. *

    + * * @author Brett Henderson */ -public class PbfStreamSplitter implements Iterator -{ +public class PbfStreamSplitter implements Iterator { private static Logger log = Logger.getLogger(PbfStreamSplitter.class.getName()); private DataInputStream dis; private int dataBlockCount; @@ -27,17 +27,16 @@ public class PbfStreamSplitter implements Iterator /** * Creates a new instance. *

    + * * @param pbfStream The PBF data stream to be parsed. */ - public PbfStreamSplitter( DataInputStream pbfStream ) - { + public PbfStreamSplitter(DataInputStream pbfStream) { dis = pbfStream; dataBlockCount = 0; eof = false; } - private Fileformat.BlobHeader readHeader( int headerLength ) throws IOException - { + private Fileformat.BlobHeader readHeader(int headerLength) throws IOException { byte[] headerBuffer = new byte[headerLength]; dis.readFully(headerBuffer); @@ -46,8 +45,7 @@ private Fileformat.BlobHeader readHeader( int headerLength ) throws IOException return blobHeader; } - private byte[] readRawBlob( Fileformat.BlobHeader blobHeader ) throws IOException - { + private byte[] readRawBlob(Fileformat.BlobHeader blobHeader) throws IOException { byte[] rawBlob = new byte[blobHeader.getDatasize()]; dis.readFully(rawBlob); @@ -55,48 +53,39 @@ private byte[] readRawBlob( Fileformat.BlobHeader blobHeader ) throws IOExceptio return rawBlob; } - private void getNextBlob() - { - try - { + private void getNextBlob() { + try { // Read the length of the next header block. This is the only time // we should expect to encounter an EOF exception. In all other // cases it indicates a corrupt or truncated file. int headerLength; - try - { + try { headerLength = dis.readInt(); - } catch (EOFException e) - { + } catch (EOFException e) { eof = true; return; } - if (log.isLoggable(Level.FINER)) - { + if (log.isLoggable(Level.FINER)) { log.finer("Reading header for blob " + dataBlockCount++); } Fileformat.BlobHeader blobHeader = readHeader(headerLength); - if (log.isLoggable(Level.FINER)) - { + if (log.isLoggable(Level.FINER)) { log.finer("Processing blob of type " + blobHeader.getType() + "."); } byte[] blobData = readRawBlob(blobHeader); nextBlob = new PbfRawBlob(blobHeader.getType(), blobData); - } catch (IOException e) - { + } catch (IOException e) { throw new RuntimeException("Unable to get next blob from PBF stream.", e); } } @Override - public boolean hasNext() - { - if (nextBlob == null && !eof) - { + public boolean hasNext() { + if (nextBlob == null && !eof) { getNextBlob(); } @@ -104,8 +93,7 @@ public boolean hasNext() } @Override - public PbfRawBlob next() - { + public PbfRawBlob next() { PbfRawBlob result = nextBlob; nextBlob = null; @@ -113,20 +101,15 @@ public PbfRawBlob next() } @Override - public void remove() - { + public void remove() { throw new UnsupportedOperationException(); } - public void release() - { - if (dis != null) - { - try - { + public void release() { + if (dis != null) { + try { dis.close(); - } catch (IOException e) - { + } catch (IOException e) { log.log(Level.SEVERE, "Unable to close PBF stream.", e); } } diff --git a/reader-osm/src/main/java/com/graphhopper/reader/osm/pbf/Sink.java b/reader-osm/src/main/java/com/graphhopper/reader/osm/pbf/Sink.java index 480e9f67f3e..31849c41a0b 100644 --- a/reader-osm/src/main/java/com/graphhopper/reader/osm/pbf/Sink.java +++ b/reader-osm/src/main/java/com/graphhopper/reader/osm/pbf/Sink.java @@ -22,9 +22,8 @@ /** * @author Nop */ -public interface Sink -{ - void process( ReaderElement item ); +public interface Sink { + void process(ReaderElement item); void complete(); } diff --git a/reader-osm/src/test/java/com/graphhopper/GraphHopperAPITest.java b/reader-osm/src/test/java/com/graphhopper/GraphHopperAPITest.java index 1082ca41693..6ae541b494b 100644 --- a/reader-osm/src/test/java/com/graphhopper/GraphHopperAPITest.java +++ b/reader-osm/src/test/java/com/graphhopper/GraphHopperAPITest.java @@ -19,7 +19,9 @@ import com.graphhopper.reader.osm.GraphHopperOSM; import com.graphhopper.routing.util.EncodingManager; -import com.graphhopper.storage.*; +import com.graphhopper.storage.GraphBuilder; +import com.graphhopper.storage.GraphHopperStorage; +import com.graphhopper.storage.NodeAccess; import com.graphhopper.util.PointList; import org.junit.Test; @@ -28,13 +30,11 @@ /** * @author Peter Karich */ -public class GraphHopperAPITest -{ +public class GraphHopperAPITest { final EncodingManager encodingManager = new EncodingManager("car"); @Test - public void testLoad() - { + public void testLoad() { GraphHopperStorage graph = new GraphBuilder(encodingManager).create(); NodeAccess na = graph.getNodeAccess(); na.setNode(0, 42, 10); @@ -57,7 +57,7 @@ public void testLoad() assertFalse(rsp.hasErrors()); PathWrapper arsp = rsp.getBest(); assertEquals(80, arsp.getDistance(), 1e-6); - + PointList points = arsp.getPoints(); assertEquals(42, points.getLatitude(0), 1e-5); assertEquals(10.4, points.getLongitude(0), 1e-5); @@ -68,8 +68,7 @@ public void testLoad() } @Test - public void testDisconnected179() - { + public void testDisconnected179() { GraphHopperStorage graph = new GraphBuilder(encodingManager).create(); NodeAccess na = graph.getNodeAccess(); na.setNode(0, 42, 10); @@ -87,39 +86,32 @@ public void testDisconnected179() GHResponse rsp = instance.route(new GHRequest(42, 10, 42, 10.4)); assertTrue(rsp.hasErrors()); - try - { + try { rsp.getBest().getPoints(); assertTrue(false); - } catch (Exception ex) - { + } catch (Exception ex) { } instance.close(); } @Test - public void testNoLoad() - { + public void testNoLoad() { GraphHopper instance = new GraphHopper(). setStoreOnFlush(false). setEncodingManager(encodingManager).setCHEnabled(false); - try - { + try { instance.route(new GHRequest(42, 10.4, 42, 10)); assertTrue(false); - } catch (Exception ex) - { + } catch (Exception ex) { assertTrue(ex.getMessage(), ex.getMessage().startsWith("Do a successful call to load or importOrLoad before routing")); } instance = new GraphHopper().setEncodingManager(encodingManager); - try - { + try { instance.route(new GHRequest(42, 10.4, 42, 10)); assertTrue(false); - } catch (Exception ex) - { + } catch (Exception ex) { assertTrue(ex.getMessage(), ex.getMessage().startsWith("Do a successful call to load or importOrLoad before routing")); } } diff --git a/reader-osm/src/test/java/com/graphhopper/GraphHopperIT.java b/reader-osm/src/test/java/com/graphhopper/GraphHopperIT.java index 9d32b0680bd..7dd13c1290c 100644 --- a/reader-osm/src/test/java/com/graphhopper/GraphHopperIT.java +++ b/reader-osm/src/test/java/com/graphhopper/GraphHopperIT.java @@ -21,7 +21,6 @@ import com.graphhopper.reader.osm.GraphHopperOSM; import com.graphhopper.routing.util.EncodingManager; import com.graphhopper.util.*; -import static com.graphhopper.util.Parameters.Algorithms.*; import com.graphhopper.util.Parameters.CH; import com.graphhopper.util.Parameters.Routing; import com.graphhopper.util.shapes.GHPoint; @@ -32,44 +31,30 @@ import java.util.List; import java.util.Map; +import static com.graphhopper.util.Parameters.Algorithms.*; import static org.junit.Assert.*; /** * @author Peter Karich */ -public class GraphHopperIT -{ +public class GraphHopperIT { public static final String DIR = "../core/files"; - private static GraphHopper hopper; private static final String graphFileFoot = "target/graphhopperIT-foot"; private static final String osmFile = DIR + "/monaco.osm.gz"; private static final String importVehicles = "foot"; private static final String vehicle = "foot"; private static final String weightCalcStr = "shortest"; - + private static GraphHopper hopper; private final String tmpGraphFile = "target/graphhopperIT-tmp"; - @Before - public void setUp() - { - Helper.removeDir(new File(tmpGraphFile)); - } - - @After - public void tearDown() - { - Helper.removeDir(new File(tmpGraphFile)); - } - @BeforeClass - public static void beforeClass() - { + public static void beforeClass() { // make sure we are using fresh graphhopper files with correct vehicle Helper.removeDir(new File(graphFileFoot)); hopper = new GraphHopperOSM(). setOSMFile(osmFile). - setStoreOnFlush(true). + setStoreOnFlush(true). setCHEnabled(false). setGraphHopperLocation(graphFileFoot). setEncodingManager(new EncodingManager(importVehicles)). @@ -77,14 +62,22 @@ public static void beforeClass() } @AfterClass - public static void afterClass() - { + public static void afterClass() { Helper.removeDir(new File(graphFileFoot)); } + @Before + public void setUp() { + Helper.removeDir(new File(tmpGraphFile)); + } + + @After + public void tearDown() { + Helper.removeDir(new File(tmpGraphFile)); + } + @Test - public void testMonacoWithInstructions() throws Exception - { + public void testMonacoWithInstructions() throws Exception { GHResponse rsp = hopper.route(new GHRequest(43.727687, 7.418737, 43.74958, 7.436566). setAlgorithm(ASTAR).setVehicle(vehicle).setWeighting(weightCalcStr)); @@ -131,8 +124,7 @@ public void testMonacoWithInstructions() throws Exception } @Test - public void testAlternativeRoutes() - { + public void testAlternativeRoutes() { GHRequest req = new GHRequest(43.729057, 7.41251, 43.740298, 7.423561). setAlgorithm(ALT_ROUTE).setVehicle(vehicle).setWeighting(weightCalcStr); @@ -155,8 +147,7 @@ public void testAlternativeRoutes() } @Test - public void testAlternativeRoutesBikeAndCar() - { + public void testAlternativeRoutesBikeAndCar() { GraphHopper tmpHopper = new GraphHopperOSM(). setOSMFile(DIR + "/north-bayreuth.osm.gz"). setCHEnabled(false). @@ -194,8 +185,7 @@ public void testAlternativeRoutesBikeAndCar() } @Test - public void testMonacoVia() - { + public void testMonacoVia() { GHResponse rsp = hopper.route(new GHRequest(). addPoint(new GHPoint(43.727687, 7.418737)). addPoint(new GHPoint(43.74958, 7.436566)). @@ -268,8 +258,7 @@ public void testMonacoVia() } @Test - public void testMonacoEnforcedDirection() - { + public void testMonacoEnforcedDirection() { GHRequest req = new GHRequest(). addPoint(new GHPoint(43.741069, 7.426854), 0.). addPoint(new GHPoint(43.744445, 7.429483), 190.). @@ -283,8 +272,7 @@ public void testMonacoEnforcedDirection() } @Test - public void testMonacoMaxVisitedNodes() - { + public void testMonacoMaxVisitedNodes() { GHPoint from = new GHPoint(43.741069, 7.426854); GHPoint to = new GHPoint(43.744445, 7.429483); GHRequest req = new GHRequest(). @@ -306,8 +294,7 @@ public void testMonacoMaxVisitedNodes() } @Test - public void testMonacoStraightVia() - { + public void testMonacoStraightVia() { GHRequest rq = new GHRequest(). addPoint(new GHPoint(43.741069, 7.426854)). addPoint(new GHPoint(43.740371, 7.426946)). @@ -322,8 +309,7 @@ public void testMonacoStraightVia() } @Test - public void testSRTMWithInstructions() throws Exception - { + public void testSRTMWithInstructions() throws Exception { GraphHopper tmpHopper = new GraphHopperOSM(). setOSMFile(osmFile). setStoreOnFlush(true). @@ -377,8 +363,7 @@ public void testSRTMWithInstructions() throws Exception } @Test - public void testKremsCyclewayInstructionsWithWayTypeInfo() - { + public void testKremsCyclewayInstructionsWithWayTypeInfo() { String tmpOsmFile = DIR + "/krems.osm.gz"; String tmpVehicle = "bike"; String tmpImportVehicles = "car,bike"; @@ -423,8 +408,7 @@ public void testKremsCyclewayInstructionsWithWayTypeInfo() } @Test - public void testRoundaboutInstructionsWithCH() - { + public void testRoundaboutInstructionsWithCH() { String tmpOsmFile = DIR + "/monaco.osm.gz"; String tmpVehicle = "car"; String tmpImportVehicles = "car,bike"; @@ -432,7 +416,7 @@ public void testRoundaboutInstructionsWithCH() GraphHopper tmpHopper = new GraphHopperOSM(). setOSMFile(tmpOsmFile). - setStoreOnFlush(true). + setStoreOnFlush(true). setGraphHopperLocation(tmpGraphFile). setEncodingManager(new EncodingManager(tmpImportVehicles)). importOrLoad(); @@ -464,12 +448,11 @@ public void testRoundaboutInstructionsWithCH() } @Test - public void testMultipleVehiclesWithCH() - { + public void testMultipleVehiclesWithCH() { String tmpOsmFile = DIR + "/monaco.osm.gz"; GraphHopper tmpHopper = new GraphHopperOSM(). setOSMFile(tmpOsmFile). - setStoreOnFlush(true). + setStoreOnFlush(true). setGraphHopperLocation(tmpGraphFile). setEncodingManager(new EncodingManager("bike,car")). importOrLoad(); @@ -481,7 +464,7 @@ public void testMultipleVehiclesWithCH() // new instance, try different order, resulting only in different default vehicle tmpHopper = new GraphHopperOSM(). setOSMFile(tmpOsmFile). - setStoreOnFlush(true). + setStoreOnFlush(true). setGraphHopperLocation(tmpGraphFile). setEncodingManager(new EncodingManager("car,bike")). importOrLoad(); @@ -490,8 +473,7 @@ public void testMultipleVehiclesWithCH() tmpHopper.close(); } - private void checkMultiVehiclesWithCH( GraphHopper tmpHopper ) - { + private void checkMultiVehiclesWithCH(GraphHopper tmpHopper) { String str = tmpHopper.getEncodingManager().toString(); GHResponse rsp = tmpHopper.route(new GHRequest(43.73005, 7.415707, 43.741522, 7.42826) .setVehicle("car")); @@ -521,8 +503,7 @@ private void checkMultiVehiclesWithCH( GraphHopper tmpHopper ) } @Test - public void testIfCHIsUsed() throws Exception - { + public void testIfCHIsUsed() throws Exception { // route directly after import executeCHFootRoute(); @@ -530,14 +511,13 @@ public void testIfCHIsUsed() throws Exception executeCHFootRoute(); } - private void executeCHFootRoute() - { + private void executeCHFootRoute() { String tmpOsmFile = DIR + "/monaco.osm.gz"; String tmpImportVehicles = "foot"; GraphHopper tmpHopper = new GraphHopperOSM(). setOSMFile(tmpOsmFile). - setStoreOnFlush(true). + setStoreOnFlush(true). setGraphHopperLocation(tmpGraphFile). setEncodingManager(new EncodingManager(tmpImportVehicles)); tmpHopper.getCHFactoryDecorator().setWeightingsAsStrings(weightCalcStr); @@ -559,8 +539,7 @@ private void executeCHFootRoute() } @Test - public void testRoundTour() - { + public void testRoundTour() { GHRequest rq = new GHRequest(). addPoint(new GHPoint(43.741069, 7.426854)). setVehicle(vehicle).setWeighting("fastest"). @@ -579,13 +558,12 @@ public void testRoundTour() } @Test - public void testFlexMode_631() - { + public void testFlexMode_631() { String tmpOsmFile = DIR + "/monaco.osm.gz"; GraphHopper tmpHopper = new GraphHopperOSM(). setOSMFile(tmpOsmFile). - setStoreOnFlush(true). + setStoreOnFlush(true). setGraphHopperLocation(tmpGraphFile). setEncodingManager(new EncodingManager("car")); @@ -617,12 +595,11 @@ public void testFlexMode_631() } @Test - public void testTurnCostsOnOff() - { + public void testTurnCostsOnOff() { GraphHopper tmpHopper = new GraphHopperOSM(). setOSMFile(DIR + "/moscow.osm.gz"). setStoreOnFlush(true). - setCHEnabled(false). + setCHEnabled(false). setGraphHopperLocation(tmpGraphFile). setEncodingManager(new EncodingManager("car|turn_costs=true")); tmpHopper.importOrLoad(); @@ -644,11 +621,10 @@ public void testTurnCostsOnOff() } @Test - public void testCHAndTurnCostsWithFlexmode() - { + public void testCHAndTurnCostsWithFlexmode() { GraphHopper tmpHopper = new GraphHopperOSM(). setOSMFile(DIR + "/moscow.osm.gz"). - setStoreOnFlush(true). + setStoreOnFlush(true). setGraphHopperLocation(tmpGraphFile). setEncodingManager(new EncodingManager("car|turn_costs=true")); tmpHopper.getCHFactoryDecorator().setDisablingAllowed(true); diff --git a/reader-osm/src/test/java/com/graphhopper/reader/osm/GraphHopperOSMTest.java b/reader-osm/src/test/java/com/graphhopper/reader/osm/GraphHopperOSMTest.java index cc642dc8451..217d5823b60 100644 --- a/reader-osm/src/test/java/com/graphhopper/reader/osm/GraphHopperOSMTest.java +++ b/reader-osm/src/test/java/com/graphhopper/reader/osm/GraphHopperOSMTest.java @@ -33,7 +33,6 @@ import com.graphhopper.util.CmdArgs; import com.graphhopper.util.Helper; import com.graphhopper.util.Instruction; -import static com.graphhopper.util.Parameters.Algorithms.*; import com.graphhopper.util.Parameters.Routing; import com.graphhopper.util.shapes.GHPoint; import org.junit.After; @@ -51,35 +50,33 @@ import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicReference; +import static com.graphhopper.util.Parameters.Algorithms.DIJKSTRA; +import static com.graphhopper.util.Parameters.Algorithms.DIJKSTRA_BI; import static org.junit.Assert.*; /** * @author Peter Karich */ -public class GraphHopperOSMTest -{ +public class GraphHopperOSMTest { private static final String ghLoc = "./target/tmp/ghosm"; private static final String testOsm = "./src/test/resources/com/graphhopper/reader/osm/test-osm.xml"; private static final String testOsm3 = "./src/test/resources/com/graphhopper/reader/osm/test-osm3.xml"; private GraphHopper instance; @Before - public void setUp() - { + public void setUp() { Helper.removeDir(new File(ghLoc)); } @After - public void tearDown() - { + public void tearDown() { if (instance != null) instance.close(); Helper.removeDir(new File(ghLoc)); } @Test - public void testLoadOSM() - { + public void testLoadOSM() { GraphHopper closableInstance = new GraphHopperOSM(). setStoreOnFlush(true). setEncodingManager(new EncodingManager("car")). @@ -101,28 +98,23 @@ public void testLoadOSM() assertEquals(3, rsp.getBest().getPoints().getSize()); closableInstance.close(); - try - { + try { rsp = closableInstance.route(new GHRequest(51.2492152, 9.4317166, 51.2, 9.4)); assertTrue(false); - } catch (Exception ex) - { + } catch (Exception ex) { assertEquals("You need to create a new GraphHopper instance as it is already closed", ex.getMessage()); } - try - { + try { closableInstance.getLocationIndex().findClosest(51.2492152, 9.4317166, EdgeFilter.ALL_EDGES); assertTrue(false); - } catch (Exception ex) - { + } catch (Exception ex) { assertEquals("You need to create a new LocationIndex instance as it is already closed", ex.getMessage()); } } @Test - public void testLoadOSMNoCH() - { + public void testLoadOSMNoCH() { GraphHopper gh = new GraphHopperOSM().setStoreOnFlush(true).setCHEnabled(false). setEncodingManager(new EncodingManager("car")). setGraphHopperLocation(ghLoc). @@ -155,8 +147,7 @@ public void testLoadOSMNoCH() } @Test - public void testLoadingWithDifferentCHConfig_issue471() - { + public void testLoadingWithDifferentCHConfig_issue471() { // with CH should not be loadable without CH configured GraphHopper gh = new GraphHopperOSM().setStoreOnFlush(true). setEncodingManager(new EncodingManager("car")). @@ -170,12 +161,10 @@ public void testLoadingWithDifferentCHConfig_issue471() gh = new GraphHopperOSM().setStoreOnFlush(true).setCHEnabled(false). setEncodingManager(new EncodingManager("car")); - try - { + try { gh.load(ghLoc); assertTrue(false); - } catch (Exception ex) - { + } catch (Exception ex) { assertTrue(ex.getMessage(), ex.getMessage().startsWith("Configured graph.ch.weightings:")); } @@ -194,19 +183,16 @@ public void testLoadingWithDifferentCHConfig_issue471() gh = new GraphHopperOSM().setStoreOnFlush(true). setEncodingManager(new EncodingManager("car")); - try - { + try { gh.load(ghLoc); assertTrue(false); - } catch (Exception ex) - { + } catch (Exception ex) { assertTrue(ex.getMessage(), ex.getMessage().startsWith("Configured graph.ch.weightings:")); } } @Test - public void testAllowMultipleReadingInstances() - { + public void testAllowMultipleReadingInstances() { GraphHopper instance1 = new GraphHopperOSM().setStoreOnFlush(true). setEncodingManager(new EncodingManager("car")). setGraphHopperLocation(ghLoc). @@ -229,21 +215,16 @@ public void testAllowMultipleReadingInstances() } @Test - public void testDoNotAllowWritingAndLoadingAtTheSameTime() throws Exception - { + public void testDoNotAllowWritingAndLoadingAtTheSameTime() throws Exception { final CountDownLatch latch1 = new CountDownLatch(1); final CountDownLatch latch2 = new CountDownLatch(1); - final GraphHopper instance1 = new GraphHopperOSM() - { + final GraphHopper instance1 = new GraphHopperOSM() { @Override - protected DataReader importData() throws IOException - { - try - { + protected DataReader importData() throws IOException { + try { latch2.countDown(); latch1.await(3, TimeUnit.SECONDS); - } catch (InterruptedException ex) - { + } catch (InterruptedException ex) { } return super.importData(); } @@ -252,16 +233,12 @@ protected DataReader importData() throws IOException setGraphHopperLocation(ghLoc). setDataReaderFile(testOsm); final AtomicReference ar = new AtomicReference(); - Thread thread = new Thread() - { + Thread thread = new Thread() { @Override - public void run() - { - try - { + public void run() { + try { instance1.importOrLoad(); - } catch (Exception ex) - { + } catch (Exception ex) { ar.set(ex); } } @@ -271,19 +248,16 @@ public void run() GraphHopper instance2 = new GraphHopperOSM().setStoreOnFlush(true). setEncodingManager(new EncodingManager("car")). setDataReaderFile(testOsm); - try - { + try { // let thread reach the CountDownLatch latch2.await(3, TimeUnit.SECONDS); // now importOrLoad should have create a lock which this load call does not like instance2.load(ghLoc); assertTrue(false); - } catch (RuntimeException ex) - { + } catch (RuntimeException ex) { assertNotNull(ex); assertTrue(ex.getMessage(), ex.getMessage().startsWith("To avoid reading partial data")); - } finally - { + } finally { instance2.close(); latch1.countDown(); // make sure the import process wasn't interrupted and no other error happened @@ -296,8 +270,7 @@ public void run() } @Test - public void testPrepare() - { + public void testPrepare() { instance = new GraphHopperOSM(). setStoreOnFlush(false). setEncodingManager(new EncodingManager("car")). @@ -313,8 +286,7 @@ public void testPrepare() } @Test - public void testSortedGraph_noCH() - { + public void testSortedGraph_noCH() { instance = new GraphHopperOSM().setStoreOnFlush(false). setSortGraph(true). setEncodingManager(new EncodingManager("car")).setCHEnabled(false). @@ -341,8 +313,7 @@ public void testSortedGraph_noCH() } @Test - public void testFootAndCar() - { + public void testFootAndCar() { // now all ways are imported instance = new GraphHopperOSM().setStoreOnFlush(false). setEncodingManager(new EncodingManager("car,foot")).setCHEnabled(false). @@ -387,8 +358,7 @@ public void testFootAndCar() } @Test - public void testFailsForWrongConfig() throws IOException - { + public void testFailsForWrongConfig() throws IOException { instance = new GraphHopperOSM().init( new CmdArgs(). put("datareader.file", testOsm3). @@ -401,8 +371,7 @@ public void testFailsForWrongConfig() throws IOException instance.close(); // different config (flagEncoder list) - try - { + try { GraphHopper tmpGH = new GraphHopperOSM().init( new CmdArgs(). put("datareader.file", testOsm3). @@ -412,8 +381,7 @@ public void testFailsForWrongConfig() throws IOException setDataReaderFile(testOsm3); tmpGH.load(ghLoc); assertTrue(false); - } catch (Exception ex) - { + } catch (Exception ex) { assertTrue(ex.getMessage(), ex.getMessage().startsWith("Encoding does not match")); } @@ -426,18 +394,15 @@ public void testFailsForWrongConfig() throws IOException put("graph.bytes_for_flags", 8). put("prepare.ch.weightings", "no")). setDataReaderFile(testOsm3); - try - { + try { instance.load(ghLoc); assertTrue(false); - } catch (Exception ex) - { + } catch (Exception ex) { assertTrue(ex.getMessage(), ex.getMessage().startsWith("Configured graph.bytes_for_flags (8) is not equal to loaded 4")); } // different order is no longer okay, see #350 - try - { + try { GraphHopper tmpGH = new GraphHopperOSM().init(new CmdArgs(). put("datareader.file", testOsm3). put("datareader.dataaccess", "RAM"). @@ -446,34 +411,29 @@ public void testFailsForWrongConfig() throws IOException setDataReaderFile(testOsm3); tmpGH.load(ghLoc); assertTrue(false); - } catch (Exception ex) - { + } catch (Exception ex) { assertTrue(ex.getMessage(), ex.getMessage().startsWith("Encoding does not match")); } } @Test - public void testNoNPE_ifLoadNotSuccessful() - { + public void testNoNPE_ifLoadNotSuccessful() { instance = new GraphHopperOSM(). setStoreOnFlush(true). setEncodingManager(new EncodingManager("car")); - try - { + try { // loading from empty directory new File(ghLoc).mkdirs(); assertFalse(instance.load(ghLoc)); instance.route(new GHRequest(10, 40, 12, 32)); assertTrue(false); - } catch (IllegalStateException ex) - { + } catch (IllegalStateException ex) { assertEquals("Do a successful call to load or importOrLoad before routing", ex.getMessage()); } } @Test - public void testDoesNotCreateEmptyFolderIfLoadingFromNonExistingPath() - { + public void testDoesNotCreateEmptyFolderIfLoadingFromNonExistingPath() { instance = new GraphHopperOSM(). setEncodingManager(new EncodingManager("car")); @@ -482,37 +442,30 @@ public void testDoesNotCreateEmptyFolderIfLoadingFromNonExistingPath() } @Test - public void testFailsForMissingParameters() throws IOException - { - class GHTmp extends GraphHopperOSM - { + public void testFailsForMissingParameters() throws IOException { + class GHTmp extends GraphHopperOSM { @Override - public DataReader importData() throws IOException - { + public DataReader importData() throws IOException { return super.importData(); } } // missing load of graph GHTmp tmp = new GHTmp(); - try - { + try { tmp.setDataReaderFile(testOsm); tmp.importData(); assertTrue(false); - } catch (IllegalStateException ex) - { + } catch (IllegalStateException ex) { assertEquals("Load graph before importing OSM data", ex.getMessage()); } // missing graph location instance = new GraphHopperOSM(); - try - { + try { instance.importOrLoad(); assertTrue(false); - } catch (IllegalStateException ex) - { + } catch (IllegalStateException ex) { assertEquals("GraphHopperLocation is not specified. Call setGraphHopperLocation or init before", ex.getMessage()); } @@ -521,12 +474,10 @@ public DataReader importData() throws IOException setStoreOnFlush(true). setEncodingManager(new EncodingManager("car")). setGraphHopperLocation(ghLoc); - try - { + try { instance.importOrLoad(); assertTrue(false); - } catch (IllegalStateException ex) - { + } catch (IllegalStateException ex) { assertEquals("Couldn't load from existing folder: " + ghLoc + " but also cannot use file for DataReader as it wasn't specified!", ex.getMessage()); } @@ -536,12 +487,10 @@ public DataReader importData() throws IOException setStoreOnFlush(true). setGraphHopperLocation(ghLoc). setDataReaderFile(testOsm3); - try - { + try { instance.importOrLoad(); assertTrue(false); - } catch (IllegalStateException ex) - { + } catch (IllegalStateException ex) { assertTrue(ex.getMessage(), ex.getMessage().startsWith("Cannot load properties to fetch EncodingManager")); } @@ -550,20 +499,17 @@ public DataReader importData() throws IOException setStoreOnFlush(false). setEncodingManager(new EncodingManager("car")). setGraphHopperLocation(ghLoc); - try - { + try { instance.importOrLoad(); assertTrue(false); - } catch (Exception ex) - { + } catch (Exception ex) { assertEquals("Couldn't load from existing folder: " + ghLoc + " but also cannot use file for DataReader as it wasn't specified!", ex.getMessage()); } } @Test - public void testFootOnly() - { + public void testFootOnly() { // now only footable ways are imported => no A D C and B D E => the other both ways have pillar nodes! instance = new GraphHopperOSM().setStoreOnFlush(false). setEncodingManager(new EncodingManager("foot")). @@ -583,8 +529,7 @@ public void testFootOnly() } @Test - public void testVia() - { + public void testVia() { instance = new GraphHopperOSM().setStoreOnFlush(true). init(new CmdArgs(). put("datareader.file", testOsm3). @@ -614,8 +559,7 @@ public void testVia() } @Test - public void testGetPathsDirectionEnforcement1() - { + public void testGetPathsDirectionEnforcement1() { // Test enforce start direction // Note: This Test does not pass for CH enabled instance = createSquareGraphInstance(false); @@ -629,15 +573,11 @@ public void testGetPathsDirectionEnforcement1() GHResponse response = new GHResponse(); List paths = instance.calcPaths(req, response); assertFalse(response.hasErrors()); - assertArrayEquals(new int[] - { - 9, 5, 8, 3, 10 - }, paths.get(0).calcNodes().toArray()); + assertArrayEquals(new int[]{9, 5, 8, 3, 10}, paths.get(0).calcNodes().toArray()); } @Test - public void testGetPathsDirectionEnforcement2() - { + public void testGetPathsDirectionEnforcement2() { // Test enforce south start direction and east end direction instance = createSquareGraphInstance(false); @@ -650,25 +590,18 @@ public void testGetPathsDirectionEnforcement2() GHResponse response = new GHResponse(); List paths = instance.calcPaths(req, response); assertFalse(response.hasErrors()); - assertArrayEquals(new int[] - { - 9, 5, 8, 1, 2, 10 - }, paths.get(0).calcNodes().toArray()); + assertArrayEquals(new int[]{9, 5, 8, 1, 2, 10}, paths.get(0).calcNodes().toArray()); // Test uni-directional case req.setAlgorithm(DIJKSTRA); response = new GHResponse(); paths = instance.calcPaths(req, response); assertFalse(response.hasErrors()); - assertArrayEquals(new int[] - { - 9, 5, 8, 1, 2, 10 - }, paths.get(0).calcNodes().toArray()); + assertArrayEquals(new int[]{9, 5, 8, 1, 2, 10}, paths.get(0).calcNodes().toArray()); } @Test - public void testGetPathsDirectionEnforcement3() - { + public void testGetPathsDirectionEnforcement3() { instance = createSquareGraphInstance(false); // Start in middle of edge 4-5 @@ -682,15 +615,11 @@ public void testGetPathsDirectionEnforcement3() GHResponse response = new GHResponse(); List paths = instance.calcPaths(req, response); assertFalse(response.hasErrors()); - assertArrayEquals(new int[] - { - 10, 5, 6, 7, 11 - }, paths.get(0).calcNodes().toArray()); + assertArrayEquals(new int[]{10, 5, 6, 7, 11}, paths.get(0).calcNodes().toArray()); } @Test - public void testGetPathsDirectionEnforcement4() - { + public void testGetPathsDirectionEnforcement4() { // Test straight via routing instance = createSquareGraphInstance(false); @@ -706,19 +635,12 @@ public void testGetPathsDirectionEnforcement4() List paths = instance.calcPaths(req, response); assertFalse(response.hasErrors()); assertEquals(1, response.getAll().size()); - assertArrayEquals(new int[] - { - 10, 4, 3, 11 - }, paths.get(0).calcNodes().toArray()); - assertArrayEquals(new int[] - { - 11, 8, 1, 2, 9 - }, paths.get(1).calcNodes().toArray()); + assertArrayEquals(new int[]{10, 4, 3, 11}, paths.get(0).calcNodes().toArray()); + assertArrayEquals(new int[]{11, 8, 1, 2, 9}, paths.get(1).calcNodes().toArray()); } @Test - public void testGetPathsDirectionEnforcement5() - { + public void testGetPathsDirectionEnforcement5() { // Test independence of previous enforcement for subsequent pathes instance = createSquareGraphInstance(false); @@ -733,19 +655,12 @@ public void testGetPathsDirectionEnforcement5() GHResponse response = new GHResponse(); List paths = instance.calcPaths(req, response); assertFalse(response.hasErrors()); - assertArrayEquals(new int[] - { - 10, 4, 3, 8, 7, 9 - }, paths.get(0).calcNodes().toArray()); - assertArrayEquals(new int[] - { - 9, 6, 5, 10, 4, 3, 11 - }, paths.get(1).calcNodes().toArray()); + assertArrayEquals(new int[]{10, 4, 3, 8, 7, 9}, paths.get(0).calcNodes().toArray()); + assertArrayEquals(new int[]{9, 6, 5, 10, 4, 3, 11}, paths.get(1).calcNodes().toArray()); } @Test - public void testGetPathsDirectionEnforcement6() - { + public void testGetPathsDirectionEnforcement6() { // Test if query results at tower nodes are ignored instance = createSquareGraphInstance(false); @@ -758,18 +673,11 @@ public void testGetPathsDirectionEnforcement6() GHResponse response = new GHResponse(); List paths = instance.calcPaths(req, response); assertFalse(response.hasErrors()); - assertArrayEquals(new int[] - { - 0, 1, 2 - }, paths.get(0).calcNodes().toArray()); - assertArrayEquals(new int[] - { - 2, 3, 4 - }, paths.get(1).calcNodes().toArray()); + assertArrayEquals(new int[]{0, 1, 2}, paths.get(0).calcNodes().toArray()); + assertArrayEquals(new int[]{2, 3, 4}, paths.get(1).calcNodes().toArray()); } - private GraphHopper createSquareGraphInstance( boolean withCH ) - { + private GraphHopper createSquareGraphInstance(boolean withCH) { CarFlagEncoder carEncoder = new CarFlagEncoder(); EncodingManager encodingManager = new EncodingManager(carEncoder); Weighting weighting = new FastestWeighting(carEncoder); @@ -818,8 +726,7 @@ private GraphHopper createSquareGraphInstance( boolean withCH ) } @Test - public void testCustomFactoryForNoneCH() - { + public void testCustomFactoryForNoneCH() { CarFlagEncoder carEncoder = new CarFlagEncoder(); EncodingManager em = new EncodingManager(carEncoder); // Weighting weighting = new FastestWeighting(carEncoder); @@ -828,17 +735,14 @@ public void testCustomFactoryForNoneCH() setGraphHopperLocation(ghLoc). setDataReaderFile(testOsm); final RoutingAlgorithmFactory af = new RoutingAlgorithmFactorySimple(); - instance.addAlgorithmFactoryDecorator(new RoutingAlgorithmFactoryDecorator() - { + instance.addAlgorithmFactoryDecorator(new RoutingAlgorithmFactoryDecorator() { @Override - public RoutingAlgorithmFactory getDecoratedAlgorithmFactory( RoutingAlgorithmFactory algoFactory, HintsMap map ) - { + public RoutingAlgorithmFactory getDecoratedAlgorithmFactory(RoutingAlgorithmFactory algoFactory, HintsMap map) { return af; } @Override - public boolean isEnabled() - { + public boolean isEnabled() { return true; } }); @@ -848,16 +752,12 @@ public boolean isEnabled() // test that hints are passed to algorithm opts final AtomicInteger cnt = new AtomicInteger(0); - instance.addAlgorithmFactoryDecorator(new RoutingAlgorithmFactoryDecorator() - { + instance.addAlgorithmFactoryDecorator(new RoutingAlgorithmFactoryDecorator() { @Override - public RoutingAlgorithmFactory getDecoratedAlgorithmFactory( RoutingAlgorithmFactory algoFactory, HintsMap map ) - { - return new RoutingAlgorithmFactorySimple() - { + public RoutingAlgorithmFactory getDecoratedAlgorithmFactory(RoutingAlgorithmFactory algoFactory, HintsMap map) { + return new RoutingAlgorithmFactorySimple() { @Override - public RoutingAlgorithm createAlgo( Graph g, AlgorithmOptions opts ) - { + public RoutingAlgorithm createAlgo(Graph g, AlgorithmOptions opts) { cnt.addAndGet(1); assertFalse(opts.getHints().getBool("test", true)); return super.createAlgo(g, opts); @@ -866,8 +766,7 @@ public RoutingAlgorithm createAlgo( Graph g, AlgorithmOptions opts ) } @Override - public boolean isEnabled() - { + public boolean isEnabled() { return true; } }); @@ -878,12 +777,10 @@ public boolean isEnabled() } @Test - public void testMultipleCHPreparationsInParallel() - { + public void testMultipleCHPreparationsInParallel() { HashMap shortcutCountMap = new HashMap(); // try all parallelization modes - for (int threadCount = 1; threadCount < 6; threadCount++) - { + for (int threadCount = 1; threadCount < 6; threadCount++) { EncodingManager em = new EncodingManager(Arrays.asList(new CarFlagEncoder(), new MotorcycleFlagEncoder(), new MountainBikeFlagEncoder(), new RacingBikeFlagEncoder(), new FootFlagEncoder()), 8); @@ -897,8 +794,7 @@ public void testMultipleCHPreparationsInParallel() tmpGH.importOrLoad(); assertEquals(5, tmpGH.getCHFactoryDecorator().getPreparations().size()); - for (RoutingAlgorithmFactory raf : tmpGH.getCHFactoryDecorator().getPreparations()) - { + for (RoutingAlgorithmFactory raf : tmpGH.getCHFactoryDecorator().getPreparations()) { PrepareContractionHierarchies pch = (PrepareContractionHierarchies) raf; assertTrue("Preparation wasn't run! [" + threadCount + "]", pch.isPrepared()); @@ -921,25 +817,8 @@ public void testMultipleCHPreparationsInParallel() } } - class TestEncoder extends CarFlagEncoder - { - private final String name; - - public TestEncoder( String name ) - { - this.name = name; - } - - @Override - public String toString() - { - return name; - } - } - @Test - public void testGetWeightingForCH() - { + public void testGetWeightingForCH() { TestEncoder truck = new TestEncoder("truck"); TestEncoder simpleTruck = new TestEncoder("simple_truck"); @@ -964,18 +843,15 @@ public void testGetWeightingForCH() // make sure weighting cannot be mixed decorator.addWeighting(fwT); decorator.addWeighting(fwSimpleT); - try - { + try { decorator.addPreparation(new PrepareContractionHierarchies(ramDir, storage, storage.getGraph(CHGraph.class, fwSimpleT), simpleTruck, fwSimpleT, TraversalMode.NODE_BASED)); assertTrue(false); - } catch (Exception ex) - { + } catch (Exception ex) { } } @Test - public void testGetMultipleWeightingsForCH() - { + public void testGetMultipleWeightingsForCH() { EncodingManager em = new EncodingManager(Arrays.asList(new CarFlagEncoder()), 8); GraphHopper tmpGH = new GraphHopperOSM(). @@ -985,4 +861,17 @@ public void testGetMultipleWeightingsForCH() assertEquals(2, tmpGH.getCHFactoryDecorator().getWeightingsAsStrings().size()); } + + class TestEncoder extends CarFlagEncoder { + private final String name; + + public TestEncoder(String name) { + this.name = name; + } + + @Override + public String toString() { + return name; + } + } } diff --git a/reader-osm/src/test/java/com/graphhopper/reader/osm/OSMReaderTest.java b/reader-osm/src/test/java/com/graphhopper/reader/osm/OSMReaderTest.java index f8b1bb5ba14..1d4b42585af 100644 --- a/reader-osm/src/test/java/com/graphhopper/reader/osm/OSMReaderTest.java +++ b/reader-osm/src/test/java/com/graphhopper/reader/osm/OSMReaderTest.java @@ -17,53 +17,42 @@ */ package com.graphhopper.reader.osm; -import com.graphhopper.reader.ReaderWay; -import com.graphhopper.reader.ReaderNode; -import com.graphhopper.reader.ReaderRelation; -import static org.junit.Assert.*; - -import gnu.trove.list.TLongList; - -import java.io.File; -import java.io.IOException; -import java.io.InputStream; -import java.net.URISyntaxException; -import java.util.Collection; -import java.util.Collections; -import java.util.HashMap; -import java.util.Map; - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - -import com.graphhopper.GraphHopper; import com.graphhopper.GHRequest; import com.graphhopper.GHResponse; +import com.graphhopper.GraphHopper; import com.graphhopper.GraphHopperIT; import com.graphhopper.reader.DataReader; +import com.graphhopper.reader.ReaderNode; +import com.graphhopper.reader.ReaderRelation; +import com.graphhopper.reader.ReaderWay; import com.graphhopper.reader.dem.ElevationProvider; import com.graphhopper.reader.dem.SRTMProvider; import com.graphhopper.routing.util.*; import com.graphhopper.storage.*; import com.graphhopper.storage.index.LocationIndex; import com.graphhopper.storage.index.QueryResult; -import com.graphhopper.util.EdgeExplorer; -import com.graphhopper.util.EdgeIterator; -import com.graphhopper.util.EdgeIteratorState; -import com.graphhopper.util.GHUtility; -import com.graphhopper.util.Helper; +import com.graphhopper.util.*; import com.graphhopper.util.shapes.GHPoint; +import gnu.trove.list.TLongList; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.net.URISyntaxException; import java.util.*; +import static org.junit.Assert.*; + /** * Tests the OSMReader with the normal helper initialized. *

    + * * @author Peter Karich */ -public class OSMReaderTest -{ +public class OSMReaderTest { private final String file1 = "test-osm.xml"; private final String file2 = "test-osm2.xml"; private final String file3 = "test-osm3.xml"; @@ -83,90 +72,26 @@ public class OSMReaderTest private EdgeExplorer carAllExplorer; @Before - public void setUp() - { + public void setUp() { new File(dir).mkdirs(); } @After - public void tearDown() - { + public void tearDown() { Helper.removeDir(new File(dir)); } - GraphHopperStorage newGraph( String directory, EncodingManager encodingManager, boolean is3D, boolean turnRestrictionsImport ) - { + GraphHopperStorage newGraph(String directory, EncodingManager encodingManager, boolean is3D, boolean turnRestrictionsImport) { return new GraphHopperStorage(new RAMDirectory(directory, false), encodingManager, is3D, turnRestrictionsImport ? new TurnCostExtension() : new GraphExtension.NoOpExtension()); } - class GraphHopperTest extends GraphHopperOSM - { - public GraphHopperTest( String osmFile ) - { - this(osmFile, false); - } - - public GraphHopperTest( String osmFile, boolean turnCosts ) - { - setStoreOnFlush(false); - setOSMFile(osmFile); - setGraphHopperLocation(dir); - setEncodingManager(new EncodingManager("car,foot")); - setCHEnabled(false); - - if (turnCosts) - { - carEncoder = new CarFlagEncoder(5, 5, 1); - bikeEncoder = new BikeFlagEncoder(4, 2, 1); - } else - { - carEncoder = new CarFlagEncoder(); - bikeEncoder = new BikeFlagEncoder(); - } - - footEncoder = new FootFlagEncoder(); - - setEncodingManager(new EncodingManager(footEncoder, carEncoder, bikeEncoder)); - } - - @Override - protected DataReader createReader( GraphHopperStorage tmpGraph ) - { - return initDataReader(new OSMReader(tmpGraph)); - } - - @Override - protected DataReader importData() throws IOException - { - getEncodingManager().setPreferredLanguage(getPreferredLanguage()); - GraphHopperStorage tmpGraph = newGraph(dir, getEncodingManager(), hasElevation(), - getEncodingManager().needsTurnCostsSupport()); - setGraphHopperStorage(tmpGraph); - - DataReader osmReader = createReader(tmpGraph); - try - { - osmReader.setFile(new File(getClass().getResource(getOSMFile()).toURI())); - } catch (URISyntaxException e) - { - throw new RuntimeException(e); - } - osmReader.readGraph(); - carOutExplorer = getGraphHopperStorage().createEdgeExplorer(new DefaultEdgeFilter(carEncoder, false, true)); - carAllExplorer = getGraphHopperStorage().createEdgeExplorer(new DefaultEdgeFilter(carEncoder, true, true)); - return osmReader; - } - } - - InputStream getResource( String file ) - { + InputStream getResource(String file) { return getClass().getResourceAsStream(file); } @Test - public void testMain() - { + public void testMain() { GraphHopper hopper = new GraphHopperTest(file1).importOrLoad(); GraphHopperStorage graph = hopper.getGraphHopperStorage(); @@ -222,14 +147,12 @@ public void testMain() assertEquals(9, na.getLongitude(findID(hopper.getLocationIndex(), 51.25, 9.43)), 1e-3); } - protected int findID( LocationIndex index, double lat, double lon ) - { + protected int findID(LocationIndex index, double lat, double lon) { return index.findClosest(lat, lon, EdgeFilter.ALL_EDGES).getClosestNode(); } @Test - public void testSort() - { + public void testSort() { GraphHopper hopper = new GraphHopperTest(file1).setSortGraph(true).importOrLoad(); NodeAccess na = hopper.getGraphHopperStorage().getNodeAccess(); assertEquals(10, na.getLongitude(findID(hopper.getLocationIndex(), 49, 10)), 1e-3); @@ -237,18 +160,13 @@ public void testSort() } @Test - public void testWithBounds() - { - GraphHopper hopper = new GraphHopperTest(file1) - { + public void testWithBounds() { + GraphHopper hopper = new GraphHopperTest(file1) { @Override - protected DataReader createReader( GraphHopperStorage tmpGraph ) - { - return new OSMReader(tmpGraph) - { + protected DataReader createReader(GraphHopperStorage tmpGraph) { + return new OSMReader(tmpGraph) { @Override - public boolean isInBounds( ReaderNode node ) - { + public boolean isInBounds(ReaderNode node) { return node.getLat() > 49 && node.getLon() > 8; } }.setEncodingManager(getEncodingManager()); @@ -289,8 +207,7 @@ public boolean isInBounds( ReaderNode node ) } @Test - public void testOneWay() - { + public void testOneWay() { GraphHopper hopper = new GraphHopperTest(file2).importOrLoad(); GraphHopperStorage graph = hopper.getGraphHopperStorage(); @@ -339,13 +256,10 @@ public void testOneWay() } @Test - public void testFerry() - { - GraphHopper hopper = new GraphHopperTest(file2) - { + public void testFerry() { + GraphHopper hopper = new GraphHopperTest(file2) { @Override - public void cleanUp() - { + public void cleanUp() { } }.importOrLoad(); Graph graph = hopper.getGraphHopperStorage(); @@ -360,21 +274,18 @@ public void cleanUp() iter.next(); assertEquals(5, carEncoder.getSpeed(iter.getFlags()), 1e-1); - // duration 01:10 is given => more precise speed calculation! - // ~111km (from 54.0,10.1 to 55.0,10.2) in duration=70 minutes => 95km/h => / 1.4 => 71km/h + // duration 01:10 is given => more precise speed calculation! + // ~111km (from 54.0,10.1 to 55.0,10.2) in duration=70 minutes => 95km/h => / 1.4 => 71km/h iter = carOutExplorer.setBaseNode(n40); iter.next(); assertEquals(70, carEncoder.getSpeed(iter.getFlags()), 1e-1); } @Test - public void testMaxSpeed() - { - GraphHopper hopper = new GraphHopperTest(file2) - { + public void testMaxSpeed() { + GraphHopper hopper = new GraphHopperTest(file2) { @Override - public void cleanUp() - { + public void cleanUp() { } }.importOrLoad(); Graph graph = hopper.getGraphHopperStorage(); @@ -386,8 +297,7 @@ public void cleanUp() } @Test - public void testWayReferencesNotExistingAdjNode() - { + public void testWayReferencesNotExistingAdjNode() { GraphHopper hopper = new GraphHopperTest(file4).importOrLoad(); Graph graph = hopper.getGraphHopperStorage(); @@ -399,8 +309,7 @@ public void testWayReferencesNotExistingAdjNode() } @Test - public void testFoot() - { + public void testFoot() { GraphHopper hopper = new GraphHopperTest(file3).importOrLoad(); Graph graph = hopper.getGraphHopperStorage(); @@ -422,8 +331,7 @@ public void testFoot() } @Test - public void testNegativeIds() - { + public void testNegativeIds() { GraphHopper hopper = new GraphHopperTest(fileNegIds).importOrLoad(); Graph graph = hopper.getGraphHopperStorage(); assertEquals(4, graph.getNodes()); @@ -447,8 +355,7 @@ public void testNegativeIds() } @Test - public void testBarriers() - { + public void testBarriers() { GraphHopper hopper = new GraphHopperTest(fileBarriers). setMinNetworkSize(0, 0). importOrLoad(); @@ -485,8 +392,7 @@ public void testBarriers() } @Test - public void testBarriersOnTowerNodes() - { + public void testBarriersOnTowerNodes() { GraphHopper hopper = new GraphHopperTest(fileBarriers). setMinNetworkSize(0, 0). importOrLoad(); @@ -509,8 +415,7 @@ public void testBarriersOnTowerNodes() } @Test - public void testRelation() - { + public void testRelation() { EncodingManager manager = new EncodingManager("bike"); GraphHopperStorage ghStorage = new GraphHopperStorage(new RAMDirectory(), manager, false, new GraphExtension.NoOpExtension()); OSMReader reader = new OSMReader(ghStorage). @@ -540,8 +445,7 @@ public void testRelation() } @Test - public void testTurnRestrictions() - { + public void testTurnRestrictions() { GraphHopper hopper = new GraphHopperTest(fileTurnRestrictions, true). importOrLoad(); @@ -585,7 +489,7 @@ public void testTurnRestrictions() int edge5_6 = GHUtility.getEdge(graph, n5, n6).getEdge(); int edge5_1 = GHUtility.getEdge(graph, n5, n1).getEdge(); - // (4-5)->(5-1) right_turn_only = (4-5)->(5-6) restricted + // (4-5)->(5-1) right_turn_only = (4-5)->(5-6) restricted long costsFlags = tcStorage.getTurnCostFlags(edge4_5, n5, edge5_6); assertFalse(carEncoder.isTurnRestricted(costsFlags)); assertTrue(carEncoder.getTurnCost(tcStorage.getTurnCostFlags(edge4_5, n5, edge5_1)) > 0); @@ -608,23 +512,19 @@ public void testTurnRestrictions() } @Test - public void testEstimatedCenter() - { - final CarFlagEncoder encoder = new CarFlagEncoder() - { + public void testEstimatedCenter() { + final CarFlagEncoder encoder = new CarFlagEncoder() { private EncodedValue objectEncoder; @Override - public int defineNodeBits( int index, int shift ) - { + public int defineNodeBits(int index, int shift) { shift = super.defineNodeBits(index, shift); objectEncoder = new EncodedValue("oEnc", shift, 2, 1, 0, 3, true); return shift + 2; } @Override - public long handleNodeTags( ReaderNode node ) - { + public long handleNodeTags(ReaderNode node) { if (node.hasTag("test", "now")) return -objectEncoder.setValue(0, 1); return 0; @@ -640,24 +540,20 @@ public long handleNodeTags( ReaderNode node ) lonMap.put(1, 1.0d); lonMap.put(2, 1.0d); - OSMReader osmreader = new OSMReader(ghStorage) - { + OSMReader osmreader = new OSMReader(ghStorage) { // mock data access @Override - double getTmpLatitude( int id ) - { + double getTmpLatitude(int id) { return latMap.get(id); } @Override - double getTmpLongitude( int id ) - { + double getTmpLongitude(int id) { return lonMap.get(id); } @Override - Collection addOSMWay( TLongList osmNodeIds, long wayFlags, long osmId ) - { + Collection addOSMWay(TLongList osmNodeIds, long wayFlags, long osmId) { return Collections.emptyList(); } }; @@ -683,18 +579,13 @@ Collection addOSMWay( TLongList osmNodeIds, long wayFlags, lo } @Test - public void testReadEleFromCustomOSM() - { - GraphHopper hopper = new GraphHopperTest("custom-osm-ele.xml") - { + public void testReadEleFromCustomOSM() { + GraphHopper hopper = new GraphHopperTest("custom-osm-ele.xml") { @Override - protected DataReader createReader( GraphHopperStorage tmpGraph ) - { - return initDataReader(new OSMReader(tmpGraph) - { + protected DataReader createReader(GraphHopperStorage tmpGraph) { + return initDataReader(new OSMReader(tmpGraph) { @Override - protected double getElevation( ReaderNode node ) - { + protected double getElevation(ReaderNode node) { return node.getEle(); } }); @@ -710,8 +601,7 @@ protected double getElevation( ReaderNode node ) } @Test - public void testReadEleFromDataProvider() - { + public void testReadEleFromDataProvider() { GraphHopper hopper = new GraphHopperTest("test-osm5.xml"); // get N10E046.hgt.zip ElevationProvider provider = new SRTMProvider(); @@ -737,8 +627,7 @@ public void testReadEleFromDataProvider() * Tests the combination of different turn cost flags by different encoders. */ @Test - public void testTurnFlagCombination() - { + public void testTurnFlagCombination() { final OSMTurnRelation.TurnCostTableEntry turnCostEntry_car = new OSMTurnRelation.TurnCostTableEntry(); final OSMTurnRelation.TurnCostTableEntry turnCostEntry_foot = new OSMTurnRelation.TurnCostTableEntry(); final OSMTurnRelation.TurnCostTableEntry turnCostEntry_bike = new OSMTurnRelation.TurnCostTableEntry(); @@ -749,32 +638,26 @@ public void testTurnFlagCombination() EncodingManager manager = new EncodingManager(Arrays.asList(bike, foot, car), 4); GraphHopperStorage ghStorage = new GraphBuilder(manager).create(); - OSMReader reader = new OSMReader(ghStorage) - { + OSMReader reader = new OSMReader(ghStorage) { @Override - public Collection analyzeTurnRelation( FlagEncoder encoder, - OSMTurnRelation turnRelation ) - { + public Collection analyzeTurnRelation(FlagEncoder encoder, + OSMTurnRelation turnRelation) { // simulate by returning one turn cost entry directly - if (encoder.toString().equalsIgnoreCase("car")) - { + if (encoder.toString().equalsIgnoreCase("car")) { return Collections.singleton(turnCostEntry_car); - } else if (encoder.toString().equalsIgnoreCase("foot")) - { + } else if (encoder.toString().equalsIgnoreCase("foot")) { return Collections.singleton(turnCostEntry_foot); - } else if (encoder.toString().equalsIgnoreCase("bike")) - { + } else if (encoder.toString().equalsIgnoreCase("bike")) { return Collections.singleton(turnCostEntry_bike); - } else - { + } else { throw new IllegalArgumentException("illegal encoder " + encoder.toString()); } } }.setEncodingManager(manager); - // turn cost entries for car and foot are for the same relations (same viaNode, edgeFrom and edgeTo), - // turn cost entry for bike is for another relation (different viaNode) + // turn cost entries for car and foot are for the same relations (same viaNode, edgeFrom and edgeTo), + // turn cost entry for bike is for another relation (different viaNode) turnCostEntry_car.edgeFrom = 1; turnCostEntry_foot.edgeFrom = 1; turnCostEntry_bike.edgeFrom = 2; @@ -784,7 +667,7 @@ public Collection analyzeTurnRelation( FlagE turnCostEntry_foot.flags = foot.getTurnFlags(true, 0); turnCostEntry_bike.flags = bike.getTurnFlags(false, 10); - // we expect two different entries: the first one is a combination of turn flags of car and foot, + // we expect two different entries: the first one is a combination of turn flags of car and foot, // since they provide the same relation, the other one is for bike only long assertFlag1 = turnCostEntry_car.flags | turnCostEntry_foot.flags; long assertFlag2 = turnCostEntry_bike.flags; @@ -795,11 +678,9 @@ public Collection analyzeTurnRelation( FlagE // we expect two different turnCost entries assertEquals(2, entries.size()); - for (OSMTurnRelation.TurnCostTableEntry entry : entries) - { - if (entry.edgeFrom == 1) - { - // the first entry provides turn flags for car and foot only + for (OSMTurnRelation.TurnCostTableEntry entry : entries) { + if (entry.edgeFrom == 1) { + // the first entry provides turn flags for car and foot only assertEquals(assertFlag1, entry.flags); assertTrue(car.isTurnRestricted(entry.flags)); assertFalse(foot.isTurnRestricted(entry.flags)); @@ -808,8 +689,7 @@ public Collection analyzeTurnRelation( FlagE assertTrue(Double.isInfinite(car.getTurnCost(entry.flags))); assertEquals(0, foot.getTurnCost(entry.flags), 1e-1); assertEquals(0, bike.getTurnCost(entry.flags), 1e-1); - } else if (entry.edgeFrom == 2) - { + } else if (entry.edgeFrom == 2) { // the 2nd entry provides turn flags for bike only assertEquals(assertFlag2, entry.flags); assertFalse(car.isTurnRestricted(entry.flags)); @@ -824,8 +704,7 @@ public Collection analyzeTurnRelation( FlagE } @Test - public void testPreferredLanguage() - { + public void testPreferredLanguage() { GraphHopper hopper = new GraphHopperTest(file1).setPreferredLanguage("de").importOrLoad(); GraphHopperStorage graph = hopper.getGraphHopperStorage(); int n20 = AbstractGraphStorageTester.getIdOf(graph, 52); @@ -843,8 +722,7 @@ public void testPreferredLanguage() } @Test - public void testDataDateWithinPBF() - { + public void testDataDateWithinPBF() { GraphHopper hopper = new GraphHopperTest(file6).importOrLoad(); GraphHopperStorage graph = hopper.getGraphHopperStorage(); @@ -852,8 +730,7 @@ public void testDataDateWithinPBF() } @Test - public void testCrossBoundary_issue667() - { + public void testCrossBoundary_issue667() { GraphHopper hopper = new GraphHopperTest("test-osm-waterway.xml").importOrLoad(); QueryResult qr = hopper.getLocationIndex().findClosest(0.1, 179.5, EdgeFilter.ALL_EDGES); assertTrue(qr.isValid()); @@ -868,8 +745,7 @@ public void testCrossBoundary_issue667() assertEquals(56, qr.getClosestEdge().getDistance() / 1000, 1); } - public void testRoutingRequestFails_issue665() - { + public void testRoutingRequestFails_issue665() { GraphHopper hopper = new GraphHopperOSM() .setDataReaderFile("src/test/resources/com/graphhopper/reader/" + file7) .setEncodingManager(new EncodingManager("car,motorcycle")) @@ -883,4 +759,54 @@ public void testRoutingRequestFails_issue665() GHResponse ghRsp = hopper.route(req); assertFalse(ghRsp.getErrors().toString(), ghRsp.hasErrors()); } + + class GraphHopperTest extends GraphHopperOSM { + public GraphHopperTest(String osmFile) { + this(osmFile, false); + } + + public GraphHopperTest(String osmFile, boolean turnCosts) { + setStoreOnFlush(false); + setOSMFile(osmFile); + setGraphHopperLocation(dir); + setEncodingManager(new EncodingManager("car,foot")); + setCHEnabled(false); + + if (turnCosts) { + carEncoder = new CarFlagEncoder(5, 5, 1); + bikeEncoder = new BikeFlagEncoder(4, 2, 1); + } else { + carEncoder = new CarFlagEncoder(); + bikeEncoder = new BikeFlagEncoder(); + } + + footEncoder = new FootFlagEncoder(); + + setEncodingManager(new EncodingManager(footEncoder, carEncoder, bikeEncoder)); + } + + @Override + protected DataReader createReader(GraphHopperStorage tmpGraph) { + return initDataReader(new OSMReader(tmpGraph)); + } + + @Override + protected DataReader importData() throws IOException { + getEncodingManager().setPreferredLanguage(getPreferredLanguage()); + GraphHopperStorage tmpGraph = newGraph(dir, getEncodingManager(), hasElevation(), + getEncodingManager().needsTurnCostsSupport()); + setGraphHopperStorage(tmpGraph); + + DataReader osmReader = createReader(tmpGraph); + try { + osmReader.setFile(new File(getClass().getResource(getOSMFile()).toURI())); + } catch (URISyntaxException e) { + throw new RuntimeException(e); + } + osmReader.readGraph(); + carOutExplorer = getGraphHopperStorage().createEdgeExplorer(new DefaultEdgeFilter(carEncoder, false, true)); + carAllExplorer = getGraphHopperStorage().createEdgeExplorer(new DefaultEdgeFilter(carEncoder, true, true)); + return osmReader; + } + } } diff --git a/reader-osm/src/test/java/com/graphhopper/reader/osm/OSMTagParserTest.java b/reader-osm/src/test/java/com/graphhopper/reader/osm/OSMTagParserTest.java index 1f485498216..5f2b5e43c5c 100644 --- a/reader-osm/src/test/java/com/graphhopper/reader/osm/OSMTagParserTest.java +++ b/reader-osm/src/test/java/com/graphhopper/reader/osm/OSMTagParserTest.java @@ -17,21 +17,18 @@ */ package com.graphhopper.reader.osm; -import com.graphhopper.reader.osm.OSMTagParser; -import static org.junit.Assert.*; -import static org.junit.Assert.fail; import org.junit.Test; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + /** - * * @author Peter Karich * @author ratrun */ -public class OSMTagParserTest -{ +public class OSMTagParserTest { @Test - public void testParseDuration() - { + public void testParseDuration() { assertEquals(10 * 60, OSMTagParser.parseDuration("00:10")); assertEquals(35 * 60, OSMTagParser.parseDuration("35")); assertEquals(70 * 60, OSMTagParser.parseDuration("01:10")); @@ -50,30 +47,23 @@ public void testParseDuration() } @Test - public void testWrongDurationFormats() - { - try - { + public void testWrongDurationFormats() { + try { OSMTagParser.parseDuration("PT5h12m36s"); fail("parseDuration didn't throw when I expected it to"); - } catch (IllegalArgumentException expectedException) - { + } catch (IllegalArgumentException expectedException) { assertEquals(expectedException.getMessage(), "Cannot parse duration tag value: PT5h12m36s"); } - try - { + try { OSMTagParser.parseDuration("oh"); fail("parseDuration didn't throw when I expected it to"); - } catch (IllegalArgumentException expectedException) - { + } catch (IllegalArgumentException expectedException) { assertEquals(expectedException.getMessage(), "Cannot parse duration tag value: oh"); } - try - { + try { OSMTagParser.parseDuration("01:10:2"); fail("parseDuration didn't throw when I expected it to"); - } catch (IllegalArgumentException expectedException) - { + } catch (IllegalArgumentException expectedException) { assertEquals(expectedException.getMessage(), "Cannot parse duration tag value: 01:10:2"); } diff --git a/reader-osm/src/test/java/com/graphhopper/reader/osm/OSMTurnRelationTest.java b/reader-osm/src/test/java/com/graphhopper/reader/osm/OSMTurnRelationTest.java index e012201342e..d06724cfb85 100644 --- a/reader-osm/src/test/java/com/graphhopper/reader/osm/OSMTurnRelationTest.java +++ b/reader-osm/src/test/java/com/graphhopper/reader/osm/OSMTurnRelationTest.java @@ -24,24 +24,21 @@ import com.graphhopper.storage.GraphBuilder; import com.graphhopper.storage.GraphHopperStorage; import com.graphhopper.util.EdgeExplorer; +import org.junit.Test; import java.util.Collection; import java.util.HashMap; import java.util.Iterator; import java.util.Map; -import org.junit.Test; - -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; /** * @author Peter Karich */ -public class OSMTurnRelationTest -{ +public class OSMTurnRelationTest { @Test - public void testGetRestrictionAsEntries() - { + public void testGetRestrictionAsEntries() { CarFlagEncoder encoder = new CarFlagEncoder(5, 5, 1); final Map osmNodeToInternal = new HashMap(); final Map internalToOSMEdge = new HashMap(); @@ -53,18 +50,15 @@ public void testGetRestrictionAsEntries() GraphHopperStorage ghStorage = new GraphBuilder(new EncodingManager(encoder)).create(); EdgeBasedRoutingAlgorithmTest.initGraph(ghStorage); - OSMReader osmReader = new OSMReader(ghStorage) - { + OSMReader osmReader = new OSMReader(ghStorage) { @Override - public int getInternalNodeIdOfOsmNode( long nodeOsmId ) - { + public int getInternalNodeIdOfOsmNode(long nodeOsmId) { return osmNodeToInternal.get(nodeOsmId); } @Override - public long getOsmIdOfInternalEdge( int edgeId ) - { + public long getOsmIdOfInternalEdge(int edgeId) { Long l = internalToOSMEdge.get(edgeId); if (l == null) return -1; @@ -91,7 +85,6 @@ public long getOsmIdOfInternalEdge( int edgeId ) assertEquals(2, entry.edgeTo); assertEquals(3, entry.nodeVia); - // TYPE == NOT instance = new OSMTurnRelation(4, 3, 3, Type.NOT); result = instance.getRestrictionAsEntries(encoder, edgeExplorer, edgeExplorer, osmReader); diff --git a/reader-osm/src/test/java/com/graphhopper/routing/RoutingAlgorithmWithOSMIT.java b/reader-osm/src/test/java/com/graphhopper/routing/RoutingAlgorithmWithOSMIT.java index c780dbe0762..9f2720fcd6d 100644 --- a/reader-osm/src/test/java/com/graphhopper/routing/RoutingAlgorithmWithOSMIT.java +++ b/reader-osm/src/test/java/com/graphhopper/routing/RoutingAlgorithmWithOSMIT.java @@ -17,51 +17,50 @@ */ package com.graphhopper.routing; -import com.graphhopper.routing.util.TestAlgoCollector; import com.graphhopper.GraphHopper; -import static com.graphhopper.GraphHopperIT.DIR; import com.graphhopper.reader.dem.SRTMProvider; import com.graphhopper.reader.osm.GraphHopperOSM; import com.graphhopper.routing.util.*; -import com.graphhopper.routing.util.EncodingManager; import com.graphhopper.routing.util.TestAlgoCollector.AlgoHelperEntry; import com.graphhopper.routing.util.TestAlgoCollector.OneRun; import com.graphhopper.routing.weighting.ShortestWeighting; import com.graphhopper.routing.weighting.Weighting; -import com.graphhopper.storage.*; +import com.graphhopper.storage.CHGraph; +import com.graphhopper.storage.Graph; import com.graphhopper.storage.index.LocationIndex; import com.graphhopper.storage.index.QueryResult; import com.graphhopper.util.GHUtility; import com.graphhopper.util.Helper; -import static com.graphhopper.util.Parameters.Algorithms.*; +import org.junit.Before; +import org.junit.Test; import java.io.File; import java.io.IOException; -import java.util.*; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; import java.util.concurrent.atomic.AtomicInteger; -import static org.junit.Assert.*; - -import org.junit.Before; -import org.junit.Test; +import static com.graphhopper.GraphHopperIT.DIR; +import static com.graphhopper.util.Parameters.Algorithms.ASTAR; +import static com.graphhopper.util.Parameters.Algorithms.DIJKSTRA_BI; +import static org.junit.Assert.assertEquals; /** * Try algorithms, indices and graph storages with real data *

    + * * @author Peter Karich */ -public class RoutingAlgorithmWithOSMIT -{ +public class RoutingAlgorithmWithOSMIT { TestAlgoCollector testCollector; @Before - public void setUp() - { + public void setUp() { testCollector = new TestAlgoCollector("core integration tests"); } - List createMonacoCar() - { + List createMonacoCar() { List list = new ArrayList(); list.add(new OneRun(43.730729, 7.42135, 43.727697, 7.419199, 2580, 110)); list.add(new OneRun(43.727687, 7.418737, 43.74958, 7.436566, 3588, 170)); @@ -81,8 +80,7 @@ List createMonacoCar() } @Test - public void testMonaco() - { + public void testMonaco() { Graph g = runAlgo(testCollector, DIR + "/monaco.osm.gz", "target/monaco-gh", createMonacoCar(), "car", true, "car", "shortest", false); @@ -98,8 +96,7 @@ public void testMonaco() } @Test - public void testMonacoMotorcycle() - { + public void testMonacoMotorcycle() { List list = new ArrayList(); list.add(new OneRun(43.730729, 7.42135, 43.727697, 7.419199, 2703, 119)); list.add(new OneRun(43.727687, 7.418737, 43.74958, 7.436566, 3749, 170)); @@ -114,8 +111,7 @@ public void testMonacoMotorcycle() } @Test - public void testMonacoMotorcycleCurvature() - { + public void testMonacoMotorcycleCurvature() { List list = new ArrayList(); list.add(new OneRun(43.730729, 7.42135, 43.727697, 7.419199, 2703, 119)); list.add(new OneRun(43.727687, 7.418737, 43.74958, 7.436566, 3749, 170)); @@ -130,8 +126,7 @@ public void testMonacoMotorcycleCurvature() } @Test - public void testBike2_issue432() - { + public void testBike2_issue432() { List list = new ArrayList(); list.add(new OneRun(52.349969, 8.013813, 52.349713, 8.013293, 56, 7)); // reverse route avoids the location @@ -143,8 +138,7 @@ public void testBike2_issue432() } @Test - public void testMonacoAllAlgorithmsWithBaseGraph() - { + public void testMonacoAllAlgorithmsWithBaseGraph() { String vehicle = "car"; String graphFile = "target/monaco-gh"; String osmFile = DIR + "/monaco.osm.gz"; @@ -172,21 +166,17 @@ public void testMonacoAllAlgorithmsWithBaseGraph() // set all normal algorithms to baseGraph of already prepared to see if all algorithms still work Graph baseGraphOfCHPrepared = chPrepare.getBaseGraph(); - for (AlgoHelperEntry ahe : prepares) - { - if (!(ahe.getQueryGraph() instanceof CHGraph)) - { + for (AlgoHelperEntry ahe : prepares) { + if (!(ahe.getQueryGraph() instanceof CHGraph)) { ahe.setQueryGraph(baseGraphOfCHPrepared); } } List forEveryAlgo = createMonacoCar(); EdgeFilter edgeFilter = new DefaultEdgeFilter(encoder); - for (AlgoHelperEntry entry : prepares) - { + for (AlgoHelperEntry entry : prepares) { LocationIndex idx = entry.getIdx(); - for (OneRun oneRun : forEveryAlgo) - { + for (OneRun oneRun : forEveryAlgo) { List list = oneRun.getList(idx, edgeFilter); testCollector.assertDistance(entry, list, oneRun); } @@ -194,8 +184,7 @@ public void testMonacoAllAlgorithmsWithBaseGraph() } @Test - public void testOneWayCircleBug() - { + public void testOneWayCircleBug() { // export from http://www.openstreetmap.org/export#map=19/51.37605/-0.53155 List list = new ArrayList(); // going the bit longer way out of the circle @@ -209,8 +198,7 @@ public void testOneWayCircleBug() } @Test - public void testMoscow() - { + public void testMoscow() { // extracted via ./graphhopper.sh extract "37.582641,55.805261,37.626929,55.824455" List list = new ArrayList(); // choose perpendicular @@ -228,8 +216,7 @@ public void testMoscow() } @Test - public void testMoscowTurnCosts() - { + public void testMoscowTurnCosts() { List list = new ArrayList(); list.add(new OneRun(55.813357, 37.5958585, 55.811042, 37.594689, 1043.99, 12)); list.add(new OneRun(55.813159, 37.593884, 55.811278, 37.594217, 1048, 13)); @@ -242,8 +229,7 @@ public void testMoscowTurnCosts() } @Test - public void testSidewalkNo() - { + public void testSidewalkNo() { List list = new ArrayList(); // roundabout contains sidewalk=no which should be avoided list.add(new OneRun(57.154888, -2.101822, 57.153445, -2.099869, 329, 31)); @@ -258,8 +244,7 @@ public void testSidewalkNo() } @Test - public void testMonacoFastest() - { + public void testMonacoFastest() { List list = createMonacoCar(); list.get(0).setLocs(1, 117); list.get(0).setDistance(1, 2584); @@ -273,8 +258,7 @@ public void testMonacoFastest() } @Test - public void testMonacoMixed() - { + public void testMonacoMixed() { // Additional locations are inserted because of new crossings from foot to highway paths! // Distance is the same. List list = createMonacoCar(); @@ -289,8 +273,7 @@ public void testMonacoMixed() assertEquals(testCollector.toString(), 0, testCollector.errors.size()); } - List createMonacoFoot() - { + List createMonacoFoot() { List list = new ArrayList(); list.add(new OneRun(43.730729, 7.421288, 43.727697, 7.419199, 1566, 92)); list.add(new OneRun(43.727687, 7.418737, 43.74958, 7.436566, 3438, 136)); @@ -300,8 +283,7 @@ List createMonacoFoot() } @Test - public void testMonacoFoot() - { + public void testMonacoFoot() { Graph g = runAlgo(testCollector, DIR + "/monaco.osm.gz", "target/monaco-gh", createMonacoFoot(), "foot", true, "foot", "shortest", false); assertEquals(testCollector.toString(), 0, testCollector.errors.size()); @@ -316,8 +298,7 @@ public void testMonacoFoot() } @Test - public void testMonacoFoot3D() - { + public void testMonacoFoot3D() { // most routes have same number of points as testMonaceFoot results but longer distance due to elevation difference List list = createMonacoFoot(); list.get(0).setDistance(1, 1627); @@ -334,8 +315,7 @@ public void testMonacoFoot3D() } @Test - public void testNorthBayreuthHikeFastestAnd3D() - { + public void testNorthBayreuthHikeFastestAnd3D() { List list = new ArrayList(); // prefer hiking route 'Teufelsloch Unterwaiz' and 'Rotmain-Wanderweg' list.add(new OneRun(49.974972, 11.515657, 49.991022, 11.512299, 2365, 66)); @@ -347,8 +327,7 @@ public void testNorthBayreuthHikeFastestAnd3D() } @Test - public void testMonacoBike3D_twoSpeedsPerEdge() - { + public void testMonacoBike3D_twoSpeedsPerEdge() { List list = new ArrayList(); // 1. alternative: go over steps 'Rampe Major' => 1.7km vs. around 2.7km list.add(new OneRun(43.730864, 7.420771, 43.727687, 7.418737, 2710, 118)); @@ -372,8 +351,7 @@ public void testMonacoBike3D_twoSpeedsPerEdge() } @Test - public void testMonacoBike() - { + public void testMonacoBike() { List list = new ArrayList(); list.add(new OneRun(43.730864, 7.420771, 43.727687, 7.418737, 1642, 87)); list.add(new OneRun(43.727687, 7.418737, 43.74958, 7.436566, 3580, 168)); @@ -385,8 +363,7 @@ public void testMonacoBike() } @Test - public void testMonacoMountainBike() - { + public void testMonacoMountainBike() { List list = new ArrayList(); list.add(new OneRun(43.730864, 7.420771, 43.727687, 7.418737, 2322, 110)); list.add(new OneRun(43.727687, 7.418737, 43.74958, 7.436566, 3613, 178)); @@ -403,8 +380,7 @@ public void testMonacoMountainBike() } @Test - public void testMonacoRacingBike() - { + public void testMonacoRacingBike() { List list = new ArrayList(); list.add(new OneRun(43.730864, 7.420771, 43.727687, 7.418737, 2594, 111)); list.add(new OneRun(43.727687, 7.418737, 43.74958, 7.436566, 3588, 170)); @@ -420,8 +396,7 @@ public void testMonacoRacingBike() } @Test - public void testKremsBikeRelation() - { + public void testKremsBikeRelation() { List list = new ArrayList(); list.add(new OneRun(48.409523, 15.602394, 48.375466, 15.72916, 12491, 159)); // 3109m is better as cyclepath is used @@ -438,8 +413,7 @@ public void testKremsBikeRelation() } @Test - public void testKremsMountainBikeRelation() - { + public void testKremsMountainBikeRelation() { List list = new ArrayList(); list.add(new OneRun(48.409523, 15.602394, 48.375466, 15.72916, 12574, 169)); list.add(new OneRun(48.410061, 15.63951, 48.411386, 15.604899, 3101, 94)); @@ -454,8 +428,7 @@ public void testKremsMountainBikeRelation() assertEquals(testCollector.toString(), 0, testCollector.errors.size()); } - List createAndorra() - { + List createAndorra() { List list = new ArrayList(); list.add(new OneRun(42.56819, 1.603231, 42.571034, 1.520662, 17708, 524)); list.add(new OneRun(42.529176, 1.571302, 42.571034, 1.520662, 11408, 305)); @@ -463,24 +436,21 @@ List createAndorra() } @Test - public void testAndorra() - { + public void testAndorra() { runAlgo(testCollector, DIR + "/andorra.osm.gz", "target/andorra-gh", createAndorra(), "car", true, "car", "shortest", false); assertEquals(testCollector.toString(), 0, testCollector.errors.size()); } @Test - public void testAndorraPbf() - { + public void testAndorraPbf() { runAlgo(testCollector, DIR + "/andorra.osm.pbf", "target/andorra-gh", createAndorra(), "car", true, "car", "shortest", false); assertEquals(testCollector.toString(), 0, testCollector.errors.size()); } @Test - public void testAndorraFoot() - { + public void testAndorraFoot() { List list = createAndorra(); list.get(0).setDistance(1, 16354); list.get(0).setLocs(1, 648); @@ -493,8 +463,7 @@ public void testAndorraFoot() } @Test - public void testCampoGrande() - { + public void testCampoGrande() { // test not only NE quadrant of earth! // bzcat campo-grande.osm.bz2 @@ -509,8 +478,7 @@ public void testCampoGrande() } @Test - public void testMonacoVia() - { + public void testMonacoVia() { OneRun oneRun = new OneRun(); oneRun.add(43.730729, 7.42135, 0, 0); oneRun.add(43.727697, 7.419199, 2581, 110); @@ -525,8 +493,7 @@ public void testMonacoVia() } @Test - public void testHarsdorf() - { + public void testHarsdorf() { List list = new ArrayList(); // choose Unterloher Weg and the following residential + cycleway list.add(new OneRun(50.004333, 11.600254, 50.044449, 11.543434, 6931, 184)); @@ -536,8 +503,7 @@ public void testHarsdorf() } @Test - public void testNeudrossenfeld() - { + public void testNeudrossenfeld() { List list = new ArrayList(); // choose cycleway (Dreschenauer Straße) list.add(new OneRun(49.987132, 11.510496, 50.018839, 11.505024, 3985, 106)); @@ -552,16 +518,14 @@ public void testNeudrossenfeld() /** * @param testAlsoCH if true also the CH algorithms will be tested which needs preparation and - * takes a bit longer + * takes a bit longer */ - Graph runAlgo( TestAlgoCollector testCollector, String osmFile, - String graphFile, List forEveryAlgo, String importVehicles, - boolean testAlsoCH, String vehicle, String weightStr, boolean is3D ) - { + Graph runAlgo(TestAlgoCollector testCollector, String osmFile, + String graphFile, List forEveryAlgo, String importVehicles, + boolean testAlsoCH, String vehicle, String weightStr, boolean is3D) { AlgoHelperEntry algoEntry = null; OneRun tmpOneRun = null; - try - { + try { Helper.removeDir(new File(graphFile)); GraphHopper hopper = new GraphHopperOSM(). setStoreOnFlush(true). @@ -584,12 +548,10 @@ Graph runAlgo( TestAlgoCollector testCollector, String osmFile, Collection prepares = RoutingAlgorithmIT.createAlgos(hopper.getGraphHopperStorage(), hopper.getLocationIndex(), encoder, testAlsoCH, tMode, weighting, hopper.getEncodingManager()); EdgeFilter edgeFilter = new DefaultEdgeFilter(encoder); - for (AlgoHelperEntry entry : prepares) - { + for (AlgoHelperEntry entry : prepares) { algoEntry = entry; LocationIndex idx = entry.getIdx(); - for (OneRun oneRun : forEveryAlgo) - { + for (OneRun oneRun : forEveryAlgo) { tmpOneRun = oneRun; List list = oneRun.getList(idx, edgeFilter); testCollector.assertDistance(algoEntry, list, oneRun); @@ -597,22 +559,19 @@ Graph runAlgo( TestAlgoCollector testCollector, String osmFile, } return hopper.getGraphHopperStorage(); - } catch (Exception ex) - { + } catch (Exception ex) { if (algoEntry == null) throw new RuntimeException("cannot handle file " + osmFile + ", " + ex.getMessage(), ex); throw new RuntimeException("cannot handle " + algoEntry.toString() + ", for " + tmpOneRun + ", file " + osmFile + ", " + ex.getMessage(), ex); - } finally - { + } finally { // Helper.removeDir(new File(graphFile)); } } @Test - public void testMonacoParallel() throws IOException - { + public void testMonacoParallel() throws IOException { System.out.println("testMonacoParallel takes a bit time..."); String graphFile = "target/monaco-gh"; Helper.removeDir(new File(graphFile)); @@ -637,23 +596,17 @@ public void testMonacoParallel() throws IOException int algosLength = 2; final Weighting weighting = new ShortestWeighting(encodingManager.getEncoder("car")); final EdgeFilter filter = new DefaultEdgeFilter(carEncoder); - for (int no = 0; no < MAX; no++) - { - for (int instanceNo = 0; instanceNo < instances.size(); instanceNo++) - { - String[] algos = new String[] - { + for (int no = 0; no < MAX; no++) { + for (int instanceNo = 0; instanceNo < instances.size(); instanceNo++) { + String[] algos = new String[]{ ASTAR, DIJKSTRA_BI }; - for (final String algoStr : algos) - { + for (final String algoStr : algos) { // an algorithm is not thread safe! reuse via clear() is ONLY appropriated if used from same thread! final int instanceIndex = instanceNo; - Thread t = new Thread() - { + Thread t = new Thread() { @Override - public void run() - { + public void run() { OneRun oneRun = instances.get(instanceIndex); AlgorithmOptions opts = AlgorithmOptions.start().flagEncoder(carEncoder).weighting(weighting).algorithm(algoStr).build(); testCollector.assertDistance(new AlgoHelperEntry(g, g, opts, idx), @@ -667,13 +620,10 @@ public void run() } } - for (Thread t : threads) - { - try - { + for (Thread t : threads) { + try { t.join(); - } catch (InterruptedException ex) - { + } catch (InterruptedException ex) { throw new RuntimeException(ex); } } diff --git a/reader-overlay-data/src/main/java/com/graphhopper/json/GHson.java b/reader-overlay-data/src/main/java/com/graphhopper/json/GHson.java index 1ce0673df4c..7225ce9f931 100644 --- a/reader-overlay-data/src/main/java/com/graphhopper/json/GHson.java +++ b/reader-overlay-data/src/main/java/com/graphhopper/json/GHson.java @@ -25,11 +25,10 @@ * * @author Peter Karich */ -public interface GHson -{ +public interface GHson { /** * This method reads JSON data from the provided source and creates an instance of the provided * class. */ - T fromJson( Reader source, Class aClass ); + T fromJson(Reader source, Class aClass); } diff --git a/reader-overlay-data/src/main/java/com/graphhopper/json/GHsonBuilder.java b/reader-overlay-data/src/main/java/com/graphhopper/json/GHsonBuilder.java index 2b9eb174841..1a1d1862f51 100644 --- a/reader-overlay-data/src/main/java/com/graphhopper/json/GHsonBuilder.java +++ b/reader-overlay-data/src/main/java/com/graphhopper/json/GHsonBuilder.java @@ -23,13 +23,10 @@ import com.graphhopper.json.geo.JsonFeature; /** - * * @author Peter Karich */ -public class GHsonBuilder -{ - public GHson create() - { +public class GHsonBuilder { + public GHson create() { // for now always return Gson implementation Gson gson = new GsonBuilder() .disableHtmlEscaping() diff --git a/reader-overlay-data/src/main/java/com/graphhopper/json/GHsonGson.java b/reader-overlay-data/src/main/java/com/graphhopper/json/GHsonGson.java index 83900155547..27fe0fc7cda 100644 --- a/reader-overlay-data/src/main/java/com/graphhopper/json/GHsonGson.java +++ b/reader-overlay-data/src/main/java/com/graphhopper/json/GHsonGson.java @@ -18,24 +18,21 @@ package com.graphhopper.json; import com.google.gson.Gson; + import java.io.Reader; /** - * * @author Peter Karich */ -public class GHsonGson implements GHson -{ +public class GHsonGson implements GHson { private final Gson gson; - public GHsonGson( Gson gson ) - { + public GHsonGson(Gson gson) { this.gson = gson; } @Override - public T fromJson( Reader source, Class aClass ) - { + public T fromJson(Reader source, Class aClass) { return gson.fromJson(source, aClass); } } diff --git a/reader-overlay-data/src/main/java/com/graphhopper/json/geo/FeatureJsonDeserializer.java b/reader-overlay-data/src/main/java/com/graphhopper/json/geo/FeatureJsonDeserializer.java index 478340ef641..f30e7f52b56 100644 --- a/reader-overlay-data/src/main/java/com/graphhopper/json/geo/FeatureJsonDeserializer.java +++ b/reader-overlay-data/src/main/java/com/graphhopper/json/geo/FeatureJsonDeserializer.java @@ -19,6 +19,7 @@ import com.google.gson.*; import com.graphhopper.util.shapes.BBox; + import java.lang.reflect.Type; import java.util.Map; import java.util.UUID; @@ -28,13 +29,10 @@ * * @author Peter Karich */ -public class FeatureJsonDeserializer implements JsonDeserializer -{ +public class FeatureJsonDeserializer implements JsonDeserializer { @Override - public JsonFeature deserialize( JsonElement json, Type type, JsonDeserializationContext context ) throws JsonParseException - { - try - { + public JsonFeature deserialize(JsonElement json, Type type, JsonDeserializationContext context) throws JsonParseException { + try { JsonFeature feature = new JsonFeature(); JsonObject obj = json.getAsJsonObject(); @@ -44,8 +42,7 @@ public JsonFeature deserialize( JsonElement json, Type type, JsonDeserialization else feature.id = UUID.randomUUID().toString(); - if (obj.has("properties")) - { + if (obj.has("properties")) { Map map = context.deserialize(obj.get("properties"), Map.class); feature.properties = map; } @@ -53,19 +50,16 @@ public JsonFeature deserialize( JsonElement json, Type type, JsonDeserialization if (obj.has("bbox")) feature.bbox = parseBBox(obj.get("bbox").getAsJsonArray()); - if (obj.has("geometry")) - { + if (obj.has("geometry")) { JsonObject geometry = obj.get("geometry").getAsJsonObject(); - if (geometry.has("coordinates")) - { + if (geometry.has("coordinates")) { if (!geometry.has("type")) throw new IllegalArgumentException("No type for non-empty coordinates specified"); String strType = context.deserialize(geometry.get("type"), String.class); feature.type = strType; - if ("Point".equals(strType)) - { + if ("Point".equals(strType)) { JsonArray arr = geometry.get("coordinates").getAsJsonArray(); double lon = arr.get(0).getAsDouble(); double lat = arr.get(1).getAsDouble(); @@ -74,16 +68,13 @@ public JsonFeature deserialize( JsonElement json, Type type, JsonDeserialization else feature.geometry = new Point(lat, lon); - } else if ("MultiPoint".equals(strType)) - { + } else if ("MultiPoint".equals(strType)) { feature.geometry = parseLineString(geometry); - } else if ("LineString".equals(strType)) - { + } else if ("LineString".equals(strType)) { feature.geometry = parseLineString(geometry); - } else - { + } else { throw new IllegalArgumentException("Coordinates type " + strType + " not yet supported"); } } @@ -91,20 +82,17 @@ public JsonFeature deserialize( JsonElement json, Type type, JsonDeserialization return feature; - } catch (Exception ex) - { + } catch (Exception ex) { throw new JsonParseException("Problem parsing JSON feature " + json); } } - LineString parseLineString( JsonObject geometry ) - { + LineString parseLineString(JsonObject geometry) { JsonArray arr = geometry.get("coordinates").getAsJsonArray(); boolean is3D = arr.size() == 0 || arr.get(0).getAsJsonArray().size() == 3; LineString lineString = new LineString(arr.size(), is3D); - for (int i = 0; i < arr.size(); i++) - { + for (int i = 0; i < arr.size(); i++) { JsonArray pointArr = arr.get(i).getAsJsonArray(); double lon = pointArr.get(0).getAsDouble(); double lat = pointArr.get(1).getAsDouble(); @@ -116,13 +104,11 @@ LineString parseLineString( JsonObject geometry ) return lineString; } - private BBox parseBBox( JsonArray arr ) - { + private BBox parseBBox(JsonArray arr) { // The value of the bbox member must be a 2*n array where n is the number of dimensions represented // in the contained geometries, with the lowest values for all axes followed by the highest values. // The axes order of a bbox follows the axes order of geometries => lon,lat,ele - if (arr.size() == 6) - { + if (arr.size() == 6) { double minLon = arr.get(0).getAsDouble(); double minLat = arr.get(1).getAsDouble(); double minEle = arr.get(2).getAsDouble(); @@ -133,8 +119,7 @@ private BBox parseBBox( JsonArray arr ) return new BBox(minLon, maxLon, minLat, maxLat, minEle, maxEle); - } else if (arr.size() == 4) - { + } else if (arr.size() == 4) { double minLon = arr.get(0).getAsDouble(); double minLat = arr.get(1).getAsDouble(); @@ -142,8 +127,7 @@ private BBox parseBBox( JsonArray arr ) double maxLat = arr.get(3).getAsDouble(); return new BBox(minLon, maxLon, minLat, maxLat); - } else - { + } else { throw new IllegalArgumentException("Illegal array dimension (" + arr.size() + ") of bbox " + arr.toString()); } } diff --git a/reader-overlay-data/src/main/java/com/graphhopper/json/geo/Geometry.java b/reader-overlay-data/src/main/java/com/graphhopper/json/geo/Geometry.java index 7cdb0b3aae6..e8eeefb72d5 100644 --- a/reader-overlay-data/src/main/java/com/graphhopper/json/geo/Geometry.java +++ b/reader-overlay-data/src/main/java/com/graphhopper/json/geo/Geometry.java @@ -21,11 +21,9 @@ import com.graphhopper.util.shapes.GHPoint; /** - * * @author Peter Karich */ -public interface Geometry -{ +public interface Geometry { String getType(); boolean isPoint(); diff --git a/reader-overlay-data/src/main/java/com/graphhopper/json/geo/JsonFeature.java b/reader-overlay-data/src/main/java/com/graphhopper/json/geo/JsonFeature.java index 3f87a6cb09e..466d30a3a75 100644 --- a/reader-overlay-data/src/main/java/com/graphhopper/json/geo/JsonFeature.java +++ b/reader-overlay-data/src/main/java/com/graphhopper/json/geo/JsonFeature.java @@ -18,63 +18,53 @@ package com.graphhopper.json.geo; import com.graphhopper.util.shapes.BBox; + import java.util.Map; /** - * * @author Peter Karich */ -public class JsonFeature -{ +public class JsonFeature { String id; String type; BBox bbox; Geometry geometry; Map properties; - public String getId() - { + public String getId() { return id; } - public String getType() - { + public String getType() { return type; } - public BBox getBBox() - { + public BBox getBBox() { return bbox; } - public boolean hasGeometry() - { + public boolean hasGeometry() { return geometry != null; } - public Geometry getGeometry() - { + public Geometry getGeometry() { return geometry; } - public boolean hasProperties() - { + public boolean hasProperties() { return properties != null; } - public Map getProperties() - { + public Map getProperties() { return properties; } - public Object getProperty( String key ) - { + public Object getProperty(String key) { return properties.get(key); } @Override - public String toString() - { + public String toString() { return "id:" + getId(); } } diff --git a/reader-overlay-data/src/main/java/com/graphhopper/json/geo/JsonFeatureCollection.java b/reader-overlay-data/src/main/java/com/graphhopper/json/geo/JsonFeatureCollection.java index 97c419abbb8..9e846a9ce5c 100644 --- a/reader-overlay-data/src/main/java/com/graphhopper/json/geo/JsonFeatureCollection.java +++ b/reader-overlay-data/src/main/java/com/graphhopper/json/geo/JsonFeatureCollection.java @@ -20,21 +20,17 @@ import java.util.List; /** - * * @author Peter Karich */ -public class JsonFeatureCollection -{ +public class JsonFeatureCollection { String type; List features; - public String getType() - { + public String getType() { return type; } - public List getFeatures() - { + public List getFeatures() { return features; } } diff --git a/reader-overlay-data/src/main/java/com/graphhopper/json/geo/LineString.java b/reader-overlay-data/src/main/java/com/graphhopper/json/geo/LineString.java index f0fbefe556c..d3c9359c617 100644 --- a/reader-overlay-data/src/main/java/com/graphhopper/json/geo/LineString.java +++ b/reader-overlay-data/src/main/java/com/graphhopper/json/geo/LineString.java @@ -25,40 +25,33 @@ * * @author Peter Karich */ -public class LineString extends PointList implements Geometry -{ - public LineString( int size, boolean is3D ) - { +public class LineString extends PointList implements Geometry { + public LineString(int size, boolean is3D) { super(size, is3D); } @Override - public String getType() - { + public String getType() { return "LineString"; } @Override - public boolean isPoint() - { + public boolean isPoint() { return false; } @Override - public GHPoint asPoint() - { + public GHPoint asPoint() { throw new UnsupportedOperationException("Not supported"); } @Override - public boolean isPointList() - { + public boolean isPointList() { return true; } @Override - public PointList asPointList() - { + public PointList asPointList() { return this; } } diff --git a/reader-overlay-data/src/main/java/com/graphhopper/json/geo/Point.java b/reader-overlay-data/src/main/java/com/graphhopper/json/geo/Point.java index 7cc699304fd..29ade6a432b 100644 --- a/reader-overlay-data/src/main/java/com/graphhopper/json/geo/Point.java +++ b/reader-overlay-data/src/main/java/com/graphhopper/json/geo/Point.java @@ -26,51 +26,42 @@ * * @author Peter Karich */ -public class Point extends GHPoint3D implements Geometry -{ - public Point( double lat, double lon ) - { +public class Point extends GHPoint3D implements Geometry { + public Point(double lat, double lon) { super(lat, lon, Double.NaN); } - public Point( double lat, double lon, double ele ) - { + public Point(double lat, double lon, double ele) { super(lat, lon, ele); } @Override - public String toString() - { + public String toString() { return lat + ", " + lon; } @Override - public boolean isPoint() - { + public boolean isPoint() { return true; } @Override - public GHPoint asPoint() - { + public GHPoint asPoint() { return this; } @Override - public boolean isPointList() - { + public boolean isPointList() { return false; } @Override - public PointList asPointList() - { + public PointList asPointList() { throw new UnsupportedOperationException("Not supported"); } @Override - public String getType() - { + public String getType() { return "Point"; } } diff --git a/reader-overlay-data/src/main/java/com/graphhopper/reader/overlaydata/FeedOverlayData.java b/reader-overlay-data/src/main/java/com/graphhopper/reader/overlaydata/FeedOverlayData.java index e19d2d850e0..3ea9cf8933a 100644 --- a/reader-overlay-data/src/main/java/com/graphhopper/reader/overlaydata/FeedOverlayData.java +++ b/reader-overlay-data/src/main/java/com/graphhopper/reader/overlaydata/FeedOverlayData.java @@ -31,24 +31,23 @@ import com.graphhopper.storage.index.QueryResult; import com.graphhopper.util.BreadthFirstSearch; import com.graphhopper.util.EdgeIteratorState; -import com.graphhopper.util.Helper; import com.graphhopper.util.PointList; import com.graphhopper.util.shapes.BBox; import com.graphhopper.util.shapes.GHPoint; import gnu.trove.iterator.TIntIterator; import gnu.trove.set.TIntSet; import gnu.trove.set.hash.TIntHashSet; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import java.io.*; import java.util.List; import java.util.Map; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; /** * @author Peter Karich */ -public class FeedOverlayData -{ +public class FeedOverlayData { private final Logger logger = LoggerFactory.getLogger(getClass()); private final Graph graph; private final LocationIndex locationIndex; @@ -56,46 +55,37 @@ public class FeedOverlayData private final EncodingManager em; private boolean enableLogging = false; - public FeedOverlayData( Graph graph, EncodingManager em, LocationIndex locationIndex, GHson ghson ) - { + public FeedOverlayData(Graph graph, EncodingManager em, LocationIndex locationIndex, GHson ghson) { this.ghson = ghson; this.graph = graph; this.em = em; this.locationIndex = locationIndex; } - public void setLogging( boolean log ) - { + public void setLogging(boolean log) { enableLogging = log; } - public long applyChanges( String fileOrFolderStr ) - { + public long applyChanges(String fileOrFolderStr) { File fileOrFolder = new File(fileOrFolderStr); - try - { - if (fileOrFolder.isFile()) - { + try { + if (fileOrFolder.isFile()) { return applyChanges(new FileReader(fileOrFolder)); } long sum = 0; - File[] fList = new File(fileOrFolderStr).listFiles(new FilenameFilter() - { + File[] fList = new File(fileOrFolderStr).listFiles(new FilenameFilter() { @Override - public boolean accept( File dir, String name ) - { + public boolean accept(File dir, String name) { return name.endsWith(".json"); } }); - for (File f : fList) - { + for (File f : fList) { sum += applyChanges(new FileReader(f)); } return sum; - } catch (FileNotFoundException ex) - { + } catch (FileNotFoundException ex) { throw new RuntimeException(ex); } } @@ -105,27 +95,21 @@ public boolean accept( File dir, String name ) * * @return number of successfully applied edge changes */ - public long applyChanges( Reader reader ) - { + public long applyChanges(Reader reader) { // read full file, later support one json feature or collection per line to avoid high mem consumption JsonFeatureCollection data = ghson.fromJson(reader, JsonFeatureCollection.class); long updates = 0; - for (JsonFeature jsonFeature : data.getFeatures()) - { + for (JsonFeature jsonFeature : data.getFeatures()) { if (!jsonFeature.hasProperties()) throw new IllegalArgumentException("One feature has no properties, please specify properties e.g. speed or access"); List encodersAsStr = (List) jsonFeature.getProperty("vehicles"); - if (encodersAsStr == null) - { - for (FlagEncoder encoder : em.fetchEdgeEncoders()) - { + if (encodersAsStr == null) { + for (FlagEncoder encoder : em.fetchEdgeEncoders()) { updates += applyChange(jsonFeature, encoder); } - } else - { - for (String encoderStr : encodersAsStr) - { + } else { + for (String encoderStr : encodersAsStr) { updates += applyChange(jsonFeature, em.getEncoder(encoderStr)); } } @@ -134,42 +118,35 @@ public long applyChanges( Reader reader ) return updates; } - private long applyChange( JsonFeature jsonFeature, FlagEncoder encoder ) - { + private long applyChange(JsonFeature jsonFeature, FlagEncoder encoder) { long updates = 0; EdgeFilter filter = new DefaultEdgeFilter(encoder); TIntSet edges = new TIntHashSet(); - if (jsonFeature.hasGeometry()) - { + if (jsonFeature.hasGeometry()) { fillEdgeIDs(edges, jsonFeature.getGeometry(), filter); - } else if (jsonFeature.getBBox() != null) - { + } else if (jsonFeature.getBBox() != null) { fillEdgeIDs(edges, jsonFeature.getBBox(), filter); } else throw new IllegalArgumentException("Feature " + jsonFeature.getId() + " has no geometry and no bbox"); TIntIterator iter = edges.iterator(); Map props = jsonFeature.getProperties(); - while (iter.hasNext()) - { + while (iter.hasNext()) { int edgeId = iter.next(); EdgeIteratorState edge = graph.getEdgeIteratorState(edgeId, Integer.MIN_VALUE); - if (props.containsKey("access")) - { + if (props.containsKey("access")) { boolean value = (boolean) props.get("access"); updates++; if (enableLogging) logger.info(encoder.toString() + " - access change via feature " + jsonFeature.getId()); edge.setFlags(encoder.setAccess(edge.getFlags(), value, value)); - } else if (props.containsKey("speed")) - { + } else if (props.containsKey("speed")) { // TODO use different speed for the different directions (see e.g. Bike2WeightFlagEncoder) double value = ((Number) props.get("speed")).doubleValue(); double oldSpeed = encoder.getSpeed(edge.getFlags()); - if (oldSpeed != value) - { + if (oldSpeed != value) { updates++; if (enableLogging) logger.info(encoder.toString() + " - speed change via feature " + jsonFeature.getId() + ". Old: " + oldSpeed + ", new:" + value); @@ -180,33 +157,26 @@ private long applyChange( JsonFeature jsonFeature, FlagEncoder encoder ) return updates; } - public void fillEdgeIDs( TIntSet edgeIds, Geometry geometry, EdgeFilter filter ) - { - if (geometry.isPoint()) - { + public void fillEdgeIDs(TIntSet edgeIds, Geometry geometry, EdgeFilter filter) { + if (geometry.isPoint()) { GHPoint point = geometry.asPoint(); QueryResult qr = locationIndex.findClosest(point.lat, point.lon, filter); if (qr.isValid()) edgeIds.add(qr.getClosestEdge().getEdge()); - } else if (geometry.isPointList()) - { + } else if (geometry.isPointList()) { PointList pl = geometry.asPointList(); - if (geometry.getType().equals("LineString")) - { + if (geometry.getType().equals("LineString")) { // TODO do map matching or routing int lastIdx = pl.size() - 1; - if (pl.size() >= 2) - { + if (pl.size() >= 2) { double meanLat = (pl.getLatitude(0) + pl.getLatitude(lastIdx)) / 2; double meanLon = (pl.getLongitude(0) + pl.getLongitude(lastIdx)) / 2; QueryResult qr = locationIndex.findClosest(meanLat, meanLon, filter); if (qr.isValid()) edgeIds.add(qr.getClosestEdge().getEdge()); } - } else - { - for (int i = 0; i < pl.size(); i++) - { + } else { + for (int i = 0; i < pl.size(); i++) { QueryResult qr = locationIndex.findClosest(pl.getLatitude(i), pl.getLongitude(i), filter); if (qr.isValid()) edgeIds.add(qr.getClosestEdge().getEdge()); @@ -215,28 +185,23 @@ public void fillEdgeIDs( TIntSet edgeIds, Geometry geometry, EdgeFilter filter ) } } - public void fillEdgeIDs( final TIntSet edgeIds, final BBox bbox, EdgeFilter filter ) - { + public void fillEdgeIDs(final TIntSet edgeIds, final BBox bbox, EdgeFilter filter) { QueryResult qr = locationIndex.findClosest((bbox.maxLat + bbox.minLat) / 2, (bbox.maxLon + bbox.minLon) / 2, filter); if (!qr.isValid()) return; - BreadthFirstSearch bfs = new BreadthFirstSearch() - { + BreadthFirstSearch bfs = new BreadthFirstSearch() { final NodeAccess na = graph.getNodeAccess(); final BBox localBBox = bbox; @Override - protected boolean goFurther( int nodeId ) - { + protected boolean goFurther(int nodeId) { return localBBox.contains(na.getLatitude(nodeId), na.getLongitude(nodeId)); } @Override - protected boolean checkAdjacent( EdgeIteratorState edge ) - { - if (localBBox.contains(na.getLatitude(edge.getAdjNode()), na.getLongitude(edge.getAdjNode()))) - { + protected boolean checkAdjacent(EdgeIteratorState edge) { + if (localBBox.contains(na.getLatitude(edge.getAdjNode()), na.getLongitude(edge.getAdjNode()))) { edgeIds.add(edge.getEdge()); return true; } diff --git a/reader-overlay-data/src/test/java/com/graphhopper/json/geo/JsonFeatureCollectionTest.java b/reader-overlay-data/src/test/java/com/graphhopper/json/geo/JsonFeatureCollectionTest.java index d325f9751c8..691fe96d278 100644 --- a/reader-overlay-data/src/test/java/com/graphhopper/json/geo/JsonFeatureCollectionTest.java +++ b/reader-overlay-data/src/test/java/com/graphhopper/json/geo/JsonFeatureCollectionTest.java @@ -20,23 +20,22 @@ import com.graphhopper.json.GHson; import com.graphhopper.json.GHsonBuilder; import com.graphhopper.util.Helper; +import org.junit.Test; + import java.io.InputStreamReader; import java.io.Reader; import java.util.Map; -import org.junit.Test; -import static org.junit.Assert.*; + +import static org.junit.Assert.assertEquals; /** - * * @author Peter Karich */ -public class JsonFeatureCollectionTest -{ +public class JsonFeatureCollectionTest { private final GHson ghson = new GHsonBuilder().create(); @Test - public void testDeserialization() - { + public void testDeserialization() { JsonFeatureCollection data = getJson("geojson1.json"); assertEquals(3, data.getFeatures().size()); @@ -60,8 +59,7 @@ public void testDeserialization() assertEquals("a", ((Map) f3.getProperty("prop1")).get("test")); } - JsonFeatureCollection getJson( String name ) - { + JsonFeatureCollection getJson(String name) { Reader reader = new InputStreamReader(getClass().getResourceAsStream(name), Helper.UTF_CS); return ghson.fromJson(reader, JsonFeatureCollection.class); } diff --git a/reader-overlay-data/src/test/java/com/graphhopper/reader/overlaydata/FeedOverlayDataTest.java b/reader-overlay-data/src/test/java/com/graphhopper/reader/overlaydata/FeedOverlayDataTest.java index b30900e3515..c1eb195cf81 100644 --- a/reader-overlay-data/src/test/java/com/graphhopper/reader/overlaydata/FeedOverlayDataTest.java +++ b/reader-overlay-data/src/test/java/com/graphhopper/reader/overlaydata/FeedOverlayDataTest.java @@ -13,32 +13,31 @@ import com.graphhopper.storage.index.LocationIndexTree; import com.graphhopper.util.GHUtility; import com.graphhopper.util.Helper; +import org.junit.Before; +import org.junit.Test; + import java.io.InputStreamReader; import java.io.Reader; -import org.junit.Test; + import static org.junit.Assert.*; -import org.junit.Before; /** * @author Peter Karich */ -public class FeedOverlayDataTest -{ +public class FeedOverlayDataTest { private EncodingManager encodingManager; private GraphHopperStorage graph; private GHson ghson; @Before - public void setUp() - { + public void setUp() { encodingManager = new EncodingManager("car"); graph = new GraphBuilder(encodingManager).create(); ghson = new GHsonBuilder().create(); } @Test - public void testApplyChanges() - { + public void testApplyChanges() { // 0-1-2 // | | // 3-4 @@ -57,8 +56,7 @@ public void testApplyChanges() FlagEncoder encoder = encodingManager.getEncoder("car"); double defaultSpeed = encoder.getSpeed(GHUtility.getEdge(graph, 0, 1).getFlags()); AllEdgesIterator iter = graph.getAllEdges(); - while (iter.next()) - { + while (iter.next()) { long flags = GHUtility.getEdge(graph, 0, 1).getFlags(); assertEquals(defaultSpeed, encoder.getSpeed(flags), .1); assertTrue(encoder.isForward(flags)); diff --git a/tools-lgpl/src/main/java/com/graphhopper/coll/OTPIntDoubleBinHeap.java b/tools-lgpl/src/main/java/com/graphhopper/coll/OTPIntDoubleBinHeap.java index da1776280b7..d977f7dd8bc 100644 --- a/tools-lgpl/src/main/java/com/graphhopper/coll/OTPIntDoubleBinHeap.java +++ b/tools-lgpl/src/main/java/com/graphhopper/coll/OTPIntDoubleBinHeap.java @@ -17,23 +17,19 @@ the License, or (at your option) any later version. /** * Taken from opentripplanner. */ -public class OTPIntDoubleBinHeap -{ +public class OTPIntDoubleBinHeap { private static final double GROW_FACTOR = 2.0; private float[] keys; private int[] elem; private int size; private int capacity; - public OTPIntDoubleBinHeap() - { + public OTPIntDoubleBinHeap() { this(1000); } - public OTPIntDoubleBinHeap( int capacity ) - { - if (capacity < 10) - { + public OTPIntDoubleBinHeap(int capacity) { + if (capacity < 10) { capacity = 10; } this.capacity = capacity; @@ -45,82 +41,64 @@ public OTPIntDoubleBinHeap( int capacity ) keys[0] = Float.NEGATIVE_INFINITY; } - public int getSize() - { + public int getSize() { return size; } - public int size() - { + public int size() { return size; } - public boolean isEmpty() - { + public boolean isEmpty() { return size == 0; } - public Double peekKey() - { + public Double peekKey() { return peek_key(); } - public double peek_key() - { - if (size > 0) - { + public double peek_key() { + if (size > 0) { return keys[1]; - } else - { + } else { throw new IllegalStateException("An empty queue does not have a minimum key."); } } - public Integer peekElement() - { + public Integer peekElement() { return peek_element(); } - public int peek_element() - { - if (size > 0) - { + public int peek_element() { + if (size > 0) { return elem[1]; - } else - { + } else { throw new IllegalStateException("An empty queue does not have a minimum value."); } } - public Integer pollElement() - { + public Integer pollElement() { return poll_element(); } - public int poll_element() - { + public int poll_element() { int i, child; int minElem = elem[1]; int lastElem = elem[size]; double lastPrio = keys[size]; - if (size <= 0) - { + if (size <= 0) { throw new IllegalStateException("An empty queue does not have a minimum value."); } size -= 1; - for (i = 1; i * 2 <= size; i = child) - { + for (i = 1; i * 2 <= size; i = child) { child = i * 2; - if (child != size && keys[child + 1] < keys[child]) - { + if (child != size && keys[child + 1] < keys[child]) { child++; } - if (lastPrio > keys[child]) - { + if (lastPrio > keys[child]) { elem[i] = elem[child]; keys[i] = keys[child]; - } else - { + } else { break; } } @@ -129,55 +107,43 @@ public int poll_element() return minElem; } - public void update( Number key, Integer element ) - { + public void update(Number key, Integer element) { update_(key.doubleValue(), element); } - public boolean update_( double key, int element ) - { + public boolean update_(double key, int element) { // Perform "inefficient" but straightforward linear search // for an element then change its key by sifting up or down int i; - for (i = 1; i <= size; i++) - { - if (elem[i] == element) - { + for (i = 1; i <= size; i++) { + if (elem[i] == element) { break; } } - if (i > size) - { + if (i > size) { return false; } - if (key > keys[i]) - { + if (key > keys[i]) { // sift up (as in extract) - while (i * 2 <= size) - { + while (i * 2 <= size) { int child = i * 2; - if (child != size && keys[child + 1] < keys[child]) - { + if (child != size && keys[child + 1] < keys[child]) { child++; } - if (key > keys[child]) - { + if (key > keys[child]) { elem[i] = elem[child]; keys[i] = keys[child]; i = child; - } else - { + } else { break; } } elem[i] = element; keys[i] = (float) key; - } else - { + } else { // sift down (as in insert_) - while (keys[i / 2] > key) - { + while (keys[i / 2] > key) { elem[i] = elem[i / 2]; keys[i] = keys[i / 2]; i /= 2; @@ -188,21 +154,17 @@ public boolean update_( double key, int element ) return true; } - public void insert( Number key, Integer element ) - { + public void insert(Number key, Integer element) { insert_(key.doubleValue(), element); } - public void insert_( double key, int element ) - { + public void insert_(double key, int element) { int i; size += 1; - if (size > capacity) - { + if (size > capacity) { ensureCapacity((int) (capacity * GROW_FACTOR)); } - for (i = size; keys[i / 2] > key; i /= 2) - { + for (i = size; keys[i / 2] > key; i /= 2) { elem[i] = elem[i / 2]; keys[i] = keys[i / 2]; } @@ -210,11 +172,9 @@ public void insert_( double key, int element ) keys[i] = (float) key; } - public void ensureCapacity( int capacity ) - { + public void ensureCapacity(int capacity) { // System.out.println("Growing queue to " + capacity); - if (capacity < size) - { + if (capacity < size) { throw new IllegalStateException("BinHeap contains too many elements to fit in new capacity."); } this.capacity = capacity; @@ -222,29 +182,24 @@ public void ensureCapacity( int capacity ) elem = Arrays.copyOf(elem, capacity + 1); } - public int getCapacity() - { + public int getCapacity() { return capacity; } - float getKey( int index ) - { + float getKey(int index) { return keys[index]; } - int getElement( int index ) - { + int getElement(int index) { return elem[index]; } - void set( int index, float key, int element ) - { + void set(int index, float key, int element) { keys[index] = key; elem[index] = element; } - void trimTo( int toSize ) - { + void trimTo(int toSize) { this.size = toSize; toSize++; // necessary as we currently do not init arrays when inserting @@ -252,19 +207,15 @@ void trimTo( int toSize ) Arrays.fill(elem, toSize, size + 1, 0); } - public void clear() - { + public void clear() { trimTo(0); } @Override - public String toString() - { + public String toString() { StringBuilder sb = new StringBuilder(); - for (int i = 1; i <= size; i++) - { - if (i > 1) - { + for (int i = 1; i <= size; i++) { + if (i > 1) { sb.append(", "); } sb.append(keys[i]).append(":").append(elem[i]); @@ -272,13 +223,10 @@ public String toString() return sb.toString(); } - public String toKeyString() - { + public String toKeyString() { StringBuilder sb = new StringBuilder(); - for (int i = 1; i <= size; i++) - { - if (i > 1) - { + for (int i = 1; i <= size; i++) { + if (i > 1) { sb.append(", "); } sb.append(keys[i]); @@ -286,12 +234,9 @@ public String toKeyString() return sb.toString(); } - public int indexOfValue( int value ) - { - for (int i = 0; i <= size; i++) - { - if (elem[i] == value) - { + public int indexOfValue(int value) { + for (int i = 0; i <= size; i++) { + if (elem[i] == value) { return i; } } diff --git a/tools/src/main/java/com/graphhopper/tools/Bzip2.java b/tools/src/main/java/com/graphhopper/tools/Bzip2.java index 35d240e2fe6..49db0f8444b 100644 --- a/tools/src/main/java/com/graphhopper/tools/Bzip2.java +++ b/tools/src/main/java/com/graphhopper/tools/Bzip2.java @@ -18,28 +18,23 @@ package com.graphhopper.tools; import com.graphhopper.util.Helper; +import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; -import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream; - /** * Simple bzip2 uncompression. TODO integrate with OSMReader! */ -public class Bzip2 -{ - public static void main( String[] args ) throws IOException - { - if (args.length == 0) - { +public class Bzip2 { + public static void main(String[] args) throws IOException { + if (args.length == 0) { throw new IllegalArgumentException("You need to specify the bz2 file!"); } String fromFile = args[0]; - if (!fromFile.endsWith(".bz2")) - { + if (!fromFile.endsWith(".bz2")) { throw new IllegalArgumentException("You need to specify a bz2 file! But was:" + fromFile); } String toFile = Helper.pruneFileEnd(fromFile); @@ -47,16 +42,13 @@ public static void main( String[] args ) throws IOException FileInputStream in = new FileInputStream(fromFile); FileOutputStream out = new FileOutputStream(toFile); BZip2CompressorInputStream bzIn = new BZip2CompressorInputStream(in); - try - { + try { final byte[] buffer = new byte[1024 * 8]; int n = 0; - while (-1 != (n = bzIn.read(buffer))) - { + while (-1 != (n = bzIn.read(buffer))) { out.write(buffer, 0, n); } - } finally - { + } finally { out.close(); bzIn.close(); } diff --git a/tools/src/main/java/com/graphhopper/tools/Import.java b/tools/src/main/java/com/graphhopper/tools/Import.java index bc95f8434ff..9b668df7579 100644 --- a/tools/src/main/java/com/graphhopper/tools/Import.java +++ b/tools/src/main/java/com/graphhopper/tools/Import.java @@ -24,10 +24,8 @@ /** * @author Peter Karich */ -public class Import -{ - public static void main( String[] strs ) throws Exception - { +public class Import { + public static void main(String[] strs) throws Exception { CmdArgs args = CmdArgs.read(strs); GraphHopper hopper = new GraphHopperOSM().init(args); hopper.importOrLoad(); diff --git a/tools/src/main/java/com/graphhopper/tools/Measurement.java b/tools/src/main/java/com/graphhopper/tools/Measurement.java index 39c17511e40..9e1b52773fe 100644 --- a/tools/src/main/java/com/graphhopper/tools/Measurement.java +++ b/tools/src/main/java/com/graphhopper/tools/Measurement.java @@ -17,22 +17,27 @@ */ package com.graphhopper.tools; -import com.graphhopper.routing.weighting.Weighting; -import com.graphhopper.PathWrapper; import com.graphhopper.GHRequest; import com.graphhopper.GHResponse; import com.graphhopper.GraphHopper; +import com.graphhopper.PathWrapper; import com.graphhopper.coll.GHBitSet; import com.graphhopper.coll.GHBitSetImpl; import com.graphhopper.reader.DataReader; import com.graphhopper.reader.osm.GraphHopperOSM; import com.graphhopper.routing.util.*; -import com.graphhopper.storage.*; +import com.graphhopper.routing.weighting.Weighting; +import com.graphhopper.storage.CHGraph; +import com.graphhopper.storage.Graph; +import com.graphhopper.storage.GraphHopperStorage; +import com.graphhopper.storage.NodeAccess; import com.graphhopper.storage.index.LocationIndex; import com.graphhopper.util.*; import com.graphhopper.util.Parameters.Algorithms; import com.graphhopper.util.Parameters.CH; import com.graphhopper.util.shapes.BBox; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.io.FileWriter; import java.io.IOException; @@ -45,28 +50,22 @@ import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicLong; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - /** * @author Peter Karich */ -public class Measurement -{ - public static void main( String[] strs ) - { - new Measurement().start(CmdArgs.read(strs)); - } - +public class Measurement { private static final Logger logger = LoggerFactory.getLogger(Measurement.class); private final Map properties = new TreeMap(); private long seed; private int maxNode; + public static void main(String[] strs) { + new Measurement().start(CmdArgs.read(strs)); + } + // creates properties file in the format key=value // Every value is one y-value in a separate diagram with an identical x-value for every Measurement.start call - void start( CmdArgs args ) - { + void start(CmdArgs args) { String graphLocation = args.get("graph.location", ""); String propLocation = args.get("measurement.location", ""); if (Helper.isEmpty(propLocation)) @@ -76,11 +75,9 @@ void start( CmdArgs args ) String gitCommit = args.get("measurement.gitinfo", ""); int count = args.getInt("measurement.count", 5000); - GraphHopper hopper = new GraphHopperOSM() - { + GraphHopper hopper = new GraphHopperOSM() { @Override - protected void prepare() - { + protected void prepare() { StopWatch sw = new StopWatch().start(); super.prepare(); put("prepare.time", sw.stop().getTime()); @@ -91,8 +88,7 @@ protected void prepare() } @Override - protected DataReader importData() throws IOException - { + protected DataReader importData() throws IOException { StopWatch sw = new StopWatch().start(); DataReader dr = super.importData(); put("graph.import_time", sw.stop().getSeconds()); @@ -114,8 +110,7 @@ protected DataReader importData() throws IOException Weighting weighting = hopper.getCHFactoryDecorator().getWeightings().get(0); StopWatch sw = new StopWatch().start(); - try - { + try { maxNode = g.getNodes(); GHBitSet allowedEdges = printGraphDetails(g, vehicleStr); boolean isCH = false; @@ -133,12 +128,10 @@ protected DataReader importData() throws IOException printTimeOfRouteQuery(hopper, isCH, count, "routingCH", vehicleStr, true); printTimeOfRouteQuery(hopper, isCH, count, "routingCH_no_instr", vehicleStr, false); logger.info("store into " + propLocation); - } catch (Exception ex) - { + } catch (Exception ex) { logger.error("Problem while measuring " + graphLocation, ex); put("error", ex.toString()); - } finally - { + } finally { put("measurement.gitinfo", gitCommit); put("measurement.count", count); put("measurement.seed", seed); @@ -146,28 +139,23 @@ protected DataReader importData() throws IOException System.gc(); put("measurement.totalMB", Helper.getTotalMB()); put("measurement.usedMB", Helper.getUsedMB()); - try - { + try { store(new FileWriter(propLocation), "measurement finish, " + new Date().toString() + ", " + Constants.BUILD_DATE); - } catch (IOException ex) - { + } catch (IOException ex) { logger.error("Problem while storing properties " + graphLocation + ", " + propLocation, ex); } } } - void fillAllowedEdges( AllEdgesIterator iter, GHBitSet bs ) - { + void fillAllowedEdges(AllEdgesIterator iter, GHBitSet bs) { bs.clear(); - while (iter.next()) - { + while (iter.next()) { bs.add(iter.getEdge()); } } - private GHBitSet printGraphDetails( GraphHopperStorage g, String vehicleStr ) - { + private GHBitSet printGraphDetails(GraphHopperStorage g, String vehicleStr) { // graph size (edge, node and storage size) put("graph.nodes", g.getNodes()); put("graph.edges", g.getAllEdges().getMaxId()); @@ -182,18 +170,15 @@ private GHBitSet printGraphDetails( GraphHopperStorage g, String vehicleStr ) return allowedEdges; } - private void printLocationIndexQuery( Graph g, final LocationIndex idx, int count ) - { + private void printLocationIndexQuery(Graph g, final LocationIndex idx, int count) { count *= 2; final BBox bbox = g.getBounds(); final double latDelta = bbox.maxLat - bbox.minLat; final double lonDelta = bbox.maxLon - bbox.minLon; final Random rand = new Random(seed); - MiniPerfTest miniPerf = new MiniPerfTest() - { + MiniPerfTest miniPerf = new MiniPerfTest() { @Override - public int doCalc( boolean warmup, int run ) - { + public int doCalc(boolean warmup, int run) { double lat = rand.nextDouble() * latDelta + bbox.minLat; double lon = rand.nextDouble() * lonDelta + bbox.minLon; int val = idx.findClosest(lat, lon, EdgeFilter.ALL_EDGES).getClosestNode(); @@ -207,21 +192,17 @@ public int doCalc( boolean warmup, int run ) print("location_index", miniPerf); } - private void printMiscUnitPerfTests( final Graph graph, boolean isCH, final FlagEncoder encoder, - int count, final GHBitSet allowedEdges ) - { + private void printMiscUnitPerfTests(final Graph graph, boolean isCH, final FlagEncoder encoder, + int count, final GHBitSet allowedEdges) { final Random rand = new Random(seed); String description = ""; - if (isCH) - { + if (isCH) { description = "CH"; CHGraph lg = (CHGraph) graph; final CHEdgeExplorer chExplorer = lg.createEdgeExplorer(new LevelEdgeFilter(lg)); - MiniPerfTest miniPerf = new MiniPerfTest() - { + MiniPerfTest miniPerf = new MiniPerfTest() { @Override - public int doCalc( boolean warmup, int run ) - { + public int doCalc(boolean warmup, int run) { int nodeId = rand.nextInt(maxNode); return GHUtility.count(chExplorer.setBaseNode(nodeId)); } @@ -229,15 +210,12 @@ public int doCalc( boolean warmup, int run ) print("unit_testsCH.level_edge_state_next", miniPerf); final CHEdgeExplorer chExplorer2 = lg.createEdgeExplorer(); - miniPerf = new MiniPerfTest() - { + miniPerf = new MiniPerfTest() { @Override - public int doCalc( boolean warmup, int run ) - { + public int doCalc(boolean warmup, int run) { int nodeId = rand.nextInt(maxNode); CHEdgeIterator iter = chExplorer2.setBaseNode(nodeId); - while (iter.next()) - { + while (iter.next()) { if (iter.isShortcut()) nodeId += (int) iter.getWeight(); } @@ -249,11 +227,9 @@ public int doCalc( boolean warmup, int run ) EdgeFilter outFilter = new DefaultEdgeFilter(encoder, false, true); final EdgeExplorer outExplorer = graph.createEdgeExplorer(outFilter); - MiniPerfTest miniPerf = new MiniPerfTest() - { + MiniPerfTest miniPerf = new MiniPerfTest() { @Override - public int doCalc( boolean warmup, int run ) - { + public int doCalc(boolean warmup, int run) { int nodeId = rand.nextInt(maxNode); return GHUtility.count(outExplorer.setBaseNode(nodeId)); } @@ -261,11 +237,9 @@ public int doCalc( boolean warmup, int run ) print("unit_tests" + description + ".out_edge_state_next", miniPerf); final EdgeExplorer allExplorer = graph.createEdgeExplorer(); - miniPerf = new MiniPerfTest() - { + miniPerf = new MiniPerfTest() { @Override - public int doCalc( boolean warmup, int run ) - { + public int doCalc(boolean warmup, int run) { int nodeId = rand.nextInt(maxNode); return GHUtility.count(allExplorer.setBaseNode(nodeId)); } @@ -273,13 +247,10 @@ public int doCalc( boolean warmup, int run ) print("unit_tests" + description + ".all_edge_state_next", miniPerf); final int maxEdgesId = graph.getAllEdges().getMaxId(); - miniPerf = new MiniPerfTest() - { + miniPerf = new MiniPerfTest() { @Override - public int doCalc( boolean warmup, int run ) - { - while (true) - { + public int doCalc(boolean warmup, int run) { + while (true) { int edgeId = rand.nextInt(maxEdgesId); if (allowedEdges.contains(edgeId)) return graph.getEdgeIteratorState(edgeId, Integer.MIN_VALUE).getEdge(); @@ -289,9 +260,8 @@ public int doCalc( boolean warmup, int run ) print("unit_tests" + description + ".get_edge_state", miniPerf); } - private void printTimeOfRouteQuery( final GraphHopper hopper, final boolean ch, int count, String prefix, - final String vehicle, final boolean withInstructions ) - { + private void printTimeOfRouteQuery(final GraphHopper hopper, final boolean ch, int count, String prefix, + final String vehicle, final boolean withInstructions) { final Graph g = hopper.getGraphHopperStorage(); final AtomicLong maxDistance = new AtomicLong(0); final AtomicLong minDistance = new AtomicLong(Long.MAX_VALUE); @@ -310,11 +280,9 @@ private void printTimeOfRouteQuery( final GraphHopper hopper, final boolean ch, // if using none-bidirectional algorithm make sure you exclude CH routing final String algo = Algorithms.DIJKSTRA_BI; - MiniPerfTest miniPerf = new MiniPerfTest() - { + MiniPerfTest miniPerf = new MiniPerfTest() { @Override - public int doCalc( boolean warmup, int run ) - { + public int doCalc(boolean warmup, int run) { int from = rand.nextInt(maxNode); int to = rand.nextInt(maxNode); double fromLat = na.getLatitude(from); @@ -332,18 +300,15 @@ public int doCalc( boolean warmup, int run ) // req.getHints().put(algo + ".epsilon", 2); req.getHints().put("instructions", withInstructions); GHResponse rsp; - try - { + try { rsp = hopper.route(req); - } catch (Exception ex) - { + } catch (Exception ex) { // 'not found' can happen if import creates more than one subnetwork throw new RuntimeException("Error while calculating route! " + "nodes:" + from + " -> " + to + ", request:" + req, ex); } - if (rsp.hasErrors()) - { + if (rsp.hasErrors()) { if (!warmup) failedCount.incrementAndGet(); @@ -354,8 +319,7 @@ public int doCalc( boolean warmup, int run ) } PathWrapper arsp = rsp.getBest(); - if (!warmup) - { + if (!warmup) { visitedNodesSum.addAndGet(rsp.getHints().getLong("visited_nodes.sum", 0)); long dist = (long) arsp.getDistance(); distSum.addAndGet(dist); @@ -392,8 +356,7 @@ public int doCalc( boolean warmup, int run ) print(prefix, miniPerf); } - void print( String prefix, MiniPerfTest perf ) - { + void print(String prefix, MiniPerfTest perf) { logger.info(prefix + ": " + perf.getReport()); put(prefix + ".sum", perf.getSum()); // put(prefix+".rms", perf.getRMS()); @@ -402,17 +365,14 @@ void print( String prefix, MiniPerfTest perf ) put(prefix + ".max", perf.getMax()); } - void put( String key, Object val ) - { + void put(String key, Object val) { // convert object to string to make serialization possible properties.put(key, "" + val); } - private void store( FileWriter fileWriter, String comment ) throws IOException - { + private void store(FileWriter fileWriter, String comment) throws IOException { fileWriter.append("#" + comment + "\n"); - for (Entry e : properties.entrySet()) - { + for (Entry e : properties.entrySet()) { fileWriter.append(e.getKey()); fileWriter.append("="); fileWriter.append(e.getValue()); diff --git a/tools/src/main/java/com/graphhopper/tools/QueryTorture.java b/tools/src/main/java/com/graphhopper/tools/QueryTorture.java index d5a1044c328..61facd840f9 100644 --- a/tools/src/main/java/com/graphhopper/tools/QueryTorture.java +++ b/tools/src/main/java/com/graphhopper/tools/QueryTorture.java @@ -17,13 +17,10 @@ */ package com.graphhopper.tools; -import com.graphhopper.util.CmdArgs; -import com.graphhopper.util.DistanceCalc; -import com.graphhopper.util.DistanceCalcEarth; -import com.graphhopper.util.Downloader; -import com.graphhopper.util.Helper; -import com.graphhopper.util.StopWatch; +import com.graphhopper.util.*; import com.graphhopper.util.shapes.GHPoint; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.io.*; import java.net.MalformedURLException; @@ -34,21 +31,13 @@ import java.util.concurrent.atomic.AtomicInteger; import java.util.zip.GZIPInputStream; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - /** * Reads log files and queries any GraphHopper service *

    + * * @author Peter Karich */ -public class QueryTorture -{ - public static void main( String[] args ) - { - new QueryTorture().start(CmdArgs.read(args)); - } - +public class QueryTorture { private final Logger logger = LoggerFactory.getLogger(getClass()); private ExecutorService service; private BlockingQueue queryQueue; @@ -66,12 +55,14 @@ public static void main( String[] args ) private int statusUpdateCnt; private boolean logRequest = false; - public QueryTorture() - { + public QueryTorture() { + } + + public static void main(String[] args) { + new QueryTorture().start(CmdArgs.read(args)); } - public void start( CmdArgs cmdArgs ) - { + public void start(CmdArgs cmdArgs) { String logfile = cmdArgs.get("logfile", ""); int workers = cmdArgs.getInt("workers", 1); baseUrl = cmdArgs.get("baseurl", ""); @@ -82,8 +73,7 @@ public void start( CmdArgs cmdArgs ) if (Helper.isEmpty(baseUrl)) throw new IllegalArgumentException("baseUrl cannot be empty!?"); - if (!baseUrl.contains("?")) - { + if (!baseUrl.contains("?")) { if (!baseUrl.endsWith("/")) baseUrl += "/"; if (!baseUrl.endsWith("route/")) @@ -105,11 +95,9 @@ public void start( CmdArgs cmdArgs ) // start reading the logs and interrupt mainThread if no further entry available startReadingLogs(logfile); - try - { + try { mainThread.join(); - } catch (Exception ex) - { + } catch (Exception ex) { logger.info("End waiting", ex); } @@ -126,34 +114,25 @@ public void start( CmdArgs cmdArgs ) logger.info("mean query time in sec:" + sw.getSeconds() / successfullQueries.get()); } - Thread startWorkers( final int workers ) - { - Thread mainThread = new Thread("mainThread") - { + Thread startWorkers(final int workers) { + Thread mainThread = new Thread("mainThread") { @Override - public void run() - { + public void run() { Collection> workerCollection = new ArrayList>(workers); - for (int i = 0; i < workers; i++) - { + for (int i = 0; i < workers; i++) { final int workerNo = i; - workerCollection.add(new Callable() - { + workerCollection.add(new Callable() { @Override - public Object call() throws Exception - { + public Object call() throws Exception { workerStartedBarrier.countDown(); - try - { - while (!isInterrupted()) - { + try { + while (!isInterrupted()) { if (logfileEOFBarrier.getCount() == 0 && queryQueue.isEmpty()) break; execute(workerNo); } - } catch (Throwable ex) - { + } catch (Throwable ex) { logger.error(getName() + " - worker " + workerNo + " died", ex); } return null; @@ -161,16 +140,13 @@ public Object call() throws Exception }); } service = Executors.newFixedThreadPool(workers); - try - { + try { logger.info(getName() + " started with " + workers + " workers"); service.invokeAll(workerCollection); logger.info(getName() + " FINISHED"); - } catch (RejectedExecutionException ex) - { + } catch (RejectedExecutionException ex) { logger.info(getName() + " cannot create threads", ex); - } catch (InterruptedException ex) - { + } catch (InterruptedException ex) { // logger.info(getName() + " was interrupted", ex); } } @@ -179,11 +155,9 @@ public Object call() throws Exception return mainThread; } - void execute( int workerNo ) throws InterruptedException - { + void execute(int workerNo) throws InterruptedException { Query query = queryQueue.take(); - try - { + try { String url = baseUrl + query.createQueryString(); if (logRequest) logger.info(url); @@ -193,31 +167,24 @@ void execute( int workerNo ) throws InterruptedException else successfullQueries.incrementAndGet(); - if (successfullQueries.get() % statusUpdateCnt == 0) - { + if (successfullQueries.get() % statusUpdateCnt == 0) { logger.info("progress: " + (int) (successfullQueries.get() * 100 / maxQueries) + "%"); } - } catch (MalformedURLException ex) - { + } catch (MalformedURLException ex) { logger.error("Error while querying", ex); - } catch (IOException ex) - { + } catch (IOException ex) { // if (httpErrorCounter.get() % maxQueries == 0) // logger.error("http error", ex); httpErrorCounter.incrementAndGet(); } } - void startReadingLogs( final String logFile ) - { + void startReadingLogs(final String logFile) { final DistanceCalc distCalc = new DistanceCalcEarth(); - new Thread("readLogFile") - { + new Thread("readLogFile") { @Override - public void run() - { - try - { + public void run() { + try { InputStream is; if (logFile.endsWith(".gz")) is = new GZIPInputStream(new FileInputStream(logFile)); @@ -225,18 +192,15 @@ public void run() is = new FileInputStream(logFile); BufferedReader reader = new BufferedReader(new InputStreamReader(is, Helper.UTF_CS)); - try - { + try { String logLine; - while ((logLine = reader.readLine()) != null) - { + while ((logLine = reader.readLine()) != null) { Query q = Query.parse(logLine); if (q == null) continue; double dist = distCalc.calcDist(q.start.lat, q.start.lon, q.end.lat, q.end.lon); - if (dist < 100) - { + if (dist < 100) { skippedTooShort++; continue; } @@ -246,8 +210,7 @@ public void run() if (noDuplicate.add(q)) queryQueue.put(q); } - } finally - { + } finally { reader.close(); } @@ -259,8 +222,7 @@ public void run() logfileEOFBarrier.countDown(); // now wait for termination service.shutdown(); - } catch (Exception ex) - { + } catch (Exception ex) { logger.error("Stopped reading logs", ex); // do not wait, just shut down if (service != null) @@ -270,15 +232,13 @@ public void run() }.start(); } - static class Query - { + static class Query { GHPoint start; GHPoint end; List points = new ArrayList(); Map params = new HashMap(); - static Query parse( String logLine ) - { + static Query parse(String logLine) { String START = "GHBaseServlet - "; int index = logLine.indexOf(START); if (index < 0) @@ -292,16 +252,14 @@ static Query parse( String logLine ) Query q = new Query(); String queryString = logLine.substring(0, index); String[] tmpStrings = queryString.split("\\&"); - for (String paramStr : tmpStrings) - { + for (String paramStr : tmpStrings) { int equalIndex = paramStr.indexOf("="); if (equalIndex <= 0) continue; String key = paramStr.substring(0, equalIndex); String value = paramStr.substring(equalIndex + 1); - if (!paramStr.startsWith("point=")) - { + if (!paramStr.startsWith("point=")) { q.params.put(key, value); continue; } @@ -323,23 +281,27 @@ else if (q.end == null) return null; } - public void put( String key, String value ) - { + static String encodeURL(String str) { + try { + return URLEncoder.encode(str, "UTF-8"); + } catch (Exception _ignore) { + return str; + } + } + + public void put(String key, String value) { params.put(key, value); } - public String createQueryString() - { + public String createQueryString() { String qStr = ""; - for (String pointStr : points) - { + for (String pointStr : points) { if (!qStr.isEmpty()) qStr += "&"; qStr += "point=" + encodeURL(pointStr); } - for (Entry e : params.entrySet()) - { + for (Entry e : params.entrySet()) { if (!qStr.isEmpty()) qStr += "&"; @@ -349,20 +311,8 @@ public String createQueryString() return qStr; } - static String encodeURL( String str ) - { - try - { - return URLEncoder.encode(str, "UTF-8"); - } catch (Exception _ignore) - { - return str; - } - } - @Override - public String toString() - { + public String toString() { return createQueryString(); } } diff --git a/tools/src/main/java/com/graphhopper/ui/DebugAStar.java b/tools/src/main/java/com/graphhopper/ui/DebugAStar.java index 882370a159c..7f0c86ff8fc 100644 --- a/tools/src/main/java/com/graphhopper/ui/DebugAStar.java +++ b/tools/src/main/java/com/graphhopper/ui/DebugAStar.java @@ -21,38 +21,32 @@ import com.graphhopper.routing.util.FlagEncoder; import com.graphhopper.routing.util.TraversalMode; import com.graphhopper.routing.weighting.Weighting; -import com.graphhopper.storage.SPTEntry; import com.graphhopper.storage.Graph; +import com.graphhopper.storage.SPTEntry; import com.graphhopper.util.EdgeIteratorState; -import java.awt.Color; -import java.awt.Graphics2D; +import java.awt.*; /** * @author Peter Karich */ -public class DebugAStar extends AStar implements DebugAlgo -{ +public class DebugAStar extends AStar implements DebugAlgo { private GraphicsWrapper mg; private Graphics2D g2; - public DebugAStar( Graph graph, FlagEncoder encoder, Weighting type, TraversalMode tMode, GraphicsWrapper mg ) - { + public DebugAStar(Graph graph, FlagEncoder encoder, Weighting type, TraversalMode tMode, GraphicsWrapper mg) { super(graph, encoder, type, tMode); this.mg = mg; } @Override - public void setGraphics2D( Graphics2D g2 ) - { + public void setGraphics2D(Graphics2D g2) { this.g2 = g2; } @Override - public void updateBestPath( EdgeIteratorState es, SPTEntry bestEE, int currLoc ) - { - if (g2 != null) - { + public void updateBestPath(EdgeIteratorState es, SPTEntry bestEE, int currLoc) { + if (g2 != null) { mg.plotNode(g2, currLoc, Color.YELLOW); } super.updateBestPath(es, bestEE, currLoc); diff --git a/tools/src/main/java/com/graphhopper/ui/DebugAStarBi.java b/tools/src/main/java/com/graphhopper/ui/DebugAStarBi.java index dab518a46f4..e18a73e7042 100644 --- a/tools/src/main/java/com/graphhopper/ui/DebugAStarBi.java +++ b/tools/src/main/java/com/graphhopper/ui/DebugAStarBi.java @@ -21,38 +21,32 @@ import com.graphhopper.routing.util.FlagEncoder; import com.graphhopper.routing.util.TraversalMode; import com.graphhopper.routing.weighting.Weighting; -import com.graphhopper.storage.SPTEntry; import com.graphhopper.storage.Graph; +import com.graphhopper.storage.SPTEntry; import com.graphhopper.util.EdgeIteratorState; -import java.awt.Color; -import java.awt.Graphics2D; +import java.awt.*; /** * @author Peter Karich */ -public class DebugAStarBi extends AStarBidirection implements DebugAlgo -{ +public class DebugAStarBi extends AStarBidirection implements DebugAlgo { private GraphicsWrapper mg; private Graphics2D g2; - public DebugAStarBi( Graph graph, FlagEncoder encoder, Weighting type, TraversalMode tMode, GraphicsWrapper mg ) - { + public DebugAStarBi(Graph graph, FlagEncoder encoder, Weighting type, TraversalMode tMode, GraphicsWrapper mg) { super(graph, encoder, type, tMode); this.mg = mg; } @Override - public void setGraphics2D( Graphics2D g2 ) - { + public void setGraphics2D(Graphics2D g2) { this.g2 = g2; } @Override - public void updateBestPath( EdgeIteratorState es, SPTEntry bestEE, int currLoc ) - { - if (g2 != null) - { + public void updateBestPath(EdgeIteratorState es, SPTEntry bestEE, int currLoc) { + if (g2 != null) { mg.plotNode(g2, currLoc, Color.YELLOW); } super.updateBestPath(es, bestEE, currLoc); diff --git a/tools/src/main/java/com/graphhopper/ui/DebugAlgo.java b/tools/src/main/java/com/graphhopper/ui/DebugAlgo.java index 5964ddb251d..7e6a39d0f12 100644 --- a/tools/src/main/java/com/graphhopper/ui/DebugAlgo.java +++ b/tools/src/main/java/com/graphhopper/ui/DebugAlgo.java @@ -17,12 +17,11 @@ */ package com.graphhopper.ui; -import java.awt.Graphics2D; +import java.awt.*; /** * @author Peter Karich */ -public interface DebugAlgo -{ - void setGraphics2D( Graphics2D g2 ); +public interface DebugAlgo { + void setGraphics2D(Graphics2D g2); } diff --git a/tools/src/main/java/com/graphhopper/ui/DebugDijkstraBidirection.java b/tools/src/main/java/com/graphhopper/ui/DebugDijkstraBidirection.java index be46a29776b..7b2310503fd 100644 --- a/tools/src/main/java/com/graphhopper/ui/DebugDijkstraBidirection.java +++ b/tools/src/main/java/com/graphhopper/ui/DebugDijkstraBidirection.java @@ -21,38 +21,32 @@ import com.graphhopper.routing.util.FlagEncoder; import com.graphhopper.routing.util.TraversalMode; import com.graphhopper.routing.weighting.Weighting; -import com.graphhopper.storage.SPTEntry; import com.graphhopper.storage.Graph; +import com.graphhopper.storage.SPTEntry; import com.graphhopper.util.EdgeIteratorState; -import java.awt.Color; -import java.awt.Graphics2D; +import java.awt.*; /** * @author Peter Karich */ -public class DebugDijkstraBidirection extends DijkstraBidirectionRef implements DebugAlgo -{ +public class DebugDijkstraBidirection extends DijkstraBidirectionRef implements DebugAlgo { private GraphicsWrapper mg; private Graphics2D g2; - public DebugDijkstraBidirection( Graph graph, FlagEncoder encoder, Weighting type, TraversalMode tMode, GraphicsWrapper mg ) - { + public DebugDijkstraBidirection(Graph graph, FlagEncoder encoder, Weighting type, TraversalMode tMode, GraphicsWrapper mg) { super(graph, encoder, type, tMode); this.mg = mg; } @Override - public void setGraphics2D( Graphics2D g2 ) - { + public void setGraphics2D(Graphics2D g2) { this.g2 = g2; } @Override - public void updateBestPath( EdgeIteratorState es, SPTEntry bestEE, int currLoc ) - { - if (g2 != null) - { + public void updateBestPath(EdgeIteratorState es, SPTEntry bestEE, int currLoc) { + if (g2 != null) { mg.plotNode(g2, currLoc, Color.BLUE); } // System.out.println("new node:" + currLoc); diff --git a/tools/src/main/java/com/graphhopper/ui/DebugDijkstraSimple.java b/tools/src/main/java/com/graphhopper/ui/DebugDijkstraSimple.java index a9dc83218db..d9f4f8c8b94 100644 --- a/tools/src/main/java/com/graphhopper/ui/DebugDijkstraSimple.java +++ b/tools/src/main/java/com/graphhopper/ui/DebugDijkstraSimple.java @@ -25,34 +25,28 @@ import com.graphhopper.storage.SPTEntry; import com.graphhopper.util.EdgeIteratorState; -import java.awt.Color; -import java.awt.Graphics2D; +import java.awt.*; /** * @author Peter Karich */ -public class DebugDijkstraSimple extends Dijkstra implements DebugAlgo -{ +public class DebugDijkstraSimple extends Dijkstra implements DebugAlgo { private GraphicsWrapper mg; private Graphics2D g2; - public DebugDijkstraSimple( Graph graph, FlagEncoder encoder, Weighting weighting, TraversalMode tMode, GraphicsWrapper mg ) - { + public DebugDijkstraSimple(Graph graph, FlagEncoder encoder, Weighting weighting, TraversalMode tMode, GraphicsWrapper mg) { super(graph, encoder, weighting, tMode); this.mg = mg; } @Override - public void setGraphics2D( Graphics2D g2 ) - { + public void setGraphics2D(Graphics2D g2) { this.g2 = g2; } @Override - public void updateBestPath( EdgeIteratorState es, SPTEntry bestEE, int currLoc ) - { - if (g2 != null) - { + public void updateBestPath(EdgeIteratorState es, SPTEntry bestEE, int currLoc) { + if (g2 != null) { mg.plotNode(g2, currLoc, Color.YELLOW); } super.updateBestPath(es, bestEE, currLoc); diff --git a/tools/src/main/java/com/graphhopper/ui/DefaultMapLayer.java b/tools/src/main/java/com/graphhopper/ui/DefaultMapLayer.java index 7b97fe2a22b..fb496a5268a 100644 --- a/tools/src/main/java/com/graphhopper/ui/DefaultMapLayer.java +++ b/tools/src/main/java/com/graphhopper/ui/DefaultMapLayer.java @@ -17,108 +17,89 @@ */ package com.graphhopper.ui; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import java.awt.*; import java.awt.image.BufferedImage; import java.awt.image.RescaleOp; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - /** * @author Peter Karich */ -public abstract class DefaultMapLayer implements MapLayer -{ +public abstract class DefaultMapLayer implements MapLayer { + protected BufferedImage image; private Logger logger = LoggerFactory.getLogger(getClass()); private Rectangle bounds = new Rectangle(); private Graphics2D tmpG; - protected BufferedImage image; private boolean buffering = true; // a bit transparent: // private RescaleOp op = new RescaleOp(new float[]{1f, 1f, 1f, 0.5f}, new float[4], null); - private RescaleOp op = new RescaleOp(new float[] - { - 1f, 1f, 1f, 1f - }, new float[4], null); + private RescaleOp op = new RescaleOp(new float[]{1f, 1f, 1f, 1f}, new float[4], null); - protected abstract void paintComponent( Graphics2D createGraphics ); + protected abstract void paintComponent(Graphics2D createGraphics); @Override - public void paint( Graphics2D mainGraphics ) - { - if (!buffering) - { - try - { + public void paint(Graphics2D mainGraphics) { + if (!buffering) { + try { paintComponent(mainGraphics); - } catch (Exception ex) - { + } catch (Exception ex) { logger.error("Problem in paintComponent", ex); } return; } - if (image != null) - { + if (image != null) { mainGraphics.drawImage(image, op, bounds.x, bounds.y); } } @Override - public void setBuffering( boolean enable ) - { + public void setBuffering(boolean enable) { buffering = enable; } @Override - public final void repaint() - { - if (tmpG != null) - { + public final void repaint() { + if (tmpG != null) { paintComponent(tmpG); } } @Override - public Rectangle getBounds() - { + public Rectangle getBounds() { return bounds; } - public void makeTransparent( Graphics2D g2 ) - { + @Override + public void setBounds(Rectangle bounds) { + if (image == null || image.getHeight() != bounds.height || image.getWidth() != bounds.width) { + image = new BufferedImage(bounds.width, bounds.height, BufferedImage.TYPE_INT_ARGB); + tmpG = image.createGraphics(); + tmpG.setColor(Color.BLACK); + tmpG.setBackground(Color.WHITE); + } + this.bounds = bounds; + repaint(); + } + + public void makeTransparent(Graphics2D g2) { Color col = g2.getColor(); Composite comp = null; // force transparence of this layer only. If no buffering we would clear layers below - if (buffering) - { + if (buffering) { comp = g2.getComposite(); g2.setComposite(AlphaComposite.Clear); } g2.setColor(new Color(0, 0, 0, 0)); g2.fillRect(0, 0, bounds.width, bounds.height); g2.setColor(col); - if (comp != null) - { + if (comp != null) { g2.setComposite(comp); } } - public void clearGraphics( Graphics2D g2 ) - { + public void clearGraphics(Graphics2D g2) { g2.clearRect(0, 0, bounds.width, bounds.height); } - - @Override - public void setBounds( Rectangle bounds ) - { - if (image == null || image.getHeight() != bounds.height || image.getWidth() != bounds.width) - { - image = new BufferedImage(bounds.width, bounds.height, BufferedImage.TYPE_INT_ARGB); - tmpG = image.createGraphics(); - tmpG.setColor(Color.BLACK); - tmpG.setBackground(Color.WHITE); - } - this.bounds = bounds; - repaint(); - } } diff --git a/tools/src/main/java/com/graphhopper/ui/GraphicsWrapper.java b/tools/src/main/java/com/graphhopper/ui/GraphicsWrapper.java index c4dc898f207..ad2a1b7f45e 100644 --- a/tools/src/main/java/com/graphhopper/ui/GraphicsWrapper.java +++ b/tools/src/main/java/com/graphhopper/ui/GraphicsWrapper.java @@ -20,19 +20,15 @@ import com.graphhopper.storage.Graph; import com.graphhopper.storage.NodeAccess; import com.graphhopper.util.shapes.BBox; - -import java.awt.BasicStroke; -import java.awt.Color; -import java.awt.Graphics2D; - import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.awt.*; + /** * @author Peter Karich */ -public class GraphicsWrapper -{ +public class GraphicsWrapper { private final Logger logger = LoggerFactory.getLogger(getClass()); private final NodeAccess na; private double scaleX; @@ -41,8 +37,7 @@ public class GraphicsWrapper private double offsetY; private BBox bounds = new BBox(-180, 180, -90, 90); - public GraphicsWrapper( Graph g ) - { + public GraphicsWrapper(Graph g) { this.na = g.getNodeAccess(); BBox b = g.getBounds(); scaleX = scaleY = 0.002 * (b.maxLat - b.minLat); @@ -50,68 +45,55 @@ public GraphicsWrapper( Graph g ) offsetX = -b.minLon; } - public double getOffsetX() - { + public double getOffsetX() { return offsetX; } - public double getOffsetY() - { + public double getOffsetY() { return offsetY; } - public double getScaleX() - { + public double getScaleX() { return scaleX; } - public double getScaleY() - { + public double getScaleY() { return scaleY; } - public void plotText( Graphics2D g2, double lat, double lon, String text ) - { + public void plotText(Graphics2D g2, double lat, double lon, String text) { g2.drawString(text, (int) getX(lon) + 5, (int) getY(lat) + 5); } - public void plotEdge( Graphics2D g2, double lat, double lon, double lat2, double lon2, float width ) - { + public void plotEdge(Graphics2D g2, double lat, double lon, double lat2, double lon2, float width) { g2.setStroke(new BasicStroke(width)); g2.drawLine((int) getX(lon), (int) getY(lat), (int) getX(lon2), (int) getY(lat2)); } - public void plotEdge( Graphics2D g2, double lat, double lon, double lat2, double lon2 ) - { + public void plotEdge(Graphics2D g2, double lat, double lon, double lat2, double lon2) { plotEdge(g2, lat, lon, lat2, lon2, 1); } - public double getX( double lon ) - { + public double getX(double lon) { return (lon + offsetX) / scaleX; } - public double getY( double lat ) - { + public double getY(double lat) { return (90 - lat + offsetY) / scaleY; } - public double getLon( int x ) - { + public double getLon(int x) { return x * scaleX - offsetX; } - public double getLat( int y ) - { + public double getLat(int y) { return 90 - (y * scaleY - offsetY); } - public void plotNode( Graphics2D g2, int loc, Color c ) - { + public void plotNode(Graphics2D g2, int loc, Color c) { double lat = na.getLatitude(loc); double lon = na.getLongitude(loc); - if (lat < bounds.minLat || lat > bounds.maxLat || lon < bounds.minLon || lon > bounds.maxLon) - { + if (lat < bounds.minLat || lat > bounds.maxLat || lon < bounds.minLon || lon > bounds.maxLon) { return; } @@ -121,43 +103,36 @@ public void plotNode( Graphics2D g2, int loc, Color c ) g2.setColor(old); } - public void plot( Graphics2D g2, double lat, double lon, int width ) - { + public void plot(Graphics2D g2, double lat, double lon, int width) { double x = getX(lon); double y = getY(lat); g2.fillOval((int) x, (int) y, width, width); } - void scale( int x, int y, boolean zoomIn ) - { + void scale(int x, int y, boolean zoomIn) { double tmpFactor = 0.5f; - if (!zoomIn) - { + if (!zoomIn) { tmpFactor = 2; } double oldScaleX = scaleX; double oldScaleY = scaleY; double resX = scaleX * tmpFactor; - if (resX > 0) - { + if (resX > 0) { scaleX = resX; } double resY = scaleY * tmpFactor; - if (resY > 0) - { + if (resY > 0) { scaleY = resY; } // respect mouse x,y when scaling // TODO minor bug: compute difference of lat,lon position for mouse before and after scaling - if (zoomIn) - { + if (zoomIn) { offsetX -= (offsetX + x) * scaleX; offsetY -= (offsetY + y) * scaleY; - } else - { + } else { offsetX += x * oldScaleX; offsetY += y * oldScaleY; } @@ -166,14 +141,12 @@ void scale( int x, int y, boolean zoomIn ) + " " + scaleX + "," + scaleY); } - public void setNewOffset( int offX, int offY ) - { + public void setNewOffset(int offX, int offY) { offsetX += offX * scaleX; offsetY += offY * scaleY; } - public BBox setBounds( int minX, int maxX, int minY, int maxY ) - { + public BBox setBounds(int minX, int maxX, int minY, int maxY) { double minLon = getLon(minX); double maxLon = getLon(maxX); diff --git a/tools/src/main/java/com/graphhopper/ui/LayeredPanel.java b/tools/src/main/java/com/graphhopper/ui/LayeredPanel.java index c19c871187d..f1a0cb55376 100644 --- a/tools/src/main/java/com/graphhopper/ui/LayeredPanel.java +++ b/tools/src/main/java/com/graphhopper/ui/LayeredPanel.java @@ -17,38 +17,32 @@ */ package com.graphhopper.ui; +import javax.swing.*; import java.awt.*; -import java.util.Collection; import java.awt.event.ComponentAdapter; import java.awt.event.ComponentEvent; import java.util.ArrayList; -import javax.swing.JPanel; +import java.util.Collection; /** * @author Peter Karich */ -public class LayeredPanel extends JPanel -{ +public class LayeredPanel extends JPanel { private final Collection layers; - public LayeredPanel() - { + public LayeredPanel() { this(new ArrayList()); } - public LayeredPanel( Collection layer ) - { + public LayeredPanel(Collection layer) { this.layers = layer; - this.addComponentListener(new ComponentAdapter() - { + this.addComponentListener(new ComponentAdapter() { @Override - public void componentResized( ComponentEvent e ) - { + public void componentResized(ComponentEvent e) { int w = e.getComponent().getWidth(); int h = e.getComponent().getHeight(); System.out.println("mainResized:" + w + " " + h); - for (MapLayer ml : layers) - { + for (MapLayer ml : layers) { ml.setBounds(new Rectangle(0, 0, w, h)); } repaint(); @@ -56,22 +50,18 @@ public void componentResized( ComponentEvent e ) }); } - public void setBuffering( boolean enable ) - { - for (MapLayer ml : layers) - { + public void setBuffering(boolean enable) { + for (MapLayer ml : layers) { ml.setBuffering(enable); } } - public void addLayer( MapLayer ml ) - { + public void addLayer(MapLayer ml) { layers.add(ml); } @Override - protected void paintComponent( Graphics g ) - { + protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D) g; // StopWatch sw = new StopWatch(); @@ -81,8 +71,7 @@ protected void paintComponent( Graphics g ) // g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF); // g2.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_SPEED); // int counter = 0; - for (MapLayer ml : layers) - { + for (MapLayer ml : layers) { // sw.start(); ml.paint(g2); // System.out.println(++counter + " | mainRepaint took " + sw.stop().getSeconds() + " sec"); diff --git a/tools/src/main/java/com/graphhopper/ui/MapLayer.java b/tools/src/main/java/com/graphhopper/ui/MapLayer.java index 67f752eca1a..2d609c78959 100644 --- a/tools/src/main/java/com/graphhopper/ui/MapLayer.java +++ b/tools/src/main/java/com/graphhopper/ui/MapLayer.java @@ -17,22 +17,20 @@ */ package com.graphhopper.ui; -import java.awt.Graphics2D; -import java.awt.Rectangle; +import java.awt.*; import java.io.Serializable; /** * @author Peter Karich */ -public interface MapLayer extends Serializable -{ - void setBounds( Rectangle r ); - +public interface MapLayer extends Serializable { Rectangle getBounds(); - void setBuffering( boolean enable ); + void setBounds(Rectangle r); + + void setBuffering(boolean enable); void repaint(); - void paint( Graphics2D g ); + void paint(Graphics2D g); } diff --git a/tools/src/main/java/com/graphhopper/ui/MiniGraphUI.java b/tools/src/main/java/com/graphhopper/ui/MiniGraphUI.java index 10770cea640..187d92f3c48 100644 --- a/tools/src/main/java/com/graphhopper/ui/MiniGraphUI.java +++ b/tools/src/main/java/com/graphhopper/ui/MiniGraphUI.java @@ -17,12 +17,14 @@ */ package com.graphhopper.ui; -import com.graphhopper.routing.weighting.Weighting; import com.graphhopper.GraphHopper; import com.graphhopper.coll.GHBitSet; import com.graphhopper.coll.GHTBitSet; import com.graphhopper.routing.*; -import com.graphhopper.routing.util.*; +import com.graphhopper.routing.util.EdgeFilter; +import com.graphhopper.routing.util.FlagEncoder; +import com.graphhopper.routing.util.HintsMap; +import com.graphhopper.routing.weighting.Weighting; import com.graphhopper.storage.Graph; import com.graphhopper.storage.NodeAccess; import com.graphhopper.storage.index.LocationIndexTree; @@ -31,14 +33,16 @@ import com.graphhopper.util.Parameters.Algorithms; import com.graphhopper.util.shapes.BBox; import gnu.trove.list.TIntList; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import javax.swing.*; import java.awt.*; -import java.awt.event.*; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; +import java.awt.event.MouseWheelEvent; +import java.awt.event.MouseWheelListener; import java.util.Random; -import javax.swing.*; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; /** * A rough graphical user interface for visualizing the OSM graph. Mainly for debugging algorithms @@ -47,37 +51,33 @@ *

    * Use the web module for a better/faster/userfriendly/... alternative! *

    + * * @author Peter Karich */ -public class MiniGraphUI -{ - public static void main( String[] strs ) throws Exception - { - CmdArgs args = CmdArgs.read(strs); - GraphHopper hopper = new GraphHopper().init(args).importOrLoad(); - boolean debug = args.getBool("minigraphui.debug", false); - new MiniGraphUI(hopper, debug).visualize(); - } - +public class MiniGraphUI { + private final Graph graph; + private final NodeAccess na; + private final MapLayer pathLayer; + private final Weighting weighting; + private final FlagEncoder encoder; + // for moving + int currentPosX; + int currentPosY; private Logger logger = LoggerFactory.getLogger(getClass()); private Path path; private RoutingAlgorithmFactory algoFactory; - private final Graph graph; - private final NodeAccess na; private LocationIndexTree index; private String latLon = ""; private GraphicsWrapper mg; private JPanel infoPanel; private LayeredPanel mainPanel; private MapLayer roadsLayer; - private final MapLayer pathLayer; private boolean fastPaint = false; - private final Weighting weighting; - private final FlagEncoder encoder; private AlgorithmOptions algoOpts; + private QueryResult fromRes; + private QueryResult toRes; - public MiniGraphUI( GraphHopper hopper, boolean debug ) - { + public MiniGraphUI(GraphHopper hopper, boolean debug) { this.graph = hopper.getGraphHopperStorage(); this.na = graph.getNodeAccess(); encoder = hopper.getEncodingManager().getEncoder("car"); @@ -98,11 +98,9 @@ public MiniGraphUI( GraphHopper hopper, boolean debug ) // this.algo = new AStar(graph); // this.algo = new DijkstraSimple(graph); // this.algo = new DebugDijkstraSimple(graph, mg); - infoPanel = new JPanel() - { + infoPanel = new JPanel() { @Override - protected void paintComponent( Graphics g ) - { + protected void paintComponent(Graphics g) { g.setColor(Color.WHITE); Rectangle b = infoPanel.getBounds(); g.fillRect(0, 0, b.width, b.height); @@ -120,19 +118,16 @@ protected void paintComponent( Graphics g ) // TODO make it correct with bitset-skipping too final GHBitSet bitset = new GHTBitSet(graph.getNodes()); - mainPanel.addLayer(roadsLayer = new DefaultMapLayer() - { + mainPanel.addLayer(roadsLayer = new DefaultMapLayer() { Random rand = new Random(); @Override - public void paintComponent( Graphics2D g2 ) - { + public void paintComponent(Graphics2D g2) { clearGraphics(g2); int locs = graph.getNodes(); Rectangle d = getBounds(); BBox b = mg.setBounds(0, d.width, 0, d.height); - if (fastPaint) - { + if (fastPaint) { rand.setSeed(0); bitset.clear(); } @@ -154,8 +149,7 @@ public void paintComponent( Graphics2D g2 ) EdgeExplorer explorer = graph.createEdgeExplorer(EdgeFilter.ALL_EDGES); Color[] speedColors = generateColors(15); - for (int nodeIndex = 0; nodeIndex < locs; nodeIndex++) - { + for (int nodeIndex = 0; nodeIndex < locs; nodeIndex++) { if (fastPaint && rand.nextInt(30) > 1) continue; double lat = na.getLatitude(nodeIndex); @@ -166,12 +160,10 @@ public void paintComponent( Graphics2D g2 ) continue; EdgeIterator edge = explorer.setBaseNode(nodeIndex); - while (edge.next()) - { + while (edge.next()) { int nodeId = edge.getAdjNode(); int sum = nodeIndex + nodeId; - if (fastPaint) - { + if (fastPaint) { if (bitset.contains(sum)) continue; @@ -184,30 +176,22 @@ public void paintComponent( Graphics2D g2 ) //mg.plotText(g2, lat * 0.9 + lat2 * 0.1, lon * 0.9 + lon2 * 0.1, "s:" + (int) encoder.getSpeed(iter.getFlags())); double speed = encoder.getSpeed(edge.getFlags()); Color color; - if (speed >= 120) - { + if (speed >= 120) { // red color = speedColors[12]; - } else if (speed >= 100) - { + } else if (speed >= 100) { color = speedColors[10]; - } else if (speed >= 80) - { + } else if (speed >= 80) { color = speedColors[8]; - } else if (speed >= 60) - { + } else if (speed >= 60) { color = speedColors[6]; - } else if (speed >= 50) - { + } else if (speed >= 50) { color = speedColors[5]; - } else if (speed >= 40) - { + } else if (speed >= 40) { color = speedColors[4]; - } else if (speed >= 30) - { + } else if (speed >= 30) { color = Color.GRAY; - } else - { + } else { color = Color.LIGHT_GRAY; } @@ -218,8 +202,7 @@ public void paintComponent( Graphics2D g2 ) g2.setColor(Color.WHITE); g2.fillRect(0, 0, 1000, 20); - for (int i = 4; i < speedColors.length; i++) - { + for (int i = 4; i < speedColors.length; i++) { g2.setColor(speedColors[i]); g2.drawString("" + (i * 10), i * 30 - 100, 10); } @@ -228,19 +211,16 @@ public void paintComponent( Graphics2D g2 ) } }); - mainPanel.addLayer(pathLayer = new DefaultMapLayer() - { + mainPanel.addLayer(pathLayer = new DefaultMapLayer() { @Override - public void paintComponent( Graphics2D g2 ) - { + public void paintComponent(Graphics2D g2) { if (fromRes == null || toRes == null) return; makeTransparent(g2); QueryGraph qGraph = new QueryGraph(graph).lookup(fromRes, toRes); RoutingAlgorithm algo = algoFactory.createAlgo(qGraph, algoOpts); - if (algo instanceof DebugAlgo) - { + if (algo instanceof DebugAlgo) { ((DebugAlgo) algo).setGraphics2D(g2); } @@ -265,8 +245,7 @@ public void paintComponent( Graphics2D g2 ) sw.stop(); // if directed edges - if (!path.isFound()) - { + if (!path.isFound()) { logger.warn("path not found! direction not valid?"); return; } @@ -278,8 +257,7 @@ public void paintComponent( Graphics2D g2 ) } }); - if (debug) - { + if (debug) { // disable double buffering for debugging drawing - nice! when do we need DebugGraphics then? RepaintManager repaintManager = RepaintManager.currentManager(mainPanel); repaintManager.setDoubleBufferingEnabled(false); @@ -287,19 +265,23 @@ public void paintComponent( Graphics2D g2 ) } } - public Color[] generateColors( int n ) - { + public static void main(String[] strs) throws Exception { + CmdArgs args = CmdArgs.read(strs); + GraphHopper hopper = new GraphHopper().init(args).importOrLoad(); + boolean debug = args.getBool("minigraphui.debug", false); + new MiniGraphUI(hopper, debug).visualize(); + } + + public Color[] generateColors(int n) { Color[] cols = new Color[n]; - for (int i = 0; i < n; i++) - { + for (int i = 0; i < n; i++) { cols[i] = Color.getHSBColor((float) i / (float) n, 0.85f, 1.0f); } return cols; } // for debugging - private Path calcPath( RoutingAlgorithm algo ) - { + private Path calcPath(RoutingAlgorithm algo) { // int from = index.findID(50.042, 10.19); // int to = index.findID(50.049, 10.23); // @@ -311,17 +293,14 @@ private Path calcPath( RoutingAlgorithm algo ) return algo.calcPath(162810, 35120); } - void plotNodeName( Graphics2D g2, int node ) - { + void plotNodeName(Graphics2D g2, int node) { double lat = na.getLatitude(node); double lon = na.getLongitude(node); mg.plotText(g2, lat, lon, "" + node); } - private Path plotPath( Path tmpPath, Graphics2D g2, int w ) - { - if (!tmpPath.isFound()) - { + private Path plotPath(Path tmpPath, Graphics2D g2, int w) { + if (!tmpPath.isFound()) { logger.info("nothing found " + w); return tmpPath; } @@ -330,23 +309,18 @@ private Path plotPath( Path tmpPath, Graphics2D g2, int w ) double prevLon = Double.NaN; boolean plotNodes = false; TIntList nodes = tmpPath.calcNodes(); - if (plotNodes) - { - for (int i = 0; i < nodes.size(); i++) - { + if (plotNodes) { + for (int i = 0; i < nodes.size(); i++) { plotNodeName(g2, nodes.get(i)); } } PointList list = tmpPath.calcPoints(); - for (int i = 0; i < list.getSize(); i++) - { + for (int i = 0; i < list.getSize(); i++) { double lat = list.getLatitude(i); double lon = list.getLongitude(i); - if (!Double.isNaN(prevLat)) - { + if (!Double.isNaN(prevLat)) { mg.plotEdge(g2, prevLat, prevLon, lat, lon, w); - } else - { + } else { mg.plot(g2, lat, lon, w); } prevLat = lat; @@ -356,18 +330,11 @@ private Path plotPath( Path tmpPath, Graphics2D g2, int w ) return tmpPath; } - private QueryResult fromRes; - private QueryResult toRes; - - public void visualize() - { - try - { - SwingUtilities.invokeAndWait(new Runnable() - { + public void visualize() { + try { + SwingUtilities.invokeAndWait(new Runnable() { @Override - public void run() - { + public void run() { int frameHeight = 800; int frameWidth = 1200; JFrame frame = new JFrame("GraphHopper UI - Small&Ugly ;)"); @@ -378,11 +345,9 @@ public void run() infoPanel.setPreferredSize(new Dimension(300, 100)); // scale - mainPanel.addMouseWheelListener(new MouseWheelListener() - { + mainPanel.addMouseWheelListener(new MouseWheelListener() { @Override - public void mouseWheelMoved( MouseWheelEvent e ) - { + public void mouseWheelMoved(MouseWheelEvent e) { mg.scale(e.getX(), e.getY(), e.getWheelRotation() < 0); repaintRoads(); } @@ -406,21 +371,18 @@ public void mouseWheelMoved( MouseWheelEvent e ) // updateLatLon(e); // } // }; - MouseAdapter ml = new MouseAdapter() - { + MouseAdapter ml = new MouseAdapter() { // for routing: double fromLat, fromLon; boolean fromDone = false; + boolean dragging = false; @Override - public void mouseClicked( MouseEvent e ) - { - if (!fromDone) - { + public void mouseClicked(MouseEvent e) { + if (!fromDone) { fromLat = mg.getLat(e.getY()); fromLon = mg.getLon(e.getX()); - } else - { + } else { double toLat = mg.getLat(e.getY()); double toLon = mg.getLon(e.getX()); StopWatch sw = new StopWatch().start(); @@ -437,11 +399,8 @@ public void mouseClicked( MouseEvent e ) fromDone = !fromDone; } - boolean dragging = false; - @Override - public void mouseDragged( MouseEvent e ) - { + public void mouseDragged(MouseEvent e) { dragging = true; fastPaint = true; update(e); @@ -449,10 +408,8 @@ public void mouseDragged( MouseEvent e ) } @Override - public void mouseReleased( MouseEvent e ) - { - if (dragging) - { + public void mouseReleased(MouseEvent e) { + if (dragging) { // update only if mouse release comes from dragging! (at the moment equal to fastPaint) dragging = false; fastPaint = false; @@ -460,21 +417,18 @@ public void mouseReleased( MouseEvent e ) } } - public void update( MouseEvent e ) - { + public void update(MouseEvent e) { mg.setNewOffset(e.getX() - currentPosX, e.getY() - currentPosY); repaintRoads(); } @Override - public void mouseMoved( MouseEvent e ) - { + public void mouseMoved(MouseEvent e) { updateLatLon(e); } @Override - public void mousePressed( MouseEvent e ) - { + public void mousePressed(MouseEvent e) { updateLatLon(e); } }; @@ -502,32 +456,24 @@ public void mousePressed( MouseEvent e ) frame.setVisible(true); } }); - } catch (Exception ex) - { + } catch (Exception ex) { throw new RuntimeException(ex); } } - // for moving - int currentPosX; - int currentPosY; - - void updateLatLon( MouseEvent e ) - { + void updateLatLon(MouseEvent e) { latLon = mg.getLat(e.getY()) + "," + mg.getLon(e.getX()); infoPanel.repaint(); currentPosX = e.getX(); currentPosY = e.getY(); } - void repaintPaths() - { + void repaintPaths() { pathLayer.repaint(); mainPanel.repaint(); } - void repaintRoads() - { + void repaintRoads() { // avoid threading as there should be no updated to scale or offset while painting // (would to lead to artifacts) StopWatch sw = new StopWatch().start(); diff --git a/tools/src/test/java/com/graphhopper/tools/QueryTortureTest.java b/tools/src/test/java/com/graphhopper/tools/QueryTortureTest.java index 473be957fbd..e0da9f5232c 100644 --- a/tools/src/test/java/com/graphhopper/tools/QueryTortureTest.java +++ b/tools/src/test/java/com/graphhopper/tools/QueryTortureTest.java @@ -20,16 +20,14 @@ import com.graphhopper.tools.QueryTorture.Query; import org.junit.Test; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; /** * @author Peter Karich */ -public class QueryTortureTest -{ +public class QueryTortureTest { @Test - public void testGetQuery() - { + public void testGetQuery() { Query result = Query.parse("2016-05-04 16:37:37,647 [qtp1604002113-823] INFO com.graphhopper.http.GHBaseServlet - point=46.444481%2C11.306992&point=46.07847%2C11.178589&locale=en_US&vehicle=car&weighting=fastest&elevation=true 127.0.0.1 en_US Directions API 195.232.147.121 [46.456721,11.258966, 46.15583,11.153478, 46.067933,11.223352, 46.456721,11.258966], took:0.008627106, , fastest, car, alternatives: 1, distance0: 146967.68084669442, time0: 112min, points0: 1507, debugInfo: idLookup:0.004824006s; , algoInit:2.6879E-5s, dijkstrabiCH-routing:5.69366E-4s, extract time:9.987E-5;, algoInit:1.6976E-5s, dijkstrabiCH-routing:3.22521E-4s, extract time:5.9076E-5;, algoInit:1.6084E-5s, dijkstrabiCH-routing:6.75566E-4s, extract time:8.2527E-5;, algoInit:2.6879E-5s, dijkstrabiCH-routing:5.69366E-4s, extract time:9.987E-5;, algoInit:1.6976E-5s, dijkstrabiCH-routing:3.22521E-4s, extract time:5.9076E-5;, algoInit:1.6084E-5s, dijkstrabiCH-routing:6.75566E-4s, extract time:8.2527E-5, simplify (1903->1507)"); assertEquals("point=46.444481%2C11.306992&point=46.07847%2C11.178589&elevation=true&locale=en_US&weighting=fastest&vehicle=car", result.createQueryString()); assertEquals(46.444481, result.start.lat, 1e-5); diff --git a/web/src/main/java/com/graphhopper/http/CORSFilter.java b/web/src/main/java/com/graphhopper/http/CORSFilter.java index 3588d5c0214..e85258009bd 100644 --- a/web/src/main/java/com/graphhopper/http/CORSFilter.java +++ b/web/src/main/java/com/graphhopper/http/CORSFilter.java @@ -17,20 +17,17 @@ */ package com.graphhopper.http; -import java.io.IOException; import javax.servlet.*; import javax.servlet.http.HttpServletResponse; +import java.io.IOException; /** * @author Peter Karich */ -public class CORSFilter implements Filter -{ +public class CORSFilter implements Filter { @Override - public void doFilter( ServletRequest request, ServletResponse response, FilterChain chain ) throws IOException, ServletException - { - if (!"jsonp".equals(request.getParameter("type"))) - { + public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { + if (!"jsonp".equals(request.getParameter("type"))) { HttpServletResponse rsp = (HttpServletResponse) response; rsp.setHeader("Access-Control-Allow-Methods", "GET, POST, HEAD, OPTIONS"); rsp.setHeader("Access-Control-Allow-Headers", "Origin,Accept,X-Requested-With," @@ -42,12 +39,10 @@ public void doFilter( ServletRequest request, ServletResponse response, FilterCh } @Override - public void init( FilterConfig filterConfig ) throws ServletException - { + public void init(FilterConfig filterConfig) throws ServletException { } @Override - public void destroy() - { + public void destroy() { } } diff --git a/web/src/main/java/com/graphhopper/http/DefaultModule.java b/web/src/main/java/com/graphhopper/http/DefaultModule.java index 5f75b2a642d..6d901eed9b8 100644 --- a/web/src/main/java/com/graphhopper/http/DefaultModule.java +++ b/web/src/main/java/com/graphhopper/http/DefaultModule.java @@ -29,19 +29,16 @@ /** * @author Peter Karich */ -public class DefaultModule extends AbstractModule -{ - private final Logger logger = LoggerFactory.getLogger(getClass()); +public class DefaultModule extends AbstractModule { protected final CmdArgs args; + private final Logger logger = LoggerFactory.getLogger(getClass()); private GraphHopper graphHopper; - public DefaultModule( CmdArgs args ) - { + public DefaultModule(CmdArgs args) { this.args = CmdArgs.readFromConfigAndMerge(args, "config", "graphhopper.config"); } - public GraphHopper getGraphHopper() - { + public GraphHopper getGraphHopper() { if (graphHopper == null) throw new IllegalStateException("createGraphHopper not called"); @@ -51,8 +48,7 @@ public GraphHopper getGraphHopper() /** * @return an initialized GraphHopper instance */ - protected GraphHopper createGraphHopper( CmdArgs args ) - { + protected GraphHopper createGraphHopper(CmdArgs args) { GraphHopper tmp = new GraphHopperOSM().forServer().init(args); tmp.importOrLoad(); logger.info("loaded graph at:" + tmp.getGraphHopperLocation() @@ -63,10 +59,8 @@ protected GraphHopper createGraphHopper( CmdArgs args ) } @Override - protected void configure() - { - try - { + protected void configure() { + try { graphHopper = createGraphHopper(args); bind(GraphHopper.class).toInstance(graphHopper); bind(TranslationMap.class).toInstance(graphHopper.getTranslationMap()); @@ -80,8 +74,7 @@ protected void configure() bind(Boolean.class).annotatedWith(Names.named("jsonp_allowed")).toInstance(jsonpAllowed); bind(RouteSerializer.class).toInstance(new SimpleRouteSerializer(graphHopper.getGraphHopperStorage().getBounds())); - } catch (Exception ex) - { + } catch (Exception ex) { throw new IllegalStateException("Couldn't load graph", ex); } } diff --git a/web/src/main/java/com/graphhopper/http/GHBaseServlet.java b/web/src/main/java/com/graphhopper/http/GHBaseServlet.java index 855b593e2c6..53b4fce9e75 100644 --- a/web/src/main/java/com/graphhopper/http/GHBaseServlet.java +++ b/web/src/main/java/com/graphhopper/http/GHBaseServlet.java @@ -38,30 +38,25 @@ /** * @author Peter Karich */ -public class GHBaseServlet extends HttpServlet -{ +public class GHBaseServlet extends HttpServlet { protected static final Logger logger = LoggerFactory.getLogger(GHBaseServlet.class); @Inject @Named("jsonp_allowed") private boolean jsonpAllowed; - protected void writeJson( HttpServletRequest req, HttpServletResponse res, JSONObject json ) throws JSONException, IOException - { + protected void writeJson(HttpServletRequest req, HttpServletResponse res, JSONObject json) throws JSONException, IOException { String type = getParam(req, "type", "json"); res.setCharacterEncoding("UTF-8"); boolean debug = getBooleanParam(req, "debug", false) || getBooleanParam(req, "pretty", false); - if ("jsonp".equals(type)) - { + if ("jsonp".equals(type)) { res.setContentType("application/javascript"); - if (!jsonpAllowed) - { + if (!jsonpAllowed) { writeError(res, SC_BAD_REQUEST, "Server is not configured to allow jsonp!"); return; } String callbackName = getParam(req, "callback", null); - if (callbackName == null) - { + if (callbackName == null) { writeError(res, SC_BAD_REQUEST, "No callback provided, necessary if type=jsonp"); return; } @@ -71,8 +66,7 @@ protected void writeJson( HttpServletRequest req, HttpServletResponse res, JSONO else writeResponse(res, callbackName + "(" + json.toString() + ")"); - } else - { + } else { res.setContentType("application/json"); if (debug) writeResponse(res, json.toString(2)); @@ -81,31 +75,26 @@ protected void writeJson( HttpServletRequest req, HttpServletResponse res, JSONO } } - protected void writeError( HttpServletResponse res, int code, String message ) - { + protected void writeError(HttpServletResponse res, int code, String message) { JSONObject json = new JSONObject(); json.put("message", message); writeJsonError(res, code, json); } - protected void writeJsonError( HttpServletResponse res, int code, JSONObject json ) - { - try - { + protected void writeJsonError(HttpServletResponse res, int code, JSONObject json) { + try { // no type parameter check here as jsonp does not work if an error // also no debug parameter yet res.setContentType("application/json"); res.setCharacterEncoding("UTF-8"); res.setStatus(code); res.getWriter().append(json.toString(2)); - } catch (IOException ex) - { + } catch (IOException ex) { logger.error("Cannot write error " + ex.getMessage()); } } - protected String getParam( HttpServletRequest req, String key, String _default ) - { + protected String getParam(HttpServletRequest req, String key, String _default) { String[] l = req.getParameterMap().get(key); if (l != null && l.length > 0) return l[0]; @@ -113,24 +102,19 @@ protected String getParam( HttpServletRequest req, String key, String _default ) return _default; } - protected String[] getParams( HttpServletRequest req, String key ) - { + protected String[] getParams(HttpServletRequest req, String key) { String[] l = req.getParameterMap().get(key); - if (l != null && l.length > 0) - { + if (l != null && l.length > 0) { return l; } return new String[0]; } - protected List getDoubleParamList( HttpServletRequest req, String key ) - { + protected List getDoubleParamList(HttpServletRequest req, String key) { String[] l = req.getParameterMap().get(key); - if (l != null && l.length > 0) - { + if (l != null && l.length > 0) { ArrayList doubleList = new ArrayList(l.length); - for (String s : l) - { + for (String s : l) { doubleList.add(Double.valueOf(s)); } return doubleList; @@ -138,58 +122,43 @@ protected List getDoubleParamList( HttpServletRequest req, String key ) return Collections.emptyList(); } - protected long getLongParam( HttpServletRequest req, String key, long _default ) - { - try - { + protected long getLongParam(HttpServletRequest req, String key, long _default) { + try { return Long.parseLong(getParam(req, key, "" + _default)); - } catch (Exception ex) - { + } catch (Exception ex) { return _default; } } - - protected int getIntParam( HttpServletRequest req, String key, int _default ) - { - try - { + + protected int getIntParam(HttpServletRequest req, String key, int _default) { + try { return Integer.parseInt(getParam(req, key, "" + _default)); - } catch (Exception ex) - { + } catch (Exception ex) { return _default; } } - protected boolean getBooleanParam( HttpServletRequest req, String key, boolean _default ) - { - try - { + protected boolean getBooleanParam(HttpServletRequest req, String key, boolean _default) { + try { return Boolean.parseBoolean(getParam(req, key, "" + _default)); - } catch (Exception ex) - { + } catch (Exception ex) { return _default; } } - protected double getDoubleParam( HttpServletRequest req, String key, double _default ) - { - try - { + protected double getDoubleParam(HttpServletRequest req, String key, double _default) { + try { return Double.parseDouble(getParam(req, key, "" + _default)); - } catch (Exception ex) - { + } catch (Exception ex) { return _default; } } - public void writeResponse( HttpServletResponse res, String str ) - { - try - { + public void writeResponse(HttpServletResponse res, String str) { + try { res.setStatus(SC_OK); res.getWriter().append(str); - } catch (IOException ex) - { + } catch (IOException ex) { logger.error("Cannot write message:" + str, ex); } } diff --git a/web/src/main/java/com/graphhopper/http/GHErrorHandler.java b/web/src/main/java/com/graphhopper/http/GHErrorHandler.java index 38271bad203..2b812eaa418 100644 --- a/web/src/main/java/com/graphhopper/http/GHErrorHandler.java +++ b/web/src/main/java/com/graphhopper/http/GHErrorHandler.java @@ -17,39 +17,35 @@ */ package com.graphhopper.http; -import java.io.IOException; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import static javax.servlet.http.HttpServletResponse.SC_INTERNAL_SERVER_ERROR; import org.eclipse.jetty.server.Request; import org.eclipse.jetty.server.handler.ErrorHandler; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; + +import static javax.servlet.http.HttpServletResponse.SC_INTERNAL_SERVER_ERROR; + /** * @author Peter Karich */ -public class GHErrorHandler extends ErrorHandler -{ +public class GHErrorHandler extends ErrorHandler { private static final long serialVersionUID = 1L; private final Logger logger = LoggerFactory.getLogger(GHErrorHandler.class); @Override - public void handle( String str, Request req, HttpServletRequest httpReq, HttpServletResponse httpRes ) throws IOException - { + public void handle(String str, Request req, HttpServletRequest httpReq, HttpServletResponse httpRes) throws IOException { Throwable throwable = (Throwable) httpReq.getAttribute("javax.servlet.error.exception"); - if (throwable != null) - { + if (throwable != null) { String message = throwable.getMessage(); logger.error(message + ", via:" + httpReq.getRequestURL(), throwable); - } else - { + } else { String message = (String) httpReq.getAttribute("javax.servlet.error.message"); - if (message != null) - { + if (message != null) { logger.error("Internal error " + message + "! Via:" + httpReq.getRequestURL()); - } else - { + } else { logger.error("Internal error " + str + ", throwable not known! Via:" + httpReq.getRequestURL()); } } diff --git a/web/src/main/java/com/graphhopper/http/GHServer.java b/web/src/main/java/com/graphhopper/http/GHServer.java index c488454cb3f..f8f526e225e 100644 --- a/web/src/main/java/com/graphhopper/http/GHServer.java +++ b/web/src/main/java/com/graphhopper/http/GHServer.java @@ -23,53 +23,47 @@ import com.google.inject.Module; import com.google.inject.servlet.GuiceFilter; import com.graphhopper.util.CmdArgs; - +import org.eclipse.jetty.server.Handler; +import org.eclipse.jetty.server.HttpConnectionFactory; import org.eclipse.jetty.server.Server; -import org.eclipse.jetty.servlet.ServletHolder; - -import java.util.EnumSet; -import javax.servlet.DispatcherType; - -import org.eclipse.jetty.server.*; +import org.eclipse.jetty.server.ServerConnector; import org.eclipse.jetty.server.handler.HandlerList; import org.eclipse.jetty.server.handler.ResourceHandler; import org.eclipse.jetty.server.handler.gzip.GzipHandler; import org.eclipse.jetty.servlet.FilterHolder; import org.eclipse.jetty.servlet.ServletContextHandler; +import org.eclipse.jetty.servlet.ServletHolder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import javax.servlet.DispatcherType; +import java.util.EnumSet; + /** * Simple server similar to integration tests setup. */ -public class GHServer -{ - public static void main( String[] args ) throws Exception - { - new GHServer(CmdArgs.read(args)).start(); - } - +public class GHServer { private final CmdArgs args; - private Server server; private final Logger logger = LoggerFactory.getLogger(getClass()); + private Server server; - public GHServer( CmdArgs args ) - { + public GHServer(CmdArgs args) { this.args = args; } - public void start() throws Exception - { + public static void main(String[] args) throws Exception { + new GHServer(CmdArgs.read(args)).start(); + } + + public void start() throws Exception { Injector injector = Guice.createInjector(createModule()); start(injector); } - public void start( Injector injector ) throws Exception - { + public void start(Injector injector) throws Exception { ResourceHandler resHandler = new ResourceHandler(); resHandler.setDirectoriesListed(false); - resHandler.setWelcomeFiles(new String[] - { + resHandler.setWelcomeFiles(new String[]{ "index.html" }); resHandler.setResourceBase(args.get("jetty.resourcebase", "./src/main/webapp")); @@ -100,8 +94,7 @@ public void start( Injector injector ) throws Exception server.addConnector(connector0); HandlerList handlers = new HandlerList(); - handlers.setHandlers(new Handler[] - { + handlers.setHandlers(new Handler[]{ resHandler, servHandler }); @@ -117,13 +110,10 @@ public void start( Injector injector ) throws Exception logger.info("Started server at HTTP " + host + ":" + httpPort); } - protected Module createModule() - { - return new AbstractModule() - { + protected Module createModule() { + return new AbstractModule() { @Override - protected void configure() - { + protected void configure() { binder().requireExplicitBindings(); install(new DefaultModule(args)); @@ -134,16 +124,13 @@ protected void configure() }; } - public void stop() - { + public void stop() { if (server == null) return; - try - { + try { server.stop(); - } catch (Exception ex) - { + } catch (Exception ex) { logger.error("Cannot stop jetty", ex); } } diff --git a/web/src/main/java/com/graphhopper/http/GHServletModule.java b/web/src/main/java/com/graphhopper/http/GHServletModule.java index eaa1defb9f9..947d3a21158 100644 --- a/web/src/main/java/com/graphhopper/http/GHServletModule.java +++ b/web/src/main/java/com/graphhopper/http/GHServletModule.java @@ -20,20 +20,18 @@ import com.google.inject.servlet.ServletModule; import com.graphhopper.util.CmdArgs; +import javax.inject.Singleton; import java.util.HashMap; import java.util.Map; -import javax.inject.Singleton; /** * @author Peter Karich */ -public class GHServletModule extends ServletModule -{ - protected Map params = new HashMap(); +public class GHServletModule extends ServletModule { protected final CmdArgs args; + protected Map params = new HashMap(); - public GHServletModule( CmdArgs args ) - { + public GHServletModule(CmdArgs args) { this.args = args; params.put("mimeTypes", "text/html," + "text/plain," @@ -48,8 +46,7 @@ public GHServletModule( CmdArgs args ) } @Override - protected void configureServlets() - { + protected void configureServlets() { filter("*").through(CORSFilter.class, params); bind(CORSFilter.class).in(Singleton.class); diff --git a/web/src/main/java/com/graphhopper/http/GraphHopperServlet.java b/web/src/main/java/com/graphhopper/http/GraphHopperServlet.java index 0644ef08780..75f3518e9e3 100644 --- a/web/src/main/java/com/graphhopper/http/GraphHopperServlet.java +++ b/web/src/main/java/com/graphhopper/http/GraphHopperServlet.java @@ -17,10 +17,10 @@ */ package com.graphhopper.http; -import com.graphhopper.PathWrapper; import com.graphhopper.GHRequest; import com.graphhopper.GHResponse; import com.graphhopper.GraphHopper; +import com.graphhopper.PathWrapper; import com.graphhopper.routing.util.FlagEncoder; import com.graphhopper.routing.util.HintsMap; import com.graphhopper.util.StopWatch; @@ -54,16 +54,14 @@ * * @author Peter Karich */ -public class GraphHopperServlet extends GHBaseServlet -{ +public class GraphHopperServlet extends GHBaseServlet { @Inject private GraphHopper hopper; @Inject private RouteSerializer routeSerializer; @Override - public void doGet( HttpServletRequest httpReq, HttpServletResponse httpRes ) throws ServletException, IOException - { + public void doGet(HttpServletRequest httpReq, HttpServletResponse httpRes) throws ServletException, IOException { List requestPoints = getPoints(httpReq, "point"); GHResponse ghRsp = new GHResponse(); @@ -82,49 +80,38 @@ public void doGet( HttpServletRequest httpReq, HttpServletResponse httpRes ) thr StopWatch sw = new StopWatch().start(); - if (!ghRsp.hasErrors()) - { - try - { + if (!ghRsp.hasErrors()) { + try { List favoredHeadings = Collections.EMPTY_LIST; - try - { + try { favoredHeadings = getDoubleParamList(httpReq, "heading"); - } catch (NumberFormatException e) - { + } catch (NumberFormatException e) { throw new IllegalArgumentException("heading list in from format: " + e.getMessage()); } - if (!hopper.getEncodingManager().supports(vehicleStr)) - { + if (!hopper.getEncodingManager().supports(vehicleStr)) { throw new IllegalArgumentException("Vehicle not supported: " + vehicleStr); - } else if (enableElevation && !hopper.hasElevation()) - { + } else if (enableElevation && !hopper.hasElevation()) { throw new IllegalArgumentException("Elevation not supported!"); - } else if (favoredHeadings.size() > 1 && favoredHeadings.size() != requestPoints.size()) - { + } else if (favoredHeadings.size() > 1 && favoredHeadings.size() != requestPoints.size()) { throw new IllegalArgumentException("The number of 'heading' parameters must be <= 1 " + "or equal to the number of points (" + requestPoints.size() + ")"); } FlagEncoder algoVehicle = hopper.getEncodingManager().getEncoder(vehicleStr); GHRequest request; - if (favoredHeadings.size() > 0) - { + if (favoredHeadings.size() > 0) { // if only one favored heading is specified take as start heading - if (favoredHeadings.size() == 1) - { + if (favoredHeadings.size() == 1) { List paddedHeadings = new ArrayList(Collections.nCopies(requestPoints.size(), Double.NaN)); paddedHeadings.set(0, favoredHeadings.get(0)); request = new GHRequest(requestPoints, paddedHeadings); - } else - { + } else { request = new GHRequest(requestPoints, favoredHeadings); } - } else - { + } else { request = new GHRequest(requestPoints); } @@ -139,8 +126,7 @@ public void doGet( HttpServletRequest httpReq, HttpServletResponse httpRes ) thr put("wayPointMaxDistance", minPathPrecision); ghRsp = hopper.route(request); - } catch (IllegalArgumentException ex) - { + } catch (IllegalArgumentException ex) { ghRsp.addError(ex); } } @@ -155,11 +141,9 @@ public void doGet( HttpServletRequest httpReq, HttpServletResponse httpRes ) thr if (writeGPX && alternatives > 1) ghRsp.addError(new IllegalArgumentException("Alternatives are currently not yet supported for GPX")); - if (ghRsp.hasErrors()) - { + if (ghRsp.hasErrors()) { logger.error(logStr + ", errors:" + ghRsp.getErrors()); - } else - { + } else { PathWrapper altRsp0 = ghRsp.getBest(); logger.info(logStr + ", alternatives: " + alternatives + ", distance0: " + altRsp0.getDistance() @@ -168,20 +152,16 @@ public void doGet( HttpServletRequest httpReq, HttpServletResponse httpRes ) thr + ", debugInfo: " + ghRsp.getDebugInfo()); } - if (writeGPX) - { - if (ghRsp.hasErrors()) - { + if (writeGPX) { + if (ghRsp.hasErrors()) { httpRes.setStatus(SC_BAD_REQUEST); httpRes.getWriter().append(errorsToXML(ghRsp.getErrors())); - } else - { + } else { // no error => we can now safely call getFirst String xml = createGPXString(httpReq, httpRes, ghRsp.getBest()); writeResponse(httpRes, xml); } - } else - { + } else { Map map = routeSerializer.toJSON(ghRsp, calcPoints, pointsEncoded, enableElevation, enableInstructions); @@ -196,8 +176,7 @@ public void doGet( HttpServletRequest httpReq, HttpServletResponse httpRes ) thr } } - protected String createGPXString( HttpServletRequest req, HttpServletResponse res, PathWrapper rsp ) - { + protected String createGPXString(HttpServletRequest req, HttpServletResponse res, PathWrapper rsp) { boolean includeElevation = getBooleanParam(req, "elevation", false); // default to false for the route part in next API version, see #437 boolean withRoute = getBooleanParam(req, "gpx.route", true); @@ -215,13 +194,11 @@ protected String createGPXString( HttpServletRequest req, HttpServletResponse re return rsp.getInstructions().createGPX(trackName, time, includeElevation, withRoute, withTrack, withWayPoints); } - protected String errorsToXML( Collection list ) - { + protected String errorsToXML(Collection list) { if (list.isEmpty()) throw new RuntimeException("errorsToXML should not be called with an empty list"); - try - { + try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.newDocument(); @@ -243,8 +220,7 @@ protected String errorsToXML( Collection list ) Element hintsElement = doc.createElement("hints"); extensionsElement.appendChild(hintsElement); - for (Throwable t : list) - { + for (Throwable t : list) { Element error = doc.createElement("error"); hintsElement.appendChild(error); error.setAttribute("message", t.getMessage()); @@ -255,21 +231,17 @@ protected String errorsToXML( Collection list ) StringWriter writer = new StringWriter(); transformer.transform(new DOMSource(doc), new StreamResult(writer)); return writer.toString(); - } catch (Exception ex) - { + } catch (Exception ex) { throw new RuntimeException(ex); } } - protected List getPoints( HttpServletRequest req, String key ) - { + protected List getPoints(HttpServletRequest req, String key) { String[] pointsAsStr = getParams(req, key); final List infoPoints = new ArrayList(pointsAsStr.length); - for (String str : pointsAsStr) - { + for (String str : pointsAsStr) { String[] fromStrs = str.split(","); - if (fromStrs.length == 2) - { + if (fromStrs.length == 2) { GHPoint point = GHPoint.parse(str); if (point != null) infoPoints.add(point); @@ -279,11 +251,9 @@ protected List getPoints( HttpServletRequest req, String key ) return infoPoints; } - protected void initHints( GHRequest request, Map parameterMap ) - { + protected void initHints(GHRequest request, Map parameterMap) { HintsMap m = request.getHints(); - for (Entry e : parameterMap.entrySet()) - { + for (Entry e : parameterMap.entrySet()) { if (e.getValue().length == 1) m.put(e.getKey(), e.getValue()[0]); } diff --git a/web/src/main/java/com/graphhopper/http/GraphHopperWeb.java b/web/src/main/java/com/graphhopper/http/GraphHopperWeb.java index 53ac0dc9eac..9ee781cbe47 100644 --- a/web/src/main/java/com/graphhopper/http/GraphHopperWeb.java +++ b/web/src/main/java/com/graphhopper/http/GraphHopperWeb.java @@ -17,41 +17,39 @@ */ package com.graphhopper.http; -import com.graphhopper.PathWrapper; import com.graphhopper.GHRequest; import com.graphhopper.GHResponse; import com.graphhopper.GraphHopperAPI; +import com.graphhopper.PathWrapper; import com.graphhopper.util.*; import com.graphhopper.util.exceptions.CannotFindPointException; import com.graphhopper.util.exceptions.PointOutOfBoundsException; import com.graphhopper.util.shapes.GHPoint; +import org.json.JSONArray; +import org.json.JSONObject; + import java.util.ArrayList; import java.util.HashSet; - import java.util.List; import java.util.Map.Entry; import java.util.Set; -import org.json.JSONArray; -import org.json.JSONObject; - /** * Main wrapper of the GraphHopper Directions API for a simple and efficient usage. *

    + * * @author Peter Karich */ -public class GraphHopperWeb implements GraphHopperAPI -{ +public class GraphHopperWeb implements GraphHopperAPI { + private final Set ignoreSet; private Downloader downloader = new Downloader("GraphHopper Java Client"); private String routeServiceUrl = "https://graphhopper.com/api/1/route"; private String key = ""; private boolean instructions = true; private boolean calcPoints = true; private boolean elevation = false; - private final Set ignoreSet; - public GraphHopperWeb() - { + public GraphHopperWeb() { // some parameters are supported directly via Java API so ignore them when writing the getHints map ignoreSet = new HashSet(); ignoreSet.add("calc_points"); @@ -72,149 +70,30 @@ public GraphHopperWeb() ignoreSet.add("type"); } - public GraphHopperWeb setDownloader( Downloader downloader ) - { - this.downloader = downloader; - return this; - } - - @Override - public boolean load( String serviceUrl ) - { - this.routeServiceUrl = serviceUrl; - return true; - } - - public GraphHopperWeb setKey( String key ) - { - if (key == null || key.isEmpty()) - throw new IllegalStateException("Key cannot be empty"); - - this.key = key; - return this; - } - - public GraphHopperWeb setCalcPoints( boolean calcPoints ) - { - this.calcPoints = calcPoints; - return this; - } - - public GraphHopperWeb setInstructions( boolean b ) - { - instructions = b; - return this; - } - - public GraphHopperWeb setElevation( boolean withElevation ) - { - this.elevation = withElevation; - return this; - } - - @Override - public GHResponse route( GHRequest request ) - { - try - { - String places = ""; - for (GHPoint p : request.getPoints()) - { - places += "point=" + p.lat + "," + p.lon + "&"; - } - - boolean tmpInstructions = request.getHints().getBool("instructions", instructions); - boolean tmpCalcPoints = request.getHints().getBool("calc_points", calcPoints); - - if (tmpInstructions && !tmpCalcPoints) - throw new IllegalStateException("Cannot calculate instructions without points (only points without instructions). " - + "Use calc_points=false and instructions=false to disable point and instruction calculation"); - - boolean tmpElevation = request.getHints().getBool("elevation", elevation); - - String url = routeServiceUrl - + "?" - + places - + "&type=json" - + "&instructions=" + tmpInstructions - + "&points_encoded=true" - + "&calc_points=" + tmpCalcPoints - + "&algorithm=" + request.getAlgorithm() - + "&locale=" + request.getLocale().toString() - + "&elevation=" + tmpElevation; - - if (!request.getVehicle().isEmpty()) - url += "&vehicle=" + request.getVehicle(); - - if (!key.isEmpty()) - url += "&key=" + key; - - for (Entry entry : request.getHints().toMap().entrySet()) - { - String urlKey = entry.getKey(); - String urlValue = entry.getValue(); - - // use lower case conversion for check only! - if (ignoreSet.contains(urlKey.toLowerCase())) - continue; - - if (urlValue != null && !urlValue.isEmpty()) - url += "&" + WebHelper.encodeURL(urlKey) + "=" + WebHelper.encodeURL(urlValue); - } - - String str = downloader.downloadAsString(url, true); - JSONObject json = new JSONObject(str); - - GHResponse res = new GHResponse(); - res.addErrors(readErrors(json)); - if (res.hasErrors()) - return res; - - JSONArray paths = json.getJSONArray("paths"); - for (int index = 0; index < paths.length(); index++) - { - JSONObject path = paths.getJSONObject(index); - PathWrapper altRsp = createPathWrapper(path, tmpCalcPoints, tmpInstructions, tmpElevation); - res.add(altRsp); - } - - return res; - - } catch (Exception ex) - { - throw new RuntimeException("Problem while fetching path " + request.getPoints() + ": " + ex.getMessage(), ex); - } - } - - public static PathWrapper createPathWrapper( JSONObject path, - boolean tmpCalcPoints, boolean tmpInstructions, boolean tmpElevation ) - { + public static PathWrapper createPathWrapper(JSONObject path, + boolean tmpCalcPoints, boolean tmpInstructions, boolean tmpElevation) { PathWrapper pathWrapper = new PathWrapper(); pathWrapper.addErrors(readErrors(path)); if (pathWrapper.hasErrors()) return pathWrapper; - if (path.has("snapped_waypoints")) - { + if (path.has("snapped_waypoints")) { String snappedPointStr = path.getString("snapped_waypoints"); PointList snappedPoints = WebHelper.decodePolyline(snappedPointStr, 5, tmpElevation); pathWrapper.setWaypoints(snappedPoints); } - if (tmpCalcPoints) - { + if (tmpCalcPoints) { String pointStr = path.getString("points"); PointList pointList = WebHelper.decodePolyline(pointStr, 100, tmpElevation); pathWrapper.setPoints(pointList); - if (tmpInstructions) - { + if (tmpInstructions) { JSONArray instrArr = path.getJSONArray("instructions"); InstructionList il = new InstructionList(null); int viaCount = 1; - for (int instrIndex = 0; instrIndex < instrArr.length(); instrIndex++) - { + for (int instrIndex = 0; instrIndex < instrArr.length(); instrIndex++) { JSONObject jsonObj = instrArr.getJSONObject(instrIndex); double instDist = jsonObj.getDouble("distance"); String text = jsonObj.getString("text"); @@ -224,29 +103,24 @@ public static PathWrapper createPathWrapper( JSONObject path, int from = iv.getInt(0); int to = iv.getInt(1); PointList instPL = new PointList(to - from, tmpElevation); - for (int j = from; j <= to; j++) - { + for (int j = from; j <= to; j++) { instPL.add(pointList, j); } InstructionAnnotation ia = InstructionAnnotation.EMPTY; - if (jsonObj.has("annotation_importance") && jsonObj.has("annotation_text")) - { + if (jsonObj.has("annotation_importance") && jsonObj.has("annotation_text")) { ia = new InstructionAnnotation(jsonObj.getInt("annotation_importance"), jsonObj.getString("annotation_text")); } Instruction instr; - if (sign == Instruction.USE_ROUNDABOUT || sign == Instruction.LEAVE_ROUNDABOUT) - { + if (sign == Instruction.USE_ROUNDABOUT || sign == Instruction.LEAVE_ROUNDABOUT) { RoundaboutInstruction ri = new RoundaboutInstruction(sign, text, ia, instPL); - if (jsonObj.has("exit_number")) - { + if (jsonObj.has("exit_number")) { ri.setExitNumber(jsonObj.getInt("exit_number")); } - if (jsonObj.has("turn_angle")) - { + if (jsonObj.has("turn_angle")) { // TODO provide setTurnAngle setter double angle = jsonObj.getDouble("turn_angle"); ri.setDirOfRotation(angle); @@ -254,17 +128,14 @@ public static PathWrapper createPathWrapper( JSONObject path, } instr = ri; - } else if (sign == Instruction.REACHED_VIA) - { + } else if (sign == Instruction.REACHED_VIA) { ViaInstruction tmpInstr = new ViaInstruction(text, ia, instPL); tmpInstr.setViaCount(viaCount); viaCount++; instr = tmpInstr; - } else if (sign == Instruction.FINISH) - { + } else if (sign == Instruction.FINISH) { instr = new FinishInstruction(instPL, 0); - } else - { + } else { instr = new Instruction(sign, text, ia, instPL); } @@ -285,18 +156,14 @@ public static PathWrapper createPathWrapper( JSONObject path, return pathWrapper; } - public static List readErrors( JSONObject json ) - { + public static List readErrors(JSONObject json) { List errors = new ArrayList(); JSONArray errorJson; - if (json.has("message")) - { - if (json.has("hints")) - { + if (json.has("message")) { + if (json.has("hints")) { errorJson = json.getJSONArray("hints"); - } else - { + } else { // should not happen errors.add(new RuntimeException(json.getString("message"))); return errors; @@ -304,8 +171,7 @@ public static List readErrors( JSONObject json ) } else return errors; - for (int i = 0; i < errorJson.length(); i++) - { + for (int i = 0; i < errorJson.length(); i++) { JSONObject error = errorJson.getJSONObject(i); String exClass = ""; if (error.has("details")) @@ -321,17 +187,13 @@ else if (exClass.equals(RuntimeException.class.getName())) errors.add(new RuntimeException(exMessage)); else if (exClass.equals(IllegalArgumentException.class.getName())) errors.add(new IllegalArgumentException(exMessage)); - else if (exClass.equals(CannotFindPointException.class.getName())) - { + else if (exClass.equals(CannotFindPointException.class.getName())) { int pointIndex = error.getInt("point_index"); errors.add(new CannotFindPointException(exMessage, pointIndex)); - } - else if (exClass.equals(PointOutOfBoundsException.class.getName())) - { + } else if (exClass.equals(PointOutOfBoundsException.class.getName())) { int pointIndex = error.getInt("point_index"); errors.add(new PointOutOfBoundsException(exMessage, pointIndex)); - } - else if (exClass.isEmpty()) + } else if (exClass.isEmpty()) errors.add(new RuntimeException(exMessage)); else errors.add(new RuntimeException(exClass + " " + exMessage)); @@ -342,4 +204,106 @@ else if (exClass.isEmpty()) return errors; } + + public GraphHopperWeb setDownloader(Downloader downloader) { + this.downloader = downloader; + return this; + } + + @Override + public boolean load(String serviceUrl) { + this.routeServiceUrl = serviceUrl; + return true; + } + + public GraphHopperWeb setKey(String key) { + if (key == null || key.isEmpty()) + throw new IllegalStateException("Key cannot be empty"); + + this.key = key; + return this; + } + + public GraphHopperWeb setCalcPoints(boolean calcPoints) { + this.calcPoints = calcPoints; + return this; + } + + public GraphHopperWeb setInstructions(boolean b) { + instructions = b; + return this; + } + + public GraphHopperWeb setElevation(boolean withElevation) { + this.elevation = withElevation; + return this; + } + + @Override + public GHResponse route(GHRequest request) { + try { + String places = ""; + for (GHPoint p : request.getPoints()) { + places += "point=" + p.lat + "," + p.lon + "&"; + } + + boolean tmpInstructions = request.getHints().getBool("instructions", instructions); + boolean tmpCalcPoints = request.getHints().getBool("calc_points", calcPoints); + + if (tmpInstructions && !tmpCalcPoints) + throw new IllegalStateException("Cannot calculate instructions without points (only points without instructions). " + + "Use calc_points=false and instructions=false to disable point and instruction calculation"); + + boolean tmpElevation = request.getHints().getBool("elevation", elevation); + + String url = routeServiceUrl + + "?" + + places + + "&type=json" + + "&instructions=" + tmpInstructions + + "&points_encoded=true" + + "&calc_points=" + tmpCalcPoints + + "&algorithm=" + request.getAlgorithm() + + "&locale=" + request.getLocale().toString() + + "&elevation=" + tmpElevation; + + if (!request.getVehicle().isEmpty()) + url += "&vehicle=" + request.getVehicle(); + + if (!key.isEmpty()) + url += "&key=" + key; + + for (Entry entry : request.getHints().toMap().entrySet()) { + String urlKey = entry.getKey(); + String urlValue = entry.getValue(); + + // use lower case conversion for check only! + if (ignoreSet.contains(urlKey.toLowerCase())) + continue; + + if (urlValue != null && !urlValue.isEmpty()) + url += "&" + WebHelper.encodeURL(urlKey) + "=" + WebHelper.encodeURL(urlValue); + } + + String str = downloader.downloadAsString(url, true); + JSONObject json = new JSONObject(str); + + GHResponse res = new GHResponse(); + res.addErrors(readErrors(json)); + if (res.hasErrors()) + return res; + + JSONArray paths = json.getJSONArray("paths"); + for (int index = 0; index < paths.length(); index++) { + JSONObject path = paths.getJSONObject(index); + PathWrapper altRsp = createPathWrapper(path, tmpCalcPoints, tmpInstructions, tmpElevation); + res.add(altRsp); + } + + return res; + + } catch (Exception ex) { + throw new RuntimeException("Problem while fetching path " + request.getPoints() + ": " + ex.getMessage(), ex); + } + } } diff --git a/web/src/main/java/com/graphhopper/http/GuiceServletConfig.java b/web/src/main/java/com/graphhopper/http/GuiceServletConfig.java index fbfc8555a09..e51d03f1465 100644 --- a/web/src/main/java/com/graphhopper/http/GuiceServletConfig.java +++ b/web/src/main/java/com/graphhopper/http/GuiceServletConfig.java @@ -28,36 +28,30 @@ *

    * http://code.google.com/p/google-guice/wiki/ServletModule *

    + * * @author Peter Karich */ -public class GuiceServletConfig extends GuiceServletContextListener -{ +public class GuiceServletConfig extends GuiceServletContextListener { private final CmdArgs args; - public GuiceServletConfig() - { - try - { + public GuiceServletConfig() { + try { args = CmdArgs.readFromConfig("config.properties", "graphhopper.config"); - } catch (Exception ex) - { + } catch (Exception ex) { throw new RuntimeException(ex); } } @Override - protected Injector getInjector() - { + protected Injector getInjector() { return Guice.createInjector(createDefaultModule(), createServletModule()); } - protected Module createDefaultModule() - { + protected Module createDefaultModule() { return new DefaultModule(args); } - protected Module createServletModule() - { + protected Module createServletModule() { return new GHServletModule(args); } } diff --git a/web/src/main/java/com/graphhopper/http/I18NServlet.java b/web/src/main/java/com/graphhopper/http/I18NServlet.java index 67e95cd0e3a..f8b655333a3 100644 --- a/web/src/main/java/com/graphhopper/http/I18NServlet.java +++ b/web/src/main/java/com/graphhopper/http/I18NServlet.java @@ -18,36 +18,32 @@ package com.graphhopper.http; import com.graphhopper.util.Helper; -import com.graphhopper.util.TranslationMap; import com.graphhopper.util.Translation; +import com.graphhopper.util.TranslationMap; +import org.json.JSONObject; -import java.io.IOException; -import java.util.Locale; import javax.inject.Inject; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; - -import org.json.JSONObject; +import java.io.IOException; +import java.util.Locale; /** * @author Peter Karich */ -public class I18NServlet extends GHBaseServlet -{ +public class I18NServlet extends GHBaseServlet { @Inject private TranslationMap map; @Override - public void doGet( HttpServletRequest req, HttpServletResponse res ) throws ServletException, IOException - { + public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { String locale = ""; String path = req.getPathInfo(); if (!Helper.isEmpty(path) && path.startsWith("/")) locale = path.substring(1); - if (Helper.isEmpty(locale)) - { + if (Helper.isEmpty(locale)) { // fall back to language specified in header e.g. via browser settings String acceptLang = req.getHeader("Accept-Language"); if (!Helper.isEmpty(acceptLang)) diff --git a/web/src/main/java/com/graphhopper/http/IPFilter.java b/web/src/main/java/com/graphhopper/http/IPFilter.java index dc80597068a..068e0b28eb5 100644 --- a/web/src/main/java/com/graphhopper/http/IPFilter.java +++ b/web/src/main/java/com/graphhopper/http/IPFilter.java @@ -17,14 +17,14 @@ */ package com.graphhopper.http; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.servlet.*; +import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.HashSet; import java.util.Set; -import javax.servlet.*; -import javax.servlet.http.HttpServletResponse; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; /** * This IP filter class accepts a list of IPs for blacklisting OR for whitelisting (but not both). @@ -33,16 +33,15 @@ *

    * The internal ip filter from jetty did not work (NP exceptions) *

    + * * @author Peter Karich */ -public class IPFilter implements Filter -{ +public class IPFilter implements Filter { private final Logger logger = LoggerFactory.getLogger(getClass()); private final Set whites; private final Set blacks; - public IPFilter( String whiteList, String blackList ) - { + public IPFilter(String whiteList, String blackList) { whites = createSet(whiteList.split(",")); blacks = createSet(blackList.split(",")); if (!whites.isEmpty()) @@ -55,28 +54,22 @@ public IPFilter( String whiteList, String blackList ) } @Override - public void doFilter( ServletRequest request, ServletResponse response, FilterChain chain ) throws IOException, ServletException - { + public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { String ip = request.getRemoteAddr(); - if (accept(ip)) - { + if (accept(ip)) { chain.doFilter(request, response); - } else - { + } else { logger.warn("Did not accept IP " + ip); ((HttpServletResponse) response).sendError(HttpServletResponse.SC_FORBIDDEN); } } - public boolean accept( String ip ) - { + public boolean accept(String ip) { if (whites.isEmpty() && blacks.isEmpty()) return true; - if (!whites.isEmpty()) - { - for (String w : whites) - { + if (!whites.isEmpty()) { + for (String w : whites) { if (simpleMatch(ip, w)) return true; } @@ -86,8 +79,7 @@ public boolean accept( String ip ) if (blacks.isEmpty()) throw new IllegalStateException("cannot happen"); - for (String b : blacks) - { + for (String b : blacks) { if (simpleMatch(ip, b)) return false; } @@ -96,20 +88,16 @@ public boolean accept( String ip ) } @Override - public void init( FilterConfig filterConfig ) throws ServletException - { + public void init(FilterConfig filterConfig) throws ServletException { } @Override - public void destroy() - { + public void destroy() { } - private Set createSet( String[] split ) - { + private Set createSet(String[] split) { Set set = new HashSet(split.length); - for (String str : split) - { + for (String str : split) { str = str.trim(); if (!str.isEmpty()) set.add(str); @@ -117,11 +105,9 @@ private Set createSet( String[] split ) return set; } - public boolean simpleMatch( String ip, String pattern ) - { + public boolean simpleMatch(String ip, String pattern) { String[] ipParts = pattern.split("\\*"); - for (String ipPart : ipParts) - { + for (String ipPart : ipParts) { int idx = ip.indexOf(ipPart); if (idx == -1) return false; diff --git a/web/src/main/java/com/graphhopper/http/InfoServlet.java b/web/src/main/java/com/graphhopper/http/InfoServlet.java index 1eb01cd6ebd..524edb102b4 100644 --- a/web/src/main/java/com/graphhopper/http/InfoServlet.java +++ b/web/src/main/java/com/graphhopper/http/InfoServlet.java @@ -22,29 +22,25 @@ import com.graphhopper.util.Constants; import com.graphhopper.util.Helper; import com.graphhopper.util.shapes.BBox; +import org.json.JSONObject; -import java.io.IOException; -import java.util.ArrayList; -import java.util.List; import javax.inject.Inject; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; - - -import org.json.JSONObject; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; /** * @author Peter Karich */ -public class InfoServlet extends GHBaseServlet -{ +public class InfoServlet extends GHBaseServlet { @Inject private GraphHopper hopper; @Override - public void doGet( HttpServletRequest req, HttpServletResponse res ) throws ServletException, IOException - { + public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { BBox bb = hopper.getGraphHopperStorage().getBounds(); List list = new ArrayList(4); list.add(bb.minLon); @@ -58,8 +54,7 @@ public void doGet( HttpServletRequest req, HttpServletResponse res ) throws Serv String[] vehicles = hopper.getGraphHopperStorage().getEncodingManager().toString().split(","); json.put("supported_vehicles", vehicles); JSONObject features = new JSONObject(); - for (String v : vehicles) - { + for (String v : vehicles) { JSONObject perVehicleJson = new JSONObject(); perVehicleJson.put("elevation", hopper.hasElevation()); features.put(v, perVehicleJson); @@ -73,7 +68,7 @@ public void doGet( HttpServletRequest req, HttpServletResponse res ) throws Serv json.put("import_date", props.get("datareader.import.date")); if (!Helper.isEmpty(props.get("datareader.data.date"))) - json.put("data_date" , props.get("datareader.data.date")); + json.put("data_date", props.get("datareader.data.date")); if (!Helper.isEmpty(props.get("prepare.date"))) json.put("prepare_date", props.get("prepare.date")); diff --git a/web/src/main/java/com/graphhopper/http/InvalidRequestServlet.java b/web/src/main/java/com/graphhopper/http/InvalidRequestServlet.java index 8de227530b1..a047a3748fe 100644 --- a/web/src/main/java/com/graphhopper/http/InvalidRequestServlet.java +++ b/web/src/main/java/com/graphhopper/http/InvalidRequestServlet.java @@ -22,11 +22,9 @@ import javax.servlet.http.HttpServletResponse; import java.io.IOException; -public class InvalidRequestServlet extends GHBaseServlet -{ +public class InvalidRequestServlet extends GHBaseServlet { @Override - protected void service( HttpServletRequest req, HttpServletResponse res ) throws ServletException, IOException - { + protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { JSONObject json = new JSONObject(); json.put("message", "Not found"); writeJsonError(res, HttpServletResponse.SC_NOT_FOUND, json); diff --git a/web/src/main/java/com/graphhopper/http/NearestServlet.java b/web/src/main/java/com/graphhopper/http/NearestServlet.java index ccce8f033ef..8aa0eec9612 100644 --- a/web/src/main/java/com/graphhopper/http/NearestServlet.java +++ b/web/src/main/java/com/graphhopper/http/NearestServlet.java @@ -37,30 +37,25 @@ /** * @author svantulden */ -public class NearestServlet extends GHBaseServlet -{ +public class NearestServlet extends GHBaseServlet { + private final DistanceCalc calc = Helper.DIST_EARTH; @Inject private GraphHopper hopper; - private final DistanceCalc calc = Helper.DIST_EARTH; @Override - public void doGet( HttpServletRequest httpReq, HttpServletResponse httpRes ) throws ServletException, IOException - { + public void doGet(HttpServletRequest httpReq, HttpServletResponse httpRes) throws ServletException, IOException { String pointStr = getParam(httpReq, "point", null); boolean enabledElevation = getBooleanParam(httpReq, "elevation", false); JSONObject result = new JSONObject(); - if (pointStr != null && !pointStr.equalsIgnoreCase("")) - { + if (pointStr != null && !pointStr.equalsIgnoreCase("")) { GHPoint place = GHPoint.parse(pointStr); LocationIndex index = hopper.getLocationIndex(); QueryResult qr = index.findClosest(place.lat, place.lon, EdgeFilter.ALL_EDGES); - if (!qr.isValid()) - { + if (!qr.isValid()) { result.put("error", "Nearest point cannot be found!"); - } else - { + } else { GHPoint3D snappedPoint = qr.getSnappedPoint(); result.put("type", "Point"); @@ -76,8 +71,7 @@ public void doGet( HttpServletRequest httpReq, HttpServletResponse httpRes ) thr // Distance from input to snapped point in meters result.put("distance", calc.calcDist(place.lat, place.lon, snappedPoint.lat, snappedPoint.lon)); } - } else - { + } else { result.put("error", "No lat/lon specified!"); } diff --git a/web/src/main/java/com/graphhopper/http/RouteSerializer.java b/web/src/main/java/com/graphhopper/http/RouteSerializer.java index fe2d80b2a78..8ef600dccb4 100644 --- a/web/src/main/java/com/graphhopper/http/RouteSerializer.java +++ b/web/src/main/java/com/graphhopper/http/RouteSerializer.java @@ -19,25 +19,26 @@ import com.graphhopper.GHResponse; import com.graphhopper.util.PointList; + import java.util.Map; /** * This interface speficies how the route should be transformed into JSON. *

    + * * @author Peter Karich */ -public interface RouteSerializer -{ +public interface RouteSerializer { /** * This method transforms the specified response into a JSON. */ - Map toJSON( GHResponse response, - boolean calcPoints, boolean pointsEncoded, - boolean includeElevation, boolean enableInstructions ); + Map toJSON(GHResponse response, + boolean calcPoints, boolean pointsEncoded, + boolean includeElevation, boolean enableInstructions); /** * This method returns either a Map containing the GeoJSON of the specified points OR the string * encoded polyline of it. */ - Object createPoints( PointList points, boolean pointsEncoded, boolean includeElevation ); + Object createPoints(PointList points, boolean pointsEncoded, boolean includeElevation); } diff --git a/web/src/main/java/com/graphhopper/http/SimpleRouteSerializer.java b/web/src/main/java/com/graphhopper/http/SimpleRouteSerializer.java index 932be038eba..37dd5b1e87e 100644 --- a/web/src/main/java/com/graphhopper/http/SimpleRouteSerializer.java +++ b/web/src/main/java/com/graphhopper/http/SimpleRouteSerializer.java @@ -17,30 +17,27 @@ */ package com.graphhopper.http; -import com.graphhopper.PathWrapper; import com.graphhopper.GHResponse; +import com.graphhopper.PathWrapper; import com.graphhopper.util.Helper; import com.graphhopper.util.InstructionList; import com.graphhopper.util.PointList; import com.graphhopper.util.exceptions.GHException; import com.graphhopper.util.shapes.BBox; + import java.util.*; /** - * * @author Peter Karich */ -public class SimpleRouteSerializer implements RouteSerializer -{ +public class SimpleRouteSerializer implements RouteSerializer { private final BBox maxBounds; - public SimpleRouteSerializer( BBox maxBounds ) - { + public SimpleRouteSerializer(BBox maxBounds) { this.maxBounds = maxBounds; } - private String getMessage( Throwable t ) - { + private String getMessage(Throwable t) { if (t.getMessage() == null) return t.getClass().getSimpleName(); else @@ -48,30 +45,25 @@ private String getMessage( Throwable t ) } @Override - public Map toJSON( GHResponse rsp, - boolean calcPoints, boolean pointsEncoded, - boolean includeElevation, boolean enableInstructions ) - { + public Map toJSON(GHResponse rsp, + boolean calcPoints, boolean pointsEncoded, + boolean includeElevation, boolean enableInstructions) { Map json = new HashMap(); - if (rsp.hasErrors()) - { + if (rsp.hasErrors()) { json.put("message", getMessage(rsp.getErrors().get(0))); List> errorHintList = new ArrayList>(); - for (Throwable t : rsp.getErrors()) - { + for (Throwable t : rsp.getErrors()) { Map map = new HashMap(); map.put("message", getMessage(t)); map.put("details", t.getClass().getName()); - if(t instanceof GHException) - { + if (t instanceof GHException) { map.putAll(((GHException) t).getDetails()); } errorHintList.add(map); } json.put("hints", errorHintList); - } else - { + } else { Map jsonInfo = new HashMap(); json.put("info", jsonInfo); json.put("hints", rsp.getHints().toMap()); @@ -80,8 +72,7 @@ public Map toJSON( GHResponse rsp, jsonInfo.put("copyrights", Arrays.asList("GraphHopper", "OpenStreetMap contributors")); List> jsonPathList = new ArrayList>(); - for (PathWrapper ar : rsp.getAll()) - { + for (PathWrapper ar : rsp.getAll()) { Map jsonPath = new HashMap(); jsonPath.put("distance", Helper.round(ar.getDistance(), 3)); jsonPath.put("weight", Helper.round6(ar.getRouteWeight())); @@ -89,21 +80,18 @@ public Map toJSON( GHResponse rsp, if (!ar.getDescription().isEmpty()) jsonPath.put("description", ar.getDescription()); - if (calcPoints) - { + if (calcPoints) { jsonPath.put("points_encoded", pointsEncoded); PointList points = ar.getPoints(); - if (points.getSize() >= 2) - { + if (points.getSize() >= 2) { BBox maxBounds2D = new BBox(maxBounds.minLon, maxBounds.maxLon, maxBounds.minLat, maxBounds.maxLat); jsonPath.put("bbox", ar.calcRouteBBox(maxBounds2D).toGeoJson()); } jsonPath.put("points", createPoints(points, pointsEncoded, includeElevation)); - if (enableInstructions) - { + if (enableInstructions) { InstructionList instructions = ar.getInstructions(); jsonPath.put("instructions", instructions.createJson()); } @@ -122,8 +110,7 @@ public Map toJSON( GHResponse rsp, } @Override - public Object createPoints( PointList points, boolean pointsEncoded, boolean includeElevation ) - { + public Object createPoints(PointList points, boolean pointsEncoded, boolean includeElevation) { if (pointsEncoded) return WebHelper.encodePolyline(points, includeElevation); diff --git a/web/src/main/java/com/graphhopper/http/WebHelper.java b/web/src/main/java/com/graphhopper/http/WebHelper.java index 1dbd2417eff..4bc566a7bef 100644 --- a/web/src/main/java/com/graphhopper/http/WebHelper.java +++ b/web/src/main/java/com/graphhopper/http/WebHelper.java @@ -20,13 +20,13 @@ import com.graphhopper.util.PointList; import com.graphhopper.util.shapes.GHPoint; import com.graphhopper.util.shapes.GHPoint3D; +import org.json.JSONArray; import java.io.BufferedInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.URLEncoder; -import org.json.JSONArray; /** * Code which handles polyline encoding and other web stuff. @@ -35,33 +35,27 @@ * http://stackoverflow.com/a/24510799/194609 with a link to official Java sources as well as to a * good explanation. *

    + * * @author Peter Karich */ -public class WebHelper -{ - public static String encodeURL( String str ) - { - try - { +public class WebHelper { + public static String encodeURL(String str) { + try { return URLEncoder.encode(str, "UTF-8"); - } catch (Exception _ignore) - { + } catch (Exception _ignore) { return str; } } - public static PointList decodePolyline( String encoded, int initCap, boolean is3D ) - { + public static PointList decodePolyline(String encoded, int initCap, boolean is3D) { PointList poly = new PointList(initCap, is3D); int index = 0; int len = encoded.length(); int lat = 0, lng = 0, ele = 0; - while (index < len) - { + while (index < len) { // latitude int b, shift = 0, result = 0; - do - { + do { b = encoded.charAt(index++) - 63; result |= (b & 0x1f) << shift; shift += 5; @@ -72,8 +66,7 @@ public static PointList decodePolyline( String encoded, int initCap, boolean is3 // longitute shift = 0; result = 0; - do - { + do { b = encoded.charAt(index++) - 63; result |= (b & 0x1f) << shift; shift += 5; @@ -81,13 +74,11 @@ public static PointList decodePolyline( String encoded, int initCap, boolean is3 int deltaLongitude = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1)); lng += deltaLongitude; - if (is3D) - { + if (is3D) { // elevation shift = 0; result = 0; - do - { + do { b = encoded.charAt(index++) - 63; result |= (b & 0x1f) << shift; shift += 5; @@ -101,31 +92,27 @@ public static PointList decodePolyline( String encoded, int initCap, boolean is3 return poly; } - public static String encodePolyline( PointList poly ) - { + public static String encodePolyline(PointList poly) { if (poly.isEmpty()) return ""; return encodePolyline(poly, poly.is3D()); } - public static String encodePolyline( PointList poly, boolean includeElevation ) - { + public static String encodePolyline(PointList poly, boolean includeElevation) { StringBuilder sb = new StringBuilder(); int size = poly.getSize(); int prevLat = 0; int prevLon = 0; int prevEle = 0; - for (int i = 0; i < size; i++) - { + for (int i = 0; i < size; i++) { int num = (int) Math.floor(poly.getLatitude(i) * 1e5); encodeNumber(sb, num - prevLat); prevLat = num; num = (int) Math.floor(poly.getLongitude(i) * 1e5); encodeNumber(sb, num - prevLon); prevLon = num; - if (includeElevation) - { + if (includeElevation) { num = (int) Math.floor(poly.getElevation(i) * 100); encodeNumber(sb, num - prevEle); prevEle = num; @@ -134,15 +121,12 @@ public static String encodePolyline( PointList poly, boolean includeElevation ) return sb.toString(); } - private static void encodeNumber( StringBuilder sb, int num ) - { + private static void encodeNumber(StringBuilder sb, int num) { num = num << 1; - if (num < 0) - { + if (num < 0) { num = ~num; } - while (num >= 0x20) - { + while (num >= 0x20) { int nextValue = (0x20 | (num & 0x1f)) + 63; sb.append((char) (nextValue)); num >>= 5; @@ -151,30 +135,24 @@ private static void encodeNumber( StringBuilder sb, int num ) sb.append((char) (num)); } - public static String readString( InputStream inputStream ) throws IOException - { + public static String readString(InputStream inputStream) throws IOException { String encoding = "UTF-8"; InputStream in = new BufferedInputStream(inputStream, 4096); - try - { + try { byte[] buffer = new byte[4096]; ByteArrayOutputStream output = new ByteArrayOutputStream(); int numRead; - while ((numRead = in.read(buffer)) != -1) - { + while ((numRead = in.read(buffer)) != -1) { output.write(buffer, 0, numRead); } return output.toString(encoding); - } finally - { + } finally { in.close(); } } - public static GHPoint toGHPoint( JSONArray point ) - { - if (point.length() == 3 && !Double.isNaN(point.getDouble(2))) - { + public static GHPoint toGHPoint(JSONArray point) { + if (point.length() == 3 && !Double.isNaN(point.getDouble(2))) { return new GHPoint3D(point.getDouble(1), point.getDouble(0), point.getDouble(2)); } diff --git a/web/src/main/webapp/js/gpxexport.js b/web/src/main/webapp/js/gpxexport.js index 1f3c0014880..40ddcc00826 100644 --- a/web/src/main/webapp/js/gpxexport.js +++ b/web/src/main/webapp/js/gpxexport.js @@ -10,8 +10,7 @@ var ensureOneCheckboxSelected = function () { if (!$("#gpx_waypoints").is(':checked')) $("#gpx_track").prop("disabled", true); } - } - else + } else { $("#gpx_track").prop("disabled", false); $("#gpx_waypoints").prop("disabled", false); @@ -26,8 +25,7 @@ var ensureOneCheckboxSelected = function () { if (!$("#gpx_waypoints").is(':checked')) $("#gpx_route").prop("disabled", true); } - } - else + } else { $("#gpx_route").prop("disabled", false); $("#gpx_waypoints").prop("disabled", false); @@ -42,8 +40,7 @@ var ensureOneCheckboxSelected = function () { if (!$("#gpx_track").is(':checked')) $("#gpx_route").prop("disabled", true); } - } - else + } else { $("#gpx_route").prop("disabled", false); $("#gpx_track").prop("disabled", false); diff --git a/web/src/main/webapp/js/map.js b/web/src/main/webapp/js/map.js index 111948b2cc3..8e64baa4ab4 100644 --- a/web/src/main/webapp/js/map.js +++ b/web/src/main/webapp/js/map.js @@ -49,7 +49,7 @@ function initMap(bounds, setStartCoord, setIntermediateCoord, setEndCoord, selec // default map = L.map('map', { layers: [defaultLayer], - minZoom : 2, + minZoom: 2, contextmenu: true, contextmenuWidth: 150, contextmenuItems: [{ @@ -279,7 +279,7 @@ module.exports.getMap = function () { module.exports.updateScale = function (useMiles) { if (scaleControl === null) { - return; + return; } scaleControl.removeFrom(map); var options = useMiles ? {metric: false} : {imperial: false}; diff --git a/web/src/test/java/com/graphhopper/http/BaseServletTester.java b/web/src/test/java/com/graphhopper/http/BaseServletTester.java index 021b5d5e1b7..38fd1178653 100644 --- a/web/src/test/java/com/graphhopper/http/BaseServletTester.java +++ b/web/src/test/java/com/graphhopper/http/BaseServletTester.java @@ -34,31 +34,39 @@ /** * @author Peter Karich */ -public class BaseServletTester -{ - private static GHServer server; +public class BaseServletTester { protected static final Logger logger = LoggerFactory.getLogger(BaseServletTester.class); protected static int port; + private static GHServer server; protected Injector injector; - public void setUpGuice( Module... modules ) - { + public static void shutdownJetty(boolean force) { + // this is too slow so allow force == false. Then on setUpJetty a new server is created on a different port + if (force && server != null) + try { + server.stop(); + } catch (Exception ex) { + logger.error("Cannot stop jetty", ex); + } + + server = null; + } + + public void setUpGuice(Module... modules) { injector = Guice.createInjector(/*Stage.DEVELOPMENT,*/modules); } /** * This method will start jetty with andorra area loaded as OSM. */ - public void setUpJetty( CmdArgs args ) - { + public void setUpJetty(CmdArgs args) { if (injector != null) throw new UnsupportedOperationException("do not call guice before"); bootJetty(args, 3); } - private void bootJetty( CmdArgs args, int retryCount ) - { + private void bootJetty(CmdArgs args, int retryCount) { if (server != null) return; @@ -67,56 +75,34 @@ private void bootJetty( CmdArgs args, int retryCount ) if (injector == null) setUpGuice(new DefaultModule(args), new GHServletModule(args)); - for (int i = 0; i < retryCount; i++) - { + for (int i = 0; i < retryCount; i++) { port = 18080 + i; args.put("jetty.port", "" + port); - try - { + try { logger.info("Trying to start jetty at port " + port); server.start(injector); // server.join(); break; - } catch (Exception ex) - { + } catch (Exception ex) { server = null; logger.error("Cannot start jetty at port " + port + " " + ex.getMessage()); } } } - public static void shutdownJetty( boolean force ) - { - // this is too slow so allow force == false. Then on setUpJetty a new server is created on a different port - if (force && server != null) - try - { - server.stop(); - } catch (Exception ex) - { - logger.error("Cannot stop jetty", ex); - } - - server = null; - } - - protected String getTestRouteAPIUrl() - { + protected String getTestRouteAPIUrl() { String host = "localhost"; return "http://" + host + ":" + port + "/route"; } - protected String getTestNearestAPIUrl() - { + protected String getTestNearestAPIUrl() { String host = "localhost"; return "http://" + host + ":" + port + "/nearest"; } - protected String queryString( String query, int code ) throws Exception - { + protected String queryString(String query, int code) throws Exception { String resQuery = ""; - for (String q : query.split("\\&")) - { + for (String q : query.split("\\&")) { int index = q.indexOf("="); if (index > 0) resQuery += q.substring(0, index + 1) + WebHelper.encodeURL(q.substring(index + 1)); @@ -133,16 +119,13 @@ protected String queryString( String query, int code ) throws Exception return Helper.isToString(downloader.fetch(conn, true)); } - protected JSONObject query( String query, int code ) throws Exception - { + protected JSONObject query(String query, int code) throws Exception { return new JSONObject(queryString(query, code)); } - protected JSONObject nearestQuery( String query ) throws Exception - { + protected JSONObject nearestQuery(String query) throws Exception { String resQuery = ""; - for (String q : query.split("\\&")) - { + for (String q : query.split("\\&")) { int index = q.indexOf("="); if (index > 0) resQuery += q.substring(0, index + 1) + WebHelper.encodeURL(q.substring(index + 1)); diff --git a/web/src/test/java/com/graphhopper/http/GraphHopperServletIT.java b/web/src/test/java/com/graphhopper/http/GraphHopperServletIT.java index 2cd0bf86724..5516358324a 100644 --- a/web/src/test/java/com/graphhopper/http/GraphHopperServletIT.java +++ b/web/src/test/java/com/graphhopper/http/GraphHopperServletIT.java @@ -17,10 +17,10 @@ */ package com.graphhopper.http; -import com.graphhopper.PathWrapper; import com.graphhopper.GHRequest; import com.graphhopper.GHResponse; import com.graphhopper.GraphHopperAPI; +import com.graphhopper.PathWrapper; import com.graphhopper.util.CmdArgs; import com.graphhopper.util.Helper; import com.graphhopper.util.exceptions.PointOutOfBoundsException; @@ -39,20 +39,17 @@ /** * @author Peter Karich */ -public class GraphHopperServletIT extends BaseServletTester -{ +public class GraphHopperServletIT extends BaseServletTester { private static final String DIR = "./target/andorra-gh/"; @AfterClass - public static void cleanUp() - { + public static void cleanUp() { Helper.removeDir(new File(DIR)); shutdownJetty(true); } @Before - public void setUp() - { + public void setUp() { CmdArgs args = new CmdArgs(). put("config", "../config-example.properties"). put("datareader.file", "../core/files/andorra.osm.pbf"). @@ -61,8 +58,7 @@ public void setUp() } @Test - public void testBasicQuery() throws Exception - { + public void testBasicQuery() throws Exception { JSONObject json = query("point=42.554851,1.536198&point=42.510071,1.548128", 200); JSONObject infoJson = json.getJSONObject("info"); assertFalse(infoJson.has("errors")); @@ -73,8 +69,7 @@ public void testBasicQuery() throws Exception } @Test - public void testQueryWithDirections() throws Exception - { + public void testQueryWithDirections() throws Exception { // Note, in general specifying directions does not work with CH, but this is an example where it works JSONObject json = query("point=42.496696,1.499323&point=42.497257,1.501501&heading=240&heading=240&ch.force_heading=true", 200); JSONObject infoJson = json.getJSONObject("info"); @@ -86,8 +81,7 @@ public void testQueryWithDirections() throws Exception } @Test - public void testQueryWithStraightVia() throws Exception - { + public void testQueryWithStraightVia() throws Exception { // Note, in general specifying straightvia does not work with CH, but this is an example where it works JSONObject json = query( "point=42.534133,1.581473&point=42.534781,1.582149&point=42.535042,1.582514&pass_through=true", 200); @@ -100,16 +94,14 @@ public void testQueryWithStraightVia() throws Exception } @Test - public void testJsonRounding() throws Exception - { + public void testJsonRounding() throws Exception { JSONObject json = query("point=42.554851234,1.536198&point=42.510071,1.548128&points_encoded=false", 200); JSONObject cson = json.getJSONArray("paths").getJSONObject(0).getJSONObject("points"); assertTrue("unexpected precision!", cson.toString().contains("[1.536374,42.554839]")); } @Test - public void testFailIfElevationRequestedButNotIncluded() throws Exception - { + public void testFailIfElevationRequestedButNotIncluded() throws Exception { JSONObject json = query("point=42.554851234,1.536198&point=42.510071,1.548128&points_encoded=false&elevation=true", 400); assertTrue(json.has("message")); assertEquals("Elevation not supported!", json.get("message")); @@ -117,8 +109,7 @@ public void testFailIfElevationRequestedButNotIncluded() throws Exception } @Test - public void testGraphHopperWeb() throws Exception - { + public void testGraphHopperWeb() throws Exception { GraphHopperAPI hopper = new GraphHopperWeb(); assertTrue(hopper.load(getTestRouteAPIUrl())); GHResponse rsp = hopper.route(new GHRequest(42.554851, 1.536198, 42.510071, 1.548128)); @@ -145,8 +136,7 @@ public void testGraphHopperWeb() throws Exception } @Test - public void testGraphHopperWebRealExceptions() - { + public void testGraphHopperWebRealExceptions() { GraphHopperAPI hopper = new GraphHopperWeb(); assertTrue(hopper.load(getTestRouteAPIUrl())); @@ -163,8 +153,7 @@ public void testGraphHopperWebRealExceptions() assertFalse("Errors expected but not found.", rsp.getErrors().isEmpty()); List errs = rsp.getErrors(); - for (int i = 0; i < errs.size(); i++) - { + for (int i = 0; i < errs.size(); i++) { assertEquals(((PointOutOfBoundsException) errs.get(i)).getPointIndex(), i); } @@ -178,8 +167,7 @@ public void testGraphHopperWebRealExceptions() } @Test - public void testGPX() throws Exception - { + public void testGPX() throws Exception { String str = queryString("point=42.554851,1.536198&point=42.510071,1.548128&type=gpx", 200); // For backward compatibility we currently export route and track. assertTrue(str.contains("115.1")); @@ -188,8 +176,7 @@ public void testGPX() throws Exception } @Test - public void testGPXWithExcludedRouteSelection() throws Exception - { + public void testGPXWithExcludedRouteSelection() throws Exception { String str = queryString("point=42.554851,1.536198&point=42.510071,1.548128&type=gpx&gpx.route=false&gpx.waypoints=false", 200); assertFalse(str.contains("115.1")); assertFalse(str.contains(" Finish!")); @@ -197,8 +184,7 @@ public void testGPXWithExcludedRouteSelection() throws Exception } @Test - public void testGPXWithTrackAndWaypointsSelection() throws Exception - { + public void testGPXWithTrackAndWaypointsSelection() throws Exception { String str = queryString("point=42.554851,1.536198&point=42.510071,1.548128&type=gpx&gpx.track=true&gpx.route=false&gpx.waypoints=true", 200); assertFalse(str.contains("115.1")); assertTrue(str.contains(" Finish!")); @@ -206,8 +192,7 @@ public void testGPXWithTrackAndWaypointsSelection() throws Exception } @Test - public void testGPXWithError() throws Exception - { + public void testGPXWithError() throws Exception { String str = queryString("point=42.554851,1.536198&type=gpx", 400); assertFalse(str, str.contains("")); assertFalse(str, str.contains("{")); diff --git a/web/src/test/java/com/graphhopper/http/GraphHopperServletWithEleIT.java b/web/src/test/java/com/graphhopper/http/GraphHopperServletWithEleIT.java index 409879770ca..4f6d81065d6 100644 --- a/web/src/test/java/com/graphhopper/http/GraphHopperServletWithEleIT.java +++ b/web/src/test/java/com/graphhopper/http/GraphHopperServletWithEleIT.java @@ -19,33 +19,29 @@ import com.graphhopper.util.CmdArgs; import com.graphhopper.util.Helper; - -import java.io.File; - import org.json.JSONObject; import org.junit.AfterClass; import org.junit.Before; import org.junit.Test; +import java.io.File; + import static org.junit.Assert.*; /** * @author Peter Karich */ -public class GraphHopperServletWithEleIT extends BaseServletTester -{ +public class GraphHopperServletWithEleIT extends BaseServletTester { private static final String dir = "./target/monaco-gh/"; @AfterClass - public static void cleanUp() - { + public static void cleanUp() { Helper.removeDir(new File(dir)); shutdownJetty(true); } @Before - public void setUp() - { + public void setUp() { CmdArgs args = new CmdArgs(). put("graph.elevation.provider", "srtm"). put("graph.elevation.cachedir", "../core/files/"). @@ -58,8 +54,7 @@ public void setUp() } @Test - public void testElevation() throws Exception - { + public void testElevation() throws Exception { JSONObject json = query("point=43.730864,7.420771&point=43.727687,7.418737&points_encoded=false&elevation=true", 200); JSONObject infoJson = json.getJSONObject("info"); assertFalse(infoJson.has("errors")); @@ -77,8 +72,7 @@ public void testElevation() throws Exception } @Test - public void testNoElevation() throws Exception - { + public void testNoElevation() throws Exception { // default is elevation=false JSONObject json = query("point=43.730864,7.420771&point=43.727687,7.418737&points_encoded=false", 200); JSONObject infoJson = json.getJSONObject("info"); diff --git a/web/src/test/java/com/graphhopper/http/GraphHopperWebTest.java b/web/src/test/java/com/graphhopper/http/GraphHopperWebTest.java index 423b0e14240..0b54f07b142 100644 --- a/web/src/test/java/com/graphhopper/http/GraphHopperWebTest.java +++ b/web/src/test/java/com/graphhopper/http/GraphHopperWebTest.java @@ -17,35 +17,30 @@ */ package com.graphhopper.http; -import com.graphhopper.PathWrapper; import com.graphhopper.GHRequest; import com.graphhopper.GHResponse; +import com.graphhopper.PathWrapper; import com.graphhopper.util.Downloader; import com.graphhopper.util.Helper; +import org.junit.Test; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; -import org.junit.Test; - import static org.junit.Assert.*; /** * @author Peter Karich */ -public class GraphHopperWebTest -{ +public class GraphHopperWebTest { // see also GraphHopperServletIT.testGraphHopperWeb for real routes against local jetty service @Test - public void testReadEncoded() throws Exception - { - Downloader downloader = new Downloader("GraphHopper Test") - { + public void testReadEncoded() throws Exception { + Downloader downloader = new Downloader("GraphHopper Test") { @Override - public InputStream fetch( HttpURLConnection conn, boolean readErrorStreamNoException ) throws IOException - { + public InputStream fetch(HttpURLConnection conn, boolean readErrorStreamNoException) throws IOException { return getClass().getResourceAsStream("test_encoded.json"); } }; @@ -65,13 +60,10 @@ public InputStream fetch( HttpURLConnection conn, boolean readErrorStreamNoExcep } @Test - public void testCreateURL() throws Exception - { - Downloader downloader = new Downloader("GraphHopper Test") - { + public void testCreateURL() throws Exception { + Downloader downloader = new Downloader("GraphHopper Test") { @Override - public String downloadAsString( String url, boolean readErrorStreamNoException ) throws IOException - { + public String downloadAsString(String url, boolean readErrorStreamNoException) throws IOException { assertFalse(url.contains("xy")); assertFalse(url.contains("algo1")); assertTrue(url.contains("alternative_route.max_paths=4")); diff --git a/web/src/test/java/com/graphhopper/http/IPFilterTest.java b/web/src/test/java/com/graphhopper/http/IPFilterTest.java index a5a61bcb3d2..f4153871449 100644 --- a/web/src/test/java/com/graphhopper/http/IPFilterTest.java +++ b/web/src/test/java/com/graphhopper/http/IPFilterTest.java @@ -19,16 +19,15 @@ import org.junit.Test; -import static org.junit.Assert.*; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; /** * @author Peter Karich */ -public class IPFilterTest -{ +public class IPFilterTest { @Test - public void testAcceptWhite() - { + public void testAcceptWhite() { IPFilter instance = new IPFilter("1.2.3.4, 4.5.67.1", ""); assertTrue(instance.accept("1.2.3.4")); assertTrue(instance.accept("4.5.67.1")); @@ -45,8 +44,7 @@ public void testAcceptWhite() } @Test - public void testAcceptBlack() - { + public void testAcceptBlack() { IPFilter instance = new IPFilter("", "1.2.3.4, 4.5.67.1"); assertFalse(instance.accept("1.2.3.4")); @@ -55,17 +53,14 @@ public void testAcceptBlack() } @Test - public void testFilterSpecialCases() - { + public void testFilterSpecialCases() { IPFilter instance = new IPFilter("", ""); assertTrue(instance.accept("1.2.3.4")); - try - { + try { new IPFilter("1.2.3.4, 4.5.67.1", "8.9.7.3"); assertFalse("black and white", true); - } catch (Exception ex) - { + } catch (Exception ex) { } } diff --git a/web/src/test/java/com/graphhopper/http/NearestServletIT.java b/web/src/test/java/com/graphhopper/http/NearestServletIT.java index f73156f2d66..1cdd7c6f572 100644 --- a/web/src/test/java/com/graphhopper/http/NearestServletIT.java +++ b/web/src/test/java/com/graphhopper/http/NearestServletIT.java @@ -17,38 +17,33 @@ */ package com.graphhopper.http; -import static com.graphhopper.http.BaseServletTester.shutdownJetty; - import com.graphhopper.util.CmdArgs; import com.graphhopper.util.Helper; - -import java.io.File; - import org.json.JSONArray; import org.json.JSONObject; import org.junit.AfterClass; import org.junit.Before; import org.junit.Test; -import static org.junit.Assert.*; +import java.io.File; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; /** * @author svantulden */ -public class NearestServletIT extends BaseServletTester -{ +public class NearestServletIT extends BaseServletTester { private static final String dir = "./target/andorra-gh/"; @AfterClass - public static void cleanUp() - { + public static void cleanUp() { Helper.removeDir(new File(dir)); shutdownJetty(true); } @Before - public void setUp() - { + public void setUp() { CmdArgs args = new CmdArgs(). put("config", "../config-example.properties"). put("datareader.file", "../core/files/andorra.osm.pbf"). @@ -57,8 +52,7 @@ public void setUp() } @Test - public void testBasicNearestQuery() throws Exception - { + public void testBasicNearestQuery() throws Exception { JSONObject json = nearestQuery("point=42.554851,1.536198"); assertFalse(json.has("error")); JSONArray point = json.getJSONArray("coordinates"); diff --git a/web/src/test/java/com/graphhopper/http/NearestServletWithEleIT.java b/web/src/test/java/com/graphhopper/http/NearestServletWithEleIT.java index 5eb838c8970..9bd1d4b9c46 100644 --- a/web/src/test/java/com/graphhopper/http/NearestServletWithEleIT.java +++ b/web/src/test/java/com/graphhopper/http/NearestServletWithEleIT.java @@ -17,38 +17,33 @@ */ package com.graphhopper.http; -import static com.graphhopper.http.BaseServletTester.shutdownJetty; - import com.graphhopper.util.CmdArgs; import com.graphhopper.util.Helper; - -import java.io.File; - import org.json.JSONArray; import org.json.JSONObject; import org.junit.AfterClass; import org.junit.Before; import org.junit.Test; -import static org.junit.Assert.*; +import java.io.File; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; /** * @author svantulden */ -public class NearestServletWithEleIT extends BaseServletTester -{ +public class NearestServletWithEleIT extends BaseServletTester { private static final String dir = "./target/monaco-gh/"; @AfterClass - public static void cleanUp() - { + public static void cleanUp() { Helper.removeDir(new File(dir)); shutdownJetty(true); } @Before - public void setUp() - { + public void setUp() { CmdArgs args = new CmdArgs(). put("graph.elevation.provider", "srtm"). put("graph.elevation.cachedir", "../core/files/"). @@ -61,8 +56,7 @@ public void setUp() } @Test - public void testWithEleQuery() throws Exception - { + public void testWithEleQuery() throws Exception { JSONObject json = nearestQuery("point=43.730864,7.420771&elevation=true"); assertFalse(json.has("error")); JSONArray point = json.getJSONArray("coordinates"); @@ -74,8 +68,7 @@ public void testWithEleQuery() throws Exception } @Test - public void testWithoutEleQuery() throws Exception - { + public void testWithoutEleQuery() throws Exception { JSONObject json = nearestQuery("point=43.730864,7.420771&elevation=false"); assertFalse(json.has("error")); JSONArray point = json.getJSONArray("coordinates"); diff --git a/web/src/test/java/com/graphhopper/http/WebHelperTest.java b/web/src/test/java/com/graphhopper/http/WebHelperTest.java index a3885815b98..1d526b6fcc3 100644 --- a/web/src/test/java/com/graphhopper/http/WebHelperTest.java +++ b/web/src/test/java/com/graphhopper/http/WebHelperTest.java @@ -21,16 +21,14 @@ import com.graphhopper.util.PointList; import org.junit.Test; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; /** * @author Peter Karich */ -public class WebHelperTest -{ +public class WebHelperTest { @Test - public void testDecode() throws Exception - { + public void testDecode() throws Exception { PointList list = WebHelper.decodePolyline("_p~iF~ps|U", 1, false); assertEquals(Helper.createPointList(38.5, -120.2), list); @@ -39,8 +37,7 @@ public void testDecode() throws Exception } @Test - public void testEncode() throws Exception - { + public void testEncode() throws Exception { assertEquals("_p~iF~ps|U", WebHelper.encodePolyline( Helper.createPointList(38.5, -120.2))); @@ -49,8 +46,7 @@ public void testEncode() throws Exception } @Test - public void testBoth() throws Exception - { + public void testBoth() throws Exception { PointList list = Helper.createPointList(38.5, -120.2, 43.252, -126.453, 40.7, -120.95, 50.3139, 10.61279, 50.04303, 9.49768); String str = WebHelper.encodePolyline(list); @@ -63,8 +59,7 @@ public void testBoth() throws Exception } @Test - public void testDecode3D() throws Exception - { + public void testDecode3D() throws Exception { PointList list = WebHelper.decodePolyline("_p~iF~ps|Uo}@", 1, true); assertEquals(Helper.createPointList3D(38.5, -120.2, 10), list); @@ -73,8 +68,7 @@ public void testDecode3D() throws Exception } @Test - public void testEncode3D() throws Exception - { + public void testEncode3D() throws Exception { assertEquals("_p~iF~ps|Uo}@", WebHelper.encodePolyline(Helper.createPointList3D(38.5, -120.2, 10))); assertEquals("_p~iF~ps|Uo}@_ulLnnqC_anF_mqNvxq`@?", WebHelper.encodePolyline( Helper.createPointList3D(38.5, -120.2, 10, 40.7, -120.95, 1234, 43.252, -126.453, 1234))); diff --git a/web/src/test/webapp/spec/translateSpec.js b/web/src/test/webapp/spec/translateSpec.js index be8d88629af..6b45f53f81b 100644 --- a/web/src/test/webapp/spec/translateSpec.js +++ b/web/src/test/webapp/spec/translateSpec.js @@ -8,7 +8,7 @@ describe('translation', function () { defaultTranslationMap["ft_abbr"] = 'ft'; defaultTranslationMap["mi_abbr"] = 'mi'; - translate.init({ default: defaultTranslationMap }); + translate.init({default: defaultTranslationMap}); expect(translate.createDistanceString(877.34)).toBe("877m"); expect(translate.createDistanceString(1877.34)).toBe("1.88km"); @@ -22,12 +22,12 @@ describe('translation', function () { expect(translate.createDistanceString(162543.74, true)).toBe("101mi") }); - it("should format elevation string correctly", function() { + it("should format elevation string correctly", function () { var defaultTranslationMap = {}; defaultTranslationMap["m_abbr"] = 'm'; defaultTranslationMap["ft_abbr"] = 'ft'; - translate.init({ default: defaultTranslationMap }); + translate.init({default: defaultTranslationMap}); expect(translate.createEleInfoString(156.3, 324.6, true)).toBe("
    ↗513ft ↘1065ft"); expect(translate.createEleInfoString(156.3, 324.6, false)).toBe("
    ↗156m ↘325m"); @@ -40,7 +40,7 @@ describe('translation', function () { defaultTranslationMap["hour_abbr"] = 'h'; defaultTranslationMap["day_abbr"] = 'd'; - translate.init({ default: defaultTranslationMap }); + translate.init({default: defaultTranslationMap}); expect(translate.createTimeString(10773331)).toBe("2h 59min"); @@ -55,19 +55,19 @@ describe('translation', function () { // toBe, toBeTruthy, toBeFalsy var defaultTranslationMap = {}; defaultTranslationMap["web.somekey1"] = "%s wow %s"; - translate.init({ default: defaultTranslationMap }); + translate.init({default: defaultTranslationMap}); expect(translate.tr("somekey1", ["nice", "to"])).toBe("nice wow to"); defaultTranslationMap["web.somekey2"] = "%2$s wow %1$s"; - translate.init({ default: defaultTranslationMap }); + translate.init({default: defaultTranslationMap}); expect(translate.tr("somekey2", ["nice", "to"])).toBe("to wow nice"); defaultTranslationMap["web.key"] = "it will take %1$s"; - translate.init({ default: defaultTranslationMap }); + translate.init({default: defaultTranslationMap}); expect(translate.tr("key", "2min")).toBe("it will take 2min"); defaultTranslationMap["web.key"] = "%1$s%2$s werden %3$s brauchen"; - translate.init({ default: defaultTranslationMap }); + translate.init({default: defaultTranslationMap}); expect(translate.tr("key", [200, "km", "2min"])).toBe("200km werden 2min brauchen"); }); });