add weather gen basics
This commit is contained in:
@@ -1,9 +1,10 @@
|
||||
<script lang="ts">
|
||||
import { page } from '$app/state';
|
||||
import WeatherGen from '$lib/generators/weather/WeatherGen.svelte';
|
||||
|
||||
const lat = $derived(page.url.searchParams.get('lat'));
|
||||
const long = $derived(page.url.searchParams.get('long'));
|
||||
import type { PageProps } from './$types';
|
||||
|
||||
$inspect(lat);
|
||||
$inspect(long);
|
||||
const { data }: PageProps = $props();
|
||||
const currentWeather = data.current;
|
||||
</script>
|
||||
|
||||
<WeatherGen {currentWeather}></WeatherGen>
|
||||
|
||||
@@ -1,32 +1,38 @@
|
||||
//TODO: LOAD ALL API DATA FOR WEATHER, AIR QUALITY, ETC
|
||||
import { fetchWeatherApi } from 'openmeteo';
|
||||
import type { PageLoad } from './$types';
|
||||
|
||||
const params = {
|
||||
"latitude": 52.52,
|
||||
"longitude": 13.41,
|
||||
"current": ["temperature_2m", "relative_humidity_2m", "is_day", "precipitation", "cloud_cover", "pressure_msl", "wind_speed_10m"]
|
||||
};
|
||||
const url = "https://api.open-meteo.com/v1/forecast";
|
||||
const responses = await fetchWeatherApi(url, params);
|
||||
export const load: PageLoad = async ({ url }) => {
|
||||
|
||||
// Process first location. Add a for-loop for multiple locations or weather models
|
||||
const response = responses[0];
|
||||
const lat = url.searchParams.get('lat');
|
||||
const long = url.searchParams.get('long');
|
||||
|
||||
// Attributes for timezone and location
|
||||
const utcOffsetSeconds = response.utcOffsetSeconds();
|
||||
const apiParams = {
|
||||
"latitude": lat,
|
||||
"longitude": long,
|
||||
"current": ["temperature_2m", "relative_humidity_2m", "is_day", "precipitation", "cloud_cover", "pressure_msl", "wind_speed_10m"]
|
||||
};
|
||||
|
||||
const current = response.current()!;
|
||||
const apiUrl = "https://api.open-meteo.com/v1/forecast";
|
||||
const responses = await fetchWeatherApi(apiUrl, apiParams);
|
||||
|
||||
// Note: The order of weather variables in the URL query and the indices below need to match!
|
||||
const weatherData = {
|
||||
current: {
|
||||
time: new Date((Number(current.time()) + utcOffsetSeconds) * 1000),
|
||||
temperature2m: current.variables(0)!.value(),
|
||||
relativeHumidity2m: current.variables(1)!.value(),
|
||||
isDay: current.variables(2)!.value(),
|
||||
precipitation: current.variables(3)!.value(),
|
||||
cloudCover: current.variables(4)!.value(),
|
||||
pressureMsl: current.variables(5)!.value(),
|
||||
windSpeed10m: current.variables(6)!.value(),
|
||||
},
|
||||
const response = responses[0];
|
||||
const utcOffsetSeconds = response.utcOffsetSeconds();
|
||||
|
||||
const current = response.current()!;
|
||||
|
||||
// Note: The order of weather variables in the URL query and the indices below need to match!
|
||||
const weatherData: WeatherData = {
|
||||
current: {
|
||||
time: new Date((Number(current.time()) + utcOffsetSeconds) * 1000),
|
||||
temperature2m: current.variables(0)!.value(),
|
||||
relativeHumidity2m: current.variables(1)!.value(),
|
||||
isDay: current.variables(2)!.value(),
|
||||
precipitation: current.variables(3)!.value(),
|
||||
cloudCover: current.variables(4)!.value(),
|
||||
pressureMsl: current.variables(5)!.value(),
|
||||
windSpeed10m: current.variables(6)!.value(),
|
||||
},
|
||||
};
|
||||
|
||||
return weatherData
|
||||
};
|
||||
Reference in New Issue
Block a user