Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mgrs typings #16

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
118 changes: 87 additions & 31 deletions dist/mgrs.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,16 @@ var O = 79; // O
var V = 86; // V
var Z = 90; // Z

var ZDLS = "CDEFGHJKLMNPQRSTUVWX";
exports.ZDLS = ZDLS;

/**
* Conversion of lat/lon to MGRS.
*
* @param {object} ll Object literal with lat and lon properties on a
* WGS84 ellipsoid.
* @param {int} accuracy Accuracy in digits (5 for 1 m, 4 for 10 m, 3 for
* 100 m, 4 for 1000 m or 5 for 10000 m). Optional, default is 5.
* 100 m, 2 for 1000 m or 1 for 10000 m). Optional, default is 5.
* @return {string} the MGRS string for the given location and accuracy.
*/
exports.forward = function(ll, accuracy) {
Expand Down Expand Up @@ -94,6 +97,39 @@ function radToDeg(rad) {
return (180.0 * (rad / Math.PI));
}

function lonZone(ll) {
var Lat = ll.lat;
var Long = ll.lon;
ZoneNumber = Math.floor((Long + 180) / 6) + 1;

//Make sure the longitude 180.00 is in Zone 60
if (Long == 180) {
ZoneNumber = 60;
}

// Special zone for Norway
if (Lat >= 56.0 && Lat < 64.0 && Long >= 3.0 && Long < 12.0) {
ZoneNumber = 32;
}

// Special zones for Svalbard
if (Lat >= 72.0 && Lat < 84.0) {
if (Long >= 0.0 && Long < 9.0)
ZoneNumber = 31;
else if (Long >= 9.0 && Long < 21.0)
ZoneNumber = 33;
else if (Long >= 21.0 && Long < 33.0)
ZoneNumber = 35;
else if (Long >= 33.0 && Long < 42.0)
ZoneNumber = 37;
}
return ZoneNumber;
}

exports.lonZone = function(ll) {
return lonZone(ll);
}

/**
* Converts a set of Longitude and Latitude co-ordinates to UTM
* using the WGS84 ellipsoid.
Expand All @@ -105,6 +141,7 @@ function radToDeg(rad) {
* northing, zoneNumber and zoneLetter properties, and an optional
* accuracy property in digits. Returns null if the conversion failed.
*/

function LLtoUTM(ll) {
var Lat = ll.lat;
var Long = ll.lon;
Expand All @@ -117,35 +154,8 @@ function LLtoUTM(ll) {
var LatRad = degToRad(Lat);
var LongRad = degToRad(Long);
var LongOriginRad;
var ZoneNumber;
// (int)
ZoneNumber = Math.floor((Long + 180) / 6) + 1;

//Make sure the longitude 180.00 is in Zone 60
if (Long === 180) {
ZoneNumber = 60;
}

// Special zone for Norway
if (Lat >= 56.0 && Lat < 64.0 && Long >= 3.0 && Long < 12.0) {
ZoneNumber = 32;
}

// Special zones for Svalbard
if (Lat >= 72.0 && Lat < 84.0) {
if (Long >= 0.0 && Long < 9.0) {
ZoneNumber = 31;
}
else if (Long >= 9.0 && Long < 21.0) {
ZoneNumber = 33;
}
else if (Long >= 21.0 && Long < 33.0) {
ZoneNumber = 35;
}
else if (Long >= 33.0 && Long < 42.0) {
ZoneNumber = 37;
}
}
var ZoneNumber = lonZone(ll);

LongOrigin = (ZoneNumber - 1) * 6 - 180 + 3; //+3 puts origin
// in middle of
Expand Down Expand Up @@ -177,6 +187,10 @@ function LLtoUTM(ll) {
};
}

exports.LLtoUTM = function(ll) {
return LLtoUTM(ll);
}

/**
* Converts UTM coords to lat/long, using the WGS84 ellipsoid. This is a convenience
* class where the Zone can be specified as a single string eg."60N" which
Expand Down Expand Up @@ -274,6 +288,14 @@ function UTMtoLL(utm) {
return result;
}

exports.UTMtoLL = function(utm) {
return UTMtoLL(utm);
}

exports.latZone = function(ll) {
return getLetterDesignator(ll.lat);
}

/**
* Calculates the MGRS letter designator for the given latitude.
*
Expand Down Expand Up @@ -360,8 +382,9 @@ function getLetterDesignator(lat) {
* @return {string} MGRS string for the given UTM location.
*/
function encode(utm, accuracy) {
var seasting = "" + utm.easting,
snorthing = "" + utm.northing;
// prepend with leading zeroes
var seasting = "00000" + utm.easting,
snorthing = "00000" + utm.northing;

return utm.zoneNumber + utm.zoneLetter + get100kID(utm.easting, utm.northing, utm.zoneNumber) + seasting.substr(seasting.length - 5, accuracy) + snorthing.substr(snorthing.length - 5, accuracy);
}
Expand Down Expand Up @@ -741,5 +764,38 @@ function getMinNorthing(zoneLetter) {

}

exports.zoneBounds = function(zoneNumber, zoneLetter) {
var latIndex = ZDLS.indexOf(zoneLetter);
var minLon = (zoneNumber - 1) * 6.0 - 180.0;
var minLat = latIndex * 8.0 - 80.0;
var latRange = 8.0;
var lonRange = 6.0;
if (zoneLetter == 'X') {
latRange = 12.0;
if (zoneNumber == 31) {
lonRange = 9.0;
} else if (zoneNumber == 37) {
minLon -= 3.0;
lonRange = 9.0;
} else if (zoneNumber == 33 || zoneNumber == 35) {
lonRange = 12.0;
minLon -= 3.0;
}
} else if (zoneLetter == 'V') { // Norway special case areas.
if (zoneNumber == 31) {
lonRange = 3.0;
} else if (zoneNumber == 32) {
minLon -= 3.0;
lonRange = 9.0;
}
}
return {
minLon: minLon,
minLat: minLat,
lonRange: lonRange,
latRange: latRange
}
}

},{}]},{},[1])(1)
});
111 changes: 83 additions & 28 deletions mgrs.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ var O = 79; // O
var V = 86; // V
var Z = 90; // Z

var ZDLS = "CDEFGHJKLMNPQRSTUVWX";
exports.ZDLS = ZDLS;

/**
* Conversion of lat/lon to MGRS.
*
Expand Down Expand Up @@ -93,6 +96,39 @@ function radToDeg(rad) {
return (180.0 * (rad / Math.PI));
}

function lonZone(ll) {
var Lat = ll.lat;
var Long = ll.lon;
ZoneNumber = Math.floor((Long + 180) / 6) + 1;

//Make sure the longitude 180.00 is in Zone 60
if (Long == 180) {
ZoneNumber = 60;
}

// Special zone for Norway
if (Lat >= 56.0 && Lat < 64.0 && Long >= 3.0 && Long < 12.0) {
ZoneNumber = 32;
}

// Special zones for Svalbard
if (Lat >= 72.0 && Lat < 84.0) {
if (Long >= 0.0 && Long < 9.0)
ZoneNumber = 31;
else if (Long >= 9.0 && Long < 21.0)
ZoneNumber = 33;
else if (Long >= 21.0 && Long < 33.0)
ZoneNumber = 35;
else if (Long >= 33.0 && Long < 42.0)
ZoneNumber = 37;
}
return ZoneNumber;
}

exports.lonZone = function(ll) {
return lonZone(ll);
}

/**
* Converts a set of Longitude and Latitude co-ordinates to UTM
* using the WGS84 ellipsoid.
Expand All @@ -104,6 +140,7 @@ function radToDeg(rad) {
* northing, zoneNumber and zoneLetter properties, and an optional
* accuracy property in digits. Returns null if the conversion failed.
*/

function LLtoUTM(ll) {
var Lat = ll.lat;
var Long = ll.lon;
Expand All @@ -116,35 +153,8 @@ function LLtoUTM(ll) {
var LatRad = degToRad(Lat);
var LongRad = degToRad(Long);
var LongOriginRad;
var ZoneNumber;
// (int)
ZoneNumber = Math.floor((Long + 180) / 6) + 1;

//Make sure the longitude 180.00 is in Zone 60
if (Long === 180) {
ZoneNumber = 60;
}

// Special zone for Norway
if (Lat >= 56.0 && Lat < 64.0 && Long >= 3.0 && Long < 12.0) {
ZoneNumber = 32;
}

// Special zones for Svalbard
if (Lat >= 72.0 && Lat < 84.0) {
if (Long >= 0.0 && Long < 9.0) {
ZoneNumber = 31;
}
else if (Long >= 9.0 && Long < 21.0) {
ZoneNumber = 33;
}
else if (Long >= 21.0 && Long < 33.0) {
ZoneNumber = 35;
}
else if (Long >= 33.0 && Long < 42.0) {
ZoneNumber = 37;
}
}
var ZoneNumber = lonZone(ll);

LongOrigin = (ZoneNumber - 1) * 6 - 180 + 3; //+3 puts origin
// in middle of
Expand Down Expand Up @@ -176,6 +186,10 @@ function LLtoUTM(ll) {
};
}

exports.LLtoUTM = function(ll) {
return LLtoUTM(ll);
}

/**
* Converts UTM coords to lat/long, using the WGS84 ellipsoid. This is a convenience
* class where the Zone can be specified as a single string eg."60N" which
Expand Down Expand Up @@ -273,6 +287,14 @@ function UTMtoLL(utm) {
return result;
}

exports.UTMtoLL = function(utm) {
return UTMtoLL(utm);
}

exports.latZone = function(ll) {
return getLetterDesignator(ll.lat);
}

/**
* Calculates the MGRS letter designator for the given latitude.
*
Expand Down Expand Up @@ -740,3 +762,36 @@ function getMinNorthing(zoneLetter) {
}

}

exports.zoneBounds = function(zoneNumber, zoneLetter) {
var latIndex = ZDLS.indexOf(zoneLetter);
var minLon = (zoneNumber - 1) * 6.0 - 180.0;
var minLat = latIndex * 8.0 - 80.0;
var latRange = 8.0;
var lonRange = 6.0;
if (zoneLetter == 'X') {
latRange = 12.0;
if (zoneNumber == 31) {
lonRange = 9.0;
} else if (zoneNumber == 37) {
minLon -= 3.0;
lonRange = 9.0;
} else if (zoneNumber == 33 || zoneNumber == 35) {
lonRange = 12.0;
minLon -= 3.0;
}
} else if (zoneLetter == 'V') { // Norway special case areas.
if (zoneNumber == 31) {
lonRange = 3.0;
} else if (zoneNumber == 32) {
minLon -= 3.0;
lonRange = 9.0;
}
}
return {
minLon: minLon,
minLat: minLat,
lonRange: lonRange,
latRange: latRange
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mgrs",
"version": "0.0.4-alpha",
"version": "0.0.4-pwilczynski",
"description": "Utility for converting between WGS84 lat/lng and MGRS coordinates",
"main": "mgrs.js",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion publish.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ git commit -m "build $VERSION"

# Tag and push
git tag $VERSION
git push --tags [email protected]:proj4js/mgrs.git $VERSION
git push --tags [email protected]:pwilczynski/mgrs.git $VERSION

# Publish
npm publish
Expand Down