2014-12-03 19 views
2

我已经在Angular中构建了一个表单,以使用Mongoose将GeoJSON功能添加到MongoDB。这些功能包括几个属性和一个带点和线串的GeometryCollection。使用Mongoose添加GeometryCollections

问题来了:我只能在几何体中创建一个点,但我无法使用一个使用lineString的几何集合创建特征。我得到两种:

16755 Can't extract geo keys from object, malformed geometry? 

或:

{ [CastError: Cast to number failed for value "0,0,1,1" at path "coordinates"] 
    message: 'Cast to number failed for value "0,0,1,1" at path "coordinates"', 
    name: 'CastError', 
    type: 'number', 
    value: [[0,0],[1,1]], 
    path: 'coordinates' }' 

我不知道它说类型: '数量',而我的架构设置为数组的数组:

var featureSchema = new mongoose.Schema({ 
    'type': { 
    type: String, 
    default: "Feature" 
    }, 
    geometry: { 
    'type': { 
     type: String, 
     default: 'GeometryCollection', 
    }, geometries: [{ 
     'type': { 
     type: String, 
     default: 'Point' 
     }, 
     coordinates: [Number] 
    }, { 
     'type': { 
     type: String, 
     default: 'LineString' 
     }, 
     coordinates: { 
     type: [Array], 
     default: [[0,0], [1,1]] 
     } 
    }] 
    }, 
    properties: { 
    title: String 
    } 
}); 

所以我第一个问题是:有谁知道如何正确添加使用Mongoose的GeometryCollections功能?

我的第二个问题是如何在使用表单时添加数组数组?当我现在使用文本输入时,我将我的数组数组作为字符串传递。我能点转换为使用坐标:

var array = req.body.feature.geometry.geometries.coordinates.split(','); 
for(var i=0; i<array.length; i++) { 
    array[i] = +array[i]; 
} 

有没有办法将字符串转换(即“[[0,0],[1,1]]”),以一个数组的数组创建lineString的坐标?

在此先感谢!

+0

我没有回答你的问题,但我察觉的第一件事情是,你在你的架构设置型点坐标号码类型。坐标始终是数组或数组。这就是你的错误来自哪里。 – iH8 2014-12-03 14:41:43

+0

谢谢你的快速回复@ iH8。 GeometryCollection的点/第一部分确实有效,[Number]是一个数字数组,我可以用它创建特征。这是我遇到的lineString/second部分。其他人能帮助我吗? – dljcollette 2014-12-03 15:19:59

+0

我找到了一组预定义的模式并添加了答案。我认为如果你使用这些或作为一个例子,并推出自己的,它将很好地工作 – iH8 2014-12-03 15:23:07

回答

1

正确的方法是将其拆分成多个模式,这些模式更易于阅读,使用和维护。例如:

GeoJSON.FeatureCollection = { 
    "type" : { 
     "type": String, 
     "default": "FeatureCollection" 
    }, 
    "features": [GeoJSON.Feature] 
} 

GeoJSON.Feature = { 
    "id": { 
     "type": "String" 
    }, 
    "type": { 
     "type": String, 
     "default": "Feature" 
    }, 
    "properties": { 
     "type": "Object" 
    }, 
    "geometry": GeoJSON.Geometry 
} 

GeoJSON.GeometryCollection = { 
    "type": { 
     "type": String, 
     "default": "GeometryCollection" 
    }, 
    "geometries": [GeoJSON.Geometry] 
} 

GeoJSON.Geometry = { 
    "type": { 
     "type": String, 
     "enum": [ 
      "Point", 
      "MultiPoint", 
      "LineString", 
      "MultiLineString", 
      "Polygon", 
      "MultiPolygon" 
     ] 
    }, 
    "coordinates": [] 
} 

摘自:https://github.com/RideAmigosCorp/mongoose-geojson-schema