@trackunit/react-map 0.2.295 → 0.2.297
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/index.cjs.js +398 -29
- package/index.esm.js +399 -30
- package/package.json +12 -11
- package/src/layers/__testHelpers.fillTiling.d.ts +18 -0
- package/src/layers/useShapes/captureFillTilingDifferenceFailures.d.ts +11 -0
- package/src/layers/useShapes/clipper2PolygonDifference.d.ts +37 -0
- package/src/layers/useShapes/shapeFillTiling.d.ts +42 -2
- package/src/layers/useShapes/useShapeFillTiling.d.ts +1 -1
- package/translation11.cjs.js +8 -8
- package/translation11.esm.js +8 -8
- package/translation12.cjs.js +8 -8
- package/translation12.esm.js +8 -8
- package/translation13.cjs.js +8 -8
- package/translation13.esm.js +8 -8
- package/translation14.cjs.js +8 -8
- package/translation14.esm.js +8 -8
- package/translation15.cjs.js +8 -8
- package/translation15.esm.js +8 -8
- package/translation16.cjs.js +8 -8
- package/translation16.esm.js +8 -8
- package/translation17.cjs.js +8 -8
- package/translation17.esm.js +8 -8
- package/translation2.cjs.js +8 -8
- package/translation2.esm.js +8 -8
- package/translation3.cjs.js +8 -8
- package/translation3.esm.js +8 -8
- package/translation5.cjs.js +8 -8
- package/translation5.esm.js +8 -8
- package/translation6.cjs.js +8 -8
- package/translation6.esm.js +8 -8
- package/translation8.cjs.js +8 -8
- package/translation8.esm.js +8 -8
- package/translation9.cjs.js +8 -8
- package/translation9.esm.js +8 -8
package/index.cjs.js
CHANGED
|
@@ -9510,13 +9510,15 @@ const shapesUnderCursor = (position, features, options) => {
|
|
|
9510
9510
|
};
|
|
9511
9511
|
/**
|
|
9512
9512
|
* Connected components of features that share fill area. Bbox prefilter then
|
|
9513
|
-
* exact
|
|
9513
|
+
* exact overlap predicate, mirroring `countOverlappingShapes`.
|
|
9514
9514
|
*
|
|
9515
|
-
* `
|
|
9516
|
-
*
|
|
9517
|
-
*
|
|
9518
|
-
*
|
|
9519
|
-
*
|
|
9515
|
+
* `polygonalGeometriesOverlap` answers the yes/no question without computing the
|
|
9516
|
+
* intersection geometry; only degenerate pairs (touching edges, shared vertices)
|
|
9517
|
+
* fall back to `polygon-clipping`, which can throw `"Unable to complete output ring"`
|
|
9518
|
+
* for degenerate but schema-valid geometry (SAGA-743). A throw here is treated as
|
|
9519
|
+
* "no overlap" — the safe degradation for grouping — and recorded in
|
|
9520
|
+
* `intersectionFailures` so the hook can report it (mirrors how
|
|
9521
|
+
* `droppedInvalidFeatures` surfaces the sibling failure mode).
|
|
9520
9522
|
*/
|
|
9521
9523
|
const buildOverlapGroups = (records) => {
|
|
9522
9524
|
const parent = records.map((_, index) => index);
|
|
@@ -9546,7 +9548,7 @@ const buildOverlapGroups = (records) => {
|
|
|
9546
9548
|
continue;
|
|
9547
9549
|
let overlaps;
|
|
9548
9550
|
try {
|
|
9549
|
-
overlaps = geoJsonUtils.
|
|
9551
|
+
overlaps = geoJsonUtils.polygonalGeometriesOverlap(a.geometry, b.geometry);
|
|
9550
9552
|
}
|
|
9551
9553
|
catch (error) {
|
|
9552
9554
|
// polygon-clipping failed on this pair. Treat as no overlap (do not union)
|
|
@@ -9572,11 +9574,19 @@ const buildOverlapGroups = (records) => {
|
|
|
9572
9574
|
});
|
|
9573
9575
|
return { groups: [...groups.values()].filter(group => group.length >= 2), intersectionFailures };
|
|
9574
9576
|
};
|
|
9577
|
+
/**
|
|
9578
|
+
* A geometry can only be contained in another when its bbox sits inside the other's bbox.
|
|
9579
|
+
* `computeBbox` never emits a wrapped (west > east) box; an antimeridian-split MultiPolygon gets a
|
|
9580
|
+
* conservative full-width box, which only makes this pre-test pass more often, never less.
|
|
9581
|
+
*/
|
|
9582
|
+
const bboxMayBeContained = (inner, outer) => inner[0] >= outer[0] && inner[1] >= outer[1] && inner[2] <= outer[2] && inner[3] <= outer[3];
|
|
9575
9583
|
const buildContendedShapes = (group) => group.map(record => {
|
|
9576
9584
|
const containedIn = [];
|
|
9577
9585
|
for (const other of group) {
|
|
9578
9586
|
if (other.id === record.id)
|
|
9579
9587
|
continue;
|
|
9588
|
+
if (!bboxMayBeContained(record.bbox, other.bbox))
|
|
9589
|
+
continue;
|
|
9580
9590
|
// Pure predicate: geometry was validated once at the computeFillTiling
|
|
9581
9591
|
// boundary, so this O(n²) loop performs no safeParse per pair (SAGA-664).
|
|
9582
9592
|
if (geoJsonUtils.isFullyContainedInGeometry(record.geometry, other.geometry)) {
|
|
@@ -9594,16 +9604,42 @@ const groupKeyOf = (group) => group
|
|
|
9594
9604
|
.map(record => record.id)
|
|
9595
9605
|
.sort()
|
|
9596
9606
|
.join("|");
|
|
9607
|
+
/**
|
|
9608
|
+
* `subject` minus all `clips`, in Web Mercator. `polygon-clipping` can throw for degenerate but
|
|
9609
|
+
* schema-valid geometry (SAGA-743); when the batched call fails, the clips are subtracted one at a
|
|
9610
|
+
* time and only the ones that throw are skipped, so one degenerate neighbour costs the overlap with
|
|
9611
|
+
* that neighbour alone rather than every clip of the subject.
|
|
9612
|
+
*/
|
|
9613
|
+
const clipAgainstHigherRanked = (subject, clips, polygonDifference) => {
|
|
9614
|
+
let fallbacks = 0;
|
|
9615
|
+
let lastError;
|
|
9616
|
+
const onFallback = (error) => {
|
|
9617
|
+
fallbacks += 1;
|
|
9618
|
+
lastError = error;
|
|
9619
|
+
};
|
|
9620
|
+
const batched = polygonDifference(subject, clips.map(clip => clip.geometry), onFallback);
|
|
9621
|
+
if (fallbacks === 0)
|
|
9622
|
+
return { geometry: batched, failure: null };
|
|
9623
|
+
// The batch failed: subtract one clip at a time and skip only the clips that throw on their own.
|
|
9624
|
+
const skipped = [];
|
|
9625
|
+
let current = subject;
|
|
9626
|
+
for (const clip of clips) {
|
|
9627
|
+
if (current === null)
|
|
9628
|
+
break;
|
|
9629
|
+
const before = fallbacks;
|
|
9630
|
+
const next = polygonDifference(current, [clip.geometry], onFallback);
|
|
9631
|
+
if (fallbacks > before) {
|
|
9632
|
+
skipped.push(clip.id);
|
|
9633
|
+
continue;
|
|
9634
|
+
}
|
|
9635
|
+
current = next;
|
|
9636
|
+
}
|
|
9637
|
+
// Only a clip that was actually dropped is a failure; a batch that throws while every clip succeeds alone is not.
|
|
9638
|
+
return { geometry: current, failure: skipped.length > 0 ? { clipFeatureIds: skipped, error: lastError } : null };
|
|
9639
|
+
};
|
|
9597
9640
|
/** Promote `id` to the front of `order` (no-op if not present). */
|
|
9598
9641
|
const promoteToFront = (order, id) => order.includes(id) ? [id, ...order.filter(item => item !== id)] : order;
|
|
9599
|
-
|
|
9600
|
-
* Hover-promotion overlay for one overlap group: the winner renders at full
|
|
9601
|
-
* geometry (caller removes its fill override). Each peer is clipped against the
|
|
9602
|
-
* winner's full outline starting from its **resting** fill (when present), not
|
|
9603
|
-
* its full geometry — so peer-to-peer overlap ownership from resting tiling is
|
|
9604
|
-
* preserved and translucent fills do not compound under the winner.
|
|
9605
|
-
*/
|
|
9606
|
-
const computePromotionGroupFills = (members, winnerId, restingPeerFills = new Map(), layerGeodesic = undefined, featureStyles = undefined) => {
|
|
9642
|
+
const computePromotionGroupFills = ({ members, winnerId, restingPeerFills = new Map(), layerGeodesic, featureStyles, polygonDifference = geoJsonUtils.geoJsonPolygonDifference, onDifferenceFallback, }) => {
|
|
9607
9643
|
// Densify before peer-vs-winner clip so promotion clips follow great-circle
|
|
9608
9644
|
// arcs. Always defer to densifyGeodesicFeatures' per-feature resolution
|
|
9609
9645
|
// (geodesic override > layer default) rather than short-circuiting on
|
|
@@ -9644,14 +9680,16 @@ const computePromotionGroupFills = (members, winnerId, restingPeerFills = new Ma
|
|
|
9644
9680
|
subject = restingPolygonal;
|
|
9645
9681
|
}
|
|
9646
9682
|
const subjectMercator = geoJsonUtils.projectPolygonalToWebMercator(subject);
|
|
9647
|
-
const clippedMercator =
|
|
9683
|
+
const clippedMercator = polygonDifference(subjectMercator, [winnerMercator], error => {
|
|
9684
|
+
onDifferenceFallback?.(memberId, error);
|
|
9685
|
+
});
|
|
9648
9686
|
const clipped = clippedMercator === null ? null : geoJsonUtils.unprojectPolygonalFromWebMercator(clippedMercator);
|
|
9649
9687
|
result.set(memberId, clipped === null ? EMPTY_FILL : clipped);
|
|
9650
9688
|
}
|
|
9651
9689
|
return result;
|
|
9652
9690
|
};
|
|
9653
9691
|
const computeFillTiling = (input) => {
|
|
9654
|
-
const { features, viewportBounds, resolveStackOrder, selectedFeatureId, suppressedFeatureIds, layerGeodesic, featureStyles, } = input;
|
|
9692
|
+
const { features, viewportBounds, resolveStackOrder, selectedFeatureId, suppressedFeatureIds, layerGeodesic, featureStyles, polygonDifference = geoJsonUtils.geoJsonPolygonDifference, } = input;
|
|
9655
9693
|
// Validation boundary (SAGA-664): validate each input feature's polygonal
|
|
9656
9694
|
// geometry once here — O(n) — dropping malformed geometry. The downstream O(n²)
|
|
9657
9695
|
// overlap loop then runs the pure predicates on trusted geometry without a
|
|
@@ -9708,12 +9746,23 @@ const computeFillTiling = (input) => {
|
|
|
9708
9746
|
const fillGeometries = new Map();
|
|
9709
9747
|
const featureToGroupKey = new Map();
|
|
9710
9748
|
const featureZIndex = new Map();
|
|
9749
|
+
const differenceFailures = [];
|
|
9711
9750
|
const { groups: overlapGroups, intersectionFailures } = buildOverlapGroups(records);
|
|
9712
9751
|
// Failures are returned for the hook to warn about and report (SAGA-743).
|
|
9713
9752
|
for (const group of overlapGroups) {
|
|
9714
9753
|
const contended = buildContendedShapes(group);
|
|
9715
9754
|
const recordById = new Map(group.map(record => [record.id, record]));
|
|
9716
9755
|
const key = groupKeyOf(group);
|
|
9756
|
+
// Each member is projected at most once per recompute, however many subjects it clips.
|
|
9757
|
+
const mercatorById = new Map();
|
|
9758
|
+
const mercatorOf = (record) => {
|
|
9759
|
+
const cached = mercatorById.get(record.id);
|
|
9760
|
+
if (cached !== undefined)
|
|
9761
|
+
return cached;
|
|
9762
|
+
const projected = geoJsonUtils.projectPolygonalToWebMercator(record.geometry);
|
|
9763
|
+
mercatorById.set(record.id, projected);
|
|
9764
|
+
return projected;
|
|
9765
|
+
};
|
|
9717
9766
|
for (const record of group) {
|
|
9718
9767
|
featureToGroupKey.set(record.id, key);
|
|
9719
9768
|
}
|
|
@@ -9733,7 +9782,8 @@ const computeFillTiling = (input) => {
|
|
|
9733
9782
|
}
|
|
9734
9783
|
}
|
|
9735
9784
|
// Top feature (index 0) keeps its full fill. Each lower-ranked feature is
|
|
9736
|
-
// clipped against the union of
|
|
9785
|
+
// clipped against the union of the features ranked above it whose bbox touches
|
|
9786
|
+
// its own — a higher-ranked member elsewhere in the group cannot cut it.
|
|
9737
9787
|
for (let rank = 1; rank < order.length; rank++) {
|
|
9738
9788
|
const id = order[rank];
|
|
9739
9789
|
if (id === undefined)
|
|
@@ -9745,17 +9795,22 @@ const computeFillTiling = (input) => {
|
|
|
9745
9795
|
for (let above = 0; above < rank; above++) {
|
|
9746
9796
|
const aboveId = order[above];
|
|
9747
9797
|
const aboveRecord = aboveId !== undefined ? recordById.get(aboveId) : undefined;
|
|
9748
|
-
if (aboveRecord !== undefined)
|
|
9749
|
-
higherRanked.push(aboveRecord
|
|
9798
|
+
if (aboveRecord !== undefined && bboxesIntersect$1(record.bbox, aboveRecord.bbox)) {
|
|
9799
|
+
higherRanked.push(aboveRecord);
|
|
9800
|
+
}
|
|
9750
9801
|
}
|
|
9751
9802
|
// Clip in Web Mercator, not planar lng/lat. Google Maps and Mapbox both render
|
|
9752
9803
|
// polygon edges as straight lines in Web Mercator, so a clip computed in lng/lat
|
|
9753
9804
|
// produces boundary vertices that drift off the rendered edge of the covering
|
|
9754
9805
|
// shape — visibly so on long, diagonal, high-latitude-span edges. Projecting
|
|
9755
9806
|
// before the difference lands every cut exactly on the rendered edge (ADR-0021).
|
|
9756
|
-
|
|
9757
|
-
|
|
9758
|
-
|
|
9807
|
+
// Nothing above touches this member: no override, the adapter renders the full geometry.
|
|
9808
|
+
if (higherRanked.length === 0)
|
|
9809
|
+
continue;
|
|
9810
|
+
const { geometry: clippedMercator, failure } = clipAgainstHigherRanked(mercatorOf(record), higherRanked.map(above => ({ id: above.id, geometry: mercatorOf(above) })), polygonDifference);
|
|
9811
|
+
if (failure !== null) {
|
|
9812
|
+
differenceFailures.push({ featureId: id, clipFeatureIds: failure.clipFeatureIds, error: failure.error });
|
|
9813
|
+
}
|
|
9759
9814
|
const clipped = clippedMercator === null ? null : geoJsonUtils.unprojectPolygonalFromWebMercator(clippedMercator);
|
|
9760
9815
|
fillGeometries.set(id, clipped === null ? EMPTY_FILL : clipped);
|
|
9761
9816
|
}
|
|
@@ -9785,6 +9840,7 @@ const computeFillTiling = (input) => {
|
|
|
9785
9840
|
featureZIndex,
|
|
9786
9841
|
droppedInvalidFeatures,
|
|
9787
9842
|
intersectionFailures,
|
|
9843
|
+
differenceFailures,
|
|
9788
9844
|
};
|
|
9789
9845
|
};
|
|
9790
9846
|
|
|
@@ -11404,11 +11460,12 @@ const buildOverlappingShapesCountMap = (features, featureBboxes, viewportBounds)
|
|
|
11404
11460
|
if (isPolygonalA && isPolygonalB) {
|
|
11405
11461
|
let overlaps;
|
|
11406
11462
|
try {
|
|
11407
|
-
overlaps = geoJsonUtils.
|
|
11463
|
+
overlaps = geoJsonUtils.polygonalGeometriesOverlap(geomA, geomB);
|
|
11408
11464
|
}
|
|
11409
11465
|
catch (error) {
|
|
11410
|
-
// polygon-clipping failed on this pair (SAGA-743)
|
|
11411
|
-
//
|
|
11466
|
+
// polygon-clipping failed on this pair (SAGA-743) while deciding a degenerate
|
|
11467
|
+
// (touching) overlap. Treat as no overlap so decoration counts degrade gracefully
|
|
11468
|
+
// instead of crashing the map.
|
|
11412
11469
|
intersectionFailures.push({
|
|
11413
11470
|
featureIdA: featureA.id !== undefined ? String(featureA.id) : `index:${i}`,
|
|
11414
11471
|
featureIdB: featureB.id !== undefined ? String(featureB.id) : `index:${j}`,
|
|
@@ -11962,6 +12019,219 @@ const captureDroppedInvalidFillTilingFeatures = (droppedInvalidFeatures, layerHa
|
|
|
11962
12019
|
}
|
|
11963
12020
|
};
|
|
11964
12021
|
|
|
12022
|
+
/**
|
|
12023
|
+
* Report to Sentry each feature whose clip fell back to an unclipped fill because
|
|
12024
|
+
* `polygon-clipping` threw (SAGA-743), at most once per handle, channel and feature per session.
|
|
12025
|
+
* Note: `shapeFillTiling.ts` emits `console.warn` separately on both channels.
|
|
12026
|
+
*/
|
|
12027
|
+
const captureFillTilingDifferenceFailures = (differenceFailures, layerHandleId, channel, sessionReportedDedupeKeys, report) => {
|
|
12028
|
+
for (const failure of differenceFailures) {
|
|
12029
|
+
const key = `${layerHandleId}:difference:${channel}:${failure.featureId}`;
|
|
12030
|
+
if (sessionReportedDedupeKeys.has(key))
|
|
12031
|
+
continue;
|
|
12032
|
+
sessionReportedDedupeKeys.add(key);
|
|
12033
|
+
report({
|
|
12034
|
+
layerHandleId,
|
|
12035
|
+
channel,
|
|
12036
|
+
featureId: failure.featureId,
|
|
12037
|
+
clipFeatureIds: [...failure.clipFeatureIds],
|
|
12038
|
+
message: failure.error instanceof Error ? failure.error.message : String(failure.error),
|
|
12039
|
+
});
|
|
12040
|
+
}
|
|
12041
|
+
};
|
|
12042
|
+
|
|
12043
|
+
/**
|
|
12044
|
+
* Clipper2 scales coordinates to integers by 10^precision. Fill tiling clips in Web Mercator
|
|
12045
|
+
* metres, so 3 keeps millimetres.
|
|
12046
|
+
*/
|
|
12047
|
+
const MERCATOR_PRECISION = 3;
|
|
12048
|
+
/** Metres added around the subject bbox before trimming clips, so rect edges stay clear of subject vertices. */
|
|
12049
|
+
const RECT_CLIP_MARGIN = 1;
|
|
12050
|
+
const polygonsOf = (geometry) => geometry.type === "Polygon" ? [geometry.coordinates] : geometry.coordinates;
|
|
12051
|
+
const signedArea = (ring) => {
|
|
12052
|
+
let sum = 0;
|
|
12053
|
+
for (let i = 0; i + 1 < ring.length; i++) {
|
|
12054
|
+
const current = ring[i];
|
|
12055
|
+
const next = ring[i + 1];
|
|
12056
|
+
if (current === undefined || next === undefined)
|
|
12057
|
+
continue;
|
|
12058
|
+
sum += current[0] * next[1] - next[0] * current[1];
|
|
12059
|
+
}
|
|
12060
|
+
return sum / 2;
|
|
12061
|
+
};
|
|
12062
|
+
/**
|
|
12063
|
+
* GeoJSON rings become Clipper2 paths with the closing vertex dropped. Exterior rings are forced
|
|
12064
|
+
* counter-clockwise and holes clockwise so that NonZero winding matches polygon-clipping for
|
|
12065
|
+
* well-nested, non-overlapping holes: holes subtract, and clips that overlap each other still union.
|
|
12066
|
+
*
|
|
12067
|
+
* Known divergence for invalid hole configurations that the schema does not reject: NonZero sums
|
|
12068
|
+
* windings, so where two holes of one polygon overlap (winding −1) the area counts as filled, and
|
|
12069
|
+
* the part of a hole that pokes outside its exterior (winding −1 as well) is painted. polygon-clipping
|
|
12070
|
+
* excludes any region inside an interior ring instead. See the spec for the pinned numbers.
|
|
12071
|
+
*/
|
|
12072
|
+
const appendRingsAsPaths = (clipper, paths, geometries) => {
|
|
12073
|
+
for (const geometry of geometries) {
|
|
12074
|
+
for (const rings of polygonsOf(geometry)) {
|
|
12075
|
+
rings.forEach((ring, ringIndex) => {
|
|
12076
|
+
const wantCounterClockwise = ringIndex === 0;
|
|
12077
|
+
const oriented = signedArea(ring) > 0 === wantCounterClockwise ? ring : [...ring].reverse();
|
|
12078
|
+
// Drop the closing vertex; Clipper2 paths are implicitly closed.
|
|
12079
|
+
const flat = [];
|
|
12080
|
+
for (const [x, y] of oriented.slice(0, -1))
|
|
12081
|
+
flat.push(x, y);
|
|
12082
|
+
const path = clipper.MakePathD(flat);
|
|
12083
|
+
paths.push_back(path);
|
|
12084
|
+
path.delete();
|
|
12085
|
+
});
|
|
12086
|
+
}
|
|
12087
|
+
}
|
|
12088
|
+
};
|
|
12089
|
+
/**
|
|
12090
|
+
* Closed GeoJSON ring from a tree node. `polygon()` hands out an owned copy whose `view()` is
|
|
12091
|
+
* x, y, z per vertex (Z build); it is released as soon as the coordinates are copied out.
|
|
12092
|
+
*/
|
|
12093
|
+
const ringOf = (node) => {
|
|
12094
|
+
const path = node.polygon();
|
|
12095
|
+
try {
|
|
12096
|
+
const xyz = path.view();
|
|
12097
|
+
const ring = [];
|
|
12098
|
+
for (let i = 0; i + 2 < xyz.length; i += 3) {
|
|
12099
|
+
ring.push([xyz[i] ?? 0, xyz[i + 1] ?? 0]);
|
|
12100
|
+
}
|
|
12101
|
+
const first = ring[0];
|
|
12102
|
+
if (first !== undefined)
|
|
12103
|
+
ring.push(first);
|
|
12104
|
+
return ring;
|
|
12105
|
+
}
|
|
12106
|
+
finally {
|
|
12107
|
+
path.delete();
|
|
12108
|
+
}
|
|
12109
|
+
};
|
|
12110
|
+
/**
|
|
12111
|
+
* Walk the PolyTree: each child of an outer is a hole, each child of a hole is an island.
|
|
12112
|
+
* `child()` returns non-owning references into the tree and must not be deleted.
|
|
12113
|
+
*/
|
|
12114
|
+
const collectPolygons = (node, into) => {
|
|
12115
|
+
const outerCount = node.count();
|
|
12116
|
+
for (let i = 0; i < outerCount; i++) {
|
|
12117
|
+
const outer = node.child(i);
|
|
12118
|
+
const rings = [ringOf(outer)];
|
|
12119
|
+
const holeCount = outer.count();
|
|
12120
|
+
for (let h = 0; h < holeCount; h++) {
|
|
12121
|
+
const hole = outer.child(h);
|
|
12122
|
+
rings.push(ringOf(hole));
|
|
12123
|
+
collectPolygons(hole, into);
|
|
12124
|
+
}
|
|
12125
|
+
into.push(rings);
|
|
12126
|
+
}
|
|
12127
|
+
};
|
|
12128
|
+
const bboxOf = (geometry) => {
|
|
12129
|
+
let minX = Infinity;
|
|
12130
|
+
let minY = Infinity;
|
|
12131
|
+
let maxX = -Infinity;
|
|
12132
|
+
let maxY = -Infinity;
|
|
12133
|
+
for (const rings of polygonsOf(geometry)) {
|
|
12134
|
+
const exterior = rings[0];
|
|
12135
|
+
if (exterior === undefined)
|
|
12136
|
+
continue;
|
|
12137
|
+
for (const [x, y] of exterior) {
|
|
12138
|
+
if (x < minX)
|
|
12139
|
+
minX = x;
|
|
12140
|
+
if (x > maxX)
|
|
12141
|
+
maxX = x;
|
|
12142
|
+
if (y < minY)
|
|
12143
|
+
minY = y;
|
|
12144
|
+
if (y > maxY)
|
|
12145
|
+
maxY = y;
|
|
12146
|
+
}
|
|
12147
|
+
}
|
|
12148
|
+
return [minX, minY, maxX, maxY];
|
|
12149
|
+
};
|
|
12150
|
+
/**
|
|
12151
|
+
* Build the difference function on an instantiated module. Throws (from Clipper2 or from the
|
|
12152
|
+
* bridge itself) propagate to the caller, which owns the fallback and the reporting; every embind
|
|
12153
|
+
* object is released in `finally` either way.
|
|
12154
|
+
*/
|
|
12155
|
+
const createClipper2PolygonDifference = (clipper) => (subject, clips) => {
|
|
12156
|
+
if (clips.length === 0)
|
|
12157
|
+
return subject;
|
|
12158
|
+
const owned = [];
|
|
12159
|
+
try {
|
|
12160
|
+
const subjectPaths = new clipper.PathsD();
|
|
12161
|
+
owned.push(subjectPaths);
|
|
12162
|
+
appendRingsAsPaths(clipper, subjectPaths, [subject]);
|
|
12163
|
+
const clipPaths = new clipper.PathsD();
|
|
12164
|
+
owned.push(clipPaths);
|
|
12165
|
+
appendRingsAsPaths(clipper, clipPaths, clips);
|
|
12166
|
+
// Trim every clip to the subject's bbox first (O(n)), so a country-sized clip only
|
|
12167
|
+
// contributes the few edges near a site-sized subject.
|
|
12168
|
+
const [minX, minY, maxX, maxY] = bboxOf(subject);
|
|
12169
|
+
const rect = new clipper.RectD(minX - RECT_CLIP_MARGIN, minY - RECT_CLIP_MARGIN, maxX + RECT_CLIP_MARGIN, maxY + RECT_CLIP_MARGIN);
|
|
12170
|
+
owned.push(rect);
|
|
12171
|
+
const trimmedClips = clipper.RectClipPathsD(rect, clipPaths, MERCATOR_PRECISION);
|
|
12172
|
+
owned.push(trimmedClips);
|
|
12173
|
+
const solution = new clipper.PolyPathD();
|
|
12174
|
+
owned.push(solution);
|
|
12175
|
+
clipper.BooleanOpOutD(clipper.ClipType.Difference, clipper.FillRule.NonZero, subjectPaths, trimmedClips, solution, MERCATOR_PRECISION);
|
|
12176
|
+
const polygons = [];
|
|
12177
|
+
collectPolygons(solution, polygons);
|
|
12178
|
+
const [single] = polygons;
|
|
12179
|
+
if (single === undefined)
|
|
12180
|
+
return null;
|
|
12181
|
+
if (polygons.length === 1)
|
|
12182
|
+
return { type: "Polygon", coordinates: single };
|
|
12183
|
+
return { type: "MultiPolygon", coordinates: polygons };
|
|
12184
|
+
}
|
|
12185
|
+
finally {
|
|
12186
|
+
for (const object of owned)
|
|
12187
|
+
object.delete();
|
|
12188
|
+
}
|
|
12189
|
+
};
|
|
12190
|
+
let sharedEngine = null;
|
|
12191
|
+
let loadedEngine;
|
|
12192
|
+
let unusable = false;
|
|
12193
|
+
const createEngine = async (options) => {
|
|
12194
|
+
const { default: createClipper2Module } = await import('clipper2-wasm/dist/es/clipper2z.js');
|
|
12195
|
+
// Emscripten writes load failures and aborts to console.error as well as throwing. The thrown
|
|
12196
|
+
// error is what callers act on (rejected load, or a throw from the returned function that the
|
|
12197
|
+
// fill-tiling hook reports and falls back from), so keep the duplicate stderr channel quiet.
|
|
12198
|
+
const clipper = await createClipper2Module({ ...options, printErr: () => undefined });
|
|
12199
|
+
return createClipper2PolygonDifference(clipper);
|
|
12200
|
+
};
|
|
12201
|
+
/**
|
|
12202
|
+
* Polygon difference backed by Clipper2 compiled to WebAssembly: a drop-in for
|
|
12203
|
+
* `geoJsonPolygonDifference` in fill tiling, roughly 6× faster on the same inputs. Unlike
|
|
12204
|
+
* `geoJsonPolygonDifference` it throws on failure instead of returning the subject unclipped:
|
|
12205
|
+
* polygon-clipping is still available as an exact fallback, and an Emscripten abort disables the
|
|
12206
|
+
* module for good, so the caller decides how to degrade and report.
|
|
12207
|
+
*
|
|
12208
|
+
* The module (~210 KB) is fetched and instantiated on first call and shared afterwards, including a
|
|
12209
|
+
* failed load: callers keep `geoJsonPolygonDifference` until the promise resolves and stay on it for
|
|
12210
|
+
* the page lifetime if it rejects (no WebAssembly support, blocked asset, …). `options.wasmBinary`
|
|
12211
|
+
* bypasses the fetch; engines created with options are not shared.
|
|
12212
|
+
*/
|
|
12213
|
+
const loadClipper2PolygonDifference = (options) => {
|
|
12214
|
+
if (unusable)
|
|
12215
|
+
return Promise.reject(new Error("Clipper2 was marked unusable after a runtime failure"));
|
|
12216
|
+
sharedEngine ?? (sharedEngine = createEngine(undefined).then(engine => {
|
|
12217
|
+
loadedEngine = engine;
|
|
12218
|
+
return engine;
|
|
12219
|
+
}));
|
|
12220
|
+
return sharedEngine;
|
|
12221
|
+
};
|
|
12222
|
+
/** The shared engine when it has already loaded, so a later mount can use it for its first pass. */
|
|
12223
|
+
const getLoadedClipper2PolygonDifference = () => loadedEngine;
|
|
12224
|
+
/**
|
|
12225
|
+
* Called when the shared engine threw: an Emscripten abort disables the module for the rest of the
|
|
12226
|
+
* page, so no later mount may adopt it and no reload is attempted.
|
|
12227
|
+
*/
|
|
12228
|
+
const markClipper2PolygonDifferenceUnusable = () => {
|
|
12229
|
+
unusable = true;
|
|
12230
|
+
loadedEngine = undefined;
|
|
12231
|
+
};
|
|
12232
|
+
/** Whether the shared engine was marked unusable; callers stay on polygon-clipping silently. */
|
|
12233
|
+
const isClipper2PolygonDifferenceUnusable = () => unusable;
|
|
12234
|
+
|
|
11965
12235
|
/** Lists the ids of features dropped from fill tiling, or a placeholder when none carry an id. */
|
|
11966
12236
|
const formatDroppedInvalidFeatureIds = (dropped) => dropped
|
|
11967
12237
|
.map(entry => (entry.feature.id !== undefined ? String(entry.feature.id) : undefined))
|
|
@@ -12163,6 +12433,13 @@ const useShapeFillTiling = ({ api, handles, onFillGeometriesChange, onZIndexOver
|
|
|
12163
12433
|
const errorHandler = reactCoreHooks.useErrorHandlerOrNull();
|
|
12164
12434
|
/** Handle/feature keys already reported to Sentry this session. */
|
|
12165
12435
|
const sessionReportedDedupeKeysRef = react.useRef(new Set());
|
|
12436
|
+
/**
|
|
12437
|
+
* Clipper2 WebAssembly difference once loaded, wrapped so that a failure reports once and
|
|
12438
|
+
* hands the clip (and every later clip) back to polygon-clipping. `undefined` = default path.
|
|
12439
|
+
*/
|
|
12440
|
+
const polygonDifferenceRef = react.useRef(undefined);
|
|
12441
|
+
/** Load lifecycle; `settled` (loaded, failed or unusable) means the effect never re-subscribes. */
|
|
12442
|
+
const clipper2StatusRef = react.useRef("idle");
|
|
12166
12443
|
const tilingContentKey = react.useMemo(() => buildTilingContentKey(handles), [handles]);
|
|
12167
12444
|
const tilingHandles = react.useMemo(() => handles.filter((handle) => handle.layerType === "shapes" && handle.overlap?.mode === "tile"), [handles]);
|
|
12168
12445
|
const hasTilingHandles = tilingHandles.length > 0;
|
|
@@ -12297,7 +12574,26 @@ const useShapeFillTiling = ({ api, handles, onFillGeometriesChange, onZIndexOver
|
|
|
12297
12574
|
if (restingClip !== undefined)
|
|
12298
12575
|
restingPeerFills.set(featureId, restingClip);
|
|
12299
12576
|
}
|
|
12300
|
-
const
|
|
12577
|
+
const unclippedPeerIds = [];
|
|
12578
|
+
const peerFills = computePromotionGroupFills({
|
|
12579
|
+
members: groupMembers,
|
|
12580
|
+
winnerId,
|
|
12581
|
+
restingPeerFills,
|
|
12582
|
+
layerGeodesic: handle?.style.geodesic,
|
|
12583
|
+
featureStyles: handle?.featureStyles,
|
|
12584
|
+
polygonDifference: polygonDifferenceRef.current,
|
|
12585
|
+
onDifferenceFallback: (featureId, error) => {
|
|
12586
|
+
unclippedPeerIds.push(featureId);
|
|
12587
|
+
if (errorHandler === null)
|
|
12588
|
+
return;
|
|
12589
|
+
captureFillTilingDifferenceFailures([{ featureId, clipFeatureIds: [winnerId], error }], handleId, "promotion", sessionReportedDedupeKeysRef.current, payload => {
|
|
12590
|
+
errorHandler.captureException(new Error(`polygon-clipping failed during fill-tiling promotion clip: ${JSON.stringify(payload)}`), { level: "warning", fingerprint: ["react-map", "fill-tiling", "difference-failure"] });
|
|
12591
|
+
});
|
|
12592
|
+
},
|
|
12593
|
+
});
|
|
12594
|
+
if (unclippedPeerIds.length > 0) {
|
|
12595
|
+
consoleWarnMap("useShapes", `polygon-clipping failed to clip ${unclippedPeerIds.length} peer(s) against promoted ${winnerId}; full fill kept: ${unclippedPeerIds.join(", ")}`);
|
|
12596
|
+
}
|
|
12301
12597
|
modifiedFills.delete(winnerId);
|
|
12302
12598
|
for (const [featureId, clip] of peerFills) {
|
|
12303
12599
|
modifiedFills.set(featureId, clip);
|
|
@@ -12337,7 +12633,7 @@ const useShapeFillTiling = ({ api, handles, onFillGeometriesChange, onZIndexOver
|
|
|
12337
12633
|
prevZIndexOverridesRef.current = nextZIndices;
|
|
12338
12634
|
onZIndexChangeRef.current(nextZIndices);
|
|
12339
12635
|
}
|
|
12340
|
-
}, []);
|
|
12636
|
+
}, [errorHandler]);
|
|
12341
12637
|
const recompute = react.useCallback(() => {
|
|
12342
12638
|
const currentHandles = tilingHandlesRef.current;
|
|
12343
12639
|
// Stable suppressed-key: per-handle sorted ids so suppression changes are
|
|
@@ -12396,13 +12692,14 @@ const useShapeFillTiling = ({ api, handles, onFillGeometriesChange, onZIndexOver
|
|
|
12396
12692
|
const nextRestingFills = new Map();
|
|
12397
12693
|
const nextRestingZIndices = new Map();
|
|
12398
12694
|
for (const handle of currentHandles) {
|
|
12399
|
-
const { fillGeometries, featureToGroupKey, featureZIndex, droppedInvalidFeatures, intersectionFailures } = computeFillTiling({
|
|
12695
|
+
const { fillGeometries, featureToGroupKey, featureZIndex, droppedInvalidFeatures, intersectionFailures, differenceFailures, } = computeFillTiling({
|
|
12400
12696
|
features: handle.features.features,
|
|
12401
12697
|
viewportBounds: GLOBAL_BOUNDS,
|
|
12402
12698
|
resolveStackOrder: handle.overlap?.resolveStackOrder ?? defaultShapeStackOrder,
|
|
12403
12699
|
suppressedFeatureIds: suppressedFillIdsByHandleRef.current?.get(handle.id),
|
|
12404
12700
|
layerGeodesic: handle.style.geodesic,
|
|
12405
12701
|
featureStyles: handle.featureStyles,
|
|
12702
|
+
polygonDifference: polygonDifferenceRef.current,
|
|
12406
12703
|
});
|
|
12407
12704
|
// The dev warnings live here, not in computeFillTiling: recompute runs from
|
|
12408
12705
|
// effects and idle callbacks, so the pure function stays silent (SAGA-907).
|
|
@@ -12412,6 +12709,9 @@ const useShapeFillTiling = ({ api, handles, onFillGeometriesChange, onZIndexOver
|
|
|
12412
12709
|
if (intersectionFailures.length > 0) {
|
|
12413
12710
|
consoleWarnMap("useShapes", `polygon-clipping failed on ${intersectionFailures.length} feature pair(s); treated as non-overlapping (full fill still renders): ${formatIntersectionFailurePairs(intersectionFailures)}`);
|
|
12414
12711
|
}
|
|
12712
|
+
if (differenceFailures.length > 0) {
|
|
12713
|
+
consoleWarnMap("useShapes", `polygon-clipping failed to clip ${differenceFailures.length} feature(s) against a degenerate neighbour; that overlap is left unclipped: ${differenceFailures.map(failure => failure.featureId).join(", ")}`);
|
|
12714
|
+
}
|
|
12415
12715
|
if (errorHandler !== null && droppedInvalidFeatures.length > 0) {
|
|
12416
12716
|
captureDroppedInvalidFillTilingFeatures(droppedInvalidFeatures, handle.id, sessionReportedDedupeKeysRef.current, payload => {
|
|
12417
12717
|
errorHandler.captureException(new Error(`Schema-invalid polygonal geometry excluded from fill tiling: ${JSON.stringify(payload)}`), {
|
|
@@ -12428,6 +12728,14 @@ const useShapeFillTiling = ({ api, handles, onFillGeometriesChange, onZIndexOver
|
|
|
12428
12728
|
});
|
|
12429
12729
|
});
|
|
12430
12730
|
}
|
|
12731
|
+
if (errorHandler !== null && differenceFailures.length > 0) {
|
|
12732
|
+
captureFillTilingDifferenceFailures(differenceFailures, handle.id, "resting", sessionReportedDedupeKeysRef.current, payload => {
|
|
12733
|
+
errorHandler.captureException(new Error(`polygon-clipping failed during fill-tiling clip: ${JSON.stringify(payload)}`), {
|
|
12734
|
+
level: "warning",
|
|
12735
|
+
fingerprint: ["react-map", "fill-tiling", "difference-failure"],
|
|
12736
|
+
});
|
|
12737
|
+
});
|
|
12738
|
+
}
|
|
12431
12739
|
featureToGroupKeyByHandleRef.current.set(handle.id, new Map(featureToGroupKey));
|
|
12432
12740
|
if (fillGeometries.size > 0) {
|
|
12433
12741
|
nextRestingFills.set(handle.id, fillGeometries);
|
|
@@ -12459,6 +12767,67 @@ const useShapeFillTiling = ({ api, handles, onFillGeometriesChange, onZIndexOver
|
|
|
12459
12767
|
flushPendingRestingRecompute();
|
|
12460
12768
|
}, IDLE_RECOMPUTE_DEBOUNCE_MS);
|
|
12461
12769
|
}, [flushPendingRestingRecompute]);
|
|
12770
|
+
// Use the Clipper2 WebAssembly difference for the clip phase once a tiling layer exists. An engine
|
|
12771
|
+
// that already loaded (an earlier map on this page) is adopted synchronously here, before the
|
|
12772
|
+
// mount recompute below, so the first pass already uses it. Otherwise the load runs in the
|
|
12773
|
+
// background and the resting clips are re-tiled through the idle path when it resolves, so a load
|
|
12774
|
+
// finishing mid-pan never freezes the gesture. A failed load (no WebAssembly, blocked asset) or a
|
|
12775
|
+
// failing clip keeps polygon-clipping for the rest of this map's life, reported once each.
|
|
12776
|
+
react.useEffect(() => {
|
|
12777
|
+
if (!hasTilingHandles || clipper2StatusRef.current !== "idle")
|
|
12778
|
+
return;
|
|
12779
|
+
if (isClipper2PolygonDifferenceUnusable()) {
|
|
12780
|
+
// Another map on this page already hit a runtime failure and reported it.
|
|
12781
|
+
clipper2StatusRef.current = "settled";
|
|
12782
|
+
return;
|
|
12783
|
+
}
|
|
12784
|
+
const adopt = (clipper2Difference) => {
|
|
12785
|
+
clipper2StatusRef.current = "settled";
|
|
12786
|
+
polygonDifferenceRef.current = (subject, clips, onFallback) => {
|
|
12787
|
+
try {
|
|
12788
|
+
return clipper2Difference(subject, clips);
|
|
12789
|
+
}
|
|
12790
|
+
catch (error) {
|
|
12791
|
+
// An Emscripten abort disables the module for good: for this map and any later one.
|
|
12792
|
+
markClipper2PolygonDifferenceUnusable();
|
|
12793
|
+
polygonDifferenceRef.current = undefined;
|
|
12794
|
+
errorHandler?.captureException(new Error(`Clipper2 WebAssembly difference failed; fill tiling falls back to polygon-clipping: ${String(error)}`), { level: "warning", fingerprint: ["react-map", "fill-tiling", "clipper2-difference-failed"] });
|
|
12795
|
+
return geoJsonUtils.geoJsonPolygonDifference(subject, clips, onFallback);
|
|
12796
|
+
}
|
|
12797
|
+
};
|
|
12798
|
+
};
|
|
12799
|
+
const loaded = getLoadedClipper2PolygonDifference();
|
|
12800
|
+
if (loaded !== undefined) {
|
|
12801
|
+
adopt(loaded);
|
|
12802
|
+
return;
|
|
12803
|
+
}
|
|
12804
|
+
clipper2StatusRef.current = "loading";
|
|
12805
|
+
let cancelled = false;
|
|
12806
|
+
loadClipper2PolygonDifference()
|
|
12807
|
+
.then(clipper2Difference => {
|
|
12808
|
+
if (cancelled)
|
|
12809
|
+
return;
|
|
12810
|
+
adopt(clipper2Difference);
|
|
12811
|
+
// Re-tile the resting clips with the engine, but never mid-gesture: while the map moves the
|
|
12812
|
+
// existing `idle` handler flushes the pending recompute once the movement settles.
|
|
12813
|
+
lastComputeInputKeyRef.current = null;
|
|
12814
|
+
pendingRestingRecomputeRef.current = true;
|
|
12815
|
+
if (!isMapMovingRef.current)
|
|
12816
|
+
scheduleIdleRestingRecompute();
|
|
12817
|
+
})
|
|
12818
|
+
.catch((error) => {
|
|
12819
|
+
if (cancelled)
|
|
12820
|
+
return;
|
|
12821
|
+
clipper2StatusRef.current = "settled";
|
|
12822
|
+
errorHandler?.captureException(new Error(`Clipper2 WebAssembly engine failed to load; fill tiling stays on polygon-clipping: ${String(error)}`), { level: "warning", fingerprint: ["react-map", "fill-tiling", "clipper2-load-failed"] });
|
|
12823
|
+
});
|
|
12824
|
+
return () => {
|
|
12825
|
+
cancelled = true;
|
|
12826
|
+
// Unmounted mid-load: allow the next mount of a tiling layer to subscribe again.
|
|
12827
|
+
if (clipper2StatusRef.current === "loading")
|
|
12828
|
+
clipper2StatusRef.current = "idle";
|
|
12829
|
+
};
|
|
12830
|
+
}, [hasTilingHandles, scheduleIdleRestingRecompute, errorHandler]);
|
|
12462
12831
|
const clearSelectionPromotion = react.useCallback((handleId, featureId) => {
|
|
12463
12832
|
const groupKey = featureToGroupKeyByHandleRef.current.get(handleId)?.get(featureId);
|
|
12464
12833
|
if (groupKey === undefined)
|