Sequelize,在边界框内搜索点

时间:2016-10-16 22:21:54

标签: node.js gis sequelize.js

我需要搜索边界框中存在点的任何记录,但我不确定如何使用Sequelize来制作它,以便与MariaDB一起使用。目前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。问题是我不知道如何合并两者,为我提供我想要的结果?有人可以帮忙吗?

使用Sequelize 3.24.3,node.js 6.7.0和MariaDB 10.0。

1 个答案:

答案 0 :(得分:2)

我想出的解决方案如下。 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查询也可以如下,省去了从几何到多边形的转换:

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代码:

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

注意,我没有比较两者的表现,所以我不能说一个人是否比另一个人“更好”。

*请注意,由于使用了模板字符串,代码需要ES6。