OGR Python – Unexpected Results in Geometry Transform

ogrpython

Transformation query PostGIS:

SELECT ST_AsText(ST_Transform(ST_GeomFromText('POINT (16.0317217831498 45.0589613134642)',4326),3857))

Result:

POINT(1784643.10543967 5630808.51923201)

These coordinates are correctly transformed ones

I am trying to get same result only by using Python ogr lib.

Python code:

from osgeo import ogr
from osgeo import osr

source = osr.SpatialReference()
source.ImportFromEPSG(4326)

target = osr.SpatialReference()
target.ImportFromEPSG(3857)

transform = osr.CoordinateTransformation(source, target)

point = ogr.CreateGeometryFromWkt("POINT (16.0317217831498 45.0589613134642)")
point.Transform(transform)

point.ExportToWkt()

Result:

POINT (5015940.62908865 1808396.61831747)

What am I not doing right in transformation with ogr lib ? (ps. I also tried to assignSpatialReference 4326 to geometry before applying transformation but result is still the same)

Best Answer

EPSG:4326 is officially using axis order latitude-longitude but PostGIS and many others are using longitude-latitude order that has been a tradition in GIS. Turn the coordinates into "POINT (45.0589613134642 16.0317217831498)

>>> point = ogr.CreateGeometryFromWkt("POINT (45.0589613134642 16.0317217831498)")
>>> point.Transform(transform)
0
>>> point.ExportToWkt()
'POINT (1784643.10543967 5630808.51923201)'
Related Question