Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions packages/turf-square/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { distance } from "@turf/distance";
import { BBox } from "geojson";

/**
Expand All @@ -21,22 +20,24 @@ function square(bbox: BBox): BBox {
var east = bbox[2];
var north = bbox[3];

var horizontalDistance = distance(bbox.slice(0, 2), [east, south]);
var verticalDistance = distance(bbox.slice(0, 2), [west, north]);
if (horizontalDistance >= verticalDistance) {
// Spans are compared in degrees, the same units they are grown in below, so
// that the result always surrounds the input.
var horizontalSpan = east - west;
var verticalSpan = north - south;
if (horizontalSpan >= verticalSpan) {
var verticalMidpoint = (south + north) / 2;
return [
west,
verticalMidpoint - (east - west) / 2,
verticalMidpoint - horizontalSpan / 2,
east,
verticalMidpoint + (east - west) / 2,
verticalMidpoint + horizontalSpan / 2,
];
} else {
var horizontalMidpoint = (west + east) / 2;
return [
horizontalMidpoint - (north - south) / 2,
horizontalMidpoint - verticalSpan / 2,
south,
horizontalMidpoint + (north - south) / 2,
horizontalMidpoint + verticalSpan / 2,
north,
];
}
Expand Down
4 changes: 3 additions & 1 deletion packages/turf-square/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
"version": "7.4.0",
"description": "Takes a bounding box and calculates the minimum square bounding box that would contain the input.",
"author": "Turf Authors",
"contributors": [
"Jayesh Bhade <@Jaybhade>"
],
"license": "MIT",
"bugs": {
"url": "https://github.com/Turfjs/turf/issues"
Expand Down Expand Up @@ -49,7 +52,6 @@
"typescript": "catalog:"
},
"dependencies": {
"@turf/distance": "workspace:*",
"@turf/helpers": "workspace:*",
"@types/geojson": "catalog:"
},
Expand Down
11 changes: 11 additions & 0 deletions packages/turf-square/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,14 @@ test("square", function (t) {
t.deepEqual(sq2, [0, -2.5, 10, 7.5]);
t.end();
});

test("square -- surrounds the input away from the equator", function (t) {
// Wider than they are tall in degrees, but the great circle distance along
// their southern edge is the shorter of the two.
const bbox1: BBox = [0, 60, 10, 66];
const bbox2: BBox = [100, -70, 112, -63];

t.deepEqual(square(bbox1), [0, 58, 10, 68]);
t.deepEqual(square(bbox2), [100, -72.5, 112, -60.5]);
t.end();
});
3 changes: 0 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.