给定两个点,TurfJS 得到 bbox

TurfJS get bbox given two Points

我正在尝试在后端移动客户端方法:

客户端方法是使用TurfJS和Leaflet来执行一些操作。唯一的问题是服务器端 (nodejs) window 不可用,因此我无法使用 Leaflet。

我正在寻找一种方法来将这段代码转换为 vanilla Turf 等价物:

    const pointA = turf.point([originCoords.lng, originCoords.lat]);
    const pointB = turf.destination.default(pointA, 50, 45, { units: 'kilometers' });

    // here I'm using leaflet to get a BBox
    const latLngBounds = L.latLngBounds(
        L.latLng(pointA.geometry.coordinates[1],
            pointA.geometry.coordinates[0]),
        L.latLng(pointB.geometry.coordinates[1], pointB.geometry.coordinates[0]),
    );

    // using that BBox I then create the rectangle, again with leaflet
    tile = L.rectangle(latLngBounds);

我对整个 GeoJSON 还是个新手,也许我遗漏了什么,有人可以帮助我吗?

这里是仅使用 Turf.JS 的代码。

var turf = require('@turf/turf');
var pointA = turf.point([-75.343, 39.984]);
var pointB = turf.destination(pointA, 50, 45, { units: 'kilometers' });
//create a bbox that extends to include all the features
var bbx = turf.bbox(turf.featureCollection([pointA, pointB]));
var pgn = turf.bboxPolygon(bbx);  //convert it to Polygon feature
console.log(JSON.stringify(pgn)); //output to console

输出:

{"type":"Feature","properties":{},
    "geometry":{
    "type":"Polygon",
    "coordinates":[[
        [-75.343,39.984],
        [-74.92609,39.984],
        [-74.92609,40.3012],
        [-75.343,40.3012],
        [-75.343,39.984]]]
    }
}

编辑 1

为运行代码添加能力:

//var turf = require('@turf/turf');
var pointA = turf.point([-75.343, 39.984]);
var pointB = turf.destination(pointA, 50, 45, { units: 'kilometers' });
//create a bbox that extends to include all the features
var bbx = turf.bbox(turf.featureCollection([pointA, pointB]));
var pgn = turf.bboxPolygon(bbx);  //convert it to Polygon feature
console.log(JSON.stringify(pgn)); //output to console
<script src="https://cdnjs.cloudflare.com/ajax/libs/Turf.js/5.1.5/turf.min.js"></script>