Last active
December 12, 2015 01:28
-
-
Save alexprengere/4690966 to your computer and use it in GitHub Desktop.
Webservices over GeoBases with Flask
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
#!/usr/bin/python | |
# -*- coding: utf-8 -*- | |
""" | |
Webservice over GeoBases using Flask. | |
""" | |
# Flask is a microframework web | |
from flask import Flask, request, jsonify | |
app = Flask(__name__) | |
app.secret_key = 'super_secret_app_key_#$#$#$' | |
# Importing GeoBases | |
from GeoBases import GeoBase | |
BASE = GeoBase('ori_por', verbose=False) | |
@app.route('/get/<key>', methods=['GET']) | |
def get(key): | |
"""Get service. | |
""" | |
try: | |
res = BASE.get(key) | |
except KeyError: | |
return jsonify({'error' : 'Key not found'}) | |
else: | |
return jsonify(res) | |
@app.route('/findNearPoint', methods=['GET']) | |
def findNearPoint(): | |
"""findNearPoint service. | |
""" | |
radius = request.args.get('radius', 50) | |
radius = 50 if not radius else float(radius) | |
location = float(request.args.get('lat')), float(request.args.get('lng')) | |
return jsonify({ | |
'results' : sorted(BASE.findNearPoint(location, radius)) | |
}) | |
if __name__ == '__main__': | |
# Debug mode, do not use in production | |
app.run(host='0.0.0.0', debug=True, port=5000) | |
# Check the results by looking at these urls | |
#http://localhost:5000/findNearPoint?lat=48.7&lng=2.35&radius=100 | |
#http://localhost:5000/get/ORY | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment