@classic-homes/maps-core 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,435 @@
1
+ import { G as GeoJSONFeatureCollection, a as GeoJSONFeature, B as BoundsArray, M as MapBounds } from './data-DDS6ok7I.js';
2
+ import RBush from 'rbush';
3
+
4
+ /**
5
+ * Spatial indexing utilities for viewport-based culling
6
+ *
7
+ * These utilities help optimize rendering by:
8
+ * 1. Building a spatial index of features for fast queries
9
+ * 2. Filtering features based on viewport bounds
10
+ * 3. Supporting buffer zones to prevent pop-in at edges
11
+ */
12
+
13
+ /**
14
+ * A cell in the spatial grid
15
+ */
16
+ interface GridCell {
17
+ features: GeoJSONFeature[];
18
+ }
19
+ /**
20
+ * Spatial index using a grid-based approach
21
+ * Fast to build and query, good for uniformly distributed data
22
+ */
23
+ interface SpatialIndex {
24
+ /** Grid of cells containing features */
25
+ grid: Map<string, GridCell>;
26
+ /** Number of columns in the grid */
27
+ cols: number;
28
+ /** Number of rows in the grid */
29
+ rows: number;
30
+ /** Bounds of all indexed data */
31
+ bounds: BoundsArray;
32
+ /** Width of each cell in degrees */
33
+ cellWidth: number;
34
+ /** Height of each cell in degrees */
35
+ cellHeight: number;
36
+ /** Total number of features indexed */
37
+ featureCount: number;
38
+ }
39
+ /**
40
+ * Options for building a spatial index
41
+ */
42
+ interface BuildSpatialIndexOptions {
43
+ /** Target number of cells (will be adjusted to form a grid). Default: 100 */
44
+ targetCells?: number;
45
+ /** Minimum cell size in degrees. Default: 0.001 (~111m) */
46
+ minCellSize?: number;
47
+ }
48
+ /**
49
+ * Build a spatial index from a GeoJSON feature collection
50
+ *
51
+ * Uses a grid-based approach where each cell contains features
52
+ * whose bounding box intersects that cell. Features spanning
53
+ * multiple cells are stored in each intersecting cell.
54
+ *
55
+ * @example
56
+ * ```ts
57
+ * const index = buildSpatialIndex(lotsData);
58
+ * console.log(`Indexed ${index.featureCount} features in ${index.cols}x${index.rows} grid`);
59
+ * ```
60
+ */
61
+ declare function buildSpatialIndex(geojson: GeoJSONFeatureCollection, options?: BuildSpatialIndexOptions): SpatialIndex;
62
+ /**
63
+ * Options for querying the spatial index
64
+ */
65
+ interface QuerySpatialIndexOptions {
66
+ /** Buffer to add around viewport in degrees. Default: 0.01 (~1.1km) */
67
+ buffer?: number;
68
+ /** Buffer as a fraction of viewport size. Overrides buffer if set. */
69
+ bufferFraction?: number;
70
+ }
71
+ /**
72
+ * Query the spatial index for features within the given bounds
73
+ *
74
+ * @example
75
+ * ```ts
76
+ * const visibleFeatures = queryByBounds(index, viewport.bounds, {
77
+ * bufferFraction: 0.2, // 20% buffer for smooth scrolling
78
+ * });
79
+ * ```
80
+ */
81
+ declare function queryByBounds(index: SpatialIndex, queryBounds: BoundsArray | MapBounds, options?: QuerySpatialIndexOptions): GeoJSONFeatureCollection;
82
+ /**
83
+ * Statistics about a spatial index query
84
+ */
85
+ interface QueryStats {
86
+ /** Total features in index */
87
+ totalFeatures: number;
88
+ /** Features returned by query */
89
+ visibleFeatures: number;
90
+ /** Percentage of features culled */
91
+ culledPercent: number;
92
+ /** Number of grid cells checked */
93
+ cellsChecked: number;
94
+ /** Total grid cells */
95
+ totalCells: number;
96
+ }
97
+ /**
98
+ * Query with statistics for debugging/monitoring
99
+ */
100
+ declare function queryByBoundsWithStats(index: SpatialIndex, queryBounds: BoundsArray | MapBounds, options?: QuerySpatialIndexOptions): {
101
+ data: GeoJSONFeatureCollection;
102
+ stats: QueryStats;
103
+ };
104
+ /**
105
+ * Create a debounced viewport query function
106
+ *
107
+ * @example
108
+ * ```ts
109
+ * const index = buildSpatialIndex(lotsData);
110
+ * const queryViewport = createViewportQuery(index, {
111
+ * debounceMs: 100,
112
+ * bufferFraction: 0.2,
113
+ * onUpdate: (data, stats) => {
114
+ * setVisibleData(data);
115
+ * console.log(`Showing ${stats.visibleFeatures}/${stats.totalFeatures} (${stats.culledPercent.toFixed(1)}% culled)`);
116
+ * },
117
+ * });
118
+ *
119
+ * // Call on every map move
120
+ * map.on('move', () => queryViewport(map.getBounds()));
121
+ * ```
122
+ */
123
+ declare function createViewportQuery(index: SpatialIndex, options: {
124
+ /** Debounce delay in milliseconds. Default: 50 */
125
+ debounceMs?: number;
126
+ /** Buffer fraction of viewport. Default: 0.2 */
127
+ bufferFraction?: number;
128
+ /** Callback when data is updated */
129
+ onUpdate: (data: GeoJSONFeatureCollection, stats: QueryStats) => void;
130
+ }): (bounds: BoundsArray | MapBounds) => void;
131
+ /**
132
+ * Options for viewport-managed data
133
+ */
134
+ interface ViewportManagedDataOptions {
135
+ /** Buffer fraction of viewport (0-1). Default: 0.25 */
136
+ bufferFraction?: number;
137
+ /** Minimum movement (fraction of viewport) before updating. Default: 0.1 */
138
+ minMovement?: number;
139
+ /** Debounce delay in ms. Default: 50 */
140
+ debounceMs?: number;
141
+ /** Enable predictive pan loading. Default: false */
142
+ predictivePan?: boolean;
143
+ /** Multiplier for predictive buffer in pan direction. Default: 2.0 */
144
+ predictiveMultiplier?: number;
145
+ /** Number of samples to track for velocity calculation. Default: 5 */
146
+ velocitySamples?: number;
147
+ }
148
+ /**
149
+ * Result of computing viewport-managed data
150
+ */
151
+ interface ViewportManagedResult {
152
+ /** Filtered data for current viewport */
153
+ data: GeoJSONFeatureCollection;
154
+ /** Statistics about the query */
155
+ stats: QueryStats;
156
+ /** Whether the data was actually updated */
157
+ updated: boolean;
158
+ }
159
+ /**
160
+ * Create a viewport-managed data controller
161
+ *
162
+ * This manages viewport-based culling with smart update detection
163
+ * to avoid unnecessary data churn.
164
+ *
165
+ * @example
166
+ * ```tsx
167
+ * const controller = createViewportManager(index);
168
+ *
169
+ * function handleMove(e) {
170
+ * const result = controller.update(e.bounds);
171
+ * if (result.updated) {
172
+ * setData(result.data);
173
+ * console.log(`${result.stats.culledPercent.toFixed(1)}% culled`);
174
+ * }
175
+ * }
176
+ * ```
177
+ */
178
+ declare function createViewportManager(index: SpatialIndex, options?: ViewportManagedDataOptions): {
179
+ update: (bounds: BoundsArray | MapBounds) => ViewportManagedResult;
180
+ reset: () => void;
181
+ getStats: () => QueryStats | null;
182
+ };
183
+ /**
184
+ * Options for idle-aware viewport management
185
+ */
186
+ interface IdleViewportManagerOptions {
187
+ /** Buffer fraction of viewport (0-1). Default: 0.25 */
188
+ bufferFraction?: number;
189
+ /** Minimum movement (fraction of viewport) before updating. Default: 0.1 */
190
+ minMovement?: number;
191
+ /** Maximum updates per second. Default: 12 */
192
+ maxUpdatesPerSecond?: number;
193
+ /** Delay before idle callback fires (ms). Default: 100 */
194
+ idleDelay?: number;
195
+ /** Enable predictive pan loading. Default: true */
196
+ predictivePan?: boolean;
197
+ /** Multiplier for predictive buffer in pan direction. Default: 2.0 */
198
+ predictiveMultiplier?: number;
199
+ /** Number of samples to track for velocity calculation. Default: 5 */
200
+ velocitySamples?: number;
201
+ /**
202
+ * Maximum cumulative pan distance (as fraction of viewport) before forcing update.
203
+ * This is like Minecraft chunk loading - when you've panned far enough from the
204
+ * last data update, force a refresh regardless of whether you've stopped.
205
+ * Default: 0.5 (50% of viewport - at bufferFraction 0.25, this leaves 50% buffer remaining)
206
+ */
207
+ maxPanDistance?: number;
208
+ }
209
+ /**
210
+ * Result from idle viewport manager with velocity info
211
+ */
212
+ interface IdleViewportResult extends ViewportManagedResult {
213
+ /** Current pan velocity */
214
+ velocity?: PanVelocity;
215
+ /** Whether update was triggered by chunk-loading (exceeding maxPanDistance) */
216
+ chunkLoadTriggered?: boolean;
217
+ }
218
+ /**
219
+ * Create an idle-aware viewport manager that defers updates to browser idle time
220
+ *
221
+ * This manager prevents choppy panning by:
222
+ * 1. Deferring spatial queries to `requestIdleCallback`
223
+ * 2. Throttling to max N updates per second
224
+ * 3. Skipping updates during active panning
225
+ * 4. Providing `forceUpdate()` for immediate updates on `moveend`
226
+ *
227
+ * @example
228
+ * ```tsx
229
+ * const idleManager = createIdleViewportManager(rtreeIndex, {
230
+ * bufferFraction: 0.25,
231
+ * maxUpdatesPerSecond: 12,
232
+ * idleDelay: 100,
233
+ * });
234
+ *
235
+ * // Register callback for when data is ready
236
+ * idleManager.onDataReady((result) => {
237
+ * if (result.updated) {
238
+ * setVisibleLotsData(result.data);
239
+ * setCullingStats(result.stats);
240
+ * }
241
+ * });
242
+ *
243
+ * // Queue updates during pan (non-blocking)
244
+ * function handleMove(e) {
245
+ * idleManager.queueUpdate([e.bounds.southwest, e.bounds.northeast]);
246
+ * }
247
+ *
248
+ * // Force final update when pan ends
249
+ * function handleMoveEnd(e) {
250
+ * const result = idleManager.forceUpdate([e.bounds.southwest, e.bounds.northeast]);
251
+ * setVisibleLotsData(result.data);
252
+ * }
253
+ *
254
+ * <Map onMove={handleMove} onMoveEnd={handleMoveEnd} />
255
+ * ```
256
+ */
257
+ declare function createIdleViewportManager(index: RTreeIndex | SpatialIndex, options?: IdleViewportManagerOptions): {
258
+ /**
259
+ * Queue an update to run during browser idle time.
260
+ * @param bounds - Current viewport bounds
261
+ * @param trackOnly - If true, only tracks bounds for chunk-loading detection but doesn't
262
+ * schedule normal updates. Use this in "smooth pan" mode where you
263
+ * want updates only on moveend, but still want chunk-loading when
264
+ * the user pans far enough. Default: false
265
+ */
266
+ queueUpdate: (bounds: BoundsArray | MapBounds, trackOnly?: boolean) => void;
267
+ /** Force an immediate update (use on moveend) */
268
+ forceUpdate: (bounds: BoundsArray | MapBounds) => IdleViewportResult;
269
+ /** Register callback for when async data is ready */
270
+ onDataReady: (callback: (result: IdleViewportResult) => void) => void;
271
+ /** Reset internal state */
272
+ reset: () => void;
273
+ /** Get current stats */
274
+ getStats: () => QueryStats | null;
275
+ /** Get current velocity */
276
+ getVelocity: () => PanVelocity;
277
+ /** Get cumulative distance from last data fetch as fraction of viewport */
278
+ getCumulativeDistance: (bounds: BoundsArray | MapBounds) => number;
279
+ };
280
+ /**
281
+ * Pan velocity vector
282
+ */
283
+ interface PanVelocity {
284
+ /** Velocity in lng per second */
285
+ lngPerSec: number;
286
+ /** Velocity in lat per second */
287
+ latPerSec: number;
288
+ /** Magnitude of velocity */
289
+ magnitude: number;
290
+ }
291
+ /**
292
+ * Extended result with velocity info
293
+ */
294
+ interface PredictiveViewportResult extends ViewportManagedResult {
295
+ /** Current pan velocity */
296
+ velocity?: PanVelocity;
297
+ }
298
+ /**
299
+ * Create a viewport manager with predictive pan loading
300
+ *
301
+ * Tracks pan velocity and expands buffer asymmetrically in the direction
302
+ * of movement to eliminate pop-in during fast panning.
303
+ *
304
+ * @example
305
+ * ```tsx
306
+ * const index = buildRTreeIndex(lotsData);
307
+ * const controller = createPredictiveViewportManager(index, {
308
+ * bufferFraction: 0.25,
309
+ * predictiveMultiplier: 2.0, // 2x buffer in pan direction
310
+ * });
311
+ *
312
+ * function handleMove(e) {
313
+ * const result = controller.update(e.bounds);
314
+ * if (result.updated) {
315
+ * setData(result.data);
316
+ * console.log(`Velocity: ${result.velocity?.magnitude.toFixed(6)} deg/s`);
317
+ * }
318
+ * }
319
+ * ```
320
+ */
321
+ declare function createPredictiveViewportManager(index: RTreeIndex | SpatialIndex, options?: ViewportManagedDataOptions): {
322
+ update: (bounds: BoundsArray | MapBounds) => PredictiveViewportResult;
323
+ reset: () => void;
324
+ getStats: () => QueryStats | null;
325
+ getVelocity: () => PanVelocity;
326
+ };
327
+
328
+ /**
329
+ * A feature bounding box item for R-tree indexing
330
+ */
331
+ interface RTreeItem {
332
+ minX: number;
333
+ minY: number;
334
+ maxX: number;
335
+ maxY: number;
336
+ feature: GeoJSONFeature;
337
+ }
338
+ /**
339
+ * R-tree based spatial index using RBush
340
+ * Better performance than grid for clustered/non-uniform data
341
+ */
342
+ interface RTreeIndex {
343
+ /** The underlying RBush instance */
344
+ tree: RBush<RTreeItem>;
345
+ /** Bounds of all indexed data */
346
+ bounds: BoundsArray;
347
+ /** Total number of features indexed */
348
+ featureCount: number;
349
+ }
350
+ /**
351
+ * Options for building an R-tree index
352
+ */
353
+ interface BuildRTreeIndexOptions {
354
+ /** Maximum entries per node (default: 9) - higher = faster build, slower query */
355
+ maxEntries?: number;
356
+ }
357
+ /**
358
+ * Build an R-tree spatial index from a GeoJSON feature collection
359
+ *
360
+ * R-tree is better than grid index for:
361
+ * - Clustered data (lots grouped by community/filing)
362
+ * - Non-uniformly distributed features
363
+ * - Datasets with varying feature sizes
364
+ *
365
+ * @example
366
+ * ```ts
367
+ * const index = buildRTreeIndex(lotsData);
368
+ * console.log(`Indexed ${index.featureCount} features`);
369
+ *
370
+ * // Query by viewport
371
+ * const visible = queryRTreeByBounds(index, viewport.bounds, { bufferFraction: 0.2 });
372
+ * ```
373
+ */
374
+ declare function buildRTreeIndex(geojson: GeoJSONFeatureCollection, options?: BuildRTreeIndexOptions): RTreeIndex;
375
+ /**
376
+ * Query the R-tree index for features within the given bounds
377
+ *
378
+ * @example
379
+ * ```ts
380
+ * const visibleFeatures = queryRTreeByBounds(index, viewport.bounds, {
381
+ * bufferFraction: 0.2, // 20% buffer for smooth scrolling
382
+ * });
383
+ * ```
384
+ */
385
+ declare function queryRTreeByBounds(index: RTreeIndex, queryBounds: BoundsArray | MapBounds, options?: QuerySpatialIndexOptions): GeoJSONFeatureCollection;
386
+ /**
387
+ * Query R-tree with statistics for debugging/monitoring
388
+ */
389
+ declare function queryRTreeByBoundsWithStats(index: RTreeIndex, queryBounds: BoundsArray | MapBounds, options?: QuerySpatialIndexOptions): {
390
+ data: GeoJSONFeatureCollection;
391
+ stats: RTreeQueryStats;
392
+ };
393
+ /**
394
+ * Statistics about an R-tree spatial index query
395
+ */
396
+ interface RTreeQueryStats {
397
+ /** Total features in index */
398
+ totalFeatures: number;
399
+ /** Features returned by query */
400
+ visibleFeatures: number;
401
+ /** Percentage of features culled */
402
+ culledPercent: number;
403
+ /** Query execution time in milliseconds */
404
+ queryTimeMs: number;
405
+ }
406
+ /**
407
+ * Create a viewport manager using R-tree index
408
+ *
409
+ * This provides the same interface as createViewportManager but uses
410
+ * R-tree for better performance with clustered data.
411
+ *
412
+ * @example
413
+ * ```tsx
414
+ * const index = buildRTreeIndex(lotsData);
415
+ * const controller = createRTreeViewportManager(index);
416
+ *
417
+ * function handleMove(e) {
418
+ * const result = controller.update(e.bounds);
419
+ * if (result.updated) {
420
+ * setData(result.data);
421
+ * }
422
+ * }
423
+ * ```
424
+ */
425
+ declare function createRTreeViewportManager(index: RTreeIndex, options?: ViewportManagedDataOptions): {
426
+ update: (bounds: BoundsArray | MapBounds) => ViewportManagedResult & {
427
+ queryTimeMs?: number;
428
+ };
429
+ reset: () => void;
430
+ getStats: () => (QueryStats & {
431
+ queryTimeMs?: number;
432
+ }) | null;
433
+ };
434
+
435
+ export { type BuildSpatialIndexOptions as B, type IdleViewportManagerOptions as I, type PanVelocity as P, type QueryStats as Q, type RTreeQueryStats as R, type SpatialIndex as S, type ViewportManagedDataOptions as V, queryByBoundsWithStats as a, buildSpatialIndex as b, createViewportQuery as c, createViewportManager as d, buildRTreeIndex as e, queryRTreeByBounds as f, queryRTreeByBoundsWithStats as g, createRTreeViewportManager as h, createPredictiveViewportManager as i, createIdleViewportManager as j, type QuerySpatialIndexOptions as k, type ViewportManagedResult as l, type RTreeItem as m, type RTreeIndex as n, type BuildRTreeIndexOptions as o, type PredictiveViewportResult as p, queryByBounds as q, type IdleViewportResult as r };