Alex
Alex

Reputation: 93

Find a point in a polygon using Sequelize and postgis

I'm using sequelize and postgis, and I have a table device with a column coord with type GEOMETRY('POINT').

when an insert a new device, in my columns coord value I use this:

coord = { type: 'Point', coordinates: [lng,lat], crs: { type: 'name', properties: { name: 'EPSG:3857'}} }

and it's ok for now.

Next, I need to find all devices in an area limited by a polygon

example in pseudo-code:

device coord is (lng1, lat1)

my poligon is [lng2 lat2, lng3 lat3, lng4 lat4, ...]

how can I do this query using sequelize?

sequelize 4.41.0 node v9.4.0

Upvotes: 1

Views: 827

Answers (2)

Hassan Rehan
Hassan Rehan

Reputation: 144

You can use sequelize.fn() to call the postgis function st_within(), which basically returns true if the first geometry argument is completely inside the second geometry argument. Here is an example

Device.findAll({
  where: sequelize.where(
    sequelize.fn('ST_Within', sequelize.col('coord'), sequelize.fn('ST_GeomFromText', 'POLYGON((lng2 lat2, lng3 lat3, lng4 lat4, ...))')),
    true
  )
});

Upvotes: 1

Rob
Rob

Reputation: 11

You need to use sequelize.fn() to call postgis st_within() function

https://postgis.net/docs/ST_Within.html

https://sequelize.readthedocs.io/en/v3/api/sequelize/#fnfn-args-sequelizefn

Upvotes: 0

Related Questions