The SE_BBoxFromBSON() function
The SE_BBoxFromBSON() function takes a bson value with spatial data following the GeoJSON specification and a JSON path in a VARCHAR string, and returns an ST_Polygon object.
Syntax
create function SE_BBoxFromBSON(bson,varchar(255))
returns ST_Polygon
Return type
ST_Polygon
Example
The city engineer wants to know the “Bounding Box” of a city.
The following code inserts one row for GeoJSON geometry type Polygon.
-- create a table of bson value containing GeoJson spatial values under the city tag
create table bson_tab (pid serial, geometry bson, type varchar(16));
Table created.
insert into bson_tab values(0, ('{
"city":{
"type":"Polygon",
"coordinates" : [[[0, 0], [10, 0], [10, 10], [0, 10], [0, 0]], [[2, 2], [8, 2], [8, 8], [2, 8], [2, 2]]]
}
}'::json)::bson,
'Polygon');
1 row(s) inserted.
The query selects a bounding box value from the table by using
SE_BBoxFromBSON.
select pid, se_bboxfrombson(geometry, 'city') from bson_tab;
This is the result.
pid 1
(expression) 4326 POLYGON ((2.9999999 1.9999999, 3.0000001 1.9999999, 3.000000
1 2.0000001, 2.9999999 2.0000001, 2.9999999 1.9999999))
pid 2
(expression) 4326 POLYGON ((1 0, 4 0, 4 5, 1 5, 1 0))
pid 3
(expression) 4326 POLYGON ((0 0, 10 0, 10 10, 0 10, 0 0))
pid 4
(expression) 4326 POLYGON ((1 0, 4 0, 4 5, 1 5, 1 0))
pid 5
(expression) 4326 POLYGON ((1 0, 135 0, 135 21, 1 21, 1 0))
pid 6
(expression) 4326 POLYGON ((0 0, 28 0, 28 28, 0 28, 0 0))
6 row(s) retrieved.