Skip to content

Instantly share code, notes, and snippets.

@joshkopecek
Last active April 14, 2016 20:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshkopecek/93048b306804f18d801b576e85252d78 to your computer and use it in GitHub Desktop.
Save joshkopecek/93048b306804f18d801b576e85252d78 to your computer and use it in GitHub Desktop.
// 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
Copy link
Author

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