CityBEM-CityLayers-SaeedRay.../scripts/geojson_creator.py

63 lines
2.0 KiB
Python

import json
from shapely import Polygon
from shapely import Point
from pathlib import Path
# def process_geojson(a, b, c , d, e, f, g , h):
# selection_box = Polygon([[a, b],
# [c, d],
# [e, f],
# [g, h]])
# 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
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