Update app.py
Browse files
app.py
CHANGED
|
@@ -73,7 +73,7 @@ class DataCollector:
|
|
| 73 |
continue
|
| 74 |
|
| 75 |
return weather_data if weather_data else None
|
| 76 |
-
|
| 77 |
def fetch_usgs_earthquake_data(self):
|
| 78 |
"""Fetch earthquake data from USGS website"""
|
| 79 |
try:
|
|
@@ -146,34 +146,124 @@ class DataCollector:
|
|
| 146 |
|
| 147 |
return features_df.dropna()
|
| 148 |
|
| 149 |
-
def
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 155 |
<script>
|
| 156 |
-
|
| 157 |
-
const
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 167 |
});
|
| 168 |
|
| 169 |
-
//
|
| 170 |
-
|
| 171 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 172 |
|
| 173 |
-
//
|
| 174 |
-
|
| 175 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 176 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 177 |
|
| 178 |
# Add earthquake data if available
|
| 179 |
if earthquake_data:
|
|
|
|
| 73 |
continue
|
| 74 |
|
| 75 |
return weather_data if weather_data else None
|
| 76 |
+
|
| 77 |
def fetch_usgs_earthquake_data(self):
|
| 78 |
"""Fetch earthquake data from USGS website"""
|
| 79 |
try:
|
|
|
|
| 146 |
|
| 147 |
return features_df.dropna()
|
| 148 |
|
| 149 |
+
def download_csv(df, filename):
|
| 150 |
+
"""Generate a download link for a dataframe"""
|
| 151 |
+
csv = df.to_csv(index=False)
|
| 152 |
+
b64 = base64.b64encode(csv.encode()).decode()
|
| 153 |
+
href = f'<a href="data:file/csv;base64,{b64}" download="{filename}.csv">Download {filename} CSV</a>'
|
| 154 |
+
return href
|
| 155 |
+
|
| 156 |
+
|
| 157 |
+
# Replace the create_cesium_component function with a more user-friendly 3D visualization
|
| 158 |
+
def create_3d_visualization(earthquake_data=None, weather_data=None):
|
| 159 |
+
"""Create a more accessible 3D visualization using Three.js"""
|
| 160 |
+
threejs_html = """
|
| 161 |
+
<div id="visualizationContainer" style="width: 100%; height: 600px;"></div>
|
| 162 |
+
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
|
| 163 |
+
<script src="https://cdnjs.cloudflare.com/ajax/libs/dat-gui/0.7.7/dat.gui.min.js"></script>
|
| 164 |
<script>
|
| 165 |
+
// Set up scene
|
| 166 |
+
const container = document.getElementById('visualizationContainer');
|
| 167 |
+
const scene = new THREE.Scene();
|
| 168 |
+
const camera = new THREE.PerspectiveCamera(75, container.clientWidth / container.clientHeight, 0.1, 1000);
|
| 169 |
+
const renderer = new THREE.WebGLRenderer();
|
| 170 |
+
renderer.setSize(container.clientWidth, container.clientHeight);
|
| 171 |
+
container.appendChild(renderer.domElement);
|
| 172 |
+
|
| 173 |
+
// Add terrain
|
| 174 |
+
const terrainGeometry = new THREE.PlaneGeometry(100, 100, 50, 50);
|
| 175 |
+
const terrainMaterial = new THREE.MeshPhongMaterial({
|
| 176 |
+
color: 0x3d9970,
|
| 177 |
+
wireframe: false,
|
| 178 |
+
flatShading: true
|
| 179 |
+
});
|
| 180 |
+
const terrain = new THREE.Mesh(terrainGeometry, terrainMaterial);
|
| 181 |
+
|
| 182 |
+
// Add random terrain elevation
|
| 183 |
+
const vertices = terrainGeometry.attributes.position.array;
|
| 184 |
+
for (let i = 0; i < vertices.length; i += 3) {
|
| 185 |
+
vertices[i + 2] = Math.random() * 10;
|
| 186 |
+
}
|
| 187 |
+
terrainGeometry.computeVertexNormals();
|
| 188 |
+
scene.add(terrain);
|
| 189 |
+
|
| 190 |
+
// Add lighting
|
| 191 |
+
const ambientLight = new THREE.AmbientLight(0x404040);
|
| 192 |
+
scene.add(ambientLight);
|
| 193 |
+
const directionalLight = new THREE.DirectionalLight(0xffffff, 0.5);
|
| 194 |
+
directionalLight.position.set(1, 1, 1);
|
| 195 |
+
scene.add(directionalLight);
|
| 196 |
+
|
| 197 |
+
// Add cities as markers
|
| 198 |
+
const cities = {
|
| 199 |
+
'Islamabad': {lat: 33.7294, lon: 73.0931},
|
| 200 |
+
'Karachi': {lat: 24.8607, lon: 67.0011},
|
| 201 |
+
'Lahore': {lat: 31.5204, lon: 74.3587}
|
| 202 |
+
};
|
| 203 |
+
|
| 204 |
+
Object.entries(cities).forEach(([name, coords]) => {
|
| 205 |
+
const markerGeometry = new THREE.SphereGeometry(1, 32, 32);
|
| 206 |
+
const markerMaterial = new THREE.MeshPhongMaterial({color: 0xff0000});
|
| 207 |
+
const marker = new THREE.Mesh(markerGeometry, markerMaterial);
|
| 208 |
+
|
| 209 |
+
// Convert lat/lon to scene coordinates (simplified)
|
| 210 |
+
const x = (coords.lon - 67) * 2;
|
| 211 |
+
const y = (coords.lat - 30) * 2;
|
| 212 |
+
marker.position.set(x, y, 5);
|
| 213 |
+
scene.add(marker);
|
| 214 |
});
|
| 215 |
|
| 216 |
+
// Camera position
|
| 217 |
+
camera.position.z = 50;
|
| 218 |
+
camera.position.y = 30;
|
| 219 |
+
camera.lookAt(scene.position);
|
| 220 |
+
|
| 221 |
+
// Add controls
|
| 222 |
+
const controls = {
|
| 223 |
+
rotateSpeed: 0.01,
|
| 224 |
+
elevationScale: 1.0
|
| 225 |
+
};
|
| 226 |
+
|
| 227 |
+
const gui = new dat.GUI();
|
| 228 |
+
gui.add(controls, 'rotateSpeed', 0, 0.05);
|
| 229 |
+
gui.add(controls, 'elevationScale', 0, 2);
|
| 230 |
|
| 231 |
+
// Animation
|
| 232 |
+
function animate() {
|
| 233 |
+
requestAnimationFrame(animate);
|
| 234 |
+
terrain.rotation.y += controls.rotateSpeed;
|
| 235 |
+
|
| 236 |
+
// Update terrain elevation
|
| 237 |
+
const vertices = terrainGeometry.attributes.position.array;
|
| 238 |
+
for (let i = 0; i < vertices.length; i += 3) {
|
| 239 |
+
vertices[i + 2] *= controls.elevationScale;
|
| 240 |
+
}
|
| 241 |
+
terrainGeometry.attributes.position.needsUpdate = true;
|
| 242 |
+
|
| 243 |
+
renderer.render(scene, camera);
|
| 244 |
+
}
|
| 245 |
+
animate();
|
| 246 |
+
|
| 247 |
+
// Handle window resize
|
| 248 |
+
window.addEventListener('resize', () => {
|
| 249 |
+
camera.aspect = container.clientWidth / container.clientHeight;
|
| 250 |
+
camera.updateProjectionMatrix();
|
| 251 |
+
renderer.setSize(container.clientWidth, container.clientHeight);
|
| 252 |
+
});
|
| 253 |
+
</script>
|
| 254 |
"""
|
| 255 |
+
components.html(threejs_html, height=600)
|
| 256 |
+
|
| 257 |
+
# Update the show_disaster_monitor function to use the new visualization
|
| 258 |
+
def show_disaster_monitor(data_collector):
|
| 259 |
+
st.header("Advanced Disaster Monitoring System 🚨")
|
| 260 |
+
|
| 261 |
+
earthquake_data = data_collector.fetch_usgs_earthquake_data()
|
| 262 |
+
|
| 263 |
+
if earthquake_data:
|
| 264 |
+
st.subheader("3D Terrain Visualization")
|
| 265 |
+
create_3d_visualization(earthquake_data) # Use the new visualization function
|
| 266 |
+
# ... rest of the function remains the same
|
| 267 |
|
| 268 |
# Add earthquake data if available
|
| 269 |
if earthquake_data:
|