LoginSignup
2
1

More than 3 years have passed since last update.

TypeScriptで60進数から10進数へ変換する(緯度経度)

Last updated at Posted at 2020-01-14

やりたい事

DMS(※1)形式 から Degree形式 に変換したい

つまりタイトル通り 60進数 to 10進数 をしたい
10進数 to 60進数も↓の方で実装してます

e.g.) 東京の緯度経度
DMS形式 [35度39分10.1952秒, 139度50分22.1208秒]

Degree形式 [35.652832, 139.839478]

※1 Degree Minute Second の略

実装

interface PointInDMS {
  lat: DMS;
  lng: DMS;
}

interface PointInDegree {
  lat: number;
  lng: number;
}

interface DMS {
  degree: number;
  minute: number;
  second: number;
}

const degreeMinuteSecond2Degree = (pointInDMS: PointInDMS): PointInDegree => {
  return {
    lat:
      pointInDMS.lat.degree +
      pointInDMS.lat.minute / 60 +
      pointInDMS.lat.second / 60 / 60,
    lng:
      pointInDMS.lng.degree +
      pointInDMS.lng.minute / 60 +
      pointInDMS.lng.second / 60 / 60
  };
};

export const main = () => {
  // 東京
  const tokyoPoint: PointInDMS = {
    lat: { degree: 35, minute: 39, second: 10.1952 },
    lng: { degree: 139, minute: 50, second: 22.1208 }
  };
  console.log({ tokyoPoint });

  const deg: PointInDegree = degreeMinuteSecond2Degree(tokyoPoint);
  console.log({ deg });
};

main();

実行結果

{
  tokyoPoint: {
    lat: { degree: 35, minute: 39, second: 10.1952 },
    lng: { degree: 139, minute: 50, second: 22.1208 }
  }
}
{ deg: { lat: 35.652832, lng: 139.839478 } }

逆もやってみた

10進数 to 60進数

一般的にDegree形式が使われてると思うので、こちらを使う事が多そう

interface PointInDMS {
  lat: DMS;
  lng: DMS;
}

interface PointInDegree {
  lat: number;
  lng: number;
}

interface DMS {
  degree: number;
  minute: number;
  second: number;
}

const degreeMinuteSecond2Degree = (pointInDMS: PointInDMS): PointInDegree => {
  return {
    lat:
      pointInDMS.lat.degree +
      pointInDMS.lat.minute / 60 +
      pointInDMS.lat.second / 60 / 60,
    lng:
      pointInDMS.lng.degree +
      pointInDMS.lng.minute / 60 +
      pointInDMS.lng.second / 60 / 60
  };
};

const degree2DegreeMinuteSecond = (pointInDMS: PointInDegree): PointInDMS => {
  const d2dms = (degree: number): DMS => {
    const minuteSecond = parseFloat("0." + degree.toString().split(".")[1]) * 60;
    return {
      degree: Math.trunc(degree),
      minute: Math.trunc(minuteSecond),
      second: parseFloat("0." + minuteSecond.toString().split(".")[1]) * 60
    };
  };

  return {
    lat: d2dms(pointInDMS.lat),
    lng: d2dms(pointInDMS.lng)
  };
};

export const main = () => {
  // 東京
  const tokyoPoint: PointInDMS = {
    lat: { degree: 35, minute: 39, second: 10.1952 },
    lng: { degree: 139, minute: 50, second: 22.1208 }
  };
  console.log({ tokyoPoint });

  const deg: PointInDegree = degreeMinuteSecond2Degree(tokyoPoint);
  console.log({ deg });

  const dms: PointInDMS = degree2DegreeMinuteSecond(deg);
  console.log({ dms });
};

main();

実行結果

{
  tokyoPoint: {
    lat: { degree: 35, minute: 39, second: 10.1952 },
    lng: { degree: 139, minute: 50, second: 22.1208 }
  }
}
{ deg: { lat: 35.652832, lng: 139.839478 } }
{
  dms: {
    lat: { degree: 35, minute: 39, second: 10.1952 },
    lng: { degree: 139, minute: 50, second: 22.1208 }
  }
}
2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1