Complete implementation for the greenery catalog integration

This commit is contained in:
Guille Gutierrez 2022-03-09 11:40:06 -05:00
parent 868dfe22d5
commit d5a4ffe0fa
5 changed files with 115 additions and 21 deletions

View File

@ -17,8 +17,7 @@ class Catalog:
""" """
raise NotImplementedError raise NotImplementedError
@property def entries(self, category=None):
def entries(self):
""" """
Base property to return the catalog entries Base property to return the catalog entries
:return: not implemented error :return: not implemented error

View File

@ -4,6 +4,8 @@ SPDX - License - Identifier: LGPL - 3.0 - or -later
Copyright © 2022 Project Author Guille Gutierrez guillermo.gutierrezmorote@concordia.ca Copyright © 2022 Project Author Guille Gutierrez guillermo.gutierrezmorote@concordia.ca
""" """
from catalogs.data_model.greenery.soil import Soil as libs_soil
class Plant: class Plant:
def __init__(self, category, plant): def __init__(self, category, plant):
@ -15,6 +17,9 @@ class Plant:
self._leaf_emissivity = plant.leafEmissivity self._leaf_emissivity = plant.leafEmissivity
self._minimal_stomatal_resistance = plant.minimalStomatalResistance self._minimal_stomatal_resistance = plant.minimalStomatalResistance
self._co2_sequestration = plant.co2Sequestration self._co2_sequestration = plant.co2Sequestration
self._grows_on = []
for soil in plant.growsOn:
self._grows_on.append(libs_soil(soil))
@property @property
def name(self): def name(self):
@ -71,3 +76,10 @@ class Plant:
Get plant co2 sequestration capacity Get plant co2 sequestration capacity
""" """
return self._co2_sequestration return self._co2_sequestration
@property
def grows_on(self) -> [libs_soil]:
"""
Get plant compatible soils
"""
return self._grows_on

View File

@ -4,12 +4,12 @@ SPDX - License - Identifier: LGPL - 3.0 - or -later
Copyright © 2022 Project Author Guille Gutierrez guillermo.gutierrezmorote@concordia.ca Copyright © 2022 Project Author Guille Gutierrez guillermo.gutierrezmorote@concordia.ca
""" """
from catalogs.data_model.greenery.plant import Plant from catalogs.data_model.greenery.plant import Plant as libs_plant
class PlantPercentage(Plant): class PlantPercentage(libs_plant):
def __init__(self, percentage, plant): def __init__(self, percentage, plant_category, plant):
super().__init__("unknown", plant) super().__init__(plant_category, plant)
self._percentage = percentage self._percentage = percentage
@property @property

View File

@ -26,6 +26,12 @@ class GreeneryCatalog(Catalog):
resource = resource_set.get_resource(URI(str(path))) resource = resource_set.get_resource(URI(str(path)))
catalog_data: gc = resource.contents[0] catalog_data: gc = resource.contents[0]
plants = []
for plant_category in catalog_data.plantCategories:
name = plant_category.name
for plant in plant_category.plants:
plants.append(libs_plant(name, plant))
vegetations = [] vegetations = []
for vegetation_category in catalog_data.vegetationCategories: for vegetation_category in catalog_data.vegetationCategories:
name = vegetation_category.name name = vegetation_category.name
@ -33,7 +39,12 @@ class GreeneryCatalog(Catalog):
plant_percentages = [] plant_percentages = []
for plant_percentage in vegetation.plants: for plant_percentage in vegetation.plants:
plant_percentages.append(libs_pp(plant_percentage.percentage, plant_percentage.plant)) plant_category = "Unknown"
for plant in plants:
if plant.name == plant_percentage.plant.name:
plant_category = plant.category
break
plant_percentages.append(libs_pp(plant_percentage.percentage,plant_category, plant_percentage.plant))
vegetations.append(libs_vegetation(name, vegetation, plant_percentages)) vegetations.append(libs_vegetation(name, vegetation, plant_percentages))
plants = [] plants = []
for plant_category in catalog_data.plantCategories: for plant_category in catalog_data.plantCategories:
@ -47,23 +58,58 @@ class GreeneryCatalog(Catalog):
self._data = GreeneryContent(vegetations, plants, soils) self._data = GreeneryContent(vegetations, plants, soils)
@property def names(self, category=None):
def names(self):
""" """
:parm: Get the catalog elements names
:parm: optional category filter
""" """
_names = [] if category is None:
_names = {'vegetations': [], 'plants': [], 'soils': []}
for vegetation in self._data.vegetations: for vegetation in self._data.vegetations:
_names.append(vegetation.name) _names['vegetations'].append(vegetation.name)
for plant in self._data.plants: for plant in self._data.plants:
_names.append(plant.name) _names['plants'].append(plant.name)
for soil in self._data.soils: for soil in self._data.soils:
_names.append(soil.name) _names['soils'].append(soil.name)
else:
_names = {category: []}
if category == 'vegetations':
for vegetation in self._data.vegetations:
_names[category].append(vegetation.name)
elif category == 'plants':
for plant in self._data.plants:
_names[category].append(plant.name)
elif category == 'soils':
for soil in self._data.soils:
_names[category].append(soil.name)
else:
raise ValueError(f'Unknown category [{category}]')
return _names return _names
def get_entry(self, name): def get_entry(self, name):
return 'value' """
Get one complete entry from the greenery catalog
"""
for entry in self._data.vegetations:
if entry.name == name:
return entry
for entry in self._data.plants:
if entry.name == name:
return entry
for entry in self._data.soils:
if entry.name == name:
return entry
raise IndexError(f"{name} doesn't exists in the catalog")
@property def entries(self, category=None):
def entries(self): if category is None:
pass return self._data
else:
if category == 'vegetations':
return self._data.vegetations
elif category == 'plants':
return self._data.plants
elif category == 'soils':
return self._data.soils
else:
raise ValueError(f'Unknown category [{category}]')

View File

@ -0,0 +1,37 @@
"""
TestGeometryFactory test and validate the city model structure geometric parameters
SPDX - License - Identifier: LGPL - 3.0 - or -later
Copyright © 2020 Project Author Guille Gutierrez guillermo.gutierrezmorote@concordia.ca
"""
from unittest import TestCase
from catalogs.greenery_catalog_factory import GreeneryCatalogFactory
class TestGreeneryCatalog(TestCase):
def test_catalog(self):
catalog = GreeneryCatalogFactory('nrel').catalog_debug
catalog_categories = catalog.names()
vegetations = catalog.names('vegetations')
plants = catalog.names('plants')
soils = catalog.names('soils')
self.assertTrue(len(catalog_categories) == 3)
self.assertTrue(len(vegetations['vegetations']) == 4)
self.assertTrue(len(plants['plants']) == 14)
self.assertTrue(len(soils['soils']) == 6)
with self.assertRaises(ValueError):
catalog.names('unknown')
# retrieving all the entries should not raise any exceptions
for category in catalog_categories:
for value in catalog_categories[category]:
catalog.get_entry(value)
with self.assertRaises(IndexError):
catalog.get_entry('unknown')
self.assertTrue(len(catalog.entries().vegetations) == 4)
self.assertTrue(len(catalog.entries().plants) == 14)
self.assertTrue(len(catalog.entries().soils) == 6)