primary-area.test.mjs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import test from 'node:test'
  2. import assert from 'node:assert/strict'
  3. import { lineString, multiLineString, polygon } from '@turf/turf'
  4. import {
  5. PRIMARY_AREA_BOUNDS,
  6. clipFeatureToBounds,
  7. isInsidePrimaryArea,
  8. isWithinPrimaryArea,
  9. } from './primary-area.mjs'
  10. const bounds = [0, 0, 10, 10]
  11. test('the configured primary-area corner coordinates are ordered and finite', () => {
  12. assert.equal(PRIMARY_AREA_BOUNDS.length, 4)
  13. assert.ok(PRIMARY_AREA_BOUNDS[0] < PRIMARY_AREA_BOUNDS[2])
  14. assert.ok(PRIMARY_AREA_BOUNDS[1] < PRIMARY_AREA_BOUNDS[3])
  15. assert.ok(isInsidePrimaryArea([PRIMARY_AREA_BOUNDS[0], PRIMARY_AREA_BOUNDS[1]]))
  16. })
  17. test('building footprints require complete containment, not just an in-bounds center', () => {
  18. const crosses = polygon([[[4, 4], [12, 4], [12, 6], [4, 6], [4, 4]]])
  19. const contained = polygon([[[4, 4], [6, 4], [6, 6], [4, 6], [4, 4]]])
  20. assert.equal(isWithinPrimaryArea(crosses, bounds), false)
  21. assert.equal(isWithinPrimaryArea(contained, bounds), true)
  22. })
  23. test('clipFeatureToBounds clips line segments extending outside the primary area', () => {
  24. const feature = lineString([[-2, 5], [5, 5], [12, 5]], { kind: 'road' })
  25. const result = clipFeatureToBounds(feature, bounds)
  26. assert.deepEqual(result.geometry.coordinates, [[0, 5], [5, 5], [10, 5]])
  27. assert.deepEqual(result.properties, { kind: 'road' })
  28. })
  29. test('clipFeatureToBounds clips multi-line roads to the primary area', () => {
  30. const feature = multiLineString([[[-1, 2], [3, 2]], [[7, 9], [11, 9]]])
  31. const result = clipFeatureToBounds(feature, bounds)
  32. assert.ok(result.geometry.coordinates.flat(1).every(([x, y]) => x >= 0 && x <= 10 && y >= 0 && y <= 10))
  33. })
  34. test('clipFeatureToBounds clips polygon footprints without preserving out-of-area pieces', () => {
  35. const feature = polygon([[[-2, 2], [5, 2], [5, 8], [-2, 8], [-2, 2]]])
  36. const result = clipFeatureToBounds(feature, bounds)
  37. assert.ok(isWithinPrimaryArea(result, bounds))
  38. })
  39. test('clipFeatureToBounds rejects geometry entirely outside the area', () => {
  40. const feature = lineString([[-4, -4], [-2, -2]])
  41. assert.equal(clipFeatureToBounds(feature, bounds), null)
  42. })