rishab1090 commited on
Commit
29d8459
·
verified ·
1 Parent(s): e8423eb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -29
app.py CHANGED
@@ -2,6 +2,7 @@ import gradio as gr
2
  import rasterio
3
  import numpy as np
4
  import matplotlib.pyplot as plt
 
5
 
6
  def process_dem(dem_path):
7
  # --- OPEN DEM ---
@@ -25,7 +26,7 @@ def process_dem(dem_path):
25
  threshold = np.percentile(slope, 95) # Top 5% steepest slopes
26
  risk_mask = slope > threshold
27
 
28
- # --- RISK MAP (2D) ---
29
  fig2d, ax = plt.subplots(figsize=(8, 6))
30
  c = ax.imshow(slope, cmap="hot", extent=[x.min(), x.max(), y.min(), y.max()], origin="upper")
31
  ax.contour(risk_mask, levels=[0.5], colors="blue", linewidths=0.8,
@@ -38,36 +39,50 @@ def process_dem(dem_path):
38
  plt.savefig(risk_map_path, dpi=150, bbox_inches="tight")
39
  plt.close(fig2d)
40
 
41
- # --- 3D DEM (Matplotlib as original) ---
42
- fig3d = plt.figure(figsize=(12, 9))
43
- ax = fig3d.add_subplot(111, projection="3d")
 
 
 
 
 
 
 
 
 
 
 
 
44
 
45
- step = max(1, nrows // 300) # downsample for speed
46
- # Base DEM surface
47
- surf = ax.plot_surface(
48
- X[::step, ::step], Y[::step, ::step], Z[::step, ::step],
49
- cmap="terrain", linewidth=0, alpha=0.9
50
- )
51
- # Contours
52
- ax.contour(
53
- X[::step, ::step], Y[::step, ::step], Z[::step, ::step],
54
- levels=20, colors="k", linewidths=0.4, offset=np.nanmin(Z)
55
- )
56
  # Risk overlay (purple)
57
- ax.plot_surface(
58
- X[::step, ::step], Y[::step, ::step],
59
- np.where(risk_mask[::step, ::step], Z[::step, ::step], np.nan),
60
- color="purple", alpha=0.6, zorder=10
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
  )
62
- ax.set_xlabel("Longitude")
63
- ax.set_ylabel("Latitude")
64
- ax.set_zlabel("Elevation (m)")
65
- plt.title("3D DEM with Contours & Steep Slope Highlight")
66
- dem3d_path = "dem3d.png"
67
- plt.savefig(dem3d_path, dpi=150, bbox_inches="tight")
68
- plt.close(fig3d)
69
 
70
- return risk_map_path, dem3d_path
71
 
72
 
73
  demo = gr.Interface(
@@ -75,10 +90,10 @@ demo = gr.Interface(
75
  inputs=gr.File(label="Upload DEM (.tif)", file_types=[".tif"]),
76
  outputs=[
77
  gr.Image(type="filepath", label="2D Slope Risk Map"),
78
- gr.Image(type="filepath", label="3D DEM Visualization")
79
  ],
80
  title="3D DEM & Landslide Risk Visualizer",
81
- description="Upload a GeoTIFF DEM file to see a 2D slope risk map and a static 3D DEM with contours & steep slope zones highlighted."
82
  )
83
 
84
  if __name__ == "__main__":
 
2
  import rasterio
3
  import numpy as np
4
  import matplotlib.pyplot as plt
5
+ import plotly.graph_objs as go
6
 
7
  def process_dem(dem_path):
8
  # --- OPEN DEM ---
 
26
  threshold = np.percentile(slope, 95) # Top 5% steepest slopes
27
  risk_mask = slope > threshold
28
 
29
+ # --- 2D RISK MAP ---
30
  fig2d, ax = plt.subplots(figsize=(8, 6))
31
  c = ax.imshow(slope, cmap="hot", extent=[x.min(), x.max(), y.min(), y.max()], origin="upper")
32
  ax.contour(risk_mask, levels=[0.5], colors="blue", linewidths=0.8,
 
39
  plt.savefig(risk_map_path, dpi=150, bbox_inches="tight")
40
  plt.close(fig2d)
41
 
42
+ # --- INTERACTIVE 3D DEM (Plotly, fixed aspect ratio) ---
43
+ step = max(1, nrows // 200)
44
+
45
+ fig3d = go.Figure()
46
+
47
+ # Base DEM
48
+ fig3d.add_trace(go.Surface(
49
+ z=Z[::step, ::step],
50
+ x=X[::step, ::step],
51
+ y=Y[::step, ::step],
52
+ colorscale="Earth",
53
+ showscale=True,
54
+ opacity=0.9,
55
+ contours=dict(z=dict(show=True, usecolormap=True, highlightcolor="black", project_z=True))
56
+ ))
57
 
 
 
 
 
 
 
 
 
 
 
 
58
  # Risk overlay (purple)
59
+ fig3d.add_trace(go.Surface(
60
+ z=np.where(risk_mask[::step, ::step], Z[::step, ::step], np.nan),
61
+ x=X[::step, ::step],
62
+ y=Y[::step, ::step],
63
+ surfacecolor=np.ones_like(Z[::step, ::step]),
64
+ colorscale=[[0, "purple"], [1, "purple"]],
65
+ showscale=False,
66
+ opacity=0.6
67
+ ))
68
+
69
+ # --- FIX DISTORTION: Set aspect ratio ---
70
+ xrange = X.max() - X.min()
71
+ yrange = Y.max() - Y.min()
72
+ zrange = Z.max() - Z.min()
73
+ # Scale so Z looks correct compared to XY
74
+ fig3d.update_layout(
75
+ title="Interactive 3D DEM with Contours & Steep Slope Highlight",
76
+ scene=dict(
77
+ xaxis_title="Longitude",
78
+ yaxis_title="Latitude",
79
+ zaxis_title="Elevation (m)",
80
+ aspectmode="manual",
81
+ aspectratio=dict(x=xrange/yrange, y=1, z=zrange/yrange*2) # tweak multiplier if Z looks too flat/steep
82
+ )
83
  )
 
 
 
 
 
 
 
84
 
85
+ return risk_map_path, fig3d
86
 
87
 
88
  demo = gr.Interface(
 
90
  inputs=gr.File(label="Upload DEM (.tif)", file_types=[".tif"]),
91
  outputs=[
92
  gr.Image(type="filepath", label="2D Slope Risk Map"),
93
+ gr.Plot(label="Interactive 3D DEM (Correct Aspect)")
94
  ],
95
  title="3D DEM & Landslide Risk Visualizer",
96
+ description="Upload a GeoTIFF DEM file to see a 2D slope risk map and an interactive 3D DEM with contours & steep slope zones highlighted."
97
  )
98
 
99
  if __name__ == "__main__":