-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
476 lines (349 loc) · 14.2 KB
/
app.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
# =================================================================
#
# Authors: Tom Kralidis <[email protected]>
# Norman Barker <[email protected]>
#
# Copyright (c) 2022 Tom Kralidis
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following
# conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
#
# =================================================================
"""Flask module providing the route paths to the api"""
import os
import click
from flask import Flask, Blueprint, make_response, request, send_from_directory
from pygeoapi.api import API
from pygeoapi.util import get_mimetype, yaml_load
CONFIG = None
if 'PYGEOAPI_CONFIG' not in os.environ:
raise RuntimeError('PYGEOAPI_CONFIG environment variable not set')
with open(os.environ.get('PYGEOAPI_CONFIG'), encoding='utf8') as fh:
CONFIG = yaml_load(fh)
STATIC_FOLDER = 'static'
if 'templates' in CONFIG['server']:
STATIC_FOLDER = CONFIG['server']['templates'].get('static', 'static')
APP = Flask(__name__, static_folder=STATIC_FOLDER, static_url_path='/static')
APP.url_map.strict_slashes = False
BLUEPRINT = Blueprint('pygeoapi', __name__, static_folder=STATIC_FOLDER)
# CORS: optionally enable from config.
if CONFIG['server'].get('cors', False):
try:
from flask_cors import CORS
CORS(APP)
except ModuleNotFoundError:
print('Python package flask-cors required for CORS support')
APP.config['JSONIFY_PRETTYPRINT_REGULAR'] = CONFIG['server'].get(
'pretty_print', True)
api_ = API(CONFIG)
OGC_SCHEMAS_LOCATION = CONFIG['server'].get('ogc_schemas_location')
if (OGC_SCHEMAS_LOCATION is not None and
not OGC_SCHEMAS_LOCATION.startswith('http')):
# serve the OGC schemas locally
if not os.path.exists(OGC_SCHEMAS_LOCATION):
raise RuntimeError('OGC schemas misconfigured')
@BLUEPRINT.route('/schemas/<path:path>', methods=['GET'])
def schemas(path):
"""
Serve OGC schemas locally
:param path: path of the OGC schema document
:returns: HTTP response
"""
full_filepath = os.path.join(OGC_SCHEMAS_LOCATION, path)
dirname_ = os.path.dirname(full_filepath)
basename_ = os.path.basename(full_filepath)
# TODO: better sanitization?
path_ = dirname_.replace('..', '').replace('//', '')
return send_from_directory(path_, basename_,
mimetype=get_mimetype(basename_))
def get_response(result: tuple):
"""
Creates a Flask Response object and updates matching headers.
:param result: The result of the API call.
This should be a tuple of (headers, status, content).
:returns: A Response instance.
"""
headers, status, content = result
response = make_response(content, status)
if headers:
response.headers = headers
return response
@BLUEPRINT.route('/')
def landing_page():
"""
OGC API landing page endpoint
:returns: HTTP response
"""
return get_response(api_.landing_page(request))
@BLUEPRINT.route('/openapi')
def openapi():
"""
OpenAPI endpoint
:returns: HTTP response
"""
with open(os.environ.get('PYGEOAPI_OPENAPI'), encoding='utf8') as ff:
if os.environ.get('PYGEOAPI_OPENAPI').endswith(('.yaml', '.yml')):
openapi_ = yaml_load(ff)
else: # JSON string, do not transform
openapi_ = ff.read()
return get_response(api_.openapi(request, openapi_))
@BLUEPRINT.route('/conformance')
def conformance():
"""
OGC API conformance endpoint
:returns: HTTP response
"""
return get_response(api_.conformance(request))
@BLUEPRINT.route('/collections')
@BLUEPRINT.route('/collections/<path:collection_id>')
def collections(collection_id=None):
"""
OGC API collections endpoint
:param collection_id: collection identifier
:returns: HTTP response
"""
return get_response(api_.describe_collections(request, collection_id))
@BLUEPRINT.route('/collections/<path:collection_id>/queryables')
def collection_queryables(collection_id=None):
"""
OGC API collections querybles endpoint
:param collection_id: collection identifier
:returns: HTTP response
"""
return get_response(api_.get_collection_queryables(request, collection_id))
@BLUEPRINT.route('/collections/<path:collection_id>/items',
methods=['GET', 'POST'])
@BLUEPRINT.route('/collections/<path:collection_id>/items/<path:item_id>',
methods=['GET', 'PUT', 'DELETE'])
def collection_items(collection_id, item_id=None):
"""
OGC API collections items endpoint
:param collection_id: collection identifier
:param item_id: item identifier
:returns: HTTP response
"""
if item_id is None:
if request.method == 'GET': # list items
return get_response(
api_.get_collection_items(request, collection_id))
elif request.method == 'POST': # filter or manage items
if request.content_type is not None:
if request.content_type == 'application/geo+json':
return get_response(
api_.manage_collection_item(request, 'create',
collection_id))
else:
return get_response(
api_.post_collection_items(request, collection_id))
elif request.method == 'DELETE':
return get_response(
api_.manage_collection_item(request, 'delete',
collection_id, item_id))
elif request.method == 'PUT':
return get_response(
api_.manage_collection_item(request, 'update',
collection_id, item_id))
else:
return get_response(
api_.get_collection_item(request, collection_id, item_id))
@BLUEPRINT.route('/collections/<path:collection_id>/coverage')
def collection_coverage(collection_id):
"""
OGC API - Coverages coverage endpoint
:param collection_id: collection identifier
:returns: HTTP response
"""
return get_response(api_.get_collection_coverage(request, collection_id))
@BLUEPRINT.route('/collections/<path:collection_id>/coverage/domainset')
def collection_coverage_domainset(collection_id):
"""
OGC API - Coverages coverage domainset endpoint
:param collection_id: collection identifier
:returns: HTTP response
"""
return get_response(api_.get_collection_coverage_domainset(
request, collection_id))
@BLUEPRINT.route('/collections/<path:collection_id>/coverage/rangetype')
def collection_coverage_rangetype(collection_id):
"""
OGC API - Coverages coverage rangetype endpoint
:param collection_id: collection identifier
:returns: HTTP response
"""
return get_response(api_.get_collection_coverage_rangetype(
request, collection_id))
@BLUEPRINT.route('/collections/<path:collection_id>/tiles')
def get_collection_tiles(collection_id=None):
"""
OGC open api collections tiles access point
:param collection_id: collection identifier
:returns: HTTP response
"""
return get_response(api_.get_collection_tiles(
request, collection_id))
@BLUEPRINT.route('/collections/<path:collection_id>/tiles/<tileMatrixSetId>')
@BLUEPRINT.route('/collections/<path:collection_id>/tiles/<tileMatrixSetId>/metadata') # noqa
def get_collection_tiles_metadata(collection_id=None, tileMatrixSetId=None):
"""
OGC open api collection tiles service metadata
:param collection_id: collection identifier
:param tileMatrixSetId: identifier of tile matrix set
:returns: HTTP response
"""
return get_response(api_.get_collection_tiles_metadata(
request, collection_id, tileMatrixSetId))
@BLUEPRINT.route('/collections/<path:collection_id>/tiles/\
<tileMatrixSetId>/<tileMatrix>/<tileRow>/<tileCol>')
def get_collection_tiles_data(collection_id=None, tileMatrixSetId=None,
tileMatrix=None, tileRow=None, tileCol=None):
"""
OGC open api collection tiles service data
:param collection_id: collection identifier
:param tileMatrixSetId: identifier of tile matrix set
:param tileMatrix: identifier of {z} matrix index
:param tileRow: identifier of {y} matrix index
:param tileCol: identifier of {x} matrix index
:returns: HTTP response
"""
return get_response(api_.get_collection_tiles_data(
request, collection_id, tileMatrixSetId, tileMatrix, tileRow, tileCol))
@BLUEPRINT.route('/collections/<collection_id>/map')
@BLUEPRINT.route('/collections/<collection_id>/styles/<style_id>/map')
def collection_map(collection_id, style_id=None):
"""
OGC API - Maps map render endpoint
:param collection_id: collection identifier
:param style_id: style identifier
:returns: HTTP response
"""
headers, status_code, content = api_.get_collection_map(
request, collection_id, style_id)
response = make_response(content, status_code)
if headers:
response.headers = headers
return response
@BLUEPRINT.route('/processes')
@BLUEPRINT.route('/processes/<path:process_id>')
def get_processes(process_id=None):
"""
OGC API - Processes description endpoint
:param process_id: process identifier
:returns: HTTP response
"""
return get_response(api_.describe_processes(request, process_id))
@BLUEPRINT.route('/jobs')
@BLUEPRINT.route('/jobs/<job_id>',
methods=['GET', 'DELETE'])
def get_jobs(job_id=None):
"""
OGC API - Processes jobs endpoint
:param job_id: job identifier
:returns: HTTP response
"""
if job_id is None:
return get_response(api_.get_jobs(request))
else:
if request.method == 'DELETE': # dismiss job
return get_response(api_.delete_job(job_id))
else: # Return status of a specific job
return get_response(api_.get_jobs(request, job_id))
@BLUEPRINT.route('/processes/<path:process_id>/execution', methods=['POST'])
def execute_process_jobs(process_id):
"""
OGC API - Processes execution endpoint
:param process_id: process identifier
:returns: HTTP response
"""
return get_response(api_.execute_process(request, process_id))
@BLUEPRINT.route('/jobs/<job_id>/results',
methods=['GET'])
def get_job_result(job_id=None):
"""
OGC API - Processes job result endpoint
:param job_id: job identifier
:returns: HTTP response
"""
return get_response(api_.get_job_result(request, job_id))
@BLUEPRINT.route('/jobs/<job_id>/results/<resource>',
methods=['GET'])
def get_job_result_resource(job_id, resource):
"""
OGC API - Processes job result resource endpoint
:param job_id: job identifier
:param resource: job resource
:returns: HTTP response
"""
return get_response(api_.get_job_result_resource(
request, job_id, resource))
@BLUEPRINT.route('/collections/<path:collection_id>/position')
@BLUEPRINT.route('/collections/<path:collection_id>/area')
@BLUEPRINT.route('/collections/<path:collection_id>/cube')
@BLUEPRINT.route('/collections/<path:collection_id>/radius')
@BLUEPRINT.route('/collections/<path:collection_id>/trajectory')
@BLUEPRINT.route('/collections/<path:collection_id>/corridor')
@BLUEPRINT.route('/collections/<path:collection_id>/instances/<instance_id>/position') # noqa
@BLUEPRINT.route('/collections/<path:collection_id>/instances/<instance_id>/area') # noqa
@BLUEPRINT.route('/collections/<path:collection_id>/instances/<instance_id>/cube') # noqa
@BLUEPRINT.route('/collections/<path:collection_id>/instances/<instance_id>/radius') # noqa
@BLUEPRINT.route('/collections/<path:collection_id>/instances/<instance_id>/trajectory') # noqa
@BLUEPRINT.route('/collections/<path:collection_id>/instances/<instance_id>/corridor') # noqa
def get_collection_edr_query(collection_id, instance_id=None):
"""
OGC EDR API endpoints
:param collection_id: collection identifier
:param instance_id: instance identifier
:returns: HTTP response
"""
query_type = request.path.split('/')[-1]
return get_response(api_.get_collection_edr_query(request, collection_id,
instance_id, query_type))
@BLUEPRINT.route('/stac')
def stac_catalog_root():
"""
STAC root endpoint
:returns: HTTP response
"""
return get_response(api_.get_stac_root(request))
@BLUEPRINT.route('/stac/<path:path>')
def stac_catalog_path(path):
"""
STAC path endpoint
:param path: path
:returns: HTTP response
"""
return get_response(api_.get_stac_path(request, path))
APP.register_blueprint(BLUEPRINT)
@click.command()
@click.pass_context
@click.option('--debug', '-d', default=False, is_flag=True, help='debug')
def serve(ctx, server=None, debug=False):
"""
Serve pygeoapi via Flask. Runs pygeoapi
as a flask server. Not recommend for production.
:param server: `string` of server type
:param debug: `bool` of whether to run in debug mode
:returns: void
"""
# setup_logger(CONFIG['logging'])
APP.run(debug=True, host=api_.config['server']['bind']['host'],
port=api_.config['server']['bind']['port'])
if __name__ == '__main__': # run locally, for testing
serve()