And*_*M16 10 mongoose mongodb google-maps-api-3 node.js angularjs
我正在尝试使用MEAN构建一个应用程序,但现在我在尝试时遇到困难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)
}
Run Code Online (Sandbox Code Playgroud)
当我运行的查询上MongoChef,我觉得都poly1和poly2我不觉得poly3像我想:
{
geo :{
$geoIntersects:{
$geometry :{
type: "LineString" ,
coordinates: [ [14.59, 24.847], [28.477, 15.961] ]
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
虽然,当我运行查询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
}
} } });
Run Code Online (Sandbox Code Playgroud)
这是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);
Run Code Online (Sandbox Code Playgroud)
这就是我从前端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));
});
};
Run Code Online (Sandbox Code Playgroud)
这是我的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);
})
});
}
Run Code Online (Sandbox Code Playgroud)
这是我的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);
})
}
Run Code Online (Sandbox Code Playgroud)
console.loginQueryController总是给我Object {}任何线串名称.我确保插入
[lng, lat]坐标
你有没有想过为什么我找不到任何与Id交叉的LineString,而我可以使用直线坐标找到它们?
提前致谢.
我终于用下面的代码解决了这个问题
/** 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'});
});
}
Run Code Online (Sandbox Code Playgroud)
该错误是由于我没有正确传递linestrings和linestringById对象到查询而引起的。
我希望它能帮助某人。
| 归档时间: |
|
| 查看次数: |
209 次 |
| 最近记录: |