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

iTowns / itowns / 17292947856

28 Aug 2025 10:21AM UTC coverage: 86.774% (-0.2%) from 86.927%
17292947856

Pull #2593

github

web-flow
Merge 3396d4fd1 into 737a8f3de
Pull Request #2593: Refactoring sources

2792 of 3749 branches covered (74.47%)

Branch coverage included in aggregate %.

459 of 531 new or added lines in 18 files covered. (86.44%)

10 existing lines in 5 files now uncovered.

26155 of 29610 relevant lines covered (88.33%)

1094.86 hits per line

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

95.92
/packages/Main/src/Source/Source.js
1
import { Extent, CRS } from '@itowns/geographic';
1✔
2

1✔
3
/**
1✔
4
 * This interface describes parsing options.
1✔
5
 * @typedef {Object} ParsingOptions
1✔
6
 * @property {Source} in - data informations contained in the file.
1✔
7
 * @property {FeatureBuildingOptions|Layer} out - options indicates how the features should be built.
1✔
8
 */
1✔
9

1✔
10
let uid = 0;
1✔
11

1✔
12
/**
1✔
13
 * Sources are object containing informations on how to fetch resources, from a
1✔
14
 * set source.
1✔
15
 *
1✔
16
 * To extend a Source, it is necessary to implement two functions:
1✔
17
 * `urlFromExtent` and `extentInsideLimit`.
1✔
18
 *
1✔
19
 * @extends InformationsData
1✔
20
 *
1✔
21
 * @property {boolean} isSource - Used to checkout whether this source is a
1✔
22
 * Source. Default is true. You should not change this, as it is used internally
1✔
23
 * for optimisation.
1✔
24
 * @property {number} uid - Unique uid mainly used to store data linked to this
1✔
25
 * source into Cache.
1✔
26
 * @property {string} url - The url of the resources that are fetched.
1✔
27
 * @property {string} format - The format of the resources that are fetched.
1✔
28
 * @property {function} fetcher - The method used to fetch the resources from
1✔
29
 * the source. iTowns provides some methods in {@link Fetcher}, but it can be
1✔
30
 * specified a custom one. This method should return a `Promise` containing the
1✔
31
 * fetched resource. If this property is set, it overrides the chosen fetcher
1✔
32
 * method with `format`.
1✔
33
 * @property {Object} networkOptions - Fetch options (passed directly to
1✔
34
 * `fetch()`), see [the syntax for more information](
1✔
35
 * https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch#Syntax).
1✔
36
 * By default, set to `{ crossOrigin: 'anonymous' }`.
1✔
37
 * @property {string} crs - The crs projection of the resources.
1✔
38
 * @property {string} attribution - The intellectual property rights for the
1✔
39
 * resources.
1✔
40
 * @property {Extent} extent - The extent of the resources.
1✔
41
 * @property {function} parser - The method used to parse the resources attached
1✔
42
 * to the layer. iTowns provides some parsers, visible in the `Parser/` folder.
1✔
43
 * If the method is custom, it should return a `Promise` containing the parsed
1✔
44
 * resource. If this property is set, it overrides the default selected parser
1✔
45
 * method with `source.format`. If `source.format` is also empty, no parsing
1✔
46
 * action is done.
1✔
47
 * <br><br>
1✔
48
 * When calling this method, two parameters are passed:
1✔
49
 * <ul>
1✔
50
 *  <li>the fetched data, i.e. the data to parse</li>
1✔
51
 *  <li>an {@link ParsingOptions}  containing severals properties, set when this method is
1✔
52
 *  called: it is specific to each call, so the value of each property can vary
1✔
53
 *  depending on the current fetched tile for example</li>
1✔
54
 * </ul>
1✔
55
 */
1✔
56
class Source {
1✔
57
    /**
1✔
58
     * @param {Object} source - An object that can contain all properties of a
1✔
59
     * Source. Only the `url` property is mandatory.
1✔
60
     */
1✔
61
    constructor(source = {}) {
1!
62
        if (source.crs) {
132✔
63
            CRS.isValid(source.crs);
45✔
64
        }
45✔
65
        this.crs = source.crs;
132✔
66
        this.isSource = true;
132✔
67
        this.uid = uid++;
132✔
68

132✔
69
        this.attribution = source.attribution;
132✔
70
        /** @type {Promise<any>} */
132✔
71
        this.whenReady = Promise.resolve();
132✔
72
        if (source.extent && !(source.extent.isExtent)) {
132✔
73
            this.extent = new Extent(this.crs).setFromExtent(source.extent);
3✔
74
        } else {
132✔
75
            this.extent = source.extent;
129✔
76
        }
129✔
77
    }
132✔
78

1✔
79
    handlingError(err) {
1✔
80
        throw new Error(err);
2✔
81
    }
2✔
82

1✔
83
    /**
1✔
84
     * Generates an url from an extent. This url is a link to fetch the
1✔
85
     * resources inside the extent.
1✔
86
     *
1✔
87
     * @param {Extent} extent - Extent to convert in url.
1✔
88

1✔
89
     * @return {string} The URL constructed from the extent.
1✔
90
     */
1✔
91
    // eslint-disable-next-line
1✔
92
    urlFromExtent(extent) {
1✔
93
        throw new Error('In extended Source, you have to implement the method urlFromExtent!');
1✔
94
    }
1✔
95

1✔
96
    // eslint-disable-next-line
1✔
97
    getDataKey(extent) {
1✔
NEW
98
        throw new Error('In extended Source, you have to implement the method getDataKey!');
×
UNCOV
99
    }
×
100

1✔
101
    // eslint-disable-next-line
1✔
102
    loadData(extent, out) {
1✔
NEW
103
        throw new Error('In extended Source, you have to implement the method loadData!');
×
UNCOV
104
    }
×
105

1✔
106
    /**
1✔
107
     * Called when layer added.
1✔
108
     *
1✔
109
     * @param {object} options
1✔
110
     */
1✔
111
    // eslint-disable-next-line
1✔
112
    onLayerAdded(options) {}
1✔
113

1✔
114
    /**
1✔
115
     * Called when layer removed.
1✔
116
     *
1✔
117
     * @param {options}  [options={}] options
1✔
118
     */
1✔
119
    // eslint-disable-next-line
1✔
120
    onLayerRemoved(options = {}) {}
1!
121

1✔
122
    /**
1✔
123
     * Tests if an extent is inside the source limits.
1✔
124
     *
1✔
125
     * @param {Extent} extent - Extent to test.
1✔
126

1✔
127
     * @return {boolean} True if the extent is inside the limit, false otherwise.
1✔
128
     */
1✔
129
    // eslint-disable-next-line
1✔
130
    extentInsideLimit(extent) {
1✔
131
        throw new Error('In extented Source, you have to implement the method extentInsideLimit!');
1✔
132
    }
1✔
133
}
1✔
134

1✔
135
export default Source;
1✔
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