So i'm currently playing around with turfjs. And i'm trying to add a squaregrid inside a polygon.
So here's the code
var triangleCoords = [
{ lat: 25.774, lng: -80.19 },
{ lat: 18.466, lng: -66.118 },
{ lat: 32.321, lng: -64.757 },
{ lat: 25.774, lng: -80.19 }
];
// Construct the polygon.
var bermudaTriangle = new google.maps.Polygon({
paths: triangleCoords,
strokeColor: "#FF0000",
strokeOpacity: 0.8,
strokeWeight: 2,
fillColor: "#FF0000",
fillOpacity: 0.35
});
bermudaTriangle.setMap(map);
const geoJSON = {
type: 'Polygon',
coordinates: []
};
// convert to bermudaTriangle path to geojson
for (let point of bermudaTriangle.getPath().getArray()) {
geoJSON.coordinates.push([point.lat(), point.lng()]);
}
const feature = turf.feature(geoJSON);
const bbox = turf.bbox(feature);
// ERROR: this one's showing Infinity values
console.table(bbox);
const grid = turf.squareGrid(bbox, 50, {
units: "miles",
mask: feature
});
map.data.addGeoJson(grid);
and looking at the console, it shows Infinity values for bbox as commented on the code.
I've added a link for the code https://codepen.io/chan-dev/pen/yrdRoM
geoJSON
is not a valid GeoJSON object for Polygon in the provided example, that's the reason why turf.bbox
returns invalid result. GeoJSON for polygon could be constructed via turf.polygon
like this:
var triangleCoords = [
{ lat: 25.774, lng: -80.19 },
{ lat: 18.466, lng: -66.118 },
{ lat: 32.321, lng: -64.757 },
{ lat: 25.774, lng: -80.19 }
];
var data = triangleCoords.map(coord => {
return [coord.lng, coord.lat];
});
var geojson = turf.polygon([data]);
and bounding box calculated like this:
const bbox = turf.bbox(geojson);