mirror of
https://github.com/graphhopper/jsprit.git
synced 2020-01-24 07:45:05 +01:00
add great circle distance calc
This commit is contained in:
parent
600e29f353
commit
5d0a962e24
3 changed files with 105 additions and 7 deletions
|
|
@ -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 <http://www.gnu.org/licenses/>.
|
||||||
|
******************************************************************************/
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
/*******************************************************************************
|
/*******************************************************************************
|
||||||
* Copyright (C) 2013 Stefan Schroeder
|
* Copyright (C) 2014 Stefan Schroeder
|
||||||
*
|
*
|
||||||
* This library is free software; you can redistribute it and/or
|
* This library is free software; you can redistribute it and/or
|
||||||
* modify it under the terms of the GNU Lesser General Public
|
* modify it under the terms of the GNU Lesser General Public
|
||||||
|
|
@ -21,9 +21,8 @@ package jsprit.core.util;
|
||||||
public class ManhattanDistanceCalculator {
|
public class ManhattanDistanceCalculator {
|
||||||
|
|
||||||
public static double calculateDistance(Coordinate coord1, Coordinate coord2) {
|
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());
|
+ Math.abs(coord1.getY() - coord2.getY());
|
||||||
return distance;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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 <http://www.gnu.org/licenses/>.
|
||||||
|
******************************************************************************/
|
||||||
|
|
||||||
|
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.);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue