building_selection_tool/main.py

35 lines
1.1 KiB
Python

import json
from shapely import Polygon
from shapely import Point
from pathlib import Path
# Make sure to enter your points in the clockwise direction
# and in the longitude, latitude format
selection_box = Polygon([[-73.543833, 45.575932] ,
[-73.541834, 45.575245],
[-73.542275, 45.574652],
[-73.544235, 45.575329],
[-73.543833, 45.575932]])
geojson_file = Path('./data/collinear_clean.geojson').resolve()
output_file = Path('./output_buildings.geojson').resolve()
buildings_in_region = []
with open(geojson_file, 'r') as file:
city = json.load(file)
buildings = city['features']
for building in buildings:
coordinates = building['geometry']['coordinates'][0]
building_polygon = Polygon(coordinates)
centroid = Point(building_polygon.centroid)
if centroid.within(selection_box):
buildings_in_region.append(building)
output_region = {"type": "FeatureCollection",
"features": buildings_in_region}
with open(output_file, 'w') as file:
file.write(json.dumps(output_region, indent=2))