- Home /
Mapping co-ordinates from Google maps onto sphere.
I have a sphere in Unity which I've textured with (I believe) satellite images from NASA to get a relatively accurate image of the earth - I'd like to be able to pinpoint locations on this sphere using Long/Lat co-ordinates from Google Maps.
I found some info online on converting from spherical to cartesian co-ordinate systems, and tried to implement it like this:
Vector3 convertSphericalToCartesian(Vector2 latlong){
float earthRadius = sphere.radius; //SphereCollider with the correct radius
float lat = latlong.y*Mathf.Deg2Rad;
float lon = latlong.x*Mathf.Deg2Rad;
float x = earthRadius * Mathf.Cos(lat)*Mathf.Cos(lon);
float y = earthRadius * Mathf.Cos(lat)*Mathf.Sin(lon);
float z = earthRadius * Mathf.Sin(lat);
return new Vector3(x,y,z);
}
At first this seemed to work, but it only does when it is given either a longitude or a latitude, but not both. Otherwise it seems to drift really far from the correct location.
For example, at 0 , 50 it's correct:
or 50 , 0:
But at 50,50 it's wrong:
I just don't know what's going on here. I know that the earth is actually an ellipsoid rather than a sphere, and that that will cause some error in the positions, but I'm sure that isn't the issue here. If anyone has any idea what's happening, I could really use your help!
Thanks!
There are many different ways to project a curved 3D surface (i.e. your lat/long data) onto a flat 2D surface (i.e. your x/y). Some common map projections are $$anonymous$$ercator, Equirectangular, for example (see https://en.wikipedia.org/wiki/$$anonymous$$ap_projection).
However, by definition, all map projections must distort the data in some way - either its shape, area, or both. You won't get a lat/long point to line up with the same position on your map image unless you're using the same projection method by which the map was produced. Check the metadata of the NASA image you have to see how the map was flattened, and ensure you use the same method in your conversion formula.
Answer by Fredex8 · Mar 23, 2016 at 01:28 PM
In the map view Google Maps uses Mercator projection which is basically a representation of the Earth's surface unwrapped around a cylinder. Since Google Maps is in 2D it works well enough and looks good but it is quite inaccurate and stretches countries so they are bigger than in reality. If you switch back and forth between Maps and Earth view you will see countries appear to change shape as a result of the different projection methods.
The NASA Earth textures use Equirectangular projection which works far better for texturing spheres and creates a reasonable facsimile of the real Earth but does crunch things up at the poles.
When you are comparing maps that use two different projection techniques things will never match up that accurately and since the NASA maps become more compressed towards the poles these errors will be magnified at the extremes of south and north. This is especially noticeable for European countries.
The error you are getting with those coordinates in Russia does look too large to be fully explained by this but it is something you should be aware of. I'm not sure how you are moving from one point to the next but it looks like diagonal movement is resulting in the figures being essentially doubled. I've had something similar to that before.
Right, nevertheless you should be able to convert the coordinates to a meaningful representation in your used projection.
I guess that the vector should be new Vector3(x,z,y);
ins$$anonymous$$d as at least Unity's default sphere has it's "poles" on the y axis, not on the z axis. You may also need to flip one or more coordinates depending on the image and mesh layout. It's also possible that the mesh is somehow rotated. Usually lat / lon 0/0 should be at a normalized point (0,0,-1).
lat lon x y z
--------------------
0 0 0 0 -1
0 90 1 0 0
0 180 0 0 1
0 270 -1 0 0
90 0 0 1 0
-90 0 0 -1 0
There was this old question which asked basically the opposite: how to calculate the lat / lon from a given cartesian point. I've made a webplayer build for that.