Skip to content
Advertisement

Convert bbox coordinates to geoJSON

Hello I have a array of coordinates that look something like this: [14.540536348680394,65.03068471588048,18.945463651319603,66.82731528411952]

I need to properly display it on my openlayers map but it seems like I must convert the bbox to a valid geoJSON object first. Is there a fuction I can write or a library that can do this for me? Or is there any work arounds using openlayers, I have searched through github but all I could find is libraries that convert geoJSON into bboxes and not the other way around. I am trying to use the bbox to create a shape on a openlayers map with this function and the source is required to be geoJSON.

map.addInteraction(
  new Draw({
    type: 'Polygon',
    source: source,
  })
);

Advertisement

Answer

Run npm install @turf/bbox-polygon in order to install the @turf/bbox-polygon package and transform the bbox that you have with turf.bboxPolygon(bbox). Here is the npm repository: https://www.npmjs.com/package/@turf/bbox-polygon.
For example:

const bboxPolygon = require('@turf/bbox-polygon').default
const bbox = [14.540536348680394,65.03068471588048,18.945463651319603,66.82731528411952]
const polygon = bboxPolygon(bbox)
console.log(polygon)
Advertisement