@caoguo/maplibre 0.0.5 → 0.0.6

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/dist/index.d.cts CHANGED
@@ -268,7 +268,7 @@ declare class ExportControl {
268
268
  interface GlowLine {
269
269
  /** 线坐标 [lng, lat][] */
270
270
  coordinates: [number, number][];
271
- /** 该线所属分组(如 'pipe' | 'road' | 'water'),用于着色 */
271
+ /** 该线所属分组(如 'pipe' | 'road' | 'water'),用于按组分色。缺省归入 'default'。 */
272
272
  group?: string;
273
273
  }
274
274
  interface GlowPass {
@@ -279,11 +279,12 @@ interface GlowPass {
279
279
  }
280
280
  interface GlowGeometry {
281
281
  /**
282
- * 扁平化顶点缓冲。每个顶点 5 个 float:
283
- * [worldX, worldY, dirX, dirY, side]
282
+ * 扁平化顶点缓冲。每个顶点 6 个 float:
283
+ * [worldX, worldY, dirX, dirY, side, groupIndex]
284
284
  * - worldX/worldY:该端点的归一化世界坐标
285
285
  * - dirX/dirY:所在线段的世界空间方向(用于屏幕空间法线)
286
286
  * - side:±1,左/右扩展符号
287
+ * - groupIndex:该顶点所属分组在 `groups` 中的下标(用于按组分色)
287
288
  */
288
289
  vertices: Float32Array;
289
290
  /** 每遍在 vertices 中的顶点区间 [start, count](按顶点数,非 float 数) */
@@ -295,6 +296,15 @@ interface GlowGeometry {
295
296
  passes: GlowPass[];
296
297
  /** 顶点属性跨距(float 数) */
297
298
  stride: number;
299
+ /** 参与渲染的去重分组名(与上层 colorMap 对应,下标即 groupIndex) */
300
+ groups: string[];
301
+ /** 逐遍、逐分组的连续顶点区间,供按组分色绘制 */
302
+ renderGroups: {
303
+ passIndex: number;
304
+ group: string;
305
+ start: number;
306
+ count: number;
307
+ }[];
298
308
  }
299
309
  /**
300
310
  * 生成辉光多遍档位。
@@ -362,9 +372,15 @@ declare class CustomLineLayer implements CustomLayerInterface {
362
372
  private buffer?;
363
373
  private geometry;
364
374
  private map?;
375
+ private gl?;
365
376
  constructor(opts: GlowLayerOptions);
366
377
  onAdd(map: unknown, gl: WebGLRenderingContext): void;
367
378
  render(gl: WebGLRenderingContext, matrix: number[]): void;
379
+ /**
380
+ * 动态更新线集合(高亮选中、切换数据等无需重建图层)。
381
+ * 浏览器环境会刷新 GPU 缓冲;纯逻辑/测试环境仅更新几何与数据。
382
+ */
383
+ setLines(lines: GlowLine[]): void;
368
384
  onRemove(_map: unknown, gl: WebGLRenderingContext): void;
369
385
  }
370
386
 
@@ -516,6 +532,13 @@ interface CaoguoMapGlobalConfig {
516
532
  }
517
533
  declare function setGlobalConfig(cfg: CaoguoMapGlobalConfig): void;
518
534
  declare function getGlobalConfig(): CaoguoMapGlobalConfig;
535
+ /** `addGlowLayer` 返回的句柄:既提供图层 id(用于 removeLayer),也支持动态更新线数据。 */
536
+ interface GlowLayerHandle {
537
+ /** 图层 id,可用于 map.removeLayer(id) */
538
+ id: string;
539
+ /** 动态替换线集合(高亮选中 / 切换数据,无需重建图层) */
540
+ setLines: (lines: GlowLine[]) => void;
541
+ }
519
542
  interface MapOptions {
520
543
  container: string | HTMLElement;
521
544
  center?: [number, number];
@@ -645,7 +668,7 @@ declare class Map {
645
668
  colors?: Record<string, [number, number, number]>;
646
669
  baseWidth?: number;
647
670
  passes?: number;
648
- }): string;
671
+ }): GlowLayerHandle;
649
672
  /**
650
673
  * 挂载 LOD 控制器(T7 / F-1.7)。
651
674
  * 随 zoom 切换数据密度等级,并在等级变化时回调(调用方据此 setData / 切源)。
@@ -675,4 +698,4 @@ declare class Map {
675
698
  get instance(): Map$1;
676
699
  }
677
700
 
678
- export { type Bounds, type CRS, type CaoguoMapGlobalConfig, type CustomLayerInterface, CustomLineLayer, ExportControl, type ExportOptions, type GlowGeometry, type GlowLayerOptions, type GlowLine, type GlowPass, type GridShiftProvider, LegendControl, type LegendItem, type LegendOptions, type LngLat, type LodChangeEvent, LodController, type LodLevel, Map, type MapInstance, type MapOptions, type MlMapSourceApi, type Point, type ScaleBar, ScaleControl, type ScaleControlOptions, ThemeSwitcher, type ThemeSwitcherOptions, TiandituOptions, TiandituType, TileStoreBackend, type Transformer, WebGLUnavailableError, buildGlowGeometry, cgcs2000ToWgs84, computeScaleBar, createTransformer, Map as default, fromWgs84, gcj02ToWgs84, getGlobalConfig, glowPasses, isInChina, isWebGLAvailable, oppositeTheme, packGeoJSONToStore, projectSimple, removeSourceSafe, removeSourcesSafe, renderLegendHtml, resolveLod, setCgcs2000GridShift, setGlobalConfig, suggestDensity, themeFromStyle, toWgs84, transformBounds, transformPoint, triggerDownload, upsertSource, wgs84ToCgcs2000, wgs84ToGcj02 };
701
+ export { type Bounds, type CRS, type CaoguoMapGlobalConfig, type CustomLayerInterface, CustomLineLayer, ExportControl, type ExportOptions, type GlowGeometry, type GlowLayerHandle, type GlowLayerOptions, type GlowLine, type GlowPass, type GridShiftProvider, LegendControl, type LegendItem, type LegendOptions, type LngLat, type LodChangeEvent, LodController, type LodLevel, Map, type MapInstance, type MapOptions, type MlMapSourceApi, type Point, type ScaleBar, ScaleControl, type ScaleControlOptions, ThemeSwitcher, type ThemeSwitcherOptions, TiandituOptions, TiandituType, TileStoreBackend, type Transformer, WebGLUnavailableError, buildGlowGeometry, cgcs2000ToWgs84, computeScaleBar, createTransformer, Map as default, fromWgs84, gcj02ToWgs84, getGlobalConfig, glowPasses, isInChina, isWebGLAvailable, oppositeTheme, packGeoJSONToStore, projectSimple, removeSourceSafe, removeSourcesSafe, renderLegendHtml, resolveLod, setCgcs2000GridShift, setGlobalConfig, suggestDensity, themeFromStyle, toWgs84, transformBounds, transformPoint, triggerDownload, upsertSource, wgs84ToCgcs2000, wgs84ToGcj02 };
package/dist/index.d.ts CHANGED
@@ -268,7 +268,7 @@ declare class ExportControl {
268
268
  interface GlowLine {
269
269
  /** 线坐标 [lng, lat][] */
270
270
  coordinates: [number, number][];
271
- /** 该线所属分组(如 'pipe' | 'road' | 'water'),用于着色 */
271
+ /** 该线所属分组(如 'pipe' | 'road' | 'water'),用于按组分色。缺省归入 'default'。 */
272
272
  group?: string;
273
273
  }
274
274
  interface GlowPass {
@@ -279,11 +279,12 @@ interface GlowPass {
279
279
  }
280
280
  interface GlowGeometry {
281
281
  /**
282
- * 扁平化顶点缓冲。每个顶点 5 个 float:
283
- * [worldX, worldY, dirX, dirY, side]
282
+ * 扁平化顶点缓冲。每个顶点 6 个 float:
283
+ * [worldX, worldY, dirX, dirY, side, groupIndex]
284
284
  * - worldX/worldY:该端点的归一化世界坐标
285
285
  * - dirX/dirY:所在线段的世界空间方向(用于屏幕空间法线)
286
286
  * - side:±1,左/右扩展符号
287
+ * - groupIndex:该顶点所属分组在 `groups` 中的下标(用于按组分色)
287
288
  */
288
289
  vertices: Float32Array;
289
290
  /** 每遍在 vertices 中的顶点区间 [start, count](按顶点数,非 float 数) */
@@ -295,6 +296,15 @@ interface GlowGeometry {
295
296
  passes: GlowPass[];
296
297
  /** 顶点属性跨距(float 数) */
297
298
  stride: number;
299
+ /** 参与渲染的去重分组名(与上层 colorMap 对应,下标即 groupIndex) */
300
+ groups: string[];
301
+ /** 逐遍、逐分组的连续顶点区间,供按组分色绘制 */
302
+ renderGroups: {
303
+ passIndex: number;
304
+ group: string;
305
+ start: number;
306
+ count: number;
307
+ }[];
298
308
  }
299
309
  /**
300
310
  * 生成辉光多遍档位。
@@ -362,9 +372,15 @@ declare class CustomLineLayer implements CustomLayerInterface {
362
372
  private buffer?;
363
373
  private geometry;
364
374
  private map?;
375
+ private gl?;
365
376
  constructor(opts: GlowLayerOptions);
366
377
  onAdd(map: unknown, gl: WebGLRenderingContext): void;
367
378
  render(gl: WebGLRenderingContext, matrix: number[]): void;
379
+ /**
380
+ * 动态更新线集合(高亮选中、切换数据等无需重建图层)。
381
+ * 浏览器环境会刷新 GPU 缓冲;纯逻辑/测试环境仅更新几何与数据。
382
+ */
383
+ setLines(lines: GlowLine[]): void;
368
384
  onRemove(_map: unknown, gl: WebGLRenderingContext): void;
369
385
  }
370
386
 
@@ -516,6 +532,13 @@ interface CaoguoMapGlobalConfig {
516
532
  }
517
533
  declare function setGlobalConfig(cfg: CaoguoMapGlobalConfig): void;
518
534
  declare function getGlobalConfig(): CaoguoMapGlobalConfig;
535
+ /** `addGlowLayer` 返回的句柄:既提供图层 id(用于 removeLayer),也支持动态更新线数据。 */
536
+ interface GlowLayerHandle {
537
+ /** 图层 id,可用于 map.removeLayer(id) */
538
+ id: string;
539
+ /** 动态替换线集合(高亮选中 / 切换数据,无需重建图层) */
540
+ setLines: (lines: GlowLine[]) => void;
541
+ }
519
542
  interface MapOptions {
520
543
  container: string | HTMLElement;
521
544
  center?: [number, number];
@@ -645,7 +668,7 @@ declare class Map {
645
668
  colors?: Record<string, [number, number, number]>;
646
669
  baseWidth?: number;
647
670
  passes?: number;
648
- }): string;
671
+ }): GlowLayerHandle;
649
672
  /**
650
673
  * 挂载 LOD 控制器(T7 / F-1.7)。
651
674
  * 随 zoom 切换数据密度等级,并在等级变化时回调(调用方据此 setData / 切源)。
@@ -675,4 +698,4 @@ declare class Map {
675
698
  get instance(): Map$1;
676
699
  }
677
700
 
678
- export { type Bounds, type CRS, type CaoguoMapGlobalConfig, type CustomLayerInterface, CustomLineLayer, ExportControl, type ExportOptions, type GlowGeometry, type GlowLayerOptions, type GlowLine, type GlowPass, type GridShiftProvider, LegendControl, type LegendItem, type LegendOptions, type LngLat, type LodChangeEvent, LodController, type LodLevel, Map, type MapInstance, type MapOptions, type MlMapSourceApi, type Point, type ScaleBar, ScaleControl, type ScaleControlOptions, ThemeSwitcher, type ThemeSwitcherOptions, TiandituOptions, TiandituType, TileStoreBackend, type Transformer, WebGLUnavailableError, buildGlowGeometry, cgcs2000ToWgs84, computeScaleBar, createTransformer, Map as default, fromWgs84, gcj02ToWgs84, getGlobalConfig, glowPasses, isInChina, isWebGLAvailable, oppositeTheme, packGeoJSONToStore, projectSimple, removeSourceSafe, removeSourcesSafe, renderLegendHtml, resolveLod, setCgcs2000GridShift, setGlobalConfig, suggestDensity, themeFromStyle, toWgs84, transformBounds, transformPoint, triggerDownload, upsertSource, wgs84ToCgcs2000, wgs84ToGcj02 };
701
+ export { type Bounds, type CRS, type CaoguoMapGlobalConfig, type CustomLayerInterface, CustomLineLayer, ExportControl, type ExportOptions, type GlowGeometry, type GlowLayerHandle, type GlowLayerOptions, type GlowLine, type GlowPass, type GridShiftProvider, LegendControl, type LegendItem, type LegendOptions, type LngLat, type LodChangeEvent, LodController, type LodLevel, Map, type MapInstance, type MapOptions, type MlMapSourceApi, type Point, type ScaleBar, ScaleControl, type ScaleControlOptions, ThemeSwitcher, type ThemeSwitcherOptions, TiandituOptions, TiandituType, TileStoreBackend, type Transformer, WebGLUnavailableError, buildGlowGeometry, cgcs2000ToWgs84, computeScaleBar, createTransformer, Map as default, fromWgs84, gcj02ToWgs84, getGlobalConfig, glowPasses, isInChina, isWebGLAvailable, oppositeTheme, packGeoJSONToStore, projectSimple, removeSourceSafe, removeSourcesSafe, renderLegendHtml, resolveLod, setCgcs2000GridShift, setGlobalConfig, suggestDensity, themeFromStyle, toWgs84, transformBounds, transformPoint, triggerDownload, upsertSource, wgs84ToCgcs2000, wgs84ToGcj02 };
package/dist/index.js CHANGED
@@ -359,39 +359,54 @@ function projectSimple(lng, lat) {
359
359
  }
360
360
  function buildGlowGeometry(lines, opts = {}) {
361
361
  const passes = glowPasses(opts.passes ?? 4, opts.baseWidth ?? 3);
362
- const stride = 5;
362
+ const stride = 6;
363
363
  const verts = [];
364
364
  const passRanges = [];
365
- for (const pass of passes) {
365
+ const renderGroups = [];
366
+ const groups = [];
367
+ for (const line of lines) {
368
+ const g = line.group ?? "default";
369
+ if (!groups.includes(g)) groups.push(g);
370
+ }
371
+ for (let p = 0; p < passes.length; p++) {
372
+ passes[p];
366
373
  const start = verts.length / stride;
367
- for (const line of lines) {
368
- const pts = line.coordinates.map(([lng, lat]) => projectSimple(lng, lat));
369
- if (pts.length < 2) continue;
370
- for (let i = 0; i < pts.length - 1; i++) {
371
- const a = pts[i];
372
- const b = pts[i + 1];
373
- const dx = b[0] - a[0];
374
- const dy = b[1] - a[1];
375
- const quad = [
376
- [a[0], a[1], dx, dy, 1],
377
- [a[0], a[1], dx, dy, -1],
378
- [b[0], b[1], dx, dy, 1],
379
- [b[0], b[1], dx, dy, -1]
380
- ];
381
- const idx = [0, 1, 2, 2, 1, 3];
382
- for (const k of idx) {
383
- const v = quad[k];
384
- verts.push(v[0], v[1], v[2], v[3], v[4]);
374
+ for (const group of groups) {
375
+ const groupStart = verts.length / stride;
376
+ const groupIndex = groups.indexOf(group);
377
+ for (const line of lines) {
378
+ if ((line.group ?? "default") !== group) continue;
379
+ const pts = line.coordinates.map(([lng, lat]) => projectSimple(lng, lat));
380
+ if (pts.length < 2) continue;
381
+ for (let i = 0; i < pts.length - 1; i++) {
382
+ const a = pts[i];
383
+ const b = pts[i + 1];
384
+ const dx = b[0] - a[0];
385
+ const dy = b[1] - a[1];
386
+ const quad = [
387
+ [a[0], a[1], dx, dy, 1, groupIndex],
388
+ [a[0], a[1], dx, dy, -1, groupIndex],
389
+ [b[0], b[1], dx, dy, 1, groupIndex],
390
+ [b[0], b[1], dx, dy, -1, groupIndex]
391
+ ];
392
+ const idx = [0, 1, 2, 2, 1, 3];
393
+ for (const k of idx) {
394
+ const v = quad[k];
395
+ verts.push(v[0], v[1], v[2], v[3], v[4], v[5]);
396
+ }
385
397
  }
386
398
  }
399
+ const count2 = verts.length / stride - groupStart;
400
+ if (count2 > 0) renderGroups.push({ passIndex: p, group, start: groupStart, count: count2 });
387
401
  }
388
402
  const count = verts.length / stride - start;
389
403
  passRanges.push({ start, count });
390
404
  }
391
- return { vertices: new Float32Array(verts), passRanges, passes, stride };
405
+ return { vertices: new Float32Array(verts), passRanges, passes, stride, groups, renderGroups };
392
406
  }
393
407
 
394
408
  // src/shaders/CustomLineLayer.ts
409
+ var FALLBACK_COLOR = [0.6, 0.7, 0.9];
395
410
  var DEFAULT_COLORS = {
396
411
  pipe: [0.2, 0.85, 1],
397
412
  // 青蓝:管线
@@ -445,9 +460,13 @@ var CustomLineLayer = class {
445
460
  this.colors = { ...DEFAULT_COLORS, ...opts.colors };
446
461
  this.baseWidth = opts.baseWidth ?? 3;
447
462
  this.geometry = buildGlowGeometry(this.lines, { passes: opts.passes ?? 4, baseWidth: this.baseWidth });
463
+ for (const g of this.geometry.groups) {
464
+ if (!(g in this.colors)) this.colors[g] = FALLBACK_COLOR;
465
+ }
448
466
  }
449
467
  onAdd(map, gl) {
450
468
  this.map = map;
469
+ this.gl = gl;
451
470
  const vs = compile(gl, gl.VERTEX_SHADER, VERT);
452
471
  const fs = compile(gl, gl.FRAGMENT_SHADER, FRAG);
453
472
  const prog = gl.createProgram();
@@ -471,12 +490,17 @@ var CustomLineLayer = class {
471
490
  const aPos = gl.getAttribLocation(this.program, "aPos");
472
491
  const aDir = gl.getAttribLocation(this.program, "aDir");
473
492
  const aSide = gl.getAttribLocation(this.program, "aSide");
493
+ const aGroup = gl.getAttribLocation(this.program, "aGroup");
474
494
  gl.enableVertexAttribArray(aPos);
475
495
  gl.vertexAttribPointer(aPos, 2, gl.FLOAT, false, stride, 0);
476
496
  gl.enableVertexAttribArray(aDir);
477
497
  gl.vertexAttribPointer(aDir, 2, gl.FLOAT, false, stride, 8);
478
498
  gl.enableVertexAttribArray(aSide);
479
499
  gl.vertexAttribPointer(aSide, 1, gl.FLOAT, false, stride, 16);
500
+ if (aGroup >= 0) {
501
+ gl.enableVertexAttribArray(aGroup);
502
+ gl.vertexAttribPointer(aGroup, 1, gl.FLOAT, false, stride, 20);
503
+ }
480
504
  const uMatrix = gl.getUniformLocation(this.program, "uMatrix");
481
505
  gl.uniformMatrix4fv(uMatrix, false, new Float32Array(matrix));
482
506
  const uWidth = gl.getUniformLocation(this.program, "uWidth");
@@ -487,15 +511,31 @@ var CustomLineLayer = class {
487
511
  gl.uniform2f(uRes, canvas?.width ?? 1024, canvas?.height ?? 768);
488
512
  gl.enable(gl.BLEND);
489
513
  gl.blendFunc(gl.SRC_ALPHA, gl.ONE);
490
- const color = this.colors[this.lines[0]?.group ?? "pipe"] ?? this.colors.pipe;
491
- for (let p = 0; p < this.geometry.passes.length; p++) {
492
- const pass = this.geometry.passes[p];
493
- const range = this.geometry.passRanges[p];
494
- if (range.count <= 0) continue;
514
+ for (const rg of this.geometry.renderGroups) {
515
+ const pass = this.geometry.passes[rg.passIndex];
516
+ const color = this.colors[rg.group] ?? FALLBACK_COLOR;
495
517
  gl.uniform1f(uWidth, pass.width);
496
518
  gl.uniform3f(uColor, color[0], color[1], color[2]);
497
519
  gl.uniform1f(uOpacity, pass.opacity);
498
- gl.drawArrays(gl.TRIANGLES, range.start, range.count);
520
+ gl.drawArrays(gl.TRIANGLES, rg.start, rg.count);
521
+ }
522
+ }
523
+ /**
524
+ * 动态更新线集合(高亮选中、切换数据等无需重建图层)。
525
+ * 浏览器环境会刷新 GPU 缓冲;纯逻辑/测试环境仅更新几何与数据。
526
+ */
527
+ setLines(lines) {
528
+ this.lines = lines;
529
+ this.geometry = buildGlowGeometry(this.lines, {
530
+ passes: this.geometry.passes.length,
531
+ baseWidth: this.baseWidth
532
+ });
533
+ for (const g of this.geometry.groups) {
534
+ if (!(g in this.colors)) this.colors[g] = FALLBACK_COLOR;
535
+ }
536
+ if (this.buffer && this.gl) {
537
+ this.gl.bindBuffer(this.gl.ARRAY_BUFFER, this.buffer);
538
+ this.gl.bufferData(this.gl.ARRAY_BUFFER, this.geometry.vertices, this.gl.STATIC_DRAW);
499
539
  }
500
540
  }
501
541
  onRemove(_map, gl) {
@@ -814,7 +854,10 @@ var Map = class {
814
854
  addGlowLayer(opts) {
815
855
  const layer = new CustomLineLayer(opts);
816
856
  this._map.addLayer(layer);
817
- return layer.id;
857
+ return {
858
+ id: layer.id,
859
+ setLines: (lines) => layer.setLines(lines)
860
+ };
818
861
  }
819
862
  /**
820
863
  * 挂载 LOD 控制器(T7 / F-1.7)。