The SE_PointFromBSON() function
The SE_PointFromBson() function takes a bson value with spatial data following the GeoJSON specification and a JSON path in a VARCHAR string, and returns an ST_Point object.
Syntax
create function SE_PointFromBSON(bson,varchar(255))
returns ST_Point
Return type
ST_Point
Example
The city engineer wants to know the “Point” of a city, an array of at least two coordinate values of the position of the city. The first coordinate is longitude in the range of -180.0 and 180, the second value is latitude from -90 and 90.
The following code inserts one row for GeoJSON geometry type Point.
-- 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 values for Point
insert into bson_tab values(0, ('{
"city":{
"type":"Point",
"coordinates" : [3, 2]
}
}'::json)::bson,
'Point');
1 row(s) inserted.
The query selects a point value from the table by using
SE_PointFromBSON.
select pid,se_pointfrombson(geometry,'city') from bson_tab where type = 'Point' ;
This is the result.
pid 1
(expression) 4326 POINT (3 2)
1 row(s) retrieved.