import gdal
import osr
gdal.UseExceptions()
gdal.VersionInfo()
'2040100'
srs_4326 = osr.SpatialReference()
srs_4326.ImportFromEPSG(4326)
0
srs_3857 = osr.SpatialReference()
srs_3857.ImportFromEPSG(3857)
0
ct_3857_to_4326 = osr.CoordinateTransformation(srs_3857, srs_4326)
ct_4326_to_3857 = osr.CoordinateTransformation(srs_4326, srs_3857)
lat = 50.0
lon = -120.0
# this output is equivalent to the cmd (gdaltransform.exe)
mapx, mapy, z = ct_4326_to_3857.TransformPoint(lon, lat)
mapx, mapy
(-13358338.89519283, 6446275.8410171615)
lat = 50.0
lon = -120.0
# Incorrect output, as expected (wrong order of arguments)
mapy, mapx, z = ct_4326_to_3857.TransformPoint(lat, lon)
mapy, mapx
(0.872664625997165, -2.0943951023931957)
mapx = -13358338.895192828
mapy = 6446275.841017158
# this output is equivalent to the cmd (gdaltransform.exe)
ct_3857_to_4326.TransformPoint(mapx, mapy)
(-119.99999999999996, 49.99999999999998, 0.0)
mapx = -13358338.895192828
mapy = 6446275.841017158
# Incorrect output, as expected (wrong order of arguments)
ct_3857_to_4326.TransformPoint(mapy, mapx)
(57.90788113636133, -75.95934455387824, 0.0)
# (py37) C:\Users\Rutger>gdaltransform --version
# GDAL 2.4.1, released 2019/03/15
# (py37) C:\Users\Rutger>gdaltransform -s_srs EPSG:4326 -t_srs EPSG:3857
# -120 50
# -13358338.8951928 6446275.84101716 0
# docstring suggests (x,y,z)
ct_4326_to_3857.TransformPoint?
Signature: ct_4326_to_3857.TransformPoint(*args) Docstring: TransformPoint(CoordinateTransformation self, double [3] inout) TransformPoint(CoordinateTransformation self, double x, double y, double z=0.0) File: c:\miniconda3\envs\py37\lib\site-packages\osgeo\osr.py Type: method