用GeoJSON点对geoNear进行猫鼬调用,因为查询参数不起作用 [英] Mongoose calls to geoNear with GeoJSON points as query parameters not working

查看:141
本文介绍了用GeoJSON点对geoNear进行猫鼬调用,因为查询参数不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出为包含GeoJSON位置的文档定义的架构;

Given a schema defined for documents containing a GeoJSON location;


var BranchSchema = new Schema({
  location: {
    'type': {
      type: String,
      required: true,
      enum: ['Point', 'LineString', 'Polygon'],
      default: 'Point'
    },
    coordinates: [Number]
  },
  name: String
});
BranchSchema.index({location: '2dsphere'});

以及一些示例数据:


[
  {
    "name": "A",
    "location": {
      "type": "Point",
      "coordinates": [153.027117, -27.468515 ] //Brisbane, Australia
    }
  },
  {
    "name": "B",
    "location": {
      "type": "Point",
      "coordinates": [153.029884, -27.45643] //Also Brisbane, Australia
    }
  }
]

以下geoNear查询的行为不符合预期.我读这个查询为 在南美海岸附近的某个地点,拖网搜寻这些地点,然后找到距离所提供地点1米以内的任何地点."

The following geoNear query is not behaving as expected. I read this query as "given a location off the coast of South America, trawl through the locations and find any that are within 1 meter of the provided location."


// Somewhere off the east coast of South America.
var point = {type: 'Point', coordinates: [0.0776590, -33.7797590]};

Branch.geoNear(point, {maxDistance:1, spherical: true}, function (err, data) {
  ...
  // at this point I expected data.length === 0.
  // Instead it is returning both documents.
  ...
});

我在做什么错了?

  • 在根据WGS84标准定义职位时,我使用[long,lat].
  • 运行MongooseJS V3.8.8

推荐答案

问题是使用maxDistance错误.以下表达式有效.

The problem was incorrectly using maxDistance. The following expression worked.


Branch.geoNear({type: "Point", coordinates: [0.0776590, -33.7797590]}, {
  spherical: true, 
  maxDistance: 1 / 6378137, 
  distanceMultiplier: 6378137
})
  .then(function (doc) {
    console.log(doc);
    process.exit();
  });


Mongoose: branches.ensureIndex({ location: '2dsphere' }) { safe: undefined, background: true }  
Mongoose: branches.geoNear(0.077659) -33.779759 { distanceMultiplier: 6378137, lean: true, maxDistance: 1.567855942887398e-7, spherical: true } 
[]

现在,查询可以正确地发现集合中的两个文档不在所查询位置的1米范围内.查询离家较近的位置也可以给我们带来预期的结果.

Now the query correctly discovers that the two documents in the collection are not within 1 meter of the queried location. Querying a location closer to home also gives us the expected results.


Branch.geoNear({type: "Point", coordinates: [153.027117, -27.468515]}, {
  spherical: true, 
  maxDistance: 1 / 6378137, 
  distanceMultiplier: 6378137
})
  .then(function (doc) {
    console.log(doc);
    process.exit();
  });

Mongoose: branches.ensureIndex({ location: '2dsphere' }) { safe: undefined, background: true }  
Mongoose: branches.geoNear(153.027117) -27.468515 { distanceMultiplier: 6378137, lean: true, maxDistance: 1.567855942887398e-7, spherical: true } 
[ { dis: 0.0026823704060803567,
    obj: 
     { name: 'A',
       _id: 533200e49ba06bec37c0cc22,
       location: [Object],
       __v: 0 } } ]

解决方案?

geoNear的MongoDb文档指出,如果使用geoJSON对象,则maxDistance应该以米为单位,如果使用坐标对,则应该以弧度为单位.

MongoDb documentation of geoNear states that if using a geoJSON object maxDistance should be in meters, and in radians if using coordinate pairs.

可选.距中心点的距离.为GeoJSON数据指定距离(以米为单位),为旧坐标对指定距离(以弧度为单位). MongoDB将结果限制为距中心点指定距离内的那些文档. http://docs.mongodb.org/manual/reference/command/geoNear/#dbcmd.geoNear

这是错误的,或者我对此的理解是错误的.

如上所示,它不是以maxDistance指定1米,而是以弧度提供.

As you can see above, rather than specifying 1 meter for maxDistance, it is supplied in Radians.

截至本文发布之日,geoNear要求maxDistance位于 弧度,无论您使用的是geoJSON对象还是 旧版坐标对.

As at the date of this post geoNear requires that maxDistance be in Radians regardless of whether you are using a geoJSON object or a legacy coordinate pair.

这篇关于用GeoJSON点对geoNear进行猫鼬调用,因为查询参数不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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