Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for rectangle and square grid dimensions #2106

Merged
merged 13 commits into from
Aug 5, 2021
Merged
1,880 changes: 1,255 additions & 625 deletions packages/turf-interpolate/test/out/data-1km.geojson

Large diffs are not rendered by default.

8,609 changes: 5,848 additions & 2,761 deletions packages/turf-interpolate/test/out/data-500m.geojson

Large diffs are not rendered by default.

636 changes: 444 additions & 192 deletions packages/turf-interpolate/test/out/data-weight-2.geojson

Large diffs are not rendered by default.

17 changes: 7 additions & 10 deletions packages/turf-rectangle-grid/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import intersect from "@turf/boolean-intersects";
import distance from "@turf/distance";
import {
BBox,
convertLength,
Feature,
featureCollection,
FeatureCollection,
Expand Down Expand Up @@ -54,17 +54,14 @@ function rectangleGrid<P = Properties>(
const east = bbox[2];
const north = bbox[3];

const xFraction = cellWidth / distance([west, south], [east, south], options);
const cellWidthDeg = xFraction * (east - west);
const yFraction =
cellHeight / distance([west, south], [west, north], options);
const cellHeightDeg = yFraction * (north - south);

// rows & columns
const bboxWidth = east - west;
const cellWidthDeg = convertLength(cellWidth, options.units, "degrees");

const bboxHeight = north - south;
const columns = Math.floor(bboxWidth / cellWidthDeg);
const rows = Math.floor(bboxHeight / cellHeightDeg);
const cellHeightDeg = convertLength(cellHeight, options.units, "degrees");

const columns = Math.floor(Math.abs(bboxWidth) / cellWidthDeg);
const rows = Math.floor(Math.abs(bboxHeight) / cellHeightDeg);

// if the grid does not fill the bbox perfectly, center it.
const deltaX = (bboxWidth - columns * cellWidthDeg) / 2;
Expand Down
6 changes: 6 additions & 0 deletions packages/turf-rectangle-grid/test/in/global-grid.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"bbox": [-180, -90, 180, 90],
"cellWidth": 10,
"cellHeight": 10,
"units": "degrees"
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"bbox": [141, -34, 150, -39],
"bbox": [141, -39, 150, -34],
mfedderly marked this conversation as resolved.
Show resolved Hide resolved
"cellWidth": 20,
"cellHeight": 100,
"units": "kilometers"
Expand Down
Loading