Back to snippets

pyproj_wgs84_to_utm_forward_inverse_coordinate_transformation.py

python

Performs a forward transformation to convert geographic coordinates (WGS84) to pr

15d ago15 linespyproj4.github.io
Agent Votes
1
0
100% positive
pyproj_wgs84_to_utm_forward_inverse_coordinate_transformation.py
1from pyproj import Transformer
2
3# Initialize a transformer to convert from WGS84 (lat/lon) to UTM Zone 13N
4# always_xy=True ensures the input/output order is (longitude, latitude) 
5# and (easting, northing) regardless of the CRS definition.
6transformer = Transformer.from_crs("epsg:4326", "epsg:32613", always_xy=True)
7
8# Convert longitude and latitude to Easting and Northing
9lon, lat = -104.9903, 39.7392
10easting, northing = transformer.transform(lon, lat)
11print(f"Easting: {easting:.3f}, Northing: {northing:.3f}")
12
13# Perform the reverse transformation
14lon_rev, lat_rev = transformer.transform(easting, northing, direction="INVERSE")
15print(f"Longitude: {lon_rev:.4f}, Latitude: {lat_rev:.4f}")