2020-02-04 12:54:53 +00:00
|
|
|
import {Injectable} from '@angular/core';
|
|
|
|
import {HttpClient} from '@angular/common/http';
|
|
|
|
import {Observable} from 'rxjs';
|
2020-07-21 14:57:11 +00:00
|
|
|
import {GeoJSON} from 'ol/format';
|
2020-09-03 08:19:12 +00:00
|
|
|
import {map, switchMap, tap} from 'rxjs/operators';
|
2020-07-21 14:57:11 +00:00
|
|
|
import {getCenter} from 'ol/extent';
|
2020-09-03 08:19:12 +00:00
|
|
|
import * as moment from 'moment';
|
2020-09-02 13:23:54 +00:00
|
|
|
import {AppConfig} from '../shared/app.config';
|
|
|
|
import {WeatherCurrentObservation} from '../models/weatherCurrentObservation';
|
2020-07-21 15:06:58 +00:00
|
|
|
import {IItem} from '../models/item';
|
2020-09-02 14:21:49 +00:00
|
|
|
import {HourlyWeatherData, WeatherData} from '../models/WeatherData';
|
2020-02-04 12:54:53 +00:00
|
|
|
|
|
|
|
@Injectable({
|
|
|
|
providedIn: 'root',
|
|
|
|
})
|
2020-09-02 13:28:12 +00:00
|
|
|
export class WeatherService {
|
2020-07-21 14:57:11 +00:00
|
|
|
private apiCurrentObservationUrl = 'currentobservation';
|
|
|
|
private apiObservation = 'observation';
|
|
|
|
private apiForecast = 'forecast';
|
2020-02-04 12:54:53 +00:00
|
|
|
|
2020-07-21 14:57:11 +00:00
|
|
|
private format: GeoJSON;
|
|
|
|
|
2020-09-03 08:19:12 +00:00
|
|
|
constructor(public httpClient: HttpClient, public appConfig: AppConfig) {
|
2020-07-21 14:57:11 +00:00
|
|
|
this.format = new GeoJSON();
|
2020-02-04 12:54:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public GetCurrentObservation(centroid: number[]): Observable<WeatherCurrentObservation> {
|
2020-02-04 16:38:04 +00:00
|
|
|
const endpoint = this.appConfig.getConfig('weatherApiEndPoint');
|
|
|
|
const apiKey = this.appConfig.getConfig('weatherApiKey');
|
2020-07-21 14:57:11 +00:00
|
|
|
const observationUrl = `${endpoint}${this.apiCurrentObservationUrl}/?c=${centroid[0]},${centroid[1]}&key=${apiKey}`;
|
2020-02-04 12:54:53 +00:00
|
|
|
|
|
|
|
return this.httpClient.get<WeatherCurrentObservation>(observationUrl);
|
|
|
|
}
|
2020-07-21 14:57:11 +00:00
|
|
|
|
2020-09-02 14:21:49 +00:00
|
|
|
public getWeatherRangeForItem(item: IItem, daysBefore = 10, daysAfter = 10): Observable<HourlyWeatherData[]> {
|
2020-07-21 14:57:11 +00:00
|
|
|
const geometry = this.format.readGeometry(item.geometry);
|
|
|
|
const centroid = getCenter(geometry.getExtent());
|
|
|
|
|
2020-09-02 13:23:54 +00:00
|
|
|
const startDate = new Date();
|
|
|
|
startDate.setDate(startDate.getDate() - daysBefore);
|
2020-09-03 08:19:12 +00:00
|
|
|
startDate.setHours(0, 0, 0, 0);
|
|
|
|
const startDateString = moment(startDate).local().format('YYYY-MM-DD[T]HH:mm:ss');
|
|
|
|
console.log(startDateString);
|
2020-07-21 14:57:11 +00:00
|
|
|
|
2020-09-02 13:23:54 +00:00
|
|
|
const endDate = new Date();
|
|
|
|
endDate.setDate(endDate.getDate() + daysAfter);
|
2020-09-03 08:19:12 +00:00
|
|
|
endDate.setHours(23, 0, 0, 0);
|
|
|
|
const endDateString = moment(endDate).local().format('YYYY-MM-DD[T]HH:mm:ss');
|
|
|
|
console.log(endDateString);
|
2020-09-02 13:23:54 +00:00
|
|
|
|
|
|
|
return this.getWeatherRange(centroid, startDateString, endDateString);
|
2020-07-21 14:57:11 +00:00
|
|
|
}
|
|
|
|
|
2020-09-02 14:21:49 +00:00
|
|
|
public getWeatherRange(centroid: number[], startDate: string, endDate: string): Observable<HourlyWeatherData[]> {
|
2020-07-21 14:57:11 +00:00
|
|
|
const endpoint = this.appConfig.getConfig('weatherApiEndPoint');
|
|
|
|
const apiKey = this.appConfig.getConfig('weatherApiKey');
|
|
|
|
|
|
|
|
// weather does not support UTC format, also remove Z
|
2020-09-03 08:19:12 +00:00
|
|
|
const sd = encodeURIComponent(startDate);
|
2020-07-21 14:57:11 +00:00
|
|
|
|
2020-09-02 13:23:54 +00:00
|
|
|
const edHistoricalDate = new Date();
|
2020-09-03 08:19:12 +00:00
|
|
|
edHistoricalDate.setHours(edHistoricalDate.getHours(), 0, 0, 0);
|
|
|
|
const edHistorical = moment(edHistoricalDate).local().format('YYYY-MM-DD[T]HH:mm:ss');
|
2020-09-02 13:23:54 +00:00
|
|
|
|
|
|
|
const historical = `${endpoint}${this.apiObservation}/?c=${centroid[0]},${centroid[1]}&sd=${sd}&ed=${edHistorical}&t=observation&interval=hourly&key=${apiKey}`;
|
2020-07-22 21:04:06 +00:00
|
|
|
const forecast = `${endpoint}${this.apiForecast}/?c=${centroid[0]},${centroid[1]}&interval=hourly&key=${apiKey}`;
|
2020-07-21 14:57:11 +00:00
|
|
|
|
|
|
|
return this.httpClient.get<any[]>(historical).pipe(
|
2020-07-22 21:04:06 +00:00
|
|
|
map(h => h.map(d => ({...d, rain: d.rainPastHour}))),
|
2020-07-21 14:57:11 +00:00
|
|
|
switchMap(h => {
|
|
|
|
return this.httpClient.get<any[]>(forecast)
|
|
|
|
.pipe(
|
2020-09-03 08:19:12 +00:00
|
|
|
tap(v => console.log(v)),
|
2020-09-02 13:23:54 +00:00
|
|
|
map(f => [...h, ...f.filter(fd => fd.time <= endDate)] ));
|
2020-07-21 14:57:11 +00:00
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-09-02 14:21:49 +00:00
|
|
|
public hourlyToDaily(hourlyWeatherData: HourlyWeatherData[]): WeatherData[] {
|
2020-07-22 21:04:06 +00:00
|
|
|
const days = this.groupBy(hourlyWeatherData, hi => hi.time.split('T')[0]);
|
|
|
|
return Object.entries(days)
|
|
|
|
.reduce ( (result, entry) => {
|
|
|
|
const wData = entry[1];
|
|
|
|
|
|
|
|
const sortData = wData.sort(d => new Date(d.time).getHours() - 12);
|
|
|
|
const closestToTwelveOClockData = sortData[0];
|
|
|
|
if (closestToTwelveOClockData.length === 0) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
const bestCardinal = closestToTwelveOClockData.windDirectionCardinal;
|
|
|
|
const bestWindDirection = closestToTwelveOClockData.windDirection;
|
|
|
|
const bestIconCode = closestToTwelveOClockData.iconCode;
|
|
|
|
return [
|
|
|
|
...result,
|
|
|
|
{
|
|
|
|
date: entry[0],
|
|
|
|
iconCode: bestIconCode,
|
|
|
|
minTemperature: Math.min(...wData.map(d => d.temperature)),
|
|
|
|
maxTemperature: Math.max(...wData.map(d => d.temperature)),
|
|
|
|
relHumidity: wData.reduce( (r, e) => r + e.relativeHumidity, 0) / wData.length,
|
2020-07-25 07:57:24 +00:00
|
|
|
precipitation: wData.reduce( (r, e) => r + e.rain, 0),
|
2020-07-22 21:04:06 +00:00
|
|
|
wSpeed: wData.reduce( (r, e) => r + e.windSpeed, 0) / wData.length,
|
|
|
|
wDir: bestWindDirection,
|
|
|
|
wCardinal: bestCardinal
|
|
|
|
}
|
|
|
|
];
|
|
|
|
}, []);
|
2020-07-21 14:57:11 +00:00
|
|
|
}
|
|
|
|
|
2020-07-22 21:04:06 +00:00
|
|
|
private groupBy(items: any[], selector: (item) => any): any[] {
|
|
|
|
return items.reduce(
|
|
|
|
(result, item) => {
|
|
|
|
const group = selector(item);
|
|
|
|
return ({
|
|
|
|
...result,
|
|
|
|
[group]: [
|
|
|
|
...(result[group] || []),
|
|
|
|
item,
|
|
|
|
],
|
2020-09-02 13:23:54 +00:00
|
|
|
}); },
|
2020-07-22 21:04:06 +00:00
|
|
|
{},
|
|
|
|
);
|
2020-07-21 14:57:11 +00:00
|
|
|
}
|
2020-02-04 12:54:53 +00:00
|
|
|
}
|