|
| 1 | +import type * as geojson from 'geojson' |
| 2 | +import { MAX_LEVEL } from '../s2/cellid_constants' |
| 3 | +import { CellUnion } from '../s2/CellUnion' |
| 4 | +import * as geometry from './geometry' |
| 5 | +import { Polyline } from '../s2/Polyline' |
| 6 | +import { Polygon } from '../s2/Polygon' |
| 7 | +import type { Region } from '../s2/Region' |
| 8 | +import type { RegionCovererOptions as S2RegionCovererOptions } from '../s2/RegionCoverer' |
| 9 | +import { RegionCoverer as S2RegionCoverer } from '../s2/RegionCoverer' |
| 10 | + |
| 11 | +/** |
| 12 | + * RegionCovererOptions allows the RegionCoverer to be configured. |
| 13 | + */ |
| 14 | +export interface RegionCovererOptions extends S2RegionCovererOptions { |
| 15 | + /** |
| 16 | + * the maximum desired number of cells for each member of a multi-member geometry in the approximation. |
| 17 | + * @default Math.max(Math.floor(maxCells / 10), 8) |
| 18 | + */ |
| 19 | + memberMaxCells?: number |
| 20 | + |
| 21 | + /** |
| 22 | + * the maximum size the approximation may reach before a compaction is triggered. |
| 23 | + * used to avoid OOM errors. |
| 24 | + * @default 65536 |
| 25 | + */ |
| 26 | + compactAt?: number |
| 27 | + |
| 28 | + /** |
| 29 | + * the maximum area of a shape to be considered for fast covering. |
| 30 | + * used to speed up covering small shapes. |
| 31 | + * area values are between 0 and 4*Pi. |
| 32 | + * @default 1e-6 |
| 33 | + */ |
| 34 | + smallAreaEpsilon?: number |
| 35 | +} |
| 36 | + |
| 37 | +/** |
| 38 | + * RegionCoverer allows arbitrary GeoJSON geometries to be approximated as unions of cells (CellUnion). |
| 39 | + * |
| 40 | + * Typical usage: |
| 41 | + * |
| 42 | + * feature = loadGeoJSON() |
| 43 | + * rc = new RegionCoverer({ maxCells: 256, memberMaxCells: 64 }) |
| 44 | + * covering = rc.covering(feature.geometry) |
| 45 | + * |
| 46 | + * @beta unstable API |
| 47 | + */ |
| 48 | +export class RegionCoverer { |
| 49 | + coverer: S2RegionCoverer |
| 50 | + memberCoverer: S2RegionCoverer |
| 51 | + compactAt: number |
| 52 | + smallAreaEpsilon: number |
| 53 | + |
| 54 | + /** |
| 55 | + * Returns a new RegionCoverer with the appropriate defaults. |
| 56 | + * |
| 57 | + * @param options - RegionCoverer options |
| 58 | + * |
| 59 | + * @category Constructors |
| 60 | + */ |
| 61 | + constructor({ |
| 62 | + minLevel = 0, |
| 63 | + maxLevel = MAX_LEVEL, |
| 64 | + levelMod = 1, |
| 65 | + maxCells = 8, |
| 66 | + memberMaxCells = Math.max(Math.floor(maxCells / 10), 8), |
| 67 | + compactAt = 65536, |
| 68 | + smallAreaEpsilon = 1e-6 |
| 69 | + }: RegionCovererOptions = {}) { |
| 70 | + this.coverer = new S2RegionCoverer({ minLevel, maxLevel, levelMod, maxCells }) |
| 71 | + this.memberCoverer = new S2RegionCoverer({ minLevel, maxLevel, levelMod, maxCells: memberMaxCells }) |
| 72 | + this.compactAt = compactAt |
| 73 | + this.smallAreaEpsilon = smallAreaEpsilon |
| 74 | + } |
| 75 | + |
| 76 | + /** Computes the covering of a multi-member geometry (ie. MultiPoint, MultiLineString, MultiPolygon). */ |
| 77 | + private mutliMemberCovering(shapes: Region[]): CellUnion { |
| 78 | + // sort shapes from largest to smallest |
| 79 | + shapes.sort((a: Region, b: Region): number => RegionCoverer.area(b) - RegionCoverer.area(a)) |
| 80 | + |
| 81 | + let union = new CellUnion() |
| 82 | + shapes.forEach((shape: Region) => { |
| 83 | + // optionally elect to use a fast covering method for small areas |
| 84 | + const fast = union.length >= this.memberCoverer.maxCells && RegionCoverer.area(shape) < this.smallAreaEpsilon |
| 85 | + |
| 86 | + // append covering to union |
| 87 | + union = CellUnion.fromUnion( |
| 88 | + union, |
| 89 | + fast ? this.memberCoverer.fastCovering(shape) : this.memberCoverer.covering(shape) |
| 90 | + ) |
| 91 | + |
| 92 | + // force compact large coverings to avoid OOM errors |
| 93 | + if (union.length >= this.compactAt) union = this.coverer.covering(union) |
| 94 | + }) |
| 95 | + |
| 96 | + // reduce the global covering to maxCells |
| 97 | + if (union.length < this.coverer.maxCells) return union |
| 98 | + return this.coverer.covering(union) |
| 99 | + } |
| 100 | + |
| 101 | + /** Returns a CellUnion that covers the given GeoJSON geometry and satisfies the various restrictions. */ |
| 102 | + covering(geom: geojson.Geometry): CellUnion { |
| 103 | + const shape = geometry.unmarshal(geom) |
| 104 | + if (Array.isArray(shape)) return this.mutliMemberCovering(shape as Region[]) |
| 105 | + return this.coverer.covering(shape) |
| 106 | + } |
| 107 | + |
| 108 | + /** Computes the area of a shape */ |
| 109 | + static area(shape: Region): number { |
| 110 | + if (shape instanceof Polygon) return shape.area() |
| 111 | + if (shape instanceof Polyline) shape.capBound().area() |
| 112 | + return 0 |
| 113 | + } |
| 114 | +} |
0 commit comments