-
Notifications
You must be signed in to change notification settings - Fork 31
/
S2Wrapper.java
40 lines (33 loc) · 1.66 KB
/
S2Wrapper.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import com.google.common.geometry.*;
import java.math.BigInteger;
public class S2Wrapper{
private final static double EARTH_RADIUS = 6371000;
public static double GreatEarthDistance(S2LatLng a, S2LatLng b){
//double angle = Haversin(a.latDegrees() - b.latDegrees()) + Math.cos(a.latDegrees()) * Math.cos(b.latDegrees()) * Haversin(a.lngDegrees() - b.lngDegrees());
double angle = Haversin(a.latRadians() - b.latRadians()) + Math.cos(a.latRadians()) * Math.cos(b.latRadians()) * Haversin(a.lngRadians() - b.lngRadians());
double ret = 2 * EARTH_RADIUS * Math.asin(Math.sqrt(angle));
return ret;
}
public static double Haversin(double a){
return ((1 - Math.cos(a))/2);
}
public static S2LatLng decodeLocation(String locationString){
String[] locationStringArray = locationString.split(",");
BigInteger lat = new BigInteger(locationStringArray[0],16);
BigInteger lng = new BigInteger(locationStringArray[1],16);
double dlat = (double) lat.intValue() / 1000000;
double dlng = (double) lng.intValue() / 1000000;
S2LatLng ret = S2LatLng.fromDegrees(dlat, dlng);
return ret;
}
public static String encodeLocation(S2LatLng s2ll){
int lat = (int) (s2ll.latDegrees() * 1000000);
int lng = (int) (s2ll.lngDegrees() * 1000000);
String ret = String.format("%08x,%08x", Integer.valueOf(lat), Integer.valueOf(lng));
return ret;
}
public static String getLocationString(S2LatLng s2ll){
String ret = "" + Double.toString(s2ll.latDegrees()) + "," + Double.toString(s2ll.lngDegrees());
return ret;
}
}