TurfJS 得到 bbox 给定两个点

Cai*_*ius 3 geojson leaflet turfjs

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

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

我正在寻找一种方法来将这段代码转换为普通 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);
Run Code Online (Sandbox Code Playgroud)

我仍然是整个 GeoJSON 的新手,也许我错过了一些东西,有人可以帮助我吗?

swa*_*hai 5

这是仅使用 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
Run Code Online (Sandbox Code Playgroud)

输出:

{"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]]]
    }
}
Run Code Online (Sandbox Code Playgroud)

编辑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
Run Code Online (Sandbox Code Playgroud)
{"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]]]
    }
}
Run Code Online (Sandbox Code Playgroud)