diff --git a/hub_api/config.py b/hub_api/config.py index 33c1ac7..79e526f 100644 --- a/hub_api/config.py +++ b/hub_api/config.py @@ -3,10 +3,12 @@ Config SPDX - License - Identifier: LGPL - 3.0 - or -later Copyright © 2023 Project Peter Yefi peteryefi@gmail.com """ +import distutils import os import platform from pathlib import Path +import hub.helpers.dictionaries from hub.persistence.db_control import DBControl from hub.persistence.repository import Repository 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._repository = Repository(db_name=database_name, app_env=environment, dotenv_path=dotenv_path) 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 def database(self): @@ -42,3 +51,14 @@ class Config: @property def max_file_size(self): 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') diff --git a/hub_api/workflow/insel_montly_energy_balance.py b/hub_api/workflow/insel_montly_energy_balance.py index fb01a21..5dd48dd 100644 --- a/hub_api/workflow/insel_montly_energy_balance.py +++ b/hub_api/workflow/insel_montly_energy_balance.py @@ -1,10 +1,16 @@ import json import os +import subprocess from pathlib import Path from flask_restful import Resource 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.imports.construction_factory import ConstructionFactory 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 hub_api.config import Config @@ -16,23 +22,25 @@ class InselMonthlyEnergyBalance(Resource, Config): super().__init__() self._extensions = ['geojson', 'citygml'] self._tmp_path = (Path(__file__).parent / 'tmp').resolve() + self._city = None def _allowed_extensions(self, filename): self._file_extension = Path(filename).suffix return self._file_extension in self._extensions - def _geojson(self, file_path): + @staticmethod + def _geojson(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', + function_dictionary = Config.function_dictionary(request.args.get('function_dictionary_name')) + + return 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 + function_to_hub=function_dictionary).city def post(self): """ @@ -52,5 +60,13 @@ class InselMonthlyEnergyBalance(Resource, Config): file_path = os.path.join(self._tmp_path, filename) geometry_file.save(file_path) 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()