Created
August 4, 2013 20:08
-
-
Save celoyd/6151749 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import psycopg2 as pg | |
import Image | |
from sys import argv | |
conn = pg.connect(database='tweets', user='char', host='localhost') | |
cur = conn.cursor() | |
pxper = 16 | |
# south = 37 | |
# north = 45 | |
# east = -67 | |
# west = -80 | |
south = 24 | |
north = 50 | |
east = -64 | |
west = -130 | |
width_deg = east - west | |
height_deg = north - south | |
width_px = width_deg * pxper | |
height_px = height_deg * pxper | |
print (width_px, height_px) | |
out = Image.new('RGB', (width_px, height_px)) | |
o = out.load() | |
def scale(lat, lon): | |
lat -= south | |
lon -= west | |
lat = height_px - int(lat * pxper) | |
lon = int(lon * pxper) | |
return lat, lon | |
def bump(n, rgb): | |
rgb = list(rgb) | |
rgb[n] += 80 | |
return tuple(rgb) | |
cur.execute('select lat, lon, text from tweets where lat > %s and lat < %s and lon > %s and lon < %s', (south, north, west, east)) | |
for pt in cur: | |
lat, lon = scale(pt[0], pt[1]) | |
if 'beach' in pt[2]: n = 0 | |
elif 'river' in pt[2]: n = 1 | |
elif 'bbq' in pt[2] or 'BBQ' in pt[2]: n = 2 | |
else: continue | |
try: | |
o[lon, lat] = bump(n, o[lon, lat]) | |
except IndexError: | |
# Fenceposts: | |
print lon, lat | |
out.save(argv[1]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment