system_assignation/geojson_creator.py
Saeed Ranjbar 0a4465f669 geojson_creator.py is changed to a function getting x, y, and diff as input to generate the .geojson file
in main.py city is created and using ep_workflow.py the building demands are calculated and buildings are enriched.
2023-12-20 15:33:50 -05:00

35 lines
1.0 KiB
Python

import json
from shapely import Polygon
from shapely import Point
from pathlib import Path
def process_geojson(x, y, diff):
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 2.geojson').resolve()
output_file = Path('./input_files/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))
return output_file