python modularize attempt 2
Browse files- geospatial_operations.py +86 -0
- plot.py +0 -19
geospatial_operations.py
ADDED
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pandas as pd
|
2 |
+
import geopandas as gpd
|
3 |
+
import matplotlib.pyplot as plt
|
4 |
+
from shapely.ops import unary_union
|
5 |
+
|
6 |
+
def process_buildings(input_gdf, sensitive_sites_gdf, default_building_height_m, multiplier_factor):
|
7 |
+
# List to store all intersected sensitive sites
|
8 |
+
intersected_sites = []
|
9 |
+
|
10 |
+
# List to store all buffers
|
11 |
+
buffers = []
|
12 |
+
|
13 |
+
intersection_desc = ""
|
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 |
+
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."
|
53 |
+
intersected_sites.append(intersects)
|
54 |
+
else:
|
55 |
+
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."
|
56 |
+
|
57 |
+
if intersection_desc == "":
|
58 |
+
intersection_desc = building_intersect_desc
|
59 |
+
else:
|
60 |
+
intersection_desc += "\n" + building_intersect_desc
|
61 |
+
|
62 |
+
return buffers, intersected_sites, intersection_desc
|
63 |
+
|
64 |
+
def get_max_extent(*gdfs): # takes in unlimited number of gdfs and calculates max/min xy extents
|
65 |
+
minx = min(gdf.total_bounds[0] for gdf in gdfs)
|
66 |
+
miny = min(gdf.total_bounds[1] for gdf in gdfs)
|
67 |
+
maxx = max(gdf.total_bounds[2] for gdf in gdfs)
|
68 |
+
maxy = max(gdf.total_bounds[3] for gdf in gdfs)
|
69 |
+
|
70 |
+
return minx, miny, maxx, maxy
|
71 |
+
|
72 |
+
def create_plot(filename, extent, *gdfs): # takes in unlimited number of gdfs
|
73 |
+
fig, ax = plt.subplots(figsize=(10, 8)) #Sets image size by width & height (in inches)
|
74 |
+
|
75 |
+
colors = ['tan', 'mediumseagreen', 'thistle', 'lightcoral', 'sienna', 'yellow'] # Extend/improve this list as needed
|
76 |
+
|
77 |
+
for idx, gdf in enumerate(gdfs):
|
78 |
+
gdf.plot(ax=ax, color=colors[idx % len(colors)]) # Cycle through colors
|
79 |
+
|
80 |
+
ax.set_xlim(extent[0], extent[2])
|
81 |
+
ax.set_ylim(extent[1], extent[3])
|
82 |
+
|
83 |
+
# Hide axes
|
84 |
+
ax.axis('off')
|
85 |
+
|
86 |
+
plt.savefig(filename, bbox_inches='tight', pad_inches=0) # remove padding
|
plot.py
DELETED
@@ -1,19 +0,0 @@
|
|
1 |
-
import matplotlib.pyplot as plt
|
2 |
-
import geopandas as gpd
|
3 |
-
|
4 |
-
# all plotting functions
|
5 |
-
def create_plot(filename, extent, *gdfs): # takes in unlimited number of gdfs
|
6 |
-
fig, ax = plt.subplots(figsize=(10, 8)) #Sets image size by width & height (in inches)
|
7 |
-
|
8 |
-
colors = ['tan', 'mediumseagreen', 'thistle', 'lightcoral', 'sienna', 'yellow'] # Extend/improve this list as needed
|
9 |
-
|
10 |
-
for idx, gdf in enumerate(gdfs):
|
11 |
-
gdf.plot(ax=ax, color=colors[idx % len(colors)]) # Cycle through colors
|
12 |
-
|
13 |
-
ax.set_xlim(extent[0], extent[2])
|
14 |
-
ax.set_ylim(extent[1], extent[3])
|
15 |
-
|
16 |
-
# Hide axes
|
17 |
-
ax.axis('off')
|
18 |
-
|
19 |
-
plt.savefig(filename, bbox_inches='tight', pad_inches=0) # remove padding
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|