api_v1.4/hub_api/city_info.py

73 lines
2.7 KiB
Python
Raw Normal View History

"""
City info
SPDX - License - Identifier: LGPL - 3.0 - or -later
Copyright © 2022 Project Author name guillermo.gutierrezmorote@concordia.ca
"""
import json
from flask import Response, request
from flask_restful import Resource
from hub_api.helpers.session_helper import refresh_session
class CityInfo(Resource):
def __init__(self):
pass
@staticmethod
def get():
session = refresh_session(request)
if session is None:
return Response(json.dumps({'error': 'invalid session'}), status=401)
headers = session.headers
city = session.city
# TODO: this is only for dompark project and need to be removed in future versions.
floor_area = 0
wall_construction = 'unknown'
floor_construction = 'unknown'
roof_construction = 'unknown'
window_type = 'unknown'
building_dic = {}
for building in city.buildings:
usages = [] # This is only valid for dompark project as all the building have the same usage
if building.lower_corner[2] == 0:
floor_area += building.floor_area
for internal_zone in building.internal_zones:
for usage_zone in internal_zone.usage_zones:
usages.append({'percentage': usage_zone.percentage, 'usage': usage_zone.usage})
for thermal_zone in internal_zone.thermal_zones:
for thermal_boundary in thermal_zone.thermal_boundaries:
if thermal_boundary.parent_surface.type == 'Ground':
floor_construction = thermal_boundary.construction_name
elif thermal_boundary.parent_surface.type == 'Wall':
wall_construction = thermal_boundary.construction_name
for thermal_opening in thermal_boundary.thermal_openings:
if thermal_opening.construction_name is not None:
window_type = thermal_opening.construction_name
break
else:
roof_construction = thermal_boundary.construction_name
name = building.human_readable_name
year_of_construction = str(building.year_of_construction)
building_dic = {
'name': str(name),
'floor_area': str(floor_area),
'year_of_construction': str(year_of_construction),
'usages': usages,
'wall_construction': wall_construction,
'floor_construction': floor_construction,
'roof_construction': roof_construction,
'window_type': window_type,
'default_archetype': 'industry ASHRAE_2004:4A non_standard_dompark'
}
buildings = [building_dic]
response = {'city_name': 'Montreal',
'climate_reference_city': str(city.climate_reference_city),
'buildings': buildings
}
return Response(json.dumps(response), headers=headers)