fixed weather service
FarmMaps.Develop/FarmMapsLib/pipeline/head There was a failure building this commit Details

2022.01
Mark van der Wal 2020-09-02 15:23:54 +02:00
parent 80522a3a2f
commit cdc900d5fd
1 changed files with 21 additions and 18 deletions

View File

@ -1,19 +1,19 @@
import {Injectable} from '@angular/core';
import {HttpClient} from '@angular/common/http';
import {AppConfig} from '../shared/app.config';
import {Observable} from 'rxjs';
import {WeatherCurrentObservation} from '../models/weatherCurrentObservation';
import {DatePipe} from '@angular/common';
import {WeatherData} from '../models/WeatherData';
import {GeoJSON} from 'ol/format';
import {map, switchMap} from 'rxjs/operators';
import {getCenter} from 'ol/extent';
import {DatePipe} from '@angular/common';
import {AppConfig} from '../shared/app.config';
import {WeatherCurrentObservation} from '../models/weatherCurrentObservation';
import {IItem} from '../models/item';
import {WeatherData} from '../models/WeatherData';
@Injectable({
providedIn: 'root',
})
export class WeatherService {
export class WeatherTempService {
private apiCurrentObservationUrl = 'currentobservation';
private apiObservation = 'observation';
private apiForecast = 'forecast';
@ -36,14 +36,15 @@ export class WeatherService {
const geometry = this.format.readGeometry(item.geometry);
const centroid = getCenter(geometry.getExtent());
const currentDate = new Date(Date.now());
const sd = new Date(Date.now());
const ed = new Date(Date.now());
sd.setDate((currentDate.getDate() - daysBefore));
ed.setDate((currentDate.getDate() + daysAfter));
const startDate = new Date();
startDate.setDate(startDate.getDate() - daysBefore);
const startDateString = `${this.datePipe.transform(startDate, 'yyyy-MM-dd')}T00:00:00`;
return this.getWeatherRange(centroid, this.datePipe.transform(sd, 'yyyy-MM-ddThh:mm:ss'),
this.datePipe.transform(ed, 'yyyy-MM-ddThh:mm:ss'));
const endDate = new Date();
endDate.setDate(endDate.getDate() + daysAfter);
const endDateString = `${this.datePipe.transform(endDate, 'yyyy-MM-dd')}T24:00:00`;
return this.getWeatherRange(centroid, startDateString, endDateString);
}
public getWeatherRange(centroid: number[], startDate: string, endDate: string): Observable<WeatherData[]> {
@ -52,9 +53,12 @@ export class WeatherService {
// weather does not support UTC format, also remove Z
const sd = encodeURIComponent(this.removeUTCZ(startDate));
const ed = encodeURIComponent(this.removeUTCZ(endDate));
const historical = `${endpoint}${this.apiObservation}/?c=${centroid[0]},${centroid[1]}&sd=${sd}&ed=${ed}&t=observation&interval=hourly&key=${apiKey}`;
const edHistoricalDate = new Date();
const edHistoricalString = `${this.datePipe.transform(edHistoricalDate, 'yyyy-MM-dd')}T${edHistoricalDate.getHours()}:00:00`;
const edHistorical = encodeURIComponent(edHistoricalString);
const historical = `${endpoint}${this.apiObservation}/?c=${centroid[0]},${centroid[1]}&sd=${sd}&ed=${edHistorical}&t=observation&interval=hourly&key=${apiKey}`;
const forecast = `${endpoint}${this.apiForecast}/?c=${centroid[0]},${centroid[1]}&interval=hourly&key=${apiKey}`;
return this.httpClient.get<any[]>(historical).pipe(
@ -62,13 +66,12 @@ export class WeatherService {
switchMap(h => {
return this.httpClient.get<any[]>(forecast)
.pipe(
map(f => this.hourlyToDaily([...h, ...f.filter(fd => fd.time <= endDate)])
));
map(f => [...h, ...f.filter(fd => fd.time <= endDate)] ));
})
);
}
private hourlyToDaily(hourlyWeatherData: any[]): WeatherData[] {
public hourlyToDaily(hourlyWeatherData: any[]): WeatherData[] {
const days = this.groupBy(hourlyWeatherData, hi => hi.time.split('T')[0]);
return Object.entries(days)
.reduce ( (result, entry) => {
@ -110,7 +113,7 @@ export class WeatherService {
...(result[group] || []),
item,
],
});},
}); },
{},
);
}