def setBinColor(self, colorProfile): self.binLims = GenerateCPCMap.CreateBins( "static/BinLimits.csv").tolist() self.colsHex = GenerateCPCMap.AssignColours(self.binLims, colorProfile) if not os.path.exists(self.colorbar): GenerateCPCMap.CreateColourBar(self.binLims, self.colsHex, colorProfile)
def getArrayStats(self): midpoints = [] minpoints = [] maxpoints = [] for key in self.data: arrstats = GenerateCPCMap.ArrayStats(self.data[key].lats, self.data[key].lons) midpoints.append(arrstats['middle']) minpoints.append(arrstats['min']) maxpoints.append(arrstats['max']) self.midpoint = GenerateCPCMap.elementMean(midpoints).tolist() self.extent.append(GenerateCPCMap.elementMin(minpoints)) self.extent.append(GenerateCPCMap.elementMax(maxpoints))
def getData(self): try: with open(CPC_DIR + '/CPC_' + str(self.id) + '.csv', 'r', encoding='iso8859_15') as CPCFile: CPCtext = CPCFile.read() CPCData, CPCdate, CPClen = GenerateCPCMap.ReadCPCFile(CPCtext) GPSData = pandas.read_pickle( GPS_DIR + '/GPS_' + str(self.id) + '.pkl') MergeData = GenerateCPCMap.NearestNghbr(CPCData, GPSData) self.lats = MergeData['lat'] self.lons = MergeData['lon'] self.concs = MergeData['conc'] except Exception as e: flash('Error generating map: ' + str(e), 'danger') return redirect(subd + '/error')
def average(self): if self.concs: self.concMedian = GenerateCPCMap.Median(self.concs)
def uploads(): # If user tries to upload a file if request.method == 'POST': # No file part: if 'file' not in request.files: flash('No file part', 'danger') return redirect(subd + '/uploads') # Get file info file = request.files['file'] # No selected file if file.filename == '': flash('No file selected', 'danger') return redirect(subd + '/uploads') # Else upload file (unless bad extension) if file and allowed_file(file.filename): try: CPCtext = file.read().decode("iso8859_15") CPCData, CPCdate, CPClen = GenerateCPCMap.ReadCPCFile(CPCtext) GPSData = GenerateCPCMap.FetchGPSData('StravaTokens.txt', CPCdate, CPClen) MergeData = GenerateCPCMap.NearestNghbr(CPCData, GPSData) except Exception: raise # Add entry to CPCFiles DB # Create cursor db = get_db() cur = db.cursor() # Execute query: cur.execute( "INSERT INTO CPCFiles(filename, username, start_date) VALUES(?, ?, ?)", (secure_filename(file.filename), session['username'], CPCdate)) # Commit to DB db.commit() # Close connection cur.close() # Save CPC file, renaming based on DB ID lastID = query_db( 'SELECT * FROM CPCFiles ORDER BY id DESC LIMIT 1', one=True)['id'] CPCFile = open(CPC_DIR + '/CPC_' + str(lastID) + '.csv', 'w', encoding='iso8859_15') CPCFile.write(CPCtext) CPCFile.close() # save GPS dataframe GPSData.to_pickle(GPS_DIR + '/GPS_' + str(lastID) + '.pkl') # calculate averages results = query_db('SELECT * FROM CPCFiles') dataset = {} for result in results: data = MapData(result['id']) dataset[data.id] = data grid = Grid('hex.geojson') grid.getAverage(dataset) with open('static/average.json', 'w+') as f: f.seek(0) json.dump(grid.toJSON(), f, cls=ComplexEncoder, indent=1) # return flash('File uploaded', 'success') return redirect(subd + '/uploads') else: flash('Only .csv files allowed', 'danger') return redirect(subd + '/uploads') # If user just navigates to page AllCPCFiles = query_db('SELECT * FROM CPCFiles') if AllCPCFiles is not None: # AllCPCFiles = reversed(AllCPCFiles) return render_template('uploads.html', AllCPCFiles=AllCPCFiles, LoggedIn=('logged_in' in session), subd=subd) else: return render_template('uploads.html', LoggedIn=('logged_in' in session), subd=subd)
def delete_CPCFile(id): #Get start date of entry to be deleted delDate = parse( query_db('SELECT * FROM CPCFiles WHERE id = ?', (id, ), one=True)['start_date']) #Create cursor db = get_db() cur = db.cursor() #Execute query: cur.execute("DELETE FROM CPCFiles WHERE id = ?", [id]) #Commit to DB db.commit() #Close connection cur.close() #Move associated files to a 'deleted' directory if os.path.exists(CPC_DIR + '/CPC_' + id + '.csv'): os.rename(CPC_DIR + '/CPC_' + id + '.csv', CPC_DEL_DIR + '/CPC_' + id + '.csv') if os.path.exists(GPS_DIR + '/GPS_' + id + '.pkl'): os.rename(GPS_DIR + '/GPS_' + id + '.pkl', GPS_DEL_DIR + '/GPS_' + id + '.pkl') #Delete and update latest map if this was it: latestByDate = query_db( 'SELECT * FROM CPCFiles ORDER BY start_date DESC LIMIT 1', one=True) if latestByDate is not None: latestDate = parse(latestByDate['start_date']) latestID = latestByDate['id'] if delDate > latestDate: if os.path.exists(MAP_DIR + '/latest.html'): os.remove(MAP_DIR + '/latest.html') try: with open(CPC_DIR + '/CPC_' + str(latestID) + '.csv', 'r', encoding='utf-8') as CPCFile: CPCtext = CPCFile.read() CPCData, CPCdate, CPClen = GenerateCPCMap.ReadCPCFile( CPCtext) GPSData = pandas.read_pickle(GPS_DIR + '/GPS_' + str(latestID) + '.pkl') MergeData = GenerateCPCMap.NearestNghbr(CPCData, GPSData) mapFileIn = GenerateCPCMap.CreateMap(MergeData, str(latestID), MAP_DIR) mapTitle = 'Concentration map for walk commencing ' + str( CPCdate) mapFileOut = GenerateCPCMap.BuildMap(MAP_DIR, str(latestID), mapFileIn, mapTitle, subd=subd) os.remove(MAP_DIR + '/' + mapFileIn) os.rename(MAP_DIR + '/' + mapFileOut, MAP_DIR + '/latest.html') except: raise else: if os.path.exists(MAP_DIR + '/latest.html'): os.remove(MAP_DIR + '/latest.html') flash('CPC file deleted', 'success') return redirect(subd + '/uploads')
def maps(id, mapType): if not os.path.exists(GPS_DIR + '/GPS_' + id + '.pkl'): abort(404) start_date = query_db('SELECT * FROM CPCFiles WHERE id = ?', (id, ), one=True)['start_date'] parseDate = parse(start_date) startYMD = dt.date(parseDate.year, parseDate.month, parseDate.day) AllCPCFiles = query_db('SELECT * FROM CPCFiles') numCPCFiles = len(AllCPCFiles) allDates = [parse(x['start_date']) for x in AllCPCFiles] YMD = [] for date in allDates: YMD.append(dt.date(date.year, date.month, date.day)) if mapType == "multi" and YMD.count(startYMD) > 1: ids = [] for i, date in enumerate(YMD): if (date == startYMD): ids.append(AllCPCFiles[i]['id']) MergeDataAll = [] try: for idx in ids: with open(CPC_DIR + '/CPC_' + str(idx) + '.csv', 'r', encoding='utf-8') as CPCFile: CPCtext = CPCFile.read() CPCData, CPCdate, CPClen = GenerateCPCMap.ReadCPCFile( CPCtext) GPSData = pandas.read_pickle(GPS_DIR + '/GPS_' + str(idx) + '.pkl') MergeData = GenerateCPCMap.NearestNghbr(CPCData, GPSData) MergeDataAll.append(MergeData) MergeDataConcat = pandas.concat(MergeDataAll) mapFileIn = GenerateCPCMap.CreateMap(MergeDataConcat, id, MAP_DIR, addMarkers=False) except Exception as e: flash('Error generating map: ' + str(e), 'danger') return redirect(subd + '/error') mapTitle = 'Concentration map for all walks on ' + str(startYMD) elif mapType == "single" or (mapType == "multi" and YMD.count(startYMD) == 1): try: with open(CPC_DIR + '/CPC_' + id + '.csv', 'r', encoding='utf-8') as CPCFile: CPCtext = CPCFile.read() CPCData, CPCdate, CPClen = GenerateCPCMap.ReadCPCFile(CPCtext) GPSData = pandas.read_pickle(GPS_DIR + '/GPS_' + id + '.pkl') MergeData = GenerateCPCMap.NearestNghbr(CPCData, GPSData) mapFileIn = GenerateCPCMap.CreateMap(MergeData, id, MAP_DIR) except Exception as e: flash('Error generating map: ' + str(e), 'danger') return redirect(subd + '/error') mapTitle = 'Concentration map for walk commencing ' + start_date else: abort(404) mapFileOut = GenerateCPCMap.BuildMap(MAP_DIR, id, mapFileIn, mapTitle, subd=subd) with open(MAP_DIR + '/' + mapFileOut) as f: mapText = f.read() os.remove(MAP_DIR + '/' + mapFileIn) os.remove(MAP_DIR + '/' + mapFileOut) return mapText
def uploads(): #If user tries to upload a file if request.method == 'POST': #No file part: if 'file' not in request.files: flash('No file part', 'danger') return redirect(subd + '/uploads') #Get file info file = request.files['file'] #No selected file if file.filename == '': flash('No file selected', 'danger') return redirect(subd + '/uploads') #Else upload file (unless bad extension) if file and allowed_file(file.filename): try: CPCtext = file.read().decode("utf-8") CPCData, CPCdate, CPClen = GenerateCPCMap.ReadCPCFile(CPCtext) GPSData = GenerateCPCMap.FetchGPSData('StravaTokens.txt', CPCdate, CPClen) MergeData = GenerateCPCMap.NearestNghbr(CPCData, GPSData) except Exception: raise #Add entry to CPCFiles DB #Create cursor db = get_db() cur = db.cursor() #Execute query: cur.execute( "INSERT INTO CPCFiles(filename, username, start_date) VALUES(?, ?, ?)", (secure_filename(file.filename), session['username'], CPCdate)) #Commit to DB db.commit() #Close connection cur.close() #Save CPC file, renaming based on DB ID lastID = query_db( 'SELECT * FROM CPCFiles ORDER BY id DESC LIMIT 1', one=True)['id'] CPCFile = open(CPC_DIR + '/CPC_' + str(lastID) + '.csv', 'w', encoding='utf-8') CPCFile.write(CPCtext) CPCFile.close() #save GPS dataframe GPSData.to_pickle(GPS_DIR + '/GPS_' + str(lastID) + '.pkl') #Save map as latest if applicable latestDate = parse( query_db( 'SELECT * FROM CPCFiles ORDER BY start_date DESC LIMIT 1', one=True)['start_date']) if CPCdate >= latestDate: mapFileIn = GenerateCPCMap.CreateMap(MergeData, str(lastID), MAP_DIR) mapTitle = 'Concentration map for walk commencing ' + str( CPCdate) mapFileOut = GenerateCPCMap.BuildMap(MAP_DIR, str(lastID), mapFileIn, mapTitle, subd=subd) os.remove(MAP_DIR + '/' + mapFileIn) if os.path.exists(MAP_DIR + '/latest.html'): os.remove(MAP_DIR + '/latest.html') os.rename(MAP_DIR + '/' + mapFileOut, MAP_DIR + '/latest.html') #return flash('File uploaded', 'success') return redirect(subd + '/uploads') else: flash('Only .csv files allowed', 'danger') return redirect(subd + '/uploads') #If user just navigates to page AllCPCFiles = query_db('SELECT * FROM CPCFiles') if AllCPCFiles is not None: AllCPCFiles = reversed(AllCPCFiles) return render_template('uploads.html', AllCPCFiles=AllCPCFiles, LoggedIn=('logged_in' in session), subd=subd) else: return render_template('uploads.html', LoggedIn=('logged_in' in session), subd=subd)