Skip to content

Commit

Permalink
Fix for rectangle and square grid dimensions (#2106)
Browse files Browse the repository at this point in the history
* Use non-haversine distance calc

* remove unused import

* fix up non-haversine

* Update turf/squareGrid test files as well

* prettier

* Fix the square grid tests properly

* prettier again

* update @turf/interpolate test output (because of square-grid changes)

* update @turf/square-grid test output

Co-authored-by: Matt Fedderly <mfedderly@palantir.com>
Co-authored-by: mfedderly <mdfedderly@mdfedderly.com>
  • Loading branch information
3 people authored Aug 5, 2021
1 parent cd86447 commit 446b27b
Show file tree
Hide file tree
Showing 20 changed files with 103,181 additions and 17,477 deletions.
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],
"cellWidth": 20,
"cellHeight": 100,
"units": "kilometers"
Expand Down
Loading

0 comments on commit 446b27b

Please sign in to comment.