mongoose-geojson-schema
Advanced tools
Comparing version 1.0.1 to 2.0.0
227
GeoJSON.js
@@ -12,3 +12,2 @@ /** | ||
var GeoJSON = {}; | ||
var mongoose = require('mongoose'); | ||
@@ -43,10 +42,61 @@ var Schema = mongoose.Schema; | ||
/** | ||
* @SchemaType Point | ||
* @SchemaType GeoJSON | ||
* | ||
*/ | ||
function Point(key, options) { | ||
mongoose.SchemaType.call(this, key, options, 'Point'); | ||
function GeoJSON(key, options) { | ||
mongoose.SchemaType.call(this, key, options, 'GeoJSON'); | ||
} | ||
GeoJSON.prototype = Object.create(mongoose.SchemaType.prototype); | ||
GeoJSON.prototype.cast = function(geojson) { | ||
if (!geojson.type) { | ||
throw new mongoose.Error('GeoJSON objects must have a type'); | ||
} | ||
switch (geojson.type) { | ||
case 'Point': | ||
validatePointObject(geojson); | ||
break; | ||
case 'MultiPoint': | ||
validateMultiPointObject(geojson); | ||
break; | ||
case 'LineString': | ||
validateLineStringObject(geojson); | ||
break; | ||
case 'MultiLineString': | ||
validateMultiLineStringObject(geojson); | ||
break; | ||
case 'Polygon': | ||
validatePolygonObject(geojson); | ||
break; | ||
case 'MultiPolygon': | ||
validateMultiPolygonObject(geojson); | ||
break; | ||
case 'Geometry': | ||
validateGeometryObject(geojson); | ||
break; | ||
case 'GeometryCollection': | ||
validateGeometryCollectionObject(geojson); | ||
break; | ||
case 'Feature': | ||
validateFeatureObject(geojson); | ||
break; | ||
case 'FeatureCollection': | ||
validateFeatureCollectionObject(geojson); | ||
break; | ||
default: | ||
throw new mongoose.Error(geojson.type + ' is not a valid GeoJSON type'); | ||
} | ||
return geojson; | ||
}; | ||
Schema.Types.GeoJSON = GeoJSON; | ||
/** | ||
* @SchemaType Point | ||
* | ||
*/ | ||
function validatePoint(coordinates) { | ||
@@ -77,12 +127,6 @@ // must be an array (object) | ||
Point.prototype = Object.create(mongoose.SchemaType.prototype); | ||
Point.prototype.cast = function(point) { | ||
function validatePointObject(point) { | ||
if (!point.type) { | ||
throw new mongoose.Error('Point', point.type, 'point.type'); | ||
} | ||
// type must be Point | ||
if (point.type !== 'Point') { | ||
throw new mongoose.Error('Point type must be Point'); | ||
} | ||
// check for crs | ||
@@ -97,6 +141,4 @@ if (point.crs) { | ||
return point; | ||
}; | ||
} | ||
mongoose.Schema.Types.Point = Point; | ||
/** | ||
@@ -107,6 +149,2 @@ * @SchemaType MultiPoint | ||
function MultiPoint(key, options) { | ||
mongoose.SchemaType.call(this, key, options, 'MultiPoint'); | ||
} | ||
function validateMultiPoint(coordinates) { | ||
@@ -118,5 +156,3 @@ for (var i = 0; i < coordinates.length; i++) { | ||
MultiPoint.prototype = Object.create(mongoose.SchemaType.prototype); | ||
MultiPoint.prototype.cast = function(multipoint) { | ||
function validateMultiPointObject(multipoint) { | ||
// must be an array (object) | ||
@@ -126,9 +162,2 @@ if (typeof multipoint.coordinates !== 'object') { | ||
} | ||
if (!multipoint.type) { | ||
throw new mongoose.Error('MultiPoint must have a type'); | ||
} | ||
// type must be MultiPoint | ||
if (multipoint.type !== 'MultiPoint') { | ||
throw new mongoose.Error('MultiPoint type must be MultiPoint'); | ||
} | ||
// check for crs | ||
@@ -141,6 +170,4 @@ if (multipoint.crs) { | ||
return multipoint; | ||
}; | ||
} | ||
mongoose.Schema.Types.MultiPoint = MultiPoint; | ||
/** | ||
@@ -151,6 +178,2 @@ * @SchemaType LineString | ||
function LineString(key, options) { | ||
mongoose.SchemaType.call(this, key, options, 'LineString'); | ||
} | ||
function validateLineString(coordinates) { | ||
@@ -162,12 +185,3 @@ for (var i = 0; i < coordinates.length; i++) { | ||
LineString.prototype = Object.create(mongoose.SchemaType.prototype); | ||
LineString.prototype.cast = function(linestring) { | ||
if (!linestring.type) { | ||
throw new mongoose.Error('LineString must have a type'); | ||
} | ||
// type must be LineString | ||
if (linestring.type !== 'LineString') { | ||
throw new mongoose.Error('LineString type must be LineString'); | ||
} | ||
function validateLineStringObject(linestring) { | ||
// must have at least two Points | ||
@@ -184,6 +198,4 @@ if (linestring.coordinates.length < 2) { | ||
return linestring; | ||
}; | ||
} | ||
mongoose.Schema.Types.LineString = LineString; | ||
/** | ||
@@ -194,6 +206,2 @@ * @SchemaType MultiLineString | ||
function MultiLineString(key, options) { | ||
mongoose.SchemaType.call(this, key, options, 'MultiLineString'); | ||
} | ||
function validateMultiLineString(coordinates) { | ||
@@ -205,5 +213,3 @@ for (var i = 0; i < coordinates.length; i++) { | ||
MultiLineString.prototype = Object.create(mongoose.SchemaType.prototype); | ||
MultiLineString.prototype.cast = function(multilinestring) { | ||
function validateMultiLineStringObject(multilinestring) { | ||
// must be an array (object) | ||
@@ -213,15 +219,6 @@ if (typeof multilinestring.coordinates !== 'object') { | ||
} | ||
if (!multilinestring.type) { | ||
throw new mongoose.Error('MultiLineString', multilinestring.type + ' must have a type'); | ||
} | ||
// type must be MultiLineString | ||
if (multilinestring.type !== 'MultiLineString') { | ||
throw new mongoose.Error('MultiLineString type must be MultiLineString'); | ||
} | ||
validateMultiLineString(multilinestring.coordinates); | ||
return multilinestring; | ||
}; | ||
} | ||
mongoose.Schema.Types.MultiLineString = MultiLineString; | ||
/** | ||
@@ -232,6 +229,2 @@ * @SchemaType Polygon | ||
function Polygon(key, options) { | ||
mongoose.SchemaType.call(this, key, options, 'Polygon'); | ||
} | ||
function arraysEqual(arr1, arr2) { | ||
@@ -261,12 +254,3 @@ if(arr1.length !== arr2.length) return false; | ||
Polygon.prototype = Object.create(mongoose.SchemaType.prototype); | ||
Polygon.prototype.cast = function(polygon) { | ||
if (!polygon.type) { | ||
throw new mongoose.Error('Polygon', polygon.type + ' must have a type'); | ||
} | ||
// type must be Polygon | ||
if (polygon.type !== 'Polygon') { | ||
throw new mongoose.Error('Polygon type must be Polygon'); | ||
} | ||
function validatePolygonObject(polygon) { | ||
// check for crs | ||
@@ -279,6 +263,4 @@ if (polygon.crs) { | ||
return polygon; | ||
}; | ||
} | ||
mongoose.Schema.Types.Polygon = Polygon; | ||
/** | ||
@@ -289,6 +271,2 @@ * @SchemaType MultiPolygon | ||
function MultiPolygon(key, options) { | ||
mongoose.SchemaType.call(this, key, options, 'MultiPolygon'); | ||
} | ||
function validateMultiPolygon(coordinates) { | ||
@@ -300,5 +278,3 @@ for (var i = 0; i < coordinates.length; i++) { | ||
MultiPolygon.prototype = Object.create(mongoose.SchemaType.prototype); | ||
MultiPolygon.prototype.cast = function(multipolygon) { | ||
function validateMultiPolygonObject(multipolygon) { | ||
// must be an array (object) | ||
@@ -308,9 +284,2 @@ if (typeof multipolygon.coordinates !== 'object') { | ||
} | ||
if (!multipolygon.type) { | ||
throw new mongoose.Error('MultiPolygon must have a type'); | ||
} | ||
// type must be Polygon | ||
if (multipolygon.type !== 'MultiPolygon') { | ||
throw new mongoose.Error('MultiPolygon type must be MultiPolygon'); | ||
} | ||
// check for crs | ||
@@ -323,6 +292,4 @@ if (multipolygon.crs) { | ||
return multipolygon; | ||
}; | ||
} | ||
mongoose.Schema.Types.MultiPolygon = MultiPolygon; | ||
/** | ||
@@ -333,6 +300,2 @@ * @SchemaType Geometry | ||
function Geometry(key, options) { | ||
mongoose.SchemaType.call(this, key, options, 'Geometry'); | ||
} | ||
function validateGeometry(geometry) { | ||
@@ -363,5 +326,3 @@ switch (geometry.type) { | ||
Geometry.prototype = Object.create(mongoose.SchemaType.prototype); | ||
Geometry.prototype.cast = function(geometry) { | ||
function validateGeometryObject(geometry) { | ||
// console.log(geometry); | ||
@@ -379,6 +340,4 @@ // must be an array (object) | ||
return geometry; | ||
}; | ||
} | ||
mongoose.Schema.Types.Geometry = Geometry; | ||
/** | ||
@@ -389,6 +348,2 @@ * @SchemaType GeometryCollection | ||
function GeometryCollection(key, options) { | ||
mongoose.SchemaType.call(this, key, options, 'GeometryCollection'); | ||
} | ||
function validateGeometries(geometries) { | ||
@@ -400,5 +355,3 @@ for (var i = 0; i < geometries.length; i++) { | ||
GeometryCollection.prototype = Object.create(mongoose.SchemaType.prototype); | ||
GeometryCollection.prototype.cast = function(geometrycollection) { | ||
function validateGeometryCollectionObject(geometrycollection) { | ||
// must be an array (object) | ||
@@ -415,6 +368,4 @@ if (typeof geometrycollection.geometries !== 'object') { | ||
return geometrycollection; | ||
}; | ||
} | ||
mongoose.Schema.Types.GeometryCollection = GeometryCollection; | ||
/** | ||
@@ -425,14 +376,3 @@ * @SchemaType Feature | ||
function Feature(key, options) { | ||
mongoose.SchemaType.call(this, key, options, 'Feature'); | ||
} | ||
function validateFeature(feature) { | ||
if (!feature.type) { | ||
throw new mongoose.Error('Feature must have a type'); | ||
} | ||
// type must be Feature | ||
if (feature.type !== 'Feature') { | ||
throw new mongoose.Error('Feature type must be Feature'); | ||
} | ||
if (!feature.geometry) { | ||
@@ -449,11 +389,7 @@ throw new mongoose.Error('Feature must have a geometry'); | ||
Feature.prototype = Object.create(mongoose.SchemaType.prototype); | ||
Feature.prototype.cast = function(feature) { | ||
function validateFeatureObject(feature) { | ||
validateFeature(feature); | ||
return feature; | ||
}; | ||
} | ||
mongoose.Schema.Types.Feature = Feature; | ||
/** | ||
@@ -464,6 +400,2 @@ * @SchemaType FeatureCollection | ||
function FeatureCollection(key, options) { | ||
mongoose.SchemaType.call(this, key, options, 'FeatureCollection'); | ||
} | ||
function validateFeatureCollection(featurecollection) { | ||
@@ -476,12 +408,3 @@ for (var i = 0; i < featurecollection.features.length; i++) { | ||
FeatureCollection.prototype = Object.create(mongoose.SchemaType.prototype); | ||
FeatureCollection.prototype.cast = function(featurecollection) { | ||
if (!featurecollection.type) { | ||
throw new mongoose.Error('FeatureCollection must have a type'); | ||
} | ||
// type must be Polygon | ||
if (featurecollection.type !== 'FeatureCollection') { | ||
throw new mongoose.Error('FeatureCollection type must be FeatureCollection'); | ||
} | ||
function validateFeatureCollectionObject(featurecollection) { | ||
if (!featurecollection.features) { | ||
@@ -497,4 +420,2 @@ throw new mongoose.Error('FeatureCollections must have a features object'); | ||
return featurecollection; | ||
}; | ||
mongoose.Schema.Types.FeatureCollection = FeatureCollection; | ||
} |
{ | ||
"name": "mongoose-geojson-schema", | ||
"version": "1.0.1", | ||
"version": "2.0.0", | ||
"description": "Schema definitions for GeoJSON types for use with Mongoose JS", | ||
@@ -5,0 +5,0 @@ "main": "GeoJSON.js", |
@@ -31,4 +31,12 @@ # mongoose-geojson-schema | ||
## Usage | ||
## Installation | ||
First install [node.js](http://nodejs.org/), [mongodb](https://www.mongodb.org/downloads) and [mongoose](https://www.npmjs.com/package/mongoose) | ||
``` | ||
$ npm install mongoose-geojson-schema --save | ||
``` | ||
## Usage v2.x | ||
```javascript | ||
@@ -39,2 +47,45 @@ var GeoJSON = require('mongoose-geojson-schema'); | ||
var schema = new mongoose.Schema({ | ||
point: mongoose.Schema.Types.GeoJSON, | ||
multipoint: mongoose.Schema.Types.GeoJSON, | ||
linestring: mongoose.Schema.Types.GeoJSON, | ||
multilinestring: mongoose.Schema.Types.GeoJSON, | ||
polygon: mongoose.Schema.Types.GeoJSON, | ||
multipolygon: mongoose.Schema.Types.GeoJSON, | ||
geometry: mongoose.Schema.Types.GeoJSON, | ||
geometrycollection: mongoose.Schema.Types.GeoJSON, | ||
feature: mongoose.Schema.Types.GeoJSON, | ||
featurecollection: mongoose.Schema.Types.GeoJSON | ||
}); | ||
var db = mongoose.createConnection('localhost', 'test'); | ||
var model = db.model('GeoJSON', schema); | ||
var test = new GeoJSON({ | ||
point: { | ||
type: "Point", | ||
coordinates: [12.123456, 13.134578] | ||
}, | ||
... | ||
polygon: { | ||
type: "Polygon", | ||
coordinates: [ | ||
[ | ||
[12.123456, 13.1345678], | ||
[179.999999, -1.345], | ||
[12.0002, -45.4663], | ||
[12.123456, 13.1345678] | ||
], | ||
... | ||
} | ||
}); | ||
``` | ||
## Usage v1.x | ||
```javascript | ||
var GeoJSON = require('mongoose-geojson-schema'); | ||
var mongoose = require('mongoose'); | ||
var schema = new mongoose.Schema({ | ||
point: mongoose.Schema.Types.Point, | ||
@@ -76,6 +127,4 @@ multipoint: mongoose.Schema.Types.MultiPoint, | ||
## Old Usage | ||
## Usage v0.x | ||
For v0.x | ||
```javascript | ||
@@ -82,0 +131,0 @@ var GeoJSON = require('mongoose-geojson-schema'); |
@@ -123,3 +123,3 @@ /** | ||
var error = geoJSON.validateSync(); | ||
expect(error.errors.point.reason.message).to.contain('Point type must be Point'); | ||
expect(error.errors.point.reason.message).to.contain('Square is not a valid GeoJSON typ'); | ||
}); | ||
@@ -162,3 +162,3 @@ | ||
var error = geoJSON.validateSync(); | ||
expect(error.errors.multipoint.reason.message).to.contain('MultiPoint type must be MultiPoint'); | ||
expect(error.errors.multipoint.reason.message).to.contain('Square is not a valid GeoJSON typ'); | ||
}); | ||
@@ -202,3 +202,3 @@ | ||
var error = geoJSON.validateSync(); | ||
expect(error.errors.linestring.reason.message).to.contain('LineString type must be LineString'); | ||
expect(error.errors.linestring.reason.message).to.contain('Square is not a valid GeoJSON typ'); | ||
}); | ||
@@ -256,3 +256,3 @@ | ||
var error = geoJSON.validateSync(); | ||
expect(error.errors.multilinestring.reason.message).to.contain('MultiLineString type must be MultiLineString'); | ||
expect(error.errors.multilinestring.reason.message).to.contain('Square is not a valid GeoJSON type'); | ||
}); | ||
@@ -306,3 +306,3 @@ | ||
var error = geoJSON.validateSync(); | ||
expect(error.errors.polygon.reason.message).to.contain('Polygon type must be Polygon'); | ||
expect(error.errors.polygon.reason.message).to.contain('Square is not a valid GeoJSON type'); | ||
}); | ||
@@ -371,3 +371,3 @@ | ||
var error = geoJSON.validateSync(); | ||
expect(error.errors.multipolygon.reason.message).to.contain('MultiPolygon type must be MultiPolygon'); | ||
expect(error.errors.multipolygon.reason.message).to.contain('Square is not a valid GeoJSON type'); | ||
}); | ||
@@ -432,3 +432,3 @@ | ||
var error = geoJSON.validateSync(); | ||
expect(error.errors.geometry.reason.message).to.contain('Geometry must have a valid type'); | ||
expect(error.errors.geometry.reason.message).to.contain('Square is not a valid GeoJSON type'); | ||
}); | ||
@@ -444,2 +444,3 @@ | ||
geometrycollection: { | ||
type: "GeometryCollection", | ||
geometries: [ | ||
@@ -538,2 +539,3 @@ { | ||
var error = geoJSON.validateSync(); | ||
if (error) console.log(error); | ||
expect(error).to.be.an('undefined'); | ||
@@ -540,0 +542,0 @@ }); |
@@ -9,12 +9,12 @@ 'use strict'; | ||
test: {}, | ||
point: mongoose.Schema.Types.Point, | ||
multipoint: mongoose.Schema.Types.MultiPoint, | ||
linestring: mongoose.Schema.Types.LineString, | ||
multilinestring: mongoose.Schema.Types.MultiLineString, | ||
polygon: mongoose.Schema.Types.Polygon, | ||
multipolygon: mongoose.Schema.Types.MultiPolygon, | ||
geometry: mongoose.Schema.Types.Geometry, | ||
geometrycollection: mongoose.Schema.Types.GeometryCollection, | ||
feature: mongoose.Schema.Types.Feature, | ||
featurecollection: mongoose.Schema.Types.FeatureCollection, | ||
point: mongoose.Schema.Types.GeoJSON, | ||
multipoint: mongoose.Schema.Types.GeoJSON, | ||
linestring: mongoose.Schema.Types.GeoJSON, | ||
multilinestring: mongoose.Schema.Types.GeoJSON, | ||
polygon: mongoose.Schema.Types.GeoJSON, | ||
multipolygon: mongoose.Schema.Types.GeoJSON, | ||
geometry: mongoose.Schema.Types.GeoJSON, | ||
geometrycollection: mongoose.Schema.Types.GeoJSON, | ||
feature: mongoose.Schema.Types.GeoJSON, | ||
featurecollection: mongoose.Schema.Types.GeoJSON, | ||
// requireaddressfeature: GeoJSON.requiredAddressFeature | ||
@@ -21,0 +21,0 @@ }, { typeKey: '$type', collection: 'echoes' }); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
151
38780
985