1

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)

1
  • 2
    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. Try with "POINT (45.0589613134642 16.0317217831498)". Or read gdal.org/tutorials/osr_api_tut.html and use oSRS.SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER).
    – user30184
    Dec 28, 2021 at 11:09

1 Answer 1

3

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)'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.