deeptwins-engine-3d 0.1.72 → 0.1.75

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.
@@ -1,4 +1,3 @@
1
- function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
2
1
  var _excluded = ["tilingScheme"];
3
2
  function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
4
3
  function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
@@ -7,6 +6,8 @@ function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) r
7
6
  function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
8
7
  function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
9
8
  function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
9
+ function _get() { if (typeof Reflect !== "undefined" && Reflect.get) { _get = Reflect.get.bind(); } else { _get = function _get(target, property, receiver) { var base = _superPropBase(target, property); if (!base) return; var desc = Object.getOwnPropertyDescriptor(base, property); if (desc.get) { return desc.get.call(arguments.length < 3 ? target : receiver); } return desc.value; }; } return _get.apply(this, arguments); }
10
+ function _superPropBase(object, property) { while (!Object.prototype.hasOwnProperty.call(object, property)) { object = _getPrototypeOf(object); if (object === null) break; } return object; }
10
11
  function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }
11
12
  function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
12
13
  function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
@@ -17,6 +18,7 @@ function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.g
17
18
  function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
18
19
  function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
19
20
  function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
21
+ function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
20
22
  function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
21
23
  function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
22
24
  function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
@@ -30,6 +32,7 @@ function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
30
32
  import * as turf from '@turf/turf';
31
33
  import * as Cesium from 'deeptwins-cesium';
32
34
  import proj4 from 'proj4';
35
+ import CustomRasterClipWorkerClient from "./CustomRasterClipWorkerClient";
33
36
  import ImageryLayer from "./ImageryLayer";
34
37
 
35
38
  /**
@@ -81,18 +84,22 @@ import ImageryLayer from "./ImageryLayer";
81
84
  * - `clipFeatureCrs`:可选,clipFeature 的原始坐标系,默认跟随 dataCrs。
82
85
  * - `renderCrs`:可选,bounds、clipFeature 和瓦片范围转换后的目标坐标系,默认 `EPSG:4326`。
83
86
  * - `projectionDefs`:可选,自定义 proj4 坐标系定义。
84
- * - `clipFeature`:可选,GeoJSON Polygon/MultiPolygon/Feature/FeatureCollection
87
+ * - `clipFeature`:可选,GeoJSON Polygon/MultiPolygon/Feature/FeatureCollection,`null` 等同未传。
85
88
  * - `autoFitBounds`:可选,创建后自动将相机定位到最终范围,需同时传入 `map`。
86
- * - `debug`:可选,`true` 使用 console.debug,函数则接收结构化调试事件。
89
+ * - `clipWithWorker`:可选,裁剪几何计算优先使用 Worker,默认 `true`。
90
+ * - `clipWorkerUrl`:可选,自定义裁剪 Worker 地址。
91
+ * - `debug`:可选,`true` 使用 console.log 输出调试信息,函数则接收结构化调试事件。
87
92
  */
88
93
 
89
94
  /** 通用范围格式:[minX, minY, maxX, maxY]。 */
90
95
 
91
96
  /** 调试事件类型,覆盖范围初始化、瓦片加载和裁剪决策。 */
92
97
 
98
+ /** 瓦片 debug 状态。 */
99
+
93
100
  /** debug 回调收到的结构化事件。 */
94
101
 
95
- /** debug 配置:关闭、console.debug 输出,或自定义事件处理函数。 */
102
+ /** debug 配置:关闭、console.log 输出,或自定义事件处理函数。 */
96
103
 
97
104
  /** 内置切片方案名称,或 Cesium 原生 TilingScheme 实例。 */
98
105
 
@@ -389,6 +396,14 @@ var transformGeoJson = function transformGeoJson(geoJson, sourceCrs, targetCrs)
389
396
  return clonedGeoJson;
390
397
  };
391
398
 
399
+ /** 将 null、undefined 和非 GeoJSON 对象统一视为未传裁剪面。 */
400
+ var normalizeClipFeature = function normalizeClipFeature(clipFeature) {
401
+ if (!clipFeature || _typeof(clipFeature) !== 'object' || Array.isArray(clipFeature)) {
402
+ return undefined;
403
+ }
404
+ return clipFeature;
405
+ };
406
+
392
407
  /** 将 Geometry 包装为 Turf 可处理的 Feature。 */
393
408
  var createFeature = function createFeature(geometry) {
394
409
  var properties = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
@@ -448,12 +463,55 @@ var getClipFeatureBounds = function getClipFeatureBounds(clipFeatures) {
448
463
  }
449
464
  return normalizeBounds(bbox);
450
465
  };
466
+ var boundsIntersects = function boundsIntersects(left, right) {
467
+ return left[0] <= right[2] && left[2] >= right[0] && left[1] <= right[3] && left[3] >= right[1];
468
+ };
469
+
470
+ /** 为裁剪面预计算 bbox,供瓦片级粗过滤使用。 */
471
+ var createClipFeatureEntries = function createClipFeatureEntries(clipFeatures) {
472
+ return clipFeatures.flatMap(function (feature) {
473
+ var bbox = normalizeBounds(turf.bbox(feature));
474
+ return bbox ? [{
475
+ feature: feature,
476
+ bbox: bbox
477
+ }] : [];
478
+ });
479
+ };
480
+
481
+ /** 高 minimumLevel 必须配合明确范围,避免 Cesium 初始化过多根瓦片。 */
482
+ var validateRasterSchedulingRange = function validateRasterSchedulingRange(options) {
483
+ if ((options.minimumLevel || 0) > 0 && !options.hasRenderBounds && !options.hasSourceRectangle) {
484
+ throw new Error('CustomRasterLayer source.minimumLevel > 0 requires bounds, source.rectangle, or a valid clipFeature bounds.');
485
+ }
486
+ };
451
487
  var getImageSize = function getImageSize(image) {
452
488
  return {
453
489
  width: image.width,
454
490
  height: image.height
455
491
  };
456
492
  };
493
+ var DEBUG_STATUS_STYLES = {
494
+ loaded: {
495
+ label: '瓦片已加载',
496
+ consoleColor: '#16a34a'
497
+ },
498
+ kept: {
499
+ label: '瓦片已保留',
500
+ consoleColor: '#16a34a'
501
+ },
502
+ clipped: {
503
+ label: '瓦片已裁剪',
504
+ consoleColor: '#eab308'
505
+ },
506
+ filtered: {
507
+ label: '瓦片已过滤',
508
+ consoleColor: '#dc2626'
509
+ },
510
+ error: {
511
+ label: '瓦片处理异常',
512
+ consoleColor: '#b91c1c'
513
+ }
514
+ };
457
515
 
458
516
  /**
459
517
  * 通用自定义栅格图层。
@@ -466,7 +524,7 @@ var CustomRasterLayer = /*#__PURE__*/function (_ImageryLayer) {
466
524
  var _super = _createSuper(CustomRasterLayer);
467
525
  /** 创建通用栅格图层。 */
468
526
  function CustomRasterLayer(options) {
469
- var _options$map;
527
+ var _options$clipWithWork, _options$clipWorkerUr, _options$map;
470
528
  var _this;
471
529
  _classCallCheck(this, CustomRasterLayer);
472
530
  if (!(options !== null && options !== void 0 && options.source)) {
@@ -476,15 +534,25 @@ var CustomRasterLayer = /*#__PURE__*/function (_ImageryLayer) {
476
534
  var dataCrs = normalizeCrs(options.dataCrs || DEFAULT_CRS);
477
535
  var clipFeatureCrs = normalizeCrs(options.clipFeatureCrs || dataCrs);
478
536
  var renderCrs = normalizeCrs(options.renderCrs || DEFAULT_CRS);
479
- var transformedClipFeature = transformGeoJson(options.clipFeature, clipFeatureCrs, renderCrs);
537
+ var normalizedClipFeature = normalizeClipFeature(options.clipFeature);
538
+ var transformedClipFeature = normalizedClipFeature ? transformGeoJson(normalizedClipFeature, clipFeatureCrs, renderCrs) : undefined;
480
539
  var clipFeatures = collectPolygonFeatures(transformedClipFeature);
540
+ var clipFeatureEntries = createClipFeatureEntries(clipFeatures);
541
+ var effectiveClipFeatures = clipFeatureEntries.map(function (entry) {
542
+ return entry.feature;
543
+ });
481
544
  var _options$source = options.source,
482
545
  sourceTilingSchemeOption = _options$source.tilingScheme,
483
546
  sourceOptionsRest = _objectWithoutProperties(_options$source, _excluded);
484
547
  var tileBounds = options.bounds ? transformBounds(options.bounds, dataCrs, renderCrs) : undefined;
485
- var clipBounds = getClipFeatureBounds(clipFeatures);
548
+ var clipBounds = getClipFeatureBounds(effectiveClipFeatures);
486
549
  var renderBounds = clipBounds || tileBounds;
487
550
  var sourceRectangleBounds = sourceOptionsRest.rectangle ? rectangleToBounds(sourceOptionsRest.rectangle) : undefined;
551
+ validateRasterSchedulingRange({
552
+ minimumLevel: sourceOptionsRest.minimumLevel,
553
+ hasRenderBounds: !!renderBounds,
554
+ hasSourceRectangle: !!sourceOptionsRest.rectangle
555
+ });
488
556
  var fitBounds = clipBounds || tileBounds || sourceRectangleBounds;
489
557
  var tilingScheme = createRenderTilingScheme(createSourceTilingScheme(sourceTilingSchemeOption), renderCrs);
490
558
  var sourceOptions = _objectSpread(_objectSpread({}, sourceOptionsRest), {}, {
@@ -502,21 +570,32 @@ var CustomRasterLayer = /*#__PURE__*/function (_ImageryLayer) {
502
570
  _defineProperty(_assertThisInitialized(_this), "source", void 0);
503
571
  _defineProperty(_assertThisInitialized(_this), "_debug", void 0);
504
572
  _defineProperty(_assertThisInitialized(_this), "_clipFeatures", void 0);
573
+ _defineProperty(_assertThisInitialized(_this), "_clipFeatureEntries", void 0);
505
574
  _defineProperty(_assertThisInitialized(_this), "_fitBounds", void 0);
575
+ _defineProperty(_assertThisInitialized(_this), "_clipWorkerUrl", void 0);
576
+ _defineProperty(_assertThisInitialized(_this), "_clipWithWorker", void 0);
577
+ _defineProperty(_assertThisInitialized(_this), "_clipWorkerClient", void 0);
506
578
  _defineProperty(_assertThisInitialized(_this), "_mapContext", void 0);
579
+ _defineProperty(_assertThisInitialized(_this), "_destroyed", void 0);
507
580
  _this.id = options.id;
508
581
  _this.zIndex = options.zIndex;
509
582
  _this.source = imageProvider;
510
583
  _this._debug = options.debug;
511
- _this._clipFeatures = clipFeatures;
584
+ _this._clipFeatures = effectiveClipFeatures;
585
+ _this._clipFeatureEntries = clipFeatureEntries;
512
586
  _this._fitBounds = fitBounds;
587
+ _this._clipWithWorker = (_options$clipWithWork = options.clipWithWorker) !== null && _options$clipWithWork !== void 0 ? _options$clipWithWork : true;
588
+ _this._clipWorkerUrl = (_options$clipWorkerUr = options.clipWorkerUrl) !== null && _options$clipWorkerUr !== void 0 ? _options$clipWorkerUr : '';
589
+ _this._clipWorkerClient = null;
513
590
  _this._mapContext = (_options$map = options.map) === null || _options$map === void 0 ? void 0 : _options$map._mapContext;
591
+ _this._destroyed = false;
514
592
  _this.logDebug({
515
593
  type: 'range',
516
594
  bounds: options.bounds,
517
595
  tileBounds: tileBounds,
518
596
  clipBounds: clipBounds,
519
597
  renderBounds: renderBounds,
598
+ clipFeature: transformedClipFeature,
520
599
  dataCrs: dataCrs,
521
600
  clipFeatureCrs: clipFeatureCrs,
522
601
  renderCrs: renderCrs
@@ -564,6 +643,15 @@ var CustomRasterLayer = /*#__PURE__*/function (_ImageryLayer) {
564
643
  throw new Error('CustomRasterLayer fitToBounds requires a map camera.');
565
644
  }
566
645
 
646
+ /** 销毁图层并释放裁剪 Worker。 */
647
+ }, {
648
+ key: "destroy",
649
+ value: function destroy() {
650
+ this._destroyed = true;
651
+ this.destroyClipWorkerClient();
652
+ return _get(_getPrototypeOf(CustomRasterLayer.prototype), "destroy", this).call(this);
653
+ }
654
+
567
655
  /**
568
656
  * Cesium 内部纹理创建钩子。
569
657
  *
@@ -575,7 +663,6 @@ var CustomRasterLayer = /*#__PURE__*/function (_ImageryLayer) {
575
663
  }, {
576
664
  key: "_createTexture",
577
665
  value: function _createTexture(context, imagery) {
578
- var _this2 = this;
579
666
  var tileBbox = rectangleToBounds(imagery.rectangle);
580
667
  this.logDebug({
581
668
  type: 'tile-loaded',
@@ -585,91 +672,263 @@ var CustomRasterLayer = /*#__PURE__*/function (_ImageryLayer) {
585
672
  bbox: tileBbox,
586
673
  imageSize: getImageSize(imagery.image)
587
674
  });
588
- if (!this._clipFeatures.length) {
675
+ if (!this._clipFeatureEntries.length) {
589
676
  return this.createTexture(context, imagery);
590
677
  }
591
678
  try {
592
- var tileFeature = turf.bboxPolygon(tileBbox);
593
- var isWithin = this._clipFeatures.some(function (feature) {
594
- return turf.booleanWithin(tileFeature, feature);
679
+ return this.createClippedTexture(context, imagery, tileBbox);
680
+ } catch (error) {
681
+ this.logDebug({
682
+ type: 'tile-error',
683
+ x: imagery.x,
684
+ y: imagery.y,
685
+ level: imagery.level,
686
+ bbox: tileBbox,
687
+ error: error
595
688
  });
596
- if (isWithin) {
597
- this.logDebug({
598
- type: 'tile-kept',
599
- x: imagery.x,
600
- y: imagery.y,
601
- level: imagery.level,
602
- bbox: tileBbox,
603
- reason: 'within-clip'
604
- });
605
- return this.createTexture(context, imagery);
689
+ return this.createTexture(context, imagery);
690
+ }
691
+ }
692
+
693
+ /** 调用 Cesium 原始纹理创建逻辑。 */
694
+ }, {
695
+ key: "createTexture",
696
+ value: function createTexture(context, imagery) {
697
+ return ImageryLayer.prototype._createTexture.call(this, context, imagery);
698
+ }
699
+
700
+ /** 根据裁剪面 bbox 粗过滤当前瓦片可能相交的面。 */
701
+ }, {
702
+ key: "getCandidateClipFeatureIndexes",
703
+ value: function getCandidateClipFeatureIndexes(tileBbox) {
704
+ var candidateIndexes = [];
705
+ this._clipFeatureEntries.forEach(function (entry, index) {
706
+ if (boundsIntersects(tileBbox, entry.bbox)) {
707
+ candidateIndexes.push(index);
708
+ }
709
+ });
710
+ return candidateIndexes;
711
+ }
712
+ }, {
713
+ key: "getClipFeatures",
714
+ value: function getClipFeatures(candidateIndexes) {
715
+ var _this2 = this;
716
+ var features = [];
717
+ candidateIndexes.forEach(function (index) {
718
+ var _this2$_clipFeatureEn;
719
+ var feature = (_this2$_clipFeatureEn = _this2._clipFeatureEntries[index]) === null || _this2$_clipFeatureEn === void 0 ? void 0 : _this2$_clipFeatureEn.feature;
720
+ if (feature) {
721
+ features.push(feature);
606
722
  }
607
- var intersections = this.getClipIntersections(tileFeature);
608
- if (!intersections.length) {
609
- this.logDebug({
610
- type: 'tile-discarded',
723
+ });
724
+ return features;
725
+ }
726
+
727
+ // 按需创建裁剪 Worker;初始化失败时自动回退主线程裁剪。
728
+ }, {
729
+ key: "getClipWorkerClient",
730
+ value: function getClipWorkerClient() {
731
+ if (!this._clipWithWorker || this._destroyed || !this._clipFeatureEntries.length) {
732
+ return null;
733
+ }
734
+ if (this._clipWorkerClient) {
735
+ return this._clipWorkerClient;
736
+ }
737
+ try {
738
+ this._clipWorkerClient = new CustomRasterClipWorkerClient({
739
+ features: this._clipFeatureEntries,
740
+ workerUrl: this._clipWorkerUrl || undefined
741
+ });
742
+ return this._clipWorkerClient;
743
+ } catch (error) {
744
+ this._clipWithWorker = false;
745
+ console.warn('栅格裁剪 Worker 初始化失败,已回退主线程裁剪', error);
746
+ return null;
747
+ }
748
+ }
749
+
750
+ // 销毁当前裁剪 Worker,避免销毁后仍有挂起任务占用资源。
751
+ }, {
752
+ key: "destroyClipWorkerClient",
753
+ value: function destroyClipWorkerClient() {
754
+ var _this$_clipWorkerClie;
755
+ (_this$_clipWorkerClie = this._clipWorkerClient) === null || _this$_clipWorkerClie === void 0 || _this$_clipWorkerClie.destroy();
756
+ this._clipWorkerClient = null;
757
+ }
758
+ }, {
759
+ key: "createClippedTexture",
760
+ value: function createClippedTexture(context, imagery, tileBbox) {
761
+ var _this3 = this;
762
+ var candidateIndexes = this.getCandidateClipFeatureIndexes(tileBbox);
763
+ if (!candidateIndexes.length) {
764
+ return this.applyClipResult(context, imagery, tileBbox, {
765
+ status: 'discarded',
766
+ reason: 'outside-clip'
767
+ });
768
+ }
769
+ var clipWorkerClient = this.getClipWorkerClient();
770
+ if (!clipWorkerClient) {
771
+ return this.applyClipResult(context, imagery, tileBbox, this.clipTileOnMainThread(tileBbox, candidateIndexes, imagery));
772
+ }
773
+ void clipWorkerClient.clip({
774
+ tileBbox: tileBbox,
775
+ candidateIndexes: candidateIndexes,
776
+ width: imagery.image.width,
777
+ height: imagery.image.height
778
+ }).then(function (result) {
779
+ if (_this3._destroyed) {
780
+ return;
781
+ }
782
+ _this3.applyClipResult(context, imagery, tileBbox, result);
783
+ }).catch(function (error) {
784
+ _this3.destroyClipWorkerClient();
785
+ _this3._clipWithWorker = false;
786
+ if (_this3._destroyed) {
787
+ return;
788
+ }
789
+ _this3.logDebug({
790
+ type: 'tile-error',
791
+ x: imagery.x,
792
+ y: imagery.y,
793
+ level: imagery.level,
794
+ bbox: tileBbox,
795
+ error: error
796
+ });
797
+ try {
798
+ _this3.applyClipResult(context, imagery, tileBbox, _this3.clipTileOnMainThread(tileBbox, candidateIndexes, imagery));
799
+ } catch (fallbackError) {
800
+ _this3.logDebug({
801
+ type: 'tile-error',
611
802
  x: imagery.x,
612
803
  y: imagery.y,
613
804
  level: imagery.level,
614
805
  bbox: tileBbox,
615
- reason: 'outside-clip'
806
+ error: fallbackError
616
807
  });
617
- imagery.image = this.createTileCanvas(imagery);
618
- return this.createTexture(context, imagery);
808
+ _this3.createTexture(context, imagery);
619
809
  }
620
- var canvas = this.createTileCanvas(imagery);
621
- var canvasContext = canvas.getContext('2d');
622
- if (!canvasContext) {
623
- return this.createTexture(context, imagery);
624
- }
625
- canvasContext.drawImage(imagery.image, 0, 0, imagery.image.width, imagery.image.height);
626
- this.clip(canvasContext, intersections.flatMap(function (intersection) {
627
- return _this2.geometryToPathArray({
810
+ });
811
+ return undefined;
812
+ }
813
+
814
+ /** 主线程裁剪回退逻辑,保持与 Worker 返回结构一致。 */
815
+ }, {
816
+ key: "clipTileOnMainThread",
817
+ value: function clipTileOnMainThread(tileBbox, candidateIndexes, imagery) {
818
+ var _this4 = this;
819
+ var clipFeatures = this.getClipFeatures(candidateIndexes);
820
+ if (!clipFeatures.length) {
821
+ return {
822
+ status: 'discarded',
823
+ reason: 'outside-clip'
824
+ };
825
+ }
826
+ var tileFeature = turf.bboxPolygon(tileBbox);
827
+ var isWithin = clipFeatures.some(function (feature) {
828
+ return turf.booleanWithin(tileFeature, feature);
829
+ });
830
+ if (isWithin) {
831
+ return {
832
+ status: 'kept',
833
+ reason: 'within-clip'
834
+ };
835
+ }
836
+ var intersections = this.getClipIntersections(tileFeature, clipFeatures);
837
+ if (!intersections.length) {
838
+ return {
839
+ status: 'discarded',
840
+ reason: 'outside-clip'
841
+ };
842
+ }
843
+ return {
844
+ status: 'clipped',
845
+ reason: 'intersects-clip',
846
+ paths: intersections.flatMap(function (intersection) {
847
+ return _this4.geometryToPathArray({
628
848
  boundPolygon: intersection,
629
849
  bbox: tileBbox,
630
850
  width: imagery.image.width,
631
851
  height: imagery.image.height
632
852
  });
633
- }));
853
+ })
854
+ };
855
+ }
856
+ }, {
857
+ key: "applyClipResult",
858
+ value: function applyClipResult(context, imagery, tileBbox, result) {
859
+ var _this5 = this;
860
+ if (result.status === 'kept') {
634
861
  this.logDebug({
635
- type: 'tile-clipped',
862
+ type: 'tile-kept',
636
863
  x: imagery.x,
637
864
  y: imagery.y,
638
865
  level: imagery.level,
639
866
  bbox: tileBbox,
640
- reason: 'intersects-clip'
867
+ reason: result.reason
641
868
  });
642
- void createImageBitmap(canvas).then(function (image) {
643
- imagery.image = image;
644
- _this2.createTexture(context, imagery);
645
- });
646
- return undefined;
647
- } catch (error) {
869
+ return this.createTexture(context, imagery);
870
+ }
871
+ if (result.status === 'discarded') {
648
872
  this.logDebug({
649
- type: 'tile-error',
873
+ type: 'tile-discarded',
650
874
  x: imagery.x,
651
875
  y: imagery.y,
652
876
  level: imagery.level,
653
877
  bbox: tileBbox,
654
- error: error
878
+ reason: result.reason
655
879
  });
880
+ imagery.image = this.createTileCanvas(imagery);
656
881
  return this.createTexture(context, imagery);
657
882
  }
883
+ var canvas = this.createTileCanvas(imagery);
884
+ var canvasContext = canvas.getContext('2d');
885
+ if (!canvasContext) {
886
+ return this.createTexture(context, imagery);
887
+ }
888
+ canvasContext.drawImage(imagery.image, 0, 0, imagery.image.width, imagery.image.height);
889
+ this.clip(canvasContext, result.paths);
890
+ this.logDebug({
891
+ type: 'tile-clipped',
892
+ x: imagery.x,
893
+ y: imagery.y,
894
+ level: imagery.level,
895
+ bbox: tileBbox,
896
+ reason: result.reason
897
+ });
898
+ if (typeof createImageBitmap !== 'function') {
899
+ imagery.image = canvas;
900
+ return this.createTexture(context, imagery);
901
+ }
902
+ void createImageBitmap(canvas).then(function (image) {
903
+ if (_this5._destroyed) {
904
+ return;
905
+ }
906
+ imagery.image = image;
907
+ _this5.createTexture(context, imagery);
908
+ }).catch(function (error) {
909
+ if (_this5._destroyed) {
910
+ return;
911
+ }
912
+ _this5.logDebug({
913
+ type: 'tile-error',
914
+ x: imagery.x,
915
+ y: imagery.y,
916
+ level: imagery.level,
917
+ bbox: tileBbox,
918
+ error: error
919
+ });
920
+ imagery.image = canvas;
921
+ _this5.createTexture(context, imagery);
922
+ });
923
+ return undefined;
658
924
  }
659
925
 
660
- /** 调用 Cesium 原始纹理创建逻辑。 */
661
- }, {
662
- key: "createTexture",
663
- value: function createTexture(context, imagery) {
664
- return ImageryLayer.prototype._createTexture.call(this, context, imagery);
665
- }
666
-
667
- /** 获取当前瓦片与所有裁剪面的交集,用交集路径裁剪当前瓦片。 */
926
+ /** 获取当前瓦片与候选裁剪面的交集,用交集路径裁剪当前瓦片。 */
668
927
  }, {
669
928
  key: "getClipIntersections",
670
- value: function getClipIntersections(tileFeature) {
929
+ value: function getClipIntersections(tileFeature, clipFeatures) {
671
930
  var intersections = [];
672
- this._clipFeatures.forEach(function (feature) {
931
+ clipFeatures.forEach(function (feature) {
673
932
  if (!turf.booleanIntersects(tileFeature, feature)) {
674
933
  return;
675
934
  }
@@ -709,7 +968,7 @@ var CustomRasterLayer = /*#__PURE__*/function (_ImageryLayer) {
709
968
  }, {
710
969
  key: "geometryToPathArray",
711
970
  value: function geometryToPathArray(options) {
712
- var _this3 = this;
971
+ var _this6 = this;
713
972
  var featureType = options.boundPolygon.geometry.type;
714
973
  var pointArr = [];
715
974
  if (featureType === 'Polygon') {
@@ -722,7 +981,7 @@ var CustomRasterLayer = /*#__PURE__*/function (_ImageryLayer) {
722
981
  });
723
982
  } else if (featureType === 'MultiPolygon') {
724
983
  options.boundPolygon.geometry.coordinates.forEach(function (polygon) {
725
- _this3.pointToPixel({
984
+ _this6.pointToPixel({
726
985
  pointArr: pointArr,
727
986
  coors: polygon,
728
987
  bbox: options.bbox,
@@ -756,6 +1015,94 @@ var CustomRasterLayer = /*#__PURE__*/function (_ImageryLayer) {
756
1015
  context.fill();
757
1016
  context.restore();
758
1017
  }
1018
+ }, {
1019
+ key: "getTileKey",
1020
+ value: function getTileKey(event) {
1021
+ if (event.x === undefined || event.y === undefined || event.level === undefined) {
1022
+ return undefined;
1023
+ }
1024
+ return "".concat(event.level, "/").concat(event.x, "/").concat(event.y);
1025
+ }
1026
+ }, {
1027
+ key: "getDebugStatus",
1028
+ value: function getDebugStatus(event) {
1029
+ if (event.status) {
1030
+ return event.status;
1031
+ }
1032
+ if (event.type === 'tile-loaded') {
1033
+ return 'loaded';
1034
+ }
1035
+ if (event.type === 'tile-kept') {
1036
+ return 'kept';
1037
+ }
1038
+ if (event.type === 'tile-clipped') {
1039
+ return 'clipped';
1040
+ }
1041
+ if (event.type === 'tile-discarded') {
1042
+ return 'filtered';
1043
+ }
1044
+ if (event.type === 'tile-error') {
1045
+ return 'error';
1046
+ }
1047
+ return undefined;
1048
+ }
1049
+ }, {
1050
+ key: "normalizeDebugEvent",
1051
+ value: function normalizeDebugEvent(event) {
1052
+ var status = this.getDebugStatus(event);
1053
+ return _objectSpread(_objectSpread(_objectSpread({}, event), this.getTileKey(event) ? {
1054
+ tileKey: this.getTileKey(event)
1055
+ } : {}), status ? {
1056
+ status: status,
1057
+ loaded: true,
1058
+ clipped: status === 'clipped',
1059
+ filtered: status === 'filtered'
1060
+ } : {});
1061
+ }
1062
+ }, {
1063
+ key: "logBuiltInDebug",
1064
+ value: function logBuiltInDebug(event) {
1065
+ if (event.type === 'range') {
1066
+ console.log('[CustomRasterLayer] 渲染范围/裁剪范围', {
1067
+ renderBounds: event.renderBounds,
1068
+ clipBounds: event.clipBounds,
1069
+ clipFeature: event.clipFeature,
1070
+ tileBounds: event.tileBounds,
1071
+ bounds: event.bounds,
1072
+ dataCrs: event.dataCrs,
1073
+ clipFeatureCrs: event.clipFeatureCrs,
1074
+ renderCrs: event.renderCrs
1075
+ });
1076
+ return;
1077
+ }
1078
+ if (!event.status) {
1079
+ return;
1080
+ }
1081
+ if (event.type === 'tile-loaded' && this._clipFeatures.length) {
1082
+ return;
1083
+ }
1084
+ var style = DEBUG_STATUS_STYLES[event.status];
1085
+ var title = "[CustomRasterLayer] ".concat(style.label);
1086
+ var consoleStyle = "color: ".concat(style.consoleColor, "; font-weight: bold;");
1087
+ var tileInfo = "z=".concat(event.level, " x=").concat(event.x, " y=").concat(event.y);
1088
+ if (event.type === 'tile-error') {
1089
+ console.error("%c".concat(title, " ").concat(tileInfo), consoleStyle, event.error, event);
1090
+ return;
1091
+ }
1092
+ console.log("%c".concat(title, " ").concat(tileInfo), consoleStyle, {
1093
+ tileKey: event.tileKey,
1094
+ z: event.level,
1095
+ x: event.x,
1096
+ y: event.y,
1097
+ loaded: event.loaded,
1098
+ clipped: event.clipped,
1099
+ filtered: event.filtered,
1100
+ status: event.status,
1101
+ reason: event.reason,
1102
+ bbox: event.bbox,
1103
+ imageSize: event.imageSize
1104
+ });
1105
+ }
759
1106
 
760
1107
  /** 输出结构化 debug 事件。 */
761
1108
  }, {
@@ -764,14 +1111,14 @@ var CustomRasterLayer = /*#__PURE__*/function (_ImageryLayer) {
764
1111
  if (!this._debug) {
765
1112
  return;
766
1113
  }
767
- var debugEvent = _objectSpread({
1114
+ var debugEvent = this.normalizeDebugEvent(_objectSpread({
768
1115
  layerId: this.id
769
- }, event);
1116
+ }, event));
770
1117
  if (typeof this._debug === 'function') {
771
1118
  this._debug(debugEvent);
772
1119
  return;
773
1120
  }
774
- console.debug('[CustomRasterLayer]', debugEvent);
1121
+ this.logBuiltInDebug(debugEvent);
775
1122
  }
776
1123
  }]);
777
1124
  return CustomRasterLayer;