Skip to content

Commit 3826f98

Browse files
Cleanup
1 parent a03eebf commit 3826f98

File tree

2 files changed

+80
-87
lines changed

2 files changed

+80
-87
lines changed

src/layers/LiveWeather.svelte

Lines changed: 80 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,148 +1,140 @@
11
<script lang="ts" context="module">
22
interface SubjectValue {
3-
humidity: number;
4-
issuedAt: string; // ISO-8601 date
3+
humidity: number
4+
issuedAt: string // ISO-8601 date
55
pressure: {
6-
atSeaLevel: string;
7-
};
8-
rainfall: string;
9-
temp: number;
6+
atSeaLevel: string
7+
}
8+
rainfall: string
9+
temp: number
1010
wind: {
11-
averageSpeed: 6;
12-
direction: string; // Compass direction
13-
strength: string; // Description of wind strength.
14-
};
11+
averageSpeed: 6
12+
direction: string // Compass direction
13+
strength: string // Description of wind strength.
14+
}
1515
}
1616
1717
interface MetServiceMapMarkerSubject {
18-
asAt: string; // ISO-8601 date
19-
value: SubjectValue;
20-
type: "local" | "traffic-camera";
18+
asAt: string // ISO-8601 date
19+
value: SubjectValue
20+
type: 'local' | 'traffic-camera'
2121
}
2222
2323
interface MetServiceMapMarker {
24-
label: string;
25-
point: [number, number];
26-
subject: MetServiceMapMarkerSubject[];
24+
label: string
25+
point: [number, number]
26+
subject: MetServiceMapMarkerSubject[]
2727
}
2828
2929
interface MetServiceLiveWeatherResponse {
30-
isSkiSeason: boolean;
30+
isSkiSeason: boolean
3131
layout: {
3232
primary: {
3333
map: {
34-
markers: MetServiceMapMarker[];
35-
};
36-
};
37-
};
34+
markers: MetServiceMapMarker[]
35+
}
36+
}
37+
}
3838
}
3939
</script>
4040

4141
<script lang="ts">
42-
import Feature from "ol/Feature";
43-
import type { FeatureLike } from "ol/Feature";
44-
import { fromLonLat, get, toLonLat } from "ol/proj";
45-
import Point from "ol/geom/Point";
46-
import { corsFetch } from "../utils/cors";
47-
import { Style, Circle, Stroke, Fill, Text } from "ol/style";
48-
import VectorLayer from "../ol/VectorLayer.svelte";
49-
import VectorSource from "ol/source/Vector";
50-
import Popup from "../ol/Popup.svelte";
51-
import type { Coordinate } from "ol/coordinate";
52-
import RelativeTime from "../components/RelativeTime.svelte";
42+
import Feature from 'ol/Feature'
43+
import { fromLonLat } from 'ol/proj'
44+
import Point from 'ol/geom/Point'
45+
import { Style, Circle, Stroke, Fill, Text } from 'ol/style'
46+
import VectorLayer from '../ol/VectorLayer.svelte'
47+
import VectorSource from 'ol/source/Vector'
48+
import Popup from '../ol/Popup.svelte'
49+
import type { Coordinate } from 'ol/coordinate'
50+
import RelativeTime from '../components/RelativeTime.svelte'
5351
5452
const getWeatherIcon = (value: SubjectValue): string => {
5553
const options = {
56-
sunCloud: "🌤️",
57-
rain: "🌧️",
58-
cloud: "☁️",
59-
storm: "⛈️",
60-
snow: "🌨️",
61-
sun: "☀️",
62-
wind: "💨",
63-
};
54+
sunCloud: '🌤️',
55+
rain: '🌧️',
56+
cloud: '☁️',
57+
storm: '⛈️',
58+
snow: '🌨️',
59+
sun: '☀️',
60+
wind: '💨',
61+
}
6462
65-
const rain = parseFloat(value.rainfall);
63+
const rain = parseFloat(value.rainfall)
6664
6765
if (rain > 0) {
6866
// I guess it'll be snow/hail if its cold?
69-
return value.temp < 0 ? options.snow : options.rain;
67+
return value.temp < 0 ? options.snow : options.rain
7068
}
7169
7270
// I guess wind greater than 20km/h is windy?
73-
if (value.wind && value.wind.averageSpeed > 20) return options.wind;
71+
if (value.wind && value.wind.averageSpeed > 20) return options.wind
7472
7573
// I don't actually have the data to know if it's cloudy, so this seems safe.
76-
return options.sunCloud;
77-
};
74+
return options.sunCloud
75+
}
7876
7977
const getFeatures = async () => {
80-
const url = "/api/weather";
81-
const response = await fetch(url);
82-
const data = (await response.json()) as MetServiceLiveWeatherResponse;
78+
const url = '/api/weather'
79+
const response = await fetch(url)
80+
const data = (await response.json()) as MetServiceLiveWeatherResponse
8381
84-
const observations = data?.layout?.primary?.map?.markers;
85-
if (!observations) return null;
82+
const observations = data?.layout?.primary?.map?.markers
83+
if (!observations) return null
8684
8785
return (
8886
observations
8987
// Filter out traffic cameras and stuff.
9088
.filter((marker) => {
91-
if (!marker.subject.length) return false;
89+
if (!marker.subject.length) return false
9290
93-
if (marker.subject[0].type === "traffic-camera") return false;
91+
if (marker.subject[0].type === 'traffic-camera') return false
9492
95-
return true;
93+
return true
9694
})
9795
.map((marker) => {
98-
const coords = fromLonLat([marker.point[1], marker.point[0]]);
99-
const feature = new Feature(new Point(coords));
100-
const subject = marker.subject[0];
101-
const observation = subject?.value;
96+
const coords = fromLonLat([marker.point[1], marker.point[0]])
97+
const feature = new Feature(new Point(coords))
98+
const subject = marker.subject[0]
99+
const observation = subject?.value
102100
if (observation && !observation.issuedAt)
103-
observation.issuedAt = subject.asAt;
101+
observation.issuedAt = subject.asAt
104102
105103
feature.setStyle(
106104
new Style({
107105
image: new Circle({
108106
radius: 12,
109-
fill: new Fill({ color: "blue" }),
110-
stroke: new Stroke({ color: "white" }),
107+
fill: new Fill({ color: 'blue' }),
108+
stroke: new Stroke({ color: 'white' }),
111109
}),
112110
text: new Text({
113-
fill: new Fill({ color: "white" }),
111+
fill: new Fill({ color: 'white' }),
114112
text: getWeatherIcon(observation),
115113
}),
116114
})
117-
);
118-
feature.set("observation", observation);
119-
feature.set("subject", subject);
115+
)
116+
feature.set('observation', observation)
117+
feature.set('subject', subject)
120118
121-
return feature;
119+
return feature
122120
})
123-
);
124-
};
121+
)
122+
}
125123
126-
let currentObservation: SubjectValue;
127-
let coords: Coordinate;
124+
let currentObservation: SubjectValue
125+
let coords: Coordinate
128126
</script>
129127

130-
<style>
131-
.weather {
132-
white-space: nowrap;
133-
}
134-
</style>
135-
136128
{#await getFeatures() then features}
137129
<VectorLayer
138130
id="weather"
139131
title="Live Weather"
140132
visible={false}
141133
on:featureClick={(event) => {
142-
const feature = event.detail.feature;
143-
const geometry = feature.getGeometry();
144-
currentObservation = feature.get('observation');
145-
coords = geometry.getCoordinates();
134+
const feature = event.detail.feature
135+
const geometry = feature.getGeometry()
136+
currentObservation = feature.get('observation')
137+
coords = geometry.getCoordinates()
146138
}}
147139
source={new VectorSource({ features })} />
148140
{/await}
@@ -173,10 +165,17 @@ import RelativeTime from "../components/RelativeTime.svelte";
173165
<p>
174166
<b>💨 Wind:</b>
175167
{currentObservation.wind.strength}
176-
{currentObservation.wind.averageSpeed}km/h {currentObservation.wind.direction}
168+
{currentObservation.wind.averageSpeed}km/h {currentObservation.wind
169+
.direction}
177170
</p>
178171
{/if}
179-
<i>Updated: <RelativeTime time={currentObservation.issuedAt}/></i>
172+
<i>Updated: <RelativeTime time={currentObservation.issuedAt} /></i>
180173
</div>
181174
</Popup>
182175
{/if}
176+
177+
<style>
178+
.weather {
179+
white-space: nowrap;
180+
}
181+
</style>

src/utils/cors.ts

Lines changed: 0 additions & 6 deletions
This file was deleted.

0 commit comments

Comments
 (0)