javascript - 使用 Mongoose 添加几何集合

标签 javascript forms mongodb mongoose geojson

我在 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/

相关文章:

javascript - AWS SDK 等待异步调用完成?

php - MySQL 使用不存在列中的数组

c# - 如何使用 c# 在 mongo DB 驱动程序 2.0 版中使用 IMongoDatabase 接口(interface)实现 "FindOne"和 "EnsureIndex"?

java - 如何使用 _id 字段从 mongodb 中获取记录并使用 java 在控制台中显示获取的记录

mongodb - riak 搜索 mongodb 索引之间的区别

javascript - 提交表单后只刷新一次网页吗?

javascript - 更改 owl-carousel 中图像的宽度

javascript - Puppeteer 不等待超时

javascript - 没有 jQuery 或 Ajax 的链式选择

javascript - Ajax JQuery 中的表单提交