def ChangeBySlider(self,event): if not self.MappingData == []: for e in self.MappingData: if self.DistRadDict[e[0]] == '00CCFF': e = e else: if self.DistRadDict[e[0]] < self.DistanceRadSlider.GetValue(): e[3] = "339900" else: e[3] = "FF0000" mymap = pygmapscustom.maps(self.MappingData[0][1],self.MappingData[0][2],10,self.MappingData) mymap.draw('./Tempmap.html') TempMap = htmldir + "\Tempmap.html" self.browser.LoadURL(TempMap)
def Calculate(self,DataS,DataD): #Clears the variables RowData=[] NoRoutes = [] BadPost = [] MappingData = [] for e in range(1,6): postcodestring = 'DataS[self.ServiceEntryLoc%s.GetValue()] = self.ServiceEntryName%s.GetValue()' %(e,e) exec(postcodestring) if '' in DataS: del DataS[''] #Ensures there is data to send if DataD == {} or DataS =={}: wx.MessageBox('Please select demand and service files', 'Missing files!', wx.OK | wx.ICON_ERROR) return #Sets up the progress gauge count = 0 Task_Range = 0 if self.DemandSummary.GetValue() == True or self.AppendOutputSelect.GetValue() == True: Task_Range += len(DataS)*len(DataD) if self.MapOutput.GetValue() == True: Task_Range +=len(DataD) + len(DataS) self.DistGauge.SetRange(Task_Range) self.DistGauge.SetValue(count) #Allows the user to enter a save location, or timestamps by default if self.UserSaveLoc.GetValue() == False: filebase = ".\Testdata\%s" %(time.time()) filetype = ".csv" fileloc = filebase[:-3] + filetype else: fileloc = "" SaveFileDialog = wx.FileDialog(self,"Choose save location", os.getcwd(),"" ,"csv Files (*.csv)|*.csv", wx.FD_SAVE) #Closes the dialog if the user cancels if SaveFileDialog.ShowModal() == wx.ID_CANCEL: return #Returns the file path fileloc = SaveFileDialog.GetPath() if self.DemandSummary.GetValue() == True or self.AppendOutputSelect.GetValue() == True: if self.DemandSummary.GetValue() == True: #Adds the unit of time to the data file RowData.append("%s %s" %(self.TimeUnitSelect.GetValue(),self.DistUnitSelect.GetValue())) RowData.append("Demand") #adds the demand locations as a heading for e in DataS: RowData.append(e + ' Time') RowData.append(e + ' Distance') if len(DataS)*len(DataD) < 2400: slowparam = 0.2 else: slowparam = 35 if self.DemandSummary.GetValue() == True: #Writes the header outfile = open(fileloc,'wb') output=csv.writer(outfile) output.writerow(RowData) outfile.close() self.GaugeText.SetLabel('Status: Calculating Distances') #Sends the requests, and returns the data for D in DataD: RowData = [] RowData.append(D) RowData.append(DataD[D]) for S in DataS: count+=1 self.DistGauge.SetValue(count) time.sleep(slowparam) GoogleData = LinkBuilder.CalculateDistance(D,S,self.TravelMethod.GetValue().lower(),self.TimeUnitSelect.GetValue(),self.DistUnitSelect.GetValue()) #Chooses the correct response to error messages if GoogleData == "OVER_QUERY_LIMIT": break if GoogleData[0] == "NOT_FOUND": BadPost.append(D) if GoogleData[0] == "ZERO_RESULTS": NoRoutes.append(D) RowData.append(GoogleData[0]) RowData.append(GoogleData[1]) self.FileAppendDict[D] = RowData[2:] #Saves the data upon each loop, to ensure no loss of data if self.DemandSummary.GetValue() == True: outfile = open(fileloc,'ab') output=csv.writer(outfile) output.writerow(RowData) outfile.close() if GoogleData == "OVER_QUERY_LIMIT": break #Informs the user that they have gone over the limit if GoogleData == "OVER_QUERY_LIMIT": wx.MessageBox('You have exceeded the request limit \nTry again later, although you may have to wait up to 24 hours to receive a new quota \nYour partial output has been saved', 'Too many requests', wx.OK | wx.ICON_ERROR) #Tells the user which postcodes didnt work if not NoRoutes == [] or not BadPost == []: BadPostString = "" NoRoutesString = "" for e in BadPost: BadPostString += "%s " %(e) for e in NoRoutes: NoRoutesString += "%s " %(e) BadDataString = "\tThere were several requests amongst the data: \n\tThese post codes could not be geoencoded \n\t%s \n\tThese postcodes could not be connected to service locations \n\t %s" %(BadPostString,NoRoutesString) wx.MessageBox(BadDataString, 'Unable to find data', wx.OK | wx.ICON_WARNING) if self.AppendOutputSelect.GetValue() == True: try: self.AppendData(DataS,self.FileAppendDict,0) except: wx.MessageBox('The original data file could not be appended to, it may be open in another location or may no longer exist', 'Original File Not Found', wx.OK | wx.ICON_ERROR) MaxDemand = DataD.itervalues().next() for e in DataD: if DataD[e] > MaxDemand: MaxDemand = DataD[e] if self.MapSave.GetValue() == True: if self.MapType.GetValue() == "Standard": LinkBuilder.StaticMaps(DataS,DataD,fileloc[:-4]) elif self.MapType.GetValue() == "Heat Map": for e in range(11): self.HeatDict[e] = [] for i in DataD: if int(10*DataD[str(i)]/MaxDemand) == e: self.HeatDict[e].append(i) LinkBuilder.StaticMapHeat(DataS,self.HeatDict,self.hexdict,fileloc[:-4]) if self.MapOutput.GetValue() == True: self.GaugeText.SetLabel('Status: Geoencoding') #Geoencodes the data for e in DataS: count +=1 self.DistGauge.SetValue(count) time.sleep(0.2) Latlngdata = LinkBuilder.Geoencoding(e) if Latlngdata[0] == "OK": MappingData.append([str(e).upper() + ' %s' %(str(DataS[e])),Latlngdata[1],Latlngdata[2],"0000FF"]) for e in DataD: count +=1 self.DistGauge.SetValue(count) time.sleep(0.2) Latlngdata = LinkBuilder.Geoencoding(e) if Latlngdata[0] == "OK": if self.MapType.GetValue() == "Heat Map": MappingData.append([str(e).upper() + ' Demand : %s' %(self.DemandLoc[str(e)]),Latlngdata[1],Latlngdata[2],self.hexdict[int(10*DataD[str(e)]/MaxDemand)]]) else: MappingData.append([str(e).upper() + ' Demand : %s' %(self.DemandLoc[str(e)]),Latlngdata[1],Latlngdata[2],'FFFF00']) #Creates the html file with markers and reloads the map mymap = pygmapscustom.maps(MappingData[0][1],MappingData[0][2],10,MappingData) mymap.draw('./Tempmap.html') TempMap = htmldir + "\Tempmap.html" self.browser.LoadURL(TempMap) #Shows the user that the Tool is ready self.GaugeText.SetLabel('Status: Ready')
def DistanceRadius(self): #Initialises the variables self.DistanceRadSlider.Disable() ServiceRadLoc = self.ServiceRadInput.GetValue() ServiceRadLoc = ServiceRadLoc.strip() MappingData = [] DemandWithin = [] DemandOutside = [] self.DistRadDict = {} self.DistRadAppend = {} #Ensures there is data to send if ServiceRadLoc == "" or len(ServiceRadLoc) > 12 or self.DemandLoc == {}: wx.MessageBox('Choose a service location and demand file', 'No postcodes!', wx.OK | wx.ICON_ERROR) return #Sets the Radius to default if nothing is entered if self.DrivingRadInput.GetValue() == "": DrivingRadius = 10 else: try: DrivingRadius = eval(self.DrivingRadInput.GetValue()) except: DrivingRadius = 10 #Only runs the user selected options if self.DemandSummary.GetValue() == True or self.MapSave.GetValue() == True: #Chooses the save location if self.UserSaveLoc.GetValue() == False: filebase = ".\Testdata\Radius%s" %(time.time()) filetype = ".csv" fileloc = filebase[:-3] + filetype else: fileloc = "" SaveFileDialog = wx.FileDialog(self,"Choose Save Location", os.getcwd(),"" ,"csv Files (*.csv)|*.csv", wx.FD_SAVE) #Closes the dialog if the user cancels if SaveFileDialog.ShowModal() == wx.ID_CANCEL: return fileloc = SaveFileDialog.GetPath() Header = 'Within %s %s of %s' %(DrivingRadius,self.RadUnitSelect.GetValue(),ServiceRadLoc) if self.DemandSummary.GetValue() == True: #puts the header on the file outfile = open(fileloc,'ab') output=csv.writer(outfile) output.writerow(['Post Code',Header]) outfile.close() #Stores the service Location Latlngdata = LinkBuilder.Geoencoding(ServiceRadLoc) if not Latlngdata[0] == "OK": wx.MessageBox('The service location has not been recognised by the google distance matrix service', 'Service postcode is invalid', wx.OK | wx.ICON_ERROR) return MappingData.append([str(ServiceRadLoc),Latlngdata[1],Latlngdata[2],"0000FF"]) self.DistRadDict[str(ServiceRadLoc)] = "00CCFF" count = 0 self.DistGauge.SetValue(count) Task_Range = len(self.DemandLoc) self.DistGauge.SetRange(Task_Range) self.GaugeText.SetLabel('Status: Calculating Distances') #Geoencodes and adds Icons to the map for e in self.DemandLoc: time.sleep(0.2) Latlngdata = LinkBuilder.Geoencoding(e) if self.RadUnitSelect.GetValue() == "Minutes": GoogleData = LinkBuilder.CalculateDistance(e,ServiceRadLoc,"driving","Minutes")[0] elif self.RadUnitSelect.GetValue() == "Kilometres": GoogleData = LinkBuilder.CalculateDistance(e,ServiceRadLoc,"driving","Minutes","Kilometres")[1] elif self.RadUnitSelect.GetValue() == "Miles": GoogleData = LinkBuilder.CalculateDistance(e,ServiceRadLoc,"driving","Minutes","Miles")[1] self.DistRadDict[str(e)] = GoogleData count+=1 self.DistGauge.SetValue(count) if Latlngdata[0] == "OK": if GoogleData < DrivingRadius: MappingData.append([str(e),Latlngdata[1],Latlngdata[2],"00CC33"]) DemandWithin.append(str(e)) self.DistRadAppend[str(e)] = ['True'] if self.DemandSummary.GetValue() == True: #Saves the result outfile = open(fileloc,'ab') output=csv.writer(outfile) output.writerow([str(e),'True']) outfile.close() else: MappingData.append([str(e),Latlngdata[1],Latlngdata[2],"FF0000"]) DemandOutside.append(str(e)) self.DistRadAppend[str(e)] = ['False'] if self.DemandSummary.GetValue() == True: #Saves the result outfile = open(fileloc,'ab') output=csv.writer(outfile) output.writerow([str(e),'False']) outfile.close() if self.AppendOutputSelect.GetValue() ==True: try: self.AppendData(ServiceRadLoc,self.DistRadAppend,1,Header) except: wx.MessageBox('The original data file could not be appended to, it may be open in another location or may no longer exist', 'Original File Not Found', wx.OK | wx.ICON_ERROR) if self.MapSave.GetValue() == True: LinkBuilder.StaticMapRadius(ServiceRadLoc,DemandWithin,DemandOutside,fileloc[:-4]) if self.MapOutput.GetValue() == True: self.GaugeText.SetLabel('Status: Creating Map') self.MappingData = MappingData #Creates the html file with markers and reloads the map mymap = pygmapscustom.maps(MappingData[0][1],MappingData[0][2],10,MappingData) mymap.draw('./Tempmap.html') TempMap = htmldir + "\Tempmap.html" self.browser.LoadURL(TempMap) self.DistanceRadSlider.SetValue(DrivingRadius) self.DistanceRadSlider.Enable() self.GaugeText.SetLabel('Status: Ready')