From 3f35ce1c2c2c978e2a015e59add2eb1e9b594f17 Mon Sep 17 00:00:00 2001 From: Guille Date: Fri, 26 Apr 2024 07:14:29 +0200 Subject: [PATCH] partial implementation meb workflow in the api --- .../workflow/insel_montly_energy_balance.py | 22 ++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/hub_api/workflow/insel_montly_energy_balance.py b/hub_api/workflow/insel_montly_energy_balance.py index 8a49af1..fb01a21 100644 --- a/hub_api/workflow/insel_montly_energy_balance.py +++ b/hub_api/workflow/insel_montly_energy_balance.py @@ -1,7 +1,11 @@ import json +import os from pathlib import Path from flask_restful import Resource from flask import Response, request +from hub.helpers.data.montreal_function_to_hub_function import MontrealFunctionToHubFunction +from hub.imports.geometry_factory import GeometryFactory +from werkzeug.utils import secure_filename from hub_api.config import Config from hub_api.helpers.session_helper import refresh_session @@ -11,12 +15,23 @@ class InselMonthlyEnergyBalance(Resource, Config): def __init__(self): super().__init__() self._extensions = ['geojson', 'citygml'] + self._tmp_path = (Path(__file__).parent / 'tmp').resolve() def _allowed_extensions(self, filename): self._file_extension = Path(filename).suffix return self._file_extension in self._extensions - def _geojson(self, geojson_file): + def _geojson(self, file_path): + height_field = request.args.get('height_field') + year_of_construction_field = request.args.get('year_of_construction_field') + function_field = request.args.get('function_field') + function_dictionary_name = request.args.get('function_dictionary_name') + city = GeometryFactory('geojson', + path=file_path, + height_field=height_field, + year_of_construction_field=year_of_construction_field, + function_field=function_field, + function_to_hub=MontrealFunctionToHubFunction().dictionary).city return def post(self): @@ -33,4 +48,9 @@ class InselMonthlyEnergyBalance(Resource, Config): geometry_file = request.files['geometry_file'] if not self._allowed_extensions(geometry_file): return Response(json.dumps({'error': 'Unsupported media type'}), status=415, headers=token) + filename = secure_filename(geometry_file.filename) + file_path = os.path.join(self._tmp_path, filename) + geometry_file.save(file_path) + if self._file_extension == 'geojson': + self._geojson(file_path)