Skip to content

Commit 24a4a8e

Browse files
committed
ENH: nudge every azimuth PROJ cannot build in ObliqueMercator
PROJ cannot build an omerc whose centre line runs due east or west, and that covers every azimuth congruent to 90 modulo 180, not just 90 itself: -90, 270 and 450 all fail at some central latitudes. Also add tests against a real hotine EPSG definition.
1 parent 200e05b commit 24a4a8e

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
@@ -3245,10 +3245,11 @@ def __init__(self, central_longitude=0.0, central_latitude=0.0,
32453245
Scale factor at the central meridian. Defaults to 1.
32463246
azimuth: optional
32473247
Azimuth of centerline clockwise from north at the center point of
3248-
the centre line. Defaults to 0.
3248+
the centre line. Defaults to 0. EPSG calls this ``alpha``.
32493249
gamma: optional
3250-
Azimuth of centerline clockwise from north of the rectified bearing
3251-
of centre line. If omitted, ``azimuth`` determines the values of ``gamma``.
3250+
Angle from the rectified grid to the skew grid in degrees, which
3251+
sets which way is up on the map. If omitted, PROJ uses
3252+
``azimuth``. Real Hotine systems quote the two separately.
32523253
globe: optional
32533254
An instance of :class:`cartopy.crs.Globe`. If omitted, a default
32543255
globe is created.
@@ -3260,9 +3261,12 @@ def __init__(self, central_longitude=0.0, central_latitude=0.0,
32603261
32613262
"""
32623263

3263-
if np.isclose(azimuth, 90):
3264-
# Exactly 90 causes coastline 'folding'.
3265-
azimuth -= 1e-3
3264+
# PROJ cannot build an omerc whose centre line runs due east or west
3265+
# which causes a singular value in the expansion we need to nudge
3266+
# away from
3267+
singular = 90.0 + 180.0 * round((azimuth - 90.0) / 180.0)
3268+
if abs(azimuth - singular) < 1e-3:
3269+
azimuth = singular - 1e-3
32663270

32673271
proj4_params = [('proj', 'omerc'), ('lonc', central_longitude),
32683272
('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)