# Work with lat, lon (EPSG: 4326) coordinates

Hi everyone,

I have data with lat, lon coords and I would like to visualize them in 3D space. Is there a way to set scene reference system to use a specific coordinate reference system (for example: EPSG: 4326) instead of the default, arbitrary one?

I thought that this would be a very common problem with a very straightforward solution.

I am quite new to Three.js. Any resources to read or any examples are more than welcome!

I had to deal with this for WGS84 coords before - I think thatâ€™s the same as EPSG: 4326?

The position is easy - you can map the center of your scene to any lat/long you choose, and you can pick Y=0 to be sea level or any other height you like.

Scale is also easy - three.js units are assumed to be meters, usually, but you can choose any scale you like. Just make sure it matches any models you are using and then you might need to scale your lat lon to match. But if possible just use meters, or possibly kilometers for large scenes.

The problem is rotation. I could never find a conversion formula but I did ask over on GIS Stackexchange and got a reply which I used to muddle through to a solution. But I donâ€™t have any immediate formula to give you, sorry.

If youâ€™re not tied to three.js you could try using Cesium instead, I assume they already have systems for converting geospatial coords to 3d.

1 Like

Also possibly related:

1 Like

It all depends on the precision you expect.

For a quick and rough solution, you could assume the shape of the earth to be a perfect sphere (which it isnâ€™t, but nevertheless). The formulas in this case are very simple and straightforward:

Knowing that the circumference of the earth at the equator is 40â€™000 km, you can compute the earthâ€™s Radius at the equator to be

`R = 40'000 / (2 * Ď€) = 6'366.1977 km`.

The radius at latitude â€ślatâ€ť equals to

r = R * cos( lat ).

Taking the equator (latitude = 0Â° and the Greenwich meridian (longitude = 0Â°) as reference, you can compute

`X = (r * 2 * Ď€) * (longitude / 2 * Ď€) = r * longitude [rad]`
(with the 1st bracket term being the circumference at latitude, and the 2nd bracket term being the fraction of the full circumference).

By the same reasoning, a full circumference along the 0Â° meridian is also 40â€™000 km, so the quarter circumference from equator to either of the poles is 10â€™000km.

So you can compute
Y = R * latitude [rad]

If you want to use this conversion to guide a cruise missile with [cm] precision into a target, obviously this method wonâ€™t be sufficient. But if you want to visualise a reasonably small patch of the earthâ€™s surface onto a flat plane, you can expect very good results with this approximation.

It should also be obvious, that you canâ€™t mix â€śdatumsâ€ť, that is: place markers using one datum onto a map which adheres to a different datum.

1 Like

Iâ€™m working on a project to
visualize OpenStreetMap buildings in 3d.(GitHub user Beakerboy, project OSMBuilding).

Since building footprints are so small, I convert to Cartesian-ish coordinates by first calculating the center of my object, the â€śhome pointâ€ť which the the halfway point between the object extents, min and max lat and long.

Then I rotate the object to place the home on the prime meridian. Finally I rotate to place the home at 0,0.

``````
/**
* Rotate lat/lon to reposition the home point onto 0,0.
* @param {array} latLong - [long, lat]
* @param {array} home - [long, lat] of the point which will end up at [0, 0]
* @return [x, y] Cartesian-ish coordinates in meters.
*/
static repositionPoint(latLon, home) {
const R = 6371 * 1000;   // Earth radius in m
const circ = 2 * Math.PI * R;  // Circumference
const phi = 90 - latLon[1];
const theta = latLon[0] - home[0];
const thetaPrime = home[1] / 180 * Math.PI;
const x = R * Math.sin(theta / 180 * Math.PI) * Math.sin(phi / 180 * Math.PI);
const y = R * Math.cos(phi / 180 * Math.PI);
const z = R * Math.sin(phi / 180 * Math.PI) * Math.cos(theta / 180 * Math.PI);
const abs = Math.sqrt(z**2 + y**2);
const arg = Math.atan(y / z) - thetaPrime;

return [x, Math.sin(arg) * abs];
}

``````

Iâ€™m certain there are unhandled edge conditions if the object traverses the international date line or surrounds one of the poles.

To my knowledge, there arenâ€™t any buildings covering the very poles. So I wouldnâ€™t worry too much about this remote possibility.

SCNR

My children would like to have a word with you.

2 Likes