# manuelbieh/Geolib

1 parent cdd4e74 commit a97ac7a8f081bf991b102a4ec5d5aad2cc79b4d1 committed Feb 7, 2013
Showing with 136 additions and 76 deletions.
1. +54 −20 README.md
2. +2 −2 component.json
3. +35 −25 geolib.js
4. +3 −3 geolib.min.js
5. +2 −2 package.json
6. +33 −24 src/geolib.js
7. +7 −0 tests/geolib.test.js
 @@ -5,17 +5,17 @@ A small library to provide some basic geo functions like distance calculation, c [View demo](http://www.manuel-bieh.de/publikationen/scripts/geolib/demo.html) -## Methods +

Methods

-### geolib.getDistance(object start, object end, [int accuracy]) +

geolib.getDistance(object start, object end[, int accuracy])

Calculates the distance between two geo coordinates Takes 2 or 3. First 2 arguments must be an object with a latitude and a longitude property (e.g. {latitude: 52.518611, longitude: 13.408056}). Coordinates can be in sexagesimal or decimal format. 3rd argument is accuracy (in meters). So a calculated distance of 1248 meters with an accuracy of 100 is returned as 1200. Return value is always an integer and represents the distance in meters. -#### Examples +

Examples

geolib.getDistance(
{latitude: 51.5103, longitude: 7.49347},
@@ -43,15 +43,15 @@ navigator.geolocation.getCurrentPosition(
);
-### geolib.getCenter(array coords) +

geolib.getCenter(array coords)

Calculates the geographical center of all points in a collection of geo coordinates Takes an object or array of coordinates and calculates the center of it. Returns an object: `{"latitude": centerLat, "longitude": centerLng, "distance": diagonalDistance}` -#### Examples +

Examples

var spots = {
"Brandenburg Gate, Berlin": {latitude: 52.516272, longitude: 13.377722},
@@ -71,14 +71,14 @@ geolib.getCenter([
]);
-### geolib.isPointInside(object latlng, array coords) +

geolib.isPointInside(object latlng, array coords)

Checks whether a point is inside of a polygon or not. Note: the polygon coords must be in correct order! Returns true or false -#### Example +

Example

geolib.isPointInside(
@@ -130,11 +130,11 @@ geolib.orderByDistance({latitude: 51.515, longitude: 7.453619}, {
});
-### geolib.findNearest(object latlng, mixed coords, int offset) +

geolib.findNearest(object latlng, mixed coords[, int offset])

Finds the nearest coordinate to a reference coordinate. -#### Examples +

Examples

var spots = {
"Brandenburg Gate, Berlin": {latitude: 52.516272, longitude: 13.377722},
@@ -150,13 +150,29 @@ Finds the nearest coordinate to a reference coordinate.
geolib.findNearest(spots['Dortmund U-Tower'], spots, 1)
-### geolib.getPathLength(mixed coords) +

geolib.getPathLength(mixed coords)

+ +Calculates the length of a collection of coordinates + +Returns the length of the path in kilometers + +

Example

+ +
+// Calculate distance from Berlin via Dortmund to London
+geolib.getPathLength([
+	{latitude: 52.516272, longitude: 13.377722}, // Berlin
+	{latitude: 51.515, longitude: 7.453619}, // Dortmund
+	{latitude: 51.503333, longitude: -0.119722} // London
+]); // -> 945235
+ +

geolib.getPathLength(mixed coords)

Calculates the length of a collection of coordinates Returns the length of the path in kilometers -#### Example +

Example

// Calculate distance from Berlin via Dortmund to London
@@ -166,11 +182,29 @@ geolib.getPathLength([
{latitude: 51.503333, longitude: -0.119722} // London
]); // -> 945235
-### geolib.convertUnit(string unit, float distance, [int round]) + +

geolib.getSpeed(coords, coords[, options])

+ +Calculates the speed between to points within a given time span. + +Returns the speed in options.unit (default is km/h). + +

Example

+ +
+geolib.getSpeed(
+	{lat: 51.567294, lng: 7.38896, time: 1360231200880},
+	{lat: 52.54944, lng: 13.468509, time: 1360245600880},
+	{unit: 'mph'}
+); // -> 66.9408 (mph)
+ + + +

geolib.convertUnit(string unit, float distance[, int round])

Converts a given distance (in meters) to another unit. -#### Parameters +

Parameters

`unit` can be one of: @@ -188,32 +222,32 @@ Converts a given distance (in meters) to another unit. `round` fractional digits -#### Example +

Example

`geolib.convertUnit('km', 14213, 2) // -> 14,21` -### geolib.sexagesimal2decimal(string coord) +

geolib.sexagesimal2decimal(string coord)

Converts a sexagesimal coordinate to decimal format -#### Example +

Example

`geolib.sexagesimal2decimal("51° 29' 46\" N")` -### geolib.decimal2sexagesimal(float coord) +

geolib.decimal2sexagesimal(float coord)

Converts a decimal coordinate to sexagesimal format -#### Example +

Example

`geolib.decimal2sexagesimal(51.49611111); // -> 51° 29' 46.00` -### geolib.useDecimal(mixed coordinate) +

geolib.useDecimal(mixed coordinate)

Checks if a coordinate is already in decimal format and, if not, converts it to -#### Example +

Example

geolib.useDecimal("51° 29' 46\" N"); // -> 51.59611111
geolib.useDecimal(51.59611111) // -> 51.59611111
 @@ -13,7 +13,7 @@ "url": "http://github.com/manuelbieh/geolib.git" }, "devDependencies": { - "grunt": "~0.3.9", + "grunt": "~0.3.17", "grunt-contrib-clean": "~0.3.0", "grunt-contrib-copy": "~0.3.0" }, @@ -35,6 +35,6 @@ "scripts": { "test": "grunt travis --verbose" }, - "version": "1.2.6", + "version": "1.2.7", "main": "./geolib" }
 @@ -1,15 +1,15 @@ -/*! geolib 1.2.6 by Manuel Bieh +/*! geolib 1.2.7 by Manuel Bieh * A small library to provide some basic geo functions like distance calculation, * conversion of decimal coordinates to sexagesimal and vice versa, etc. * WGS 84 (World Geodetic System 1984) * * @author Manuel Bieh * @url http://www.manuelbieh.com/ -* @version 1.2.6 +* @version 1.2.7 * @license LGPL **/ -/*global geolib:true require:true module:true window:true*/ +/*global console:true geolib:true require:true module:true window:true*/ (function (window, undefined) { var radius = 6378137; // Earth radius @@ -23,6 +23,18 @@ distance: 0, + measures: { + m: 1, + km: 0.001, + cm: 100, + mm: 1000, + mi: (1 / 1609.344), + sm: (1 / 1852.216), + ft: (100 / 30.48), + "in": (100 / 2.54), + yd: (1 / 0.9144) + }, + /** * Get the key names for a geo point. * @@ -631,6 +643,22 @@ }, + getSpeed: function(start, end, options) { + + var unit = options && options.unit || 'km'; + + if(unit == 'mph') { + unit = 'mi'; + } + + var distance = geolib.getDistance(start, end); + var time = ((end.time*1)/1000) - ((start.time*1)/1000); + var mPerHr = (distance/time)*3600; + var speed = Math.round(mPerHr * geolib.measures[unit] * 10000)/10000; + return speed; + + }, + /** * Converts a distance from meters to km, mm, cm, mi, ft, in or yd * @@ -655,30 +683,12 @@ unit = unit || 'm'; round = (null == round ? 4 : round); - switch(unit) { - - case 'm': // Meter - return geolib.round(distance, round); - case 'km': // Kilometer - return geolib.round(distance / 1000, round); - case 'cm': // Centimeter - return geolib.round(distance * 100, round); - case 'mm': // Millimeter - return geolib.round(distance * 1000, round); - case 'mi': // Miles - return geolib.round(distance * (1 / 1609.344), round); - case 'sm': // Seamiles - return geolib.round(distance * (1 / 1852.216), round); - case 'ft': // Feet - return geolib.round(distance * (100 / 30.48), round); - case 'in': // Inch - return geolib.round(distance * 100 / 2.54, round); - case 'yd': // Yards - return geolib.round(distance * (1 / 0.9144), round); + if(typeof geolib.measures[unit] !== 'undefined') { + return geolib.round(distance * geolib.measures[unit], round); + } else { + throw new Error('Unknown unit for conversion.'); } - return distance; - },
 @@ -13,7 +13,7 @@ "url": "http://github.com/manuelbieh/geolib.git" }, "devDependencies": { - "grunt": "~0.3.9", + "grunt": "~0.3.17", "grunt-contrib-clean": "~0.3.0", "grunt-contrib-copy": "~0.3.0" }, @@ -35,6 +35,6 @@ "scripts": { "test": "grunt travis --verbose" }, - "version": "1.2.6", + "version": "1.2.7", "main": "./geolib" }
 @@ -5,11 +5,10 @@ * * @author Manuel Bieh * @url http://www.manuelbieh.com/ -* @version 1.2.1 * @license LGPL **/ -/*global geolib:true require:true module:true window:true*/ +/*global console:true geolib:true require:true module:true window:true*/ (function (window, undefined) { var radius = 6378137; // Earth radius @@ -23,6 +22,18 @@ distance: 0, + measures: { + m: 1, + km: 0.001, + cm: 100, + mm: 1000, + mi: (1 / 1609.344), + sm: (1 / 1852.216), + ft: (100 / 30.48), + "in": (100 / 2.54), + yd: (1 / 0.9144) + }, + /** * Get the key names for a geo point. * @@ -631,6 +642,22 @@ }, + getSpeed: function(start, end, options) { + + var unit = options && options.unit || 'km'; + + if(unit == 'mph') { + unit = 'mi'; + } + + var distance = geolib.getDistance(start, end); + var time = ((end.time*1)/1000) - ((start.time*1)/1000); + var mPerHr = (distance/time)*3600; + var speed = Math.round(mPerHr * geolib.measures[unit] * 10000)/10000; + return speed; + + }, + /** * Converts a distance from meters to km, mm, cm, mi, ft, in or yd * @@ -655,30 +682,12 @@ unit = unit || 'm'; round = (null == round ? 4 : round); - switch(unit) { - - case 'm': // Meter - return geolib.round(distance, round); - case 'km': // Kilometer - return geolib.round(distance / 1000, round); - case 'cm': // Centimeter - return geolib.round(distance * 100, round); - case 'mm': // Millimeter - return geolib.round(distance * 1000, round); - case 'mi': // Miles - return geolib.round(distance * (1 / 1609.344), round); - case 'sm': // Seamiles - return geolib.round(distance * (1 / 1852.216), round); - case 'ft': // Feet - return geolib.round(distance * (100 / 30.48), round); - case 'in': // Inch - return geolib.round(distance * 100 / 2.54, round); - case 'yd': // Yards - return geolib.round(distance * (1 / 0.9144), round); + if(typeof geolib.measures[unit] !== 'undefined') { + return geolib.round(distance * geolib.measures[unit], round); + } else { + throw new Error('Unknown unit for conversion.'); } - return distance; - },