building_selection_tool/building_region_selector.py

53 lines
2.1 KiB
Python

import json
from pathlib import Path
from shapely.geometry import Polygon
"""
BuildingSelectionTool is a tool that allows for a subset of buildings to be selected from a larger geojson dataset.
SPDX - License - Identifier: LGPL - 3.0 - or -later
Copyright © 2023 Concordia CERC group
Project Coder Koa Wells kekoa.wells@concordia.ca
"""
class BuildingSelectionTool:
def __init__(self, city_file, output_file):
self._city_file = city_file
self._city = json.load(city_file)
self._buildings = self._city['features']
self._polygon_points = []
def _assign_buildings_to_regions(self):
for building in self._buildings:
if building['geometry']['type'] == 'Polygon':
building_centroid = Polygon(building["geometry"]["coordinates"][0]).centroid
elif building['geometry']['type'] == 'MultiPolygon':
# use the centroid of the first polygon inside of the multipolygon
building_centroid = Polygon(building["geometry"]["coordinates"][0][0]).centroid
building['properties']['centroid'] = [building_centroid.x, building_centroid.y]
building['properties']['district_property'] = []
target_regions = []
region_assigned = False
target_region = self._regions[len(self._regions) / 2]
while not region_assigned:
if building_centroid.within(Polygon(target_region['geometry']['coordinates'])):
region_assigned = True
break
target_region_centroid = Polygon(target_region['geometry']['coordinates']).centroid
if building_centroid.x <= target_region_centroid.x:
if building_centroid.y >= target_region_centroid.y:
regions = regions[0:len(regions) / 2]
elif building_centroid.y < target_region_centroid.y:
regions = regions[len(regions) / 2:len(regions) - 1]
elif building_centroid.x > target_region_centroid.x:
if building_centroid.y >= target_region_centroid.y:
regions = regions[0:len(regions) / 2]
elif building_centroid.y < target_region_centroid.y:
regions = regions[len(regions) / 2:len(regions) - 1]