Skip to content
Advertisement

Join two bounding boxes bboxes

I have two bounding boxes and want to create a big one that includes this 2 – join them.

For example(2 results of the turf.bbox):

  var bboxCircles = turf.bbox({
            "type": "FeatureCollection",
            "name": "bboxes",
            "features": circles
          });

          var bboxPoly = turf.bbox({
            "type": "FeatureCollection",
            "name": "bboxes",
            "features": polygon
          });

        bboxCircles = [10, 5, 15, 12];
        bboxPoly = [-35.9999999999999, -18.9999999999999, 35.4250000000001, 45.5000000000001];

var resBbox = bboxCircles.concat(bboxPoly).reduce(function(result, value, index, array) {
        if (index % 2 === 0)
          result.push(array.slice(index, index + 2));
        return result;
      }, []);

      var bounds = new mapboxgl.LngLatBounds();
      resBbox.forEach(item => {
          bounds.extend(item);
      });
      map.fitBounds(bounds);

Is there an easy way with turf etc.? thanks

Advertisement

Answer

Maybe this could be solved using a combination of bboxPolygon, combine and bbox. bboxPolygon converts a bounding box to a Polygon feature.

var resBbox = turf.bbox(turf.bboxPolygon(bboxCircles).combine(turf.bboxPolygon(bboxPoly)));
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement