diff --git a/llplot/google_maps_templates.py b/llplot/google_maps_templates.py index a3e3f4d..89ace8e 100644 --- a/llplot/google_maps_templates.py +++ b/llplot/google_maps_templates.py @@ -57,6 +57,11 @@ """ % EARTH_RADIUS +OVERLAY = """ +var imageUrl = "{imageUrl}", +imageBounds = {imageBounds}; +var overlay = L.imageOverlay(imageUrl, imageBounds, {settings}).addTo(llMap); +""" # FIXME: This generate a Xmark in cartesian frame rather than in lat/long. CROSS = """ diff --git a/llplot/llplot.py b/llplot/llplot.py index fc644f9..6e4ee23 100644 --- a/llplot/llplot.py +++ b/llplot/llplot.py @@ -9,7 +9,7 @@ from collections import namedtuple from llplot.color_dicts import mpl_color_map, html_color_codes -from llplot.google_maps_templates import SYMBOLS, CIRCLE +from llplot.google_maps_templates import SYMBOLS, CIRCLE, OVERLAY Symbol = namedtuple('Symbol', ['symbol', 'lat', 'long', 'size']) @@ -213,33 +213,32 @@ def _process_heatmap_kwargs(self, settings_dict): return settings_string - def ground_overlay(self, url, bounds_dict): + def ground_overlay(self, imageUrl, imageBounds, opacity=1.0, alt='', + interactive=0, crossOrigin=0, errorOverlayUrl='', + zIndex=1, className=''): ''' - :param url: Url of image to overlay - :param bounds_dict: dict of the form {'north': , 'south': , 'west': , 'east': } + :param imageUrl: Url of image to overlay + :param imageBounds: dict of the form [[40.712216, -74.22655], [40.773941, -74.12544]] setting the image container :return: None Example use: import llplot - gmap = llplot.GoogleMapPlotter(37.766956, -122.438481, 13) - bounds_dict = {'north':37.832285, 'south': 37.637336, 'west': -122.520364, 'east': -122.346922} - gmap.ground_overlay('http://explore.museumca.org/creeks/images/TopoSFCreeks.jpg', bounds_dict) - gmap.draw("my_map.html") - Google Maps API documentation - https://developers.google.com/maps/documentation/javascript/groundoverlays#introduction + lmap = llplot.LeafletPlotter(37.766956, -122.438481, 13) + bounds_dict = [[40.712216, -74.22655], [40.773941, -74.12544]] + lmap.ground_overlay('http://explore.museumca.org/creeks/images/TopoSFCreeks.jpg', bounds_dict) + lmap.draw("my_map.html") + Leaflet API documentation + https://leafletjs.com/reference-1.5.0.html#imageoverlay ''' - - bounds_string = self._process_ground_overlay_image_bounds(bounds_dict) - self.ground_overlays.append((url, bounds_string)) - - def _process_ground_overlay_image_bounds(self, bounds_dict): - bounds_string = 'var imageBounds = {' - bounds_string += "north: %.4f,\n" % bounds_dict['north'] - bounds_string += "south: %.4f,\n" % bounds_dict['south'] - bounds_string += "east: %.4f,\n" % bounds_dict['east'] - bounds_string += "west: %.4f};\n" % bounds_dict['west'] - - return bounds_string + settings = {} + settings['opacity'] = opacity + settings['alt'] = alt + settings['interactive'] = interactive + settings['crossOrigin'] = crossOrigin + settings['errorOverlayUrl'] = errorOverlayUrl + settings['zIndex'] = zIndex + settings['className'] = className + self.ground_overlays.append((imageUrl, imageBounds, settings)) def polygon(self, lats, lngs, color=None, c=None, **kwargs): color = color or c @@ -282,7 +281,7 @@ def draw(self, htmlfile, img_path=None, header=None, footer=None): # self.write_symbols(f) # self.write_shapes(f) # self.write_heatmap(f) - # self.write_ground_overlay(f) + self.write_ground_overlay(f) self.write_fitbounds(f) f.write('\t}\n') f.write('\n') @@ -509,14 +508,9 @@ def write_heatmap(self, f): def write_ground_overlay(self, f): - for url, bounds_string in self.ground_overlays: - f.write(bounds_string) - f.write('var groundOverlay;' + '\n') - f.write('groundOverlay = new google.maps.GroundOverlay(' + '\n') - f.write('\n') - f.write("'" + url + "'," + '\n') - f.write('imageBounds);' + '\n') - f.write('groundOverlay.setMap(map);' + '\n') + for url, bounds_string, settings in self.ground_overlays: + f.write(OVERLAY.format(imageUrl=url, imageBounds=bounds_string, settings=settings)) + def write_fitbounds(self, f): if self.bounding_box is None: