building_selection_tool/main.py

41 lines
1.3 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]])
x=-73.61574532884296
y=45.603911965131
diff=0.001
selection_box = Polygon([[x+diff, y-diff],
[x-diff, y-diff],
[x-diff, y+diff],
[x+diff, y+diff]])
geojson_file = Path('./data/collinear_clean 1.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))