Skip to content

Commit 0479a55

Browse files
authored
[semver:patch] Fix density and lonlatbox in mapvector (#45)
Merge pull request #45 from psyplot/develop
2 parents d065d58 + 5e0cec7 commit 0479a55

File tree

5 files changed

+59
-11
lines changed

5 files changed

+59
-11
lines changed

.circleci/config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
version: 2.1
22

33
orbs:
4-
psyplot: psyplot/psyplot-ci-orb@1.5.31
4+
psyplot: psyplot/psyplot-ci-orb@1.5.32
55
mattermost-plugin-notify: nathanaelhoun/mattermost-plugin-notify@1.2.0
66

77
executors:

CHANGELOG.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
V1.4.2
2+
======
3+
Fix mapvector issue
4+
5+
Fixed
6+
-----
7+
- This PR fixes an issue that arises when using ``density`` together with
8+
``lonlatbox``, see `#43 <https://github.com/psyplot/psy-maps/issues/43>`__
9+
and `#44 <https://github.com/psyplot/psy-maps/pull/44>`__
10+
111
v1.4.1
212
======
313
Fix projection issues

psy_maps/plotters.py

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,20 @@ class ProjectionBase(Formatoption):
273273
#'vertical_perspective', # not available for cartopy
274274
]
275275

276+
def transform_lonlatbox(self, value):
277+
"""Transform a lon-lat-box to the specific projection"""
278+
value = np.asarray(value)
279+
transformed = self.projection.transform_points(
280+
ccrs.PlateCarree(), value[:2], value[2:]
281+
)[..., :2]
282+
value[:2] = transformed[..., 0]
283+
value[2:] = transformed[..., 1]
284+
if value[0] == value[1] and isinstance(
285+
self.projection, ccrs.PlateCarree
286+
):
287+
value[1] += 360
288+
return value
289+
276290
@property
277291
def cf_projection(self):
278292
data = next(self.iter_data)
@@ -514,12 +528,17 @@ class Projection(ProjectionBase):
514528

515529
dependencies = ['clon', 'clat']
516530

517-
connections = ['transform']
531+
connections = ['transform', 'lonlatbox']
518532

519533
def __init__(self, *args, **kwargs):
520534
super(Projection, self).__init__(*args, **kwargs)
521535
self.projection = None
522536

537+
@property
538+
def lonlatbox_transformed(self):
539+
"""Transform the lonlatbox according to the projection"""
540+
return self.transform_lonlatbox(self.lonlatbox.lonlatbox)
541+
523542
def initialize_plot(self, value, clear=True):
524543
"""Initialize the plot and set the projection for the axes
525544
"""
@@ -715,15 +734,7 @@ class LonLatBox(BoxBase):
715734

716735
@property
717736
def lonlatbox_transformed(self):
718-
value = np.asarray(self.lonlatbox)
719-
transformed = self.transform.projection.transform_points(
720-
ccrs.PlateCarree(), value[:2], value[2:])[..., :2]
721-
value[:2] = transformed[..., 0]
722-
value[2:] = transformed[..., 1]
723-
if value[0] == value[1] and isinstance(self.transform.projection,
724-
ccrs.PlateCarree):
725-
value[1] += 360
726-
return value
737+
return self.transform.transform_lonlatbox(self.lonlatbox)
727738

728739
def data_dependent(self, data, set_data=True):
729740
if isinstance(data, InteractiveList):
@@ -1927,9 +1938,12 @@ class MapDensity(psyps.Density):
19271938
--------------
19281939
%(Density.possible_types)s"""
19291940

1941+
dependencies = psyps.Density.dependencies + ["projection"]
1942+
19301943
def _set_quiver_density(self, value):
19311944
if all(val == 1.0 for val in value):
19321945
self.plot._kwargs.pop('regrid_shape', None)
1946+
self.plot._kwargs.pop('target_extent', None)
19331947
elif self.decoder.is_unstructured(self.raw_data):
19341948
warnings.warn("Quiver plot of unstructered data does not support "
19351949
"the density keyword!", RuntimeWarning)
@@ -1940,6 +1954,8 @@ def _set_quiver_density(self, value):
19401954
shape = self.data.shape[-2:]
19411955
value = map(int, [value[0]*shape[0], value[1]*shape[1]])
19421956
self.plot._kwargs['regrid_shape'] = tuple(value)
1957+
lonlatbox = self.projection.lonlatbox_transformed
1958+
self.plot._kwargs["target_extent"] = lonlatbox
19431959

19441960
def _unset_quiver_density(self):
19451961
self.plot._kwargs.pop('regrid_shape', None)

tests/conftest.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
#
2323
# You should have received a copy of the GNU LGPL-3.0 license
2424
# along with this program. If not, see <https://www.gnu.org/licenses/>.
25+
import pytest
26+
import os.path as osp
2527

2628
try:
2729
# make sure we import QtWebEngineWidgets at the start
@@ -40,3 +42,8 @@ def pytest_configure(config):
4042
if config.getoption('ref'):
4143
import unittest
4244
unittest.TestLoader.testMethodPrefix = 'ref'
45+
46+
47+
@pytest.fixture
48+
def regular_test_file():
49+
return osp.join(osp.dirname(__file__), "test-t2m-u-v.nc")

tests/test_plotters_vectorplotter.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,5 +175,20 @@ class VectorPlotterTest2D(bt.TestBase2D, VectorPlotterTest):
175175
var = ['u_2d', 'v_2d']
176176

177177

178+
def test_density_with_lonlatbox(regular_test_file):
179+
"""Test that the lonlatbox still works with density.
180+
181+
see https://github.com/psyplot/psy-maps/issues/43
182+
"""
183+
sp = psy.plot.mapvector(
184+
regular_test_file, name=[["u", "v"]], density=0.5, lonlatbox="Europe"
185+
)
186+
ax = sp.plotters[0].ax
187+
xmin, xmax, ymin, ymax = ax.get_extent()
188+
assert xmax - xmin < 180
189+
assert ymax - ymin < 90
190+
191+
192+
178193
if __name__ == '__main__':
179194
unittest.main()

0 commit comments

Comments
 (0)