-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
351 lines (278 loc) · 10.9 KB
/
main.js
File metadata and controls
351 lines (278 loc) · 10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
// Create a Leaflet map instance and center it on Zimbabwe
const map = L.map('map').setView([-19.0154, 29.1549], 7);
// Create the Google Satellite base map layer
const googleSatellite = L.tileLayer('http://{s}.google.com/vt/lyrs=s&x={x}&y={y}&z={z}', {
maxZoom: 20,
subdomains: ['mt0', 'mt1', 'mt2', 'mt3'],
attribution: 'Africa Regional Centre for Space Sciecnce Education in English'
});
// Create the grid overlay layer group
const gridOverlay = L.layerGroup();
// Add grid lines to the grid overlay layer group
function addGridLines() {
const bounds = map.getBounds();
const southWest = bounds.getSouthWest();
const northEast = bounds.getNorthEast();
const latGridInterval = 1; // Interval between latitude grid lines
const lonGridInterval = 1; // Interval between longitude grid lines
const gridLines = [];
for (let lat = Math.floor(southWest.lat / latGridInterval) * latGridInterval; lat <= northEast.lat; lat += latGridInterval) {
gridLines.push([[lat, southWest.lng], [lat, northEast.lng]]);
}
for (let lon = Math.floor(southWest.lng / lonGridInterval) * lonGridInterval; lon <= northEast.lng; lon += lonGridInterval) {
gridLines.push([[southWest.lat, lon], [northEast.lat, lon]]);
}
// Create SVG elements for each grid line and add them to the grid overlay layer group
gridLines.forEach(line => {
const polyline = L.polyline(line, { className: 'grid-line' });
gridOverlay.addLayer(polyline);
});
// Add labels to the grid corners
const labelOptions = { className: 'grid-label', opacity: 0, direction: 'center', permanent: true };
const gridCorners = [
[southWest.lat, southWest.lng],
[southWest.lat, northEast.lng],
[northEast.lat, northEast.lng],
[northEast.lat, southWest.lng]
];
gridCorners.forEach(corner => {
const label = L.marker(corner, labelOptions).addTo(map);
label.bindTooltip(`${corner[0].toFixed(3)}, ${corner[1].toFixed(3)}`, { offset: [0, -12], className: 'grid-label-tooltip' });
});
}
// Event listener for map move to redraw the grid lines and labels
map.on('moveend', () => {
gridOverlay.clearLayers();
addGridLines();
});
// Initial drawing of grid lines and labels
addGridLines();
// Add the Google Satellite base map layer as the default
googleSatellite.addTo(map);
// Create an object to hold the base map layers
const baseMaps = {
'Google Satellite': googleSatellite
};
// Create an object to hold the overlay layers
const overlayMaps = {
'Grid Overlay': gridOverlay
};
// Function to add a Geoserver layer to the map
function addGeoserverLayer(year, month) {
// Create the WMS URL based on the selected parameters
const wmsUrl = `http://localhost:8080/geoserver/drought/wms`;
// Create the WMS layer options
const wmsLayerOptions = {
layers: `drought:${year}_${month}`,
format: 'image/png',
transparent: true
};
// Create the WMS layer and add it to the map
const wmsLayer = L.tileLayer.wms(wmsUrl, wmsLayerOptions);
// Add the WMS layer to the overlay maps
const layerName = `${year}_${month}`;
overlayMaps[layerName] = wmsLayer;
wmsLayer.addTo(map);
// Update the layers control
updateLayersControl();
}
// Function to remove a Geoserver layer from the map
function removeGeoserverLayer(year, month) {
// Remove the WMS layer from the map
const layerName = `${year}_${month}`;
const wmsLayer = overlayMaps[layerName];
if (wmsLayer) {
wmsLayer.remove();
delete overlayMaps[layerName];
// Update the layers control
updateLayersControl();
}
}
// Function to update the layers control
function updateLayersControl() {
// Remove existing layers control if it exists
if (map.layersControl) {
map.removeControl(map.layersControl);
}
// Create a new layers control
map.layersControl = L.control.layers(baseMaps, overlayMaps).addTo(map);
}
// Function to update the map based on user selections
function updateMap() {
// Get the selected year and month
const selectedYear = document.getElementById('year-select').value;
const selectedMonth = document.getElementById('month-select').value;
// Remove existing Geoserver layers
for (const layerName in overlayMaps) {
if (layerName.includes('drought:')) {
removeGeoserverLayer(layerName.split(':')[1].split('_')[0], layerName.split(':')[1].split('_')[1]);
}
}
if (selectedYear && selectedMonth) {
// Add the selected Geoserver layer to the map
addGeoserverLayer(selectedYear, selectedMonth);
}
}
// Event listener for the button to update the map on click
const viewButton = document.getElementById('view-button');
viewButton.addEventListener('click', updateMap);
document.addEventListener('DOMContentLoaded', function () {
// Get the current year
const currentYear = new Date().getFullYear();
// Populate the years dynamically
const yearSelect = document.getElementById('year-select');
for (let year = 2000; year <= currentYear; year++) {
const option = document.createElement('option');
option.value = year;
option.text = year;
yearSelect.appendChild(option);
}
// Populate the months dynamically
const monthSelect = document.getElementById('month-select');
for (let month = 1; month <= 12; month++) {
const option = document.createElement('option');
option.value = month.toString().padStart(2, '0');
option.text = month.toString().padStart(2, '0');
monthSelect.appendChild(option);
}
});
function handleMapClick(event) {
const { lat, lng } = event.latlng;
// Fetch the attribute data from the clicked location (replace with your own data retrieval method)
const attributeData = fetchAttributeData(lat, lng);
// Create a table to display the attribute data
const table = document.createElement('table');
table.classList.add('popup-table');
// Create table header
const thead = document.createElement('thead');
const headerRow = document.createElement('tr');
Object.keys(attributeData).forEach(header => {
const th = document.createElement('th');
th.textContent = header;
headerRow.appendChild(th);
});
thead.appendChild(headerRow);
table.appendChild(thead);
// Create table body
const tbody = document.createElement('tbody');
const row = document.createElement('tr');
Object.values(attributeData).forEach(value => {
const cell = document.createElement('td');
cell.textContent = value;
row.appendChild(cell);
});
tbody.appendChild(row);
table.appendChild(tbody);
// Create the popup and display the table
L.popup()
.setLatLng(event.latlng)
.setContent(table)
.openOn(map);
}
// Function to populate the years dynamically for Downloads
function populateYears() {
const currentYear = new Date().getFullYear();
const downloadYearSelect = document.getElementById('download-year-select');
for (let year = 2000; year <= currentYear; year++) {
const option = document.createElement('option');
option.value = year;
option.text = year;
downloadYearSelect.appendChild(option);
}
}
// Call the function to populate the years
populateYears();
// Function to handle the download button click
function handleDownload() {
// Get the selected year and dataset type
const selectedYear = document.getElementById('download-year-select').value;
const selectedType = document.getElementById('download-type-select').value;
// Construct the URL for the zip file
const baseUrl = 'data';
const yearUrl = `${baseUrl}/${selectedYear}`;
const fileUrl = selectedType === 'zonal' ? `${yearUrl}/zonal_statistics.zip` : `${yearUrl}/raster_maps.zip`;
// Create a temporary link element and trigger the download
const link = document.createElement('a');
link.href = fileUrl;
link.download = selectedType === 'zonal' ? 'zonal_statistics.zip' : 'raster_maps.zip';
link.click();
}
// Event listener for the download button
const downloadButton = document.getElementById('download-button');
downloadButton.addEventListener('click', () => {
// Get the selected year and dataset type
const selectedYear = document.getElementById('download-year-select').value;
const selectedType = document.getElementById('download-type-select').value;
// Construct the URL for the zip file
const baseUrl = 'data';
const yearUrl = `${baseUrl}/${selectedYear}`;
const fileUrl = selectedType === 'zonal' ? `${yearUrl}/zonal_statistics.zip` : `${yearUrl}/raster_maps.zip`;
// Create a temporary link element and trigger the download
const link = document.createElement('a');
link.href = fileUrl;
link.download = selectedType === 'zonal' ? 'zonal_statistics.zip' : 'raster_maps.zip';
link.click();
});
// Create a custom legend control
const legendControl = L.control({ position: 'bottomright' });
// Define the content of the legend control
legendControl.onAdd = function (map) {
const div = L.DomUtil.create('div', 'legend');
div.innerHTML = '<img src="img/legend.png" alt="Legend">';
return div;
};
// Define the content of the legend control
legendControl.onAdd = function (map) {
const div = L.DomUtil.create('div', 'legend');
const img = L.DomUtil.create('img', 'legend-image');
img.src = 'img/legend.png';
img.alt = 'Legend';
img.style.width = '150px'; // Adjust the width as per your requirement
img.style.height = 'auto'; // Maintain the aspect ratio
div.appendChild(img);
return div;
};
// Add the legend control to the map
legendControl.addTo(map);
// Create the scale control and add it to the map
L.control.scale().addTo(map);
// Add the printing control to the map
L.control.print().addTo(map);
/*// Add an event listener to the toggle button
const toggleButton = document.getElementById('toggle-button');
toggleButton.addEventListener('click', toggleLeftSection);
// Function to toggle the visibility of the left section
function toggleLeftSection() {
const container = document.querySelector('.container');
container.classList.toggle('hide-left-section');
}
// Function to handle the window resize event
function handleWindowResize() {
const toggleButton = document.getElementById('toggle-button');
const leftSection = document.querySelector('.left-section');
const screenWidth = window.innerWidth;
if (screenWidth <= 1024) {
toggleButton.style.display = 'block';
leftSection.classList.add('hide');
} else {
toggleButton.style.display = 'none';
leftSection.classList.remove('hide');
}
}
// Add an event listener for the window resize event
window.addEventListener('resize', handleWindowResize);
// Call the handleWindowResize function once on page load
handleWindowResize();
*/
// JavaScript code
// JavaScript code
const toggleButton = document.getElementById("toggle-button");
const leftSection = document.querySelector(".left-section");
toggleButton.addEventListener("click", function() {
leftSection.classList.toggle("hidden");
});
window.addEventListener("resize", function() {
if (window.innerWidth > 1024) {
leftSection.classList.remove("hidden");
}
});