Python example: mrpt_topography_example.py

Geodetic, ECEF, ENU and UTM coordinate conversions with mrpt.topography.

Modules: mrpt.topography

 1#!/usr/bin/env python3
 2"""
 3Geodetic, ECEF, ENU and UTM coordinate conversions with mrpt.topography.
 4
 5Demonstrates:
 6  - TCoords: degrees/minutes/seconds representation
 7  - TGeodeticCoords: WGS84 (lat, lon, height) point
 8  - geodeticToGeocentric_WGS84: WGS84 → ECEF (Earth-Centred, Earth-Fixed)
 9  - geocentricToGeodetic: ECEF → WGS84 round-trip
10  - geodeticToENU_WGS84: WGS84 → local East-North-Up (ENU) frame
11"""
12
13from mrpt.topography import (
14    TCoords,
15    TGeodeticCoords,
16    geodeticToGeocentric_WGS84,
17    geocentricToGeodetic,
18    geodeticToENU_WGS84,
19)
20
21# ---------------------------------------------------------------------------
22# TCoords — degrees/minutes/seconds helper
23# ---------------------------------------------------------------------------
24c = TCoords(40, 24, 51.2)          # 40° 24' 51.2"
25print(f"TCoords: {c}")
26print(f"  decimal: {c.getDecimalValue():.6f}°")
27deg, mins, sec = c.getDegMinSec()
28print(f"  DMS: {deg}° {mins}' {sec:.2f}\"")
29
30# ---------------------------------------------------------------------------
31# TGeodeticCoords — WGS84 position
32# ---------------------------------------------------------------------------
33# Malaga, Spain (approx.)
34malaga = TGeodeticCoords(36.7213, -4.4214, 10.0)   # lat_deg, lon_deg, height_m
35print(f"\nWGS84 point: {malaga}")
36
37# ---------------------------------------------------------------------------
38# WGS84 → ECEF (geocentric)
39# ---------------------------------------------------------------------------
40ecef = geodeticToGeocentric_WGS84(malaga)
41print(f"\nECEF (ECEF/geocentric, metres):")
42print(f"  x={ecef.x:.2f}  y={ecef.y:.2f}  z={ecef.z:.2f}")
43
44# ---------------------------------------------------------------------------
45# ECEF → WGS84 round-trip
46# ---------------------------------------------------------------------------
47back = geocentricToGeodetic(ecef)
48print(f"\nRound-trip WGS84: lat={back.lat.getDecimalValue():.6f}°  "
49      f"lon={back.lon.getDecimalValue():.6f}°  h={back.height:.2f} m")
50assert abs(back.lat.getDecimalValue() - 36.7213) < 1e-5
51assert abs(back.lon.getDecimalValue() - -4.4214) < 1e-5
52print("  Round-trip OK ✓")
53
54# ---------------------------------------------------------------------------
55# geodeticToENU_WGS84 — local East-North-Up frame
56# ---------------------------------------------------------------------------
57origin = TGeodeticCoords(36.7213, -4.4214, 10.0)
58point  = TGeodeticCoords(36.7213 + 0.001, -4.4214 + 0.001, 20.0)   # ~111 m N, ~90 m E
59
60enu = geodeticToENU_WGS84(point, origin)
61print(f"\nENU displacement from origin:")
62print(f"  East={enu.x:.2f} m  North={enu.y:.2f} m  Up={enu.z:.2f} m")
63
64# ---------------------------------------------------------------------------
65# UTM coordinates and other reference ellipsoids
66# ---------------------------------------------------------------------------
67from mrpt.topography import TEllipsoid, geodeticToUTM, UTMToGeodetic
68
69utm, zone, band = geodeticToUTM(origin)
70print(f"\nUTM: x={utm.x:.2f} y={utm.y:.2f} zone={zone}{band}")
71back = UTMToGeodetic(utm, zone, band=band)  # or hemisphere="N"
72print(f"  back to geodetic: lat={back.lat.decimal_value:.6f} lon={back.lon.decimal_value:.6f}")
73
74utm_intl, _, _ = geodeticToUTM(origin, TEllipsoid.Ellipsoid_Internacional_1924())
75print(f"  with the International 1924 ellipsoid: x={utm_intl.x:.2f} y={utm_intl.y:.2f}")