我在 Angular 中构建了一个表单,以使用 Mongoose 将 GeoJSON 功能添加到 MongoDB。这些功能包括几个属性和一个带有点和线串的 GeometryCollection。
问题来了:我能够在我的几何图形中只用一个点创建特征,但我无法用使用 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' }'
我确实意识到它说的是 type: 'number' 而我的模式设置为数组数组:
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
}
});
所以我的第一个问题是:有人知道如何使用 GeometryCollections 和 Mongoose 正确添加特征吗?
我的第二个问题是如何在使用表单时添加数组的数组?现在,当我使用文本输入时,我的数组数组以字符串形式传递。我能够使用以下方法转换点坐标:
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 坐标?
提前致谢!
最佳答案
正确的方法是将其拆分为多个模式,这样更易于阅读、使用和维护。例如:
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
关于javascript - 使用 Mongoose 添加几何集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27274125/