Skip to content

Commit

Permalink
initial import
Browse files Browse the repository at this point in the history
  • Loading branch information
Tarmo Lehtpuu committed Feb 9, 2011
0 parents commit 742863c
Show file tree
Hide file tree
Showing 4 changed files with 302 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.idea
.DS_Store
48 changes: 48 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
Javascript GeoPoint Library
=====================================================

Simple Javascript library to convert latitude/longitude between decimal degrees and degrees,minutes and seconds.

This library expects latitude and longitude in EPSG:4326 (WGS84). To convert between different projections check out [Proj4js](http://proj4js.org//)

Convert decimal -> degrees
--------------------------

<script type="text/javascript" src="geopoint.js"></script>
<script type="text/javascript">

var lon = 24.72504500749274;
var lat = 58.74554729994484;

var point = new GeoPoint(lon, lat);

console.log(point.getLonDeg()); // 24° 43' 30.16"
console.log(point.getLatDeg()); // 58° 44' 43.97"

</script>

Convert degrees -> decimal
--------------------------
<script type="text/javascript" src="geopoint.js"></script>
<script type="text/javascript">

var lon = "24° 43' 30.16\"";
var lat = "58° 44' 43.97\"";

var point = new GeoPoint(lon, lat);

console.log(point.getLonDec()); // 24.725044444444443
console.log(point.getLatDeg()); // 58.74554722222222
</script>

Authors
-------

**Tanel Suurhans** (<http://twitter.com/tanelsuurhans>)
**Tarmo Lehtpuu** (<http://twitter.com/tarmolehtpuu>)

License
-------
Copyright 2011 by PerfectLine LLC (<http://www.perfectline.ee>) and is released under the MIT license.
152 changes: 152 additions & 0 deletions geopoint.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
GeoPoint = function(lon, lat) {


switch (typeof(lon)) {

case 'number':

this.lonDeg = this.dec2deg(lon, this.MAX_LON);
this.lonDec = lon;

break;

case 'string':

if (this.decode(lon)) {
this.lonDeg = lon;
}

this.lonDec = this.deg2dec(lon, this.MAX_LON);

break;
}

switch (typeof(lat)) {

case 'number':

this.latDeg = this.dec2deg(lat, this.MAX_LAT);
this.latDec = lat;

break;

case 'string':

if (this.decode(lat)) {
this.latDeg = lat;
}

this.latDec = this.deg2dec(lat, this.MAX_LAT);

break;

}
};

GeoPoint.prototype = {

CHAR_DEG : "\u00B0",
CHAR_MIN : "\u0027",
CHAR_SEC : "\u0022",
CHAR_SEP : "\u0020",

MAX_LON: 180,
MAX_LAT: 90,

// decimal
lonDec: NaN,
latDec: NaN,

// degrees
lonDeg: NaN,
latDeg: NaN,

dec2deg: function(value, max) {

var sign = value < 0 ? -1 : 1;

var abs = Math.abs(Math.round(value * 1000000));

if (abs > (max * 1000000)) {
return NaN;
}

var dec = abs % 1000000 / 1000000;
var deg = Math.floor(abs / 1000000) * sign;
var min = Math.floor(dec * 60);
var sec = (dec - min / 60) * 3600;

var result = "";

result += deg;
result += this.CHAR_DEG;
result += this.CHAR_SEP;
result += min;
result += this.CHAR_MIN;
result += this.CHAR_SEP;
result += sec.toFixed(2);
result += this.CHAR_SEC;

return result;

},

deg2dec: function(value) {

var matches = this.decode(value);

if (!matches) {
return NaN;
}

var deg = parseFloat(matches[1]);
var min = parseFloat(matches[2]);
var sec = parseFloat(matches[3]);

if (isNaN(deg) || isNaN(min) || isNaN(sec)) {
return NaN;
}

return deg + (min / 60.0) + (sec / 3600);
},

decode: function(value) {
var pattern = "";

// deg
pattern += "(-?\\d+)";
pattern += this.CHAR_DEG;
pattern += "\\s*";

// min
pattern += "(\\d+)";
pattern += this.CHAR_MIN;
pattern += "\\s*";

// sec
pattern += "(\\d+\\.\\d+)";
pattern += this.CHAR_SEC;

return value.match(new RegExp(pattern));
},

getLonDec: function() {
return this.lonDec;
},

getLatDec: function() {
return this.latDec;
},

getLonDeg: function() {
return this.lonDeg;
},

getLatDeg: function() {
return this.latDeg;
}

};



100 changes: 100 additions & 0 deletions test/geopoint.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>

<title>GeoPoint Tests</title>

<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>

<link rel="stylesheet" href="http://code.jquery.com/qunit/git/qunit.css" type="text/css" media="screen"/>

<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript" src="http://code.jquery.com/qunit/git/qunit.js"></script>
<script type="text/javascript" src="../geopoint.js"></script>

<script type="text/javascript">

$(document).ready(function() {


test("testDegrees", function() {
var point = new GeoPoint("24° 43' 30.16\"", "58° 44' 43.97\"");

equal(point.getLonDec(), 24.725044444444443);
equal(point.getLatDec(), 58.74554722222222);

});

test("testDegreesNaN", function() {

var lon = "foo";
var lat = "bar";

var point = new GeoPoint(lon, lat);

ok(isNaN(point.getLonDec()), "lonDec == NaN");
ok(isNaN(point.getLatDec()), "latDec == NaN");
ok(isNaN(point.getLonDeg()), "lonDeg == NaN");
ok(isNaN(point.getLatDeg()), "latDeg == NaN");

});

test("testDecimal", function() {

var point = new GeoPoint(24.72504500749274, 58.74554729994484);

equal(point.getLonDeg(), "24° 43' 30.16\"");
equal(point.getLatDeg(), "58° 44' 43.97\"");

});

test("testDecimalNaN", function() {

var lon = NaN;
var lat = NaN;

var point = new GeoPoint(lon, lat);

ok(isNaN(point.getLonDec()), "lonDec == NaN");
ok(isNaN(point.getLatDec()), "latDec == NaN");
ok(isNaN(point.getLonDeg()), "lonDeg == NaN");
ok(isNaN(point.getLatDeg()), "latDeg == NaN");

});

test("testLonBounds", function() {

ok(isNaN(new GeoPoint(181, 58.74554729994484).getLonDeg()), "lonDeg == Nan if &gt; 180");
ok(isNaN(new GeoPoint(-181, 58.74554729994484).getLonDeg()), "lonDeg == NaN if &lt; -180");

});

test("testLatBounds", function() {
ok(isNaN(new GeoPoint(24.72504500749274, 91).getLonDeg()), "latDeg == Nan if &gt; 90");
ok(isNaN(new GeoPoint(24.72504500749274, -91).getLonDeg()), "latDeg == NaN if &lt; -90");
});


});

</script>

</head>

<body>

<h1 id="qunit-header">GeoPoint Tests</h1>

<h2 id="qunit-banner"></h2>

<div id="qunit-testrunner-toolbar"></div>

<h2 id="qunit-userAgent"></h2>

<ol id="qunit-tests"></ol>

<div id="qunit-fixture"></div>

</body>
</html>

0 comments on commit 742863c

Please sign in to comment.