2023-01-10 12:12:01 -05:00
|
|
|
"""
|
|
|
|
Greenery
|
|
|
|
SPDX - License - Identifier: LGPL - 3.0 - or -later
|
|
|
|
Copyright © 2022 Pilar Monsalvete Alvarez de Uribarri pilar.monsalvete@concordia.ca
|
2023-01-17 19:00:22 -05:00
|
|
|
Code contributors: Peter Yefi peteryefi@gmail.com
|
2023-01-10 12:12:01 -05:00
|
|
|
"""
|
|
|
|
|
|
|
|
import json
|
|
|
|
|
|
|
|
from flask import Response, request
|
|
|
|
from flask_restful import Resource
|
2023-01-17 19:00:22 -05:00
|
|
|
from hub_api.config import Config
|
2023-01-10 12:12:01 -05:00
|
|
|
from city_model_structure.greenery.vegetation import Vegetation
|
|
|
|
from city_model_structure.greenery.soil import Soil
|
|
|
|
from city_model_structure.greenery.plant import Plant
|
|
|
|
import helpers.constants as cte
|
2023-01-17 19:00:22 -05:00
|
|
|
from persistence.models import UserRoles
|
|
|
|
from hub_api.helpers.auth import role_required
|
2023-01-10 12:12:01 -05:00
|
|
|
|
|
|
|
|
2023-01-17 19:00:22 -05:00
|
|
|
class Greenery(Resource, Config):
|
2023-01-10 12:12:01 -05:00
|
|
|
def __init__(self):
|
2023-01-17 19:00:22 -05:00
|
|
|
super().__init__()
|
|
|
|
|
|
|
|
@role_required([UserRoles.Admin.value])
|
|
|
|
def put(self, city_id):
|
2023-01-10 12:12:01 -05:00
|
|
|
|
2023-01-17 19:00:22 -05:00
|
|
|
city = self.get_city(city_id)
|
2023-01-10 12:12:01 -05:00
|
|
|
try:
|
2023-01-17 19:00:22 -05:00
|
|
|
greenery_percentage = request.json['greenery_percentage']
|
|
|
|
if greenery_percentage == 0:
|
2023-01-10 12:12:01 -05:00
|
|
|
response = {'result': 'succeed'}
|
2023-01-17 19:00:22 -05:00
|
|
|
return Response(json.dumps(response), status=200)
|
2023-01-10 12:12:01 -05:00
|
|
|
|
|
|
|
building_names = request.json['building_names']
|
|
|
|
vegetation_requested = request.json['vegetation']
|
|
|
|
vegetation_name = vegetation_requested['vegetation_name']
|
|
|
|
soil_thickness = vegetation_requested['soil_thickness']
|
|
|
|
soil_name = vegetation_requested['soil_name']
|
|
|
|
roughness = vegetation_requested['soil_roughness']
|
|
|
|
dry_conductivity = vegetation_requested['dry_soil_conductivity']
|
|
|
|
dry_density = vegetation_requested['dry_soil_density']
|
|
|
|
dry_specific_heat = vegetation_requested['dry_soil_specific_heat']
|
|
|
|
thermal_absorptance = vegetation_requested['soil_thermal_absorptance']
|
|
|
|
solar_absorptance = vegetation_requested['soil_solar_absorptance']
|
|
|
|
visible_absorptance = vegetation_requested['soil_visible_absorptance']
|
|
|
|
saturation_volumetric_moisture_content = vegetation_requested['soil_saturation_volumetric_moisture_content']
|
|
|
|
residual_volumetric_moisture_content = vegetation_requested['soil_residual_volumetric_moisture_content']
|
|
|
|
soil = Soil(soil_name, roughness, dry_conductivity, dry_density, dry_specific_heat, thermal_absorptance,
|
|
|
|
solar_absorptance, visible_absorptance, saturation_volumetric_moisture_content,
|
|
|
|
residual_volumetric_moisture_content)
|
|
|
|
soil.initial_volumetric_moisture_content = '0.1'
|
|
|
|
plant_percentages = vegetation_requested['plant_percentages']
|
|
|
|
plants = []
|
|
|
|
for plant_percentage in plant_percentages:
|
|
|
|
plant_name = plant_percentage['plant_name']
|
|
|
|
height = plant_percentage['plant_height']
|
|
|
|
leaf_area_index = plant_percentage['plant_leaf_area_index']
|
|
|
|
leaf_reflectivity = plant_percentage['plant_leaf_reflectivity']
|
|
|
|
leaf_emissivity = plant_percentage['plant_leaf_emissivity']
|
|
|
|
minimal_stomatal_resistance = plant_percentage['plant_minimal_stomatal_resistance']
|
|
|
|
co2_sequestration = plant_percentage['plant_co2_sequestration']
|
|
|
|
grows_on_soils = plant_percentage['plant_grows_on']
|
|
|
|
plant = Plant(plant_name, height, leaf_area_index, leaf_reflectivity, leaf_emissivity,
|
|
|
|
minimal_stomatal_resistance, co2_sequestration, grows_on_soils)
|
|
|
|
plant.percentage = plant_percentage['plant_percentage']
|
|
|
|
plants.append(plant)
|
|
|
|
vegetation = Vegetation(vegetation_name, soil, soil_thickness, plants)
|
|
|
|
for building_name in building_names:
|
|
|
|
for building in city.buildings:
|
|
|
|
if building.human_readable_name != building_name:
|
|
|
|
continue
|
|
|
|
for internal_zone in building.internal_zones:
|
|
|
|
for thermal_zone in internal_zone.thermal_zones:
|
|
|
|
for thermal_boundary in thermal_zone.thermal_boundaries:
|
|
|
|
if thermal_boundary.type == cte.ROOF:
|
|
|
|
thermal_boundary.vegetation = vegetation
|
|
|
|
|
|
|
|
response = {'result': 'succeed'}
|
|
|
|
except KeyError as ex:
|
|
|
|
response = {'error': f'Mandatory parameter {ex} is missing'}
|
2023-01-17 19:00:22 -05:00
|
|
|
return Response(json.dumps(response), status=400)
|
2023-01-10 12:12:01 -05:00
|
|
|
|
2023-01-17 19:00:22 -05:00
|
|
|
return Response(json.dumps(response))
|