def __init__(self, lat, lng): # maps neighbor node to distance """ a node representing a location on the map using latitude and longitude :param lat: latitude of this node :param lng: longitude """ self.neighbors = dict() # dict of node -> distance self.latitude = lat self.longitude = lng self.elevation = elevation(Client(key='AIzaSyCtnZS7miejfbAh1FsKUBhxil1VXa0EicY'), (lat, lng))[0]['elevation']
def main(): # Get missing station elevations # [{'elevation': 182.4646606445312, 'location': {'lat': 41.93625, 'lng': -87.65357}, 'resolution': 152.7032318115234}] gmaps = Client(key=API_KEY) # print(elevation.elevation(gmaps, (41.936253, -87.653566))) db_conn = util.get_database_connection() while True: stations = db_conn.execute(GET_START_STATIONS_QUERY).fetchall() locations = list(map(lambda x: (x[1], x[2]), stations)) if len(locations) > 0: location_results = elevation(gmaps, locations) updates = [] for l, s in zip(location_results, stations): updates.append((l['elevation'], s[1], s[2])) db_conn.executemany(UPDATE_START_STATION_QUERY, updates) db_conn.commit() else: break while True: stations = db_conn.execute(GET_END_STATIONS_QUERY).fetchall() locations = list(map(lambda x: (x[1], x[2]), stations)) if len(locations) > 0: location_results = elevation(gmaps, locations) updates = [] for l, s in zip(location_results, stations): updates.append((l['elevation'], s[1], s[2])) db_conn.executemany(UPDATE_END_STATION_QUERY, updates) db_conn.commit() else: break db_conn.close()
def getElevation(client, address): """ Get elevation of given address :param client: a google map client :type client: googlemap.client.Client :param addresses: str represent address :type addresses: str :return: elev, representing elevation of the address given :rtype: float """ latitude, longitude = getLocation(client, address) elev = elevation(client, [(latitude, longitude)])[0]['elevation'] return elev
def get_elevation(gmaps, point): m = hashlib.md5() m.update(str(point)) key = m.hexdigest() c = cache.get(key) if c: logging.warning("Use cache") return c else: r = elevation.elevation(gmaps, point) cache.set(key, r) return r
def __init__(self, lat, lng, elev=None): # maps neighbor node to distance """ a node representing a location on the map using latitude and longitude :param lat: latitude of this node :param lng: longitude """ self.neighbors = dict() # dict of node -> distance self.latitude = lat self.longitude = lng if not elev: self.elevation = elevation(gmaps, (lat, lng))[0]['elevation'] else: self.elevation = elev
def collect(display, data, lat, long): country_code = data['country_code'] capital = get_capital(country_code) capital_location = capital_loc(capital) city = get_city(data) return { 'verbose_name': display, 'country': data['country'], 'city': city or 'not found', 'capital': capital, 'elevation': round(elevation(gmaps, (float(lat), float(long)))[0]['elevation']), 'street': data['road'], 'state': data['state'], 'postcode': data['postcode'], 'house': data['house_number'], 'code': country_code, 'direct': direct_distance(capital_location, lat, long), 'car_time': car_time(capital_location, lat, long), 'near': ', '.join([x.name for x in nearly_capitals(lat, long)]) }
def getElevationFromPath(gmaps, start, goal): """ get elevation gain using steps calculated by google map :return: latitudes, longitudes, elevation gain """ directionResult = gmaps.directions((start.latitude, start.longitude), (goal.latitude, goal.longitude)) steps = directionResult[0]['legs'][0]['steps'] latitudes = [start.latitude] longitudes = [start.longitude] elevGain = 0 prevElev = start.elevation for step in steps: lat = step['end_location']['lat'] lng = step['end_location']['lng'] latitudes.append(lat) longitudes.append(lng) currElev = elevation(gmaps, (lat, lng))[0]['elevation'] elevGain += max(0, currElev - prevElev) prevElev = currElev return latitudes, longitudes, elevGain
def upload(self): #---------------------------------------------------------------------- if (debug): print 'RunningRoutesFiles.upload()' self._set_services() thisfile = request.files['upload'] filename = thisfile.filename filetype = filename.split('.')[-1] filecontents = thisfile.readlines() thisfile.seek(0) latlng = LatLng(thisfile, filetype) locations = latlng.getpoints() thisss = self.sheets.spreadsheets().create(body={ 'properties' : { 'title' : filename }, 'sheets' : [ { 'properties' : { 'title' : filename } }, { 'properties' : { 'title' : 'path' } }, { 'properties' : { 'title' : 'turns' } }, ] }).execute(); # put file in appropriate folder # see https://stackoverflow.com/questions/42938990/google-sheets-api-create-or-move-spreadsheet-into-folder fileid = thisss['spreadsheetId'] thisfile = self.drive.files().get( fileId=fileid, fields='parents' ).execute() parents = ','.join(thisfile['parents']) self.drive.files().update( fileId=fileid, removeParents=parents, addParents=self.datafolderid ).execute() # calculate distance in km distance = 0.0 maxinterval = app.config['APP_MAX_DIST_INTERVAL']/1000.0 # convert m to km locs = [locations[0]] # anno is list of [cumdist, inserted], where inserted is empty or 'inserted' anno = [[0, '']] for i in range(1,len(locations)): thisdist = geodist.haversineDistance(locations[i-1], locations[i], False) eachdist = thisdist # add additional points if the distance between these is longer than allowed if thisdist > maxinterval: numnew = int(thisdist / maxinterval) eachdist = thisdist / (numnew+1) bearing = calculateBearing(locations[i-1], locations[i]) lastcoord = locations[i-1] for j in range(numnew): distance += eachdist anno.append([distance, 'inserted']) # getDestinationLatLng expects distance in meters newcoord = geodist.getDestinationLatLng(lastcoord, bearing, eachdist*1000) locs.append(newcoord) lastcoord = newcoord distance += eachdist anno.append([distance, '']) locs.append(locations[i]) # convert to miles distance /= 1.609344 # query for elevation points ## slice off locations which meet max sample requirements from google for each query pathlen = MAX_SAMPLES gelevs = [] while len(locs) > 0: if debug: print 'len(locs) = {}'.format(len(locs)) theselocs = locs[:pathlen] del locs[:pathlen] ## get this set of elevations elev = elevation(gmapsclient, theselocs) gelevs += [[p['location']['lat'], p['location']['lng'], p['elevation'], p['resolution']] for p in elev] # calculate elevation gain elevations = numpy.array([float(p[2]) for p in gelevs]) upthreshold = app.config['APP_ELEV_UPTHRESHOLD'] downthreshold = app.config['APP_ELEV_DOWNTHRESHOLD'] smoothwin = app.config['APP_SMOOTHING_WINDOW'] ## first smooth the elevations using flat window ## see http://scipy-cookbook.readthedocs.io/items/SignalSmooth.html s = numpy.r_[elevations[smoothwin-1:0:-1],elevations,elevations[-2:-smoothwin-1:-1]] w = numpy.ones(smoothwin,'d') y=numpy.convolve(w/w.sum(),s,mode='valid') smoothed = y[(smoothwin/2):-(smoothwin/2)] # reference suggested below to # smoothed = y[(smoothwin/2-1):-(smoothwin/2)] ## calculate the gain using the smoothed elevation profile gain = elevation_gain(smoothed, upthreshold=upthreshold, downthreshold=downthreshold) # combine gelevs with anno if len(gelevs) != len(anno) or len(gelevs) != len(smoothed): app.logger.debug('invalid list len len(gelevs)={} len(anno)={} len(smoothed)={}'.format(len(gelevs), len(anno), len(smoothed))) # construct rows we're going to save to the path sheet pathrows = [] smoothedl = [[e] for e in smoothed] for i in range(len(gelevs)): try: pathrows.append(gelevs[i] + smoothedl[i] + anno[i]) except IndexError: pathrows.append(gelevs[i]) # update sheet with data values self.sheets.spreadsheets().values().batchUpdate(spreadsheetId=fileid, body={ 'valueInputOption' : 'USER_ENTERED', 'data' : [ { 'range' : filename, 'values' : [['content']] + [[r.rstrip()] for r in filecontents]}, { 'range' : 'path', 'values' : [['lat', 'lng', 'orig ele', 'res', 'ele', 'cumdist(km)', 'inserted']] + pathrows }, ] }).execute() return { 'upload' : {'id': fileid }, 'files' : { 'data' : { fileid : {'filename': filename} }, }, # round for user-friendly display 'elev' : int(round(gain)), 'distance': '{:.1f}'.format(distance), 'filename': filename, # start defaults to first point 'location' : ', '.join(['{:.6f}'.format(ll) for ll in locations[0]]) }
def get_altitude(latitude, longitude): maps = Client(key=get_yml('./config.yml')['altitude']['mapstoken']) req = elevation.elevation(maps, (latitude, longitude))[0] altitude = req['elevation'] return altitude
def upload(self): if (debug): print('RunningRoutesFiles.upload()') # save gpx file thisfile = request.files['upload'] gpx_fid, filepath = create_fidfile(g.interest, thisfile.filename, thisfile.mimetype) thisfile.save(filepath) thisfile.seek(0) # process file data; update route with calculated values # open file # process file contents filename = thisfile.filename filetype = filename.split('.')[-1] # latlng processing depends on file type latlng = LatLng(thisfile, filetype) locations = latlng.getpoints() # calculate distance in km distance = 0.0 maxinterval = app.config[ 'APP_MAX_DIST_INTERVAL'] / 1000.0 # convert m to km locs = [locations[0]] # anno is list of [cumdist, inserted], where inserted is empty or 'inserted' anno = [[0, '']] for i in range(1, len(locations)): thisdist = geodist.haversineDistance(locations[i - 1], locations[i], False) eachdist = thisdist # add additional points if the distance between these is longer than allowed if thisdist > maxinterval: numnew = int(thisdist / maxinterval) eachdist = thisdist / (numnew + 1) bearing = calculateBearing(locations[i - 1], locations[i]) lastcoord = locations[i - 1] for j in range(numnew): distance += eachdist anno.append([distance, 'inserted']) # getDestinationLatLng expects distance in meters newcoord = geodist.getDestinationLatLng( lastcoord, bearing, eachdist * 1000) locs.append(newcoord) lastcoord = newcoord distance += eachdist anno.append([distance, '']) locs.append(locations[i]) # convert to miles distance /= 1.609344 # query for elevation points ## slice off locations which meet max sample requirements from google for each query pathlen = MAX_SAMPLES gelevs = [] while len(locs) > 0: if debug: print('len(locs) = {}'.format(len(locs))) theselocs = locs[:pathlen] del locs[:pathlen] ## get this set of elevations elev = elevation(gmapsclient, theselocs) # keys need to match Path fields gelevs += [{ 'lat': p['location']['lat'], 'lng': p['location']['lng'], 'orig_ele': p['elevation'], 'res': p['resolution'] } for p in elev] # calculate elevation gain elevations = numpy.array([float(p['orig_ele']) for p in gelevs]) upthreshold = current_app.config['APP_ELEV_UPTHRESHOLD'] downthreshold = current_app.config['APP_ELEV_DOWNTHRESHOLD'] smoothwin = current_app.config['APP_SMOOTHING_WINDOW'] ## first smooth the elevations using flat window ## see http://scipy-cookbook.readthedocs.io/items/SignalSmooth.html s = numpy.r_[elevations[smoothwin - 1:0:-1], elevations, elevations[-2:-smoothwin - 1:-1]] w = numpy.ones(smoothwin, 'd') y = numpy.convolve(w / w.sum(), s, mode='valid') # use floor division operator // (new in python 3) smoothed = y[(smoothwin // 2):-(smoothwin // 2)] # smoothedl = [[e] for e in smoothed] # reference suggested below to # smoothed = y[(smoothwin/2-1):-(smoothwin/2)] ## calculate the gain using the smoothed elevation profile gain = elevation_gain(smoothed, upthreshold=upthreshold, downthreshold=downthreshold) # combine gelevs with anno if len(gelevs) != len(anno) or len(gelevs) != len(smoothed): app.logger.debug( 'invalid list len len(gelevs)={} len(anno)={} len(smoothed)={}' .format(len(gelevs), len(anno), len(smoothed))) # create csv file with calculated path points # create/write path file # see https://stackoverflow.com/questions/7076042/what-mime-type-should-i-use-for-csv # see https://tools.ietf.org/html/rfc7111 path_fid, filepath = create_fidfile(g.interest, thisfile.filename + '.csv', 'text/csv') with open(filepath, mode='w', newline='') as csvfile: pathfields = 'lat,lng,orig_ele,res,ele,cumdist_km,inserted'.split( ',') pathcsv = DictWriter(csvfile, fieldnames=pathfields) pathcsv.writeheader() for ndx in range(len(gelevs)): # TODO: this is probably a bit klunky from being ported from use of google sheets, clean up try: ele = smoothed[ndx] except IndexError: ele = None try: cumdist_km = anno[ndx][0] inserted = anno[ndx][1] except IndexError: cumdist_km = None inserted = None thispoint = dict( ele=ele, cumdist_km=cumdist_km, inserted=inserted, **gelevs[ndx], ) pathcsv.writerow(thispoint) return { 'upload': { 'id': gpx_fid }, 'files': { 'data': { gpx_fid: { 'filename': thisfile.filename } }, }, 'gpx_file_id': gpx_fid, 'path_file_id': path_fid, # add calculated stuff to route # round for user-friendly display 'elevation_gain': int(round(gain)), 'distance': '{:.1f}'.format(distance), 'start_location': ', '.join(['{:.6f}'.format(ll) for ll in locations[0]]) }