def __init__(self): QtGui.QWidget.__init__(self) arg_list = self.retrieve_arg() # arg_list =['F:\\kianwee_work\\spyder_workspace\\solar_travel_gui\\solar_travel_gui\\stg_animation.py', '2019-9-2-10:0:0', '2019-9-30-18:0:0', # '133.0', '914.0', 'contextual_map', 'map', 'terrains', 'trees', 'roads', 'buildings', 'irradiations', 'travels', 'parkings', # 'F:/kianwee_work/princeton/2019_06_to_2019_12/golfcart/model3d/solar_travel_data', '529580.7566756287,4465755.661849028,42.16880273814235'] self.arg_list = arg_list self.setupGUI() self.date_range = dict(name='Date Range', type='group', expanded = True, title = "", children=[ dict(name='Data Range Loaded', type = 'str', title = "Data Range Loaded", readonly = True), dict(name='Current Date', type = 'str', title = "Current Date", readonly = True), dict(name='Pause/Play', type = 'action', title = "Pause/Play"), dict(name='Rewind', type = 'action', title = "Rewind"), dict(name='Forward', type = 'action', title = "Forward"), dict(name='Play Status', type = 'str', title = "Play Status", value = "Play(Forward)", readonly = True), dict(name='Seconds/Frame', type = 'float', title = "Seconds/Frame", value = 1.0), dict(name='Change Playback Speed', type = 'action', title = "Change Playback Speed") ] ) self.params = Parameter.create(name='ParmX', type='group', children=[self.date_range]) s_date_str = arg_list[1] e_date_str = arg_list[2] date = parse(s_date_str) end_date = parse(e_date_str) self.rewind_status = False self.current_date = date start_index = stg_func.date2index(date) self.current_index = start_index self.start_index = start_index self.start_date = date self.end_index = stg_func.date2index(end_date) self.end_date = end_date self.params.param('Date Range').param('Data Range Loaded').setValue(s_date_str + " to " + e_date_str) self.tree.setParameters(self.params, showTop=False) self.min_val = float(arg_list[3]) self.max_val = float(arg_list[4]) self.falsecolour = stg_func.gen_falsecolour_bar(self.min_val, self.max_val) self.params2 = Parameter.create(name = "Parmx2", type = "group", children = [self.falsecolour]) self.tree2.setParameters(self.params2, showTop=False) self.params.param('Date Range').param("Pause/Play").sigActivated.connect(self.pause) self.params.param('Date Range').param("Rewind").sigActivated.connect(self.rewind) self.params.param('Date Range').param("Forward").sigActivated.connect(self.forward) self.params.param('Date Range').param("Change Playback Speed").sigActivated.connect(self.change_speed)
def read_process_loc_json(location_filepath, date_range=None): print("*****************Reading the files*****************") #read the location file location_f = open(location_filepath, "r") #===================================================================== #CHANGE THE JSON DATA HERE WHEN CODING #===================================================================== json_data = json.load(location_f) #set up the projection p = Proj(proj='utm', zone=18, ellps='GRS80', preserve_units=False) year_data_dict = {} date_str_list = [] loc_pyptlist = [] keys = json_data.keys() if "locations" in keys: #this shld be a google map file json_data = json_data["locations"] #process the location points print("***************** Processing location points*****************") loc_list = [] for loc in json_data: act_dict_list = unpack_activity(loc, p) loc_list.extend(act_dict_list) cnt = 0 for loc in loc_list: date = loc["date"] if date_range[0] <= date <= date_range[1]: hour_index = stg_func.date2index(date) hour_date_str = date.strftime("%Y-%m-%d %H:0:0") year = date.year year_list = list(year_data_dict.keys()) if year not in year_list: hourly_list = [] for _ in range(8760): hourly_list.append({"locations": []}) year_data_dict[year] = hourly_list if hour_date_str not in date_str_list: date_str_list.append(hour_date_str) date_str = date.strftime("%Y-%m-%dT%H:%M:%S.%fZ") pypt = loc["pypt"] loc["date"] = date_str loc_pyptlist.append(pypt) year_data_dict[year][hour_index]["locations"].append(loc) # print(cnt, "/", len(loc_list)) cnt += 1 if "data" in keys: #this shld be a strava file data = json_data['data'][0] # fields = data['fields'] values = data['values'] #write the heading first for cnt, v in enumerate(values): timestamp = int(v[0]) date = datetime.datetime.fromtimestamp(timestamp) date_str = date.strftime("%Y-%m-%dT%H:%M:%S.%fZ") hour_index = stg_func.date2index(date) year = date.year year_list = list(year_data_dict.keys()) if year not in year_list: hourly_list = [] for _ in range(8760): hourly_list.append({"locations": []}) year_data_dict[year] = hourly_list hour_date_str = date.strftime("%Y-%m-%d %H:0:0") if hour_date_str not in date_str_list: date_str_list.append(hour_date_str) lat = v[1][0] lng = v[1][1] x, y = p(lng, lat) pypt = [x, y, 0] loc_pyptlist.append(pypt) loc = {"date": date_str, "pypt": pypt, "lat": lat, "lon": lng} year_data_dict[year][hour_index]["locations"].append(loc) location_f.close() return year_data_dict, loc_pyptlist, date_str_list
def export_data(self): self.timer = pg.QtCore.QTimer() self.timer.timeout.connect(self.update_bar) self.timer.start(50) try: time1 = time.perf_counter() self.process_status = "Exporting ... " self.params.param('Export').param('Progress').setValue(self.process_status) start_date = parse(self.str_export_start_date) start_hour = stg_func.date2index(start_date) start_year = start_date.year end_date = parse(self.str_export_end_date) end_hour = stg_func.date2index(end_date) end_year = end_date.year res_filepath = self.result_filepath self.process_status = "Reading data ... " self.params.param('Export').param('Progress').setValue(self.process_status) path_dict = stg_func.retrieve_travel_path_analysis(self.travel_dir, start_hour, start_year, end_hour, end_year) parking_dict = stg_func.retrieve_parking_analysis(self.parking_dir, start_hour, start_year, end_hour, end_year) self.process_status = "Read all Dicitonaries ... " self.params.param('Export').param('Progress').setValue(self.process_status) if start_year == end_year: year_list = [start_year] else: year_list = range(start_year, end_year+1) projection = Proj(proj='utm',zone=18,ellps='GRS80', preserve_units=False) nyear = len(year_list) strx = "Date,DistanceTravelled(m),ParkingTime(hr),SolarMax(wh/m2),SolarMaxPos,SolarMaxZ(m),SolarMin(wh/m2),SolarMinPos,SolarMinZ(m),SolarMedian(wh/m2),SolarMedPos,SolarMedZ(m)\n" ycnt = 0 for year in year_list: week_list, hour_interest = stg_func.gen_week_hour_interest_from_year_cnt(ycnt, nyear, start_hour, end_hour) total_hours = len(hour_interest) hcnt = 0 for hour in hour_interest: QtGui.QApplication.processEvents() self.progress = (hcnt/total_hours) * 100 self.process_status = "Processing Stops:" + str(hcnt) + "/" + str(total_hours) + " year " + str(year) + "/" + str(list(year_list)[-1]) self.params.param('Export').param('Progress').setValue(self.process_status) res_str = stg_func.export_data(hour, year, path_dict, parking_dict, projection) strx += res_str hcnt+=1 ycnt+=1 f = open(res_filepath, "w") f.write(strx) f.close() time2 = time.perf_counter() total_time = (time2-time1)/60 time_str = "SUCCESSFULLY COMPLETE PROCESSING, Total Processing Time: " + str(round(total_time,2)) + " mins" QtGui.QApplication.processEvents() self.progress = 100 self.update_bar() self.params.param('Export').param('Progress').setValue(time_str) self.timer.stop() except: self.params.param('Export').param('Progress').setValue("ERROR ... Last known status:" + self.process_status)
def find_parking(self): # try: self.timer = pg.QtCore.QTimer() self.timer.timeout.connect(self.update_bar) self.timer.start(50) time1 = time.perf_counter() #=============================================================================================== #UPDATE GUI #=============================================================================================== QtGui.QApplication.processEvents() self.progress = 0 self.process_status = "Retrieving solar and travel data" self.params.param('Analysis').param('Progress').setValue( self.process_status) #=============================================================================================== #=============================================================================================== #first get all the parameters from the gui start_date = parse(self.str_analysis_start_date) end_date = parse(self.str_analysis_end_date) start_hour = stg_func.date2index(start_date) end_hour = stg_func.date2index(end_date) start_year = start_date.year end_year = end_date.year parking_radius = self.params.param('Analysis').param( 'Parking Radius').value() parking_time = self.params.param('Analysis').param( 'Parking Time Threshold').value() solar_dir = self.solar_dir travel_dir = self.travel_dir parking_dir = self.parking_dir #then where are the parking spots according to the travel data travel_dict = stg_func.retrieve_travel_ext_analysis( travel_dir, start_hour, start_year, end_hour, end_year) stop_dict = stg_func.find_stops(travel_dict, stop_threshold=parking_time) solar_res_dict = stg_func.retrieve_solar4analysis( solar_dir, start_hour, start_year, end_hour, end_year) solar_pts = stg_func.get_solar_pts(solar_dir) #=============================================================================================== #UPDATE GUI #=============================================================================================== QtGui.QApplication.processEvents() self.progress = 0 self.params.param('Analysis').param('Progress').setValue( "Starting stops analysis ... ...") self.process_status = "Starting stops analysis" #=============================================================================================== #=============================================================================================== year_list = stop_dict.keys() for year in year_list: yearly_stop_dict = stop_dict[year] yearly_solar_res_dict = solar_res_dict[year] total_stops = len(yearly_stop_dict.items()) scnt = 0 for hour, stops in yearly_stop_dict.items(): #=============================================================================================== #UPDATE GUI #=============================================================================================== QtGui.QApplication.processEvents() self.progress = (scnt / total_stops) * 100 self.process_status = "Processing Stops:" + str( scnt) + "/" + str(total_stops) + " year " + str( year) + "/" + str(list(year_list)[-1]) self.params.param('Analysis').param('Progress').setValue( self.process_status) #=============================================================================================== #=============================================================================================== week_index = stg_func.id_week(hour) #for each hour find the potential parking spot result_dict = stg_func.find_parking4_the_hour( stops, solar_pts, hour, parking_radius, yearly_solar_res_dict) #write the file for analysis parking_filepath = os.path.join( parking_dir, "analysis", "parking_wk" + str(week_index) + "year" + str(year) + ".json") stg_func.append2json(parking_filepath, result_dict[hour], hour) #generate the meshes for the visualisation results parking_viz_filepath = os.path.join( parking_dir, "viz", "viz_parking_wk" + str(week_index) + "year" + str(year) + ".json") mesh_dict = stg_func.gen_parking_mesh(result_dict, hour) stg_func.append2json(parking_viz_filepath, mesh_dict, hour) scnt += 1 #write to source file source_path = os.path.join(parking_dir, "source.json") s_f = open(source_path, "w") json.dump( { "dates": [self.str_analysis_start_date, self.str_analysis_end_date] }, s_f) s_f.close() time2 = time.perf_counter() total_time = (time2 - time1) / 60 time_str = "SUCCESSFULLY COMPLETE PROCESSING, Total Processing Time: " + str( round(total_time, 2)) + " mins" QtGui.QApplication.processEvents() self.progress = 100 self.update_bar() self.params.param('Analysis').param('Progress').setValue(time_str) self.timer.stop()
def update(self): cur_date = self.current_date rewind_status = self.rewind_status if rewind_status == True: nxt_date = cur_date - timedelta(hours=1) nxt_index = stg_func.date2index(nxt_date) # self.params.param('Date Range').param('Play Status').setValue(str(nxt_index) + "nxt " + str(self.start_index)+ "start end" + str(self.end_index)) if nxt_date < self.start_date: nxt_index = self.end_index nxt_date = self.end_date else: nxt_date = cur_date + timedelta(hours=1) nxt_index = stg_func.date2index(nxt_date) # self.params.param('Date Range').param('Play Status').setValue(str(nxt_index) + "nxt " + str(self.start_index)+ "start end" + str(self.end_index)) if nxt_date > self.end_date: nxt_index = self.start_index nxt_date = self.start_date str_date = nxt_date.strftime("%Y-%m-%d %H:%M:%S") year = nxt_date.year self.current_index = nxt_index self.current_date = nxt_date self.params.param('Date Range').param('Current Date').setValue(str_date) #============================================= #retrieve the solar data #============================================= solar_dir = self.solar_dir solar_mesh = self.colour_meshes if solar_mesh !=None: stg_func.retrieve_solar_data(solar_mesh[0], nxt_index, solar_dir, self.min_val, self.max_val) stg_func.viz_graphic_items(solar_mesh, self.view3d) #============================================= #retrieve the travel data #============================================= if "travels" in self.arg_list: path_lines = self.path_lines extrude_meshes = self.extrude_meshes extrude_lines = self.extrude_lines travel_dir = self.travel_dir mesh_vis, bdry_vis, path_vis = stg_func.retrieve_travel_data(nxt_index, year, travel_dir, extrude_meshes, extrude_lines, path_lines, self.view3d) if mesh_vis !=None: stg_func.viz_graphic_items([mesh_vis], self.view3d) stg_func.viz_graphic_items([bdry_vis], self.view3d) if path_vis !=None: stg_func.viz_graphic_items([path_vis], self.view3d) self.path_lines = [path_vis] self.extrude_meshes = [mesh_vis] self.extrude_lines = [bdry_vis] #============================================= #retrieve the parking data #============================================= if "parkings" in self.arg_list: parking_meshes = self.parking_meshes parking_dir = self.parking_dir parking_mesh = stg_func.retrieve_parking_data(nxt_index, year, parking_dir, parking_meshes, self.view3d, self.min_val, self.max_val) if parking_mesh != None: stg_func.viz_graphic_items([parking_mesh], self.view3d) self.parking_meshes = [parking_mesh]
def backward(self): current_date = self.current_date backward_date = current_date - timedelta(hours=1) year = backward_date.year backward = stg_func.date2index(backward_date) #============================================= #retrieve the solar data from the date index #============================================= solar_dir = self.solar_dir solar_mesh = self.colour_meshes stg_func.retrieve_solar_data(solar_mesh[0], backward, solar_dir, self.min_val, self.max_val) #============================================= #retrieve the travel data #============================================= path_lines = self.path_lines extrude_meshes = self.extrude_meshes extrude_lines = self.extrude_lines travel_dir = self.travel_dir mesh_vis, bdry_vis, path_vis = stg_func.retrieve_travel_data( backward, year, travel_dir, extrude_meshes, extrude_lines, path_lines, self.view3d) if mesh_vis != None: stg_func.viz_graphic_items([mesh_vis], self.view3d) stg_func.viz_graphic_items([bdry_vis], self.view3d) if path_vis != None: stg_func.viz_graphic_items([path_vis], self.view3d) self.path_lines = [path_vis] self.extrude_meshes = [mesh_vis] self.extrude_lines = [bdry_vis] hrly_cart_bool = self.params.param('Layers').param( 'Extrusion Layer').param("Hourly Cart Travel Behaviour").value() stg_func.set_graphic_items_visibility(self.path_lines, hrly_cart_bool) stg_func.set_graphic_items_visibility(self.extrude_meshes, hrly_cart_bool) stg_func.set_graphic_items_visibility(self.extrude_lines, hrly_cart_bool) #============================================= #retrieve the parking data #============================================= parking_meshes = self.parking_meshes parking_dir = self.parking_dir parking_mesh = stg_func.retrieve_parking_data( backward, year, parking_dir, parking_meshes, self.view3d, self.min_val, self.max_val) if parking_mesh != None: stg_func.viz_graphic_items([parking_mesh], self.view3d) self.parking_meshes = [parking_mesh] if self.is_parking_layer: hrly_park_bool = self.params.param('Layers').param( 'Falsecolour Layer').param("Hourly Parking Spots").value() stg_func.set_graphic_items_visibility(self.parking_meshes, hrly_park_bool) #============================================= #update the dates #============================================= self.current_index = backward self.current_date = backward_date str_date = backward_date.strftime("%Y-%m-%d %H:%M:%S") self.params.param('Load Result').param('Data Loaded').setValue( str_date) self.params.param('Load Result').param('Date of Interest').param( "Year:").setValue(int(backward_date.strftime("%Y"))) self.params.param('Load Result').param('Date of Interest').param( "Month:").setValue(int(backward_date.strftime("%m"))) self.params.param('Load Result').param('Date of Interest').param( "Day:").setValue(int(backward_date.strftime("%d"))) self.params.param('Load Result').param('Date of Interest').param( "Hour:").setValue(int(backward_date.strftime("%H")))
def load_data(self): #get the specified date s_year = self.params.param('Load Result').param( 'Date of Interest').param("Year:").value() s_mth = self.params.param('Load Result').param( 'Date of Interest').param("Month:").value() s_day = self.params.param('Load Result').param( 'Date of Interest').param("Day:").value() s_hour = self.params.param('Load Result').param( 'Date of Interest').param("Hour:").value() s_min = 0 s_sec = 0 str_sp_date = str(s_year) + "-" + str(s_mth) + "-" + str(s_day) + "-" +\ str(s_hour) + ":" + str(s_min) + ":" + str(s_sec) date = parse(str_sp_date) year = date.year self.current_date = date str_date = date.strftime("%Y-%m-%d %H:%M:%S") self.params.param('Load Result').param('Data Loaded').setValue( str_date) hour_index = stg_func.date2index(date) self.current_index = hour_index #============================================= #retrieve the solar data from the date index #============================================= solar_dir = self.solar_dir solar_mesh = self.colour_meshes stg_func.retrieve_solar_data(solar_mesh[0], hour_index, solar_dir, self.min_val, self.max_val) stg_func.viz_graphic_items(solar_mesh, self.view3d) #============================================= #retrieve the travel data #============================================= path_lines = self.path_lines extrude_meshes = self.extrude_meshes extrude_lines = self.extrude_lines travel_dir = self.travel_dir mesh_vis, bdry_vis, path_vis = stg_func.retrieve_travel_data( hour_index, year, travel_dir, extrude_meshes, extrude_lines, path_lines, self.view3d) if mesh_vis != None: stg_func.viz_graphic_items([mesh_vis], self.view3d) stg_func.viz_graphic_items([bdry_vis], self.view3d) if path_vis != None: stg_func.viz_graphic_items([path_vis], self.view3d) self.path_lines = [path_vis] self.extrude_meshes = [mesh_vis] self.extrude_lines = [bdry_vis] hrly_cart_bool = self.params.param('Layers').param( 'Extrusion Layer').param("Hourly Cart Travel Behaviour").value() stg_func.set_graphic_items_visibility(self.path_lines, hrly_cart_bool) stg_func.set_graphic_items_visibility(self.extrude_meshes, hrly_cart_bool) stg_func.set_graphic_items_visibility(self.extrude_lines, hrly_cart_bool) #============================================= #retrieve the parking data #============================================= parking_meshes = self.parking_meshes parking_dir = self.parking_dir parking_mesh = stg_func.retrieve_parking_data( hour_index, year, parking_dir, parking_meshes, self.view3d, self.min_val, self.max_val) if parking_mesh != None: stg_func.viz_graphic_items([parking_mesh], self.view3d) self.parking_meshes = [parking_mesh] if self.is_parking_layer: hrly_park_bool = self.params.param('Layers').param( 'Falsecolour Layer').param("Hourly Parking Spots").value() stg_func.set_graphic_items_visibility(self.parking_meshes, hrly_park_bool)
def plot_data(self): start_date = parse(self.str_plot_start_date) start_hour = stg_func.date2index(start_date) start_year = start_date.year end_date = parse(self.str_plot_end_date) end_hour = stg_func.date2index(end_date) end_year = end_date.year path_dict = stg_func.retrieve_travel_path_analysis( self.travel_dir, start_hour, start_year, end_hour, end_year) parking_dict = stg_func.retrieve_parking_analysis( self.parking_dir, start_hour, start_year, end_hour, end_year) y1 = self.params.param('Plot').param('Y-axis1:').value() y2 = self.params.param('Plot').param('Y-axis2:').value() val_list = self.val_list key_list = [ "total_park_time", "total_dist", "solar_max", "solar_med", "solar_min" ] key_index1 = val_list.index(y1) key_index2 = val_list.index(y2) key1 = key_list[key_index1] key2 = key_list[key_index2] y1_list = [] y2_list = [] hlist = [] if start_year == end_year: year_list = [start_year] else: year_list = range(start_year, end_year + 1) projection = Proj(proj='utm', zone=18, ellps='GRS80', preserve_units=False) timezone = gettz() nyear = len(year_list) ycnt = 0 for year in year_list: week_list, hour_interest = stg_func.gen_week_hour_interest_from_year_cnt( ycnt, nyear, start_hour, end_hour) for hour in hour_interest: res_dict = stg_func.retrieve_plot_data(hour, year, path_dict, parking_dict, projection) y1_list.append(res_dict[key1]) y2_list.append(res_dict[key2]) date_str = res_dict["date_str"] date = parse(date_str) date = date.replace(tzinfo=timezone) timestamp = datetime.timestamp(date) hlist.append(timestamp) ycnt += 1 self.plot.setLabel("left", y1) self.plot.getAxis('right').setLabel(y2, color='RED') print(len(hlist), len(y1_list)) self.curve1.setData(x=hlist, y=y1_list) self.curve2.setData(x=hlist, y=y2_list)