rishab1090 commited on
Commit
de31e8b
·
verified ·
1 Parent(s): e5f5311

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -16
app.py CHANGED
@@ -8,18 +8,13 @@ def process_dem(dem_path):
8
  # --- OPEN DEM ---
9
  with rasterio.open(dem_path.name) as src:
10
  dem = src.read(1).astype(float)
11
- transform = src.transform
12
- res_x, res_y = transform.a, -transform.e # pixel size
13
 
14
- # --- BUILD GRID ---
15
  nrows, ncols = dem.shape
16
- x = np.arange(ncols) * res_x + transform.c
17
- y = np.arange(nrows) * res_y + transform.f
18
- X, Y = np.meshgrid(x, y)
19
  Z = dem
20
 
21
  # --- COMPUTE SLOPE ---
22
- dy, dx = np.gradient(Z, res_y, res_x)
 
23
  slope = np.sqrt(dx**2 + dy**2)
24
 
25
  # --- RISK MASK ---
@@ -28,26 +23,25 @@ def process_dem(dem_path):
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 (Simplified, works like Matplotlib) ---
43
  step = max(1, nrows // 200)
44
 
45
  fig3d = go.Figure()
46
 
47
- # Base DEM (terrain)
48
  fig3d.add_trace(go.Surface(
49
  z=Z[::step, ::step],
50
- colorscale="Earth",
51
  showscale=True,
52
  opacity=0.9,
53
  contours=dict(z=dict(show=True, usecolormap=True, highlightcolor="black", project_z=True))
@@ -62,7 +56,6 @@ def process_dem(dem_path):
62
  opacity=0.6
63
  ))
64
 
65
- # Fix proportions
66
  fig3d.update_layout(
67
  title="Interactive 3D DEM with Contours & Steep Slope Highlight",
68
  scene=dict(
@@ -73,5 +66,20 @@ def process_dem(dem_path):
73
  )
74
  )
75
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76
  if __name__ == "__main__":
77
  demo.launch()
 
8
  # --- OPEN DEM ---
9
  with rasterio.open(dem_path.name) as src:
10
  dem = src.read(1).astype(float)
 
 
11
 
 
12
  nrows, ncols = dem.shape
 
 
 
13
  Z = dem
14
 
15
  # --- COMPUTE SLOPE ---
16
+ # Assuming square pixels (ok for visualization)
17
+ dy, dx = np.gradient(Z)
18
  slope = np.sqrt(dx**2 + dy**2)
19
 
20
  # --- RISK MASK ---
 
23
 
24
  # --- 2D RISK MAP ---
25
  fig2d, ax = plt.subplots(figsize=(8, 6))
26
+ c = ax.imshow(slope, cmap="hot", origin="upper")
27
+ ax.contour(risk_mask, levels=[0.5], colors="blue", linewidths=0.8)
 
28
  plt.colorbar(c, ax=ax, label="Slope (steepness)")
29
  ax.set_title("Slope Risk Map (Hot = Steep, Blue = Risk zones)")
30
+ ax.set_xlabel("Column Index (X)")
31
+ ax.set_ylabel("Row Index (Y)")
32
  risk_map_path = "risk_map.png"
33
  plt.savefig(risk_map_path, dpi=150, bbox_inches="tight")
34
  plt.close(fig2d)
35
 
36
+ # --- INTERACTIVE 3D DEM (Plotly) ---
37
  step = max(1, nrows // 200)
38
 
39
  fig3d = go.Figure()
40
 
41
+ # Base DEM surface
42
  fig3d.add_trace(go.Surface(
43
  z=Z[::step, ::step],
44
+ colorscale="Earth", # similar to matplotlib 'terrain'
45
  showscale=True,
46
  opacity=0.9,
47
  contours=dict(z=dict(show=True, usecolormap=True, highlightcolor="black", project_z=True))
 
56
  opacity=0.6
57
  ))
58
 
 
59
  fig3d.update_layout(
60
  title="Interactive 3D DEM with Contours & Steep Slope Highlight",
61
  scene=dict(
 
66
  )
67
  )
68
 
69
+ return risk_map_path, fig3d
70
+
71
+
72
+ # --- GRADIO APP ---
73
+ demo = gr.Interface(
74
+ fn=process_dem,
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.Plot(label="Interactive 3D DEM (with Contours & Risk Zones)")
79
+ ],
80
+ title="3D DEM & Landslide Risk Visualizer",
81
+ description="Upload a GeoTIFF DEM file to see a 2D slope risk map and an interactive 3D DEM with contours & steep slope zones highlighted."
82
+ )
83
+
84
  if __name__ == "__main__":
85
  demo.launch()