MEAN & Geospatial queries - Find LineStrings Intersecting on Another One given Its Name
You are passing linestring.geo.coordinates in latitute,longitute format to the final query.Mongodb accepts coordinates in the x,y format, hence it has to be longitude,latitude
Updated:
You will need to directly pass the linestring as $geometry.
query = linestrings.where({ geo : { $geoIntersects : { $geometry : lineStringbyId. } } });
I finally managed to solve this issue with the following code
/** 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'}); });}
The error was caused by the fact that I did not pass correctly linestrings
and linestringById
objects to the query.
I hope it will help someone.