Skip to content

Commit

Permalink
Merge pull request #23 from beveradb/city-choice-tool
Browse files Browse the repository at this point in the history
City choice tool
  • Loading branch information
beveradb authored Nov 24, 2019
2 parents 494cae7 + 7a624cb commit dd23e7a
Show file tree
Hide file tree
Showing 14 changed files with 779 additions and 184 deletions.
Binary file not shown.
Binary file not shown.
4 changes: 3 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@ backoff
flask_sslify
pyunpack
patool
googlemaps
googlemaps
pandas
xlrd
56 changes: 51 additions & 5 deletions run_server.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env python3
import json
import logging
import os

Expand All @@ -10,7 +11,7 @@
from flask import Flask, render_template, Response, request
from flask_sslify import SSLify

from src import target_area, utils
from src import utils
from src.utils import preload_files

app = Flask(__name__)
Expand All @@ -22,8 +23,10 @@
logging.getLogger().setLevel(logging.DEBUG if app_debug else logging.INFO)

# Download dataset files and pre-seeded API call / compute cache to reduce slug size
preload_files('https://github.com/beveradb/home-area-helper/releases/download/v0.6/', [
preload_files('https://github.com/beveradb/home-area-helper/releases/download/v0.7/', [
{'dir': 'datasets/uk/', 'file': 'uk-wgs84-imd-shapefiles.zip'},
{'dir': 'datasets/europe/', 'file': 'eurostat-cities-2019.zip'},
{'dir': 'caches/', 'file': 'api_cache.sqlite.zip'},
{'dir': 'caches/', 'file': 'requests_cache.sqlite.zip'},
{'dir': 'caches/', 'file': 'static_cache.sqlite'},
])
Expand All @@ -32,6 +35,10 @@
requests_cache = requests_cache.core.CachedSession(
cache_name='caches/requests_cache', backend="sqlite", allowable_methods=('GET', 'POST'))

# Set up disk caching for API calls which are made through a 3rd party library rather than the requests library
api_cache = ucache.SqliteCache(
filename='caches/api_cache.sqlite', cache_size=5000, timeout=32000000, compression=True)

# Set up disk caching for complex computations which should not need to change -
static_cache = ucache.SqliteCache(
filename='caches/static_cache.sqlite', cache_size=5000, timeout=32000000, compression=True)
Expand All @@ -43,23 +50,62 @@


@app.route('/')
def index():
def target_area_request():
# This script requires you define environment variables with your personal API keys:
# MAPBOX_ACCESS_TOKEN from https://docs.mapbox.com/help/how-mapbox-works/access-tokens/
# TRAVELTIME_APP_ID from https://docs.traveltimeplatform.com/overview/introduction
# TRAVELTIME_API_KEY from https://docs.traveltimeplatform.com/overview/introduction

return render_template(
'index.html',
'target_area.html',
MAPBOX_ACCESS_TOKEN=os.environ['MAPBOX_ACCESS_TOKEN']
)


@app.route('/target-cities')
def target_cities_request():
return render_template(
'target_cities.html',
MAPBOX_ACCESS_TOKEN=os.environ['MAPBOX_ACCESS_TOKEN']
)


@app.route('/eurostat_countries')
def eurostat_country_codes():
from src import target_cities
results = json.dumps(target_cities.get_eurostat_countries())
return Response(results, mimetype='application/json')


@app.route('/target_cities', methods=['POST'])
def target_cities_json():
req_data = request.get_json()
logging.log(logging.INFO, "Target cities request received: " + str(req_data))

from src import target_cities
results = target_cities.get_target_cities_data_json(req_data)

utils.log_method_timings()

return Response(results, mimetype='application/json')


@app.route('/eurostat_testing/<string:country>', methods=['GET'])
def eurostat_testing(country):
logging.log(logging.INFO, "Target eurostat received: " + str(country))

from src import target_cities
results = target_cities.get_country_cities_combined_data(country)

return Response(results, mimetype='application/json')


@app.route('/target_area', methods=['POST'])
def target_area_json():
req_data = request.get_json()
logging.log(logging.INFO, "Request received: " + str(req_data))
logging.log(logging.INFO, "Target area request received: " + str(req_data))

from src import target_area
results = target_area.get_target_areas_polygons_json(req_data)

utils.log_method_timings()
Expand Down
13 changes: 7 additions & 6 deletions src/google_maps.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,19 @@

import googlemaps

from run_server import transient_cache
from src.utils import timeit


@timeit
@transient_cache.cached()
def get_centre_point_lng_lat_for_address(address_string):
gmaps = googlemaps.Client(key=os.environ['GMAPS_API_KEY'])

geocode_result = gmaps.geocode(address_string)

return [
geocode_result[0]['geometry']['location']['lng'],
geocode_result[0]['geometry']['location']['lat']
]
if len(geocode_result) > 0:
return [
geocode_result[0]['geometry']['location']['lng'],
geocode_result[0]['geometry']['location']['lat']
]
else:
return None
2 changes: 2 additions & 0 deletions src/target_area.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ def get_target_area_polygons(
result_polygons_length = 1 if not hasattr(result_polygons, 'geoms') else len(result_polygons.geoms)
logging.info("Total result_polygons after transport min area filter: " + str(result_polygons_length))
else:
if fallback_radius_miles == 0:
fallback_radius_miles = 1
result_intersection = get_bounding_circle_for_point(target_lng_lat, fallback_radius_miles)

if result_polygons_length > 0:
Expand Down
Loading

0 comments on commit dd23e7a

Please sign in to comment.