Skip to content

Instantly share code, notes, and snippets.

@ChathuraHettiarachchi
Last active August 3, 2018 13:36
Show Gist options
  • Save ChathuraHettiarachchi/c6ca23fb6116b5d2278f42962a908c55 to your computer and use it in GitHub Desktop.
Save ChathuraHettiarachchi/c6ca23fb6116b5d2278f42962a908c55 to your computer and use it in GitHub Desktop.
The World Geodetic System (WGS) is a standard for use in cartography, geodesy, and satellite navigation including GPS. It comprises a standard coordinate system for the Earth, a standard spheroidal reference surface (the datum or reference ellipsoid) for raw altitude data, and a gravitational equipotential surface (the geoid) that defines the no…
import java.lang.Math;
public class WGS84Converter{
// how to call methods
// int[] dataXY = convertLatLonToWGS84(-94.420307, 44.968046);
// System.out.println("DataXY: X="+dataXY[0]+" Y="+dataXY[1]);
// double[] dataLonLat = convertWGS84ToLatLon(dataXY[0], dataXY[1]);
// System.out.println("DataLotLan: X="+dataLonLat[0]+" Y="+dataLonLat[1]);
private static int[] convertLatLonToWGS84(double lon, double lat){
int x = (int)(lon * 20037508.34 / 180);
int y = (int)(Math.log(Math.tan((90 + lat) * Math.PI / 360)) / (Math.PI / 180));
y = (int)(y * 20037508.34 / 180);
return new int[] {x,y};
}
private static double[] convertWGS84ToLatLon(int x, int y){
double lon = (x / 20037508.34) * 180;
double lat = (y / 20037508.34) * 180;
lat = 180/Math.PI * (2 * Math.atan(Math.exp(lat * Math.PI / 180)) - Math.PI / 2);
return new double[] {lon,lat};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment