Skip to content

Commit dc75743

Browse files
authored
Merge pull request #2733 from greglucas/fix/omerc-gamma
ENH: nudge every azimuth PROJ cannot build in ObliqueMercator
2 parents dc52695 + 24a4a8e commit dc75743

2 files changed

Lines changed: 48 additions & 6 deletions

File tree

‎lib/cartopy/crs.py‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3278,10 +3278,11 @@ def __init__(self, central_longitude=0.0, central_latitude=0.0,
32783278
Scale factor at the central meridian. Defaults to 1.
32793279
azimuth: optional
32803280
Azimuth of centerline clockwise from north at the center point of
3281-
the centre line. Defaults to 0.
3281+
the centre line. Defaults to 0. EPSG calls this ``alpha``.
32823282
gamma: optional
3283-
Azimuth of centerline clockwise from north of the rectified bearing
3284-
of centre line. If omitted, ``azimuth`` determines the values of ``gamma``.
3283+
Angle from the rectified grid to the skew grid in degrees, which
3284+
sets which way is up on the map. If omitted, PROJ uses
3285+
``azimuth``. Real Hotine systems quote the two separately.
32853286
globe: optional
32863287
An instance of :class:`cartopy.crs.Globe`. If omitted, a default
32873288
globe is created.
@@ -3293,9 +3294,12 @@ def __init__(self, central_longitude=0.0, central_latitude=0.0,
32933294
32943295
"""
32953296

3296-
if np.isclose(azimuth, 90):
3297-
# Exactly 90 causes coastline 'folding'.
3298-
azimuth -= 1e-3
3297+
# PROJ cannot build an omerc whose centre line runs due east or west
3298+
# which causes a singular value in the expansion we need to nudge
3299+
# away from
3300+
singular = 90.0 + 180.0 * round((azimuth - 90.0) / 180.0)
3301+
if abs(azimuth - singular) < 1e-3:
3302+
azimuth = singular - 1e-3
32993303

33003304
proj4_params = [('proj', 'omerc'), ('lonc', central_longitude),
33013305
('lat_0', central_latitude), ('k', scale_factor),

‎lib/cartopy/tests/crs/test_oblique_mercator.py‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from typing import NamedTuple
1212

1313
import numpy as np
14+
import pyproj
1415
import pytest
1516

1617
import cartopy.crs as ccrs
@@ -199,3 +200,40 @@ def test_nan(oblique_mercator, plate_carree, reverse_coord):
199200
coord = tuple(reversed(coord))
200201
res = oblique_mercator.transform_point(*coord, src_crs=plate_carree)
201202
assert np.all(np.isnan(res))
203+
204+
205+
@pytest.mark.parametrize("azimuth, expected",
206+
[(90.0, 89.999), (-90.0, -90.001), (270.0, 269.999),
207+
(450.0, 449.999), (-270.0, -270.001)])
208+
def test_singular_azimuth_nudged(azimuth, expected):
209+
"""Azimuths PROJ cannot build are nudged; see ObliqueMercator.__init__."""
210+
crs = ccrs.ObliqueMercator(azimuth=azimuth)
211+
assert float(crs.proj4_params["alpha"]) == pytest.approx(expected)
212+
213+
214+
def test_gamma_matches_epsg_3375():
215+
"""GDM2000 / Peninsular Malaysia RSO quotes alpha and gamma 0.1 deg apart.
216+
217+
EPSG:3375 puts its false origin at the projection centre where cartopy
218+
puts it at the natural origin, so a correct definition differs from EPSG
219+
by one constant offset; the spread of those offsets is the real error.
220+
"""
221+
wgs84 = pyproj.CRS("EPSG:4326")
222+
points = [(101.7, 3.1), (103.0, 5.5), (100.5, 6.5), (104.0, 1.5)]
223+
reference = pyproj.Transformer.from_crs(
224+
wgs84, pyproj.CRS("EPSG:3375"), always_xy=True)
225+
226+
def spread(**kwargs):
227+
crs = ccrs.ObliqueMercator(
228+
central_longitude=102.25, central_latitude=4.0,
229+
scale_factor=0.99984, azimuth=323.0257964666667,
230+
globe=ccrs.Globe(ellipse="GRS80"), **kwargs)
231+
ours = pyproj.Transformer.from_crs(
232+
wgs84, pyproj.CRS(crs.proj4_init), always_xy=True)
233+
offsets = (np.array([reference.transform(*p) for p in points])
234+
- np.array([ours.transform(*p) for p in points]))
235+
return np.abs(offsets - offsets.mean(axis=0)).max()
236+
237+
assert spread(gamma=323.1301023611111) < 1e-6
238+
# Without gamma, PROJ defaults it to alpha and the map comes out turned.
239+
assert spread() > 100.0

0 commit comments

Comments
 (0)