Created
August 9, 2022 15:40
-
-
Save dougmulley/d73ccd556da649d798963130b1cdb759 to your computer and use it in GitHub Desktop.
Convert 4- or 6-character Maidenhead grid square to latitude and longitude
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
// Usage: maidenhead-to-lat_long <maidenhead-grid-square> | |
// Credit: Using the conversion method describe here at | |
// https://www.m0nwk.co.uk/how-to-convert-maidenhead-locator-to-latitude-and-longitude/ | |
const mhGridSquare = process.argv[2]; | |
console.log(`Maidenhead grid square: ${mhGridSquare}`); | |
const latitudePart1 = (mhGridSquare.charCodeAt(1) - 65) * 10; | |
const latitudePart2 = parseInt(mhGridSquare[3]); | |
const latitudePart3 = mhGridSquare.length > 4 | |
? (((mhGridSquare.charCodeAt(5) - 97) / 24.0) + (1.0 / 48.0)) | |
: 0; | |
const latitude = latitudePart1 + latitudePart2 + latitudePart3 - 90; | |
console.log(`Latitude: ${latitude}`); | |
const longitudePart1 = (mhGridSquare.charCodeAt(0) - 65) * 20; | |
const longitudePart2 = mhGridSquare[2] * 2; | |
const longitudePart3 = mhGridSquare.length > 4 | |
? ((mhGridSquare.charCodeAt(4) - 97) / 12.0) + ( 1.0 / 24.0) | |
: 0; | |
const longitude = longitudePart1 + longitudePart2 + longitudePart3 - 180; | |
console.log(`Longitude: ${longitude}`); | |
console.log(`Latitude, longitude: ${latitude}, ${longitude}`); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment