mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-04 09:54:14 +00:00
feat(places): export a trip as GPX (#1907)
* feat(places): hand a trip back as GPX TREK reads GPX but never wrote it, so a planned trip could not reach the offline map or the handheld it was planned for. The importer already fixes the mapping: a wpt is a place, a rte or trk is a place with route_geometry, so writing is that reversed. Days have no import counterpart and become routes of their stops in order, which is the part that puts a plan on a device. XMLBuilder comes from the same fast-xml-parser the importer parses with, so nothing new is pulled in. * feat(dayplan): a GPX button beside PDF and ICS Scopes are worded by what someone wants on their device rather than by the GPX element: the whole trip, just the places for an offline map, just the days to follow a plan. An empty trip says so instead of reporting a failed download. * feat(mobile): GPX in the export sheet, which is no longer calendar-only The sheet held the PDF export next to the calendar entries and was still called Export Calendar; with GPX beside them the name was simply wrong. * i18n(dayplan): translate the GPX export into all locales * docs(map): document the GPX export * fix(dayplan): drop the GPX tooltip and centre both export menus The tooltip sat under the cursor exactly where the menu opens, so hovering the button produced both at once. The label lives on as an aria-label. Both menus now open centred under their button instead of hanging off its right edge.
This commit is contained in:
Binary file not shown.
@@ -10,11 +10,13 @@ import {
|
||||
Post,
|
||||
Put,
|
||||
Query,
|
||||
Res,
|
||||
UploadedFile,
|
||||
UseGuards,
|
||||
UseInterceptors,
|
||||
} from '@nestjs/common';
|
||||
import { FileInterceptor } from '@nestjs/platform-express';
|
||||
import type { Response } from 'express';
|
||||
import { isDemoWriteBlocked, DEMO_WRITE_ERROR } from '../common/demo-write';
|
||||
import { RuntimeEnvService } from '../app-config/runtime-env.service';
|
||||
import { memoryStorage } from 'multer';
|
||||
@@ -31,6 +33,7 @@ import {
|
||||
PlaceBulkDeleteDto,
|
||||
PlaceBulkUpdateDto,
|
||||
PlaceCreateDto,
|
||||
PlaceExportGpxDto,
|
||||
PlaceImportGpxDto,
|
||||
PlaceImportListDto,
|
||||
PlaceImportMapDto,
|
||||
@@ -165,6 +168,34 @@ export class PlacesController {
|
||||
return { places: result.places, count: result.count, skipped: result.skipped };
|
||||
}
|
||||
|
||||
/**
|
||||
* The trip as GPX, for Organic Maps, a handheld or anything else that reads it.
|
||||
* A read, so viewing the trip is enough; no edit permission required. Declared
|
||||
* before the `:id` routes so the literal path wins.
|
||||
*/
|
||||
@Get('export.gpx')
|
||||
exportGpx(
|
||||
@CurrentUser() user: User,
|
||||
@Param('tripId') tripId: string,
|
||||
@Query() query: PlaceExportGpxDto,
|
||||
@Res() res: Response,
|
||||
) {
|
||||
this.requireTrip(tripId, user);
|
||||
const waypoints = parseBool(query.waypoints, true);
|
||||
const tracks = parseBool(query.tracks, true);
|
||||
const dayRoutes = parseBool(query.dayRoutes, true);
|
||||
if (!waypoints && !tracks && !dayRoutes) {
|
||||
throw new HttpException({ error: 'No export types selected' }, 400);
|
||||
}
|
||||
const result = this.places.exportGpx(tripId, { waypoints, tracks, dayRoutes });
|
||||
if (!result) {
|
||||
throw new HttpException({ error: 'Nothing to export' }, 404);
|
||||
}
|
||||
res.setHeader('Content-Type', 'application/gpx+xml; charset=utf-8');
|
||||
res.setHeader('Content-Disposition', `attachment; filename="${result.filename}"`);
|
||||
res.send(result.gpx);
|
||||
}
|
||||
|
||||
@Post('import/map')
|
||||
@UseInterceptors(FileInterceptor('file', UPLOAD))
|
||||
async importMap(
|
||||
|
||||
@@ -6,6 +6,7 @@ import {
|
||||
placeImportGpxRequestSchema,
|
||||
placeImportListRequestSchema,
|
||||
placeImportMapRequestSchema,
|
||||
placeExportGpxRequestSchema,
|
||||
placeRatingRequestSchema,
|
||||
placeUpdateRequestSchema,
|
||||
} from '@trek/shared';
|
||||
@@ -32,3 +33,4 @@ export class PlaceBulkUpdateDto extends createZodDto(placeBulkUpdateRequestSchem
|
||||
export class PlaceImportListDto extends createZodDto(placeImportListRequestSchema) {}
|
||||
export class PlaceImportGpxDto extends createZodDto(placeImportGpxRequestSchema) {}
|
||||
export class PlaceImportMapDto extends createZodDto(placeImportMapRequestSchema) {}
|
||||
export class PlaceExportGpxDto extends createZodDto(placeExportGpxRequestSchema) {}
|
||||
|
||||
@@ -19,6 +19,8 @@ import {
|
||||
parsePlacemarkNode,
|
||||
resolveCategoryIdForFolder,
|
||||
} from './kml-import.helpers';
|
||||
import { buildGpx, gpxFilename } from './gpx-export.helpers';
|
||||
import type { GpxExportDay, GpxExportOptions, GpxExportPlace } from './gpx-export.helpers';
|
||||
import { UnsplashService } from '../unsplash/unsplash.service';
|
||||
import { PlacePhotoCacheService } from '../place-photos/place-photo-cache.service';
|
||||
import { type UpdateConflict, isUpdateConflict } from '../common/conflictResult';
|
||||
@@ -471,6 +473,54 @@ export class PlacesService {
|
||||
return this.colorizeImportedTracks(tripId, this.importGpxRows(tripId, fileBuffer, opts));
|
||||
}
|
||||
|
||||
/**
|
||||
* The trip as a GPX document, the mirror of importGpx. Places without geometry
|
||||
* become waypoints, places carrying route_geometry become tracks, and each planned
|
||||
* day becomes a route of its stops in order, which is the part that has no import
|
||||
* counterpart and the reason to bother: a planned day on a handheld.
|
||||
*
|
||||
* Returns null when the selection yields nothing, so the caller answers 404 rather
|
||||
* than handing over a file that imports as nothing on the other end.
|
||||
*/
|
||||
exportGpx(tripId: string, opts: GpxExportOptions = {}): { gpx: string; filename: string } | null {
|
||||
const trip = this.dbs.get<{ title: string }>('SELECT title FROM trips WHERE id = ?', tripId);
|
||||
if (!trip) return null;
|
||||
|
||||
const places = this.dbs.all<GpxExportPlace>(`
|
||||
SELECT p.name, p.description, p.address, p.lat, p.lng, p.route_geometry, c.name AS category
|
||||
FROM places p
|
||||
LEFT JOIN categories c ON c.id = p.category_id
|
||||
WHERE p.trip_id = ?
|
||||
ORDER BY p.id
|
||||
`, tripId);
|
||||
|
||||
// One row per stop, ordered the way the day plan draws it, then folded into days.
|
||||
const stops = this.dbs.all<{
|
||||
day_number: number; date: string | null; title: string | null;
|
||||
name: string; lat: number; lng: number;
|
||||
}>(`
|
||||
SELECT d.day_number, d.date, d.title, p.name, p.lat, p.lng
|
||||
FROM days d
|
||||
JOIN day_assignments da ON da.day_id = d.id
|
||||
JOIN places p ON p.id = da.place_id
|
||||
WHERE d.trip_id = ? AND p.lat IS NOT NULL AND p.lng IS NOT NULL
|
||||
ORDER BY d.day_number, da.order_index
|
||||
`, tripId);
|
||||
|
||||
const days = new Map<number, GpxExportDay>();
|
||||
for (const stop of stops) {
|
||||
let day = days.get(stop.day_number);
|
||||
if (!day) {
|
||||
day = { dayNumber: stop.day_number, date: stop.date, title: stop.title, points: [] };
|
||||
days.set(stop.day_number, day);
|
||||
}
|
||||
day.points.push({ name: stop.name, lat: stop.lat, lng: stop.lng });
|
||||
}
|
||||
|
||||
const gpx = buildGpx({ tripTitle: trip.title, places, days: [...days.values()] }, opts);
|
||||
return gpx ? { gpx, filename: gpxFilename(trip.title) } : null;
|
||||
}
|
||||
|
||||
private importGpxRows(tripId: string, fileBuffer: Buffer, opts: GpxImportOptions = {}): GpxImportResult | null {
|
||||
const { importWaypoints = true, importRoutes = true, importTracks = true, defaultName } = opts;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user