rishab1090 commited on
Commit
2738fe4
·
verified ·
1 Parent(s): f1d1d90

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -26
app.py CHANGED
@@ -26,42 +26,50 @@ def process_dem(dem_path):
26
  threshold = np.percentile(slope, 95) # Top 5% steepest slopes
27
  risk_mask = slope > threshold
28
 
29
- # --- RISK MAP (2D) ---
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, extent=[x.min(), x.max(), y.min(), y.max()])
 
33
  plt.colorbar(c, ax=ax, label="Slope (steepness)")
34
- ax.set_title("Slope Risk Map (Hot colors = Steeper slopes, Blue = Risk zones)")
35
  ax.set_xlabel("Longitude")
36
  ax.set_ylabel("Latitude")
37
  risk_map_path = "risk_map.png"
38
  plt.savefig(risk_map_path, dpi=150, bbox_inches="tight")
39
  plt.close(fig2d)
40
 
41
- # --- INTERACTIVE 3D MODEL (Plotly) ---
42
- step = max(1, nrows // 200) # downsample for speed
43
- fig3d = go.Figure(data=[
44
- go.Surface(
45
- z=Z[::step, ::step],
46
- x=X[::step, ::step],
47
- y=Y[::step, ::step],
48
- colorscale="Earth",
49
- showscale=True,
50
- opacity=0.9
51
- ),
52
- go.Surface(
53
- z=np.where(risk_mask[::step, ::step], Z[::step, ::step], np.nan),
54
- x=X[::step, ::step],
55
- y=Y[::step, ::step],
56
- surfacecolor=np.ones_like(Z[::step, ::step]),
57
- colorscale=[[0, "purple"], [1, "purple"]],
58
- showscale=False,
59
- opacity=0.6
60
  )
61
- ])
 
 
 
 
 
 
 
 
 
 
 
62
 
63
  fig3d.update_layout(
64
- title="Interactive 3D DEM with Landslide Risk Zones",
65
  scene=dict(
66
  xaxis_title="Longitude",
67
  yaxis_title="Latitude",
@@ -78,11 +86,12 @@ demo = gr.Interface(
78
  inputs=gr.File(label="Upload DEM (.tif)", file_types=[".tif"]),
79
  outputs=[
80
  gr.Image(type="filepath", label="2D Slope Risk Map"),
81
- gr.Plot(label="Interactive 3D DEM")
82
  ],
83
  title="3D DEM & Landslide Risk Visualizer",
84
- description="Upload a GeoTIFF DEM file to see a 2D slope risk map and an interactive 3D terrain with steep slope zones highlighted."
85
  )
86
 
87
  if __name__ == "__main__":
88
  demo.launch()
 
 
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,
33
+ extent=[x.min(), x.max(), y.min(), y.max()])
34
  plt.colorbar(c, ax=ax, label="Slope (steepness)")
35
+ ax.set_title("Slope Risk Map (Hot = Steep, Blue = Risk zones)")
36
  ax.set_xlabel("Longitude")
37
  ax.set_ylabel("Latitude")
38
  risk_map_path = "risk_map.png"
39
  plt.savefig(risk_map_path, dpi=150, bbox_inches="tight")
40
  plt.close(fig2d)
41
 
42
+ # --- INTERACTIVE 3D DEM (Plotly version of Matplotlib style) ---
43
+ step = max(1, nrows // 200)
44
+
45
+ fig3d = go.Figure()
46
+
47
+ # Base DEM surface
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(
56
+ z=dict(show=True, usecolormap=True, highlightcolor="black", project_z=True)
 
 
 
 
57
  )
58
+ ))
59
+
60
+ # Risk overlay (purple)
61
+ fig3d.add_trace(go.Surface(
62
+ z=np.where(risk_mask[::step, ::step], Z[::step, ::step], np.nan),
63
+ x=X[::step, ::step],
64
+ y=Y[::step, ::step],
65
+ surfacecolor=np.ones_like(Z[::step, ::step]),
66
+ colorscale=[[0, "purple"], [1, "purple"]],
67
+ showscale=False,
68
+ opacity=0.6
69
+ ))
70
 
71
  fig3d.update_layout(
72
+ title="Interactive 3D DEM with Contours & Steep Slope Highlight",
73
  scene=dict(
74
  xaxis_title="Longitude",
75
  yaxis_title="Latitude",
 
86
  inputs=gr.File(label="Upload DEM (.tif)", file_types=[".tif"]),
87
  outputs=[
88
  gr.Image(type="filepath", label="2D Slope Risk Map"),
89
+ gr.Plot(label="Interactive 3D DEM (Matplotlib Style)")
90
  ],
91
  title="3D DEM & Landslide Risk Visualizer",
92
+ description="Upload a GeoTIFF DEM file to see a 2D slope risk map and an interactive 3D DEM with contours & steep slope zones highlighted."
93
  )
94
 
95
  if __name__ == "__main__":
96
  demo.launch()
97
+