ws4kp-linhanced/server/scripts/modules/regionalforecast.mjs

115 lines
3.3 KiB
JavaScript
Raw Normal View History

2026-04-07 18:08:38 -04:00
// regional observations display
2020-09-04 13:02:20 -05:00
2022-11-22 16:19:10 -06:00
import STATUS from './status.mjs';
2022-11-22 16:29:10 -06:00
import WeatherDisplay from './weatherdisplay.mjs';
2022-12-06 16:14:56 -06:00
import { registerDisplay } from './navigation.mjs';
2026-04-07 18:08:38 -04:00
import {
createMap,
addBaseLayers,
setPrimaryLocationMarker,
loadNearbyObservationMarkers,
clearMarkers,
} from './utils/leaflet-weather-map.mjs';
2025-05-29 08:30:01 -05:00
2020-09-04 13:02:20 -05:00
class RegionalForecast extends WeatherDisplay {
2020-10-29 16:44:28 -05:00
constructor(navId, elemId) {
2026-04-07 18:08:38 -04:00
super(navId, elemId, 'Regional Observations', true);
this.timing.totalScreens = 1;
this.map = null;
this.baseLayer = null;
this.boundaryLayer = null;
this.locationMarker = null;
this.nearbyMarkers = [];
this.nearbyMarkersKey = '';
2020-09-04 13:02:20 -05:00
}
2025-04-02 22:18:35 -05:00
async getData(weatherParameters, refresh) {
if (!super.getData(weatherParameters, refresh)) return;
2026-04-07 18:08:38 -04:00
try {
if (!window.L) {
throw new Error('Leaflet is not available');
}
2026-04-07 18:08:38 -04:00
await this.ensureMap();
this.map.invalidateSize();
this.map.setView([this.weatherParameters.latitude, this.weatherParameters.longitude], 6);
this.locationMarker = setPrimaryLocationMarker(
this.map,
this.locationMarker,
this.weatherParameters.latitude,
this.weatherParameters.longitude,
);
this.nearbyMarkers = clearMarkers(this.map, this.nearbyMarkers);
this.nearbyMarkersKey = '';
this.timing.totalScreens = 1;
this.setStatus(STATUS.loaded);
} catch (error) {
console.error(`Failed to initialize regional observations: ${error.message}`);
this.nearbyMarkers = clearMarkers(this.map, this.nearbyMarkers);
this.timing.totalScreens = 0;
if (this.isEnabled) this.setStatus(STATUS.failed);
}
2020-09-04 13:02:20 -05:00
}
2026-04-07 18:08:38 -04:00
async refreshNearbyMarkers() {
if (!this.map || !this.active) return;
2020-09-04 13:02:20 -05:00
2026-04-07 18:08:38 -04:00
this.map.invalidateSize(false);
this.map.setView([this.weatherParameters.latitude, this.weatherParameters.longitude], 6);
2020-09-04 13:02:20 -05:00
2026-04-07 18:08:38 -04:00
const bounds = this.map.getBounds();
const markerKey = [
this.weatherParameters.latitude.toFixed(2),
this.weatherParameters.longitude.toFixed(2),
bounds.getSouth().toFixed(2),
bounds.getWest().toFixed(2),
bounds.getNorth().toFixed(2),
bounds.getEast().toFixed(2),
].join(':');
2020-09-04 13:02:20 -05:00
2026-04-07 18:08:38 -04:00
if (this.nearbyMarkers.length > 0 && this.nearbyMarkersKey === markerKey) return;
2022-08-04 16:30:13 -05:00
2026-04-07 18:08:38 -04:00
this.nearbyMarkers = clearMarkers(this.map, this.nearbyMarkers);
this.nearbyMarkers = await loadNearbyObservationMarkers(this.map, {
latitude: this.weatherParameters.latitude,
longitude: this.weatherParameters.longitude,
});
this.nearbyMarkers.forEach((marker) => marker.addTo(this.map));
this.nearbyMarkersKey = markerKey;
}
2022-08-04 16:30:13 -05:00
2026-04-07 18:08:38 -04:00
async ensureMap() {
if (this.map) return;
2022-08-04 16:30:13 -05:00
2026-04-07 18:08:38 -04:00
const mapElement = this.elem.querySelector('.leaflet-map');
if (!mapElement) {
throw new Error('Regional observations map container not found');
}
2023-01-06 14:39:39 -06:00
2026-04-07 18:08:38 -04:00
this.map = createMap(mapElement);
({ baseLayer: this.baseLayer, boundaryLayer: this.boundaryLayer } = addBaseLayers(this.map));
}
2022-08-04 16:30:13 -05:00
2026-04-07 18:08:38 -04:00
drawCanvas() {
super.drawCanvas();
const titleTop = this.elem.querySelector('.title.dual .top');
const titleBottom = this.elem.querySelector('.title.dual .bottom');
titleTop.innerHTML = 'Regional';
titleBottom.innerHTML = 'Observations';
2022-08-04 16:30:13 -05:00
2026-04-07 18:08:38 -04:00
if (this.map) {
this.map.invalidateSize(false);
}
2022-08-04 16:30:13 -05:00
2020-09-04 13:02:20 -05:00
this.finishDraw();
}
2022-11-22 16:19:10 -06:00
2026-04-07 18:08:38 -04:00
async showCanvas(navCmd) {
super.showCanvas(navCmd);
await this.refreshNearbyMarkers();
}
2026-04-07 18:08:38 -04:00
}
2022-12-12 15:41:28 -06:00
2022-12-14 16:28:33 -06:00
registerDisplay(new RegionalForecast(6, 'regional-forecast'));