-
Notifications
You must be signed in to change notification settings - Fork 88
/
ArcGISGeoJSON.js
110 lines (100 loc) · 4.17 KB
/
ArcGISGeoJSON.js
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
100
101
102
103
104
105
106
107
108
109
110
// The MIT License (MIT)
//
// Copyright (c) 2019-2021 Camptocamp SA
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
import {includes as olArrayIncludes} from 'ol/array.js';
import olFormatGeoJSON from 'ol/format/GeoJSON.js';
/**
* @typedef {Object} Options
* // Properties for ArcGISGeoJSON
* @property {Array<string>} [layers] If set, only features of the given layers will be returned by the format when read.
* // Properties from GeoJSON
* @property {import("ol/proj.js").ProjectionLike} [dataProjection='EPSG:4326'] Default data projection.
* @property {import("ol/proj.js").ProjectionLike} [featureProjection] Projection for features read or
* written by the format. Options passed to read or write methods will take precedence.
* @property {string} [geometryName] Geometry name to use when creating features.
* @property {boolean} [extractGeometryName=false] Certain GeoJSON providers include
* the geometry_name field in the feature GeoJSON. If set to `true` the GeoJSON reader
* will look for that field to set the geometry name. If both this field is set to `true`
* and a `geometryName` is provided, the `geometryName` will take precedence.
*/
/**
* @const
* @type {string}
*/
const layerIdentifier = 'layerName';
class ArcGISGeoJSON extends olFormatGeoJSON {
/**
* @param {Options=} opt_options Options.
*/
constructor(opt_options) {
const options = opt_options ? opt_options : {};
super(opt_options);
/**
* @private
* @type {Array<string>}
*/
this.layers_ = options.layers ? options.layers : null;
}
/**
* @return {Array<string>} layers
*/
getLayers() {
return this.layers_;
}
/**
* @param {Array<string>} layers Layers to parse.
*/
setLayers(layers) {
this.layers_ = layers;
}
/**
* @param {import("geojson").GeoJSON} object Object.
* @param {import("ol/format/Feature.js").ReadOptions=} opt_options Read options.
* @protected
* @return {Array<import('ol/Feature.js').default<import("ol/geom/Geometry.js").default>>} Features.
* @override
*/
readFeaturesFromObject(object, opt_options) {
const geoJSONObject = /** @type {import("geojson").GeoJSON} */ (object);
/** @type {Array<import('ol/Feature.js').default<import("ol/geom/Geometry.js").default>>} */
const features = [];
/** @type {import("geojson").Feature[]} */
let geoJSONFeatures = null;
if (geoJSONObject.type === 'FeatureCollection') {
const geoJSONFeatureCollection = /** @type {import("geojson").FeatureCollection} */ (object);
geoJSONFeatures = geoJSONFeatureCollection.features;
} else {
geoJSONFeatures = [/** @type {import("geojson").Feature} */ (object)];
}
for (let i = 0, ii = geoJSONFeatures.length; i < ii; ++i) {
const feature = geoJSONFeatures[i];
/** @type {string} */
// @ts-ignore: Arcgis specific
const layerName = feature[layerIdentifier];
// Exclude feature if its layer name is not set among the layers
if (this.layers_ && !olArrayIncludes(this.layers_, layerName)) {
continue;
}
features.push(this.readFeatureFromObject(geoJSONFeatures[i], opt_options));
}
return features;
}
}
export default ArcGISGeoJSON;