geoJson坐标的猫鼬模式 [英] Mongoose Schema for geoJson coordinates

查看:51
本文介绍了geoJson坐标的猫鼬模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试为 geojson 创建架构,但是在坐标语法方面存在一些问题.

I tried to create a schema for geojson but have had some problems with syntax for coordinates.

这是我当前的代码:

var DataSchema = new Schema({
  properties: {
    title:       { type: String, required: true },
    description: { type: String, required: true },
    date:        { type:Date, default:Date.now }
  },
  geometry: {
       coordinates: []
  }
});

我尝试使用[](空数组),它创建了''[Number,Number],但是不起作用.

I tried using [] (empty array), it creates '' and [Number,Number] but it doesn't work.

我的问题是:我必须如何构建架构才能得到结果

My question is: how do I have to construct my schema so as result I will get

coordinates: [ 3.43434343, 5.543434343 ]

没有引号,这可能吗?

快速路线

   app.post('/mountain_rescue',  function (req, res){

      new rescueData({properties:{title: req.body.title, description:  req.body.description},geometry:{
     coordinates:req.body.coordinates}}).save(function (e, result) {
             console.log(result);
         });
     res.redirect('/mountain_rescue');
  });

查看

<div id="AddingPanel">
  <form method="post" action="mountain_rescue" >
      Title:<input type="text" name="title">
      Description:<textarea type="text" name="description"></textarea>
      Coordinates:<input type="text" name="coordinates">
      <button type="submit">Add</button>
  </form>

推荐答案

喜欢这个;

var DataSchema = new Schema({
  properties: {
    title:       { type: String, required: true },
    description: { type: String, required: true },
    date:        { type:Date, default:Date.now }
  },
  geometry: {
    coordinates: { type: [Number], index: '2dsphere'}
  }
});

这是您的更新路线处理程序,它将坐标字符串转换为数字数组;

Here is your update route handler, it converts coordinates string to number array;

app.post('/mountain_rescue',  function (req, res) {
  new rescueData({
    properties: {
      title: req.body.title, description: req.body.description
    },
    geometry: {
      coordinates:req.body.coordinates.split(',').map(Number)
    }
  }).save(function (e, result) {
    console.log(result);
  });
  res.redirect('/mountain_rescue');
});

这篇关于geoJson坐标的猫鼬模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆