From 5ef8db71eb9f19d89e516180b18a6f9466696656 Mon Sep 17 00:00:00 2001 From: panaaj <38519157+panaaj@users.noreply.github.com> Date: Sat, 7 Sep 2024 15:42:00 +0930 Subject: [PATCH] fix coordinate formatting --- src/app/lib/pipes/coords.pipe.ts | 67 +++++++++++---------- src/app/modules/settings/settings.facade.ts | 12 ++-- 2 files changed, 40 insertions(+), 39 deletions(-) diff --git a/src/app/lib/pipes/coords.pipe.ts b/src/app/lib/pipes/coords.pipe.ts index 4c5428ed..50ec9d4d 100644 --- a/src/app/lib/pipes/coords.pipe.ts +++ b/src/app/lib/pipes/coords.pipe.ts @@ -8,16 +8,16 @@ export class CoordsPipe implements PipeTransform { public transform(value: number, type: string, isLat?: boolean): string { const h = isLat ? (value < 0 ? 'S' : 'N') : value < 0 ? 'W' : 'E'; switch (type) { - case 'HDMS': - return this.toHDMS(value, h); - case 'DHMS': - return this.toDHMS(value, h); - case 'HDd': - return this.toHDd(value, h); case 'SHDd': return this.toHDdSigned(value, h); case 'DMdH': return this.toDMdH(value, h); + case 'HDd': + return this.toHDd(value, h); + case 'HDMS': + return this.toHDMS(value, h); + case 'DHMS': + return this.toDHMS(value, h); default: return this.toXY(value); } @@ -28,50 +28,51 @@ export class CoordsPipe implements PipeTransform { return value.toFixed(precision); } - // returns H D°M'S"sss - private toHDMS(value: number, hemisphere: string): string { - return `${hemisphere} ${decimalToSexagesimal(value)}`; - } - - // returns DHM'S"sss - private toDHMS(value: number, hemisphere: string): string { - let c = decimalToSexagesimal(value); - c = - c.slice(0, c.indexOf(' ') - 1) + hemisphere + c.slice(c.indexOf(' ') + 1); - return c; + // returns H +/-D.ddddd° + private toHDdSigned( + value: number, + hemisphere: string, + precision = 5 + ): string { + const h = ['S', 'W'].includes(hemisphere) + ? `${hemisphere} -` + : `${hemisphere} +`; + let ddec: string = value.toFixed(precision); + ddec = ddec.substring(ddec.indexOf('.')); + return `${h}${Math.floor(Math.abs(value))}${ddec}${this.symDegree}`; } // returns DDD° MM.ddd' H (e.g 020° 44.56' E) private toDMdH(value: number, hemisphere: string, precision = 5): string { - const D = Math.floor(value) ?? 0; + const D = Math.floor(Math.abs(value)) ?? 0; const d = D === 0 ? Math.abs(value) : Math.abs(value % D); const mdec = (d * 60).toFixed(precision); - return `${('000' + D.toString()).slice(-3)}${ + const pad = ['N', 'S'].includes(hemisphere) ? '00' : '000'; + const s = `${(pad + D.toString()).slice(0 - pad.length)}${ this.symDegree } ${mdec}' ${hemisphere}`; + return s; } // returns H D.ddddd° private toHDd(value: number, hemisphere: string, precision = 5): string { let ddec: string = value.toFixed(precision); ddec = ddec.substring(ddec.indexOf('.')); - return `${hemisphere} ${Math.abs(Math.floor(value))}${ddec}${ + return `${hemisphere} ${Math.floor(Math.abs(value))}${ddec}${ this.symDegree }`; } - // returns H +/-D.ddddd° - private toHDdSigned( - value: number, - hemisphere: string, - precision = 5 - ): string { - const h = - hemisphere === 'S' || hemisphere === 'W' - ? `${hemisphere} -` - : `${hemisphere} +`; - let ddec: string = value.toFixed(precision); - ddec = ddec.substring(ddec.indexOf('.')); - return `${h}${Math.floor(value)}${ddec}${this.symDegree}`; + // returns H D°M'S"sss + private toHDMS(value: number, hemisphere: string): string { + return `${hemisphere} ${decimalToSexagesimal(value)}`; + } + + // returns DHM'S"sss + private toDHMS(value: number, hemisphere: string): string { + let c = decimalToSexagesimal(value); + c = + c.slice(0, c.indexOf(' ') - 1) + hemisphere + c.slice(c.indexOf(' ') + 1); + return c; } } diff --git a/src/app/modules/settings/settings.facade.ts b/src/app/modules/settings/settings.facade.ts index 13d7c0f0..0bea6d8f 100644 --- a/src/app/modules/settings/settings.facade.ts +++ b/src/app/modules/settings/settings.facade.ts @@ -61,12 +61,12 @@ export class SettingsFacade { [true, 'Wind Apparent'] ]), coords: [ - ['XY', '-28.12345'], - ['SHDd', `S-28.12345${this.symDegree}`], - ['DMdH', `028${this.symDegree} 15.345' S`], - ['HDd', `S 28.12345${this.symDegree}`], - ['HDMS', `S 28${this.symDegree}15'46"123`], - ['DHMS', `28S15'46"123`] + ['XY', '-128.12345'], + ['SHDd', `W-128.12345${this.symDegree}`], + ['DMdH', `128${this.symDegree} 15.34567' W`], + ['HDd', `W 128.12345${this.symDegree}`], + ['HDMS', `W 128${this.symDegree}15'46"123`], + ['DHMS', `128W15'46"123`] ] };