|
1 | | -import { BBox, Feature, FeatureCollection } from "geojson"; |
| 1 | +import { |
| 2 | + BBox, |
| 3 | + Feature, |
| 4 | + FeatureCollection, |
| 5 | + GeoJSON, |
| 6 | + GeometryCollection, |
| 7 | +} from "geojson"; |
2 | 8 | import fs from "fs"; |
3 | 9 | import test from "tape"; |
4 | 10 | import path from "path"; |
@@ -277,6 +283,56 @@ function colorize(geojson: FeatureCollection | Feature) { |
277 | 283 | return geojson; |
278 | 284 | } |
279 | 285 |
|
| 286 | +test("scale -- removes stale bbox values", (t) => { |
| 287 | + const input = nestedGeojsonWithBboxes(); |
| 288 | + const scaled = scale(input, 2, { origin: [0, 0] }); |
| 289 | + |
| 290 | + t.equal(countBboxes(scaled), 0, "removes bboxes from cloned output"); |
| 291 | + t.ok(countBboxes(input) > 0, "does not alter input by default"); |
| 292 | + |
| 293 | + scale(input, 2, { origin: [0, 0], mutate: true }); |
| 294 | + t.equal(countBboxes(input), 0, "removes bboxes from mutated input"); |
| 295 | + t.end(); |
| 296 | +}); |
| 297 | + |
| 298 | +function nestedGeojsonWithBboxes() { |
| 299 | + const geojson = featureCollection([ |
| 300 | + geometryCollection([ |
| 301 | + lineString([ |
| 302 | + [0, 0], |
| 303 | + [1, 1], |
| 304 | + ]).geometry, |
| 305 | + ]), |
| 306 | + ]); |
| 307 | + addBboxes(geojson); |
| 308 | + return geojson; |
| 309 | +} |
| 310 | + |
| 311 | +function addBboxes(geojson: GeoJSON | GeometryCollection): void { |
| 312 | + geojson.bbox = [0, 0, 1, 1]; |
| 313 | + if (geojson.type === "Feature" && geojson.geometry) { |
| 314 | + addBboxes(geojson.geometry); |
| 315 | + } else if (geojson.type === "FeatureCollection") { |
| 316 | + geojson.features.forEach(addBboxes); |
| 317 | + } else if (geojson.type === "GeometryCollection") { |
| 318 | + geojson.geometries.forEach(addBboxes); |
| 319 | + } |
| 320 | +} |
| 321 | + |
| 322 | +function countBboxes(geojson: GeoJSON | GeometryCollection): number { |
| 323 | + let count = geojson.bbox ? 1 : 0; |
| 324 | + if (geojson.type === "Feature" && geojson.geometry) { |
| 325 | + count += countBboxes(geojson.geometry); |
| 326 | + } else if (geojson.type === "FeatureCollection") { |
| 327 | + geojson.features.forEach((feature) => (count += countBboxes(feature))); |
| 328 | + } else if (geojson.type === "GeometryCollection") { |
| 329 | + geojson.geometries.forEach((geometry) => { |
| 330 | + count += countBboxes(geometry); |
| 331 | + }); |
| 332 | + } |
| 333 | + return count; |
| 334 | +} |
| 335 | + |
280 | 336 | // define origin, as defined in transform-scale, and style it |
281 | 337 | function markedOrigin( |
282 | 338 | geojson: Feature, |
|
0 commit comments