Compare commits

...

1 Commits

Author SHA1 Message Date
9b5d002444 feat: add required changes to create city 2024-12-03 11:50:38 -05:00
5 changed files with 158 additions and 1 deletions

88
data/test.geojson Normal file
View File

@ -0,0 +1,88 @@
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
-81.36226736610345,
42.96538327741903
],
[
-81.36226606408695,
42.96539751865368
],
[
-81.36169147743192,
42.96539971389088
],
[
-81.36169145437769,
42.96539577872095
],
[
-81.36160750096705,
42.965396296109915
],
[
-81.36167659355637,
42.96554865177412
],
[
-81.36143594626003,
42.96560572998295
],
[
-81.3612605719971,
42.9652212961923
],
[
-81.36153890966588,
42.96521432093572
],
[
-81.36172317324467,
42.9651651651121
],
[
-81.3617438333442,
42.965204767431615
],
[
-81.36220126106349,
42.96509245874135
],
[
-81.36231896772036,
42.96532755243104
],
[
-81.36230203685074,
42.96533218062799
],
[
-81.36232060244981,
42.9653700064552
],
[
-81.36226736610345,
42.96538327741903
]
]
]
},
"id": 1,
"properties": {
"name": "Shifton Head Office",
"address": "N/A",
"function": "1000",
"height": 14,
"year_of_construction": 2017,
"adjacency": "attached"
}
}
]
}

View File

@ -0,0 +1 @@
{"type": "FeatureCollection", "features": [{"type": "Feature", "geometry": {"type": "Polygon", "coordinates": [[[-81.36226736610345, 42.96538327741903], [-81.36232060244981, 42.9653700064552], [-81.36230203685074, 42.96533218062799], [-81.36231896772036, 42.96532755243104], [-81.36220126106349, 42.96509245874135], [-81.3617438333442, 42.965204767431615], [-81.36172317324467, 42.9651651651121], [-81.36153890966588, 42.96521432093572], [-81.3612605719971, 42.9652212961923], [-81.36143594626003, 42.96560572998295], [-81.36167659355637, 42.96554865177412], [-81.36160750096705, 42.965396296109915], [-81.36169145437769, 42.96539577872095], [-81.36169147743192, 42.96539971389088], [-81.36226606408695, 42.96539751865368], [-81.36226736610345, 42.96538327741903]]]}, "id": 1, "properties": {"name": "Shifton Head Office", "address": "N/A", "function": "1000", "height": 14, "year_of_construction": 2017, "adjacency": "attached"}}]}

View File

@ -58,7 +58,8 @@ class ConstructionHelper:
'Boucherville': '6', 'Boucherville': '6',
'Mascouche': '6', 'Mascouche': '6',
'Saint-Leonard': '6', 'Saint-Leonard': '6',
'La Prairie': '6' 'La Prairie': '6',
'London': '6'
} }
_reference_city_to_israel_climate_zone = { _reference_city_to_israel_climate_zone = {

21
main.py Normal file
View File

@ -0,0 +1,21 @@
from hub.imports.geometry_factory import GeometryFactory
from hub.helpers.dictionaries import Dictionaries
from hub.imports.construction_factory import ConstructionFactory
from hub.imports.results_factory import ResultFactory
from hub.exports.exports_factory import ExportsFactory
import subprocess
from pathlib import Path
from hub.imports.weather_factory import WeatherFactory
input_file = "data/test_updated.geojson"
city = GeometryFactory(
"geojson",
input_file,
height_field="height",
year_of_construction_field="year_of_construction",
function_field="function",
function_to_hub=Dictionaries().montreal_function_to_hub_function).city
ConstructionFactory('nrcan', city).enrich()
print('done')

46
shapely_test.py Normal file
View File

@ -0,0 +1,46 @@
import json
from shapely.geometry import shape, mapping, MultiPolygon, Polygon
from shapely.ops import unary_union
from pathlib import Path
def enforce_right_hand_rule(geojson):
for feature in geojson['features']:
geometry = shape(feature['geometry'])
if isinstance(geometry, Polygon):
if not geometry.exterior.is_ccw:
geometry = Polygon(geometry.exterior.coords[::-1], [interior.coords[::-1] for interior in geometry.interiors])
elif isinstance(geometry, MultiPolygon):
new_polygons = []
for polygon in geometry.geoms: # Use .geoms to iterate over the individual polygons
if not polygon.exterior.is_ccw:
polygon = Polygon(polygon.exterior.coords[::-1], [interior.coords[::-1] for interior in polygon.interiors])
new_polygons.append(polygon)
geometry = MultiPolygon(new_polygons)
feature['geometry'] = mapping(geometry)
return geojson
data_path = Path(__file__).parent.parent / 'hub/data' # Adjust this path as needed
# Load the GeoJSON file
input_filepath = data_path / 'test.geojson'
output_filepath = data_path / 'test_updated.geojson'
with open(input_filepath, 'r') as f:
geojson_data = json.load(f)
# Iterate through the features and convert MultiPolygons to Polygons
for feature in geojson_data['features']:
geom = shape(feature['geometry'])
if isinstance(geom, MultiPolygon):
# Merge the polygons into a single polygon
merged_polygon = unary_union(geom)
# Convert the merged polygon back to GeoJSON format
feature['geometry'] = mapping(merged_polygon)
# Enforce the right-hand rule
geojson_data = enforce_right_hand_rule(geojson_data)
# Save the updated GeoJSON file
with open(output_filepath, 'w') as f:
json.dump(geojson_data, f)