hub/catalogs/greenery/greenery_catalog.py

116 lines
4.1 KiB
Python

"""
Greenery catalog
SPDX - License - Identifier: LGPL - 3.0 - or -later
Copyright © 2022 Project Author Guille Gutierrez guillermo.gutierrezmorote@concordia.ca
"""
from pyecore.resources import ResourceSet, URI
from catalogs.greenery.ecore_greenery.greenerycatalog import GreeneryCatalog as gc
from catalogs.catalog import Catalog
from pathlib import Path
from catalogs.data_model.greenery.vegetation import Vegetation as libs_vegetation
from catalogs.data_model.greenery.plant import Plant as libs_plant
from catalogs.data_model.greenery.soil import Soil as libs_soil
from catalogs.data_model.greenery.plant_percentage import PlantPercentage as libs_pp
from catalogs.data_model.greenery.greenery_content import GreeneryContent
class GreeneryCatalog(Catalog):
def __init__(self, path):
base_path = Path(Path(__file__).parent / 'ecore_greenery' / 'greenerycatalog_no_quantities.ecore')
resource_set = ResourceSet()
data_model = resource_set.get_resource(URI(str(base_path)))
data_model_root = data_model.contents[0]
resource_set.metamodel_registry[data_model_root.nsURI] = data_model_root
resource = resource_set.get_resource(URI(str(path)))
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 = []
for vegetation_category in catalog_data.vegetationCategories:
name = vegetation_category.name
for vegetation in vegetation_category.vegetationTemplates:
plant_percentages = []
for plant_percentage in vegetation.plants:
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))
plants = []
for plant_category in catalog_data.plantCategories:
name = plant_category.name
for plant in plant_category.plants:
plants.append(libs_plant(name, plant))
soils = []
for soil in catalog_data.soils:
soils.append(libs_soil(soil))
self._data = GreeneryContent(vegetations, plants, soils)
def names(self, category=None):
"""
Get the catalog elements names
:parm: optional category filter
"""
if category is None:
_names = {'vegetations': [], 'plants': [], 'soils': []}
for vegetation in self._data.vegetations:
_names['vegetations'].append(vegetation.name)
for plant in self._data.plants:
_names['plants'].append(plant.name)
for soil in self._data.soils:
_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
def get_entry(self, name):
"""
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")
def entries(self, category=None):
if category is None:
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}]')