[GIS] Convert bounding box for OL2 to extent for OL3

openlayers

I have a database with cities and values for: bb_left, bb_top, bb_right, bb_bottom, locate_x, and locate_y.

Here's an example:

city    -   bb_left -   bb_top  -   bb_right    -   bb_bottom   -   locate_x    -   locate_y
geneve  -   493485  -   124865  -   506522      -   110870      -   499910      -   117983

The data was used for an older application using OpenLayers 2 which supports a bounding box containing just 4 values, however in OpenLayers 3 – I must create an extent with co-ordinate points but I am unsure how to create the co-ords based on the bounding box data.

Is this even possible to convert ?

Best Answer

ol.Extent is simply an array of 4 numbers. But you can translate this extent to a polygon with the function ol.geom.Polygon.fromExtent. And this polygon can be used to create a feature, so that you can display the bounding-box.

var extent = [bb_left, bb_bottom, bb_right, bb_top];
var polygon = ol.geom.Polygon.fromExtent(extent);
var feature = new ol.Feature({
  geometry: polygon
});
Related Question