pyproj4 / pyproj

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

transform from WGS86 to a self-defined LCC #1302

Closed miniufo closed 1 year ago

miniufo commented 1 year ago

Code Sample, a copy-pastable example if possible

A "Minimal, Complete and Verifiable Example" will make it much easier for maintainers to help you: http://matthewrocklin.com/blog/work/2018/02/28/minimal-bug-reports

from pyproj import Proj, Transformer
s = "+ellps=sphere +a=6370000 +b=6370000 +proj=lcc +lon_0=103.0 +lat_0=30.1 +x_0=0.0 +y_0=0.0 +lat_1=40.0 +lat_2=25.0 +no_defs"
transformer = Transformer.from_crs('WGS84', s)
e, n = transformer.transform(102.0, 30.1, errcheck=True)

Problem description

This code snippet raises an error:

Traceback (most recent call last):

  Cell In[34], line 4
    e, n = transformer.transform(102.0, 30.1, errcheck=True)

  File C:\ProgramData\anaconda3\lib\site-packages\pyproj\transformer.py:804 in transform
    self._transformer._transform(

  File pyproj\_transformer.pyx:687 in pyproj._transformer._Transformer._transform

ProjError: transform error: Invalid coordinate: (Internal Proj Error: lcc: Invalid latitude)

Expected Output

No

Environment Information

pyproj.__version__
Out[35]: '3.4.1'

Installation method

Conda environment information (if you installed with conda):


Environment (conda list):

``` $ conda list proj # packages in environment at C:\ProgramData\anaconda3: # # Name Version Build Channel anaconda-project 0.11.1 py310haa95532_0 proj 8.2.1 h5ed7ab8_0 pyproj 3.4.1 py310h95ea4f9_0 ```


Details about conda and system ( conda info ):

``` $ conda info active environment : base active env location : C:\ProgramData\anaconda3 user config file : C:\Users\Yu-Kun Qian\.condarc populated config files : C:\Users\Yu-Kun Qian\.condarc conda version : 23.3.1 conda-build version : 3.23.3 python version : 3.10.9.final.0 virtual packages : __archspec=1=x86_64 __cuda=12.0=0 __win=0=0 base environment : C:\ProgramData\anaconda3 (read only) conda av data dir : C:\ProgramData\anaconda3\etc\conda conda av metadata url : None channel URLs : https://repo.anaconda.com/pkgs/main/win-64 https://repo.anaconda.com/pkgs/main/noarch https://repo.anaconda.com/pkgs/r/win-64 https://repo.anaconda.com/pkgs/r/noarch https://repo.anaconda.com/pkgs/msys2/win-64 https://repo.anaconda.com/pkgs/msys2/noarch package cache : C:\ProgramData\anaconda3\pkgs C:\Users\Yu-Kun Qian\.conda\pkgs C:\Users\Yu-Kun Qian\AppData\Local\conda\conda\pkgs envs directories : C:\Users\Yu-Kun Qian\.conda\envs C:\ProgramData\anaconda3\envs C:\Users\Yu-Kun Qian\AppData\Local\conda\conda\envs platform : win-64 user-agent : conda/23.3.1 requests/2.28.1 CPython/3.10.9 Windows/10 Windows/10.0.22621 administrator : False netrc file : None offline mode : False ```
snowman2 commented 1 year ago

If you add always_xy=True, it works:

>>> from pyproj import Proj, Transformer
>>> s = "+ellps=sphere +a=6370000 +b=6370000 +proj=lcc +lon_0=103.0 +lat_0=30.1 +x_0=0.0 +y_0=0.0 +lat_1=40.0 +lat_2=25.0 +no_defs"
>>> transformer = Transformer.from_crs('WGS84', s)
>>> transformer = Transformer.from_crs('WGS84', s, always_xy=True)
>>> e, n = transformer.transform(102.0, 30.1, errcheck=True)
>>> e, n
(-95450.8134071046, 448.8443216748683)

The reason is because CRS axis order is respected in transformations with PROJ 6+ pyproj 2+. For WSG84, latitude is expected to be first:

>>> from pyproj import CRS
>>> CRS("WGS84")
<Geographic 2D CRS: EPSG:4326>
Name: WGS 84
Axis Info [ellipsoidal]:
- Lat[north]: Geodetic latitude (degree)
- Lon[east]: Geodetic longitude (degree)
Area of Use:
- name: World.
- bounds: (-180.0, -90.0, 180.0, 90.0)
Datum: World Geodetic System 1984 ensemble
- Ellipsoid: WGS 84
- Prime Meridian: Greenwich

See getting started example.