我正在尝试使用MEAN构建一个应用程序,但是现在我试图找到一个给定名称的另一个线程的线程.
例如,给定以下图像,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都不像我想要的那样找到poly3:
{ geo :{ $geoIntersects:{ $geometry :{ type: "LineString",coordinates: [ [14.59,15.961] ] } } } }
而当我运行Mongoose查询给定Polyline Id /名称它不工作
//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); }) }
console.log
inQueryController
gives me alwaysObject {}
for any
linestring name.07001.
I’m making sure of inserting
[lng,lat]
coordinates
你有什么想法,为什么我找不到任何LineString与Id相交,而我可以使用直线坐标找到它们?
提前致谢.
你正在通过linestring.geo.coordinates以latitute,longitute格式到最终的查询.
Mongodb接受x,y格式的坐标,因此它必须是经度,纬度
Mongodb接受x,y格式的坐标,因此它必须是经度,纬度
更新:
您将需要直接将线串传递为$几何.
query = linestrings.where({ geo : { $geoIntersects : { $geometry : lineStringbyId. } } });