Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// a function to validate a correctly formed geoJSON Point for a Mongoose / Mongo Schema
// can be used in a schema as Point
// The lat lng limits correspond to Google Maps' limits
export var Point = {
type: {
$type: String,
match: [ /^Point$/, '{VALUE} must be Point'],
default: 'Point'
},
coordinates: {
$type: [Number],
validate: {
validator: function(array) {
if (!array) { return false }
// A point should be exactly two items
if (array.length !== 2) { return false }
// longitude should be first - mongo schema
if (array[0] > 180 || array[0] < -180) { return false }
// latitude
if (array[1] > 85 || array[1] < -85.05115) { return false }
return true;
},
message: '{VALUE} is not a correctly formed GeoJson Point object'
}
}
}
export var MultiPoint = {
type: {
$type: String,
match: [ /^MultiPoint$/, '{VALUE} must be MultiPoint'],
default: 'LineString'
},
coordinates: [{
$type: [Number],
validate: {
validator: function(array) {
if (!array) { return false }
for (var i = 0; i < array.length; i++) {
// A point should be exactly two items
if (array[i].length !== 2) { return false }
// longitude should be first - mongo schema
if (array[i][0] > 180 || array[0] < -180) { return false }
// latitude
if (array[i][1] > 85 || array[1] < -85.05115) { return false }
}
return true;
},
message: '{VALUE} is not a correctly formed GeoJson MultiPoint object'
}
}]
}
export var LineString = {
type: {
$type: String,
match: [ /^LineString$/, '{VALUE} must be LineString'],
default: 'LineString'
},
coordinates: [{
$type: [Number],
validate: {
validator: function(array) {
if (!array) { return false }
// A LineString cannot contain less than two Points
if (array.length < 2) { return false }
for (var i = 0; i < array.length; i++) {
// A point should be exactly two items
if (array[i].length !== 2) { return false }
// longitude should be first - mongo schema
if (array[i][0] > 180 || array[0] < -180) { return false }
// latitude
if (array[i][1] > 85 || array[1] < -85.05115) { return false }
}
return true;
},
message: '{VALUE} is not a correctly formed GeoJson LineString object'
}
}]
}
export var MultiLineString = {
type: {
$type: String,
match: [ /^MultiLineString$/, '{VALUE} must be MultiLineString'],
default: 'MultiLineString'
},
coordinates: [{
$type: [[Number]],
validate: {
validator: function(array) {
if (!array) { return false }
// longitude should be first - mongo schema
for (var i = 0; i < array.length; i++) {
for (var j = 0; j < array[i].length; j++) {
// A point should be exactly two items
if (array[i][j].length !== 2) { return false }
// longitude should be first - mongo schema
if (array[i][j][0] > 180 || array[0] < -180) { return false }
// latitude
if (array[i][j][1] > 85 || array[1] < -85.05115) { return false }
}
}
return true;
},
message: '{VALUE} is not a correctly formed GeoJson MultiLineString object'
}
}]
}
export var Polygon = {
type: {
$type: String,
match: [ /^Polygon$/, '{VALUE} must be Polygon'],
default: 'Polygon'
},
coordinates: [{
$type: [[Number]],
validate: {
validator: function(array) {
if (!array) { return false }
// A point should be exactly two items
if (array.length < 2) { return false }
// longitude should be first - mongo schema
for (var i = 0; i < array.length; i++) {
// The LinearRing elements should have at least four Points
if (array[i].length < 4) { return false }
// the LinearRing objects should have identical start and end values
if (array[i][0] !== array[i][array.length-1]) { return false }
for (var j = 0; j < array[i].length; j++) {
// A point should be exactly two items
if (array[i][j].length !== 2) { return false }
// longitude should be first - mongo schema
if (array[i][j][0] > 180 || array[0] < -180) { return false }
// latitude
if (array[i][j][1] > 85 || array[1] < -85.05115) { return false }
}
}
return true;
},
message: '{VALUE} is not a correctly formed GeoJson Polygon object'
}
}]
}
// Usage:
var GeoJSONSchema = new mongoose.schema({
point: Point,
multipoint: MultiPoint,
linestring: LineString,
multilinestring: MultiLineString,
polygon: Polygon
});
@joshkopecek

This comment has been minimized.

Copy link
Owner Author

@joshkopecek joshkopecek commented Apr 14, 2016

The MultiLineString and MultiPoint have not been fully tested

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment