wordbotch.com

Plotting Latitude & Longitude in 3D space

This is a short post on the '3D globe' series of Android apps, Moon 3D, Mars 3D and Earth 3D. These three apps all share most of their code. Each uses a single texture image for the map, and loads a csv file of latitudes and longitudes for the locations. For example, below are the first few lines of the data file for the Mars 3D app. Each line consists of a feature's name and type, then its latitude and longitude. Negative latitudes are west of zero, negative longitudes are south of zero.

```Aeolis [Albedo Feature],-4.94,-35.0
Aeria [Albedo Feature],9.88,-130.0
Aetheria [Albedo Feature],39.67,-50.0
Aethiopis [Albedo Feature],9.88,-50.0
Amazonis [Albedo Feature],0.0,40.0
Amenthes [Albedo Feature],4.94,-70.0
```

Each line becomes a point on a 3D sphere with some basic trigonometry. Below is the Java code shared by the projects that does this. This function takes two float arguments - the latitude and longitude - and returns a Vector3f object representing the corresponding point on the surface of a sphere with a radius defined by the constant WORLD_RADIUS.

```
private Vector3f calculateTranslation(float lat, float lon) {
//have to convert from spherical coordinates to cartesian coordinates
double theta = Math.toRadians(lat - 90);
double phi = Math.toRadians(lon - 180);

double z = WORLD_RADIUS * Math.sin(theta) * Math.cos(phi);
double x = WORLD_RADIUS * Math.sin(theta) * Math.sin(phi);
double y = WORLD_RADIUS * Math.cos(theta);

return new Vector3f((float) x, (float) y, (float) z);
}

```