forked from openshift-roadshow/nationalparks-py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wsgi.py
121 lines (82 loc) · 2.96 KB
/
wsgi.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
from __future__ import print_function
import os
import json
from flask import Flask, request
from flask_restful import Resource, Api
from pymongo import MongoClient, GEO2D
DB_HOST = os.environ.get('DB_HOST', 'mongodb')
DB_NAME = os.environ.get('DB_NAME', 'mongodb')
DB_USERNAME = os.environ.get('DB_USERNAME', 'mongodb')
DB_PASSWORD = os.environ.get('DB_PASSWORD', 'mongodb')
DB_URI = 'mongodb://%s:%s@%s:27017/%s' % (DB_USERNAME, DB_PASSWORD,
DB_HOST, DB_NAME)
DATASET_FILE = 'nationalparks.json'
application = Flask(__name__)
api = Api(application)
class HealthCheck(Resource):
def get(self):
return 'OK'
api.add_resource(HealthCheck, '/ws/healthz/')
class Info(Resource):
description = {
'id': 'nationalparks-py',
'displayName': 'National Parks 2 (Python)',
'type': 'cluster',
'center': {'latitude': '47.039304', 'longitude': '14.505178'},
'zoom': 4
}
def get(self):
return self.description
api.add_resource(Info, '/ws/info/')
class DataLoad(Resource):
def get(self):
client = MongoClient(DB_URI)
database = client[DB_NAME]
collection = database.nationalparks
collection.remove({})
collection.create_index([('Location', GEO2D)])
with open(DATASET_FILE, 'r') as fp:
entries = []
for data in fp.readlines():
entry = json.loads(data)
loc = [entry['coordinates'][1], entry['coordinates'][0]]
entry['Location'] = loc
entries.append(entry)
if len(entries) >= 1000:
collection.insert_many(entries)
entries = []
if entries:
collection.insert_many(entries)
return 'Inserted %s items.' % collection.count()
api.add_resource(DataLoad, '/ws/data/load')
def format_result(entries):
result = []
for entry in entries:
data = {}
data['id'] = entry['name']
data['latitude'] = str(entry['coordinates'][0])
data['longitude'] = str(entry['coordinates'][1])
data['name'] = entry['toponymName']
result.append(data)
return result
class DataAll(Resource):
def get(self):
client = MongoClient(DB_URI)
database = client[DB_NAME]
collection = database.nationalparks
return format_result(collection.find())
api.add_resource(DataAll, '/ws/data/all')
class DataWithin(Resource):
def get(self):
args = request.args
box = [[float(args['lon1']), float(args['lat1'])],
[float(args['lon2']), float(args['lat2'])]]
query = {'Location': {'$within': {'$box': box}}}
client = MongoClient(DB_URI)
database = client[DB_NAME]
collection = database.nationalparks
return format_result(collection.find(query))
api.add_resource(DataWithin, '/ws/data/within')
@application.route('/')
def index():
return 'Welcome to the National Parks data service.'