Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added getBounds function with corresponding tests.

  • Loading branch information...
commit c4bfb19fcc19ad72148114fc0e859e92ccf49cd3 1 parent 10af337
@regality regality authored
Showing with 57 additions and 1 deletion.
  1. +45 −0 geolib.js
  2. +12 −1 tests/geolib.test.html
View
45 geolib.js
@@ -296,6 +296,51 @@
},
+ /**
+ * Gets the max and min, latitude, longitude, and elevation (if provided).
+ * @param array array with coords e.g. [{latitude: 51.5143, longitude: 7.4138}, {latitude: 123, longitude: 123}, ...]
+ * @return object {maxLat: maxLat,
+ * minLat: minLat,
+ * maxLng: maxLng,
+ * minLng: minLng,
+ * maxElev: maxElev,
+ * minElev: minElev}
+ */
+ getBounds: function(coords) {
+ if (!coords.length) {
+ return false;
+ }
+
+ var keys = geolib.getKeys(coords[0]);
+ var latitude = keys.latitude;
+ var longitude = keys.longitude;
+ var elevation = keys.elevation;
+
+ var useElevation = coords[0].hasOwnProperty(elevation);
+ var stats = {
+ maxLat: 0,
+ minLat: Infinity,
+ maxLng: 0,
+ minLng: Infinity,
+ };
+
+ if (useElevation) {
+ stats.maxElev = 0;
+ stats.minElev = Infinity;
+ }
+
+ for (var i = 0, l = coords.length; i < l; ++i) {
+ stats.maxLat = Math.max(coords[i][latitude], stats.maxLat);
+ stats.minLat = Math.min(coords[i][latitude], stats.minLat);
+ stats.maxLng = Math.max(coords[i][longitude], stats.maxLng);
+ stats.minLng = Math.min(coords[i][longitude], stats.minLng);
+ if (useElevation) {
+ stats.maxElev = Math.max(coords[i][elevation], stats.maxElev);
+ stats.minElev = Math.min(coords[i][elevation], stats.minElev);
+ }
+ }
+ return stats;
+ },
/**
* Checks whether a point is inside of a polygon or not.
View
13 tests/geolib.test.html
@@ -76,6 +76,17 @@
});
+ test("Testing bounding box: getBounds()", function() {
+ expect(4);
+
+ var box = geolib.getBounds(polygon);
+
+ equal(box.maxLat, 51.5177507, "maxLat should be 51.5177507");
+ equal(box.minLat, 51.509831973, "minLat should be 51.509831973");
+ equal(box.maxLng, 7.472909447, "maxLng should be 7.472909447");
+ equal(box.minLng, 7.45518541, "minLng should be 7.45518541");
+ });
+
test("Testing conversion: sexagesimal2decimal()", function() {
expect(7);
@@ -188,4 +199,4 @@ <h2 id="qunit-userAgent"></h2>
<ol id="qunit-tests"></ol>
<div id="qunit-fixture">test markup, will be hidden</div>
</body>
-</html>
+</html>
Please sign in to comment.
Something went wrong with that request. Please try again.