mapclient.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. #!/usr/bin/env python
  2. """A slippy map GUI.
  3. Implements a tiled slippy map using Tk canvas. Displays map tiles using
  4. whatever projection the tiles are in and only knows about tile coordinates,
  5. (as opposed to geospatial coordinates.) This assumes that the tile-space is
  6. organized as a power-of-two pyramid, with the origin in the upper left corner.
  7. This currently has several spots that are hard-coded for 256x256 tiles, even
  8. though MapOverlay tries to track this.
  9. Supports mouse-based pan and zoom as well as tile upsampling while waiting
  10. for new tiles to load. The map to display is specified by a MapOverlay, and
  11. added to the GUI on creation or manually using addOverlay()
  12. gui = MapClient(MakeOverlay(mapid))
  13. Tiles are referenced using a key of (level, x, y) throughout.
  14. Several of the functions are named to match the Google Maps Javascript API,
  15. and therefore violate style guidelines.
  16. """
  17. # TODO(user):
  18. # 1) Add a zoom bar.
  19. # 2) When the move() is happening inside the Drag function, it'd be
  20. # a good idea to use a semaphore to keep new tiles from being added
  21. # and subsequently moved.
  22. from collections import abc
  23. import functools
  24. import io
  25. import math
  26. import queue
  27. import sys
  28. import threading
  29. import tkinter as Tkinter
  30. import urllib.request
  31. # check if the Python imaging libraries used by the mapclient module are
  32. # installed
  33. try:
  34. # Python3
  35. from PIL import ImageTk # pylint: disable=g-import-not-at-top
  36. from PIL import Image # pylint: disable=g-import-not-at-top
  37. except ImportError:
  38. try:
  39. # Python2
  40. import ImageTk # pylint: disable=g-import-not-at-top
  41. import Image # pylint: disable=g-import-not-at-top
  42. except ImportError:
  43. print("""
  44. ERROR: A Python library (PIL) used by the Earth Engine API mapclient module
  45. was not found. Information on PIL can be found at:
  46. http://pypi.python.org/pypi/PIL
  47. """)
  48. raise
  49. try:
  50. pass
  51. except ImportError:
  52. print("""
  53. ERROR: A Python library (Tkinter) used by the Earth Engine API mapclient
  54. module was not found. Instructions for installing Tkinter can be found at:
  55. http://tkinter.unpythonic.net/wiki/How_to_install_Tkinter
  56. """)
  57. raise
  58. # The default URL to fetch tiles from. We could pull this from the EE library,
  59. # however this doesn't have any other dependencies on that yet, so let's not.
  60. BASE_URL = 'https://earthengine.googleapis.com'
  61. # This is a URL pattern for creating an overlay from the google maps base map.
  62. # The z, x and y arguments at the end correspond to level, x, y here.
  63. DEFAULT_MAP_URL_PATTERN = ('http://mt1.google.com/vt/lyrs=m@176000000&hl=en&'
  64. 'src=app&z=%d&x=%d&y=%d')
  65. class MapClient(threading.Thread):
  66. """A simple discrete zoom level map viewer."""
  67. def __init__(self, opt_overlay=None, opt_width=1024, opt_height=768):
  68. """Initialize the MapClient UI.
  69. Args:
  70. opt_overlay: A mapoverlay to display. If not specified, the default
  71. Google Maps basemap is used.
  72. opt_width: The default width of the frame to construct.
  73. opt_height: The default height of the frame to construct.
  74. """
  75. threading.Thread.__init__(self)
  76. self.ready = False # All initialization is done.
  77. self.tiles = {} # The cached stack of images at each grid cell.
  78. self.tktiles = {} # The cached PhotoImage at each grid cell.
  79. self.level = 2 # Starting zoom level
  80. self.origin_x = None # The map origin x offset at the current level.
  81. self.origin_y = None # The map origin y offset at the current level.
  82. self.parent = None # A handle to the top level Tk widget.
  83. self.frame = None # A handle to the Tk frame.
  84. self.canvas = None # A handle to the Tk canvas
  85. self.width = opt_width
  86. self.height = opt_height
  87. self.anchor_x = None # Drag anchor.
  88. self.anchor_y = None # Drag anchor.
  89. # Map origin offsets; start at the center of the map.
  90. self.origin_x = (-(2 ** self.level) * 128) + self.width / 2
  91. self.origin_y = (-(2 ** self.level) * 128) + self.height / 2
  92. if not opt_overlay:
  93. # Default to a google maps basemap
  94. opt_overlay = MapOverlay(DEFAULT_MAP_URL_PATTERN)
  95. # The array of overlays are displayed as last on top.
  96. self.overlays = [opt_overlay]
  97. self.start()
  98. def run(self):
  99. """Set up the user interface."""
  100. width = self.width
  101. height = self.height
  102. # Build the UI
  103. self.parent = Tkinter.Tk()
  104. self.frame = frame = Tkinter.Frame(self.parent, width=width, height=height)
  105. frame.pack(fill=Tkinter.BOTH, expand=Tkinter.YES)
  106. self.canvas = canvas = Tkinter.Canvas(frame,
  107. width=self.GetFrameSize()[0],
  108. height=self.GetFrameSize()[1])
  109. canvas.pack(side=Tkinter.TOP, fill=Tkinter.BOTH, expand=Tkinter.YES)
  110. canvas.create_rectangle(0, 0, self.GetMapSize()[0], self.GetMapSize()[1],
  111. fill='#888888')
  112. canvas.bind('<Button-1>', self.ClickHandler)
  113. canvas.bind('<ButtonRelease-1>', self.ReleaseHandler)
  114. # Button-4 and Button-5 are scroll wheel up/down events.
  115. canvas.bind('<Button-4>', functools.partial(self.Zoom, direction=1))
  116. canvas.bind('<Button-5>', functools.partial(self.Zoom, direction=-1))
  117. canvas.bind('<Double-Button-1>', functools.partial(self.Zoom, direction=1))
  118. frame.bind('<Configure>', self.ResizeHandler)
  119. frame.bind_all('<Key>', self.KeypressHandler)
  120. def SetReady():
  121. self.ready = True
  122. self.parent.after_idle(SetReady)
  123. self.parent.mainloop()
  124. def addOverlay(self, overlay): # pylint: disable=g-bad-name
  125. """Add an overlay to the map."""
  126. self.overlays.append(overlay)
  127. self.LoadTiles()
  128. def GetFrameSize(self):
  129. if self.frame:
  130. return (int(self.frame.cget('width')), int(self.frame.cget('height')))
  131. else:
  132. return (self.width, self.height)
  133. def GetMapSize(self):
  134. if self.frame:
  135. return (int(self.canvas.cget('width')), int(self.canvas.cget('height')))
  136. else:
  137. return (self.width, self.height)
  138. def GetViewport(self):
  139. """Return the visible portion of the map as [xlo, ylo, xhi, yhi]."""
  140. width, height = self.GetMapSize()
  141. # pylint: disable=invalid-unary-operand-type
  142. return [-self.origin_x, -self.origin_y,
  143. -self.origin_x + width, -self.origin_y + height]
  144. def LoadTiles(self):
  145. """Refresh the entire map."""
  146. # Start with the overlay on top.
  147. if not self.ready:
  148. return
  149. for i, overlay in reversed(list(enumerate(self.overlays))):
  150. tile_list = overlay.CalcTiles(self.level, self.GetViewport())
  151. for key in tile_list:
  152. overlay.getTile(key, functools.partial(
  153. self.AddTile, key=key, overlay=overlay, layer=i))
  154. def Flush(self):
  155. """Empty out all the image fetching queues."""
  156. for overlay in self.overlays:
  157. overlay.Flush()
  158. def CompositeTiles(self, key):
  159. """Composite together all the tiles in this cell into a single image."""
  160. composite = None
  161. for layer in sorted(self.tiles[key]):
  162. image = self.tiles[key][layer]
  163. if not composite:
  164. composite = image.copy()
  165. else:
  166. composite.paste(image, (0, 0), image)
  167. return composite
  168. def AddTile(self, image, key, overlay, layer):
  169. """Add a tile to the map.
  170. This keeps track of the tiles for each overlay in each grid cell.
  171. As new tiles come in, all the tiles in a grid cell are composited together
  172. into a new tile and any old tile for that spot is replaced.
  173. Args:
  174. image: The image tile to display.
  175. key: A tuple containing the key of the image (level, x, y)
  176. overlay: The overlay this tile belongs to.
  177. layer: The layer number this overlay corresponds to. Only used
  178. for caching purposes.
  179. """
  180. # TODO(user): This function is called from multiple threads, and
  181. # could use some synchronization, but it seems to work.
  182. if self.level == key[0]: # Don't add late tiles from another level.
  183. self.tiles[key] = self.tiles.get(key, {})
  184. self.tiles[key][layer] = image
  185. newtile = self.CompositeTiles(key)
  186. if key not in self.tktiles:
  187. newtile = ImageTk.PhotoImage(newtile)
  188. xpos = key[1] * overlay.TILE_WIDTH + self.origin_x
  189. ypos = key[2] * overlay.TILE_HEIGHT + self.origin_y
  190. self.canvas.create_image(
  191. xpos, ypos, anchor=Tkinter.NW, image=newtile, tags=['tile', key])
  192. self.tktiles[key] = newtile # Hang on to the new tile.
  193. else:
  194. self.tktiles[key].paste(newtile)
  195. def Zoom(self, event, direction):
  196. """Zoom the map.
  197. Args:
  198. event: The event that caused this zoom request.
  199. direction: The direction to zoom. +1 for higher zoom, -1 for lower.
  200. """
  201. if self.level + direction >= 0:
  202. # Discard everything cached in the MapClient, and flush the fetch queues.
  203. self.Flush()
  204. self.canvas.delete(Tkinter.ALL)
  205. self.tiles = {}
  206. self.tktiles = {}
  207. if direction > 0:
  208. self.origin_x = self.origin_x * 2 - event.x
  209. self.origin_y = self.origin_y * 2 - event.y
  210. else:
  211. self.origin_x = (self.origin_x + event.x) / 2
  212. self.origin_y = (self.origin_y + event.y) / 2
  213. self.level += direction
  214. self.LoadTiles()
  215. def ClickHandler(self, event):
  216. """Records the anchor location and sets drag handler."""
  217. self.anchor_x = event.x
  218. self.anchor_y = event.y
  219. self.canvas.bind('<Motion>', self.DragHandler)
  220. def DragHandler(self, event):
  221. """Updates the map position and anchor position."""
  222. dx = event.x - self.anchor_x
  223. dy = event.y - self.anchor_y
  224. if dx or dy:
  225. self.canvas.move(Tkinter.ALL, dx, dy)
  226. self.origin_x += dx
  227. self.origin_y += dy
  228. self.anchor_x = event.x
  229. self.anchor_y = event.y
  230. def ReleaseHandler(self, unused_event):
  231. """Unbind drag handler and redraw."""
  232. self.canvas.unbind('<Motion>')
  233. self.LoadTiles()
  234. def ResizeHandler(self, event):
  235. """Handle resize events."""
  236. # There's a 2 pixel border.
  237. self.canvas.config(width=event.width - 2, height=event.height - 2)
  238. self.LoadTiles()
  239. def CenterMap(self, lon, lat, opt_zoom=None):
  240. """Center the map at the given lon, lat and zoom level."""
  241. if self.canvas:
  242. self.Flush()
  243. self.canvas.delete(Tkinter.ALL)
  244. self.tiles = {}
  245. self.tktiles = {}
  246. width, height = self.GetMapSize()
  247. if opt_zoom is not None:
  248. self.level = opt_zoom
  249. # From maps/api/javascript/geometry/mercator_projection.js
  250. mercator_range = 256.0
  251. scale = 2 ** self.level
  252. origin_x = (mercator_range / 2.0) * scale
  253. origin_y = (mercator_range / 2.0) * scale
  254. pixels_per_lon_degree = (mercator_range / 360.0) * scale
  255. pixels_per_lon_radian = (mercator_range / (2 * math.pi)) * scale
  256. x = origin_x + (lon * pixels_per_lon_degree)
  257. siny = math.sin(lat * math.pi / 180.0)
  258. # Prevent sin() overflow.
  259. e = 1 - 1e-15
  260. if siny > e:
  261. siny = e
  262. elif siny < -e:
  263. siny = -e
  264. y = origin_y + (0.5 * math.log((1 + siny) / (1 - siny)) *
  265. -pixels_per_lon_radian)
  266. self.origin_x = -x + width / 2
  267. self.origin_y = -y + height / 2
  268. self.LoadTiles()
  269. def KeypressHandler(self, event):
  270. """Handle keypress events."""
  271. if event.char == 'q' or event.char == 'Q':
  272. self.parent.destroy()
  273. class MapOverlay(object):
  274. """A class representing a map overlay."""
  275. TILE_WIDTH = 256
  276. TILE_HEIGHT = 256
  277. MAX_CACHE = 1000 # The maximum number of tiles to cache.
  278. _images = {} # The tile cache, keyed by (url, level, x, y).
  279. _lru_keys = [] # Keys to the cached tiles, for cache ejection.
  280. def __init__(self, url, tile_fetcher=None):
  281. """Initialize the MapOverlay."""
  282. self.url = url
  283. self.tile_fetcher = tile_fetcher
  284. # Make 10 workers.
  285. self.queue = queue.Queue()
  286. self.fetchers = [MapOverlay.TileFetcher(self) for unused_x in range(10)]
  287. self.constant = None
  288. def getTile(self, key, callback): # pylint: disable=g-bad-name
  289. """Get the requested tile.
  290. If the requested tile is already cached, it's returned (sent to the
  291. callback) directly. If it's not cached, a check is made to see if
  292. a lower-res version is cached, and if so that's interpolated up, before
  293. a request for the actual tile is made.
  294. Args:
  295. key: The key of the tile to fetch.
  296. callback: The callback to call when the tile is available. The callback
  297. may be called more than once if a low-res version is available.
  298. """
  299. result = self.GetCachedTile(key)
  300. if result:
  301. callback(result)
  302. else:
  303. # Interpolate what we have and put the key on the fetch queue.
  304. self.queue.put((key, callback))
  305. self.Interpolate(key, callback)
  306. def Flush(self):
  307. """Empty the tile queue."""
  308. while not self.queue.empty():
  309. self.queue.get_nowait()
  310. def CalcTiles(self, level, bbox):
  311. """Calculate which tiles to load based on the visible viewport.
  312. Args:
  313. level: The level at which to calculate the required tiles.
  314. bbox: The viewport coordinates as a tuple (xlo, ylo, xhi, yhi])
  315. Returns:
  316. The list of tile keys to fill the given viewport.
  317. """
  318. tile_list = []
  319. for y in range(
  320. int(bbox[1] / MapOverlay.TILE_HEIGHT),
  321. int(bbox[3] / MapOverlay.TILE_HEIGHT + 1)):
  322. for x in range(
  323. int(bbox[0] / MapOverlay.TILE_WIDTH),
  324. int(bbox[2] / MapOverlay.TILE_WIDTH + 1)):
  325. tile_list.append((level, x, y))
  326. return tile_list
  327. def Interpolate(self, key, callback):
  328. """Upsample a lower res tile if one is available.
  329. Args:
  330. key: The tile key to upsample.
  331. callback: The callback to call when the tile is ready.
  332. """
  333. level, x, y = key
  334. delta = 1
  335. result = None
  336. while level - delta > 0 and result is None:
  337. prevkey = (level - delta, x / 2, y / 2)
  338. result = self.GetCachedTile(prevkey)
  339. if not result:
  340. (_, x, y) = prevkey
  341. delta += 1
  342. if result:
  343. px = (key[1] % 2 ** delta) * MapOverlay.TILE_WIDTH / 2 ** delta
  344. py = (key[2] % 2 ** delta) * MapOverlay.TILE_HEIGHT / 2 ** delta
  345. image = (result.crop([px, py,
  346. px + MapOverlay.TILE_WIDTH / 2 ** delta,
  347. py + MapOverlay.TILE_HEIGHT / 2 ** delta])
  348. .resize((MapOverlay.TILE_WIDTH, MapOverlay.TILE_HEIGHT)))
  349. callback(image)
  350. def PutCacheTile(self, key, image):
  351. """Insert a new tile in the cache and eject old ones if it's too big."""
  352. cache_key = (self.url,) + key
  353. MapOverlay._images[cache_key] = image
  354. MapOverlay._lru_keys.append(cache_key)
  355. while len(MapOverlay._lru_keys) > MapOverlay.MAX_CACHE:
  356. remove_key = MapOverlay._lru_keys.pop(0)
  357. try:
  358. MapOverlay._images.pop(remove_key)
  359. except KeyError:
  360. # Just in case someone removed this before we did.
  361. pass
  362. def GetCachedTile(self, key):
  363. """Returns the specified tile if it's in the cache."""
  364. cache_key = (self.url,) + key
  365. return MapOverlay._images.get(cache_key, None)
  366. class TileFetcher(threading.Thread):
  367. """A threaded URL fetcher."""
  368. def __init__(self, overlay):
  369. threading.Thread.__init__(self)
  370. self.overlay = overlay
  371. self.setDaemon(True)
  372. self.start()
  373. def run(self):
  374. """Pull URLs off the ovelay's queue and call the callback when done."""
  375. while True:
  376. (key, callback) = self.overlay.queue.get()
  377. # Check one more time that we don't have this yet.
  378. if not self.overlay.GetCachedTile(key):
  379. (level, x, y) = key
  380. if x >= 0 and y >= 0 and x <= 2 ** level-1 and y <= 2 ** level-1:
  381. try:
  382. if self.overlay.tile_fetcher is not None:
  383. data = self.overlay.tile_fetcher.fetch_tile(x=x, y=y, z=level)
  384. else:
  385. url = self.overlay.url % key
  386. data = urllib.request.urlopen(url).read()
  387. except Exception as e: # pylint: disable=broad-except
  388. print(e, file=sys.stderr)
  389. else:
  390. # PhotoImage can't handle alpha on LA images.
  391. image = Image.open(io.BytesIO(data)).convert('RGBA')
  392. callback(image)
  393. self.overlay.PutCacheTile(key, image)
  394. def MakeOverlay(mapid, baseurl=BASE_URL):
  395. """Create an overlay from a mapid."""
  396. url = (baseurl + '/map/' + mapid['mapid'] + '/%d/%d/%d?token=' +
  397. mapid['token'])
  398. return MapOverlay(url, tile_fetcher=mapid['tile_fetcher'])
  399. #
  400. # A global MapClient instance for addToMap convenience.
  401. #
  402. map_instance = None
  403. # pylint: disable=g-bad-name
  404. def addToMap(eeobject, vis_params=None, *unused_args):
  405. """Adds a layer to the default map instance.
  406. Args:
  407. eeobject: the object to add to the map.
  408. vis_params: a dictionary of visualization parameters. See
  409. ee.data.getMapId().
  410. *unused_args: unused arguments, left for compatibility with the JS API.
  411. This call exists to be an equivalent to the playground addToMap() call.
  412. It uses a global MapInstance to hang on to "the map". If the MapInstance
  413. isn't initialized, this creates a new one.
  414. """
  415. # Flatten any lists to comma separated strings.
  416. if vis_params:
  417. vis_params = dict(vis_params)
  418. for key in vis_params:
  419. item = vis_params.get(key)
  420. if (isinstance(item, abc.Iterable) and not isinstance(item, str)):
  421. vis_params[key] = ','.join([str(x) for x in item])
  422. overlay = MakeOverlay(eeobject.getMapId(vis_params))
  423. global map_instance
  424. if not map_instance:
  425. map_instance = MapClient()
  426. map_instance.addOverlay(overlay)
  427. def centerMap(lng, lat, zoom): # pylint: disable=g-bad-name
  428. """Center the default map instance at the given lat, lon and zoom values."""
  429. global map_instance
  430. if not map_instance:
  431. map_instance = MapClient()
  432. map_instance.CenterMap(lng, lat, zoom)