-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Altitude * generating random altitude * Travis * Update adjust proper range * Update for Spawn Point Scanning * Update Elevation Request * Attempt to receive altitude via Elevation * Updated utils * Altitude for SpeedScan * Extract repeated code into get_altitude * Make altitude range param conform with the other params * Store location altitudes in database * Rename altitude range to altitude variance * Try to keep unrelated things as they were originally * Log error whe get_gmaps_altitude fails * Replace repeated code in runserver * Get nearby altitudes * Improve args help * Add option to not cache altitudes * Change default altitude Default based on the average elevation of cities around the world. Source: https://www.wikiwand.com/en/List_of_cities_by_elevation * Fix grammar * Move altitude stuff to its own file * Rename no altitude cache argument * Stop the map if elevation is not set in GMaps API * Good bye double quotes * Fix wrong call, should be no_altitude_cache * Update documentation with Elevation API info
- Loading branch information
1 parent
1abaf68
commit 7ca5e93
Showing
6 changed files
with
182 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
#!/usr/bin/python | ||
# -*- coding: utf-8 -*- | ||
|
||
import logging | ||
import requests | ||
import random | ||
from .models import LocationAltitude | ||
|
||
log = logging.getLogger(__name__) | ||
|
||
# Altitude used when no_altitude_cache is enabled | ||
fallback_altitude = None | ||
|
||
|
||
def get_gmaps_altitude(lat, lng, gmaps_key): | ||
try: | ||
r_session = requests.Session() | ||
response = r_session.get(( | ||
'https://maps.googleapis.com/maps/api/elevation/json?' + | ||
'locations={},{}&key={}').format(lat, lng, gmaps_key)) | ||
response = response.json() | ||
status = response['status'] | ||
results = response.get('results', []) | ||
result = results[0] if results else {} | ||
altitude = result.get('elevation', None) | ||
except: | ||
log.error('Unable to retrieve altitude from Google APIs.') | ||
status = 'UNKNOWN_ERROR' | ||
altitude = None | ||
|
||
return (altitude, status) | ||
|
||
|
||
def randomize_altitude(altitude, altitude_variance): | ||
if altitude_variance > 0: | ||
altitude = (altitude + | ||
random.randrange(-1 * altitude_variance, | ||
altitude_variance) + | ||
float(format(random.random(), '.13f'))) | ||
else: | ||
altitude = altitude + float(format(random.random(), '.13f')) | ||
|
||
return altitude | ||
|
||
|
||
# Only once fetched altitude | ||
def get_fallback_altitude(args, loc): | ||
global fallback_altitude | ||
|
||
if fallback_altitude is None: | ||
(fallback_altitude, status) = get_gmaps_altitude(loc[0], loc[1], | ||
args.gmaps_key) | ||
|
||
return fallback_altitude | ||
|
||
|
||
# Get altitude from the db or try to fetch from gmaps api, | ||
# otherwise, default altitude | ||
def cached_get_altitude(args, loc): | ||
altitude = LocationAltitude.get_nearby_altitude(loc) | ||
|
||
if altitude is None: | ||
(altitude, status) = get_gmaps_altitude(loc[0], loc[1], args.gmaps_key) | ||
if altitude is not None: | ||
LocationAltitude.save_altitude(loc, altitude) | ||
|
||
return altitude | ||
|
||
|
||
# Get altitude main method | ||
def get_altitude(args, loc): | ||
if args.no_altitude_cache: | ||
altitude = get_fallback_altitude(args, loc) | ||
else: | ||
altitude = cached_get_altitude(args, loc) | ||
|
||
if altitude is None: | ||
altitude = args.altitude | ||
|
||
return randomize_altitude(altitude, args.altitude_variance) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters