From 9b5d002444f4f392997b65f9943271f678739681 Mon Sep 17 00:00:00 2001
From: majidrezaei93 <abolfazl.rezaei@mail.concordia.ca>
Date: Tue, 3 Dec 2024 10:46:04 -0500
Subject: [PATCH] feat: add required changes to create city

---
 data/test.geojson                             | 88 +++++++++++++++++++
 data/test_updated.geojson                     |  1 +
 .../helpers/construction_helper.py            |  3 +-
 main.py                                       | 21 +++++
 shapely_test.py                               | 46 ++++++++++
 5 files changed, 158 insertions(+), 1 deletion(-)
 create mode 100644 data/test.geojson
 create mode 100644 data/test_updated.geojson
 create mode 100644 main.py
 create mode 100644 shapely_test.py

diff --git a/data/test.geojson b/data/test.geojson
new file mode 100644
index 00000000..a5e2bc96
--- /dev/null
+++ b/data/test.geojson
@@ -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"
+      }
+    }
+  ]
+}
\ No newline at end of file
diff --git a/data/test_updated.geojson b/data/test_updated.geojson
new file mode 100644
index 00000000..4b3973a7
--- /dev/null
+++ b/data/test_updated.geojson
@@ -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"}}]}
\ No newline at end of file
diff --git a/hub/imports/construction/helpers/construction_helper.py b/hub/imports/construction/helpers/construction_helper.py
index 8eaf758c..bd6476c7 100644
--- a/hub/imports/construction/helpers/construction_helper.py
+++ b/hub/imports/construction/helpers/construction_helper.py
@@ -58,7 +58,8 @@ class ConstructionHelper:
     'Boucherville': '6',
     'Mascouche': '6',
     'Saint-Leonard': '6',
-    'La Prairie': '6'
+    'La Prairie': '6',
+    'London': '6'
   }
 
   _reference_city_to_israel_climate_zone = {
diff --git a/main.py b/main.py
new file mode 100644
index 00000000..946abad8
--- /dev/null
+++ b/main.py
@@ -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')
diff --git a/shapely_test.py b/shapely_test.py
new file mode 100644
index 00000000..416a8f0e
--- /dev/null
+++ b/shapely_test.py
@@ -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)
+
+