pv_workflow/building_modelling/geojson_creator.py

38 lines
1.2 KiB
Python
Raw Permalink Normal View History

import json
from shapely import Polygon
from shapely import Point
from pathlib import Path
def process_geojson(x, y, diff, path, expansion=False):
selection_box = Polygon([[x + diff, y - diff],
[x - diff, y - diff],
[x - diff, y + diff],
[x + diff, y + diff]])
geojson_file = Path(path / 'data/collinear_clean 2.geojson').resolve()
if not expansion:
output_file = Path(path / 'input_files/output_buildings.geojson').resolve()
else:
output_file = Path(path / 'input_files/output_buildings_expanded.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))
return output_file