angularjs - MEAN 和地理空间查询 - 查找与给定名称的另一个相交的 LineStrings

标签 angularjs node.js mongodb google-maps-api-3 mongoose

我正在尝试使用 MEAN 构建应用程序,但现在我在尝试查找与给定名称的另一个线串相交时遇到了困难

例如,给定下图,poly1poly2 应该有交集,而 poly3 没有。

enter image description here

假设 poly1 具有以下坐标和以下 JSON:

{ 
  "_id" : ObjectId("57ab2107505ab11b1bd8422e"), 
  "name" : "poly1", 
  "updated_at" : ISODate("2016-08-10T12:41:43.789+0000"), 
  "created_at" : ISODate("2016-08-10T12:41:43.780+0000"), 
  "geo" : {
      "coordinates" : [ [14.59, 24.847], [28.477, 15.961] ], 
      "type" : "LineString"
  }, 
  "__v" : NumberInt(0)
}

当我在 MongoChef 上运行查询时,我发现 poly1poly2 都没有找到 poly3就像我想要的那样:

{
    geo :{
        $geoIntersects:{
            $geometry :{
                type: "LineString" ,
                coordinates: [ [14.59, 24.847], [28.477, 15.961] ]
            }
        }
    }
}

虽然,当我在 Mongoose 上运行查询时,给定 Polyline Id/name 它不起作用

//Given
var linestringById = Linestrings.find({name : lineName});
var linestrings    = Linestrings.find({});    

//Works
query = linestrings.where({ geo : { $geoIntersects : 
                    { $geometry : 
                       { type : 'LineString', 
                         coordinates : [ [27.528, 25.006], [14.063, 15.591] ]
                        } 
                     } } });

//Does not work
query = linestrings.where({ geo : { $geoIntersects : 
                   { $geometry : 
                     { type : 'LineString', 
                       coordinates : linestringById.geo.coordinates
                     } 
                    } } });
//Also does not work:
query = linestrings.where({ geo : { $geoIntersects : 
                   { $geometry : 
                     { type : 'LineString', 
                       coordinates : linestringById
                     } 
                    } } });

这是线串的架构:

var mongoose = require('mongoose');
var Schema   = mongoose.Schema;

// Creates a LineString Schema.
var linestrings = new Schema({
    name: {type: String, required : true},
    geo : {
        type : {type: String, default: "LineString"},
                coordinates : Array
    },
    created_at: {type: Date, default: Date.now},
    updated_at: {type: Date, default: Date.now}
});

// Sets the created_at parameter equal to the current time
linestrings.pre('save', function(next){
    now = new Date();
    this.updated_at = now;
    if(!this.created_at) {
        this.created_at = now
    }
    next();
});

linestrings.index({geo : '2dsphere'});
module.exports = mongoose.model('linestrings', linestrings);

这就是我从前端调用查询的方式 QueryController.js

/** Looks for LineStrings intersecting a given linestring **/
vm.polyIntersect = function () {

   //Taking name from a form
   vm.queryBody = {
                    name : vm.formData.poly1
   };

   // Post the queryBody 
   $http.post('/find-poly-intersection', vm.queryBody)
             .success(function(queryResults) {
                console.log(queryResults);
             })
            .error(function(queryResults) {
                console.log('Error: no results found '+queryResults));
            });
};

这是我的 Route.js:

/** Requiring Factories **/
var LinestringFactory = require('./factories/linestring.factory.js');

module.exports = function(app) {
  // Retrieves JSON records for all linestrings intersecting a given one
    app.post('/find-poly-intersection', function(req, res) {
        LinestringFactory.findIntersections(req).then( function (linestrings) {
            return res.json(linestrings);
        }, function (error) {
            return res.json(error);
        })
    });
}

这是我的 LineString.factory.js:

var Linestrings  = require('../models/linestring-model.js');

exports.findIntersections = findIntersections;

/** Finds Linestrings Intersections **/
function findIntersections(req) {
    return new Promise( function (resolve, reject) {
        var lineName       = req.body.name;
        var linestringById = Linestrings.find({name : lineName});
        var linestrings    = Linestrings.find({});

        //Check if that certain linestring exists with Lodash
        if (_.isEmpty(linestringById) || _.isUndefined(linestringById) 
            || _.isNull(linestringById)){
            return reject('No Linestrings found for that Name');
        } else {

        query = linestrings.where({ geo : 
                { $geoIntersects : { $geometry : 
                  { type : 'LineString', 
                    coordinates : linestringById.geo.coordinates} 
                } } });

        query.exec(function (err, intersections) {
            if (err){
                return reject(err);
            }
            return resolve(intersections);
        });

    }, function (error) {
        return reject(error);
    })
}

console.log in QueryController gives me always Object {} for any linestring name.

This is the Mongoose Log of the query.

I'm making sure of inserting [lng, lat] coordinates

您知道为什么我找不到任何与 Id 相交的 LineString 而我可以使用直线坐标找到它们吗?

提前致谢。

最佳答案

您将 linestring.geo.coordinates 以 latitute,longitute 格式传递给最终查询。 Mongodb 接受 x,y 格式的坐标,因此它必须是 longitude,latitude

更新:

您需要直接将线串作为 $geometry 传递。

query = linestrings.where({ geo : { $geoIntersects : 
                   { 
                       $geometry : lineStringbyId.

                    } } });

关于angularjs - MEAN 和地理空间查询 - 查找与给定名称的另一个相交的 LineStrings,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38892060/

相关文章:

javascript - angularjs动态改变页脚

javascript - 根据 ui.router 状态隐藏元素根本不起作用

javascript - 使用nodeJS模块而不使用nodeJs

mongodb - 如何从文档中删除字段

node.js - 使用 Express.js 的动态路由——这甚至可能吗?

javascript - 如何使用元标记在 manifest.json 文件中添加 Urls 和 web_Urls

javascript - 如何在单页应用程序中将数据从一个页面传递到另一个页面

javascript - 如何在 javascript (nodejs) 中避免大量的 if-else

node.js - Sails.js 一对多的表现

mongoDB - 基于返回结果数量的不同查询条件