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

Leading space in WKT raises `CRSError` #1328

Closed trexfeathers closed 1 year ago

trexfeathers commented 1 year ago

(Only reference to leading spaces I could find was not about WKT).

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

A WKT string with a leading space works with Proj directly (OSGeo/PROJ@93d8fccd51a704c4accd535743a963ad0ca5a274):

$ projinfo ' GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137,298.257223563]],PRIMEM["Greenwich",0],UNIT["Degree",0.017453292519943295]]'
PROJ.4 string:
+proj=longlat +datum=WGS84 +no_defs +type=crs

WKT2:2019 string:
GEOGCRS["WGS 84",
    DATUM["World Geodetic System 1984",
        ELLIPSOID["WGS 84",6378137,298.257223563,
            LENGTHUNIT["metre",1]],
        ID["EPSG",6326]],
    PRIMEM["Greenwich",0,
        ANGLEUNIT["Degree",0.0174532925199433]],
    CS[ellipsoidal,2],
        AXIS["longitude",east,
            ORDER[1],
            ANGLEUNIT["Degree",0.0174532925199433]],
        AXIS["latitude",north,
            ORDER[2],
            ANGLEUNIT["Degree",0.0174532925199433]]]

This same string does not work with PyProj:

>>> crs_wkt = ' GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137,298.257223563]],PRIMEM["Greenwich",0],UNIT["Degree",0.017453292519943295]]'
>>> print(pyproj.crs.CRS.from_wkt(crs_wkt))
pyproj.exceptions.CRSError: Invalid WKT string:  GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137,298.257223563]],PRIMEM["Greenwich",0],UNIT["Degree",0.017453292519943295]]

But the string does work with PyProj if the leading space is removed:

>>> crs_wkt = crs_wkt.lstrip()
>>> print(pyproj.crs.CRS.from_wkt(crs_wkt))
GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137,298.257223563]],PRIMEM["Greenwich",0],UNIT["Degree",0.017453292519943295]]

Problem description

[this should explain why the current behavior is a problem and why the expected output is a better solution.]

Consistency between Proj and PyProj is important for avoiding confusion about what WKT strings have valid formatting. We are aware of files where the WKT string is padded (SciTools/iris#5343), but it is now unclear whether this format is valid (according to Proj) or invalid (according to PyProj).

Expected Output

Environment Information

pyproj info:
    pyproj: 3.5.0
      PROJ: 9.2.0
  data dir: /home/h01/myeo/.conda/envs/iris-dev/share/proj
user_data_dir: /home/h01/myeo/.local/share/proj
PROJ DATA (recommended version): 1.13
PROJ Database: 1.2
EPSG Database: v10.082 [2023-02-06]
ESRI Database: ArcGIS Pro 3.1 [2023-19-01]
IGNF Database: 3.1.0 [2019-05-24]

System:
    python: 3.11.3 | packaged by conda-forge | (main, Apr  6 2023, 08:57:19) [GCC 11.3.0]
executable: /home/h01/myeo/.conda/envs/iris-dev/bin/python
   machine: Linux-3.10.0-1160.92.1.el7.x86_64-x86_64-with-glibc2.17

Python deps:
   certifi: 2022.12.7
    Cython: None
setuptools: 67.7.2
       pip: 23.1.1

Installation method

  • conda, pip wheel, from source, etc...

Conda

Conda environment information (if you installed with conda):


Environment (conda list):

``` $ conda list proj # packages in environment at /home/h01/myeo/.conda/envs/iris-dev: # # Name Version Build Channel proj 9.2.0 h8ffa02c_0 https://conda.anaconda.org/conda-forge pyproj 3.5.0 py311h1850bce_1 https://conda.anaconda.org/conda-forge ```


Details about conda and system ( conda info ):

``` $ conda info active environment : iris-dev active env location : /home/h01/myeo/.conda/envs/iris-dev shell level : 1 user config file : /home/h01/myeo/.condarc populated config files : /etc/conda/condarc /home/h01/myeo/.condarc conda version : 23.1.0 conda-build version : not installed python version : 3.10.4.final.0 virtual packages : __archspec=1=x86_64 __glibc=2.17=0 __linux=3.10.0=0 __unix=0=0 base environment : REDACTED (writable) conda av data dir : REDACTED conda av metadata url : None channel URLs : REDACTED [institutional Artifactory URLs] package cache : /home/h01/myeo/.conda/pkgs envs directories : /home/h01/myeo/.conda/envs REDACTED platform : linux-64 user-agent : conda/23.1.0 requests/2.28.1 CPython/3.10.4 Linux/3.10.0-1160.92.1.el7.x86_64 rhel/7.9 glibc/2.17 UID:GID : 11934:1000 netrc file : /home/h01/myeo/.netrc offline mode : False ```
trexfeathers commented 1 year ago

Thanks for any help you can give 😊

snowman2 commented 1 year ago

This is interesting:

>>> from pyproj import CRS
>>> crs_wkt = ' GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137,298.257223563]],PRIMEM["Greenwich",0],UNIT["Degree",0.017453292519943295]]'
>>> CRS(crs_wkt)
<Geographic 2D CRS: EPSG:4326>
Name: WGS 84
Axis Info [ellipsoidal]:
- lon[east]: Longitude (Degree)
- lat[north]: Latitude (Degree)
Area of Use:
- undefined
Datum: World Geodetic System 1984
- Ellipsoid: WGS 84
- Prime Meridian: Greenwich

>>> CRS.from_wkt(crs_wkt)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File ".../pyproj/pyproj/crs/crs.py", line 431, in from_wkt
    raise CRSError(f"Invalid WKT string: {in_wkt_string}")
pyproj.exceptions.CRSError: Invalid WKT string:  GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137,298.257223563]],PRIMEM["Greenwich",0],UNIT["Degree",0.017453292519943295]]
snowman2 commented 1 year ago
>>> from pyproj.crs import is_wkt
>>> is_wkt(crs_wkt)
False
>>> is_wkt(crs_wkt.strip())
True
snowman2 commented 1 year ago

This issue will be addressed in the next PROJ release: https://github.com/OSGeo/PROJ/issues/3840

trexfeathers commented 1 year ago

Brilliant, thanks so much @snowman2!

rbeucher commented 1 year ago

Thanks!

rbeucher commented 11 months ago

Hi all,

After upgrading to 3.6.1, we are still seeing the issue reported here https://github.com/ESMValGroup/ESMValTool/issues/3203.

jjimenezshaw commented 11 months ago

This issue was solved in PROJ (9.3.0), not in pyproj. Which version of PROJ are you using/linking?

rbeucher commented 11 months ago

Sorry for the late reply. Still using 9.2.1, will have to update.