2016-10-16 3 views
0

バウンディングボックス内にポイントが存在するレコードを検索する必要がありますが、MariaDBで使用するためにSequelizeでこれをどのように定式化するかはわかりません。現在、Sequelizeは地理的な検索をネイティブにサポートしているようには見えないので、SQL関数を使って直接行うことができます。以上によりバウンドボックス内のポイントを検索して続行

// latlon1 and latlon2 are of form [lat,lon] 
// representing bottom-left and top-right corners of the box 
var a = latlon1[0] + ' ' + latlon1[1]; 
var b = latlon2[0] + ' ' + latlon1[1]; 
var c = latlon2[0] + ' ' + latlon2[1]; 
var d = latlon1[0] + ' ' + latlon2[1]; 
var e = latlon1[0] + ' ' + latlon1[1]; 

var boundingBox = `${a},${b},${c},${d},${e}`; 
var fn = `ST_CONTAINS(ST_MakePolygon(ST_GeomFromText('LINESTRING(${boundingBox})'), latlon)`; 

Address.findAll({ ... }).then(function (results) { 
    // results will contain all records where the value of the 
    // latlon column is within the bounding box 
    // latlon column is of type 'point' 
}); 

を私は(私が正しいと信じて)SQLのGIS機能とのfindAllを持って次のように

現在、私のコードが見えます。問題は、私が探している結果を私に提供するために、2つをどのようにマージするのかわからないということですか?誰も助けることができますか?

Sequelize 3.24.3、node.js 6.7.0およびMariaDB 10.0を使用します。

答えて

1

私が思いついた解決策は以下の通りです。 sequelize.fn()は、データベースベースの関数を指定し、次にsequelize.col()関数を使用したい列を指定する方法です。ポリゴンにジオメトリからの変換を行う必要性を保存、次のように

// latlon1 and latlon2 are of form [lat,lon] 
// representing bottom-left and top-right corners of the box 
var a = latlon1[0] + ' ' + latlon1[1]; 
var b = latlon2[0] + ' ' + latlon1[1]; 
var c = latlon2[0] + ' ' + latlon2[1]; 
var d = latlon1[0] + ' ' + latlon2[1]; 
var e = latlon1[0] + ' ' + latlon1[1]; 

var boundingBox = `${a},${b},${c},${d},${e}`; 

// convert the textual representation to a geometry 
var geom = sequelize.fn('ST_GEOMFROMTEXT', `LINESTRING(${a},${b},${c},${d},${e})`); 
// convert the geometry to a polygon and the do the contains 
var contains = sequelize.fn('ST_CONTAINS', 
    sequelize.fn('POLYGON', geom), 
    sequelize.col('latlon') 
); 

Address.findAll({ 
    where: contains 
    }).then(function (results) { 
    // results will contain all records where the value of the 
    // latlon column is within the bounding box 
    // latlon column is of type 'point' 
}); 

注、SQLクエリも考えられます。

var contains = sequelize.fn('ST_CONTAINS', 
    sequelize.fn('ST_POLYFROMTEXT', `POLYGON((${a},${b},${c},${d},${e}))`), 
    sequelize.col('latlon') 
); 

注:

WHERE ST_CONTAINS(ST_POLYGONFROMTEXT('POLYGON((45.51195 -73.56973,45.51335 -73.56973,45.51335 -73.56584,45.51195 -73.56584,45.51195 -73.56973))'), `latlon`) 

のでSequelizeコードとして、私は2つを比較してパフォーマンスを持っていないので、私はどちらかが他よりも "良い"かどうかは言えません。

※コードでは、テンプレート文字列を使用しているためES6が必要です。

関連する問題