partial implementation meb workflow in the api

This commit is contained in:
Guille Gutierrez 2024-04-26 16:34:38 +02:00
parent 3f35ce1c2c
commit c414de2635
2 changed files with 43 additions and 7 deletions

View File

@ -3,10 +3,12 @@ Config
SPDX - License - Identifier: LGPL - 3.0 - or -later SPDX - License - Identifier: LGPL - 3.0 - or -later
Copyright © 2023 Project Peter Yefi peteryefi@gmail.com Copyright © 2023 Project Peter Yefi peteryefi@gmail.com
""" """
import distutils
import os import os
import platform import platform
from pathlib import Path from pathlib import Path
import hub.helpers.dictionaries
from hub.persistence.db_control import DBControl from hub.persistence.db_control import DBControl
from hub.persistence.repository import Repository from hub.persistence.repository import Repository
from hub.catalog_factories.energy_systems_catalog_factory import EnergySystemsCatalogFactory from hub.catalog_factories.energy_systems_catalog_factory import EnergySystemsCatalogFactory
@ -26,6 +28,13 @@ class Config:
self._database = DBControl(db_name=database_name, app_env=environment, dotenv_path=dotenv_path) self._database = DBControl(db_name=database_name, app_env=environment, dotenv_path=dotenv_path)
self._repository = Repository(db_name=database_name, app_env=environment, dotenv_path=dotenv_path) self._repository = Repository(db_name=database_name, app_env=environment, dotenv_path=dotenv_path)
self._energy_systems_catalog = EnergySystemsCatalogFactory('montreal_custom').catalog self._energy_systems_catalog = EnergySystemsCatalogFactory('montreal_custom').catalog
self._dictionaries = {
'pluto': hub.helpers.dictionaries.Dictionaries.pluto_function_to_hub_function,
'htf': hub.helpers.dictionaries.Dictionaries.hft_function_to_hub_function,
'montreal': hub.helpers.dictionaries.Dictionaries.montreal_function_to_hub_function,
'alkis': hub.helpers.dictionaries.Dictionaries.alkis_function_to_hub_function,
'eilat': hub.helpers.dictionaries.Dictionaries.eilat_function_to_hub_function
}
@property @property
def database(self): def database(self):
@ -42,3 +51,14 @@ class Config:
@property @property
def max_file_size(self): def max_file_size(self):
return self._max_file_size return self._max_file_size
def function_dictionary(self, dictionary):
return self._dictionaries[dictionary]
@property
def insel(self):
return distutils.spawn.find_executable('insel')
@property
def sra(self):
return distutils.spawn.find_executable('sra')

View File

@ -1,10 +1,16 @@
import json import json
import os import os
import subprocess
from pathlib import Path from pathlib import Path
from flask_restful import Resource from flask_restful import Resource
from flask import Response, request from flask import Response, request
from hub.exports.exports_factory import ExportsFactory
from hub.helpers.data.montreal_function_to_hub_function import MontrealFunctionToHubFunction from hub.helpers.data.montreal_function_to_hub_function import MontrealFunctionToHubFunction
from hub.imports.construction_factory import ConstructionFactory
from hub.imports.geometry_factory import GeometryFactory from hub.imports.geometry_factory import GeometryFactory
from hub.imports.results_factory import ResultFactory
from hub.imports.usage_factory import UsageFactory
from hub.imports.weather_factory import WeatherFactory
from werkzeug.utils import secure_filename from werkzeug.utils import secure_filename
from hub_api.config import Config from hub_api.config import Config
@ -16,23 +22,25 @@ class InselMonthlyEnergyBalance(Resource, Config):
super().__init__() super().__init__()
self._extensions = ['geojson', 'citygml'] self._extensions = ['geojson', 'citygml']
self._tmp_path = (Path(__file__).parent / 'tmp').resolve() self._tmp_path = (Path(__file__).parent / 'tmp').resolve()
self._city = None
def _allowed_extensions(self, filename): def _allowed_extensions(self, filename):
self._file_extension = Path(filename).suffix self._file_extension = Path(filename).suffix
return self._file_extension in self._extensions return self._file_extension in self._extensions
def _geojson(self, file_path): @staticmethod
def _geojson(file_path):
height_field = request.args.get('height_field') height_field = request.args.get('height_field')
year_of_construction_field = request.args.get('year_of_construction_field') year_of_construction_field = request.args.get('year_of_construction_field')
function_field = request.args.get('function_field') function_field = request.args.get('function_field')
function_dictionary_name = request.args.get('function_dictionary_name') function_dictionary = Config.function_dictionary(request.args.get('function_dictionary_name'))
city = GeometryFactory('geojson',
return GeometryFactory('geojson',
path=file_path, path=file_path,
height_field=height_field, height_field=height_field,
year_of_construction_field=year_of_construction_field, year_of_construction_field=year_of_construction_field,
function_field=function_field, function_field=function_field,
function_to_hub=MontrealFunctionToHubFunction().dictionary).city function_to_hub=function_dictionary).city
return
def post(self): def post(self):
""" """
@ -52,5 +60,13 @@ class InselMonthlyEnergyBalance(Resource, Config):
file_path = os.path.join(self._tmp_path, filename) file_path = os.path.join(self._tmp_path, filename)
geometry_file.save(file_path) geometry_file.save(file_path)
if self._file_extension == 'geojson': if self._file_extension == 'geojson':
self._geojson(file_path) self._city = InselMonthlyEnergyBalance._geojson(file_path)
construction_handler = request.args.get('construction_handler')
usage_handler = request.args.get('usage_handler')
WeatherFactory('epw', self._city).enrich()
ConstructionFactory(construction_handler, self._city).enrich()
UsageFactory(usage_handler, self._city).enrich()
ExportsFactory('sra', self._city, self._tmp_path).export()
sra_file = str((self._tmp_path / f'{self._city.name}_sra.xml').resolve())
subprocess.run([self.sra, sra_file], stdout=subprocess.DEVNULL)
ResultFactory('sra', self._city, self._tmp_path).enrich()