Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
13 changes: 13 additions & 0 deletions packages/turf-transform-rotate/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,21 @@ function transformRotate<T extends GeoJSON | GeometryCollection>(
pointCoords[0] = newCoords[0];
pointCoords[1] = newCoords[1];
});
removeBbox(geojson);
return geojson;
}

function removeBbox(geojson: GeoJSON | GeometryCollection): void {
Comment thread
khalilurrrahmanridoykhan marked this conversation as resolved.
Outdated
delete geojson.bbox;

if (geojson.type === "Feature") {
if (geojson.geometry) removeBbox(geojson.geometry);
} else if (geojson.type === "FeatureCollection") {
geojson.features.forEach(removeBbox);
} else if (geojson.type === "GeometryCollection") {
geojson.geometries.forEach(removeBbox);
}
}
Comment thread
khalilurrrahmanridoykhan marked this conversation as resolved.
Outdated

export { transformRotate };
export default transformRotate;
50 changes: 50 additions & 0 deletions packages/turf-transform-rotate/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,53 @@ function makePivot(pivot: Coord, geojson: GeoJSON | GeometryCollection) {
}
return point(getCoord(pivot), { "marker-symbol": "circle" });
}

test("rotate -- removes stale bbox values", (t) => {
const input = nestedGeojsonWithBboxes();
const rotated = rotate(input, 45);

t.equal(countBboxes(rotated), 0, "removes bboxes from cloned output");
t.ok(countBboxes(input) > 0, "does not alter input by default");

rotate(input, 45, { mutate: true });
t.equal(countBboxes(input), 0, "removes bboxes from mutated input");
t.end();
});

function nestedGeojsonWithBboxes() {
const geojson = featureCollection([
geometryCollection([
lineString([
[0, 0],
[1, 1],
]).geometry,
]),
]);
addBboxes(geojson);
return geojson;
}

function addBboxes(geojson: GeoJSON | GeometryCollection): void {
geojson.bbox = [0, 0, 1, 1];
if (geojson.type === "Feature" && geojson.geometry) {
addBboxes(geojson.geometry);
} else if (geojson.type === "FeatureCollection") {
geojson.features.forEach(addBboxes);
} else if (geojson.type === "GeometryCollection") {
geojson.geometries.forEach(addBboxes);
}
}

function countBboxes(geojson: GeoJSON | GeometryCollection): number {
let count = geojson.bbox ? 1 : 0;
if (geojson.type === "Feature" && geojson.geometry) {
count += countBboxes(geojson.geometry);
} else if (geojson.type === "FeatureCollection") {
geojson.features.forEach((feature) => (count += countBboxes(feature)));
} else if (geojson.type === "GeometryCollection") {
geojson.geometries.forEach((geometry) => {
count += countBboxes(geometry);
});
}
return count;
}
15 changes: 14 additions & 1 deletion packages/turf-transform-scale/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ function transformScale<T extends GeoJSON | GeometryCollection>(
origin
);
});
if (factor !== 1) delete geojson.bbox;
return geojson;
}
// Scale Feature/Geometry
Expand Down Expand Up @@ -108,11 +109,23 @@ function scale<T extends GeoJSON | GeometryCollection>(
if (coord.length === 3) coord[2] *= factor;
});

delete feature.bbox;
removeBbox(feature);

return feature;
}

function removeBbox(geojson: GeoJSON | GeometryCollection): void {
delete geojson.bbox;

if (geojson.type === "Feature") {
if (geojson.geometry) removeBbox(geojson.geometry);
} else if (geojson.type === "FeatureCollection") {
geojson.features.forEach(removeBbox);
} else if (geojson.type === "GeometryCollection") {
geojson.geometries.forEach(removeBbox);
}
}

/**
* Define Origin
*
Expand Down
58 changes: 57 additions & 1 deletion packages/turf-transform-scale/test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { BBox, Feature, FeatureCollection } from "geojson";
import {
BBox,
Feature,
FeatureCollection,
GeoJSON,
GeometryCollection,
} from "geojson";
import fs from "fs";
import test from "tape";
import path from "path";
Expand Down Expand Up @@ -277,6 +283,56 @@ function colorize(geojson: FeatureCollection | Feature) {
return geojson;
}

test("scale -- removes stale bbox values", (t) => {
const input = nestedGeojsonWithBboxes();
const scaled = scale(input, 2, { origin: [0, 0] });

t.equal(countBboxes(scaled), 0, "removes bboxes from cloned output");
t.ok(countBboxes(input) > 0, "does not alter input by default");

scale(input, 2, { origin: [0, 0], mutate: true });
t.equal(countBboxes(input), 0, "removes bboxes from mutated input");
t.end();
});

function nestedGeojsonWithBboxes() {
const geojson = featureCollection([
geometryCollection([
lineString([
[0, 0],
[1, 1],
]).geometry,
]),
]);
addBboxes(geojson);
return geojson;
}

function addBboxes(geojson: GeoJSON | GeometryCollection): void {
geojson.bbox = [0, 0, 1, 1];
if (geojson.type === "Feature" && geojson.geometry) {
addBboxes(geojson.geometry);
} else if (geojson.type === "FeatureCollection") {
geojson.features.forEach(addBboxes);
} else if (geojson.type === "GeometryCollection") {
geojson.geometries.forEach(addBboxes);
}
}

function countBboxes(geojson: GeoJSON | GeometryCollection): number {
let count = geojson.bbox ? 1 : 0;
if (geojson.type === "Feature" && geojson.geometry) {
count += countBboxes(geojson.geometry);
} else if (geojson.type === "FeatureCollection") {
geojson.features.forEach((feature) => (count += countBboxes(feature)));
} else if (geojson.type === "GeometryCollection") {
geojson.geometries.forEach((geometry) => {
count += countBboxes(geometry);
});
}
return count;
}

// define origin, as defined in transform-scale, and style it
function markedOrigin(
geojson: Feature,
Expand Down
13 changes: 13 additions & 0 deletions packages/turf-transform-translate/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,21 @@ function transformTranslate<T extends GeoJSON | GeometryCollection>(
if (zTranslation && pointCoords.length === 3)
pointCoords[2] += zTranslation;
});
removeBbox(geojson);
return geojson;
}

function removeBbox(geojson: GeoJSON | GeometryCollection): void {
delete geojson.bbox;

if (geojson.type === "Feature") {
if (geojson.geometry) removeBbox(geojson.geometry);
} else if (geojson.type === "FeatureCollection") {
geojson.features.forEach(removeBbox);
} else if (geojson.type === "GeometryCollection") {
geojson.geometries.forEach(removeBbox);
}
}

export { transformTranslate };
export default transformTranslate;
52 changes: 51 additions & 1 deletion packages/turf-transform-translate/test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Feature } from "geojson";
import { Feature, GeoJSON, GeometryCollection } from "geojson";
import fs from "fs";
import test from "tape";
import path from "path";
Expand Down Expand Up @@ -152,3 +152,53 @@ function colorize(geojson: Feature) {
}
return geojson;
}

test("translate -- removes stale bbox values", (t) => {
const input = nestedGeojsonWithBboxes();
const translated = translate(input, 10, 45);

t.equal(countBboxes(translated), 0, "removes bboxes from cloned output");
t.ok(countBboxes(input) > 0, "does not alter input by default");

translate(input, 10, 45, { mutate: true });
t.equal(countBboxes(input), 0, "removes bboxes from mutated input");
t.end();
});

function nestedGeojsonWithBboxes() {
const geojson = featureCollection([
geometryCollection([
lineString([
[0, 0],
[1, 1],
]).geometry,
]),
]);
addBboxes(geojson);
return geojson;
}

function addBboxes(geojson: GeoJSON | GeometryCollection): void {
geojson.bbox = [0, 0, 1, 1];
if (geojson.type === "Feature" && geojson.geometry) {
addBboxes(geojson.geometry);
} else if (geojson.type === "FeatureCollection") {
geojson.features.forEach(addBboxes);
} else if (geojson.type === "GeometryCollection") {
geojson.geometries.forEach(addBboxes);
}
}

function countBboxes(geojson: GeoJSON | GeometryCollection): number {
let count = geojson.bbox ? 1 : 0;
if (geojson.type === "Feature" && geojson.geometry) {
count += countBboxes(geojson.geometry);
} else if (geojson.type === "FeatureCollection") {
geojson.features.forEach((feature) => (count += countBboxes(feature)));
} else if (geojson.type === "GeometryCollection") {
geojson.geometries.forEach((geometry) => {
count += countBboxes(geometry);
});
}
return count;
}
Loading