pyproj4 / pyproj

Python interface to PROJ (cartographic projections and coordinate transformations library)
https://pyproj4.github.io/pyproj
MIT License
1.04k stars 210 forks source link

Inverted x/y for transformation from WSG84 to LV95 (EPSG:2056) #1377

Closed turbotimon closed 6 months ago

turbotimon commented 6 months ago

Code Sample, a copy-pastable example if possible

When transforming from WSG84 to LV95, the coordinates get inverted

from pyproj import Transformer

WGS84_CRS = 'EPSG:4326'  # WGS84 coordinate reference system
LV95_CRS = 'EPSG:2056'    # LV95 (Swiss Grid) coordinate reference system

lon_x, lat_y = 8.5417, 47.3769 # Zuerich, Switzerland

transformer = Transformer.from_crs(WGS84_CRS, LV95_CRS)
lv95_x, lv95_y = transformer.transform(lon_x, lat_y)
print(lv95_x, lv95_y) # 7647764, -1894770 wrong

lv95_x, lv95_y = transformer.transform(lat_y, lon_x) # inverting lon/lat
print(lv95_x, lv95_y) # 2683303, 1247925 correct

Problem description

All inputs and output should be X/Y or LON/LAT order since pyrpoj >2.6

Expected Output

Environment Information

System: python: 3.9.18 (main, Aug 24 2023, 18:16:58) [GCC 12.3.0] executable: /home/erti/PROJECT_repos/OsmCompleteness/osm-completeness/venv/bin/python machine: Linux-6.1.77-x86_64-with-glibc2.38

Python deps: certifi: 2023.11.17 Cython: None setuptools: 69.0.2 pip: 23.3.1


#### Installation method
 - pip
djhoese commented 6 months ago

https://pyproj4.github.io/pyproj/stable/gotchas.html#axis-order-changes-in-proj-6

For your specific case, EPSG 4326 has an axis order of (y, x) so you need to pass the inputs as lat first, lon second. You can pass always_xy=True to your Transformer creation which will always expect x (lon) as the first argument and y (lat) as the second argument to the transformation methods.

turbotimon commented 6 months ago

Ok sorry then. I supposed that coordinates are always the same order since is transform(xx, yy, ...). It seems really confusing to me to pass LAT as X coordinate..

Also, because https://pyproj4.github.io/pyproj/stable/api/proj.html#pyproj-proj says

Converts from longitude, latitude to native map projection x,y coordinates and vice versa

(but I found the warning now in the doc about Transformers)

djhoese commented 6 months ago

Right. Overall Proj is deprecated in favor of Transformer. Any function signature that uses x and y is just unfortunate naming. The variables have to be named something. Besides the Proj class, if there is something else in the docs that would have revealed this sooner feel free to create a pull request with the updates.