平均值和地理空间查询 – find相交的另一个LineStrings命名
我试图build立一个应用程序使用平均水平,但现在我卡住时,试图find linestrings intersecting on another one given its name
。
例如,给出以下图像, poly1
和poly2
应该有交点,而poly3
不要。
假设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
上运行查询时,我发现poly1
和poly2
都没有find像我想要的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 } } } });
这是LineStrings的Schema :
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); }) }
QueryController
console.log
总是给我任何线串名称的Object {}
。这是查询的Mongoose日志 。
我正在确保插入
[lng, lat]
坐标
你有什么想法,为什么我找不到任何LineString相交Id,而我可以find他们使用直坐标?
提前致谢。
您正在将linestring.geo.coordinates以奇数格式传递给最终查询。 Mongodb接受x,y格式的坐标,因此它必须是longitude,latitude
更新:
您将需要直接传递$ lines作为$几何。
query = linestrings.where({ geo : { $geoIntersects : { $geometry : lineStringbyId. } } });
我终于设法解决了这个问题,下面的代码
/** Finds Linestrings Intersections **/ function findIntersections(req) { return new Promise( function (resolve, reject) { var lineName = req.body.name; Linestrings.findOne({name : lineName}).then( function (linestringById, error) { if(error){ return reject({error : 'LineString not Found'}); } queryIntersections(linestringById).then( function (response) { return resolve(response); }); }); }, function (error) { return reject({error : 'Error while executing promise'}); }); } function queryIntersections(linestringById) { return new Promise( function (resolve, reject) { if (_.isEmpty(linestringById) || _.isUndefined(linestringById) || _.isNull(linestringById)){ return reject({ error : 'No Linestrings found for that Name'}); } else { query = Linestrings.where( { geo : { $geoIntersects : { $geometry : { type: 'LineString', coordinates: linestringById.geo.coordinates } } } } ); queryExec(query).then( function (intersections) { return resolve(intersections); }); } }, function (error){ return reject({error : 'Error while executing promise'}); }); }
该错误是由于我没有正确传递linestrings
和linestringById
对象到查询。
我希望这会帮助别人。
- 在使用AngularJS发送GET请求时,无法从WebStorm IDE执行“打开”无效URL
- 如何自动获取节点谷歌API的授权令牌为Gmail
- 在MEAN应用程序中处理路由的Express.js或angular?
- 通过http发送variablesget
- 如何在angularjs的ui.router的模板url中传递Nodejs的jade页面
- Strongloop Loopback:按相关模型的ID进行过滤
- 重新启动浏览器后,ngCookies不会保存cookie
- 来自AngularJS的NodeJS HTTP请求
- 节点/ Angular / Passport / Satellizer基于令牌的Facebook身份validation不返回令牌