diff --git a/jsprit-core/src/main/java/jsprit/core/util/GreatCircleDistanceCalculator.java b/jsprit-core/src/main/java/jsprit/core/util/GreatCircleDistanceCalculator.java new file mode 100644 index 00000000..3e90aeda --- /dev/null +++ b/jsprit-core/src/main/java/jsprit/core/util/GreatCircleDistanceCalculator.java @@ -0,0 +1,55 @@ +/******************************************************************************* + * Copyright (C) 2014 Stefan Schroeder + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3.0 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see . + ******************************************************************************/ + +package jsprit.core.util; + +/** + * Created by schroeder on 28.11.14. + */ +public class GreatCircleDistanceCalculator { + + private static final double R = 6372.8; // km + + /** + * Harversine method. + * + * double lon1 = coord1.getX(); + * double lon2 = coord2.getX(); + * double lat1 = coord1.getY(); + * double lat2 = coord2.getY(); + * + * @param coord1 - from coord + * @param coord2 - to coord + * @return great circle distance + */ + public static double calculateDistance(Coordinate coord1, Coordinate coord2){ + double lon1 = coord1.getX(); + double lon2 = coord2.getX(); + double lat1 = coord1.getY(); + double lat2 = coord2.getY(); + + double delta_Lat = Math.toRadians(lat2 - lat1); + double delta_Lon = Math.toRadians(lon2 - lon1); + lat1 = Math.toRadians(lat1); + lat2 = Math.toRadians(lat2); + + double a = Math.sin(delta_Lat / 2) * Math.sin(delta_Lat / 2) + Math.sin(delta_Lon / 2) * Math.sin(delta_Lon / 2) * Math.cos(lat1) * Math.cos(lat2); + double c = 2 * Math.asin(Math.sqrt(a)); + return R * c; + } + +} diff --git a/jsprit-core/src/main/java/jsprit/core/util/ManhattanDistanceCalculator.java b/jsprit-core/src/main/java/jsprit/core/util/ManhattanDistanceCalculator.java index 7059bdba..90acf6e6 100644 --- a/jsprit-core/src/main/java/jsprit/core/util/ManhattanDistanceCalculator.java +++ b/jsprit-core/src/main/java/jsprit/core/util/ManhattanDistanceCalculator.java @@ -1,16 +1,16 @@ /******************************************************************************* - * Copyright (C) 2013 Stefan Schroeder - * + * Copyright (C) 2014 Stefan Schroeder + * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either + * License as published by the Free Software Foundation; either * version 3.0 of the License, or (at your option) any later version. - * + * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. - * + * * You should have received a copy of the GNU Lesser General Public * License along with this library. If not, see . ******************************************************************************/ @@ -21,9 +21,8 @@ package jsprit.core.util; public class ManhattanDistanceCalculator { public static double calculateDistance(Coordinate coord1, Coordinate coord2) { - double distance = Math.abs(coord1.getX() - coord2.getX()) + return Math.abs(coord1.getX() - coord2.getX()) + Math.abs(coord1.getY() - coord2.getY()); - return distance; } } diff --git a/jsprit-core/src/test/java/jsprit/core/util/GreatCircleDistanceCalculatorTest.java b/jsprit-core/src/test/java/jsprit/core/util/GreatCircleDistanceCalculatorTest.java new file mode 100644 index 00000000..88c63278 --- /dev/null +++ b/jsprit-core/src/test/java/jsprit/core/util/GreatCircleDistanceCalculatorTest.java @@ -0,0 +1,44 @@ +/******************************************************************************* + * Copyright (C) 2014 Stefan Schroeder + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3.0 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see . + ******************************************************************************/ + +package jsprit.core.util; + +import junit.framework.Assert; +import org.junit.Test; + +/** + * Created by schroeder on 28.11.14. + */ +public class GreatCircleDistanceCalculatorTest { + + @Test + public void test(){ + double lon1 = 8.3858333; + double lat1 = 49.0047222; + + double lon2 = 12.1333333; + double lat2 = 54.0833333; + + double greatCircle = GreatCircleDistanceCalculator.calculateDistance( + Coordinate.newInstance(lon1,lat1), + Coordinate.newInstance(lon2,lat2) + ); + Assert.assertEquals(600,greatCircle,30.); + } + + +}