Help us understand the problem. What is going on with this article?

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

やりたい事

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 }
  }
}
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away