geojson-tools
Advanced tools
Comparing version 0.2.0 to 0.2.1
@@ -0,1 +1,6 @@ | ||
0.2.1 / 2015-06-03 | ||
================== | ||
* Fixed a reference error on `_toRadian()` | ||
0.2.0 / 2015-06-02 | ||
@@ -2,0 +7,0 @@ ================== |
{ | ||
"name": "geojson-tools", | ||
"version": "0.2.0", | ||
"version": "0.2.1", | ||
"description": "Tools for working with location data, using the GeoJSON specification", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -12,4 +12,71 @@ /** | ||
/** | ||
* returns an object with result, and error message if provided | ||
* | ||
* @param {Boolean} error, can also take any JavaScript object | ||
* @param {Boolean} indicator of whether to return error message | ||
* @param {Object} object containing error message | ||
* @returns {Object} result and error message | ||
*/ | ||
var _returnError = function (err, returnError, options) { | ||
if (!returnError) { | ||
return err; | ||
} | ||
if (!options) { | ||
options = {}; | ||
} | ||
if (options.message) { | ||
return {result: err, message: options.message}; | ||
} | ||
return {result: err}; | ||
}; | ||
var _isPosition = function (coordinates, returnError) { | ||
if (coordinates.length < 2 || !_.isNumber(coordinates[0]) || !_.isNumber(coordinates[1])) { | ||
return _returnError(false, returnError, {message: 'invalid coordinates for GeoJSON Point'}); | ||
} | ||
// Position is valid | ||
return true; | ||
}; | ||
var _isLinearRing = function (arr, returnError) { | ||
var invalid = _.find(arr, function (coordinates) { | ||
if (coordinates.length < 4) { | ||
return _returnError(false, returnError, {message: 'expecting coordinates of GeoJSON object to have at least 4 positions'}); | ||
} | ||
if (!_.isEqual(_.last(coordinates), coordinates[0])) { | ||
return _returnError(false, returnError, {message: 'the first and last positions of GeoJSON LinearRing are not the same'}); | ||
} | ||
return _isPosition(coordinates); | ||
}); | ||
if (invalid) { | ||
return _returnError(false, returnError, {message: ''}); | ||
} | ||
return true; | ||
}; | ||
var _isLineString = function (coordinates, returnError) { | ||
var invalid = _.find(coordinates, function (xy) { | ||
return !_isPosition(xy); | ||
}); | ||
if (invalid) { | ||
return _returnError(false, returnError, {message: 'one of the coordinates of the LineString are invalid'}); | ||
} | ||
// GeoJSON LineString coordinates are valid | ||
return true; | ||
}; | ||
/** | ||
* converts degrees to radians | ||
* | ||
* @param {Number} coordinates in degrees | ||
* @returns {Number} coordinates in radians | ||
*/ | ||
var _toRadian = function (degree) { | ||
return degree * Math.PI / 180; | ||
}; | ||
/** | ||
* | ||
* @param {Array} array | ||
@@ -152,3 +219,6 @@ * @param {String} type | ||
var array, | ||
error; | ||
error, | ||
poly, | ||
multiline, | ||
line; | ||
if (!geoobj.type || !geoobj.coordinates) { | ||
@@ -164,3 +234,3 @@ return new Error("The object specified is not a a valid GeoJSON object"); | ||
array = geoobj.coordinates; | ||
var line = []; | ||
line = []; | ||
_.find(array, function (ln) { | ||
@@ -179,3 +249,2 @@ if (typeof ln === 'object') { | ||
case 'polygon': | ||
var poly; | ||
array = []; | ||
@@ -208,3 +277,2 @@ // check if valid object | ||
case 'multipoint': | ||
var mpoint; | ||
array = []; | ||
@@ -217,3 +285,2 @@ // REVIEW should we check if valid object? Or can we do it at the top? | ||
case 'multilinestring': | ||
var multiline; | ||
array = []; | ||
@@ -235,3 +302,2 @@ // check if valid object | ||
case 'multipolygon': | ||
var poly; | ||
array = []; | ||
@@ -534,3 +600,3 @@ _.each(geoobj.coordinates, function (coord) { | ||
} | ||
var invalid = _.find(obj.geometries, function (geometry) { | ||
invalid = _.find(obj.geometries, function (geometry) { | ||
return !isGeoJSON(geometry); | ||
@@ -546,68 +612,2 @@ }); | ||
var _isLinearRing = function (arr, returnError) { | ||
var invalid = _.find(arr, function (coordinates) { | ||
if (coordinates.length < 4) { | ||
return _returnError(false, returnError, {message: 'expecting coordinates of GeoJSON object to have at least 4 positions'}); | ||
} | ||
if (!_.isEqual(_.last(coordinates), coordinates[0])) { | ||
return _returnError(false, returnError, {message: 'the first and last positions of GeoJSON LinearRing are not the same'}); | ||
} | ||
return _isPosition(coordinates); | ||
}); | ||
if (invalid) { | ||
return _returnError(false, returnError, {message: ''}) | ||
} | ||
return true; | ||
}; | ||
var _isPosition = function (coordinates, returnError) { | ||
if (coordinates.length < 2 || !_.isNumber(coordinates[0]) || !_.isNumber(coordinates[1])) { | ||
return _returnError(false, returnError, {message: 'invalid coordinates for GeoJSON Point'}); | ||
} | ||
// Position is valid | ||
return true; | ||
}; | ||
var _isLineString = function (coordinates, returnError) { | ||
var invalid = _.find(coordinates, function (xy) { | ||
return !_isPosition(xy); | ||
}); | ||
if (invalid) { | ||
return _returnError(false, returnError, {message: 'one of the coordinates of the LineString are invalid'}); | ||
} | ||
// GeoJSON LineString coordinates are valid | ||
return true; | ||
}; | ||
/** | ||
* converts degrees to radians | ||
* | ||
* @param {Number} coordinates in degrees | ||
* @returns {Number} coordinates in radians | ||
*/ | ||
var _toRadian = function (degree) { | ||
return decimal * Math.PI / 180; | ||
} | ||
/** | ||
* returns an object with result, and error message if provided | ||
* | ||
* @param {Boolean} error, can also take any JavaScript object | ||
* @param {Boolean} indicator of whether to return error message | ||
* @param {Object} object containing error message | ||
* @returns {Object} result and error message | ||
*/ | ||
var _returnError = function (err, returnError, options) { | ||
if (!returnError) { | ||
return err; | ||
} | ||
if (!options) { | ||
options = {}; | ||
} | ||
if (options.message) { | ||
return {result: err, message: options.message}; | ||
} | ||
return {result: err}; | ||
}; | ||
/* | ||
@@ -614,0 +614,0 @@ * Export functions |
30671
593