python modularize attempt 2
Browse files
app.py
CHANGED
@@ -1,74 +1,9 @@
|
|
1 |
import gradio as gr
|
2 |
-
|
3 |
-
import pandas as pd
|
4 |
-
import os
|
5 |
-
from shapely.ops import unary_union
|
6 |
#from datasets import load_dataset
|
7 |
|
8 |
#ds = load_dataset('psalama/NYC_sensitive_sites', data_files=data_files)
|
9 |
|
10 |
-
# Import functions from modules in data.py and plot.py
|
11 |
-
from data import get_gdf_from_feature_layer, process_buildings, get_max_extent
|
12 |
-
from plot import create_plot
|
13 |
-
|
14 |
-
|
15 |
-
def ss_intersect(geojson1, ss_geoselect, multiplier_factor, default_building_height):
|
16 |
-
# Read the GeoJSON files
|
17 |
-
input_gdf = gpd.read_file(geojson1.name)
|
18 |
-
|
19 |
-
# Check that CRS is EPSG:4326
|
20 |
-
if input_gdf.crs.to_epsg() != 4326:
|
21 |
-
raise ValueError("Input GeoJSON files must be in CRS EPSG:4326")
|
22 |
-
|
23 |
-
if ss_geoselect==0:
|
24 |
-
sensitive_sites_gdf = gpd.read_file("sensitive_sites/NYC_Parks_Properties.geojson")
|
25 |
-
else:
|
26 |
-
sensitive_sites_gdf = gpd.read_file("sensitive_sites/NYC_Parks_Zones.geojson")
|
27 |
-
|
28 |
-
default_building_height_m = default_building_height * 0.3048
|
29 |
-
|
30 |
-
buffers, intersected_sites, intersection_desc = process_buildings(input_gdf, sensitive_sites_gdf, default_building_height_m, multiplier_factor)
|
31 |
-
|
32 |
-
# Concatenate all buffer GeoDataFrames and save as a GeoJSON file
|
33 |
-
buffers_gdf = pd.concat(buffers, ignore_index=True)
|
34 |
-
buffers_gdf = buffers_gdf.to_crs("EPSG:4326")
|
35 |
-
buffers_gdf.to_file("building_buffers.geojson", driver='GeoJSON')
|
36 |
-
|
37 |
-
# Concatenate all intersected sensitive sites and save as a GeoJSON file
|
38 |
-
if intersected_sites:
|
39 |
-
intersected_sites_gdf = pd.concat(intersected_sites, ignore_index=True)
|
40 |
-
intersected_sites_gdf = intersected_sites_gdf.to_crs("EPSG:4326")
|
41 |
-
else: #if there aren't any intersections, return an empty geojson
|
42 |
-
intersected_sites_gdf = gpd.read_file("files/No_intersecting_buildings.geojson")
|
43 |
-
print("No buildings are in the vicinity of any sensitive sites.")
|
44 |
-
|
45 |
-
intersected_sites_gdf.to_file("intersected_sensitive_sites.geojson", driver='GeoJSON')
|
46 |
-
|
47 |
-
# Perform the union operation if there is more than one buffer
|
48 |
-
if len(buffers) > 1:
|
49 |
-
# Perform a unary union on the geometry column of the GeoDataFrame
|
50 |
-
buffer_union = unary_union(buffers_gdf['geometry'])
|
51 |
-
|
52 |
-
# Create a new GeoDataFrame from the union result
|
53 |
-
buffer_union_gdf = gpd.GeoDataFrame(geometry=[buffer_union], crs="EPSG:4326")
|
54 |
-
|
55 |
-
# Save the union GeoDataFrame as a GeoJSON file
|
56 |
-
buffer_union_gdf.to_file("buffer_union.geojson", driver='GeoJSON')
|
57 |
-
|
58 |
-
|
59 |
-
# Calculate the maximum extent
|
60 |
-
extent = get_max_extent(input_gdf, buffers_gdf)
|
61 |
-
|
62 |
-
lots_url = "https://services5.arcgis.com/GfwWNkhOj9bNBqoJ/arcgis/rest/services/MAPPLUTO/FeatureServer/0" # Access MapPLUTO # Eventually should be a checkbox
|
63 |
-
lots_gdf = get_gdf_from_feature_layer(lots_url)
|
64 |
-
|
65 |
-
# Create and save the plot - which is the output image
|
66 |
-
create_plot('output_image.png', extent, lots_gdf, sensitive_sites_gdf, buffer_union_gdf, intersected_sites_gdf, input_gdf)
|
67 |
-
|
68 |
-
# Return the image, geojson files, and text description
|
69 |
-
return 'output_image.png', "building_buffers.geojson", "buffer_union.geojson", intersection_desc
|
70 |
-
|
71 |
-
|
72 |
iface = gr.Interface(
|
73 |
fn=ss_intersect,
|
74 |
inputs=[
|
|
|
1 |
import gradio as gr
|
2 |
+
from .main_operations import ss_intersect
|
|
|
|
|
|
|
3 |
#from datasets import load_dataset
|
4 |
|
5 |
#ds = load_dataset('psalama/NYC_sensitive_sites', data_files=data_files)
|
6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
iface = gr.Interface(
|
8 |
fn=ss_intersect,
|
9 |
inputs=[
|