From bd229ad9fe88bb54ffea6ca1446e7be062ff722f Mon Sep 17 00:00:00 2001 From: Kevin Donkers Date: Thu, 15 Nov 2018 16:30:31 +0000 Subject: [PATCH] Add raster caching and canvas interaction triggers to smooth pan/zoom --- lib/cartopy/mpl/slippy_image_artist.py | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/lib/cartopy/mpl/slippy_image_artist.py b/lib/cartopy/mpl/slippy_image_artist.py index 181fafb33..87d1322b8 100644 --- a/lib/cartopy/mpl/slippy_image_artist.py +++ b/lib/cartopy/mpl/slippy_image_artist.py @@ -41,6 +41,20 @@ def __init__(self, ax, raster_source, **kwargs): self.raster_source = raster_source super(SlippyImageArtist, self).__init__(ax, **kwargs) self.set_clip_path(ax.outline_patch) + self.cache = [] + + ax.figure.canvas.mpl_connect('button_press_event', self.on_press) + ax.figure.canvas.mpl_connect('button_release_event', self.on_release) + + self.on_release() + + def on_press(self, event=None): + self.user_is_interacting = True + self.stale = True + + def on_release(self, event=None): + self.user_is_interacting = False + self.stale = True @matplotlib.artist.allow_rasterization def draw(self, renderer, *args, **kwargs): @@ -50,11 +64,13 @@ def draw(self, renderer, *args, **kwargs): ax = self.axes window_extent = ax.get_window_extent() [x1, y1], [x2, y2] = ax.viewLim.get_points() - located_images = self.raster_source.fetch_raster( - ax.projection, extent=[x1, x2, y1, y2], - target_resolution=(window_extent.width, window_extent.height)) + if not self.user_is_interacting: + located_images = self.raster_source.fetch_raster( + ax.projection, extent=[x1, x2, y1, y2], + target_resolution=(window_extent.width, window_extent.height)) + self.cache = located_images - for img, extent in located_images: + for img, extent in self.cache: self.set_array(img) with ax.hold_limits(): self.set_extent(extent)