如何使用 Sequelize 在 PostgreSQL、PostGIS 中查找 2 个 GEOGRAPAHICAL 点之间的距离

问题描述 投票:0回答:1

我正在为 PostgreSQL / PostGIS 使用 Sequelize。

我在 PostgreSQL 中有一个表,一个站。它有一个类型为 GEOGRAPHY 的 location_point 列。 我也有一个用户位置。 现在我想找到 fixedPoint 半径 50 英里范围内的所有站点。 我尝试了多种方法,但没有任何效果。发布以下解决方案之一。

let radius = 50 * 1000; // miles into meters

// creating GEOGRAPHY point from user's location
const location = db.fn('ST_GeogFromText', `POINT(${long} ${lat})`);

// finding distance between location and location_point (column of type GEOGRAPHY from database)
const distance = db.fn(
    "ST_DistanceSphere",
    location,
    db.col("location_point")
);

// Sequelize query
let stations = await Station.findAll({
    order: distance,
    where: db.where(distance, { [Op.lte]: radius }),
});

返回以下错误:

function st_distancesphere(geography, geography) 不存在

那么,如何计算两个地理点之间的距离呢? 任何帮助将不胜感激。

PostGIS 版本 = 3.3 PostgreSQL 版本 = 15.1

postgresql geometry sequelize.js postgis geography
1个回答
0
投票

错误消息表明

ST_DistanceSphere
这可能是因为您使用的是过时的 Postgresql/PostGIS。 尽管您可以改用
ST_Distance

let radius = 50 * 1000; // miles into meters

// creat GEOGRAPHY point from user location
const location = db.fn('ST_GeogFromText', `POINT(${long} ${lat})`);

// find distance between location and location_point (column of type GEOGRAPHY from database)
const distance = db.fn(
    "ST_Distance",
    location,
    db.col("location_point")
);

// Sequelize query
let stations = await Station.findAll({
    order: distance,
    where: db.where(distance, { [Op.lte]: radius }),
});

这可以计算距离 b/n 用户的位置和每个站点位置 pt。并返回指定半径内的所有站点。

© www.soinside.com 2019 - 2024. All rights reserved.