Build an urban groundwater salinity mapping: wells, conductivity, and sources.
- User
Build an urban groundwater salinity mapping: wells, conductivity, and sources.
Assistant
Build an urban groundwater salinity mapping: wells, conductivity, and sources.
The user wants me to create an urban groundwater salinity mapping visualization. This is a scientific/technical data visualization request. Let me think about what elements would make this compelling:
Visual Elements Needed:
Technical Approach:
Design Considerations:
Data Structure:
Let me create a sophisticated, interactive visualization with:
I'll use a dark theme with cyan/teal accents for the "water" feel, and orange/red for high salinity areas.
I'll create an interactive scientific visualization showing:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Urban Groundwater Salinity Mapping</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background: #0a1420;
color: #e0e8f0;
overflow: hidden;
min-height: 100vh;
}
#app {
display: flex;
height: 100vh;
}
#sidebar {
width: 320px;
background: linear-gradient(180deg, #0d1a28 0%, #091420 100%);
border-right: 1px solid #1e3a5f;
padding: 20px;
overflow-y: auto;
z-index: 10;
}
.panel-title {
font-size: 22px;
font-weight: 600;
color: #4ecdc4;
margin-bottom: 8px;
letter-spacing: 1px;
}
.panel-subtitle {
font-size: 12px;
color: #6a8fa8;
margin-bottom: 20px;
text-transform: uppercase;
letter-spacing: 2px;
}
.section {
margin-bottom: 24px;
padding-bottom: 20px;
border-bottom: 1px solid #1a2d42;
}
.section-title {
font-size: 11px;
text-transform: uppercase;
letter-spacing: 2px;
color: #5a7a94;
margin-bottom: 12px;
}
.legend-item {
display: flex;
align-items: center;
margin-bottom: 10px;
cursor: pointer;
padding: 8px;
border-radius: 6px;
transition: all 0.3s ease;
}
.legend-item:hover {
background: rgba(78, 205, 196, 0.1);
}
.legend-item.active {
background: rgba(78, 205, 196, 0.15);
border-left: 3px solid #4ecdc4;
}
.legend-color {
width: 30px;
height: 14px;
border-radius: 3px;
margin-right: 12px;
}
.legend-text {
flex: 1;
font-size: 13px;
}
.legend-range {
font-size: 11px;
color: #6a8fa8;
}
.source-filter {
display: flex;
flex-wrap: wrap;
gap: 8px;
}
.source-btn {
padding: 6px 12px;
border: 1px solid #2a4a6a;
background: transparent;
color: #8ab4d0;
border-radius: 20px;
font-size: 11px;
cursor: pointer;
transition: all 0.3s ease;
}
.source-btn:hover {
border-color: #4ecdc4;
color: #4ecdc4;
}
.source-btn.active {
background: #4ecdc4;
border-color: #4ecdc4;
color: #0a1420;
}
.stats-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 12px;
}
.stat-card {
background: linear-gradient(135deg, #122030 0%, #0d1825 100%);
padding: 14px;
border-radius: 8px;
border: 1px solid #1e3a5f;
}
.stat-value {
font-size: 24px;
font-weight: 700;
color: #4ecdc4;
}
.stat-label {
font-size: 10px;
color: #6a8fa8;
text-transform: uppercase;
letter-spacing: 1px;
margin-top: 4px;
}
#map-container {
flex: 1;
position: relative;
overflow: hidden;
}
#salinity-canvas {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
#wells-svg {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
pointer-events: all;
}
.well-marker {
cursor: pointer;
transition: transform 0.3s ease;
}
.well-marker:hover {
transform: scale(1.3);
}
.well-pulse {
animation: pulse 2s ease-in-out infinite;
}
@keyframes pulse {
0%, 100% { opacity: 0.3; r: 20; }
50% { opacity: 0.1; r: 35; }
}
.well-inner {
transition: all 0.3s ease;
}
.well-marker:hover .well-inner {
filter: brightness(1.3);
}
#tooltip {
position: absolute;
background: linear-gradient(135deg, #122030 0%, #0a1520 100%);
border: 1px solid #2a4a6a;
border-radius: 10px;
padding: 16px;
pointer-events: none;
opacity: 0;
transition: opacity 0.3s ease;
z-index: 100;
min-width: 240px;
box-shadow: 0 10px 40px rgba(0, 0, 0, 0.5);
}
#tooltip.visible {
opacity: 1;
}
.tooltip-header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 12px;
padding-bottom: 10px;
border-bottom: 1px solid #1e3a5f;
}
.tooltip-title {
font-size: 16px;
font-weight: 600;
color: #4ecdc4;
}
.tooltip-status {
padding: 3px 8px;
border-radius: 10px;
font-size: 10px;
font-weight: 600;
text-transform: uppercase;
}
.status-good { background: #2d5a3d; color: #6fcf97; }
.status-moderate { background: #5a4a2d; color: #f2c94c; }
.status-poor { background: #5a2d2d; color: #eb5757; }
.tooltip-row {
display: flex;
justify-content: space-between;
margin-bottom: 8px;
font-size: 12px;
}
.tooltip-label {
color: #6a8fa8;
}
.tooltip-value {
color: #e0e8f0;
font-weight: 500;
}
.conductivity-bar {
height: 6px;
background: #1a2d42;
border-radius: 3px;
margin-top: 12px;
overflow: hidden;
}
.conductivity-fill {
height: 100%;
border-radius: 3px;
transition: width 0.5s ease;
}
#map-controls {
position: absolute;
top: 20px;
right: 20px;
display: flex;
flex-direction: column;
gap: 10px;
z-index: 10;
}
.control-btn {
width: 44px;
height: 44px;
background: linear-gradient(135deg, #122030 0%, #0d1825 100%);
border: 1px solid #2a4a6a;
border-radius: 8px;
color: #8ab4d0;
cursor: pointer;
display: flex;
align-items: center;
justify-content: center;
transition: all 0.3s ease;
font-size: 18px;
}
.control-btn:hover {
border-color: #4ecdc4;
color: #4ecdc4;
background: linear-gradient(135deg, #1a3048 0%, #122438 100%);
}
.control-btn.active {
background: #4ecdc4;
border-color: #4ecdc4;
color: #0a1420;
}
#scale-bar {
position: absolute;
bottom: 30px;
left: 50%;
transform: translateX(-50%);
display: flex;
flex-direction: column;
align-items: center;
z-index: 10;
}
.scale-gradient {
width: 400px;
height: 16px;
border-radius: 8px;
background: linear-gradient(90deg,
#1a5276 0%,
#2e86ab 20%,
#48c9b0 40%,
#f4d03f 60%,
#e67e22 80%,
#c0392b 100%
);
border: 1px solid #2a4a6a;
}
.scale-labels {
display: flex;
justify-content: space-between;
width: 400px;
margin-top: 6px;
font-size: 11px;
color: #6a8fa8;
}
.scale-title {
font-size: 12px;
color: #8ab4d0;
margin-bottom: 8px;
text-transform: uppercase;
letter-spacing: 1px;
}
#well-detail {
position: absolute;
bottom: 100px;
right: 20px;
width: 280px;
background: linear-gradient(135deg, #122030 0%, #0a1520 100%);
border: 1px solid #2a4a6a;
border-radius: 12px;
padding: 20px;
z-index: 10;
display: none;
}
#well-detail.visible {
display: block;
animation: slideIn 0.3s ease;
}
@keyframes slideIn {
from { opacity: 0; transform: translateY(20px); }
to { opacity: 1; transform: translateY(0); }
}
.detail-close {
position: absolute;
top: 12px;
right: 12px;
background: none;
border: none;
color: #6a8fa8;
cursor: pointer;
font-size: 18px;
}
.detail-close:hover {
color: #4ecdc4;
}
.chart-container {
margin-top: 16px;
height: 100px;
background: #0d1825;
border-radius: 6px;
overflow: hidden;
}
#particle-canvas {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
pointer-events: none;
}
.source-marker {
cursor: pointer;
}
.source-icon {
animation: float 3s ease-in-out infinite;
}
@keyframes float {
0%, 100% { transform: translateY(0); }
50% { transform: translateY(-5px); }
}
</style>
</head>
<body>
<div id="app">
<div id="sidebar">
<div class="panel-title">Groundwater Salinity</div>
<div class="panel-subtitle">Urban Monitoring System</div>
<div class="section">
<div class="section-title">Conductivity Legend</div>
<div class="legend-item active" data-range="all">
<div class="legend-color" style="background: linear-gradient(90deg, #1a5276, #c0392b);"></div>
<div>
<div class="legend-text">All Levels</div>
<div class="legend-range">0 - 10,000+ uS/cm</div>
</div>
</div>
<div class="legend-item" data-range="fresh">
<div class="legend-color" style="background: #1a5276;"></div>
<div>
<div class="legend-text">Fresh Water</div>
<div class="legend-range">0 - 1,500 uS/cm</div>
</div>
</div>
<div class="legend-item" data-range="brackish">
<div class="legend-color" style="background: #f4d03f;"></div>
<div>
<div class="legend-text">Brackish</div>
<div class="legend-range">1,500 - 5,500 uS/cm</div>
</div>
</div>
<div class="legend-item" data-range="saline">
<div class="legend-color" style="background: #c0392b;"></div>
<div>
<div class="legend-text">Saline</div>
<div class="legend-range">5,500+ uS/cm</div>
</div>
</div>
</div>
<div class="section">
<div class="section-title">Source Types</div>
<div class="source-filter">
<button class="source-btn active" data-source="all">All</button>
<button class="source-btn" data-source="seawater">Seawater</button>
<button class="source-btn" data-source="industrial">Industrial</button>
<button class="source-btn" data-source="road_salt">Road Salt</button>
<button class="source-btn" data-source="natural">Natural</button>
<button class="source-btn" data-source="wastewater">Wastewater</button>
</div>
</div>
<div class="section">
<div class="section-title">Network Statistics</div>
<div class="stats-grid">
<div class="stat-card">
<div class="stat-value" id="well-count">24</div>
<div class="stat-label">Active Wells</div>
</div>
<div class="stat-card">
<div class="stat-value" id="avg-cond">2,847</div>
<div class="stat-label">Avg uS/cm</div>
</div>
<div class="stat-card">
<div class="stat-value" id="max-cond">8,920</div>
<div class="stat-label">Max uS/cm</div>
</div>
<div class="stat-card">
<div class="stat-value" id="alert-count">5</div>
<div class="stat-label">Alerts</div>
</div>
</div>
</div>
<div class="section" style="border-bottom: none;">
<div class="section-title">Source Markers</div>
<div id="source-list"></div>
</div>
</div>
<div id="map-container">
<canvas id="salinity-canvas"></canvas>
<canvas id="particle-canvas"></canvas>
<svg id="wells-svg"></svg>
<div id="map-controls">
<button class="control-btn active" id="btn-heatmap" title="Heat Map">◈</button>
<button class="control-btn" id="btn-contour" title="Contour Lines">▣</button>
<button class="control-btn" id="btn-flow" title="Flow Direction">↗</button>
<button class="control-btn" id="btn-animate" title="Animate Time">▶</button>
</div>
<div id="scale-bar">
<div class="scale-title">Electrical Conductivity (uS/cm)</div>
<div class="scale-gradient"></div>
<div class="scale-labels">
<span>0</span>
<span>1,500</span>
<span>3,000</span>
<span>5,500</span>
<span>8,000</span>
<span>10,000+</span>
</div>
</div>
<div id="tooltip">
<div class="tooltip-header">
<div class="tooltip-title">Well ID</div>
<div class="tooltip-status status-good">Normal</div>
</div>
<div class="tooltip-row">
<span class="tooltip-label">Depth</span>
<span class="tooltip-value">45m</span>
</div>
<div class="tooltip-row">
<span class="tooltip-label">Conductivity</span>
<span class="tooltip-value">2,450 uS/cm</span>
</div>
<div class="tooltip-row">
<span class="tooltip-label">Salinity Source</span>
<span class="tooltip-value">Natural</span>
</div>
<div class="tooltip-row">
<span class="tooltip-label">Last Reading</span>
<span class="tooltip-value">2 hours ago</span>
</div>
<div class="conductivity-bar">
<div class="conductivity-fill"></div>
</div>
</div>
<div id="well-detail">
<button class="detail-close">×</button>
<div class="panel-title" style="font-size: 16px;">Well Details</div>
<div id="detail-content"></div>
<div class="chart-container">
<canvas id="trend-chart"></canvas>
</div>
</div>
</div>
</div>
<script>
// Well data with realistic values
const wells = [
{ id: 'MW-001', x: 0.15, y: 0.25, depth: 45, conductivity: 1250, ph: 7.2, source: 'natural', status: 'good', trend: [1100, 1150, 1200, 1180, 1220, 1250] },
{ id: 'MW-002', x: 0.22, y: 0.35, depth: 52, conductivity: 2100, ph: 7.0, source: 'road_salt', status: 'moderate', trend: [1800, 1900, 2000, 2050, 2080, 2100] },
{ id: 'MW-003', x: 0.35, y: 0.20, depth: 38, conductivity: 5600, ph: 6.8, source: 'industrial', status: 'poor', trend: [4800, 5000, 5200, 5400, 5500, 5600] },
{ id: 'MW-004', x: 0.45, y: 0.40, depth: 61, conductivity: 1850, ph: 7.4, source: 'natural', status: 'good', trend: [1700, 1750, 1780, 1800, 1830, 1850] },
{ id: 'MW-005', x: 0.55, y: 0.30, depth: 48, conductivity: 8920, ph: 6.5, source: 'seawater', status: 'poor', trend: [7500, 7800, 8100, 8400, 8700, 8920] },
{ id: 'MW-006', x: 0.40, y: 0.55, depth: 55, conductivity: 2300, ph: 7.1, source: 'wastewater', status: 'moderate', trend: [2000, 2100, 2150, 2200, 2250, 2300] },
{ id: 'MW-007', x: 0.65, y: 0.45, depth: 42, conductivity: 7800, ph: 6.6, source: 'seawater', status: 'poor', trend: [7000, 7200, 7400, 7600, 7700, 7800] },
{ id: 'MW-008', x: 0.30, y: 0.65, depth: 58, conductivity: 980, ph: 7.5, source: 'natural', status: 'good', trend: [920, 940, 950, 960, 970, 980] },
{ id: 'MW-009', x: 0.75, y: 0.35, depth: 35, conductivity: 6500, ph: 6.7, source: 'seawater', status: 'poor', trend: [5800, 6000, 6200, 6350, 6450, 6500] },
{ id: 'MW-010', x: 0.20, y: 0.50, depth: 50, conductivity: 1650, ph: 7.3, source: 'natural', status: 'good', trend: [1500, 1550, 1580, 1600, 1630, 1650] },
{ id: 'MW-011', x: 0.58, y: 0.60, depth: 62, conductivity: 3200, ph: 6.9, source: 'industrial', status: 'moderate', trend: [2800, 2900, 3000, 3100, 3150, 3200] },
{ id: 'MW-012', x: 0.85, y: 0.50, depth: 40, conductivity: 4200, ph: 6.8, source: 'seawater', status: 'moderate', trend: [3800, 3900, 4000, 4080, 4150, 4200] },
{ id: 'MW-013', x: 0.12, y: 0.70, depth: 47, conductivity: 1350, ph: 7.4, source: 'natural', status: 'good', trend: [1200, 1250, 1280, 1310, 1330, 1350] },
{ id: 'MW-014', x: 0.48, y: 0.75, depth: 53, conductivity: 2900, ph: 7.0, source: 'road_salt', status: 'moderate', trend: [2500, 2600, 2700, 2780, 2850, 2900] },
{ id: 'MW-015', x: 0.70, y: 0.65, depth: 44, conductivity: 5100, ph: 6.7, source: 'wastewater', status: 'poor', trend: [4400, 4600, 4800, 4950, 5050, 5100] },
{ id: 'MW-016', x: 0.38, y: 0.85, depth: 60, conductivity: 1780, ph: 7.2, source: 'natural', status: 'good', trend: [1600, 1650, 1700, 1730, 1760, 1780] },
{ id: 'MW-017', x: 0.82, y: 0.70, depth: 36, conductivity: 7200, ph: 6.5, source: 'seawater', status: 'poor', trend: [6500, 6700, 6900, 7050, 7150, 7200] },
{ id: 'MW-018', x: 0.25, y: 0.82, depth: 49, conductivity: 2450, ph: 7.1, source: 'road_salt', status: 'moderate', trend: [2100, 2200, 2300, 2380, 2420, 2450] },
{ id: 'MW-019', x: 0.60, y: 0.82, depth: 56, conductivity: 3800, ph: 6.9, source: 'industrial', status: 'moderate', trend: [3400, 3500, 3600, 3700, 3750, 3800] },
{ id: 'MW-020', x: 0.92, y: 0.60, depth: 38, conductivity: 5800, ph: 6.6, source: 'seawater', status: 'poor', trend: [5200, 5400, 5550, 5650, 5750, 5800] },
{ id: 'MW-021', x: 0.08, y: 0.45, depth: 51, conductivity: 1100, ph: 7.5, source: 'natural', status: 'good', trend: [1000, 1040, 1060, 1080, 1090, 1100] },
{ id: 'MW-022', x: 0.52, y: 0.15, depth: 43, conductivity: 4500, ph: 6.8, source: 'industrial', status: 'moderate', trend: [4000, 4150, 4280, 4380, 4450, 4500] },
{ id: 'MW-023', x: 0.78, y: 0.20, depth: 39, conductivity: 6100, ph: 6.7, source: 'seawater', status: 'poor', trend: [5500, 5700, 5850, 5980, 6050, 6100] },
{ id: 'MW-024', x: 0.88, y: 0.85, depth: 46, conductivity: 3600, ph: 7.0, source: 'wastewater', status: 'moderate', trend: [3200, 3300, 3400, 3500, 3550, 3600] }
];
// Contamination sources
const sources = [
{ id: 'S1', type: 'seawater', name: 'Coastal Intrusion Zone', x: 0.95, y: 0.30, direction: 225 },
{ id: 'S2', type: 'industrial', name: 'Industrial Park', x: 0.40, y: 0.18, direction: 180 },
{ id: 'S3', type: 'road_salt', name: 'Highway Runoff', x: 0.22, y: 0.40, direction: 135 },
{ id: 'S4', type: 'wastewater', name: 'Treatment Plant', x: 0.42, y: 0.58, direction: 270 },
{ id: 'S5', type: 'seawater', name: 'Estuary Zone', x: 0.85, y: 0.55, direction: 200 }
];
const sourceColors = {
seawater: '#3498db',
industrial: '#9b59b6',
road_salt: '#e67e22',
natural: '#2ecc71',
wastewater: '#e74c3c'
};
const sourceLabels = {
seawater: 'Seawater Intrusion',
industrial: 'Industrial Discharge',
road_salt: 'Road Salt',
natural: 'Natural Geology',
wastewater: 'Wastewater'
};
// Canvas setup
const salinityCanvas = document.getElementById('salinity-canvas');
const salinityCtx = salinityCanvas.getContext('2d');
const particleCanvas = document.getElementById('particle-canvas');
const particleCtx = particleCanvas.getContext('2d');
const wellsSvg = document.getElementById('wells-svg');
const tooltip = document.getElementById('tooltip');
let showHeatmap = true;
let showContours = false;
let showFlow = false;
let isAnimating = false;
let activeFilter = 'all';
let animationTime = 0;
let particles = [];
function resizeCanvases() {
const container = document.getElementById('map-container');
const rect = container.getBoundingClientRect();
salinityCanvas.width = rect.width;
salinityCanvas.height = rect.height;
particleCanvas.width = rect.width;
particleCanvas.height = rect.height;
wellsSvg.setAttribute('viewBox', `0 0 ${rect.width} ${rect.height}`);
render();
}
function getConductivityColor(value) {
const normalized = Math.min(value / 10000, 1);
if (normalized < 0.15) {
return `rgb(${26 + normalized * 100}, ${82 + normalized * 200}, ${118 + normalized * 100})`;
} else if (normalized < 0.3) {
const t = (normalized - 0.15) / 0.15;
return `rgb(${72 + t * 50}, ${138 + t * 60}, ${171 - t * 20})`;
} else if (normalized < 0.55) {
const t = (normalized - 0.3) / 0.25;
return `rgb(${122 + t * 116}, ${198 - t * 50}, ${151 - t * 100})`;
} else if (normalized < 0.8) {
const t = (normalized - 0.55) / 0.25;
return `rgb(${238 + t * 20}, ${148 - t * 40}, ${51 - t * 20})`;
} else {
const t = (normalized - 0.8) / 0.2;
return `rgb(${192 - t * 20}, ${57 - t * 20}, ${43})`;
}
}
function interpolateConductivity(x, y, timeOffset = 0) {
let sum = 0;
let weightSum = 0;
wells.forEach(well => {
const dx = x - well.x;
const dy = y - well.y;
const dist = Math.sqrt(dx * dx + dy * dy);
const animOffset = isAnimating ? Math.sin(animationTime * 0.5 + well.x * 10) * 200 : 0;
if (dist < 0.01) {
sum = well.conductivity + animOffset;
weightSum = 1;
return;
}
const weight = 1 / (dist * dist);
sum += (well.conductivity + animOffset) * weight;
weightSum += weight;
});
return weightSum > 0 ? sum / weightSum : 0;
}
function renderHeatmap() {
if (!showHeatmap) {
salinityCtx.clearRect(0, 0, salinityCanvas.width, salinityCanvas.height);
return;
}
const width = salinityCanvas.width;
const height = salinityCanvas.height;
const resolution = 4;
const imageData = salinityCtx.createImageData(width, height);
const data = imageData.data;
for (let py = 0; py < height; py += resolution) {
for (let px = 0; px < width; px += resolution) {
const x = px / width;
const y = py / height;
const conductivity = interpolateConductivity(x, y);
const color = getConductivityColor(conductivity);
const rgb = color.match(/\d+/g).map(Number);
const alpha = 180;
for (let dy = 0; dy < resolution && py + dy < height; dy++) {
for (let dx = 0; dx < resolution && px + dx < width; dx++) {
const idx = ((py + dy) * width + (px + dx)) * 4;
data[idx] = rgb[0];
data[idx + 1] = rgb[1];
data[idx + 2] = rgb[2];
data[idx + 3] = alpha;
}
}
}
}
salinityCtx.putImageData(imageData, 0, 0);
if (showContours) {
renderContours();
}
}
function renderContours() {
salinityCtx.strokeStyle = 'rgba(255, 255, 255, 0.3)';
salinityCtx.lineWidth = 1;
const levels = [1500, 3000, 5500, 8000];
const width = salinityCanvas.width;
const height = salinityCanvas.height;
const step = 10;
levels.forEach(level => {
salinityCtx.beginPath();
for (let py = 0; py < height; py += step) {
for (let px = 0; px < width; px += step) {
const x = px / width;
const y = py / height;
const cond = interpolateConductivity(x, y);
if (Math.abs(cond - level) < 150) {
salinityCtx.rect(px, py, 2, 2);
}
}
}
salinityCtx.stroke();
});
}
function renderWells() {
const width = salinityCanvas.width;
const height = salinityCanvas.height;
wellsSvg.innerHTML = '';
// Add defs for gradients
const defs = document.createElementNS('http://www.w3.org/2000/svg', 'defs');
wells.forEach(well => {
const gradient = document.createElementNS('http://www.w3.org/2000/svg', 'radialGradient');
gradient.setAttribute('id', `gradient-${well.id}`);
gradient.innerHTML = `
<stop offset="0%" style="stop-color:${getConductivityColor(well.conductivity)};stop-opacity:1" />
<stop offset="100%" style="stop-color:${getConductivityColor(well.conductivity)};stop-opacity:0.3" />
`;
defs.appendChild(gradient);
});
wellsSvg.appendChild(defs);
// Render sources
sources.forEach(source => {
if (activeFilter !== 'all' && activeFilter !== source.type) return;
const g = document.createElementNS('http://www.w3.org/2000/svg', 'g');
g.classList.add('source-marker');
g.setAttribute('transform', `translate(${source.x * width}, ${source.y * height})`);
// Direction arrow
const arrow = document.createElementNS('http://www.w3.org/2000/svg', 'polygon');
const angle = (source.direction - 90) * Math.PI / 180;
const ax = Math.cos(angle) * 30;
const ay = Math.sin(angle) * 30;
arrow.setAttribute('points', `0,0 ${ax - 5},${ay} ${ax + 5},${ay}`);
arrow.setAttribute('fill', sourceColors[source.type]);
arrow.setAttribute('opacity', '0.6');
arrow.classList.add('source-icon');
g.appendChild(arrow);
// Source circle
const circle = document.createElementNS('http://www.w3.org/2000/svg', 'circle');
circle.setAttribute('r', '12');
circle.setAttribute('fill', sourceColors[source.type]);
circle.setAttribute('opacity', '0.8');
circle.setAttribute('stroke', '#fff');
circle.setAttribute('stroke-width', '2');
// Source icon
const icon = document.createElementNS('http://www.w3.org/2000/svg', 'text');
icon.setAttribute('text-anchor', 'middle');
icon.setAttribute('dy', '4');
icon.setAttribute('fill', '#fff');
icon.setAttribute('font-size', '12');
icon.setAttribute('font-weight', 'bold');
icon.textContent = source.type[0].toUpperCase();
g.appendChild(circle);
g.appendChild(icon);
g.addEventListener('mouseenter', (e) => showSourceTooltip(e, source));
g.addEventListener('mouseleave', hideTooltip);
wellsSvg.appendChild(g);
});
// Render wells
wells.forEach(well => {
if (activeFilter !== 'all' && activeFilter !== well.source) return;
const g = document.createElementNS('http://www.w3.org/2000/svg', 'g');
g.classList.add('well-marker');
g.setAttribute('transform', `translate(${well.x * width}, ${well.y * height})`);
// Pulse ring
const pulse = document.createElementNS('http://www.w3.org/2000/svg', 'circle');
pulse.setAttribute('r', '20');
pulse.setAttribute('fill', 'none');
pulse.setAttribute('stroke', getConductivityColor(well.conductivity));
pulse.classList.add('well-pulse');
// Outer ring
const outer = document.createElementNS('http://www.w3.org/2000/svg', 'circle');
outer.setAttribute('r', '10');
outer.setAttribute('fill', `url(#gradient-${well.id})`);
outer.setAttribute('stroke', '#fff');
outer.setAttribute('stroke-width', '2');
// Inner dot
const inner = document.createElementNS('http://www.w3.org/2000/svg', 'circle');
inner.classList.add('well-inner');
inner.setAttribute('r', '4');
inner.setAttribute('fill', '#fff');
// Status indicator
if (well.status === 'poor') {
const alert = document.createElementNS('http://www.w3.org/2000/svg', 'circle');
alert.setAttribute('cx', '7');
alert.setAttribute('cy', '-7');
alert.setAttribute('r', '5');
alert.setAttribute('fill', '#eb5757');
const alertText = document.createElementNS('http://www.w3.org/2000/svg', 'text');
alertText.setAttribute('x', '7');
alertText.setAttribute('y', '-4');
alertText.setAttribute('text-anchor', 'middle');
alertText.setAttribute('fill', '#fff');
alertText.setAttribute('font-size', '8');
alertText.setAttribute('font-weight', 'bold');
alertText.textContent = '!';
g.appendChild(alert);
g.appendChild(alertText);
}
g.appendChild(pulse);
g.appendChild(outer);
g.appendChild(inner);
g.addEventListener('mouseenter', (e) => showWellTooltip(e, well));
g.addEventListener('mouseleave', hideTooltip);
g.addEventListener('click', () => showWellDetail(well));
wellsSvg.appendChild(g);
});
}
// Particle system for flow visualization
function initParticles() {
particles = [];
for (let i = 0; i < 200; i++) {
particles.push({
x: Math.random(),
y: Math.random(),
speed: 0.0005 + Math.random() * 0.001,
life: Math.random()
});
}
}
function updateParticles() {
const width = particleCanvas.width;
const height = particleCanvas.height;
particles.forEach(p => {
// Flow towards lower conductivity (groundwater flow simulation)
let gradX = 0, gradY = 0;
const eps = 0.02;
const cXp = interpolateConductivity(p.x + eps, p.y);
const cXm = interpolateConductivity(p.x - eps, p.y);
const cYp = interpolateConductivity(p.x, p.y + eps);
const cYm = interpolateConductivity(p.x, p.y - eps);
gradX = (cXm - cXp) * 0.00001;
gradY = (cYm - cYp) * 0.00001;
// Add general flow direction (towards coast)
gradX += 0.0003;
p.x += gradX + (Math.random() - 0.5) * 0.001;
p.y += gradY + (Math.random() - 0.5) * 0.001;
p.life -= 0.002;
if (p.life <= 0 || p.x < 0 || p.x > 1 || p.y < 0 || p.y > 1) {
p.x = Math.random();
p.y = Math.random();
p.life = 1;
}
});
}
function renderParticles() {
const width = particleCanvas.width;
const height = particleCanvas.height;
particleCtx.clearRect(0, 0, width, height);
if (!showFlow) return;
particles.forEach(p => {
const conductivity = interpolateConductivity(p.x, p.y);
const color = getConductivityColor(conductivity);
particleCtx.beginPath();
particleCtx.arc(p.x * width, p.y * height, 2, 0, Math.PI * 2);
particleCtx.fillStyle = color;
particleCtx.globalAlpha = p.life * 0.7;
particleCtx.fill();
});
particleCtx.globalAlpha = 1;
}
function showWellTooltip(e, well) {
const rect = document.getElementById('map-container').getBoundingClientRect();
const x = e.clientX - rect.left;
const y = e.clientY - rect.top;
tooltip.querySelector('.tooltip-title').textContent = well.id;
const statusEl = tooltip.querySelector('.tooltip-status');
statusEl.textContent = well.status === 'good' ? 'Normal' : well.status === 'moderate' ? 'Warning' : 'Alert';
statusEl.className = `tooltip-status status-${well.status}`;
tooltip.querySelectorAll('.tooltip-value')[0].textContent = `${well.depth}m`;
tooltip.querySelectorAll('.tooltip-value')[1].textContent = `${well.conductivity.toLocaleString()} uS/cm`;
tooltip.querySelectorAll('.tooltip-value')[2].textContent = sourceLabels[well.source];
tooltip.querySelectorAll('.tooltip-value')[3].textContent = '2 hours ago';
const fillPercent = Math.min(well.conductivity / 10000 * 100, 100);
const fillEl = tooltip.querySelector('.conductivity-fill');
fillEl.style.width = `${fillPercent}%`;
fillEl.style.background = getConductivityColor(well.conductivity);
tooltip.style.left = `${x + 15}px`;
tooltip.style.top = `${y - 10}px`;
tooltip.classList.add('visible');
}
function showSourceTooltip(e, source) {
const rect = document.getElementById('map-container').getBoundingClientRect();
const x = e.clientX - rect.left;
const y = e.clientY - rect.top;
tooltip.querySelector('.tooltip-title').textContent = source.name;
const statusEl = tooltip.querySelector('.tooltip-status');
statusEl.textContent = source.type.replace('_', ' ').toUpperCase();
statusEl.className = 'tooltip-status';
statusEl.style.background = sourceColors[source.type];
statusEl.style.color = '#fff';
tooltip.querySelectorAll('.tooltip-value')[0].textContent = 'N/A';
tooltip.querySelectorAll('.tooltip-value')[1].textContent = 'Variable';
tooltip.querySelectorAll('.tooltip-value')[2].textContent = sourceLabels[source.type];
tooltip.querySelectorAll('.tooltip-value')[3].textContent = 'Active';
tooltip.querySelector('.conductivity-fill').style.width = '100%';
tooltip.querySelector('.conductivity-fill').style.background = sourceColors[source.type];
tooltip.style.left = `${x + 15}px`;
tooltip.style.top = `${y - 10}px`;
tooltip.classList.add('visible');
}
function hideTooltip() {
tooltip.classList.remove('visible');
}
function showWellDetail(well) {
const detail = document.getElementById('well-detail');
const content = document.getElementById('detail-content');
content.innerHTML = `
<div class="tooltip-row">
<span class="tooltip-label">Well ID</span>
<span class="tooltip-value">${well.id}</span>
</div>
<div class="tooltip-row">
<span class="tooltip-label">Depth</span>
<span class="tooltip-value">${well.depth}m</span>
</div>
<div class="tooltip-row">
<span class="tooltip-label">Conductivity</span>
<span class="tooltip-value">${well.conductivity.toLocaleString()} uS/cm</span>
</div>
<div class="tooltip-row">
<span class="tooltip-label">pH Level</span>
<span class="tooltip-value">${well.ph}</span>
</div>
<div class="tooltip-row">
<span class="tooltip-label">Source</span>
<span class="tooltip-value" style="color: ${sourceColors[well.source]}">${sourceLabels[well.source]}</span>
</div>
<div class="section-title" style="margin-top: 16px;">6-Month Trend</div>
`;
detail.classList.add('visible');
renderTrendChart(well);
}
function renderTrendChart(well) {
const canvas = document.getElementById('trend-chart');
const ctx = canvas.getContext('2d');
const rect = canvas.parentElement.getBoundingClientRect();
canvas.width = rect.width;
canvas.height = rect.height;
const padding = 10;
const width = canvas.width - padding * 2;
const height = canvas.height - padding * 2;
ctx.clearRect(0, 0, canvas.width, canvas.height);
const min = Math.min(...well.trend) * 0.9;
const max = Math.max(...well.trend) * 1.1;
const range = max - min;
// Grid
ctx.strokeStyle = 'rgba(255, 255, 255, 0.1)';
ctx.lineWidth = 1;
for (let i = 0; i <= 4; i++) {
const y = padding + (height * i / 4);
ctx.beginPath();
ctx.moveTo(padding, y);
ctx.lineTo(canvas.width - padding, y);
ctx.stroke();
}
// Line
ctx.beginPath();
ctx.strokeStyle = getConductivityColor(well.conductivity);
ctx.lineWidth = 2;
well.trend.forEach((val, i) => {
const x = padding + (width * i / (well.trend.length - 1));
const y = padding + height - ((val - min) / range * height);
if (i === 0) ctx.moveTo(x, y);
else ctx.lineTo(x, y);
});
ctx.stroke();
// Points
well.trend.forEach((val, i) => {
const x = padding + (width * i / (well.trend.length - 1));
const y = padding + height - ((val - min) / range * height);
ctx.beginPath();
ctx.arc(x, y, 4, 0, Math.PI * 2);
ctx.fillStyle = '#fff';
ctx.fill();
});
}
function updateStats() {
const filteredWells = activeFilter === 'all'
? wells
: wells.filter(w => w.source === activeFilter);
if (filteredWells.length === 0) {
document.getElementById('well-count').textContent = '0';
document.getElementById('avg-cond').textContent = '-';
document.getElementById('max-cond').textContent = '-';
document.getElementById('alert-count').textContent = '0';
return;
}
const avg = Math.round(filteredWells.reduce((a, w) => a + w.conductivity, 0) / filteredWells.length);
const max = Math.max(...filteredWells.map(w => w.conductivity));
const alerts = filteredWells.filter(w => w.status === 'poor').length;
document.getElementById('well-count').textContent = filteredWells.length;
document.getElementById('avg-cond').textContent = avg.toLocaleString();
document.getElementById('max-cond').textContent = max.toLocaleString();
document.getElementById('alert-count').textContent = alerts;
}
function render() {
renderHeatmap();
renderWells();
renderParticles();
updateStats();
}
function animate() {
if (isAnimating) {
animationTime += 0.016;
updateParticles();
renderHeatmap();
}
if (showFlow) {
updateParticles();
renderParticles();
}
requestAnimationFrame(animate);
}
// Event listeners
document.querySelectorAll('.legend-item').forEach(item => {
item.addEventListener('click', () => {
document.querySelectorAll('.legend-item').forEach(i => i.classList.remove('active'));
item.classList.add('active');
const range = item.dataset.range;
if (range === 'all') {
activeFilter = 'all';
} else if (range === 'fresh') {
activeFilter = 'natural';
} else if (range === 'brackish') {
activeFilter = 'all';
} else if (range === 'saline') {
activeFilter = 'seawater';
}
renderWells();
updateStats();
});
});
document.querySelectorAll('.source-btn').forEach(btn => {
btn.addEventListener('click', () => {
document.querySelectorAll('.source-btn').forEach(b => b.classList.remove('active'));
btn.classList.add('active');
activeFilter = btn.dataset.source;
renderWells();
updateStats();
});
});
document.getElementById('btn-heatmap').addEventListener('click', function() {
showHeatmap = !showHeatmap;
this.classList.toggle('active');
renderHeatmap();
});
document.getElementById('btn-contour').addEventListener('click', function() {
showContours = !showContours;
this.classList.toggle('active');
renderHeatmap();
});
document.getElementById('btn-flow').addEventListener('click', function() {
showFlow = !showFlow;
this.classList.toggle('active');
if (showFlow) initParticles();
renderParticles();
});
document.getElementById('btn-animate').addEventListener('click', function() {
isAnimating = !isAnimating;
this.classList.toggle('active');
this.textContent = isAnimating ? '⏸' : '▶';
});
document.querySelector('.detail-close').addEventListener('click', () => {
document.getElementById('well-detail').classList.remove('visible');
});
window.addEventListener('resize', resizeCanvases);
// Initialize
initParticles();
resizeCanvases();
animate();
</script>
</body>
</html>