|
import gradio as gr |
|
import geopandas as gpd |
|
import pandas as pd |
|
import os |
|
import matplotlib.pyplot as plt |
|
from shapely.geometry import shape |
|
from shapely.ops import unary_union |
|
|
|
|
|
|
|
|
|
def process_buildings(input_gdf, sensitive_sites_gdf, default_building_height_m, multiplier_factor): |
|
|
|
intersected_sites = [] |
|
|
|
|
|
buffers = [] |
|
|
|
intersection_desc = "" |
|
|
|
|
|
for idx, building in input_gdf.iterrows(): |
|
building_name = building.get('building_name', 'Unnamed building') |
|
|
|
|
|
|
|
if 'building_height' in building and pd.notnull(building['building_height']) and building['building_height'] != 0: |
|
building_height_m = building['building_height'] * 0.3048 |
|
else: |
|
building_height_m = default_building_height_m |
|
|
|
buffer_distance_m = building_height_m * multiplier_factor |
|
|
|
|
|
building_geometry = gpd.GeoSeries([building['geometry']], crs="EPSG:4326") |
|
building_geometry_m = building_geometry.to_crs("EPSG:3857") |
|
|
|
|
|
building_buffer = building_geometry_m.buffer(buffer_distance_m) |
|
building_buffer_gdf = gpd.GeoDataFrame(geometry=building_buffer, crs="EPSG:3857") |
|
building_buffer_gdf = building_buffer_gdf.to_crs("EPSG:4326") |
|
|
|
|
|
building_height_ft = round(building_height_m / 0.3048) |
|
buffer_distance_ft = round(buffer_distance_m / 0.3048) |
|
|
|
|
|
building_buffer_gdf['building_name'] = building_name |
|
building_buffer_gdf['building_height'] = building_height_ft |
|
building_buffer_gdf['buffer_distance'] = buffer_distance_ft |
|
|
|
buffers.append(building_buffer_gdf) |
|
|
|
|
|
intersects = gpd.overlay(building_buffer_gdf, sensitive_sites_gdf, how='intersection') |
|
|
|
if not intersects.empty: |
|
building_intersect_desc = f"Building {idx} ({building_name}), height: {building_height_ft}, buffer distance: {buffer_distance_ft} is in the vicinity of a sensitive site." |
|
intersected_sites.append(intersects) |
|
else: |
|
building_intersect_desc = f"Building {idx} ({building_name}), height: {building_height_ft}, buffer distance: {buffer_distance_ft} is not in the vicinity of any sensitive sites." |
|
|
|
if intersection_desc == "": |
|
intersection_desc = building_intersect_desc |
|
else: |
|
intersection_desc += "\n" + building_intersect_desc |
|
|
|
return buffers, intersected_sites, intersection_desc |
|
|
|
def get_max_extent(*gdfs): |
|
minx = min(gdf.total_bounds[0] for gdf in gdfs) |
|
miny = min(gdf.total_bounds[1] for gdf in gdfs) |
|
maxx = max(gdf.total_bounds[2] for gdf in gdfs) |
|
maxy = max(gdf.total_bounds[3] for gdf in gdfs) |
|
|
|
return minx, miny, maxx, maxy |
|
|
|
def create_plot(filename, extent, *gdfs): |
|
fig, ax = plt.subplots(1, 1) |
|
|
|
colors = ['blue', 'red', 'green', 'purple', 'orange', 'yellow'] |
|
|
|
for idx, gdf in enumerate(gdfs): |
|
gdf.plot(ax=ax, color=colors[idx % len(colors)]) |
|
|
|
ax.set_xlim(extent[0], extent[2]) |
|
ax.set_ylim(extent[1], extent[3]) |
|
|
|
plt.savefig(filename) |
|
|
|
def ss_intersect(geojson1, ss_geoselect, multiplier_factor, default_building_height): |
|
|
|
input_gdf = gpd.read_file(geojson1.name) |
|
|
|
|
|
if input_gdf.crs.to_epsg() != 4326: |
|
raise ValueError("Input GeoJSON files must be in CRS EPSG:4326") |
|
|
|
if ss_geoselect==0: |
|
sensitive_sites_gdf = gpd.read_file("sensitive_sites/NYC_Parks_Properties.geojson") |
|
else: |
|
sensitive_sites_gdf = gpd.read_file("sensitive_sites/NYC_Parks_Zones.geojson") |
|
|
|
default_building_height_m = default_building_height * 0.3048 |
|
|
|
buffers, intersected_sites, intersection_desc = process_buildings(input_gdf, sensitive_sites_gdf, default_building_height_m, multiplier_factor) |
|
|
|
|
|
buffers_gdf = pd.concat(buffers, ignore_index=True) |
|
buffers_gdf = buffers_gdf.to_crs("EPSG:4326") |
|
buffers_gdf.to_file("building_buffers.geojson", driver='GeoJSON') |
|
|
|
|
|
if intersected_sites: |
|
intersected_sites_gdf = pd.concat(intersected_sites, ignore_index=True) |
|
intersected_sites_gdf = intersected_sites_gdf.to_crs("EPSG:4326") |
|
intersected_sites_gdf.to_file("intersected_sensitive_sites.geojson", driver='GeoJSON') |
|
else: |
|
print("No buildings are in the vicinity of any sensitive sites.") |
|
|
|
|
|
if len(buffers) > 1: |
|
|
|
buffer_union = unary_union(buffers_gdf['geometry']) |
|
|
|
|
|
buffer_union_gdf = gpd.GeoDataFrame(geometry=[buffer_union], crs="EPSG:4326") |
|
|
|
|
|
buffer_union_gdf.to_file("buffer_union.geojson", driver='GeoJSON') |
|
|
|
|
|
|
|
extent = get_max_extent(input_gdf, buffers_gdf) |
|
|
|
|
|
create_plot('output_image.png', extent, input_gdf, intersected_sites_gdf, buffer_union_gdf) |
|
|
|
|
|
return 'output_image.png', "building_buffers.geojson", "buffer_union.geojson", intersection_desc |
|
|
|
|
|
iface = gr.Interface( |
|
fn=ss_intersect, |
|
inputs=[ |
|
gr.inputs.File(label="Building Footprints GeoJSON"), |
|
gr.Radio(["Parks Properties", "Park Zones"], label="Which Sensitive Sites?", info="From NYC DPR", type="index"), |
|
gr.inputs.Slider(minimum=0.0, maximum=10.0, default=4.3, label="Building Height Multiplier"), |
|
gr.inputs.Number(default=200, label="Default Building Height"), |
|
], |
|
outputs=[ |
|
gr.outputs.Image(type="pil", label="Result Image"), |
|
gr.outputs.File(label="Building Buffers"), |
|
gr.outputs.File(label="Union of Building Buffers"), |
|
gr.outputs.Textbox(label="Building and Sensitive Site Vicinities"), |
|
], |
|
examples=[ |
|
["files/building4test.geojson", "Parks Properties", 4.3, 200], |
|
], |
|
title="Shadow Proximity", |
|
description="Upload proposed building footprints in a GeoJSON file and select a numeric value to get the building proximity prediction.", |
|
) |
|
|
|
iface.launch() |