def test_image_overlay(self): """Test image overlay.""" # from numpy.random import random from branca.utilities import write_png # import base64 data = [[[1, 0, 0, 1], [0, 0, 0, 0], [0, 0, 0, 0]], [[1, 1, 0, 0.5], [0, 0, 1, 1], [0, 0, 1, 1]]] min_lon, max_lon, min_lat, max_lat = -90.0, 90.0, -180.0, 180.0 self.setup() image_url = 'data.png' self.map.image_overlay(data, filename=image_url) out = self.map._parent.render() imageoverlay = [val for key, val in self.map._children.items() if isinstance(val, ImageOverlay)][0] png_str = write_png(data) # with open('data.png', 'wb') as f: # f.write(png_str) png = "data:image/png;base64,{}".format inline_image_url = png(base64.b64encode(png_str).decode('utf-8')) image_tpl = self.env.get_template('image_layer.js') image_name = 'Image_Overlay' image_opacity = 0.25 image_bounds = [[min_lon, min_lat], [max_lon, max_lat]] image_rendered = image_tpl.render({'image_name': image_name, 'this': imageoverlay, 'image_url': image_url, 'image_bounds': image_bounds, 'image_opacity': image_opacity}) assert ''.join(image_rendered.split()) in ''.join(out.split()) self.setup() self.map.image_overlay(data, mercator_project=True) out = self.map._parent.render() imageoverlay = [val for key, val in self.map._children.items() if isinstance(val, ImageOverlay)][0] image_rendered = image_tpl.render({'image_name': image_name, 'this': imageoverlay, 'image_url': inline_image_url, 'image_bounds': image_bounds, 'image_opacity': image_opacity}) assert ''.join(image_rendered.split()) in ''.join(out.split()) bounds = self.map.get_bounds() assert bounds == [[-90.0, -180.0], [90.0, 180.0]], bounds
def image_overlay(self, data, opacity=0.25, min_lat=-90.0, max_lat=90.0, min_lon=-180.0, max_lon=180.0, origin='upper', colormap=None, image_name=None, filename=None, mercator_project=False): """ Simple image overlay of raster data from a numpy array. This is a lightweight way to overlay geospatial data on top of a map. If your data is high res, consider implementing a WMS server and adding a WMS layer. This function works by generating a PNG file from a numpy array. If you do not specify a filename, it will embed the image inline. Otherwise, it saves the file in the current directory, and then adds it as an image overlay layer in leaflet.js. By default, the image is placed and stretched using bounds that cover the entire globe. Parameters ---------- data: numpy array OR url string, required. if numpy array, must be a image format, i.e., NxM (mono), NxMx3 (rgb), or NxMx4 (rgba) if url, must be a valid url to a image (local or external) opacity: float, default 0.25 Image layer opacity in range 0 (transparent) to 1 (opaque) min_lat: float, default -90.0 max_lat: float, default 90.0 min_lon: float, default -180.0 max_lon: float, default 180.0 image_name: string, default None The name of the layer object in leaflet.js filename: string, default None Optional file name of output.png for image overlay. Use `None` for inline PNG. origin : ['upper' | 'lower'], optional, default 'upper' Place the [0,0] index of the array in the upper left or lower left corner of the axes. colormap : callable, used only for `mono` image. Function of the form [x -> (r,g,b)] or [x -> (r,g,b,a)] for transforming a mono image into RGB. It must output iterables of length 3 or 4, with values between 0 and 1. Hint: you can use colormaps from `matplotlib.cm`. mercator_project : bool, default False, used only for array-like image. Transforms the data to project (longitude,latitude) coordinates to the Mercator projection. Returns ------- Image overlay data layer in obj.template_vars Examples -------- # assumes a map object `m` has been created >>> import numpy as np >>> data = np.random.random((100,100)) # to make a rgba from a specific matplotlib colormap: >>> import matplotlib.cm as cm >>> cmapper = cm.cm.ColorMapper('jet') >>> data2 = cmapper.to_rgba(np.random.random((100,100))) >>> # Place the data over all of the globe (will be pretty pixelated!) >>> m.image_overlay(data) >>> # Put it only over a single city (Paris). >>> m.image_overlay(data, min_lat=48.80418, max_lat=48.90970, ... min_lon=2.25214, max_lon=2.44731) """ msg = ('This method is deprecated. Please use ' '`Map.add_children(folium.plugins.ImageOverlay(...))` instead.') warnings.warn(msg) from .plugins import ImageOverlay from branca.utilities import write_png if filename: image = write_png(data, origin=origin, colormap=colormap) open(filename, 'wb').write(image) data = filename self.add_child(ImageOverlay(data, [[min_lat, min_lon], [max_lat, max_lon]], opacity=opacity, origin=origin, colormap=colormap, mercator_project=mercator_project))
def test_image_overlay(self): """Test image overlay.""" # from numpy.random import random from branca.utilities import write_png # import base64 data = [[[1, 0, 0, 1], [0, 0, 0, 0], [0, 0, 0, 0]], [[1, 1, 0, 0.5], [0, 0, 1, 1], [0, 0, 1, 1]]] min_lon, max_lon, min_lat, max_lat = -90.0, 90.0, -180.0, 180.0 self.setup() image_url = 'data.png' self.map.image_overlay(data, filename=image_url) out = self.map._parent.render() imageoverlay = [ val for key, val in self.map._children.items() if isinstance(val, ImageOverlay) ][0] png_str = write_png(data) # with open('data.png', 'wb') as f: # f.write(png_str) png = "data:image/png;base64,{}".format inline_image_url = png(base64.b64encode(png_str).decode('utf-8')) image_tpl = self.env.get_template('image_layer.js') image_name = 'Image_Overlay' image_opacity = 0.25 image_bounds = [[min_lon, min_lat], [max_lon, max_lat]] image_rendered = image_tpl.render({ 'image_name': image_name, 'this': imageoverlay, 'image_url': image_url, 'image_bounds': image_bounds, 'image_opacity': image_opacity }) assert ''.join(image_rendered.split()) in ''.join(out.split()) self.setup() self.map.image_overlay(data, mercator_project=True) out = self.map._parent.render() imageoverlay = [ val for key, val in self.map._children.items() if isinstance(val, ImageOverlay) ][0] image_rendered = image_tpl.render({ 'image_name': image_name, 'this': imageoverlay, 'image_url': inline_image_url, 'image_bounds': image_bounds, 'image_opacity': image_opacity }) assert ''.join(image_rendered.split()) in ''.join(out.split()) bounds = self.map.get_bounds() assert bounds == [[-90.0, -180.0], [90.0, 180.0]], bounds
def image_overlay(self, data, opacity=0.25, min_lat=-90.0, max_lat=90.0, min_lon=-180.0, max_lon=180.0, origin='upper', colormap=None, image_name=None, filename=None, mercator_project=False): """ Simple image overlay of raster data from a numpy array. This is a lightweight way to overlay geospatial data on top of a map. If your data is high res, consider implementing a WMS server and adding a WMS layer. This function works by generating a PNG file from a numpy array. If you do not specify a filename, it will embed the image inline. Otherwise, it saves the file in the current directory, and then adds it as an image overlay layer in leaflet.js. By default, the image is placed and stretched using bounds that cover the entire globe. Parameters ---------- data: numpy array OR url string, required. if numpy array, must be a image format, i.e., NxM (mono), NxMx3 (rgb), or NxMx4 (rgba) if url, must be a valid url to a image (local or external) opacity: float, default 0.25 Image layer opacity in range 0 (transparent) to 1 (opaque) min_lat: float, default -90.0 max_lat: float, default 90.0 min_lon: float, default -180.0 max_lon: float, default 180.0 image_name: string, default None The name of the layer object in leaflet.js filename: string, default None Optional file name of output.png for image overlay. Use `None` for inline PNG. origin : ['upper' | 'lower'], optional, default 'upper' Place the [0,0] index of the array in the upper left or lower left corner of the axes. colormap : callable, used only for `mono` image. Function of the form [x -> (r,g,b)] or [x -> (r,g,b,a)] for transforming a mono image into RGB. It must output iterables of length 3 or 4, with values between 0 and 1. Hint: you can use colormaps from `matplotlib.cm`. mercator_project : bool, default False, used only for array-like image. Transforms the data to project (longitude,latitude) coordinates to the Mercator projection. Returns ------- Image overlay data layer in obj.template_vars Examples -------- # assumes a map object `m` has been created >>> import numpy as np >>> data = np.random.random((100,100)) # to make a rgba from a specific matplotlib colormap: >>> import matplotlib.cm as cm >>> cmapper = cm.cm.ColorMapper('jet') >>> data2 = cmapper.to_rgba(np.random.random((100,100))) >>> # Place the data over all of the globe (will be pretty pixelated!) >>> m.image_overlay(data) >>> # Put it only over a single city (Paris). >>> m.image_overlay(data, min_lat=48.80418, max_lat=48.90970, ... min_lon=2.25214, max_lon=2.44731) """ msg = ('This method is deprecated. Please use ' '`Map.add_children(folium.plugins.ImageOverlay(...))` instead.') warnings.warn(msg) from .plugins import ImageOverlay from branca.utilities import write_png if filename: image = write_png(data, origin=origin, colormap=colormap) open(filename, 'wb').write(image) data = filename self.add_child( ImageOverlay(data, [[min_lat, min_lon], [max_lat, max_lon]], opacity=opacity, origin=origin, colormap=colormap, mercator_project=mercator_project))