summer_course_2024/city_model_structure/building.py

402 lines
10 KiB
Python
Raw Normal View History

"""
Building module
SPDX - License - Identifier: LGPL - 3.0 - or -later
Copyright © 2020 Project Author Guille Gutierrez guillermo.gutierrezmorote@concordia.ca
2020-10-26 08:26:31 -04:00
contributors: Pilar Monsalvete pilar_monsalvete@yahoo.es
"""
from typing import List
import matplotlib.patches as patches
import numpy as np
from matplotlib import pylab
from shapely import ops
from shapely.geometry import MultiPolygon
from city_model_structure.attributes.surface import Surface
from city_model_structure.attributes.thermal_boundary import ThermalBoundary
from city_model_structure.attributes.thermal_zone import ThermalZone
from city_model_structure.attributes.usage_zone import UsageZone
from city_model_structure.city_object import CityObject
2020-10-13 14:47:01 -04:00
from city_model_structure.building_unit import BuildingUnit
from helpers.geometry_helper import GeometryHelper as gh
class Building(CityObject):
"""
Building(CityObject) class
"""
def __init__(self, name, lod, surfaces, terrains, year_of_construction, function, lower_corner):
# todo: take the default values out of the classes!!
super().__init__(lod, surfaces, name)
self._basement_heated = None
self._attic_heated = None
self._terrains = terrains
self._year_of_construction = year_of_construction
self._function = function
# todo: this name is not clear. Is it lower corner of the building or lower corner of te city??
self._lower_corner = lower_corner
self._heated = None
self._cooled = None
self._average_storey_height = None
2020-10-28 12:20:13 -04:00
self._storeys_above_ground = None
self._foot_print = None
self._usage_zones = []
self._building_units = []
self._type = 'building'
self._heating = dict()
self._cooling = dict()
2020-10-30 08:25:36 -04:00
self._external_temperature = dict()
self._global_horizontal = dict()
self._diffuse = dict()
self._beam = dict()
# ToDo: Check this for LOD4
self._thermal_zones = []
if self.lod < 4:
# for lod under 4 is just one thermal zone
self._thermal_zones.append(ThermalZone(self.surfaces, self._heated, self._cooled))
for t_zones in self._thermal_zones:
t_zones.bounded = [ThermalBoundary(s, [t_zones]) for s in t_zones.surfaces]
surface_id = 0
for surface in self._surfaces:
surface.lower_corner = self._lower_corner
surface.parent(self, surface_id)
surface_id += 1
@property
def usage_zones(self) -> List[UsageZone]:
"""
Get city object usage zones
:return: [UsageZone]
"""
return self._usage_zones
@usage_zones.setter
def usage_zones(self, values):
"""
Set city objects usage zones
:param values: [UsageZones]
:return: None
"""
# ToDo: this is only valid for one usage zone need to be revised for multiple usage zones.
self._usage_zones = values
for thermal_zone in self.thermal_zones:
thermal_zone.usage_zones = [(100, usage_zone) for usage_zone in values]
@property
def terrains(self) -> List[Surface]:
"""
Get city object terrain surfaces
:return: [Surface]
"""
return self._terrains
@property
def attic_heated(self):
"""
Get if the city object attic is heated
:return: Boolean
"""
return self._attic_heated
@attic_heated.setter
def attic_heated(self, value):
"""
Set if the city object attic is heated
:param value: Boolean
:return: None
"""
self._attic_heated = value
@property
def basement_heated(self):
"""
Get if the city object basement is heated
:return: Boolean
"""
return self._basement_heated
@basement_heated.setter
def basement_heated(self, value):
"""
Set if the city object basement is heated
:param value: Boolean
:return: None
"""
self._basement_heated = value
@property
def name(self):
"""
City object name
:return: str
"""
return self._name
@property
def thermal_zones(self) -> List[ThermalZone]:
"""
City object thermal zones
:return: [ThermalZone]
"""
return self._thermal_zones
@property
def heated_volume(self):
"""
City object heated volume in cubic meters
:return: float
"""
2020-11-26 09:26:55 -05:00
# ToDo: this need to be calculated based on the basement and attic heated values
return self.volume
@property
def year_of_construction(self):
"""
City object year of construction
:return: int
"""
return self._year_of_construction
@property
def function(self):
"""
City object function
:return: str
"""
return self._function
@property
def average_storey_height(self):
"""
Get city object average storey height in meters
:return: float
"""
return self._average_storey_height
@average_storey_height.setter
def average_storey_height(self, value):
"""
Set city object average storey height in meters
:param value: float
:return: None
"""
self._average_storey_height = value
@property
def storeys_above_ground(self):
"""
Get city object storeys number above ground
:return: int
"""
return self._storeys_above_ground
@storeys_above_ground.setter
def storeys_above_ground(self, value):
"""
Set city object storeys number above ground
:param value: int
:return:
"""
self._storeys_above_ground = value
@staticmethod
def _tuple_to_point(xy_tuple):
return [xy_tuple[0], xy_tuple[1], 0.0]
def _plot(self, polygon):
points = ()
for point_tuple in polygon.exterior.coords:
almost_equal = False
for point in points:
point_1 = Building._tuple_to_point(point)
point_2 = Building._tuple_to_point(point_tuple)
if self._geometry.almost_equal(point_1, point_2):
almost_equal = True
break
if not almost_equal:
points = points + (point_tuple,)
points = points + (points[0],)
pylab.scatter([point[0] for point in points], [point[1] for point in points])
pylab.gca().add_patch(patches.Polygon(points, closed=True, fill=True))
pylab.grid()
pylab.show()
@property
def foot_print(self) -> Surface:
"""
City object foot print surface
:return: Surface
"""
if self._foot_print is None:
shapelys = []
union = None
for surface in self.surfaces:
if surface.shapely.is_empty or not surface.shapely.is_valid:
continue
shapelys.append(surface.shapely)
union = ops.unary_union(shapelys)
shapelys = [union]
if isinstance(union, MultiPolygon):
Exception('foot print returns a multipolygon')
points_list = []
for point_tuple in union.exterior.coords:
# ToDo: should be Z 0.0 or min Z?
point = Building._tuple_to_point(point_tuple)
almost_equal = False
for existing_point in points_list:
if self._geometry.almost_equal(point, existing_point):
almost_equal = True
break
if not almost_equal:
points_list.append(point)
points_list = np.reshape(points_list, len(points_list) * 3)
points = np.array_str(points_list).replace('[', '').replace(']', '')
self._foot_print = Surface(points, remove_last=False, is_projected=True)
return self._foot_print
@property
def type(self):
"""
2020-10-13 14:47:01 -04:00
building type
:return: str
"""
return self._type
2020-10-13 14:47:01 -04:00
@property
def building_units(self) -> [BuildingUnit]:
"""
Building units
:return:
"""
return self._building_units
@building_units.setter
def building_units(self, value):
"""
Building units
:param value: [BuildingUnit]
"""
self._building_units = value
@property
def heating(self) -> dict:
"""
heating demand in Wh
:return: dict{DataFrame(float)}
"""
return self._heating
@heating.setter
def heating(self, value):
"""
heating demand in Wh
:param value: dict{DataFrame(float)}
"""
self._heating = value
@property
def cooling(self) -> dict:
"""
cooling demand in Wh
:return: dict{DataFrame(float)}
"""
return self._cooling
@cooling.setter
def cooling(self, value):
"""
cooling demand in Wh
:param value: dict{DataFrame(float)}
"""
self._cooling = value
@property
2020-10-30 08:25:36 -04:00
def external_temperature(self) -> dict:
"""
2020-10-30 08:25:36 -04:00
external temperature surrounding the building in grads Celsius
:return: dict{DataFrame(float)}
"""
return self._external_temperature
2020-10-30 08:25:36 -04:00
@external_temperature.setter
def external_temperature(self, value):
"""
2020-10-30 08:25:36 -04:00
external temperature surrounding the building in grads Celsius
:param value: dict{DataFrame(float)}
"""
2020-10-30 08:25:36 -04:00
self._external_temperature = value
@property
2020-10-30 08:25:36 -04:00
def global_horizontal(self) -> dict:
"""
2020-10-30 08:25:36 -04:00
global horizontal radiation surrounding the building in W/m2
:return: dict{DataFrame(float)}
"""
2020-10-30 08:25:36 -04:00
return self._global_horizontal
2020-10-30 08:25:36 -04:00
@global_horizontal.setter
def global_horizontal(self, value):
"""
2020-10-30 08:25:36 -04:00
global horizontal radiation surrounding the building in W/m2
:param value: dict{DataFrame(float)}
"""
2020-10-30 08:25:36 -04:00
self._global_horizontal = value
2020-10-30 08:25:36 -04:00
@property
def diffuse(self) -> dict:
"""
2020-10-30 08:25:36 -04:00
diffuse radiation surrounding the building in W/m2
:return: dict{DataFrame(float)}
"""
return self._diffuse
2020-10-30 08:25:36 -04:00
@diffuse.setter
def diffuse(self, value):
"""
2020-10-30 08:25:36 -04:00
diffuse radiation surrounding the building in W/m2
:param value: dict{DataFrame(float)}
"""
2020-10-30 08:25:36 -04:00
self._diffuse = value
2020-10-30 08:25:36 -04:00
@property
def beam(self) -> dict:
"""
2020-10-30 08:25:36 -04:00
beam radiation surrounding the building in W/m2
:return: dict{DataFrame(float)}
"""
2020-10-30 08:25:36 -04:00
return self._beam
2020-10-30 08:25:36 -04:00
@beam.setter
def beam(self, value):
"""
2020-10-30 08:25:36 -04:00
beam radiation surrounding the building in W/m2
:param value: dict{DataFrame(float)}
"""
2020-10-30 08:25:36 -04:00
self._beam = value
@property
def storeys(self):
storeys = []
# todo: these values are not read yet from the files
# number_of_storeys = self.storeys_above_ground
# height = self.average_storey_height
number_of_storeys = 4
height = 1.5
mesh = self.polyhedron.polyhedron_trimesh
normal_plane = [0, 0, -1]
rest_mesh = mesh
for n in range(0, number_of_storeys - 1):
# todo: I need the lower corner of the building!!
# point_plane = [self._lower_corner[0], self._lower_corner[1], self._lower_corner[2] + height]
point_plane = [self._lower_corner[0] + 0.5, self._lower_corner[1] + 0.5, self._lower_corner[2] + height * (n + 1)]
meshes = gh.divide_mesh_by_plane(rest_mesh, normal_plane, point_plane)
storey = meshes[0]
rest_mesh = meshes[1]
storeys.append(storey)
storeys.append(rest_mesh)
return storeys