UTM-WGS84 Converter
The `utm` library provides bidirectional conversion between Universal Transverse Mercator (UTM) coordinates and WGS84 latitude/longitude pairs for Python. It is a lightweight, pure Python package, optionally leveraging NumPy for enhanced performance with array-based operations. The library is actively maintained, with regular releases addressing accuracy improvements and Python version compatibility.
Warnings
- breaking Python 2.x and older Python 3.x versions (e.g., 2.6, 2.7, 3.3, 3.4, 3.5, 3.6, 3.7, 3.8) are no longer supported by recent versions of the `utm` library. The current version (0.8.1) requires Python >= 3.8.
- gotcha When converting multiple latitude/longitude points to UTM using NumPy arrays with `utm.from_latlon`, the `ZONE_NUMBER` and `ZONE_LETTER` are determined solely by the *first* point in the input array. All subsequent points are then converted to this same determined UTM zone, even if they would naturally fall into a different zone. This can lead to incorrect results for geographically dispersed datasets.
- gotcha The `zone_letter` parameter in `utm.to_latlon` is technically optional. You can provide the `northern` boolean parameter instead (True for Northern Hemisphere, False for Southern Hemisphere). Neglecting this can lead to hemisphere ambiguity if `zone_letter` is omitted, or unnecessary complexity if `northern` is preferred.
- gotcha Accuracy near the anti-meridian (longitude +/- 180 degrees) or when explicitly forcing UTM zones could have edge cases in older versions. While improvements have been made (e.g., in v0.6.0, v0.8.x), users dealing with coordinates close to these boundaries or using older library versions should validate their conversions carefully.
- gotcha While the `utm` library is pure Python and works without NumPy, conversions involving large arrays of coordinates are significantly slower without NumPy installed. The library automatically detects and utilizes NumPy if available.
Install
-
pip install utm
Imports
- from_latlon
import utm utm.from_latlon(...)
- to_latlon
import utm utm.to_latlon(...)
Quickstart
import utm
# Example 1: Latitude/Longitude to UTM
latitude = 51.2
longitude = 7.5
easting, northing, zone_number, zone_letter = utm.from_latlon(latitude, longitude)
print(f"Lat/Lon ({latitude}, {longitude}) -> UTM: ({easting:.2f}, {northing:.2f}, {zone_number}{zone_letter})")
# Example 2: UTM to Latitude/Longitude
eutm_easting = 395201.31
utm_northing = 5673135.24
utm_zone_number = 32
utm_zone_letter = 'U'
lat, lon = utm.to_latlon(utm_easting, utm_northing, utm_zone_number, utm_zone_letter)
print(f"UTM ({utm_easting:.2f}, {utm_northing:.2f}, {utm_zone_number}{utm_zone_letter}) -> Lat/Lon: ({lat:.6f}, {lon:.6f})")
# Example with 'northern' parameter (alternative to zone_letter for to_latlon)
lat_northern, lon_northern = utm.to_latlon(utm_easting, utm_northing, utm_zone_number, northern=True)
print(f"UTM (using northern=True) -> Lat/Lon: ({lat_northern:.6f}, {lon_northern:.6f})")