[GIS] PostGIS – Bounding Box

extentspostgispostgresql

I have given two co-ordinate pairs [{long, Lat}, {Long, Lat}]
like this i have several co-ordinate pairs.I need to create bounding box with each co-ordinate pair.

Now i have one co-ordinate pair[{Long, Lat}]

I have to find with this one co-ordinate pair whether this intersects any of the bounding box.

Using Nodejs i can do this easily

var bbox = new BoundingBox({ minlat: 48.123, minlon: 16.23, maxlat: 49.012, maxlon: 16.367 })
var bbox2 = new BoundingBox({ lat: 48.5, lon: 16.5 })
console.log(bbox.intersects(bbox2)) // true 

But i have to store those co-rdinate pairs in postgis as bounding box and find the intersecting pair alone.

I have gone through certain links but i could not get clear knowledge out of it.

Best Answer

In PostGIS you can store the coordinate pairs to a table that has a column with the polygon geometry type and then use the ST_Intersects function.

For example, you can create a table data and insert the bounding box in your example like this:

CREATE TABLE data(id serial PRIMARY KEY NOT NULL, geom geometry(Polygon, 4326));
INSERT INTO data(geom) VALUES(ST_GeomFromText('POLYGON((16.23 48.123, 16.23 49.012, 16.367 49.012, 16.367 48.123, 16.23 48.123))', 4326));

The column named geom has the polygon type and SRID 4326. and you can insert the bounding boxes by using the ST_GeomFromText function that accepts the Well-known text (WKT) type geometries.

After you have inserted all the bounding boxes, you can use the following query to find the intersecting boxes and their ids:

SELECT id, ST_Intersects(ST_GeomFromText('POINT(16.5 48.5)',4326), geom) FROM data;