-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·117 lines (94 loc) · 4.22 KB
/
main.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
import os
import copy
from flask import Flask, send_file, Response, jsonify, Blueprint, render_template
from flask_cors import CORS
from dotenv import load_dotenv
from exceptions import errorhandler
from flasgger import Swagger
from flasgger import swag_from
load_dotenv()
flask_app = Flask(__name__)
flask_app.config["JSON_SORT_KEYS"] = False
base_dir = os.path.dirname(os.path.abspath(__file__))
TILE_API = Blueprint("tile_api", __name__)
errorhandler._setup_error_handlers(flask_app)
swagger = Swagger(flask_app)
# handle Cors
CORS(flask_app, resources={
r"/*": {"origins": "*"},
# r"/tile/*": {"origins": "*"},
# r"/tile-async/*": {"origins": "*"},
# r"/tile-async-wms/*": {"origins": "*"},
# r"/bounds/*": {"origins": "*"},
})
@TILE_API.route('/tile/<path:id>/<int:z>/<int:x>/<int:y>.png')
@swag_from('docs/get_tile.yml')
def get_tile(id, z, x, y):
from utils.generate_image import generate_image
optimized_path = os.getenv("OPTIMIZED_PATH")
tiff_files = [f"{optimized_path}{id}_red.tif", f"{optimized_path}{
id}_green.tif", f"{optimized_path}{id}_blue.tif"]
futures = generate_image(tiff_files, id, z, x, y)
image = futures
return send_file(image, mimetype="image/png")
@TILE_API.route('/<path:keys>/<path:TileMatrixSet>/<int:TileMatrix>/<int:TileRow>/<int:TileCol>.png')
@swag_from('docs/get_tile_wmts.yml')
def get_tile_async_wmts(TileMatrix: int, TileRow: int, TileCol: int, keys: str = "", TileMatrixSet: str = "") -> Response:
# print(keys, TileMatrix, TileRow, TileCol, TileMatrixSet)
id = keys
z = TileMatrix
x = TileCol
y = TileRow
from utils.generate_image import generate_image
optimized_path = os.getenv("OPTIMIZED_PATH")
tiff_files = [f"{optimized_path}{id}_red.tif", f"{optimized_path}{
id}_green.tif", f"{optimized_path}{id}_blue.tif"]
futures = generate_image(tiff_files, id, z, x, y)
image = futures
return send_file(image, mimetype="image/png")
@TILE_API.route('/tile-async/<path:keys>/<int:tile_z>/<int:tile_x>/<int:tile_y>.png')
@swag_from('docs/get_tile_async.yml')
def get_tile_async(tile_z: int, tile_y: int, tile_x: int, keys: str = "") -> Response:
tile_xyz = (tile_x, tile_y, tile_z)
from utils.get_rgb_image import _get_rgb_image
return _get_rgb_image(keys, tile_xyz=tile_xyz)
@TILE_API.route('/tile-singleband-async/<path:keys>/<path:band>/<path:colormap>/<int:tile_z>/<int:tile_x>/<int:tile_y>.png')
@swag_from('docs/get_tile_async.yml')
def get_singleband_tile_async(tile_z: int, tile_y: int, tile_x: int, keys: str = "", band: str = "", colormap: str = "") -> Response:
tile_xyz = (tile_x, tile_y, tile_z)
from utils.get_singleband_image import _get_singleband_image
return _get_singleband_image(keys, band, colormap, tile_xyz=tile_xyz)
@TILE_API.route('<path:layer>/ows/')
# @swag_from('docs/get_tile_async.yml')
def get_tile_async_wms(layer: str = '') -> Response:
try:
from utils.createbbox import createbbox
optimized_path = os.getenv("OPTIMIZED_PATH")
tiff_file = f"{optimized_path}{layer}_red.tif"
title = str(optimized_path)
abstract = f"WMTS services provided by B3D for Orthophoto {layer}"
bounds = createbbox(tiff_file)
base_url = os.getenv("BASE_URL")
xml_content = render_template(
'wmts.xml', title=title, abstract=abstract, layer=layer, bounds=bounds, base_url=base_url)
# Return the XML content with the appropriate content type
return Response(xml_content, mimetype='text/xml')
except FileNotFoundError:
return 'WMTS XML file not found', 404
@TILE_API.route('/bounds/<path:id>', methods=["GET"])
@swag_from('docs/get_bounds.yml')
def get_bounds(id):
try:
from utils.createbbox import createbbox
print(id, 'id')
optimized_path = os.getenv("OPTIMIZED_PATH")
tiff_file = f"{optimized_path}/{id}_red.tif"
print(tiff_file, 'tiff_file')
bounds = createbbox(tiff_file)
if bounds:
return jsonify({"bounds": bounds})
except:
return jsonify({"message": "File not found"})
# extensions might modify the global blueprints, so copy before use
new_tile_api = copy.deepcopy(TILE_API)
flask_app.register_blueprint(new_tile_api, url_prefix="/")