加入两个边界框 bboxes

问题描述 投票:0回答:3

我有两个边界框,想创建一个包含这 2 个的大边界框 - 加入它们。

例如(turf.bbox的2个结果):

  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);

草坪等有什么简单的方法吗? 谢谢

javascript gis mapbox turfjs
3个回答
3
投票

还有一个非常简单的方法,不需要像 Turf 这样的库(Typescript 示例):

type BBox = [number, number, number, number];

const mergeBoundingBoxes = (boundingBoxes: BBox[]): BBox => {
  let minLeft: number = 180;
  let minBottom: number = 90;
  let maxRight: number = -180;
  let maxTop: number = -90;

  boundingBoxes.forEach(([left, bottom, right, top]) => {
    if (left < minLeft) minLeft = left;
    if (bottom < minBottom) minBottom = bottom;
    if (right > maxRight) maxRight = right;
    if (top > maxTop) maxTop = top;
  });

  return [minLeft, minBottom, maxRight, maxTop];
};

2
投票

也许这可以使用

bboxPolygon
combine
bbox
的组合来解决。
bboxPolygon
将边界框转换为多边形特征。

var resBbox = turf.bbox(turf.bboxPolygon(bboxCircles).combine(turf.bboxPolygon(bboxPoly)));

0
投票

另一种选择

turf.bbox(turf.union(turf.bboxPolygon(bbox1), turf.bboxPolygon(bbox2)))
© www.soinside.com 2019 - 2024. All rights reserved.