Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -39,16 +39,14 @@ def process_dem(dem_path):
|
|
| 39 |
plt.savefig(risk_map_path, dpi=150, bbox_inches="tight")
|
| 40 |
plt.close(fig2d)
|
| 41 |
|
| 42 |
-
|
| 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,
|
|
@@ -58,43 +56,22 @@ def process_dem(dem_path):
|
|
| 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 |
-
#
|
| 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="
|
| 78 |
-
yaxis_title="
|
| 79 |
zaxis_title="Elevation (m)",
|
| 80 |
-
aspectmode="
|
| 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(
|
| 89 |
-
fn=process_dem,
|
| 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__":
|
| 100 |
demo.launch()
|
|
|
|
| 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,
|
|
|
|
| 56 |
# Risk overlay (purple)
|
| 57 |
fig3d.add_trace(go.Surface(
|
| 58 |
z=np.where(risk_mask[::step, ::step], Z[::step, ::step], np.nan),
|
|
|
|
|
|
|
| 59 |
surfacecolor=np.ones_like(Z[::step, ::step]),
|
| 60 |
colorscale=[[0, "purple"], [1, "purple"]],
|
| 61 |
showscale=False,
|
| 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(
|
| 69 |
+
xaxis_title="X (grid cols)",
|
| 70 |
+
yaxis_title="Y (grid rows)",
|
| 71 |
zaxis_title="Elevation (m)",
|
| 72 |
+
aspectmode="data"
|
|
|
|
| 73 |
)
|
| 74 |
)
|
| 75 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
if __name__ == "__main__":
|
| 77 |
demo.launch()
|