ImageGrid

class visigoth.map_layers.imagegrid.ImageGrid(r, g, b, a, lats=None, lons=None, sharpen=False)

Create an image grid plot.

Parameters
  • r (list) – red values values, organised as a list of rows, where each row is an equal size list of column values

  • g (list) – blue values, organised as a list of rows, where each row is an equal size list of column values

  • b (list) – green values, organised as a list of rows, where each row is an equal size list of column values

  • a (list) – alpha values, organised as a list of rows, where each row is an equal size list of column values

Keyword Arguments
  • lats (list) – a list of the lat values providing the center of each row

  • lons (list) – a list of the lon values providing the center of each column

  • sharpen (bool) – produce a sharper (double resolution) image at 2x resolution, slower to run

Notes

r,g,b and a array values must be integers in the range 0 - 255. None array values are not allowed. The r,g,b and a arguments can be assigned to None but at least one should be a valid array

If lons/lats are None, just creates a rectangular image from the data, this assumes that the data reflects the map projection and boundaries. Don’t set these to None unless you know what you are doing.

Example

(Link to open example in new tab):

Source Code (Link to Github):

# -*- coding: utf-8 -*-

from visigoth import Diagram
from visigoth.map_layers import ImageGrid, Geoimport
from visigoth.containers import Map, Box
from visigoth.internal.utils.httpcache import HttpCache

height = 700 # cover lats from -70 to 70 @0.2 deg
width = 1800 # cover lons from -180 to 180 @0.2 deg

lat_spacing = 0.2
lon_spacing = 0.2

# work out the centers of each cell, first the latitudes
lats = [-70+i+lat_spacing*0.5 for i in range(height)]

# then the longitudes
lons = [-180+i+lon_spacing*0.5 for i in range(width)]

r = [[int(255*col/width) for col in range(width)] for row in range(height)]
g = [[int(255*row/height) for col in range(width)] for row in range(height)]
b = [[int(255*(row/height)*(1-(col/width))) for col in range(width)] for row in range(height)]
a = [[int(row+col)%255 for col in range(width)] for row in range(height)]

d = Diagram()

m = Map(fill="#E0E0E0",width=512,boundaries=((-180,-70),(180,70)))

hc = HttpCache()
geojson_path = hc.fetch("https://raw.githubusercontent.com/johan/world.geo.json/master/countries.geo.json",returnPath=True)
gi = Geoimport(geojson_path,line_style=lambda p:{"stroke-width":1,"stroke":"black"},polygon_style=lambda p:{"fill":"#606060"})
m.add(gi)

# add an imagegrid layer to the map using the red,green,blue and alpha channel data
cg = ImageGrid(r=r,g=g,b=b,a=a,lats=lats,lons=lons,sharpen=True)
m.add(cg)

# add the map, some descriptive text and the legend to the diagram
b = Box(m,corner_radius=5,padding=5)

d.add(b)

html = d.draw(format="html")
f = open("example.html", "w")
f.write(html)
f.close()