Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import matplotlib.pyplot as plt
- import pymongo
- import ssl
- from pymongo import MongoClient
- import datetime
- import numpy as np
- import json
- from bson.son import SON
- import copy
- class Connect:
- def __init__(self):
- self.myemotion= "I hate MOngoDB"
- def connection(self):
- try:
- client = MongoClient(host='mongodb://bigdatadb.polito.it',
- port=27017,
- ssl=True,
- username='ictts',
- password='Ictts16!',
- authSource='carsharing',
- authMechanism='SCRAM-SHA-1',
- ssl_cert_reqs=ssl.CERT_NONE)
- print ("Connected successfully!!!")
- except pymongo.errors.ConnectionFailure :
- print ("Could not connect to MongoDB")
- db= client['carsharing']
- booking_enjoy = db['enjoy_PermanentBookings']
- parking_car2go = db['PermanentParkings']
- parking_enjoy = db['enjoy_PermanentParkings']
- booking_car2go = db['PermanentBookings']
- return db, parking_enjoy, parking_car2go, booking_enjoy, booking_car2go
- class Filter:
- def __init__(self,db,startDate,endDate):
- self.db= db
- self.start=startDate
- self.end=endDate
- def nofilter(self,city,collection):
- pipeline = [{ "$match": { "city": city,"init_time": { "$gte": self.start, "$lte": self.end} }},
- {"$project": {'_id': False, "duration": { "$divide": [ { "$subtract": ["$final_time", "$init_time"] }, 60 ] }}},
- {"$group" : {"_id": "$duration","count": {"$sum": 1}}},
- {"$sort": SON([("_id", 1)])} ]
- li=list(collection.aggregate(pipeline))
- return li
- def nofilteraday(self,city,collection):
- pipeline = [{"$match": {"city": city, "init_time": {"$gte": self.start, "$lte": self.end}}},
- {"$project": {'_id': False, "day": {"$dayOfWeek": "$init_date"},
- "duration": {"$divide": [{"$subtract": ["$final_time", "$init_time"]}, 60]},
- }},
- {"$group": {"_id": {"day":"$day","duration":"$duration"},"count": {"$sum": 1}}}]
- li = list(collection.aggregate(pipeline))#da fare per ogni giorno cdf output es:{'_id': {'day': 5, 'duration': 122.1}, 'count': 1},
- #print (li)
- li.sort(key=self.take_duration)
- return li
- def take_duration(self, element):
- return element["_id"]["duration"]
- def nofilteraweek(self,city,collection):
- pipeline = [{"$match": {"city": city, "init_time": {"$gte": self.start, "$lte": self.end}}},
- {"$project": {'_id': False, "week": {"$week": "$init_date"},
- "duration": {"$divide": [{"$subtract": ["$final_time", "$init_time"]}, 60]},
- }},
- {"$group": {"_id": {"week":"$week","duration":"$duration"},"count": {"$sum": 1}}}]
- li = list(collection.aggregate(pipeline))#da fare per ogni giorno cdf output es:{'_id': {'day': 5, 'duration': 122.1}, 'count': 1},
- li.sort(key=self.take_duration)
- print(duration_list)
- return li
- def filtered(self,city,collection):
- pipeline = [{ "$match": { "city": city,"init_time": { "$gte": self.start, "$lte": self.end} }},
- {"$project": {'_id': False, "duration": { "$divide": [ { "$subtract": ["$final_time", "$init_time"] }, 60 ] },#"moved" :{ "$ne": [
- #{"$arrayElemAt": [ "$origin_destination.coordinates", 0]}, {"$arrayElemAt": [ "$origin_destination.coordinates", 1]}]}
- }},
- {"$match":{"duration": {"$lte": 480}}},#{"$match":{'moved':True,"duration": {"$lte": 240,"$gte":2}}}
- {"$group" : {"_id": "$duration","count": {"$sum": 1}}},
- {"$sort": SON([("_id", 1)])} ]
- li=list(collection.aggregate(pipeline))
- return li
- def filteredUTIL(self, city, collection):
- pipeline = [{"$match": {"city": city, "init_time": {"$gte": self.start, "$lte": self.end}}},
- {"$project": {'_id': False,
- "duration": {"$divide": [{"$subtract": ["$final_time", "$init_time"]}, 60]},
- "hour": {"$hour": "$init_date"}, "moved": {"$ne": [ # }} "moved" :{ "$ne": [
- {"$arrayElemAt": ["$origin_destination.coordinates", 0]},
- {"$arrayElemAt": ["$origin_destination.coordinates", 1]}]}}},
- {"$match": {'moved': True, "duration": {"$lte": 240, "$gte": 2}}},
- # {"$match":{'moved':True,"duration": {"$lte": 240,"$gte":2}}} {"$match":{"duration": {"$lte": 480}}}
- {"$group": {"_id": "$hour", "count": {"$sum": 1}}},
- {"$sort": SON([("_id", 1)])}]
- li = list(collection.aggregate(pipeline))
- return li
- def position(self, city, collection):
- pipeline = [{"$match": {"city": city, "init_time": {"$gte": self.start, "$lte": self.end}}},
- {"$project": {'_id': False, "hour": {"$hour": "$init_date"},
- "position": {"$arrayElemAt": ["$origin_destination.coordinates"]}
- }},
- {"$group": {"_id": "$hour", "position": "$position"}},
- {"$sort": SON([("_id", 1)])}]
- li = list(collection.aggregate(pipeline))
- print(li)
- def perhour(self,city,collection):#effettivamente non è chiaro-> hour/ tot book?
- pipeline = [{ "$match": { "city": city,"init_time": { "$gte": self.start, "$lte": self.end} }},
- {"$project": {'_id': False, "hour": {"$hour": "$init_date"}, "duration": { "$divide": [ { "$subtract": ["$final_time", "$init_time"] }, 60 ] }}},
- {"$group": {"_id": {"hour": "$hour"}, "count": {"$sum": 1}}}]#, "duration": "$duration"
- li = list(collection.aggregate(pipeline))
- return li
- def day_average(self, city, collection):
- pipeline = [{"$match": {"city": city, "init_time": {"$gte": self.start, "$lte": self.end}}},
- {"$project": {'_id': False, "day": {"$dayOfMonth": "$init_date"},
- "duration": {"$divide": [{"$subtract": ["$final_time", "$init_time"]}, 60]},
- "moved" :{ "$ne": [{"$arrayElemAt": [ "$origin_destination.coordinates", 0]}, {"$arrayElemAt": [ "$origin_destination.coordinates", 1]}]}}},
- {"$match": {"duration": {"$lte": 480}}},
- {"$match":{'moved':True,"duration": {"$lte": 240,"$gte":2}}},
- {"$group": {"_id": "$day", 'average':{'$avg':'$duration'}}},#'$stdDevPop':'$duration'}}},
- {"$sort": SON([('_id', 1)])}]
- li = list(collection.aggregate(pipeline))
- return li
- def day_std(self, city, collection):
- pipeline = [{"$match": {"city": city, "init_time": {"$gte": self.start, "$lte": self.end}}},
- {"$project": {'_id': False, "day": {"$dayOfMonth": "$init_date"},
- "duration": {"$divide": [{"$subtract": ["$final_time", "$init_time"]}, 60]},
- "moved" :{ "$ne": [{"$arrayElemAt": [ "$origin_destination.coordinates", 0]}, {"$arrayElemAt": [ "$origin_destination.coordinates", 1]}]}}},
- {"$match": {"duration": {"$lte": 480}}},
- {"$match":{'moved':True,"duration": {"$lte": 240,"$gte":2}}},
- {"$group": {"_id": "$day", 'std': {'$stdDevPop': '$duration'}}},
- {"$sort": SON([('_id', 1)])}]
- li = list(collection.aggregate(
- pipeline))
- return li
- def day_median_and_percentile(self, city, collection):
- pipeline = [{"$match": {"city": city, "init_time": {"$gte": self.start, "$lte": self.end}}},
- {"$project": {'_id': False, "day": {"$dayOfMonth": "$init_date"},
- "duration": {"$divide": [{"$subtract": ["$final_time", "$init_time"]}, 60]},#
- "moved" :{ "$ne": [{"$arrayElemAt": [ "$origin_destination.coordinates", 0]}, {"$arrayElemAt": [ "$origin_destination.coordinates", 1]}]}}},
- {"$match": {"duration": {"$lte": 480}}},
- {"$match":{'moved':True,"duration": {"$lte": 240,"$gte":2}}},
- {"$group": {"_id":{'day':"$day", 'duration':'$duration'} }},
- {"$sort": SON([('_id', 1)])}]
- li = list(collection.aggregate(pipeline))
- temp = []
- median = []
- percentile75 = []
- for k in range(32):
- temp.clear()
- for i in range (len(li)):
- if li[i]['_id']['day'] == k and k != 0:
- temp.append(li[i]['_id']['duration'])
- if k!=0:
- temp.sort()
- median.append(temp[(len(temp)-1)//2])
- ind = np.int(0.75 * len(temp))
- percentile75.append(temp[ind])
- print(len(median))
- print(percentile75)
- return median, percentile75
- # function to get all the rental car, filtering the ones which have position and destination at same coordinates,
- # parameters taked into account: time (initial and final) and coordinates (startind and ending)
- # this function will not be used due the long time that it takes to make each call to the database
- def get_car_renting(self, city, collection, start, stop):
- start = start
- stop = stop
- pipeline = [{"$match": {"city": city, "init_time": {"$gte": self.start, "$lte": self.end}}},
- {"$project": {'_id': False, "start_hour": {"$hour": "$init_date"},
- "end_hour": {"$hour": "$final_date"},
- "moved": {"$ne": [{"$arrayElemAt": ["$origin_destination.coordinates", 0]},
- {"$arrayElemAt": ["$origin_destination.coordinates", 1]}]},
- "s" : {"$arrayElemAt": ["$origin_destination.coordinates", 0]},
- "e" : {"$arrayElemAt": ["$origin_destination.coordinates", 1]},
- 'car': "$_id"}},
- {"$match": {'moved': True}},
- {"$match" :{"e": {"$geoWithin": {"$geometry": {"type": "Polygon", "coordinates": stop}}}}},
- {"$match" :{"s": {"$geoWithin": {"$geometry": {"type": "Polygon", "coordinates": start}}}}},
- {"$group": {"_id": {"s": "$s", "e": "$e"}}}]
- li = list(collection.aggregate(pipeline))
- return li
- # function to get all the parked car
- # parameters taked into account: time (initial and final)
- def get_car_position(self, city, collection):
- pipeline = [{"$match": {"city": city, "init_time": {"$gte": self.start, "$lte": self.end}}},
- {"$project": {'_id': False, "start_hour": {"$hour": "$init_date"},
- "end_hour": {"$hour": "$final_date"},
- "duration": {"$divide": [{"$subtract": ["$final_time", "$init_time"]}, 60]},
- "x" : {"$arrayElemAt": ["$loc.coordinates", 0]},
- "y" : {"$arrayElemAt": ["$loc.coordinates", 1]},
- 'car': "$_id"}},
- {"$group": {"_id": {"x": "$x", "y": "$y", 'car':"$car", "start":"$start_hour", "end":"$end_hour"}}}]
- li = list(collection.aggregate(pipeline))
- return li
- class Printer:
- def __init__(self,l1,l2,l3):
- self.l1 = l1
- self.l2 = l2
- self.l3 = l3
- def print(self):
- list1=[]
- list11=[]
- list2=[]
- list22=[]
- list3 = []
- list33 = []
- length=len(self.l1)
- length2=len(self.l2)
- length3 = len(self.l3)
- temp=0
- temp2=0
- temp3=0
- plt.figure(1)
- for i in range(length):
- b=self.l1[i]
- list1.append(b['_id'])
- temp=b['count']+temp
- list11.append(temp)
- for i in range(length2):
- c=self.l2[i]
- list2.append(c['_id'])
- temp2 = c['count'] + temp2
- list22.append(temp2)
- for i in range(length3):
- d=self.l3[i]
- list3.append(d['_id'])
- temp3 = d['count'] + temp3
- list33.append(temp3)
- for i in range(len(list11)):
- list11[i]=list11[i]/temp
- for i in range(len(list22)):
- list22[i] = list22[i] / temp2
- for i in range(len(list33)):
- list33[i] = list33[i] / temp3
- #t = np.arange(0., 1.,s)
- plt.subplot(311)
- plt.grid()
- plt.plot(list1,list11,'c')
- plt.xscale('log')
- plt.title('CDF_Parking')#"CDFParkingBooking"
- plt.xlabel('Duration')
- plt.ylabel('Roma_Enjoy')
- plt.subplot(312)
- plt.plot(list2,list22,'b')
- # plt.title('CDF#nofilter')
- plt.xscale('log')
- plt.xlabel('Duration')
- plt.ylabel('Roma_Car2Go ')
- plt.grid()
- plt.subplot(313)
- plt.plot(list3, list33, 'm')
- # plt.title('CDF#nofilter')
- plt.xscale('log')
- plt.xlabel('Duration')
- plt.ylabel('Torino_Car2Go ')
- plt.grid()
- plt.show()
- def printday(self):
- list0 = []
- list00 = []
- list1 = []
- list11 = []
- list2 = []
- list22 = []
- list3 = []
- list33 = []
- list4 = []
- list44 = []
- length = len(self.l1)
- temp0 = 0
- temp = 0
- temp2 = 0
- temp3 = 0
- temp4 = 0
- for i in range(length):
- b = self.l1[i]
- if b['_id']['week'] == 40:
- a = b['_id']['duration']
- list0.append(a)
- temp0 = b['count'] + temp0
- list00.append(temp0)
- elif b['_id']['week'] == 41:
- a = b['_id']['duration']
- list1.append(a)
- temp = b['count'] + temp
- list11.append(temp)
- elif b['_id']['week'] == 42:
- a = b['_id']['duration']
- list2.append(a)
- temp2 = b['count'] + temp2
- list22.append(temp2)
- elif b['_id']['week'] == 43:
- a = b['_id']['duration']
- list3.append(a)
- temp3 = b['count'] + temp3
- list33.append(temp3)
- elif b['_id']['week'] == 44:
- a = b['_id']['duration']
- list4.append(a)
- temp4 = b['count'] + temp4
- list44.append(temp4)
- for i in range(len(list00)):
- list00[i] = list00[i] / temp0
- for i in range(len(list11)):
- list11[i] = list11[i] / temp
- for i in range(len(list22)):
- list22[i] = list22[i] / temp2
- for i in range(len(list33)):
- list33[i] = list33[i] / temp3
- for i in range(len(list44)):
- list44[i] = list44[i] / temp4
- # for i in range(len(list0)+1,len(list33)):
- # list0.append(list0[len(list0)-1])
- # list00.append(1)
- # for i in range(len(list1)+1,len(list33)):
- # list1.append(list1[len(list1)-1])
- # list11.append(1)
- # for i in range(len(list2)+1, len(list33)):
- # list3.append(list2[len(list2)-1])
- # list33.append(1)
- # for i in range(len(list4)+1, len(list33)):
- # list4.append(list4[len(list4)-1])
- # list44.append(1)
- plt.figure(1)
- plt.grid()
- plt.xscale('log')
- plt.title('CDFperweek_Booking') # "CDFParkingBooking"
- plt.xlabel('Duration')
- plt.ylabel('Roma_Enjoy')
- plt.plot(list0, list00, 'k', label='40th week')
- plt.plot(list1, list11, 'c', label='41st week')
- plt.plot(list2, list22, 'b', label='42nd week')
- plt.plot(list3, list33, 'm', label='43rd week')
- plt.plot(list4, list44, 'orange', label='44th week')
- plt.legend(loc='upper left')
- plt.show()
- def print_week(self):
- duration_list_day1 = []
- duration_list_day2 = []
- duration_list_day3 = []
- duration_list_day4 = []
- duration_list_day5 = []
- duration_list_day6 = []
- duration_list_day7 = []
- duration_list_day1_count = []
- duration_list_day2_count = []
- duration_list_day3_count = []
- duration_list_day4_count = []
- duration_list_day5_count = []
- duration_list_day6_count = []
- duration_list_day7_count = []
- days = []
- time = []
- c = []
- li = self.l1
- print(li)
- for k in range(8):
- t = 0
- days.clear()
- c.clear()
- for i in range(len(li)):
- if li[i]['_id']['day'] == k:
- days.append(li[i]['_id']['duration'])
- t = li[i]['count'] + t
- c.append(t)
- if k != 0:
- time.append(len(c))
- for i in range(len(c)):
- c[i] = c[i] / t
- print(len(c))
- print(time)
- if k == 1:
- duration_list_day1 = days.copy()
- duration_list_day1_count = c.copy()
- print(c)
- elif k == 2:
- duration_list_day2 = days.copy()
- duration_list_day2_count = c.copy()
- print(c)
- elif k == 3:
- duration_list_day3 = days.copy()
- duration_list_day3_count = c.copy()
- elif k == 4:
- duration_list_day4 = days.copy()
- duration_list_day4_count = c.copy()
- elif k == 5:
- duration_list_day5 = days.copy()
- duration_list_day5_count = c.copy()
- elif k == 6:
- duration_list_day6 = days.copy()
- duration_list_day6_count = c.copy()
- elif k == 7:
- duration_list_day7 = days.copy()
- duration_list_day7_count = c.copy()
- print('SUN', duration_list_day7)
- print(duration_list_day2)
- t_max = max(time)
- print(t_max)
- for k in range(1):
- d = t_max - time[k]
- for i in range(d):
- if k == 0:
- duration_list_day1.append(duration_list_day1[len(duration_list_day1) - 1])
- duration_list_day1_count.append(1)
- elif k == 1:
- duration_list_day2.append(duration_list_day2[len(duration_list_day2) - 1])
- duration_list_day2_count.append(1)
- elif k == 2:
- duration_list_day3 = (duration_list_day3[len(duration_list_day3) - 1])
- duration_list_day3_count.append(1)
- elif k == 3:
- duration_list_day4 = (duration_list_day4[len(duration_list_day4) - 1])
- duration_list_day4_count.append(1)
- elif k == 4:
- duration_list_day5 = (duration_list_day4[len(duration_list_day4) - 1])
- duration_list_day5_count.append(1)
- elif k == 5:
- duration_list_day6 = (duration_list_day5[len(duration_list_day5) - 1])
- duration_list_day6_count.append(1)
- elif k == 6:
- duration_list_day7 = (duration_list_day6[len(duration_list_day6) - 1])
- duration_list_day7_count.append(1)
- print(duration_list_day1)
- print(duration_list_day2)
- plt.figure(1)
- plt.xscale('log')
- plt.title('CDF per day')
- plt.plot(duration_list_day1, duration_list_day1_count, 'k', label='MON')
- plt.plot(duration_list_day2, duration_list_day2_count, 'c', label='TUE')
- plt.plot(duration_list_day3, duration_list_day3_count, 'b', label='WED')
- plt.plot(duration_list_day4, duration_list_day4_count, 'm', label='THU')
- plt.plot(duration_list_day5, duration_list_day5_count, 'orange', label='FRI')
- plt.plot(duration_list_day6, duration_list_day6_count, 'r', label='SAT')
- plt.plot(duration_list_day7, duration_list_day7_count, 'pink', label='SUN')
- plt.legend(loc='upper left')
- plt.xlabel('Duration')
- plt.ylabel('Turin Car2go booking')
- plt.show()
- def print_statistics(self, l4, title):
- list1 = []
- list11 = []
- list2 = []
- list22 = []
- list3 = []
- list33 = []
- list4 = []
- list44 = []
- plt.figure(1)
- for i in range(len(self.l1)):
- list1.append(self.l1[i]['_id'])
- list11.append(self.l1[i]['average'])
- list2.append(self.l2[i]['_id'])
- list22.append(self.l2[i]['std'])
- list3.append(i+1)
- list33.append(self.l3[i])
- list4.append(i+1)
- list44.append(l4[i])
- plt.grid(True)
- plt.minorticks_on()
- plt.title(title)
- plt.xlabel('Day')
- plt.ylabel('Number of cars')
- plt.plot(list1, list11, 'c', label="Average")
- plt.plot(list2, list22, 'b', label="Standard Deviation")
- plt.plot(list3, list33, 'm', label="Median")
- plt.plot(list4, list44, 'pink', label="75° percentile")
- plt.legend(loc='upper left')
- plt.show()
- def print_utilization(self):
- list1 = []
- list11 = []
- list2 = []
- list22 = []
- list3 = []
- list33 = []
- length = len(self.l1)
- plt.figure(1)
- for i in range(length):
- b = self.l1[i]
- list1.append(b['_id'])
- list11.append(b['count'])
- b = self.l2[i]
- list2.append(b['_id'])
- list22.append(b['count'])
- b = self.l3[i]
- list3.append(b['_id'])
- list33.append(b['count'])
- print(list33)
- plt.grid()
- plt.title('Filtered_utilization_Booking') # "CDFParkingBooking"
- plt.xlabel('Hour')
- plt.ylabel('#cars')
- plt.plot(list1, list11, 'c', label="Roma_enjoy")
- plt.plot(list2, list22, 'b', label="Roma_Car2Go")
- plt.plot(list3, list33, 'm', label="Torino_Car2GO")
- plt.legend(loc='upper left')
- plt.show()
- # this function counts the parked car in different time intervals and write the data in a *.csv file
- # it also searches for the egdes coordinates in order to be able to calculate the grid 1 km x 1 km
- def print_csv(list):
- k = 4
- j = 0
- temp = []
- time_grid=[]
- time_grid_1kmq = []
- while (k<25):
- min_lat = 50 #45.01089
- min_lon = 50 #7.60684
- max_lat = 0 #45.18843
- max_lon = 0 #7.73177
- temp.clear()
- f = open('position' + str(k)+ '.csv', mode = 'w')
- for i in range(len(list)):
- if list[i]["_id"]["start"] >= j and list[i]["_id"]["start"] < k and list[i]["_id"]["end"] >= k:
- f.write(str(list[i]["_id"]['x']))
- f.write(',')
- f.write(str(list[i]["_id"]['y']))
- f.write(',')
- f.write(str(list[i]["_id"]['start']))
- f.write(',')
- f.write(str(list[i]["_id"]['end']))
- f.write("\n")
- temp.append(list[i])
- if list[i]["_id"]['y'] < min_lat:
- min_lat = list[i]["_id"]['y']
- if list[i]["_id"]['y'] > max_lat:
- max_lat = list[i]["_id"]['y']
- if list[i]["_id"]['x'] < min_lon:
- min_lon = list[i]["_id"]['x']
- if list[i]["_id"]['x'] > max_lon:
- max_lon = list[i]["_id"]['x']
- f.close()
- grid, grid_1kmq, n_lat_1kmq, n_lon_1kmq = write_heatmap(min_lat, max_lat, min_lon, max_lon, k)
- time_grid_1kmq.append(grid_1kmq)
- j = k
- k = k + 6
- return grid, time_grid_1kmq, n_lat_1kmq, n_lon_1kmq
- # this function uses the coordinates edges to calculate the grid
- # two different grids are saved: the first one with a polygon shape to feed the geojason,
- # the second one with only the 4 coordinates
- #this second grid is then ordered, in order to match the index with the number assigned the figure
- def write_heatmap(min_lat, max_lat, min_lon, max_lon, n):
- print('max lat', max_lat)
- print('min lat', min_lat)
- print('max lon', max_lon)
- print('max lon', min_lon)
- f = open('density' + str(n) + '.csv', mode='w')
- prev_lat = min_lat
- prev_lon = min_lon
- grid = []
- grid_1kmq = []
- n_lat_1kmq = int((max_lat - min_lat)//0.01)
- n_lon_1kmq = int((max_lon - min_lon)//0.01)
- for y in range(int(10)):
- if y!=0:
- prev_lat = lat
- else:
- prev_lat = min_lat
- lat = min_lat + 0.01*(y+1)
- prev_lon = min_lon
- for x in range(int(n_lon_1kmq)):
- lon = min_lon + 0.01*(x+1)
- grid_1kmq.append([[[prev_lon, prev_lat], [lon,prev_lat], [lon, lat], [prev_lon, lat], [prev_lon, prev_lat]]])
- grid.append([prev_lon, lon, prev_lat, lat])
- prev_lon = lon
- orderd_grid = []
- for i in range(10):
- for j in range(12):
- orderd_grid.append(grid_1kmq[len(grid_1kmq) - (1+i)*12 + j])
- return grid, orderd_grid, n_lat_1kmq, n_lon_1kmq
- def write_od(n_lat_1kmq, n_lon_1kmq, grid4_coord):
- origin = []
- destination = []
- print(len(grid4_coord))
- l=0
- with open("data.txt", "r") as filestream:
- for line in filestream:
- if l == 0:
- l = 1
- else:
- current_line = line.split("\n")
- c = current_line[0].split(" ")
- print(c)
- origin.append(c[0].split(","))
- destination.append(c[1].split(","))
- print(n_lat_1kmq, n_lon_1kmq)
- print("n° celle", 120)
- od_matrix = np.zeros((120, 120))
- f = open("od_matrix.csv", mode='w')
- for i in range(120):
- start = grid4_coord[i]
- print(i)
- for j in range(120):
- stop = grid4_coord[j]
- for z in range(0, len(origin)):
- if float(origin[z][0]) > start[0] and float(origin[z][0]) < start[1] and float(origin[z][1]) > start[2] and float(origin[z][1]) < start[3]:
- if float(destination[z][0]) > stop[0] and float(destination[z][0]) < stop[1] and float(destination[z][1]) > stop[2] and float(destination[z][1]) < stop[3]:
- od_matrix[i, j] = od_matrix[i, j] + 1
- f.write(str(od_matrix[i, j]))
- f.write(",")
- f.write("\n")
- f.close()
- plt.figure(1)
- plt.matshow(od_matrix)
- plt.show()
- if __name__ == "__main__":
- c=Connect()
- db, parking_enjoy, parking_car2go, booking_enjoy, booking_car2go = c.connection()
- city_rome = "Roma"
- city_turin = "Torino"
- startDate = datetime.datetime(2017, 10, 1).timestamp()
- endDate = datetime.datetime(2017, 10, 31, 23, 59, 59).timestamp()
- filter = Filter(db,startDate,endDate)
- #ES 1 - CDF
- #list1 = filter.nofilter(city_rome,collection1)
- #list2 = filter.nofilter(city_rome, collection2)
- #list3 = filter.nofilter(city_turin, collection2)
- #cdf = Printer(list1,list2,list3)
- #cdf.print()
- # list1 = filter.perhour(city_rome, collection1)
- # list2 = filter.perhour(city_rome, collection2)
- # list3 = filter.perhour(city_turin, collection2)
- # rome_car2go = filter.nofilteraday(city_rome, booking_car2go) # fare funzione apposta
- # rome_enjoy = filter.nofilteraday(city_rome, parking_enjoy)
- # turin_car2go = filter.nofilteraday(city_turin, booking_car2go)
- # week = Printer(rome_car2go, rome_enjoy, turin_car2go)
- # week.print_week()
- # list1 = filter.nofilteraweek(city_rome, collection1)
- # list2 = filter.nofilteraweek(city_rome, collection2)
- # list3 = filter.nofilteraweek(city_turin, collection2)
- # cdf = Printer(list1,list2,list3)
- # cdf.printday()
- #ES 2 - Utilization
- # list1 = filter.filteredUTIL(city_rome, collection1)
- # list2 = filter.filteredUTIL(city_rome, collection2)
- # list3 = filter.filteredUTIL(city_turin, collection2)
- # cdf = Printer(list1, list2, list3)
- # cdf.print_utilization()
- #ES 3 -Filter
- # list1=filter.filtered(city_rome,collection1)
- # list2 = filter.filtered(city_rome, collection2)
- # list3 = filter.filtered(city_turin, collection2)
- # cdf = Printer(list1, list2, list3)
- # cdf.print()
- #ES 4 - Utilization Filtered
- #ES 5 - statistics
- #PARKING
- #Rome Enjoy
- # avg_RE = filter.day_average(city_rome, parking_enjoy)
- # std_RE = filter.day_std(city_rome, parking_enjoy)
- # med_RE, per_RE = filter.day_median_and_percentile(city_rome, parking_enjoy)
- # print_RE = Printer(avg_RE, std_RE, med_RE)
- # print_RE.print_statistics(per_RE, 'Statistic on parking over October 2017 - Rome Enjoy')
- #Rome Car2Go
- # avg_RC = filter.day_average(city_rome, parking_car2go)
- # std_RC = filter.day_std(city_rome, parking_car2go)
- # med_RC, per_RC = filter.day_median_and_percentile(city_rome, parking_car2go)
- # print_RC = Printer(avg_RC, std_RC, med_RC)
- # print_RC.print_statistics(per_RC, 'Statistic on parking over October 2017 - Rome Car2Go')
- #Turin Car2Go
- # avg_TC = filter.day_average(city_turin, parking_car2go)
- # std_TC = filter.day_std(city_turin, parking_car2go)
- # med_TC, per_TC = filter.day_median_and_percentile(city_turin, parking_car2go)
- # print_TC = Printer(avg_TC, std_TC, med_TC)
- # print_TC.print_statistics(per_TC, 'Statistic on parking over October 2017 - Turin Car2Go')
- #BOOKING
- #Rome Enjoy
- # avg_RE = filter.day_average(city_rome, booking_enjoy)
- # std_RE = filter.day_std(city_rome, booking_enjoy)
- # med_RE, per_RE = filter.day_median_and_percentile(city_rome, booking_enjoy)
- # print_RE = Printer(avg_RE, std_RE, med_RE)
- # print_RE.print_statistics(per_RE, 'Statistic on booking over October 2017 - Rome Enjoy')
- #Rome Car2Go
- # avg_RC = filter.day_average(city_rome, booking_car2go)
- # std_RC = filter.day_std(city_rome, booking_car2go)
- # med_RC, per_RC = filter.day_median_and_percentile(city_rome, booking_car2go)
- # print_RC = Printer(avg_RC, std_RC, med_RC)
- # print_RC.print_statistics(per_RC, 'Statistic on booking over October 2017 - Rome Car2Go')
- #Turin Car2Go
- # avg_TC = filter.day_average(city_turin, booking_car2go)
- # std_TC = filter.day_std(city_turin, booking_car2go)
- # med_TC, per_TC = filter.day_median_and_percentile(city_turin, booking_car2go)
- # print_TC = Printer(avg_TC, std_TC, med_TC)
- # print_TC.print_statistics(per_TC, 'Statistic on booking over October 2017 - Turin Car2Go')
- #ES 6 - Map
- #Rome Enjoy
- car_position = filter.get_car_position(city_turin, parking_car2go)
- grid4_coord, time_grid_1kmq, n_lat_1kmq, n_lon_1kmq = print_csv(car_position)
- write_od(n_lat_1kmq, n_lon_1kmq, grid4_coord)
- exit()
- #list=filter.perhour(city1,collection1)
- #list = filter.nofilteraweek(city1, collection1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement