turfjs sandbox
by John Doe
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/Turf.js/6.5.0/turf.min.js"></script>
<div id="out"></div>
JavaScript
const getGeoJSONArea = (geojson) => {
if (!geojson || !geojson.hasOwnProperty('features')) return;
let total = 0;
geojson.features.forEach((feature) => {
let coords = feature.geometry.coordinates[0];
let polygon = turf.polygon(coords);
let area = turf.area(polygon);
console.log(area)
total += area;
});
return total;
};
const g = { "type": "FeatureCollection", "features": [{ "type": "Feature", "properties": { "id": 0 }, "geometry": { "type": "MultiPolygon", "coordinates": [ [ [ [ 34.38448556, 33.17984423 ], [ 34.38448556, 33.21272546 ], [ 34.46937237, 33.17970761 ], [ 34.38448556, 33.17984423 ] ] ] ] } }]}
const o = getGeoJSONArea(g);
document.getElementById('out').innerHTML = o/1000000;
var line = turf.lineString([
[-74, 40],
[-78, 42],
[-82, 35],
]);
var bbox = turf.bbox(line);
console.log(bbox)
var bboxPolygon = turf.bboxPolygon(bbox);
console.log(bboxPolygon)