Thanks to visit codestin.com
Credit goes to github.com

Skip to content

stereographic projections do not have their origin in the prescribed center #2643

@joernu76

Description

@joernu76

I noticed a shift in our plots transitioning from cartopy 0.24 to cartopy 0.25.

This is a minimal test case

import numpy as np
import cartopy.crs as ccrs

proj = ccrs.Stereographic(central_latitude=60.0, central_longitude=5.0)
pc = ccrs.PlateCarree()

# Transform the projection center — should map to (0, 0)
result = proj.transform_point(5.0, 60.0, pc)
print(result)  # expecting (0.0, 0.0)

Result for cartopy 0.24 (and numpy 1.x if that matters):
(1.0648497373612918e-09, 2.8606832275143835e-08)

Result for cartopy 0.25 (and numpy 2.x)
(np.float64(1.0576187589792157e-09), np.float64(25096.80415954815))

The proj4_params are identical in both cartopy versions:

{'ellps': 'WGS84',
 'proj': 'stere',
 'lat_0': 60.0,
 'lon_0': 5.0,
 'x_0': 0.0,
 'y_0': 0.0}

Using these parameters to test pyproj4 gives proper results:

import pyproj

transformer = pyproj.Transformer.from_crs("EPSG:4326", 
    "+proj=stere +ellps=WGS84 +lat_0=60 +lon_0=5 +x_0=0 +y_0=0",
    always_xy=True)

# Project a known point and back
x, y = transformer.transform(5.0, 60.0)  # lon, lat at projection center
print(x, y)  # should be exactly (0, 0) 

Disclaimer: I used claude to generate the test cases, but I discovered the issue in a regression of ours which I followed to this dead end.

The effect is small for our typical use case, but could be large for city level plots, where the desired center could be outside the boundary.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions