Problem with cartopy map tiles

224 views
Skip to first unread message

Dino

unread,
Oct 12, 2018, 1:41:32 PM10/12/18
to SciTools (iris, cartopy, cf_units, etc.) - https://github.com/scitools
I am have a problem with Cartopy maptiles.   I am using them as a baselayer for a map and want to add scroll-to-zoom capability.  This requires the ability to reload (or delete and create a new) tile service.  The example below is a much simplified version of the original code that does not implement zoom, but isolates the issue I am having.  In short, if I run this with loadOnClick=False so that the tiles are loaded at launch it works.  However if I run this with loadOnClick=True, so that the tile artist is not added until a user clicks, the tiles do not load.  I have experimented with other tile services and the results are the same.  Any ideas on what is wrong?  Thanks in advance. 

(I will delete the public key once I have a resolution to this issue)

import cartopy.crs as ccrs
from cartopy.io.img_tiles import MapboxTiles
import matplotlib.pyplot as plt
from matplotlib.figure import Figure
import tkinter as tk
import matplotlib.backends.tkagg as tkagg
from matplotlib.backends.backend_tkagg import FigureCanvas

class MapTest(tk.Frame):

        def __init__(self, master=None, loadOnClick=False):
            
            super().__init__(master)
            self.state_plane = ccrs.TransverseMercator(-88.33333333333333, 36.66666666666666, 300000 , 0, 0.9999749999999999, globe=None)
            self.createWidgets(loadOnClick)
            self.pack(side=tk.TOP, fill=tk.BOTH, expand=tk.YES)
                        
        def createWidgets(self, loadOnClick):

            self.figure = plt.figure()
            self.canvas = FigureCanvas(self.figure, self)
            self.ax = plt.axes(projection=self.state_plane)
            self.ax.set_xmargin(0.05)
            self.ax.set_ymargin(0.10)
            xmin, xmax = (322650, 322750)
            ymin, ymax = (630175, 630275)
            self.canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)
            if loadOnClick:
                    self.canvas.mpl_connect('button_press_event', self.click)
            else:
                    self.showMap(xmin, xmax, ymin, ymax)
                               
        def click(self, event):
                print("Clicked")
                xmin, xmax = (322650, 322750)
                ymin, ymax = (630175, 630275)
                self.showMap(xmin, xmax, ymin, ymax)
       
        
        def showMap(self, xmin=None, xmax=None, ymin=None, ymax=None):
            try:
                print("Showing google for", xmin, xmax, ymin, ymax)
                self.ax.set_xlim(int(xmin), int(xmax))
                self.ax.set_ylim(int(ymin), int(ymax))
                self.ax.set_xticks([xmin, xmax])
                self.ax.set_yticks([ymin, ymax])
                self.request=MapboxTiles('pk.eyJ1IjoiaWxvcGF0YTEiLCJhIjoiY2l3MThtbzlwMDc4YTJvdGFoMDJnNXdlZSJ9.Fd1nc1EslE6BZuvWJ0bvng', 'mapbox.satellite')
                self.ax.add_image(self.request, 20, zorder=0)
                self.canvas.draw()
            except:
                pass



root = tk.Tk()
root.state('zoomed')
app = MapTest(master=root, loadOnClick=False)
app.mainloop()



Dino

unread,
Oct 12, 2018, 3:55:01 PM10/12/18
to SciTools (iris, cartopy, cf_units, etc.) - https://github.com/scitools
I have a solution but it does not feel robust.

Image factories only seem to draw on the first draw on the creation of the GeoAxes.  This is controlled by the variable self._done_img_factory which is initally False and then set to True after the first draw.

That this should be controlled at the artist level and not the GeoAxes level, so that artists can be be added after initialization and will still be drawn.

For now,  I am simply going to add 
self.ax._done_img_factory = False

after the artist is added, but perhaps a topic for a future PR.
Reply all
Reply to author
Forward
0 new messages