| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- import test from 'node:test'
- import assert from 'node:assert/strict'
- import { lineString, multiLineString, polygon } from '@turf/turf'
- import {
- PRIMARY_AREA_BOUNDS,
- clipFeatureToBounds,
- isInsidePrimaryArea,
- isWithinPrimaryArea,
- } from './primary-area.mjs'
- const bounds = [0, 0, 10, 10]
- test('the configured primary-area corner coordinates are ordered and finite', () => {
- assert.equal(PRIMARY_AREA_BOUNDS.length, 4)
- assert.ok(PRIMARY_AREA_BOUNDS[0] < PRIMARY_AREA_BOUNDS[2])
- assert.ok(PRIMARY_AREA_BOUNDS[1] < PRIMARY_AREA_BOUNDS[3])
- assert.ok(isInsidePrimaryArea([PRIMARY_AREA_BOUNDS[0], PRIMARY_AREA_BOUNDS[1]]))
- })
- test('building footprints require complete containment, not just an in-bounds center', () => {
- const crosses = polygon([[[4, 4], [12, 4], [12, 6], [4, 6], [4, 4]]])
- const contained = polygon([[[4, 4], [6, 4], [6, 6], [4, 6], [4, 4]]])
- assert.equal(isWithinPrimaryArea(crosses, bounds), false)
- assert.equal(isWithinPrimaryArea(contained, bounds), true)
- })
- test('clipFeatureToBounds clips line segments extending outside the primary area', () => {
- const feature = lineString([[-2, 5], [5, 5], [12, 5]], { kind: 'road' })
- const result = clipFeatureToBounds(feature, bounds)
- assert.deepEqual(result.geometry.coordinates, [[0, 5], [5, 5], [10, 5]])
- assert.deepEqual(result.properties, { kind: 'road' })
- })
- test('clipFeatureToBounds clips multi-line roads to the primary area', () => {
- const feature = multiLineString([[[-1, 2], [3, 2]], [[7, 9], [11, 9]]])
- const result = clipFeatureToBounds(feature, bounds)
- assert.ok(result.geometry.coordinates.flat(1).every(([x, y]) => x >= 0 && x <= 10 && y >= 0 && y <= 10))
- })
- test('clipFeatureToBounds clips polygon footprints without preserving out-of-area pieces', () => {
- const feature = polygon([[[-2, 2], [5, 2], [5, 8], [-2, 8], [-2, 2]]])
- const result = clipFeatureToBounds(feature, bounds)
- assert.ok(isWithinPrimaryArea(result, bounds))
- })
- test('clipFeatureToBounds rejects geometry entirely outside the area', () => {
- const feature = lineString([[-4, -4], [-2, -2]])
- assert.equal(clipFeatureToBounds(feature, bounds), null)
- })
|