# Coordinate conversions#

Boule’s `Ellipsoid` and `Sphere` classes can be used with pymap3d for converting between different coordinate systems. While pymap3d defines some ellipsoids internally, you may want to use one from Boule if:

• You want to be certain that the parameters used for coordinate conversions and gravity calculations are consistent.

• Need to define your own ellipsoid either because you need different parameters than the built-in ones or they aren’t available in either Boule or pymap3d.

Help!

If an ellipsoid you need isn’t in Boule yet, please reach out to the team and consider adding it yourself. It requires no special knowledge of the code and is a great way to help the project!

## Geodetic to spherical#

The example below will show you how to convert geodetic latitude and height into geocentric spherical latitude and radius.

```import boule as bl
import pymap3d
import numpy as np

latitude = np.linspace(-90, 90, 45)
longitude = 40
height = 481_000  # ICESat-2 orbit height in meters

latitude_sph, longitude_sph, radius = pymap3d.geodetic2spherical(
latitude, longitude, height, ell=bl.WGS84,
)
print("Geodetic latitude:", latitude)
print("Spherical latitude:", latitude_sph)
print()
print("Geodetic longitude:", longitude)
print("Spherical longitude:", longitude_sph)
print()
print("Height (m):", height)
```
```Geodetic latitude: [-90.         -85.90909091 -81.81818182 -77.72727273 -73.63636364
-69.54545455 -65.45454545 -61.36363636 -57.27272727 -53.18181818
-49.09090909 -45.         -40.90909091 -36.81818182 -32.72727273
-28.63636364 -24.54545455 -20.45454545 -16.36363636 -12.27272727
-8.18181818  -4.09090909   0.           4.09090909   8.18181818
12.27272727  16.36363636  20.45454545  24.54545455  28.63636364
32.72727273  36.81818182  40.90909091  45.          49.09090909
53.18181818  57.27272727  61.36363636  65.45454545  69.54545455
73.63636364  77.72727273  81.81818182  85.90909091  90.        ]
Spherical latitude: [-90.         -85.88354756 -81.76762011 -77.65273153 -73.53937377
-69.42800654 -65.31904771 -61.21286467 -57.10976692 -53.00999983
-48.91373991 -44.82109151 -40.73208506 -36.6466769  -32.56475052
-28.4861193  -24.41053065 -20.33767131 -16.26717386 -12.19862422
-8.13156991  -4.06552908   0.           4.06552908   8.13156991
12.19862422  16.26717386  20.33767131  24.41053065  28.4861193
32.56475052  36.6466769   40.73208506  44.82109151  48.91373991
53.00999983  57.10976692  61.21286467  65.31904771  69.42800654
73.53937377  77.65273153  81.76762011  85.88354756  90.        ]

Geodetic longitude: 40
Spherical longitude: 40.0

Height (m): 481000
Radius (m): [6837752.31424518 6837862.00852276 6838188.80559622 6838725.89905453
6839462.11303663 6840382.14963169 6841466.92578056 6842693.99069968
6844038.01293239 6845471.32464188 6846964.50972864 6848487.02178921
6850007.81781447 6851495.99381081 6852921.40916165 6854255.28745742
6855470.78263883 6856543.50054635 6857451.9672834  6858178.03712615
6858707.23400555 6859029.02182043 6859137.         6859029.02182043
6858707.23400555 6858178.03712615 6857451.9672834  6856543.50054635
6855470.78263883 6854255.28745742 6852921.40916165 6851495.99381081
6850007.81781447 6848487.02178921 6846964.50972864 6845471.32464188
6844038.01293239 6842693.99069968 6841466.92578056 6840382.14963169
6839462.11303663 6838725.89905453 6838188.80559622 6837862.00852276
6837752.31424518]
```

Notice that:

1. The latitude is slightly different except for the poles and equator.

2. The longitude is the same in both coordinates systems.

3. The radius (distance from the center of the ellipsoid) varies even though the height is constant.

Tip

We used the WGS84 ellipsoid here but the workflow is the same for any other oblate ellipsoid or sphere. Checkout Available ellipsoids for options.

## Geodetic to Cartesian#

Another common coordinate conversion done in global studies is from geodetic latitude, longitude, and height to geocentric Cartesian X, Y, and Z. The example below performs this conversion for the location of the Insight lander on Mars based on [Parker2019]:

```X, Y, Z = pymap3d.geodetic2ecef(
lat=4.502384, lon=135.623447, alt=-2613.426, ell=bl.MARS,
)
print(f"X = {X} m")
print(f"Y = {Y} m")
print(f"Z = {Z} m")
```
```X = -2417638.770836119 m
Y = 2365589.4400994238 m
Z = 263566.88727812306 m
```