Update app.py
Browse files
app.py
CHANGED
@@ -1,27 +1,124 @@
|
|
1 |
import gradio as gr
|
2 |
import geopandas as gpd
|
3 |
from shapely.geometry import shape
|
|
|
4 |
|
5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
# Read the GeoJSON files
|
7 |
-
|
8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
-
#
|
11 |
-
|
|
|
|
|
12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
# Return the image
|
14 |
-
return output_image
|
|
|
15 |
|
16 |
iface = gr.Interface(
|
17 |
-
fn=
|
18 |
inputs=[
|
19 |
-
gr.inputs.File(label="
|
20 |
-
gr.
|
|
|
21 |
gr.inputs.Slider(minimum=0.0, maximum=10.0, default=4.3, label="Building Height Multiplier"),
|
22 |
gr.inputs.Number(default=200, label="Default Building Height"), #Can I make this optional?
|
23 |
],
|
24 |
-
outputs=
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
title="Shadow Proximity",
|
26 |
description="Upload proposed building footprints in a GeoJSON file and select a numeric value to get the building proximity prediction.",
|
27 |
)
|
|
|
1 |
import gradio as gr
|
2 |
import geopandas as gpd
|
3 |
from shapely.geometry import shape
|
4 |
+
#from datasets import load_dataset
|
5 |
|
6 |
+
#ds = load_dataset('psalama/NYC_sensitive_sites', data_files=data_files)
|
7 |
+
|
8 |
+
def process_buildings(input_gdf, sensitive_sites_gdf, default_building_height_m, multiplier_factor):
|
9 |
+
# List to store all intersected sensitive sites
|
10 |
+
intersected_sites = []
|
11 |
+
|
12 |
+
# List to store all buffers
|
13 |
+
buffers = []
|
14 |
+
|
15 |
+
# Iterate over each building in the input file
|
16 |
+
for idx, building in input_gdf.iterrows():
|
17 |
+
building_name = building.get('building_name', 'Unnamed building')
|
18 |
+
|
19 |
+
# If the 'building_height' field exists and its value is not null or zero for this building,
|
20 |
+
# use it as the building height. Otherwise, use the default building height provided by the user.
|
21 |
+
if 'building_height' in building and pd.notnull(building['building_height']) and building['building_height'] != 0:
|
22 |
+
building_height_m = building['building_height'] * 0.3048
|
23 |
+
else:
|
24 |
+
building_height_m = default_building_height_m
|
25 |
+
|
26 |
+
buffer_distance_m = building_height_m * multiplier_factor
|
27 |
+
|
28 |
+
# Convert building's geometry to EPSG:3857 for accurate meter-based distance measurement
|
29 |
+
building_geometry = gpd.GeoSeries([building['geometry']], crs="EPSG:4326")
|
30 |
+
building_geometry_m = building_geometry.to_crs("EPSG:3857")
|
31 |
+
|
32 |
+
# Create a buffer around the building and convert it to a GeoDataFrame
|
33 |
+
building_buffer = building_geometry_m.buffer(buffer_distance_m)
|
34 |
+
building_buffer_gdf = gpd.GeoDataFrame(geometry=building_buffer, crs="EPSG:3857")
|
35 |
+
building_buffer_gdf = building_buffer_gdf.to_crs("EPSG:4326")
|
36 |
+
|
37 |
+
# Convert back to feet for storing and printing, rounding to the nearest foot
|
38 |
+
building_height_ft = round(building_height_m / 0.3048)
|
39 |
+
buffer_distance_ft = round(buffer_distance_m / 0.3048)
|
40 |
+
|
41 |
+
# Assign additional attributes
|
42 |
+
building_buffer_gdf['building_name'] = building_name
|
43 |
+
building_buffer_gdf['building_height'] = building_height_ft
|
44 |
+
building_buffer_gdf['buffer_distance'] = buffer_distance_ft
|
45 |
+
|
46 |
+
buffers.append(building_buffer_gdf)
|
47 |
+
|
48 |
+
# Check if the buffer intersects with any sensitive sites
|
49 |
+
intersects = gpd.overlay(building_buffer_gdf, sensitive_sites_gdf, how='intersection')
|
50 |
+
|
51 |
+
if not intersects.empty:
|
52 |
+
print(f"Building {idx} ({building_name}), height: {building_height_ft}, buffer distance: {buffer_distance_ft} is in the vicinity of a sensitive site.")
|
53 |
+
intersected_sites.append(intersects)
|
54 |
+
else:
|
55 |
+
print(f"Building {idx} ({building_name}), height: {building_height_ft}, buffer distance: {buffer_distance_ft} is not in the vicinity of any sensitive sites.")
|
56 |
+
|
57 |
+
return buffers, intersected_sites
|
58 |
+
|
59 |
+
|
60 |
+
def ss_intersect(geojson1, ss_geoselect, multiplier_factor, default_building_height): #function should be renamed!
|
61 |
# Read the GeoJSON files
|
62 |
+
input_gdf = gpd.read_file(geojson1.name)
|
63 |
+
|
64 |
+
# Check that CRS is EPSG:4326
|
65 |
+
if input_gdf.crs.to_epsg() != 4326 or sensitive_sites_gdf.crs.to_epsg() != 4326:
|
66 |
+
raise ValueError("Input GeoJSON files must be in CRS EPSG:4326")
|
67 |
+
|
68 |
+
if ss_geoselect==0:
|
69 |
+
sensitive_sites_gdf = gpd.read_file("sensitive_sites/NYC_Parks_Properties.geojson")
|
70 |
+
else:
|
71 |
+
sensitive_sites_gdf = gpd.read_file("sensitive_sites/NYC_Parks_Zones.geojson")
|
72 |
+
|
73 |
+
default_building_height_m = default_building_height * 0.3048
|
74 |
+
|
75 |
+
buffers, intersected_sites = process_buildings(input_gdf, sensitive_sites_gdf, default_building_height_m, multiplier_factor)
|
76 |
|
77 |
+
# Concatenate all buffer GeoDataFrames and save as a GeoJSON file
|
78 |
+
buffers_gdf = pd.concat(buffers, ignore_index=True)
|
79 |
+
buffers_gdf = buffers_gdf.to_crs("EPSG:4326")
|
80 |
+
buffers_gdf.to_file("building_buffers.geojson", driver='GeoJSON')
|
81 |
|
82 |
+
# Concatenate all intersected sensitive sites and save as a GeoJSON file
|
83 |
+
if intersected_sites:
|
84 |
+
intersected_sites_gdf = pd.concat(intersected_sites, ignore_index=True)
|
85 |
+
intersected_sites_gdf = intersected_sites_gdf.to_crs("EPSG:4326")
|
86 |
+
intersected_sites_gdf.to_file("intersected_sensitive_sites.geojson", driver='GeoJSON')
|
87 |
+
else:
|
88 |
+
print("No buildings are in the vicinity of any sensitive sites.")
|
89 |
+
|
90 |
+
# Perform the union operation if there is more than one buffer
|
91 |
+
if len(buffers) > 1:
|
92 |
+
# Perform a unary union on the geometry column of the GeoDataFrame
|
93 |
+
buffer_union = unary_union(buffers_gdf['geometry'])
|
94 |
+
|
95 |
+
# Create a new GeoDataFrame from the union result
|
96 |
+
buffer_union_gdf = gpd.GeoDataFrame(geometry=[buffer_union], crs="EPSG:4326")
|
97 |
+
|
98 |
+
# Save the union GeoDataFrame as a GeoJSON file
|
99 |
+
buffer_union_gdf.to_file("buffer_union.geojson", driver='GeoJSON')
|
100 |
+
|
101 |
+
|
102 |
# Return the image
|
103 |
+
return output_image, building_vicinity
|
104 |
+
|
105 |
|
106 |
iface = gr.Interface(
|
107 |
+
fn=ss_intersect,
|
108 |
inputs=[
|
109 |
+
gr.inputs.File(label="Building Footprints GeoJSON"),
|
110 |
+
gr.Radio(["Parks Properties", "Park Zones"], label="Which Sensitive Sites?", info="From NYC DPR", type="index"),
|
111 |
+
#gr.inputs.File(label="Sensitive Sites GeoJSON"), #Replaced by radio button above
|
112 |
gr.inputs.Slider(minimum=0.0, maximum=10.0, default=4.3, label="Building Height Multiplier"),
|
113 |
gr.inputs.Number(default=200, label="Default Building Height"), #Can I make this optional?
|
114 |
],
|
115 |
+
outputs=[
|
116 |
+
gr.outputs.File(label="Intersecting Buildings"),
|
117 |
+
gr.outputs.Textbox(label="Building and Sensitive Site Vicinities"),
|
118 |
+
]
|
119 |
+
examples=[
|
120 |
+
["files/building4test.geojson", "Parks Properties", 4.3, 200],
|
121 |
+
],
|
122 |
title="Shadow Proximity",
|
123 |
description="Upload proposed building footprints in a GeoJSON file and select a numeric value to get the building proximity prediction.",
|
124 |
)
|