Complete neighbours detection (with shared lines)

This commit is contained in:
Guille Gutierrez 2023-03-08 14:24:48 -05:00
parent 4caea94271
commit fc2add511a
2 changed files with 65 additions and 18 deletions

View File

@ -5,6 +5,7 @@ Copyright © 2022 Concordia CERC group
Project Coder Guille Gutierrez guillermo.gutierrezmorote@concordia.ca Project Coder Guille Gutierrez guillermo.gutierrezmorote@concordia.ca
Code contributors: Pilar Monsalvete Alvarez de Uribarri pilar.monsalvete@concordia.ca Code contributors: Pilar Monsalvete Alvarez de Uribarri pilar.monsalvete@concordia.ca
""" """
import datetime
import math import math
import numpy as np import numpy as np
import requests import requests
@ -63,7 +64,7 @@ class GeometryHelper:
return MapPoint(((city.upper_corner[0] - coordinate[0]) * 0.5), ((city.upper_corner[1] - coordinate[1]) * 0.5)) return MapPoint(((city.upper_corner[0] - coordinate[0]) * 0.5), ((city.upper_corner[1] - coordinate[1]) * 0.5))
@staticmethod @staticmethod
def city_mapping(city, building_names=None, plot=False): def city_mapping(city, building_names=None):
""" """
Returns a shared_information dictionary like Returns a shared_information dictionary like
@ -71,17 +72,15 @@ class GeometryHelper:
"building_name" : [{line: 0 coordinate_1: [x,y,z], coordinate_2:[x, y, z], points: 0}] "building_name" : [{line: 0 coordinate_1: [x,y,z], coordinate_2:[x, y, z], points: 0}]
} }
""" """
shared_information = {} lines_information = {}
if building_names is None: if building_names is None:
building_names = [b.name for b in city.buildings] building_names = [b.name for b in city.buildings]
x = int((city.upper_corner[0] - city.lower_corner[0]) * 0.5) + 1 x = int((city.upper_corner[0] - city.lower_corner[0]) * 0.5) + 1
y = int((city.upper_corner[1] - city.lower_corner[1]) * 0.5) + 1 y = int((city.upper_corner[1] - city.lower_corner[1]) * 0.5) + 1
city_map = [[{} for _ in range(y + 1)] for _ in range(x + 1)] city_map = [['' for _ in range(y + 1)] for _ in range(x + 1)]
img = Image.new('RGB', (x + 1, y + 1), "black") # create a new black image map_info = [[{} for _ in range(y + 1)] for _ in range(x + 1)]
city_image = img.load() # create the pixel map
for building_name in building_names: for building_name in building_names:
building = city.city_object(building_name) building = city.city_object(building_name)
shared_information[building_name]: []
line = 0 line = 0
for ground in building.grounds: for ground in building.grounds:
length = len(ground.perimeter_polygon.coordinates) - 1 length = len(ground.perimeter_polygon.coordinates) - 1
@ -90,22 +89,63 @@ class GeometryHelper:
if i == length: if i == length:
j = 0 j = 0
next_coordinate = ground.perimeter_polygon.coordinates[j] next_coordinate = ground.perimeter_polygon.coordinates[j]
line_dictionary = {"line": line, "coordinate_1": coordinate, "coordinate_2":next_coordinate, "points": 0}
print(building_name, line_dictionary['line'])
point = GeometryHelper.coordinate_to_map_point(coordinate, city) point = GeometryHelper.coordinate_to_map_point(coordinate, city)
distance = GeometryHelper.distance_between_points(coordinate, next_coordinate) distance = int(GeometryHelper.distance_between_points(coordinate, next_coordinate))
if distance == 0: if distance == 0:
continue continue
delta_x = (coordinate[0] - next_coordinate[0]) / (distance / 0.5) delta_x = (coordinate[0] - next_coordinate[0]) / (distance / 0.5)
delta_y = (coordinate[1] - next_coordinate[1]) / (distance / 0.5) delta_y = (coordinate[1] - next_coordinate[1]) / (distance / 0.5)
for k in range(0, int(distance)): for k in range(0, distance):
x = MapPoint(point.x + (delta_x * k), point.y + (delta_y * k)).x x = MapPoint(point.x + (delta_x * k), point.y + (delta_y * k)).x
y = MapPoint(point.x + (delta_x * k), point.y + (delta_y * k)).y y = MapPoint(point.x + (delta_x * k), point.y + (delta_y * k)).y
if city_map[x][y] == {}: if city_map[x][y] == '':
city_map[x][y] = building.name city_map[x][y] = building.name
city_image[x, y] = (100, 0, 0) map_info[x][y] = {
'line_start': (coordinate[0], coordinate[1]),
'line_end': (next_coordinate[0], next_coordinate[1]),
}
elif city_map[x][y] != building.name: elif city_map[x][y] != building.name:
neighbour = city.city_object(city_map[x][y]) neighbour = city.city_object(city_map[x][y])
neighbour_info = map_info[x][y]
# prepare the keys
neighbour_start_coordinate = f'{neighbour_info["line_start"][0]}_{neighbour_info["line_start"][1]}'
building_start_coordinate = f'{coordinate[0]}_{coordinate[1]}'
neighbour_key = f'{neighbour.name}_{neighbour_start_coordinate}_{building_start_coordinate}'
building_key = f'{building.name}_{building_start_coordinate}_{neighbour_start_coordinate}'
# Add my neighbour info to my shared lines
if building.name in lines_information.keys() and neighbour_key in lines_information[building.name]:
shared_points = int(lines_information[building.name][neighbour_key]['shared_points'])
lines_information[building.name][neighbour_key]['shared_points'] = shared_points + 1
else:
if building.name not in lines_information.keys():
lines_information[building.name] = {}
lines_information[building.name][neighbour_key] = {
'neighbour_name': neighbour.name,
'line_start': (coordinate[0], coordinate[1]),
'line_end': (next_coordinate[0], next_coordinate[1]),
'neighbour_line_start': neighbour_info['line_start'],
'neighbour_line_end': neighbour_info['line_end'],
'shared_points': 1
}
# Add my info to my neighbour shared lines
if neighbour.name in lines_information.keys() and building_key in lines_information[neighbour.name]:
shared_points = int(lines_information[neighbour.name][building_key]['shared_points'])
lines_information[neighbour.name][building_key]['shared_points'] = shared_points + 1
else:
if neighbour.name not in lines_information.keys():
lines_information[neighbour.name] = {}
lines_information[neighbour.name][building_key] = {
'neighbour_name': building.name,
'line_start': neighbour_info['line_start'],
'line_end': neighbour_info['line_end'],
'neighbour_line_start': (coordinate[0], coordinate[1]),
'neighbour_line_end': (next_coordinate[0], next_coordinate[1]),
'shared_points': 1
}
if building.neighbours is None: if building.neighbours is None:
building.neighbours = [neighbour] building.neighbours = [neighbour]
elif neighbour not in building.neighbours: elif neighbour not in building.neighbours:
@ -115,8 +155,7 @@ class GeometryHelper:
elif building not in neighbour.neighbours: elif building not in neighbour.neighbours:
neighbour.neighbours.append(building) neighbour.neighbours.append(building)
line += 1 line += 1
if plot: return lines_information
img.show()
@staticmethod @staticmethod
def segment_list_to_trimesh(lines) -> Trimesh: def segment_list_to_trimesh(lines) -> Trimesh:

View File

@ -152,10 +152,18 @@ class TestGeometryFactory(TestCase):
""" """
Test neighbours map creation Test neighbours map creation
""" """
file = 'concordia.geojson' file = 'neighbours.geojson'
city = self._get_city(file, 'geojson', city = self._get_city(file, 'geojson',
height_field='citygml_me',
year_of_construction_field='ANNEE_CONS', year_of_construction_field='ANNEE_CONS',
function_field='LIBELLE_UT') function_field='LIBELLE_UT')
GeometryHelper.city_mapping(city, plot=False) GeometryHelper.city_mapping(city)
self.assertTrue(False) for building in city.buildings:
self.assertEqual(2, len(building.neighbours))
self.assertEqual('2_part_0_zone0_zone_0',city.city_object('1_part_0_zone0_zone_0').neighbours[0].name)
self.assertEqual('3_part_0_zone0_zone_0',city.city_object('1_part_0_zone0_zone_0').neighbours[1].name)
self.assertEqual('1_part_0_zone0_zone_0',city.city_object('2_part_0_zone0_zone_0').neighbours[0].name)
self.assertEqual('3_part_0_zone0_zone_0',city.city_object('2_part_0_zone0_zone_0').neighbours[1].name)
self.assertEqual('2_part_0_zone0_zone_0', city.city_object('3_part_0_zone0_zone_0').neighbours[0].name)
self.assertEqual('1_part_0_zone0_zone_0', city.city_object('3_part_0_zone0_zone_0').neighbours[1].name)