Skip to content

Commit

Permalink
Show line description in Osmand, use XML namespace for line style (#246)
Browse files Browse the repository at this point in the history
  • Loading branch information
cdauth committed Mar 10, 2024
1 parent d3e3c51 commit 4583476
Showing 1 changed file with 21 additions and 10 deletions.
31 changes: 21 additions & 10 deletions server/src/export/gpx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,24 @@ function dataToText(fields: Field[], data: Record<string, string>) {
return text.join('\n\n');
}

function getMetadataGpx(data: { name: string }, extensions: Record<string, string> = {}): string {
function getMetadataGpx(data: { name: string; extensions?: Record<string, string> }, otherExtensions?: Record<string, string>): string {
const { extensions, ...otherData } = data;
return (
`<metadata>\n` +
Object.entries({
time: new Date().toISOString(),
...data
...otherData
}).map(([k, v]) => `\t<${quoteHtml(k)}>${quoteHtml(v)}</${quoteHtml(k)}>\n`).join("") +
(extensions && Object.keys(extensions).length > 0 ? (
`\t<extensions>\n` +
Object.entries(extensions).map(([k, v]) => `\t\t<${quoteHtml(k)}>${quoteHtml(v)}</${quoteHtml(k)}>\n`).join("") +
`\t</extensions>\n`
) : "") +
`</metadata>` +
(Object.keys(extensions).length > 0 ? (
(otherExtensions && Object.keys(otherExtensions).length > 0 ? (
`\n` +
`<extensions>\n` +
Object.entries(extensions).map(([k, v]) => `\t<${quoteHtml(k)}>${quoteHtml(v)}</${quoteHtml(k)}>\n`).join("") +
Object.entries(otherExtensions).map(([k, v]) => `\t<${quoteHtml(k)}>${quoteHtml(v)}</${quoteHtml(k)}>\n`).join("") +
`</extensions>`
) : "")
);
Expand Down Expand Up @@ -218,15 +224,20 @@ export function exportGpxZip(database: Database, padId: PadId, useTracks: boolea

type LineForExport = Pick<LineWithTrackPoints, "name" | "data" | "mode" | "routePoints"> & Partial<Pick<Line, "colour" | "width">>;

function getLineMetadataGpx(line: LineForExport): string {
function getLineMetadataGpx(line: LineForExport, type: Type | undefined): string {
return getMetadataGpx({
name: normalizeLineName(line.name)
name: normalizeLineName(line.name),
extensions: {
...(type ? {
"osmand:desc": dataToText(type.fields, line.data)
} : {})
}
}, {
...(line.colour ? {
color: `#${line.colour}`
"osmand:color": `#${line.colour}`
} : {}),
...(line.width ? {
width: `${line.width}`
"osmand:width": `${line.width}`
} : {})
});
}
Expand All @@ -235,7 +246,7 @@ export function exportLineToTrackGpx(line: LineForExport, type: Type | undefined
return asyncIteratorToStream((async function*() {
yield (
`${gpxHeader}\n` +
`\t${getLineMetadataGpx(line).replaceAll("\n", "\n\t")}\n`
`\t${getLineMetadataGpx(line, type).replaceAll("\n", "\n\t")}\n`
);

for await (const chunk of indentStream(getLineTrackGpx(line, type, trackPoints), { indent: "\t", indentFirst: true, addNewline: true })) {
Expand All @@ -250,7 +261,7 @@ export function exportLineToRouteGpx(line: LineForExport, type: Type | undefined
return asyncIteratorToStream((async function*() {
yield (
`${gpxHeader}\n` +
`\t${getLineMetadataGpx(line).replaceAll("\n", "\n\t")}\n`
`\t${getLineMetadataGpx(line, type).replaceAll("\n", "\n\t")}\n`
);

for await (const chunk of indentStream(getLineRouteGpx(line, type), { indent: "\t", indentFirst: true, addNewline: true })) {
Expand Down

0 comments on commit 4583476

Please sign in to comment.