我正在尝试在后端移动一个客户端方法:
客户端方法是使用TurfJS和Leaflet来执行一些操作。唯一的问题是服务器端(nodejs)窗口不可用,因此我不能使用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的事情还是个新手,也许我错过了什么,有人能帮我吗?
发布于 2019-07-24 22:10:07
以下是仅使用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>
https://stackoverflow.com/questions/57164687
复制相似问题