diff --git a/building_selection_tool.py b/building_selection_tool.py index dcfaeb9..b7b05da 100644 --- a/building_selection_tool.py +++ b/building_selection_tool.py @@ -26,8 +26,7 @@ class BuildingSelectionTool: def select_by_polygon(self, selection_polygon_coordinates): """ Get all buildings within a specified polygon and write the output to output_file_path - :param selection_polygon: list of coordinates forming the selection polygon - :param output_file_path: path and file name for output file containing selected buildings and their metadata + :param selection_polygon_coordinates: list of coordinates forming the selection polygon :return: None """ selection_polygon = Polygon(selection_polygon_coordinates) @@ -42,15 +41,24 @@ class BuildingSelectionTool: def select_by_coordinate(self, coordinate): """ :param coordinate: longitude, latitude coordinate of selected - :return: + :return: None """ - pass + longitude = coordinate[0] + latitude = coordinate[1] + + point = Point([longitude, latitude]) + + for building in self.buildings: + building_coordinates = building['geometry']['coordinates'][0] + building_polygon = Polygon(building_coordinates) + if point.within(building_polygon): + self.selected_buildings.append(building) def select_by_address(self, address): """ - + Select a building by inputting an address :param address: - :return: + :return: None """ coordinates = helpers.get_gps_coordinate_by_address(address) longitude = coordinates[1] @@ -66,8 +74,9 @@ class BuildingSelectionTool: def save_selected_buildings(self, output_file_path): """ - - :return: + Save all selected buildings to an output file. + :param output_file_path: destination file path including file name and extension + :return: None """ output_file = Path(output_file_path).resolve() output_region = {"type": "FeatureCollection", diff --git a/main.py b/main.py index 48331ab..58cec0d 100644 --- a/main.py +++ b/main.py @@ -2,6 +2,7 @@ from building_selection_tool import BuildingSelectionTool selection_tool = BuildingSelectionTool('./data/collinear_clean.geojson') +# select all buildings within a polygon sample_polygon_coordinates = [[-73.543833, 45.575932], [-73.541834, 45.575245], [-73.542275, 45.574652], @@ -10,8 +11,14 @@ sample_polygon_coordinates = [[-73.543833, 45.575932], selection_tool.select_by_polygon(sample_polygon_coordinates) +# select building by address sample_address = "2155 Rue Guy, Montreal, Quebec" selection_tool.select_by_address(sample_address) +# select building by [longitude, latitude] coordinate +sample_coordinate = [-73.5790796, 45.4972906] +selection_tool.select_by_coordinate(sample_coordinate) + +# save selected buildings in geojson format to specified output_file_path output_file_path = "./output_files/sample_output.geojson" selection_tool.save_selected_buildings(output_file_path)