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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -16
app.py CHANGED
@@ -3,22 +3,31 @@ 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 ---
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 ---
21
- threshold = np.percentile(slope, 95) # Top 5% steepest slopes
22
  risk_mask = slope > threshold
23
 
24
  # --- 2D RISK MAP ---
@@ -37,17 +46,13 @@ def process_dem(dem_path):
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))
48
  ))
49
-
50
- # Risk overlay (purple)
51
  fig3d.add_trace(go.Surface(
52
  z=np.where(risk_mask[::step, ::step], Z[::step, ::step], np.nan),
53
  surfacecolor=np.ones_like(Z[::step, ::step]),
@@ -55,7 +60,6 @@ def process_dem(dem_path):
55
  showscale=False,
56
  opacity=0.6
57
  ))
58
-
59
  fig3d.update_layout(
60
  title="Interactive 3D DEM with Contours & Steep Slope Highlight",
61
  scene=dict(
@@ -68,17 +72,15 @@ def process_dem(dem_path):
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__":
 
3
  import numpy as np
4
  import matplotlib.pyplot as plt
5
  import plotly.graph_objs as go
6
+ from gradio.exceptions import Error
7
+ from rasterio.errors import RasterioIOError
8
 
9
+ def process_dem(dem_file):
10
+ # dem_file is a tempfile-like object from Gradio
11
+ path = dem_file.name
12
+
13
+ # Validate that it’s a GeoTIFF using rasterio (more robust than extension)
14
+ try:
15
+ with rasterio.open(path) as src:
16
+ if src.driver not in ("GTiff", "COG"):
17
+ raise Error(f"Unsupported raster driver: {src.driver}. Please upload a GeoTIFF (.tif/.tiff).")
18
+ dem = src.read(1).astype(float)
19
+ except RasterioIOError as e:
20
+ raise Error(f"Could not read the file as a GeoTIFF. {e}")
21
 
22
  nrows, ncols = dem.shape
23
  Z = dem
24
 
25
  # --- COMPUTE SLOPE ---
 
26
  dy, dx = np.gradient(Z)
27
  slope = np.sqrt(dx**2 + dy**2)
28
 
29
  # --- RISK MASK ---
30
+ threshold = np.percentile(slope, 95)
31
  risk_mask = slope > threshold
32
 
33
  # --- 2D RISK MAP ---
 
46
  step = max(1, nrows // 200)
47
 
48
  fig3d = go.Figure()
 
 
49
  fig3d.add_trace(go.Surface(
50
  z=Z[::step, ::step],
51
+ colorscale="Earth",
52
  showscale=True,
53
  opacity=0.9,
54
  contours=dict(z=dict(show=True, usecolormap=True, highlightcolor="black", project_z=True))
55
  ))
 
 
56
  fig3d.add_trace(go.Surface(
57
  z=np.where(risk_mask[::step, ::step], Z[::step, ::step], np.nan),
58
  surfacecolor=np.ones_like(Z[::step, ::step]),
 
60
  showscale=False,
61
  opacity=0.6
62
  ))
 
63
  fig3d.update_layout(
64
  title="Interactive 3D DEM with Contours & Steep Slope Highlight",
65
  scene=dict(
 
72
 
73
  return risk_map_path, fig3d
74
 
 
 
75
  demo = gr.Interface(
76
  fn=process_dem,
77
+ inputs=gr.File(label="Upload DEM (.tif or .tiff)", file_types=[".tif", ".tiff"]), # <-- allow both
78
  outputs=[
79
  gr.Image(type="filepath", label="2D Slope Risk Map"),
80
  gr.Plot(label="Interactive 3D DEM (with Contours & Risk Zones)")
81
  ],
82
  title="3D DEM & Landslide Risk Visualizer",
83
+ description="Upload a GeoTIFF DEM file to see a 2D slope risk map and an interactive 3D DEM."
84
  )
85
 
86
  if __name__ == "__main__":