-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathGeoVector.js
More file actions
99 lines (93 loc) · 4.36 KB
/
Copy pathGeoVector.js
File metadata and controls
99 lines (93 loc) · 4.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
class GeoVectorSource extends ol.source.Vector {
constructor(opt_options) {
const options = opt_options || {};
super(options);
// Custom xhr function
if (options.loader !== undefined) {
this.loader_ = options.loader;
} else if (this.url_ !== undefined) {
ol.asserts.assert(this.format_, 7); // `format` must be set when `url` is set
var format = this.getFormat();
// create a XHR feature loader for "url" and "format"
this.loader_ = this.xhr(this.url_, /** @type {import("../format/Feature.js").default} */(this.format_));
}
}
xhr(url, format) {
return this.loadFeaturesXhr(url, format,
/**
* @param {Array<import("./Feature.js").default>} features The loaded features.
* @param {import("./proj/Projection.js").default} dataProjection Data
* projection.
* @this {import("./source/Vector").default|import("./VectorTile.js").default}
*/
function (features, dataProjection) {
const sourceOrTile = /** @type {?} */ (this);
if (typeof sourceOrTile.addFeatures === 'function') {
/** @type {import("./source/Vector").default} */ (sourceOrTile).addFeatures(features);
}
}, /* FIXME handle error */ function() {});
}
loadFeaturesXhr(url, format, success, failure) {
return (
/**
* @param {import("./extent.js").Extent} extent Extent.
* @param {number} resolution Resolution.
* @param {import("./proj/Projection.js").default} projection Projection.
* @this {import("./source/Vector").default|import("./VectorTile.js").default}
*/
function (extent, resolution, projection) {
const xhr = new XMLHttpRequest();
xhr.open('GET',
typeof url === 'function' ? url(extent, resolution, projection) : url,
true);
if (format.getType() == ol.format.FormatType.ARRAY_BUFFER) {
xhr.responseType = 'arraybuffer';
}
/**
* @param {Event} event Event.
* @private
*/
xhr.onload = function (event) {
// status will be 0 for file:// urls
if (!xhr.status || xhr.status >= 200 && xhr.status < 300) {
const type = format.getType();
/** @type {Document|Node|Object|string|undefined} */
let source;
if (type == ol.format.FormatType.JSON || type == ol.format.FormatType.TEXT) {
source = xhr.responseText;
} else if (type == ol.format.FormatType.XML) {
source = xhr.responseXML;
if (!source) {
source = new DOMParser().parseFromString(xhr.responseText, 'application/xml');
}
} else if (type == ol.format.FormatType.ARRAY_BUFFER) {
source = /** @type {ArrayBuffer} */ (xhr.response);
}
if (source) {
let features = [];
if (format.dataProjection !== undefined) {
features = format.readFeatures(source, { featureProjection: projection, dataProjection: format.dataProjection })
}
else {
features = format.readFeatures(source, { featureProjection: projection })
}
success.call(this, features, format.readProjection(source), format.getLastExtent());
} else {
failure.call(this);
}
} else {
failure.call(this);
}
}.bind(this);
/**
* @private
*/
xhr.onerror = function () {
failure.call(this);
}.bind(this);
xhr.send();
}
);
}
}
export default GeoVectorSource;