10

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

Here's my current code:

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

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 ]

without quotation marks, is this possible?

Express Route

   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');
  });

View

<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>

Igor
  • 1,384
  • 3
  • 17
  • 34
  • You take the example of "Point" but you might want to handle various types of features, with different types of coordinates (arrays for Point, arrays of arrays for LineString, and arrays of arrays of arrays for Polygon, MultiLineString) – Eric Burel Mar 24 '20 at 09:34

3 Answers3

16

A GeoJSON field has to be included a geometry type as a string. So a GeoJSON field must be defined like the following;

geometry: { type: { type: String }, coordinates: [Number] }

or if you want to define a default value you might use the below line;

geometry: { type: { type: String, default:'Point' }, coordinates: [Number] }

Good luck..

efkan
  • 12,991
  • 6
  • 73
  • 106
10

Like this;

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');
});
Sebin
  • 1,044
  • 1
  • 8
  • 21
Safi
  • 1,112
  • 7
  • 9
  • I tried it, but it works only when i type one number: 3.4343 else when typing two 3.43, 4.343 it is undefined. – Igor Feb 26 '15 at 18:16
  • make sure req.body.coordinates is array of two numbers, which in your case is not. its just a comma separated string e.g 3.43, 4.343 – Safi Feb 26 '15 at 18:29
  • i don't get it, should i type [3.43, 4.3343] into input? – Igor Feb 26 '15 at 18:41
  • 1
    No, that would still be a string when it gets to server, try something like; req.body.coordinates.split(',').map(Number) which will return an array of numbers – Safi Feb 26 '15 at 18:46
  • I did as you say but without .map(Number) but still works great, it is a big deal i should use .map(Number) ? – Igor Feb 26 '15 at 18:52
  • 1
    No, its just to make sure strings are converted to number, which, as it seems, is being done by driver anyway – Safi Feb 26 '15 at 18:54
  • one more question. after i removed index: '2dsphere ' everything works just fine too? – Igor Feb 26 '15 at 18:56
  • 2
    That is required if later-on you wanted to perform geospatial queries. not required if you don't need to do that – Safi Feb 26 '15 at 19:01
1

try this:

var DataSchema = new Schema({
  properties: {
    title:       { type: String, required: true },
    description: { type: String, required: true },
    date:        { type:Date, default:Date.now }
  },
  geometry: {
       coordinates: {type: Array, required: true}
  }
});
siavolt
  • 6,869
  • 5
  • 24
  • 27
  • I tried it before, in mognodb result is like this "coordinates":["1.434343, 4.21322"], and with these quotation marks is not readable as geojson, i checked on my previous project. But i am curious why if it is an array, mongo is putting quotation marks? – Igor Feb 26 '15 at 18:13