Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix x ranges upon projection #722

Merged
merged 8 commits into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion geoviews/plotting/bokeh/callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ def project_ranges(cb, msg, attributes):
extents = x0, y0, x1, y1
x0, y0, x1, y1 = project_extents(extents, plot.projection,
plot.current_frame.crs)
if plot._unwrap_lons and -180 <= x0 < 0 or -180 <= x1 < 0:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be:

if plot._unwrap_lons and (-180 <= x0 < 0 or -180 <= x1 < 0):

x0, x1 = x0 + 360, x1 + 360
if x0 > x1:
x0, x1 = x1, x0
coords = {'x_range': (x0, x1), 'y_range': (y0, y1)}
return {k: v for k, v in coords.items() if k in attributes}

Expand Down Expand Up @@ -118,7 +122,8 @@ class GeoRangeXYCallback(RangeXYCallback):

def _process_msg(self, msg):
msg = super()._process_msg(msg)
return project_ranges(self, msg, ('x_range', 'y_range'))
ranges = project_ranges(self, msg, ('x_range', 'y_range'))
return ranges
ahuang11 marked this conversation as resolved.
Show resolved Hide resolved
ahuang11 marked this conversation as resolved.
Show resolved Hide resolved

class GeoRangeXCallback(RangeXCallback):

Expand Down
7 changes: 6 additions & 1 deletion geoviews/plotting/bokeh/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""
import param

from cartopy.crs import GOOGLE_MERCATOR, PlateCarree, Mercator
from cartopy.crs import GOOGLE_MERCATOR, PlateCarree, Mercator, _CylindricalProjection
from bokeh.models.tools import BoxZoomTool, WheelZoomTool
from bokeh.models import MercatorTickFormatter, MercatorTicker, CustomJSHover
from holoviews.core.dimension import Dimension
Expand Down Expand Up @@ -70,6 +70,11 @@ def __init__(self, element, **params):
'and disable the show_grid option.'
)

self._unwrap_lons = False
if isinstance(self.geographic, _CylindricalProjection):
x1, x2 = element.range(0)
ahuang11 marked this conversation as resolved.
Show resolved Hide resolved
self._unwrap_lons = 0 <= x1 <= 360 and 0 <= x2 <= 360

def _axis_properties(self, axis, key, plot, dimension=None,
ax_mapping=None):
if ax_mapping is None:
Expand Down
37 changes: 37 additions & 0 deletions geoviews/tests/test_operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,40 @@ def test_quadmesh_contoures_filled():
p1 = gv.QuadMesh(ds, kdims=["lon", "lat"])
p2 = contours(p1, filled=True)
gv.renderer("bokeh").get_plot(p2)


def test_unwrap_lons():
pytest.importorskip("datashader")
# Regression test for: https://github.com/holoviz/geoviews/pull/722
from holoviews.operation.datashader import rasterize
ds = xr.tutorial.open_dataset("air_temperature").isel(time=0)
p1 = gv.Image(ds)
p2 = rasterize(p1, filled=True)
gv.renderer("bokeh").get_plot(p2)
for x in p2.range(0):
assert x >= 0
assert x <= 360
p2.callback()
for x in p2.range(0):
assert x >= 0
assert x <= 360


def test_no_unwrap_lons():
pytest.importorskip("datashader")
# Regression test for: https://github.com/holoviz/geoviews/pull/722
from holoviews.operation.datashader import rasterize
ds = xr.tutorial.open_dataset("air_temperature").isel(time=0)
# to -180, 180
ds["lon"] = (ds["lon"] + 180) % 360 - 180

p1 = gv.Image(ds)
p2 = rasterize(p1, filled=True)
gv.renderer("bokeh").get_plot(p2)
for x in p2.range(0):
assert x >= -180
assert x <= 180
p2.callback()
for x in p2.range(0):
assert x >= -180
assert x <= 180
Loading