PyPSA icon indicating copy to clipboard operation
PyPSA copied to clipboard

get_projection_from_crs crashes on unknown CRS instead of falling back to PlateCarree

Open OnodOfTheNorth opened this issue 2 weeks ago • 4 comments

Version Checks (indicate both or one)

  • [x] I have confirmed this bug exists on the lastest release of PyPSA.
  • [x] I have confirmed this bug exists on the current master branch of PyPSA.

Issue Description

get_projection_from_crs in pypsa/geo.py crashes when it gets a CRS it doesn't recognize. The docstring says it takes an EPSG code or proj4 string, and that if the projection isn't found it logs a warning and returns PlateCarree. But it only catches ValueError around ccrs.epsg(crs), and for an unknown code pyproj raises CRSError, which is a RuntimeError. So the fallback never happens. A proj4 string like "+proj=merc" and "EPSG:3035" crash the same way. Right now the fallback only works for codes like 4326 that exist but aren't projections.

I found this while writing unit tests for geo.py. I'd like to fix it if that's okay. The simplest fix would be to also catch pyproj's CRSError, but if you don't want to support proj4 strings here I can update the docstring instead. I can add a test either way.

Reproducible Example

from pypsa.geo import get_projection_from_crs

get_projection_from_crs(999999)

Expected Behavior

The docstring says "If the projection is not found, a warning is issued and the default PlateCarree projection is returned." So I'd expect a warning in the log and PlateCarree back. Instead it raises CRSError: Invalid projection: EPSG:999999: (Internal Proj Error: proj_create: crs not found: EPSG:999999).

Installed Versions

Bottleneck==1.6.1 Cartopy==0.26.0 certifi==2026.10.23 cftime==1.6.6.1 click==8.5.1 cloudpickle==3.1.2 colorama==0.4.6 contourpy==1.4.0 cycler==0.12.1 dask==2026.8.0 deprecation==2.1.0 fonttools==4.68.0 fsspec==2026.9.0 geopandas==1.2.0 highspy==1.16.0 Jinja2==3.1.6 kiwisolver==1.5.1 linopy==0.10.1 locket==1.0.0 MarkupSafe==3.0.4 matplotlib==3.11.2 narwhals==2.28.0 netCDF4==1.7.5.1.post1 networkx==3.7 numexpr==2.14.2 numpy==2.5.4 packaging==26.3 pandas==3.1.1 partd==1.4.2 pillow==13.0.0 platformdirs==4.14.5 plotly==7.1.0 polars==2.1.0 polars-runtime-32==2.1.0 pyarrow==26.0.1 pydeck==0.9.3 pyogrio==0.13.0 pyparsing==3.3.3 pyproj==3.8.0 pypsa==1.4.0 pyshp==3.1.8 python-dateutil==4.0.0 PyYAML==6.0.3 RapidFuzz==3.14.6 scipy==1.18.1 seaborn==0.13.2 shapely==2.2.0 toolz==1.2.0 tqdm==4.70.1 tzdata==2026.6 validators==0.36.0 xarray==2026.9.0

OnodOfTheNorth avatar Nov 14 '26 04:11 OnodOfTheNorth

Reproduced on master (d1c8900, cartopy 0.26.0, pyproj 3.8.0): get_projection_from_crs(999999) raises CRSError instead of returning PlateCarree. The except ValueError in pypsa/geo.py:190-192 never fires because pyproj's CRSError is a RuntimeError subclass. Same for '+proj=merc'; the 'EPSG:3035' string fails the same way, via ccrs.epsg prepending another EPSG: prefix (EPSG:EPSG:3035). get_projection_from_crs(4326) still returns PlateCarree as expected.

chenyi6758-source avatar Nov 18 '26 02:11 chenyi6758-source

Confirmed on PyPSA 1.4.0 (cartopy 0.26.0, pyproj 3.8.0): get_projection_from_crs(999999) and "+proj=merc" both raise CRSError, "EPSG:3035" fails with a doubled prefix (EPSG:EPSG:3035), and 4326 returns PlateCarree. So the documented warning + PlateCarree fallback never kicks in for unknown codes.

In pypsa/geo.py, the try around ccrs.epsg(crs) only catches ValueError, while pyproj raises CRSError (a RuntimeError subclass).

Should we catch CRSError as well, and do you want proj4 strings like "+proj=merc" supported or just narrow the docstring? Also @OnodOfTheNorth, are you still planning to take this, or okay if I pick it up?

DresdenGman avatar Nov 22 '26 03:11 DresdenGman

Yes, I'm still on it. I'll open a PR shortly that also catches pyproj's CRSError so it falls back to PlateCarree like the docstring says, plus a test.

OnodOfTheNorth avatar Nov 25 '26 04:11 OnodOfTheNorth

I don't think we want a fallback option here and stick to fail fast. why would you not want to raise an helpful error message?

EDIT: do I understand correctly that the fallback is already happening? in that case I would like it to fail fast instead of logging the fallback

FabianHofmann avatar Nov 25 '26 11:11 FabianHofmann