Coordinate transformation with pyproj
Introduction
pyproj is an open-source Python library for converting coordinates from one coordinate reference system (CRS) to another. It is built on PROJ, the standard C library for coordinate transformation; QGIS, GDAL and PostGIS use the same PROJ.
pyproj also runs inside to_crs in GeoPandas and inside reprojection in Rasterio. Using it directly lets you convert GPS points, coordinates from a table or an API response to a metric system in a few lines of code. CRS, datum and projection are explained in the article on coordinate systems and reprojection; this article shows how they are applied in code. All examples below were run with pyproj 3.7.2 (PROJ 9.5.1).
The CRS object and EPSG codes
In pyproj a system is represented by a CRS object. It can be created from an EPSG code, WKT text or a PROJ string, but the EPSG code is the most precise and shortest way:
from pyproj import CRS
wgs84 = CRS.from_epsg(4326)
utm42 = CRS.from_epsg(32642)
print(wgs84.name) # WGS 84
print([o.abbrev for o in wgs84.axis_info]) # ['Lat', 'Lon']
print(utm42.name) # WGS 84 / UTM zone 42N
print(utm42.axis_info[0].unit_name) # metre
axis_info reveals an important detail: in the EPSG registry, the first axis of EPSG:4326 is latitude and the second is longitude. In UTM the axes point east (X) and north (Y), and the unit is the metre.
Transformer and axis order
The transformation is done by a Transformer object. It is created once and then reused for any number of points. This is where the most common mistake hides: axis order.
from pyproj import Transformer
xato = Transformer.from_crs("EPSG:4326", "EPSG:32642")
print([round(v) for v in xato.transform(69.28, 41.31)])
# [-560905, 7931326]
t = Transformer.from_crs("EPSG:4326", "EPSG:32642", always_xy=True)
x, y = t.transform(69.28, 41.31) # Tashkent, approximately
print(round(x, 2), round(y, 2)) # 523437.76 4573208.74
The first transformer followed the registry order: it took 69.28 as latitude and 41.31 as longitude and computed a point beyond the Arctic Circle. No error message appears; the result simply lands in the wrong place. The always_xy=True parameter always sets the order to "longitude (X) first, then latitude (Y)", which matches the usual order in GeoJSON, Shapely and most GIS software.

Transforming a point and a list
transform also accepts a list or a NumPy array instead of a single number. Converting a thousand points in one call, without a loop, is much faster:
# Tashkent, Samarkand, Namangan (approximate centres)
lon = [69.28, 66.96, 71.67]
lat = [41.31, 39.65, 41.00]
xs, ys = t.transform(lon, lat)
print([round(v) for v in xs]) # [523438, 324973, 724563]
print([round(v) for v in ys]) # [4573209, 4390901, 4542191]
orqaga = Transformer.from_crs("EPSG:32642", "EPSG:4326", always_xy=True)
print(orqaga.transform(x, y)) # (69.28, 41.31)
Getting the original value back from the inverse transformation is a simple way to check the code. Tashkent's X value is close to 500,000 m because the city lies near the central meridian of zone 42 (69° E), which is given exactly 500,000 m in UTM.
The UTM zone for Uzbekistan
UTM divides the Earth into 60 zones of 6 degrees each. Uzbekistan stretches from about 56° to 73° east, so it falls into four zones: 40N (EPSG:32640), 41N (EPSG:32641), 42N (EPSG:32642) and 43N (EPSG:32643). Most of the country is in 41N and 42N. Instead of calculating the zone by hand, it can be looked up in pyproj's EPSG database:
from pyproj.aoi import AreaOfInterest
from pyproj.database import query_utm_crs_info
def utm_zona(lon, lat):
natija = query_utm_crs_info(
datum_name="WGS 84",
area_of_interest=AreaOfInterest(lon, lat, lon, lat))
return natija[0].code
print(utm_zona(69.28, 41.31)) # 32642 — Tashkent
print(utm_zona(64.42, 39.77)) # 32641 — Bukhara
print(utm_zona(59.60, 42.46)) # 32640 — Nukus
The further a point is from its zone, the greater the distance distortion. For a single region its own zone is enough; for area calculations across the whole country, an equal-area projection is chosen.

Distance: degrees and metres
Calculating distance directly from coordinates in degrees makes no sense: at Tashkent's latitude, 1 degree of longitude is about 83.7 km, while 1 degree of latitude is about 111 km. Let us compare the distance between Tashkent and Samarkand in three ways:
import math
from pyproj import Geod
lon = [69.28, 66.96] # Tashkent, Samarkand
lat = [41.31, 39.65]
print(round(math.hypot(lon[1] - lon[0], lat[1] - lat[0]), 2)) # 2.85 "degrees"
x, y = t.transform(lon, lat)
print(round(math.hypot(x[1] - x[0], y[1] - y[0]))) # 269489 m
geod = Geod(ellps="WGS84")
_, _, d = geod.inv(lon[0], lat[0], lon[1], lat[1])
print(round(d)) # 269567 m
The first result has no unit. The difference between the flat UTM distance and the geodesic distance on the ellipsoid is 78 m, or 0.03%. It arises because Samarkand is far from the central meridian of zone 42. For long distances or routes that cross several zones, Geod is more reliable.

Datum transformation and accuracy
When the source and target CRS use different datums, for example Pulkovo 1942 (EPSG:4284) on old maps and WGS 84, PROJ finds several datum transformation options. Each has its own accuracy and area of use:
from pyproj.transformer import TransformerGroup
aoi = AreaOfInterest(69.28, 41.31, 69.28, 41.31)
tg = TransformerGroup("EPSG:4284", "EPSG:4326", always_xy=True,
area_of_interest=aoi)
for tr in tg.transformers:
print(tr.accuracy, tr.area_of_use.name[:28])
# 3.0 Russian Federation - onshore
# 4.5 Armenia; Azerbaijan; Belarus
# 44.0 Kazakhstan - onshore includi
# 999.0 Russian Federation - onshore
lon2, lat2 = tg.transformers[1].transform(69.28, 41.31)
print(round(lon2, 6), round(lat2, 6)) # 69.27925 41.31019
In Tashkent the shift between the two datums is about 66 m. The 3 m option at the top of the list is defined only for Russia: the area is matched by its bounding box, so it formally "covers" Tashkent too. The option that officially covers Uzbekistan is the second one, with 4.5 m accuracy; the two results differ by about 3 m. So in precise work, the option chosen automatically by Transformer.from_crs should not be taken on trust: area_of_use and accuracy are checked, and the chosen method is recorded in the metadata.
Conclusion
Reliable transformation in pyproj rests on three rules: the CRS is given precisely with an EPSG code, a Transformer is always created with always_xy=True, and the result is checked with an inverse transformation or a known control point.
Distances and areas are calculated in a metric system suited to the area, or on the ellipsoid with Geod. When datums differ, the transformation option is not left to chance: its area of use and accuracy are compared with the project's requirements.

