Skip to content

Instantly share code, notes, and snippets.

@urschrei
Created May 13, 2013 20:07
Show Gist options
  • Save urschrei/5571063 to your computer and use it in GitHub Desktop.
Save urschrei/5571063 to your computer and use it in GitHub Desktop.
Plot a dict of Lat, Lon points in an image. Requires the PIL.
# Courtesy of Charlie Loyd (@vruba), it's a black box for now
# Expects a dict of points, 'points' with keys 'Lat.', and 'Lon.'
from PIL import Image
from math import *
# these are London-specific
latrange = [51, 52]
lonrange = [-1, 1]
latgrain = 1 / 400.0
longrain = latgrain / cos(radians((latrange[0] + latrange[1]) / 2.0))
def K(h, amt):
r = sin(pi*h)
g = sin(pi*(h + 1.0 / 3.0))
b = sin(pi*(h + 2.0 / 3.0))
return tuple(int(amt*ch*ch) for ch in (r, g, b))
def bump(a, b):
return tuple(map(lambda x, y: x + y, a, b))
def color(lang, amt):
lang = lang.strip()
if lang == 'pt':
return (amt, 0, 0)
if lang == 'en':
return (0, amt, 0)
else:
return (0, 0, amt)
def scalelat(lat):
return (lat-latrange[0]) * 1 / latgrain
def scalelon(lon):
return (lon-lonrange[0]) * 1 / longrain
width = int(round((lonrange[1] - lonrange[0]) * 1 / longrain)) + 1
height = int(round((latrange[1] - latrange[0]) * 1 / latgrain)) + 1
print 'height: ' + str(latgrain * height * 111.2) + ' km'
print 'width: ' + str(longrain * latgrain/longrain * width * 111.2) + ' km'
i = Image.new('RGB', (width, height))
p = i.load()
for point in points:
y = int(scalelat(float(point['Lat.'])))
x = int(scalelon(float(point['Lon.'])))
y = height-y
c = (2, 2, 2)
try:
p[x, y] = bump(p[x, y], c)
except:
print("Argh!")
raise()
i.save("map.png")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment