initial version of streba 2
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
import { error, redirect } from '@sveltejs/kit';
|
||||
import { db, deleteActivity, getActivity } from '$lib/server/db';
|
||||
import { haversine } from '$lib/server/gpx';
|
||||
import type { Actions, PageServerLoad } from './$types';
|
||||
import type { PeakRow } from '$lib/server/db';
|
||||
|
||||
export const load: PageServerLoad = ({ params }) => {
|
||||
const activity = getActivity(Number(params.id));
|
||||
if (!activity) error(404, 'Activity not found');
|
||||
|
||||
const points = JSON.parse(activity.points) as {
|
||||
lat: number;
|
||||
lon: number;
|
||||
ele: number | null;
|
||||
t: number | null;
|
||||
}[];
|
||||
|
||||
const latlngs = points.map((p) => [p.lat, p.lon] as [number, number]);
|
||||
|
||||
// cumulative-distance elevation profile
|
||||
const profile: { d: number; ele: number }[] = [];
|
||||
let dist = 0;
|
||||
for (let i = 0; i < points.length; i++) {
|
||||
if (i > 0) {
|
||||
dist += haversine(points[i - 1].lat, points[i - 1].lon, points[i].lat, points[i].lon);
|
||||
}
|
||||
if (points[i].ele !== null) profile.push({ d: dist, ele: points[i].ele! });
|
||||
}
|
||||
|
||||
const bagged = db
|
||||
.prepare('SELECT * FROM peaks WHERE activity_id = ?')
|
||||
.all(activity.id) as PeakRow[];
|
||||
|
||||
return {
|
||||
activity: { ...activity, points: undefined },
|
||||
latlngs,
|
||||
profile,
|
||||
bagged: bagged.map((p) => ({
|
||||
id: p.id,
|
||||
name: p.name,
|
||||
elevation_m: p.elevation_m,
|
||||
lat: p.lat,
|
||||
lon: p.lon,
|
||||
climbed: true
|
||||
}))
|
||||
};
|
||||
};
|
||||
|
||||
export const actions: Actions = {
|
||||
delete: async ({ params }) => {
|
||||
deleteActivity(Number(params.id));
|
||||
redirect(303, '/activities');
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user