Code quality improvements and basic creation og geojson from gml

This commit is contained in:
Guille Gutierrez 2023-10-20 14:19:46 +02:00
parent 8d5010218b
commit a31281cae7
5 changed files with 98 additions and 4 deletions

View File

@ -9,6 +9,7 @@ from pathlib import Path
from hub.exports.formats.glb import Glb from hub.exports.formats.glb import Glb
from hub.exports.formats.obj import Obj from hub.exports.formats.obj import Obj
from hub.exports.formats.geojson import Geojson
from hub.exports.formats.simplified_radiosity_algorithm import SimplifiedRadiosityAlgorithm from hub.exports.formats.simplified_radiosity_algorithm import SimplifiedRadiosityAlgorithm
from hub.exports.formats.stl import Stl from hub.exports.formats.stl import Stl
from hub.exports.formats.cesiumjs_tileset import CesiumjsTileset from hub.exports.formats.cesiumjs_tileset import CesiumjsTileset
@ -85,6 +86,10 @@ class ExportsFactory:
def _glb(self): def _glb(self):
return Glb(self._city, self._path, target_buildings=self._target_buildings) return Glb(self._city, self._path, target_buildings=self._target_buildings)
@property
def _geojson(self):
return Geojson(self._city, self._path, target_buildings=self._target_buildings)
def export(self): def export(self):
""" """
Export the city given to the class using the given export type handler Export the city given to the class using the given export type handler

View File

@ -1,3 +1,9 @@
"""
export a city into Cesium tileset format
SPDX - License - Identifier: LGPL - 3.0 - or -later
Copyright © 2022 Concordia CERC group
Project Coder Guille Gutierrez guillermo.gutierrezmorote@concordia.ca
"""
import json import json
import math import math

View File

@ -0,0 +1,74 @@
"""
export a city into Geojson format
SPDX - License - Identifier: LGPL - 3.0 - or -later
Copyright © 2022 Concordia CERC group
Project Coder Guille Gutierrez guillermo.gutierrezmorote@concordia.ca
"""
import json
from pathlib import Path
import numpy as np
import pyproj
from pyproj import Transformer
from hub.helpers.geometry_helper import GeometryHelper
class Geojson:
"""
Export to geojson format
"""
def __init__(self, city, path, target_buildings):
self._city = city
self._file_path = Path(path / f'{self._city.name}.geojson').resolve()
try:
srs_name = self._city.srs_name
if self._city.srs_name in GeometryHelper.srs_transformations:
srs_name = GeometryHelper.srs_transformations[self._city.srs_name]
input_reference = pyproj.CRS(srs_name) # Projected coordinate system from input data
except pyproj.exceptions.CRSError as err:
raise pyproj.exceptions.CRSError from err
self._to_gps = Transformer.from_crs(input_reference, pyproj.CRS('EPSG:4326'))
if target_buildings is None:
target_buildings = [b.name for b in self._city.buildings]
self._geojson_skeleton = {
'type': 'FeatureCollection',
'features': []
}
self._feature_skeleton = {
'type': 'Feature',
'geometry': {
'type': 'Polygon',
'coordinates': []
},
'properties': {}
}
self._export()
def _export(self):
for building in self._city.buildings:
feature = self._feature_skeleton.copy()
feature['id'] = building.name
feature['properties']['height'] = f'{building.max_height}'
feature['properties']['function'] = f'{building.function}'
feature['properties']['year_of_construction'] = f'{building.year_of_construction}'
feature['properties']['aliases'] = building.aliases
feature['properties']['elevation'] = f'{building.lower_corner[2]}'
for ground in building.grounds:
ground_coordinates = []
for coordinate in ground.solid_polygon.coordinates:
gps_coordinate = self._to_gps.transform(coordinate[0], coordinate[1])
ground_coordinates.insert(0, [gps_coordinate[1], gps_coordinate[0]])
first_gps_coordinate = self._to_gps.transform(
ground.solid_polygon.coordinates[0][0],
ground.solid_polygon.coordinates[0][1]
)
ground_coordinates.insert(0, [first_gps_coordinate[1], first_gps_coordinate[0]])
feature['geometry']['coordinates'].append(ground_coordinates)
self._geojson_skeleton['features'].append(feature)
with open(self._file_path, 'w', encoding='utf-8') as f:
json.dump(self._geojson_skeleton, f, indent=2)

View File

@ -1,3 +1,9 @@
"""
export a city into Glb format
SPDX - License - Identifier: LGPL - 3.0 - or -later
Copyright © 2022 Concordia CERC group
Project Coder Guille Gutierrez guillermo.gutierrezmorote@concordia.ca
"""
import os import os
import shutil import shutil
import subprocess import subprocess
@ -13,6 +19,9 @@ class GltExceptionError(Exception):
class Glb: class Glb:
"""
Glb class
"""
def __init__(self, city, path, target_buildings=None): def __init__(self, city, path, target_buildings=None):
self._city = city self._city = city
self._path = path self._path = path
@ -23,10 +32,6 @@ class Glb:
@property @property
def _obj2gtl(self): def _obj2gtl(self):
"""
Get the SRA installation path
:return: str
"""
return shutil.which('obj2gltf') return shutil.which('obj2gltf')
def _export(self): def _export(self):

View File

@ -98,6 +98,10 @@ class TestExports(TestCase):
glb_file = Path(self._output_path / f'{building.name}.glb') glb_file = Path(self._output_path / f'{building.name}.glb')
self.assertTrue(glb_file.exists(), f'{building.name} Building glb wasn\'t correctly generated') self.assertTrue(glb_file.exists(), f'{building.name} Building glb wasn\'t correctly generated')
def test_geojson_export(self):
self._export('geojson', False)
self.assertTrue(False, "False is not True")
def test_energy_ade_export(self): def test_energy_ade_export(self):
""" """
export to energy ADE export to energy ADE