Mongoose / MongoDB Polygon数据类型和验证错误

时间:2015-04-16 16:54:53

标签: mongoose geospatial geojson

我想使用mongoose将多边形存储在MongoDB文档中。 这是我的架构:

var alarmSchema = new mongoose.Schema({
    // ... some other fields ....
    loc :   {
        "type": {
            "type": String,
            "enum": [
                "Point",
                "MultiPoint",
                "LineString",
                "MultiLineString",
                "Polygon",
                "MultiPolygon"
            ]
        },
        "coordinates": [[Number]]
    }
});

所以我尝试使用以下方法添加此类型的新对象:

var Alarm = db.model('Alarm', alarmSchema);
var alarmObj = new Alarm();
var polygonArray = [[10.371094,42.391009],[14.238281,44.024422],[16.259766,42.130821]];
alarmObj.loc = {type : "Polygon" , coordinates : polygonArray};
return alarmObj;

当我尝试保存时,我收到此错误:

  

ValidationError:CastError:Cast to Array因值而失败....(   数组通过了......

有什么想法吗?

之后我会找到一个包含传递点的alarmObj列表。 我认为这是正确的查询:

var geojsonPoint = { type: 'Point', coordinates: [44.95899,8.911711] }
AlarmModel.find({loc: { $geoIntersects: { $geometry: geojsonPoint}}},function(err,list) {});

可以吗?

2 个答案:

答案 0 :(得分:2)

我建立了这个"位置架构"它似乎处理点和多边形:

var locationSchema = new Schema({
    type: { 
            type: String, 
            enum: ['Point',
                   'MultiPoint',
                   'LineString',
                   'MultiLineString',
                   'Polygon',
                   'MultiPolygon'
            ],
            default: 'Point'
    },
    coordinates: { 
            // Array of {lat,lng} objects
            type: [{lat:{type:Number,max:90.0,min:-90.0},
                    lng:{type:Number,max:180.0,min:-180.0},
                    _id:false
            }],   
            default: [{lat:0,lng:0}] // Lat Lon
    }
},{_id:false});

var LocationModel = mongoose.model('Location',locationSchema);

// Test and validate polygon
var polygon = new LocationModel({
            type:'Polygon',
            coordinates:[{lat:-34.0,lng:105},{lat:-34.0,lng:106},
                         {lat:-35.0,lng:106},{lat:-35.0,lng:105}]
    });

polygon.validate(function(err){
    console.log(String(err));
});

// Test and validate point
var point = new LocationModel({
            type:'Point',
            coordinates:[{lat:-34.0,lng:105}]
    });

point.validate(function(err){
    console.log(String(err));
});

希望这有助于某人。

编辑:

经过一些工作,对我们来说,我们最终简化了“位置”。架构:

'use strict';

...

var schema = new Schema({
    type: {  // Type of Location
        type: String, 
        required: true,
        enum: ['Point','Polygon']
    },
    coordinates: { // Specified coordinates of location
        type: [],
        required: true
    }
},{_id:false});

... validation methods, etc.

let model = mongoose.model('Location',schema);

module.exports = {
    model : model,
    schema : schema
}

使用位置模型/架构的模型:

const Location = require('./Location');
const LocationSchema = Location.schema;

...

const schema = new Schema({
    ...
    location: {
        type: LocationSchema,
        required: true,
        validate: [Location.someValidationMethod, 'Specified location is not a point']},
    footprint: {
        type: LocationSchema,
        required: true,
        validate: [Location.someValidationMethod, 'Specified location is not        a Polygon']},
    ...
});

schema.index({'location','2dsphere'});
schema.index({'footprint','2dsphere'});

测试驱动程序:

let entry = new BlahModel({
    ...
    location:{
        type:'Point',
        coordinates:[0,2] // Lon/Lat
    },
    footprint:{
        type:'Polygon',
        coordinates:[[[0,10],[0,20],[10,20],[0,10]]]
    },
    ...

答案 1 :(得分:1)

Mongoose不喜欢双阵列短手[[Number]] https://github.com/Automattic/mongoose/issues/1361

如果您尝试处理所有这些类型,您将要一般性地定义geojson架构,例如,Points只是一维数组,LineStrings是2D,多边形是3D。试一试:

geo: {
    type: { type: String, "enum": [
            "Point",
            "MultiPoint",
            "LineString",
            "MultiLineString",
            "Polygon",
            "MultiPolygon"
        ] },
    coordinates:     { type: Array }
}

您还可以查看其中一些示例以帮助您开始: https://github.com/samluescher/geogoose https://github.com/rideamigoscorp/mongoose-geojson-schema

希望这有帮助!