geontfy 0.1.0 → 0.2.0
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.
- package/README.md +88 -2
- package/dist/index.cjs +82 -0
- package/dist/index.d.cts +28 -2
- package/dist/index.d.ts +28 -2
- package/dist/index.js +80 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,7 +1,15 @@
|
|
|
1
1
|
# geontfy
|
|
2
2
|
|
|
3
|
+
[](https://geontfy.web.app)
|
|
4
|
+
|
|
3
5
|
OpenStreetMap Geohash Proximity PubSub Utility
|
|
4
6
|
|
|
7
|
+
[Open the live map app](https://geontfy.web.app) · [npm package](https://www.npmjs.com/package/geontfy)
|
|
8
|
+
|
|
9
|
+
The app supports location/topic subscriptions and radius-targeted publishing through
|
|
10
|
+
Firebase Cloud Messaging, with a Cloudflare Worker backend. General is an ordinary
|
|
11
|
+
opt-in topic. See [web](web/README.md) and [backend](worker/README.md) setup details.
|
|
12
|
+
|
|
5
13
|
Turn geographic regions into exact-match pub/sub topics. Plan subscriptions and
|
|
6
14
|
publications, filter spatial false positives, and suppress repeated event IDs.
|
|
7
15
|
TypeScript, ESM and CommonJS; Node 18+; no runtime dependencies. The core also
|
|
@@ -11,6 +19,85 @@ This package calculates routing. Your application owns the broker, credentials,
|
|
|
11
19
|
subscriber identity, notification UI, persistence, and delivery policy. No network
|
|
12
20
|
requests happen unless you explicitly use a boundary provider or transport.
|
|
13
21
|
|
|
22
|
+
## Bounded circle covering in the web app
|
|
23
|
+
|
|
24
|
+
The deployed app uses a bounded-spill circle cover. It guarantees geometric coverage of
|
|
25
|
+
an entire positive-radius circle while allowing at most **20% excess spherical area**.
|
|
26
|
+
Every retained cell must also fit within **1.25 × the requested radius**. These are
|
|
27
|
+
geometric limits, not delivery guarantees or probabilities about subscriber locations.
|
|
28
|
+
Exact-radius filtering still happens before an alert is displayed.
|
|
29
|
+
|
|
30
|
+
The algorithm starts with a conservative cover at precision 6, refining up to precision 9
|
|
31
|
+
until both limits are satisfied. It then considers ancestor replacements, choosing the
|
|
32
|
+
least added area per topic saved. A replacement removes every covered descendant, so
|
|
33
|
+
the result stays prefix-free. Area is computed from spherical latitude/longitude cell
|
|
34
|
+
bounds; the circle uses spherical-cap area. Distance containment uses the existing
|
|
35
|
+
spherical distance-extrema classifier, not corner sampling. Ties are resolved by hash
|
|
36
|
+
order so browser and backend generate identical topics.
|
|
37
|
+
|
|
38
|
+
This greedy merge strategy reduces topic count; it does not guarantee a globally
|
|
39
|
+
minimum cover. It never relaxes a bound to fit the final 512-cell budget. Infeasible
|
|
40
|
+
precision, traversal, area, distance, or cell budgets produce an explicit error.
|
|
41
|
+
Very small radii and near-pole covers can exhaust those limits. The web app permits
|
|
42
|
+
100 m–10 km. The bounded API requires a positive radius; the existing unbounded
|
|
43
|
+
`createGeontfy` API retains its zero-radius and polygon behavior.
|
|
44
|
+
|
|
45
|
+
Measured at 12.9716° N, 77.5946° E with both default limits:
|
|
46
|
+
|
|
47
|
+
| Radius | Starting cover | Final cells | Excess area |
|
|
48
|
+
| --- | ---: | ---: | ---: |
|
|
49
|
+
| 100 m | 418 at precision 9 | 144 | 15.3% |
|
|
50
|
+
| 1.5 km | 170 at precision 7 | 113 | 16.0% |
|
|
51
|
+
| 5 km | 738 at precision 7 | 106 | 19.7% |
|
|
52
|
+
| 10 km | 234 at precision 6 | 100 | 16.4% |
|
|
53
|
+
|
|
54
|
+
Twenty percent excess area would mean at most about 16.7% outside candidates only
|
|
55
|
+
under a uniform distribution of subscribers. Actual traffic depends on subscriber
|
|
56
|
+
density; the area budget cannot guarantee a recipient false-positive rate.
|
|
57
|
+
|
|
58
|
+
The app uses **precision-9 routing topics**, with ten ancestor subscriptions per
|
|
59
|
+
location/topic, even when the selected cover uses coarser cells. All participants must
|
|
60
|
+
agree on that routing precision. Existing app users see a **Retry sync** notice:
|
|
61
|
+
replacement subscriptions are added before legacy precision-6 topics are removed.
|
|
62
|
+
Until refreshed, legacy subscriptions do not receive precision-9 publications.
|
|
63
|
+
Topic management and publishing run in batches of at most 32 upstream operations.
|
|
64
|
+
Partial acknowledgements are persisted; publishing retries retain the event ID.
|
|
65
|
+
Old precision-6 pending publications are not automatically replayed.
|
|
66
|
+
|
|
67
|
+
The app and Worker still install the published `geontfy@0.1.0` geometry, routing, and
|
|
68
|
+
FCM utilities. They share the new implementation in `shared/coverage.js`, injecting
|
|
69
|
+
those published utilities. The library source also exports typed wrappers:
|
|
70
|
+
|
|
71
|
+
```ts
|
|
72
|
+
import { createBoundedGeontfy, coverBoundedCircle, circle } from 'geontfy';
|
|
73
|
+
|
|
74
|
+
const geo = createBoundedGeontfy({
|
|
75
|
+
precision: 9,
|
|
76
|
+
maxSpillRatio: 0.2,
|
|
77
|
+
maxOvershootRatio: 0.25,
|
|
78
|
+
maxCells: 512,
|
|
79
|
+
});
|
|
80
|
+
const subscription = geo.subscribe('roads', { lat: 12.9716, lon: 77.5946 });
|
|
81
|
+
const publication = geo.publish({
|
|
82
|
+
id: 'roadworks-001',
|
|
83
|
+
topicId: 'roads',
|
|
84
|
+
position: { lat: 12.9716, lon: 77.5946 },
|
|
85
|
+
radiusMeters: 1500,
|
|
86
|
+
data: { title: 'Roadworks nearby' },
|
|
87
|
+
});
|
|
88
|
+
console.log(publication.coverage.spillRatio);
|
|
89
|
+
const cover = coverBoundedCircle(circle({ lat: 12.9716, lon: 77.5946 }, 1500));
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
These new exports are included in **geontfy 0.2.0**. The original `createGeontfy`
|
|
93
|
+
API remains unchanged; bounded covering is opt-in through the new exports.
|
|
94
|
+
The bounded wrapper currently uses the default `geontfy` namespace and publisher-region
|
|
95
|
+
mode. The lower-level cover function returns cells and area metrics without broker coupling.
|
|
96
|
+
|
|
97
|
+
The Layers menu independently controls the selected area, cover cells, nearby
|
|
98
|
+
precision-6 reference grid, and routing details. The map uses unfiltered OpenStreetMap
|
|
99
|
+
tiles; layer visibility does not change subscriptions or publishing.
|
|
100
|
+
|
|
14
101
|
## Publisher chooses the radius
|
|
15
102
|
|
|
16
103
|
```ts
|
|
@@ -295,6 +382,5 @@ lookups, FCM serialization, and packaged ESM/CommonJS entry points.
|
|
|
295
382
|
No live FCM or OSM calls are made by the tests.
|
|
296
383
|
|
|
297
384
|
The existing publish workflow uses npm trusted publishing and requires npm-side
|
|
298
|
-
configuration after the first manual publication.
|
|
299
|
-
as part of implementation. All publish commands should explicitly target
|
|
385
|
+
configuration after the first manual publication. Version 0.2.0 adds the bounded-cover APIs. All publish commands should explicitly target
|
|
300
386
|
`https://registry.npmjs.org`; `publishConfig.registry` also selects that registry.
|
package/dist/index.cjs
CHANGED
|
@@ -27,7 +27,9 @@ __export(index_exports, {
|
|
|
27
27
|
circle: () => circle,
|
|
28
28
|
classifyCell: () => classifyCell,
|
|
29
29
|
containsPosition: () => containsPosition,
|
|
30
|
+
coverBoundedCircle: () => coverBoundedCircle2,
|
|
30
31
|
coverRegion: () => coverRegion,
|
|
32
|
+
createBoundedGeontfy: () => createBoundedGeontfy2,
|
|
31
33
|
createFcmTransport: () => createFcmTransport,
|
|
32
34
|
createGeontfy: () => createGeontfy,
|
|
33
35
|
createMemoryDedupe: () => createMemoryDedupe,
|
|
@@ -571,6 +573,84 @@ function createNominatimProvider(options) {
|
|
|
571
573
|
}
|
|
572
574
|
};
|
|
573
575
|
}
|
|
576
|
+
|
|
577
|
+
// shared/coverage.js
|
|
578
|
+
var R = 63710088e-1;
|
|
579
|
+
var RAD2 = Math.PI / 180;
|
|
580
|
+
function coverBoundedCircle(core2, region, options = {}) {
|
|
581
|
+
const precision = options.precision ?? 9;
|
|
582
|
+
const maxSpillRatio = options.maxSpillRatio ?? 0.2;
|
|
583
|
+
const maxOvershootRatio = options.maxOvershootRatio ?? 0.25;
|
|
584
|
+
const maxCells = options.maxCells ?? 512;
|
|
585
|
+
core2.circle(region.center, region.radiusMeters);
|
|
586
|
+
if (!Number.isInteger(precision) || precision < 1 || precision > 12 || !Number.isInteger(maxCells) || maxCells < 1 || !Number.isFinite(maxSpillRatio) || maxSpillRatio < 0 || maxSpillRatio > 1 || !Number.isFinite(maxOvershootRatio) || maxOvershootRatio < 0 || maxOvershootRatio > 1) {
|
|
587
|
+
throw new RangeError("Invalid bounded cover options.");
|
|
588
|
+
}
|
|
589
|
+
if (region.radiusMeters === 0) throw new RangeError("Bounded circle covering requires a positive radius.");
|
|
590
|
+
const area = (hash) => {
|
|
591
|
+
const b = core2.decodeGeohash(hash);
|
|
592
|
+
return R * R * (b.east - b.west) * RAD2 * (Math.sin(b.north * RAD2) - Math.sin(b.south * RAD2));
|
|
593
|
+
};
|
|
594
|
+
const circleArea = 4 * Math.PI * R * R * Math.sin(region.radiusMeters / (2 * R)) ** 2;
|
|
595
|
+
const budget = circleArea * (1 + maxSpillRatio);
|
|
596
|
+
const expanded = core2.circle(region.center, Math.min(Math.PI * R, region.radiusMeters * (1 + maxOvershootRatio)));
|
|
597
|
+
const withinReach = (hash) => core2.classifyCell(expanded, core2.decodeGeohash(hash)) === "inside";
|
|
598
|
+
let cells = [], totalArea = 0, usedPrecision = 0;
|
|
599
|
+
for (let depth = Math.min(6, precision); depth <= precision; depth++) {
|
|
600
|
+
cells = core2.coverRegion(region, { precision: depth, maxCells: 2e4, maxVisited: 3e5 });
|
|
601
|
+
totalArea = cells.reduce((sum, hash) => sum + area(hash), 0);
|
|
602
|
+
usedPrecision = depth;
|
|
603
|
+
if (totalArea <= budget && cells.every(withinReach)) break;
|
|
604
|
+
if (depth === precision) throw new RangeError("Cannot meet spill and distance limits at this precision. Increase precision or radius.");
|
|
605
|
+
}
|
|
606
|
+
const baselineCells = cells.length;
|
|
607
|
+
while (true) {
|
|
608
|
+
const candidates = /* @__PURE__ */ new Map();
|
|
609
|
+
for (const hash of cells) {
|
|
610
|
+
const cellArea = area(hash);
|
|
611
|
+
for (let length = 0; length < hash.length; length++) {
|
|
612
|
+
const parent = hash.slice(0, length);
|
|
613
|
+
const entry = candidates.get(parent) ?? { count: 0, area: 0 };
|
|
614
|
+
entry.count++;
|
|
615
|
+
entry.area += cellArea;
|
|
616
|
+
candidates.set(parent, entry);
|
|
617
|
+
}
|
|
618
|
+
}
|
|
619
|
+
let best;
|
|
620
|
+
for (const [parent, children] of candidates) {
|
|
621
|
+
if (children.count < 2) continue;
|
|
622
|
+
const added = Math.max(0, area(parent) - children.area);
|
|
623
|
+
if (totalArea + added > budget || !withinReach(parent)) continue;
|
|
624
|
+
const score = added / (children.count - 1);
|
|
625
|
+
if (!best || score < best.score || score === best.score && parent < best.parent) best = { parent, added, score };
|
|
626
|
+
}
|
|
627
|
+
if (!best) break;
|
|
628
|
+
cells = cells.filter((hash) => !hash.startsWith(best.parent));
|
|
629
|
+
cells.push(best.parent);
|
|
630
|
+
totalArea += best.added;
|
|
631
|
+
}
|
|
632
|
+
if (cells.length > maxCells) throw new RangeError(`Bounded cover exceeds ${maxCells} cells without violating spill limits.`);
|
|
633
|
+
return { cells: cells.sort(), circleArea, coveredArea: totalArea, spillRatio: Math.max(0, totalArea / circleArea - 1), usedPrecision, baselineCells };
|
|
634
|
+
}
|
|
635
|
+
function createBoundedGeontfy(core2, options = {}) {
|
|
636
|
+
const precision = options.precision ?? 9;
|
|
637
|
+
const geo = core2.createGeontfy({ precision, maxTopics: options.maxCells ?? 512 });
|
|
638
|
+
return {
|
|
639
|
+
...geo,
|
|
640
|
+
publish(input) {
|
|
641
|
+
const point = geo.publish({ ...input, radiusMeters: 0 });
|
|
642
|
+
const radiusMeters = input.radiusMeters ?? 0;
|
|
643
|
+
const coverage = coverBoundedCircle(core2, core2.circle(point.event.position, radiusMeters), { ...options, precision });
|
|
644
|
+
const prefix = geo.subscribe(input.topicId, point.event.position).topics[0].replace(/world$/, "");
|
|
645
|
+
return { cells: coverage.cells, topics: coverage.cells.map((hash) => prefix + (hash || "world")), event: { ...point.event, radiusMeters }, coverage };
|
|
646
|
+
}
|
|
647
|
+
};
|
|
648
|
+
}
|
|
649
|
+
|
|
650
|
+
// src/bounded.ts
|
|
651
|
+
var core = { circle, classifyCell, decodeGeohash, coverRegion, createGeontfy };
|
|
652
|
+
var coverBoundedCircle2 = (region, options) => coverBoundedCircle(core, region, options);
|
|
653
|
+
var createBoundedGeontfy2 = (options) => createBoundedGeontfy(core, options);
|
|
574
654
|
// Annotate the CommonJS export names for ESM import in node:
|
|
575
655
|
0 && (module.exports = {
|
|
576
656
|
CoverLimitError,
|
|
@@ -580,7 +660,9 @@ function createNominatimProvider(options) {
|
|
|
580
660
|
circle,
|
|
581
661
|
classifyCell,
|
|
582
662
|
containsPosition,
|
|
663
|
+
coverBoundedCircle,
|
|
583
664
|
coverRegion,
|
|
665
|
+
createBoundedGeontfy,
|
|
584
666
|
createFcmTransport,
|
|
585
667
|
createGeontfy,
|
|
586
668
|
createMemoryDedupe,
|
package/dist/index.d.cts
CHANGED
|
@@ -173,5 +173,31 @@ interface NominatimOptions {
|
|
|
173
173
|
}
|
|
174
174
|
declare function createNominatimProvider(options: NominatimOptions): BoundaryProvider;
|
|
175
175
|
|
|
176
|
-
|
|
177
|
-
|
|
176
|
+
interface BoundedCoverOptions {
|
|
177
|
+
precision?: number;
|
|
178
|
+
maxSpillRatio?: number;
|
|
179
|
+
maxOvershootRatio?: number;
|
|
180
|
+
maxCells?: number;
|
|
181
|
+
}
|
|
182
|
+
interface CoverageMetrics {
|
|
183
|
+
cells: string[];
|
|
184
|
+
circleArea: number;
|
|
185
|
+
coveredArea: number;
|
|
186
|
+
spillRatio: number;
|
|
187
|
+
usedPrecision: number;
|
|
188
|
+
baselineCells: number;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
declare const coverBoundedCircle: (region: Circle, options?: BoundedCoverOptions) => CoverageMetrics;
|
|
192
|
+
declare const createBoundedGeontfy: (options?: BoundedCoverOptions) => Omit<{
|
|
193
|
+
subscribe: (topicId: string, target: Position | Region) => SubscriptionPlan;
|
|
194
|
+
publish: <T extends JsonValue>(input: PublishInput<T>) => PublicationPlan<T>;
|
|
195
|
+
lookupBoundary: (osmId: string) => Promise<Boundary>;
|
|
196
|
+
}, "publish"> & {
|
|
197
|
+
publish<T extends JsonValue>(input: PublishInput<T>): PublicationPlan<T> & {
|
|
198
|
+
coverage: CoverageMetrics;
|
|
199
|
+
};
|
|
200
|
+
};
|
|
201
|
+
|
|
202
|
+
export { CoverLimitError, EARTH_RADIUS_METERS, GEOHASH_ALPHABET, ancestors, circle, classifyCell, containsPosition, coverBoundedCircle, coverRegion, createBoundedGeontfy, createFcmTransport, createGeontfy, createMemoryDedupe, createNominatimProvider, createReceiver, decodeGeohash, dispatch, distanceMeters, encodeGeohash, fromFcmData, matchesSubscription, normalizePosition, positiveInteger, reconcileSubscriptions, subscriptionDiff, toFcmMessage, validateBoundary, validateEvent, validatePosition, validatePrecision, validateRadius, validateRegion };
|
|
203
|
+
export type { Boundary, BoundaryProvider, BoundedCoverOptions, Bounds, CellRelation, Circle, Coordinate, CoverOptions, CoverageMetrics, DedupeStore, DispatchResult, FcmDataMessage, GeoEvent, GeontfyOptions, JsonValue, MultiPolygon, NominatimOptions, Polygon, Position, PublicationPlan, PublishInput, PublishTransport, Region, Ring, RoutingMode, SubscriptionPlan, SubscriptionTransport, TopicFailure };
|
package/dist/index.d.ts
CHANGED
|
@@ -173,5 +173,31 @@ interface NominatimOptions {
|
|
|
173
173
|
}
|
|
174
174
|
declare function createNominatimProvider(options: NominatimOptions): BoundaryProvider;
|
|
175
175
|
|
|
176
|
-
|
|
177
|
-
|
|
176
|
+
interface BoundedCoverOptions {
|
|
177
|
+
precision?: number;
|
|
178
|
+
maxSpillRatio?: number;
|
|
179
|
+
maxOvershootRatio?: number;
|
|
180
|
+
maxCells?: number;
|
|
181
|
+
}
|
|
182
|
+
interface CoverageMetrics {
|
|
183
|
+
cells: string[];
|
|
184
|
+
circleArea: number;
|
|
185
|
+
coveredArea: number;
|
|
186
|
+
spillRatio: number;
|
|
187
|
+
usedPrecision: number;
|
|
188
|
+
baselineCells: number;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
declare const coverBoundedCircle: (region: Circle, options?: BoundedCoverOptions) => CoverageMetrics;
|
|
192
|
+
declare const createBoundedGeontfy: (options?: BoundedCoverOptions) => Omit<{
|
|
193
|
+
subscribe: (topicId: string, target: Position | Region) => SubscriptionPlan;
|
|
194
|
+
publish: <T extends JsonValue>(input: PublishInput<T>) => PublicationPlan<T>;
|
|
195
|
+
lookupBoundary: (osmId: string) => Promise<Boundary>;
|
|
196
|
+
}, "publish"> & {
|
|
197
|
+
publish<T extends JsonValue>(input: PublishInput<T>): PublicationPlan<T> & {
|
|
198
|
+
coverage: CoverageMetrics;
|
|
199
|
+
};
|
|
200
|
+
};
|
|
201
|
+
|
|
202
|
+
export { CoverLimitError, EARTH_RADIUS_METERS, GEOHASH_ALPHABET, ancestors, circle, classifyCell, containsPosition, coverBoundedCircle, coverRegion, createBoundedGeontfy, createFcmTransport, createGeontfy, createMemoryDedupe, createNominatimProvider, createReceiver, decodeGeohash, dispatch, distanceMeters, encodeGeohash, fromFcmData, matchesSubscription, normalizePosition, positiveInteger, reconcileSubscriptions, subscriptionDiff, toFcmMessage, validateBoundary, validateEvent, validatePosition, validatePrecision, validateRadius, validateRegion };
|
|
203
|
+
export type { Boundary, BoundaryProvider, BoundedCoverOptions, Bounds, CellRelation, Circle, Coordinate, CoverOptions, CoverageMetrics, DedupeStore, DispatchResult, FcmDataMessage, GeoEvent, GeontfyOptions, JsonValue, MultiPolygon, NominatimOptions, Polygon, Position, PublicationPlan, PublishInput, PublishTransport, Region, Ring, RoutingMode, SubscriptionPlan, SubscriptionTransport, TopicFailure };
|
package/dist/index.js
CHANGED
|
@@ -516,6 +516,84 @@ function createNominatimProvider(options) {
|
|
|
516
516
|
}
|
|
517
517
|
};
|
|
518
518
|
}
|
|
519
|
+
|
|
520
|
+
// shared/coverage.js
|
|
521
|
+
var R = 63710088e-1;
|
|
522
|
+
var RAD2 = Math.PI / 180;
|
|
523
|
+
function coverBoundedCircle(core2, region, options = {}) {
|
|
524
|
+
const precision = options.precision ?? 9;
|
|
525
|
+
const maxSpillRatio = options.maxSpillRatio ?? 0.2;
|
|
526
|
+
const maxOvershootRatio = options.maxOvershootRatio ?? 0.25;
|
|
527
|
+
const maxCells = options.maxCells ?? 512;
|
|
528
|
+
core2.circle(region.center, region.radiusMeters);
|
|
529
|
+
if (!Number.isInteger(precision) || precision < 1 || precision > 12 || !Number.isInteger(maxCells) || maxCells < 1 || !Number.isFinite(maxSpillRatio) || maxSpillRatio < 0 || maxSpillRatio > 1 || !Number.isFinite(maxOvershootRatio) || maxOvershootRatio < 0 || maxOvershootRatio > 1) {
|
|
530
|
+
throw new RangeError("Invalid bounded cover options.");
|
|
531
|
+
}
|
|
532
|
+
if (region.radiusMeters === 0) throw new RangeError("Bounded circle covering requires a positive radius.");
|
|
533
|
+
const area = (hash) => {
|
|
534
|
+
const b = core2.decodeGeohash(hash);
|
|
535
|
+
return R * R * (b.east - b.west) * RAD2 * (Math.sin(b.north * RAD2) - Math.sin(b.south * RAD2));
|
|
536
|
+
};
|
|
537
|
+
const circleArea = 4 * Math.PI * R * R * Math.sin(region.radiusMeters / (2 * R)) ** 2;
|
|
538
|
+
const budget = circleArea * (1 + maxSpillRatio);
|
|
539
|
+
const expanded = core2.circle(region.center, Math.min(Math.PI * R, region.radiusMeters * (1 + maxOvershootRatio)));
|
|
540
|
+
const withinReach = (hash) => core2.classifyCell(expanded, core2.decodeGeohash(hash)) === "inside";
|
|
541
|
+
let cells = [], totalArea = 0, usedPrecision = 0;
|
|
542
|
+
for (let depth = Math.min(6, precision); depth <= precision; depth++) {
|
|
543
|
+
cells = core2.coverRegion(region, { precision: depth, maxCells: 2e4, maxVisited: 3e5 });
|
|
544
|
+
totalArea = cells.reduce((sum, hash) => sum + area(hash), 0);
|
|
545
|
+
usedPrecision = depth;
|
|
546
|
+
if (totalArea <= budget && cells.every(withinReach)) break;
|
|
547
|
+
if (depth === precision) throw new RangeError("Cannot meet spill and distance limits at this precision. Increase precision or radius.");
|
|
548
|
+
}
|
|
549
|
+
const baselineCells = cells.length;
|
|
550
|
+
while (true) {
|
|
551
|
+
const candidates = /* @__PURE__ */ new Map();
|
|
552
|
+
for (const hash of cells) {
|
|
553
|
+
const cellArea = area(hash);
|
|
554
|
+
for (let length = 0; length < hash.length; length++) {
|
|
555
|
+
const parent = hash.slice(0, length);
|
|
556
|
+
const entry = candidates.get(parent) ?? { count: 0, area: 0 };
|
|
557
|
+
entry.count++;
|
|
558
|
+
entry.area += cellArea;
|
|
559
|
+
candidates.set(parent, entry);
|
|
560
|
+
}
|
|
561
|
+
}
|
|
562
|
+
let best;
|
|
563
|
+
for (const [parent, children] of candidates) {
|
|
564
|
+
if (children.count < 2) continue;
|
|
565
|
+
const added = Math.max(0, area(parent) - children.area);
|
|
566
|
+
if (totalArea + added > budget || !withinReach(parent)) continue;
|
|
567
|
+
const score = added / (children.count - 1);
|
|
568
|
+
if (!best || score < best.score || score === best.score && parent < best.parent) best = { parent, added, score };
|
|
569
|
+
}
|
|
570
|
+
if (!best) break;
|
|
571
|
+
cells = cells.filter((hash) => !hash.startsWith(best.parent));
|
|
572
|
+
cells.push(best.parent);
|
|
573
|
+
totalArea += best.added;
|
|
574
|
+
}
|
|
575
|
+
if (cells.length > maxCells) throw new RangeError(`Bounded cover exceeds ${maxCells} cells without violating spill limits.`);
|
|
576
|
+
return { cells: cells.sort(), circleArea, coveredArea: totalArea, spillRatio: Math.max(0, totalArea / circleArea - 1), usedPrecision, baselineCells };
|
|
577
|
+
}
|
|
578
|
+
function createBoundedGeontfy(core2, options = {}) {
|
|
579
|
+
const precision = options.precision ?? 9;
|
|
580
|
+
const geo = core2.createGeontfy({ precision, maxTopics: options.maxCells ?? 512 });
|
|
581
|
+
return {
|
|
582
|
+
...geo,
|
|
583
|
+
publish(input) {
|
|
584
|
+
const point = geo.publish({ ...input, radiusMeters: 0 });
|
|
585
|
+
const radiusMeters = input.radiusMeters ?? 0;
|
|
586
|
+
const coverage = coverBoundedCircle(core2, core2.circle(point.event.position, radiusMeters), { ...options, precision });
|
|
587
|
+
const prefix = geo.subscribe(input.topicId, point.event.position).topics[0].replace(/world$/, "");
|
|
588
|
+
return { cells: coverage.cells, topics: coverage.cells.map((hash) => prefix + (hash || "world")), event: { ...point.event, radiusMeters }, coverage };
|
|
589
|
+
}
|
|
590
|
+
};
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
// src/bounded.ts
|
|
594
|
+
var core = { circle, classifyCell, decodeGeohash, coverRegion, createGeontfy };
|
|
595
|
+
var coverBoundedCircle2 = (region, options) => coverBoundedCircle(core, region, options);
|
|
596
|
+
var createBoundedGeontfy2 = (options) => createBoundedGeontfy(core, options);
|
|
519
597
|
export {
|
|
520
598
|
CoverLimitError,
|
|
521
599
|
EARTH_RADIUS_METERS,
|
|
@@ -524,7 +602,9 @@ export {
|
|
|
524
602
|
circle,
|
|
525
603
|
classifyCell,
|
|
526
604
|
containsPosition,
|
|
605
|
+
coverBoundedCircle2 as coverBoundedCircle,
|
|
527
606
|
coverRegion,
|
|
607
|
+
createBoundedGeontfy2 as createBoundedGeontfy,
|
|
528
608
|
createFcmTransport,
|
|
529
609
|
createGeontfy,
|
|
530
610
|
createMemoryDedupe,
|