|
221 | 221 | .catch(err => {
|
222 | 222 | console.warn(err);
|
223 | 223 | });
|
| 224 | + |
| 225 | + // run point query on right-click |
| 226 | + map.on('contextmenu', function(e) { |
| 227 | + const lat = e.latlng.lat.toFixed(3); |
| 228 | + const lng = e.latlng.lng.toFixed(3); |
| 229 | + |
| 230 | + const popup = L.popup() |
| 231 | + .setLatLng(e.latlng) |
| 232 | + .setContent("<p>Loading point data...</p>") |
| 233 | + .openOn(map); |
| 234 | + |
| 235 | + fetch(`{{ point_endpoint|safe }}`.replace('{lat}', lat).replace('{lon}', lng)) |
| 236 | + .then(res => { |
| 237 | + if (res.ok) return res.json(); |
| 238 | + |
| 239 | + return res.json().then(errorData => { |
| 240 | + const error = new Error(errorData.detail || `Server error: ${res.status}`); |
| 241 | + error.status = res.status; |
| 242 | + error.errorData = errorData; |
| 243 | + throw error; |
| 244 | + }) |
| 245 | + }) |
| 246 | + .then(data => { |
| 247 | + const formatPopupContent = (data) => { |
| 248 | + let html = '<div style="max-width: 250px;">'; |
| 249 | + |
| 250 | + html += `<p><strong>Coordinates:</strong> ${lat}, ${lng}</p>`; |
| 251 | + |
| 252 | + // single band case - just show the value |
| 253 | + if (data.band_names.length === 1) { |
| 254 | + const value = data.values[0] !== null ? data.values[0] : 'No data'; |
| 255 | + html += `<p><strong>Value:</strong> ${value}</p>`; |
| 256 | + } |
| 257 | + // multiple bands case - show in a table |
| 258 | + else { |
| 259 | + html += '<table style="width: 100%; border-collapse: collapse;">'; |
| 260 | + html += '<tr><th style="text-align: left; padding: 1px; border-bottom: 1px solid #ddd;">Band</th>' + |
| 261 | + '<th style="text-align: right; padding: 1px; border-bottom: 1px solid #ddd;">Value</th></tr>'; |
| 262 | + |
| 263 | + for (let i = 0; i < data.band_names.length; i++) { |
| 264 | + const value = data.values[i] !== null ? data.values[i] : 'No data'; |
| 265 | + html += `<tr> |
| 266 | + <td style="text-align: left; padding: 1px;">${data.band_names[i]}</td> |
| 267 | + <td style="text-align: right; padding: 1px;">${value}</td> |
| 268 | + </tr>`; |
| 269 | + } |
| 270 | + |
| 271 | + html += '</table>'; |
| 272 | + } |
| 273 | + |
| 274 | + html += '</div>'; |
| 275 | + return html; |
| 276 | + }; |
| 277 | + |
| 278 | + popup.setContent(formatPopupContent(data)); |
| 279 | + }) |
| 280 | + .catch(err => { |
| 281 | + if (err.errorData && err.errorData.detail) { |
| 282 | + popup.setContent(`<p>${err.errorData.detail}</p>`); |
| 283 | + } else { |
| 284 | + popup.setContent(`<p>Error: ${err.message || 'An unknown error occurred'}</p>`); |
| 285 | + } |
| 286 | + }); |
| 287 | + }); |
| 288 | + |
224 | 289 | </script>
|
225 | 290 | </body>
|
226 | 291 | </html>
|
0 commit comments