Mercurial > hg > nsaunier > traffic-intelligence
comparison scripts/visualize-monresovelo.py @ 763:277e9cdcedce dev
added demo script for monresovelo
| author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
|---|---|
| date | Sun, 29 Nov 2015 00:22:58 -0500 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 762:d6f0e0cab07d | 763:277e9cdcedce |
|---|---|
| 1 # -*- coding: utf-8 -*- | |
| 2 import simplejson, sys, datetime | |
| 3 import moving, utils | |
| 4 from pyproj import Proj | |
| 5 | |
| 6 import matplotlib.pyplot as plt | |
| 7 import matplotlib.mlab as pylab | |
| 8 import numpy as np | |
| 9 import pandas as pd | |
| 10 | |
| 11 #notes = [f['properties']['notes'] for f in data['features'] if len(f['properties']['notes']) > 0] | |
| 12 | |
| 13 english2French = {'Commute': 'Domicile-travail', | |
| 14 'Errand': 'Courses', | |
| 15 'Exercise': 'Sport', | |
| 16 'Leisure': 'Loisirs', | |
| 17 'Other': 'Autre', | |
| 18 'Autres': 'Autre', | |
| 19 'Autres motifs': 'Autre', | |
| 20 'School': u'École', | |
| 21 'Shopping': 'Magasinage', | |
| 22 'Work-Related': 'Travail', | |
| 23 'Work-related': 'Travail', | |
| 24 'Other': 'Autre', | |
| 25 'other': 'Autre'} | |
| 26 | |
| 27 odMotifs = ['Magasinage', 'Retour au domicile', "Chercher quelqu'un", | |
| 28 'Travail ', '\xc9tude / \xc9cole', 'Autre', 'Loisir', | |
| 29 "Visites d'ami(e)s et/ou de la parent\xe9", "Reconduire quelqu'un", | |
| 30 "Rendez-vous d'affaires", 'Sant\xe9', | |
| 31 'Ind\xe9termin\xe9 / refus / NSP'] | |
| 32 | |
| 33 def convertJsonToMTM(data = None, filename = None): | |
| 34 'Converts the in put json data to MTM and optionally saves to file' | |
| 35 proj = Proj(init="epsg:2950") | |
| 36 if data is None: | |
| 37 data = simplejson.load(open(filename)) | |
| 38 for i in xrange(len(data['features'])): | |
| 39 latlon = data['features'][i]['geometry']['coordinates'] | |
| 40 mtm = [proj(p[0], p[1]) for p in latlon] | |
| 41 data['features'][i]['geometry']['coordinates'] = mtm | |
| 42 if filename is not None: | |
| 43 simplejson.dump(data, open(utils.removeExtension(filename)+'-mtm.json', 'w')) | |
| 44 return data | |
| 45 | |
| 46 def convertToObjects(data, timeStep = 1, project = True, minLength = 10): | |
| 47 'Converts the trips to the moving.MovingObject class from Traffic Intelligence' | |
| 48 if project: | |
| 49 proj = Proj(init="epsg:2950") | |
| 50 objects = [] | |
| 51 nTrips = len(data['features']) | |
| 52 for i in xrange(nTrips): | |
| 53 latlon = data['features'][i]['geometry']['coordinates'] | |
| 54 if project: | |
| 55 projectedX = [proj(p[0], p[1]) for p in latlon[::timeStep]] | |
| 56 else: | |
| 57 projectedX = latlon[::timeStep] | |
| 58 o = moving.MovingObject(num = i, positions = moving.Trajectory(np.array(projectedX).T.tolist())) | |
| 59 o.properties = data['features'][i]['properties'] | |
| 60 o.motif = english2French.get(o.properties['purpose'], o.properties['purpose']) | |
| 61 try: | |
| 62 o.start = datetime.datetime.strptime(o.properties['start'], '%Y-%m-%d %H:%M:%S') | |
| 63 o.stop = datetime.datetime.strptime(o.properties['stop'], '%Y-%m-%d %H:%M:%S') | |
| 64 if o.positions.length() > minLength: | |
| 65 objects.append(o) | |
| 66 except TypeError: | |
| 67 print('{} {}'.format(o.properties['start'], o.properties['stop'])) | |
| 68 print('issue with {}'.format(o.properties)) | |
| 69 #o.start = datetime.datetime(1979, 7, 21) | |
| 70 #o.stop = o.start | |
| 71 #print(e) | |
| 72 return objects | |
| 73 | |
| 74 filename = './trip5000.json' | |
| 75 data = simplejson.load(open(filename)) | |
| 76 # od = pd.read_csv('/home/nicolas/tmp/defivelomtl/velo-od-2013/od13niv2 - Résident MTL avec vélo.csv', delimiter = ';') | |
| 77 | |
| 78 #convertJsonToMTM(data) | |
| 79 #objects = convertToObjects(data, project = False, minLength = 10) | |
| 80 objects = convertToObjects(data, minLength = 10) | |
| 81 colors = utils.PlottingPropertyValues('rk') | |
| 82 | |
| 83 def printMRV(objects, motifs, color, linewidth, alpha, blackBG = False): | |
| 84 fig = plt.figure() | |
| 85 for o in objects: | |
| 86 if o.motif in motifs: | |
| 87 o.plot(color, linewidth = linewidth, alpha = alpha) | |
| 88 plt.axis('equal') | |
| 89 plt.axis('off') | |
| 90 plt.tight_layout() | |
| 91 if blackBG: | |
| 92 fig.patch.set_facecolor('black') | |
| 93 plt.savefig(u'mrv-'+u'-'.join(motifs).replace(' ', '-')+'-'+color+'-blackbg.png', dpi = 300, facecolor=fig.get_facecolor(), edgecolor='none') | |
| 94 else: | |
| 95 plt.savefig(u'mrv-'+u'-'.join(motifs).replace(' ', '-')+'-'+color+u'.png', dpi = 300) | |
| 96 | |
| 97 for i, m in enumerate(allmotifs): | |
| 98 printMRV(objects, [m], colors[i], 0.5, 0.1) | |
| 99 | |
| 100 printMRV(objects, ['Aller au travail', 'Domicile-travail'], 'k', 0.5, 0.1) | |
| 101 printMRV(objects, ['Travail', u'D\xe9placement professionnel'], 'k', 0.5, 0.1) | |
| 102 printMRV(objects, ['Aller au travail', 'Domicile-travail', 'Travail', u'D\xe9placement professionnel'], 'k', 0.5, 0.1) | |
| 103 | |
| 104 # generate an enriched json with added data | |
| 105 generateJSON = False | |
| 106 if generateJSON: | |
| 107 for o in objects: | |
| 108 o.positions.computeCumulativeDistances() | |
| 109 | |
| 110 #properties = {'length': [], | |
| 111 # 'wiggliness': []} | |
| 112 | |
| 113 removeBothEnds = 2 | |
| 114 # export mean speed, nseconds, wiggliness, speeds, accel | |
| 115 for o in objects: | |
| 116 #print o.positions.length(), (o.stop-o.start).seconds, (o.stop-o.start).seconds/float(o.positions.length()), o.positions.wiggliness() | |
| 117 #print o.positions.length(), o.properties['n_coord'], len(o.positions.differentiateSG(5,2,2)), len(o.positions.differentiateSG(5,2,1)) | |
| 118 speeds = o.positions.differentiateSG(5,2,1, removeBothEnds = removeBothEnds).norm().tolist() | |
| 119 accel = o.positions.differentiateSG(5,2,2, removeBothEnds = removeBothEnds).norm().tolist() | |
| 120 data['features'][o.getNum()]['properties']['speeds'] = [speeds[0]]*removeBothEnds+speeds+[speeds[-1]]*removeBothEnds | |
| 121 data['features'][o.getNum()]['properties']['accelerations'] = [accel[0]]*removeBothEnds+accel+[accel[-1]]*removeBothEnds | |
| 122 data['features'][o.getNum()]['properties']['n_points'] = o.positions.length() | |
| 123 data['features'][o.getNum()]['properties']['n_seconds'] = (o.stop-o.start).seconds | |
| 124 data['features'][o.getNum()]['properties']['mean_speed'] = o.positions.cumulativeDistances[-1]/data['features'][o.getNum()]['properties']['n_seconds'] | |
| 125 data['features'][o.getNum()]['properties']['wiggliness'] = o.positions.wiggliness() | |
| 126 | |
| 127 nums = [o.getNum() for o in objects] | |
| 128 data['features'] = [d for i, d in enumerate(data['features']) if i in nums] | |
| 129 simplejson.dump(data, open(utils.removeExtension(filename)+'-enriched.json', 'w')) |
