Created
May 18, 2013 09:26
-
-
Save gapato/5603853 to your computer and use it in GitHub Desktop.
some trivial stuff to plot jcdecaux data
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
import math, json, glob, time | |
from numpy import * | |
from matplotlib.pyplot import * | |
from matplotlib import animation | |
# http://wiki.openstreetmap.org/wiki/Slippy_map_tilenames#Python | |
def deg2num(lat_deg, lon_deg): | |
lat_rad = math.radians(lat_deg) | |
x = (lon_deg + 180.0) / 360.0 | |
y = (1.0 - math.log(math.tan(lat_rad) + (1 / math.cos(lat_rad))) / math.pi) / 2.0 | |
return (x, y) | |
# http://docs.python.org/2/library/json.html | |
def parse_city(filename): | |
with open(filename) as f: | |
city = json.load(f) | |
n = len(city)-1 | |
coords_x = zeros(n) | |
coords_y = zeros(n) | |
bikes = zeros(n) | |
stands = zeros(n) | |
update = zeros(n) | |
i = 0 | |
for station in city: | |
gps = station['position'] | |
cartesian = deg2num(gps['lat'], gps['lng']) | |
if cartesian[1] < .4: # Paris | |
(coords_x[i], coords_y[i]) = cartesian | |
bikes[i] = station['available_bikes'] | |
stands[i] = station['bike_stands'] | |
update[i] = station['last_update'] | |
i += 1 | |
return (coords_x, -coords_y, bikes, stands) | |
# http://matplotlib.org/examples/pylab_examples/polar_scatter.html | |
def city_scatter(filename, surface='bikes', scale=lambda x:5*x): | |
(x, y, bikes, stands) = parse_city(filename) | |
data = { 'bikes': bikes, | |
'stands': stands, | |
'empty': stands - bikes } | |
c = scatter(x, y, s=scale(data[surface])) | |
c.set_alpha(0.5) | |
gca().set_aspect('equal') | |
tight_layout() | |
xlim(x.min(), x.max()) | |
ylim(y.min(), y.max()) | |
def make_anim(): | |
fig = figure(figsize=(16,12)) | |
files = glob.glob('Paris/*') | |
files.sort() | |
files = files[:146] | |
scale = lambda x:5*x+1 | |
title(time.strftime('%c', time.localtime(float(files[0].split('/')[1])))) | |
(x, y, bikes, stands) = parse_city(files[0]) | |
scat = scatter(x, y, s=scale(bikes)) | |
xlim(x.min(), x.max()) | |
ylim(y.min(), y.max()) | |
def update_plot(i): | |
(x, y, bikes, stands) = parse_city(files[i]) | |
title(time.strftime('%c', time.localtime(float(files[i].split('/')[1])))) | |
scat._sizes = scale(bikes) | |
return scat, | |
ani = animation.FuncAnimation(fig, update_plot, frames=len(files)-1) | |
ani.save('pop.mp4', fps=20) | |
#make_anim() | |
files = glob.glob('Paris/*') | |
files.sort() | |
figure() | |
city_scatter(files[0], surface='stands') | |
figure() | |
city_scatter(files[0], surface='bikes') | |
show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment