How to find distance between 2 GEOGRAPAHICAL point in PostgreSQL, PostGIS using Sequelize

94 Views Asked by At

I am using Sequelize for PostgreSQL / PostGIS.

I have a table, station in PostgreSQL. It has a column location_point of type GEOGRAPHY. I have a user location, too. Now I want to find all the stations within 50miles radius of fixedPoint. I have tried multiple ways, but nothing works. Posting one of the solution below.

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

It is returning following error:

function st_distancesphere(geography, geography) does not exist

So, how can I calculate the distance between 2 geography points? Any assistance will be much appreciated.

PostGIS Version = 3.3 PostgreSQL Version = 15.1

0

There are 0 best solutions below