admin管理员组文章数量:1312800
I have a problem with search including sort by distance from some point. Here is my code and what I'm trying to do. Thanks for help
const Sequelize = require('sequelize');
var Flat = db.define('flat', {
id: {
type: Sequelize.INTEGER,
autoIncrement: true,
primaryKey: true
}
});
var FlatAddress = db.define('flat_address', {
id: {
type: Sequelize.INTEGER,
autoIncrement: true,
primaryKey: true
},
flat_id: {
type: Sequelize.INTEGER,
foreignKey:true,
allowNull:false,
references: {
model:'flats',
key: 'id'
}
},
city: {
type: Sequelize.STRING(50) //post_town
},
location: {
type: Sequelize.GEOMETRY('POINT')
}
});
Flat.hasOne(FlatAddress, { as: 'Address', foreignKey: 'flat_id', otherKey: 'id', onDelete: 'cascade' });
FlatAddress.belongsTo(Flat, { foreignKey: 'id', otherKey: 'flat_id', onDelete: 'cascade' });
and i want to do something like this
var POINT = {lat, lng} ??
Flats.findAndCountAll({
where: filter,
order: [
[ { model: FlatAddresses, as: 'Address' },
'//here should be something like distance from POINT//', 'ACS']
],
include: [
{ model: FlatAddresses, as: 'Address'}
],
offset,
limit
})
I didn't find examples or docs for my case. thanks
I have a problem with search including sort by distance from some point. Here is my code and what I'm trying to do. Thanks for help
const Sequelize = require('sequelize');
var Flat = db.define('flat', {
id: {
type: Sequelize.INTEGER,
autoIncrement: true,
primaryKey: true
}
});
var FlatAddress = db.define('flat_address', {
id: {
type: Sequelize.INTEGER,
autoIncrement: true,
primaryKey: true
},
flat_id: {
type: Sequelize.INTEGER,
foreignKey:true,
allowNull:false,
references: {
model:'flats',
key: 'id'
}
},
city: {
type: Sequelize.STRING(50) //post_town
},
location: {
type: Sequelize.GEOMETRY('POINT')
}
});
Flat.hasOne(FlatAddress, { as: 'Address', foreignKey: 'flat_id', otherKey: 'id', onDelete: 'cascade' });
FlatAddress.belongsTo(Flat, { foreignKey: 'id', otherKey: 'flat_id', onDelete: 'cascade' });
and i want to do something like this
var POINT = {lat, lng} ??
Flats.findAndCountAll({
where: filter,
order: [
[ { model: FlatAddresses, as: 'Address' },
'//here should be something like distance from POINT//', 'ACS']
],
include: [
{ model: FlatAddresses, as: 'Address'}
],
offset,
limit
})
I didn't find examples or docs for my case. thanks
Share Improve this question asked Feb 27, 2018 at 14:19 Vitalii KulykVitalii Kulyk 431 silver badge7 bronze badges1 Answer
Reset to default 11The following statement will find Flats
within your latitude and longitude, include a field called distance
in the payload, and order it by distance
.
const myDistance = 10000; // e.g. 10 kilometres
Flats.findAll({
attributes: {
include: [
[
Sequelize.fn(
'ST_Distance',
Sequelize.col('location'),
Sequelize.fn('ST_MakePoint', longitude, latitude)
),
'distance'
]
]
},
where: Sequelize.where(
Sequelize.fn(
'ST_DWithin',
Sequelize.col('location'),
Sequelize.fn('ST_MakePoint', longitude, latitude),
myDistance
),
true
),
order: Sequelize.literal('distance ASC')
});
Please keep in mind: I do not know which SRID you are using. Therefore, I'm using metres to measure distance though you may have to convert metres to radius in your code.
本文标签: javascriptsequelize amp postgis sort by distance from pointStack Overflow
版权声明:本文标题:javascript - sequelize & postgis sort by distance from point - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741836570a2400250.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论