2016-08-11 9 views
10

Próbuję utworzyć aplikację, używając MEAN, ale teraz utknąłem przy próbie uzyskania find linestrings intersecting on another one given its name.Zapytania MEAN i zapytania geoprzestrzenne - Znajdź linie przerywane na innym, podając jej nazwę

Na przykład, biorąc pod uwagę poniższe zdjęcie, poly1 i poly2 powinny mieć przecięcia, a poly3 nie.

enter image description here

Załóżmy poly1 ma następujące współrzędne i następujące 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) 
} 

Kiedy uruchomić kwerendę na MongoChef znaleźć zarówno poly1 i poly2 i nie znajdę poly3 jak chcę :

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

Podczas, gdy ja uruchomić kwerendę na Mongoose otrzymał Polyline Id/name nie działa

//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 
        } 
        } } }); 

To Schema dla dotycząca linii:

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); 

ten sposób wzywam zapytania z przód- zakończyć 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)); 
      }); 
}; 

To jest mój 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); 
     }) 
    }); 
} 

To jest moje 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 w QueryController daje mi zawsze Object {} dla każdego nazwy LineString.

This is the Mongoose Log of the query.

mam upewniając wstawiania [lng, lat] współrzędne

masz jakieś pojęcie o tym, dlaczego nie mogę znaleźć żadnego LineString przecinającą przez Id gdy mogę je znaleźć za pomocą prostych współrzędne?

Z góry dziękuję.

Odpowiedz

0

końcu udało mi się rozwiązać ten problem za pomocą następującego kodu

/** 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'}); 
    }); 
} 

Błąd spowodowany był faktem, że nie przeszedł poprawnie linestrings i linestringById obiektów do zapytania.

Mam nadzieję, że komuś to pomoże.

1

Podajesz linestring.geo.coordinates w długim, sformatowanym formacie do ostatecznego zapytania. MongoDB akceptuje współrzędne w formacie x, y, a więc musi być longitude,latitude

Aktualizacja:

Trzeba będzie bezpośrednio przekazać LineString dolarów geometrii.

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

        } } }); 
+0

Cześć, dziękuję za odpowiedź. Masz na myśli, że powinienem wstawić '[lng, lat]', gdy publikujesz nowy wiersz? Myślę, że nie muszę zmieniać kodu w tym celu. – AndreaM16

+0

Wciąż pusty obiekt. To jest "Mongoose Log" https://gist.github.com/AndreaM16/1852e3c87eba610020ddf0d107b90191 – AndreaM16

+0

Any Idea? @DhruvPathak – AndreaM16

Powiązane problemy