• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Open-S2 / open-vector-tile / #6

26 Jun 2024 06:18PM UTC coverage: 100.0%. Remained the same
#6

push

Mr Martian
include dependecy graph

2362 of 2362 relevant lines covered (100.0%)

106.23 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/src/base/vectorLayer.ts
1
import MapboxVectorLayer from '../mapbox/vectorLayer';
2
import { fromMapboxVectorFeature } from './vectorFeature';
224✔
3
import { updateShapeFromData } from '../open/shape';
198✔
4

5
import type { BaseVectorFeature } from './vectorFeature';
6
import type { Shape } from '../open/shape';
7

8
/**
9
 * Base Vector Layer
10
 * This is an intermediary for storing layer data in the Open Vector Tile format.
11
 */
12
export default class BaseVectorLayer {
13
  // if the shape was already passed in to the constructor
14
  #shapeDefined: boolean = false;
15
  // if the M-Shape was already passed in to the constructor
16
  #mShapeDefined: boolean = false;
17
  // The shape used to describe the features properties in the layer
18
  shape: Shape;
2✔
19
  /**
20
   * @param version - the version of the vector tile. This is a number that tracks the OVT specification. and shouldn't be tampered with
21
   * @param name - the name of the layer
22
   * @param extent - the extent of the vector tile (only **512**, **1_024**, **2_048**, **4_096**, and **8_192** are supported)
23
   * @param features - the **Base Vector Feature**s stored in the layer.
24
   * @param shape - the shape of each feature properies
25
   * @param mShape - the shape of each feature's M-Values
26
   */
27
  constructor(
26✔
28
    public version: number = 1,
160✔
29
    public name: string = '',
128✔
30
    public extent: number = 4096,
160✔
31
    public features: BaseVectorFeature[] = [],
176✔
32
    shape?: Shape,
28✔
33
    public mShape?: Shape,
132✔
34
  ) {
8✔
35
    if (shape !== undefined) {
114✔
36
      this.shape = shape;
50✔
37
      this.#shapeDefined = true;
70✔
38
    } else this.shape = {};
112✔
39
    if (mShape !== undefined) {
118✔
40
      this.mShape = mShape;
54✔
41
      this.#mShapeDefined = true;
72✔
42
    } else this.mShape = {};
128✔
43
  }
44

45
  /**
46
   * Add a new feature to the layer
47
   * @param feature - the new feature to add
48
   */
49
  addFeature(feature: BaseVectorFeature): void {
68✔
50
    this.features.push(feature);
128✔
51
    if (!this.#shapeDefined) updateShapeFromData(this.shape, feature.properties);
340✔
52
    if (!this.#mShapeDefined) {
122✔
53
      const mValues = feature.getMValues();
172✔
54
      if (mValues !== undefined) {
130✔
55
        if (this.mShape === undefined) this.mShape = {};
130✔
56
        for (const mv of mValues) updateShapeFromData(this.mShape, mv);
170✔
57
      }
18✔
58
    }
18✔
59
  }
60

61
  /**
62
   * @param i - index of the feature to return
63
   * @returns A base vector feature at the given index
64
   */
65
  feature(i: number): BaseVectorFeature {
38✔
66
    if (i < 0 || i >= this.features.length) throw new Error('feature index out of bounds');
288✔
67
    return this.features[i];
126✔
68
  }
69

70
  /**
71
   * @returns The number of features in the layer
72
   */
73
  get length(): number {
32✔
74
    return this.features.length;
162✔
75
  }
76

77
  /**
78
   * @param layer - a Mapbox Vector Layer
79
   * @returns A Base Vector Layer
80
   */
81
  static fromMapboxVectorLayer(layer: MapboxVectorLayer): BaseVectorLayer {
82✔
82
    const vectorLayer = new BaseVectorLayer();
176✔
83
    vectorLayer.version = layer.version;
160✔
84
    vectorLayer.name = layer.name;
136✔
85
    vectorLayer.extent = layer.extent;
152✔
86
    for (let i = 0; i < layer.length; i++) {
170✔
87
      const bFeature = fromMapboxVectorFeature(layer.feature(i));
260✔
88
      vectorLayer.addFeature(bFeature);
168✔
89
    }
6✔
90
    return vectorLayer;
96✔
91
  }
92
}
4✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc