@masterportal/masterportalapi 2.1.1

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.
Files changed (92) hide show
  1. package/.editorconfig +15 -0
  2. package/.eslintignore +6 -0
  3. package/.eslintrc +184 -0
  4. package/.husky/pre-push +4 -0
  5. package/CHANGELOG.md +217 -0
  6. package/License.txt +21 -0
  7. package/README.md +24 -0
  8. package/babel.config.json +6 -0
  9. package/example/README.md +7 -0
  10. package/example/config/localGeoJSON.js +956 -0
  11. package/example/config/portal.json +35 -0
  12. package/example/config/services.json +79 -0
  13. package/example/index.html +27 -0
  14. package/example/index.js +210 -0
  15. package/example/style.scss +59 -0
  16. package/jest.config.js +18 -0
  17. package/jest.setup.js +5 -0
  18. package/jsdoc.json +15 -0
  19. package/package.json +62 -0
  20. package/public/marker.svg +1 -0
  21. package/public/stringMarker.js +5 -0
  22. package/src/crs.js +122 -0
  23. package/src/defaults.js +38 -0
  24. package/src/index.js +30 -0
  25. package/src/layer/geojson/index.js +109 -0
  26. package/src/layer/geojson/style.js +61 -0
  27. package/src/layer/lib.js +32 -0
  28. package/src/layer/oaf.js +207 -0
  29. package/src/layer/terrain.js +72 -0
  30. package/src/layer/tileset.js +71 -0
  31. package/src/layer/vector.js +48 -0
  32. package/src/layer/vectorBase.js +39 -0
  33. package/src/layer/wfs.js +218 -0
  34. package/src/layer/wms.js +185 -0
  35. package/src/lib/coordsToPairs.js +15 -0
  36. package/src/lib/getInitialLayers.js +22 -0
  37. package/src/lib/load3DScript.js +22 -0
  38. package/src/lib/oafUtil.js +22 -0
  39. package/src/lib/setBackgroundImage.js +23 -0
  40. package/src/lib/wfsUtil.js +75 -0
  41. package/src/lib/zoomTo.js +40 -0
  42. package/src/maps/api.js +5 -0
  43. package/src/maps/map.js +35 -0
  44. package/src/maps/mapView.js +54 -0
  45. package/src/maps/ol/olMap.js +124 -0
  46. package/src/maps/olcs/3dUtils/fixedOverlaySynchronizer.js +39 -0
  47. package/src/maps/olcs/3dUtils/wmsRasterSynchronizer.js +348 -0
  48. package/src/maps/olcs/olcsMap.js +212 -0
  49. package/src/rawLayerList.js +88 -0
  50. package/src/searchAddress/gazetteerUrl.js +22 -0
  51. package/src/searchAddress/index.js +13 -0
  52. package/src/searchAddress/parse.js +196 -0
  53. package/src/searchAddress/search.js +184 -0
  54. package/src/searchAddress/searchGazetteer.js +52 -0
  55. package/src/searchAddress/showGeographicIdentifier.js +20 -0
  56. package/src/searchAddress/types.js +17 -0
  57. package/test/.eslintrc +7 -0
  58. package/test/crs.test.js +109 -0
  59. package/test/layer/geojson/index.test.js +133 -0
  60. package/test/layer/geojson/style.test.js +46 -0
  61. package/test/layer/lib.test.js +83 -0
  62. package/test/layer/oaf.test.js +245 -0
  63. package/test/layer/resources/oafFeatures.js +105 -0
  64. package/test/layer/resources/wfsFeatures.js +26 -0
  65. package/test/layer/resources/wfsFilter.js +17 -0
  66. package/test/layer/terrain.test.js +106 -0
  67. package/test/layer/tileset.test.js +116 -0
  68. package/test/layer/vector.test.js +76 -0
  69. package/test/layer/vectorBase.test.js +48 -0
  70. package/test/layer/wfs.test.js +388 -0
  71. package/test/layer/wms.test.js +256 -0
  72. package/test/lib/coordsToPairs.test.js +9 -0
  73. package/test/lib/getInitialLayers.test.js +28 -0
  74. package/test/lib/oafUtil.test.js +24 -0
  75. package/test/lib/setBackgroundImage.test.js +49 -0
  76. package/test/lib/wfsUtil.test.js +28 -0
  77. package/test/lib/zoomTo.test.js +74 -0
  78. package/test/map.test.js +258 -0
  79. package/test/mapView.test.js +59 -0
  80. package/test/rawLayerList.test.js +48 -0
  81. package/test/searchAddress/gazetteerSearchResults/address.js +175 -0
  82. package/test/searchAddress/gazetteerSearchResults/district.js +71 -0
  83. package/test/searchAddress/gazetteerSearchResults/houseNumber.js +32 -0
  84. package/test/searchAddress/gazetteerSearchResults/index.js +19 -0
  85. package/test/searchAddress/gazetteerSearchResults/parcel.js +44 -0
  86. package/test/searchAddress/gazetteerSearchResults/street.js +46 -0
  87. package/test/searchAddress/gazetteerSearchResults/streetKey.js +51 -0
  88. package/test/searchAddress/gazetteerUrl.test.js +22 -0
  89. package/test/searchAddress/parse.test.js +110 -0
  90. package/test/searchAddress/search.test.js +150 -0
  91. package/test/searchAddress/searchGazetteer.test.js +58 -0
  92. package/test/searchAddress/showGeographicIdentifier.test.js +22 -0
@@ -0,0 +1,39 @@
1
+
2
+ import VectorLayer from "ol/layer/Vector.js";
3
+ import VectorSource from "ol/source/Vector.js";
4
+
5
+ /**
6
+ * Triggered by Layer to create a layerSource
7
+ * @returns {void}
8
+ */
9
+ export function createLayerSource () {
10
+ return new VectorSource();
11
+ }
12
+
13
+ /**
14
+ * updates the source
15
+ * @param {module:ol/layer/Base~BaseLayer} layer The vector base layer that is to be updated.
16
+ * @param {module:ol/Feature~Feature[]} features The ol features.
17
+ * @returns {void}
18
+ */
19
+ export function updateSource (layer, features) {
20
+ layer.getSource().clear(true);
21
+ layer.getSource().addFeatures(features);
22
+ }
23
+
24
+ /**
25
+ * creates an vector Base
26
+ * @param {Object} attrs attributes of the layer
27
+ * @returns {Object} layer
28
+ */
29
+ export function createLayer (attrs) {
30
+ const source = this.createLayerSource();
31
+
32
+ return new VectorLayer({
33
+ source: source,
34
+ name: attrs.name,
35
+ typ: attrs.typ,
36
+ gfiAttributes: attrs.gfiAttributes,
37
+ id: attrs.id
38
+ });
39
+ }
@@ -0,0 +1,218 @@
1
+ import VectorLayer from "ol/layer/Vector.js";
2
+ import {bbox} from "ol/loadingstrategy.js";
3
+ import {WFS} from "ol/format.js";
4
+
5
+ import {createVectorSource, createClusterVectorSource} from "./vector";
6
+ import {getVersion, onError, onLoad} from "../lib/wfsUtil";
7
+
8
+ /**
9
+ * Layer specification as in services.json.
10
+ * @typedef {Object} rawLayer
11
+ * @property {string} id - id of the layer
12
+ * @property {string} url - url to load features from
13
+ * @property {string} featureType - type of features
14
+ * @property {string} featureNS - the namespace URI used for features
15
+ * @property {number} clusterDistance - pixel radius, within this radius, all features are "clustered" into one feature - if available, a cluster source is created.
16
+ * @property {function} style - style function to style the layer if options.style is not set
17
+ */
18
+
19
+ /**
20
+ * Additional options used to create and load the layer source.
21
+ * @typedef {Object} options
22
+ * @property {module:ol.source.Vector~LoadingStrategy} loadingStrategy - the loading strategy to use, if not set 'bbox' is used
23
+ * @property {string} version - version of WFS requests. If not set, '1.1.0' is used.
24
+ * @property {function} onLoadingError - function called on loading error, gets parameter error
25
+ * @property {function} beforeLoading - function called before loading
26
+ * @property {function} afterLoading - function called after loading, gets parameter features
27
+ * @property {function} featuresFilter - function called after loading to filter features, gets parameter features
28
+ * @property {function} wfsFilter - xml resource as wfs filter, the content of the filter file will be sent to the wfs server as POST request
29
+ * @property {function} doNotLoadInitially - default false, if set to true the layer will be initialized without network call, an empty array of features is promoted - this is useful to add features later on (e.g. by filtering) to a full functional but initially empty layer
30
+ * @property {function} clusterGeometryFunction - used in cluster source. Returns the geometry of the cluster, gets parameter feature
31
+ * @property {function} style - style function to style the layer
32
+ * @property {object} loadingParams - added as params to url
33
+ */
34
+
35
+ /**
36
+ * @param {object} response of the request
37
+ * @param {function} onErrorFn Calls options.onLoadingError and 'featuresloaderror' event will be fired by using failure callback.
38
+ * @param {options} [options] additional options
39
+ * @param {function} failure failure callback to ol.VectorLayer, fires 'featuresloaderror' event
40
+ * @returns {function | object} Returns the 'onError' function if response is not ok, otherwise the response object.
41
+ */
42
+ function handleErrors (response, onErrorFn, options, failure) {
43
+ if (!response.ok) {
44
+ return onErrorFn(`Request to wfs-filter failed. Response status is ${response.status}`, options, failure);
45
+ }
46
+ return response;
47
+ }
48
+
49
+ /**
50
+ * Returns the parameters for the wfs filter request.
51
+ * @param {object} payload to set as body
52
+ * @returns {object} the parameters for the wfs filter request
53
+ */
54
+ function getFilterRequestParams (payload) {
55
+ return {
56
+ method: "POST",
57
+ headers: {
58
+ "Content-Type": "text/xml"
59
+ },
60
+ body: payload
61
+ };
62
+ }
63
+ /**
64
+ * Loads the WFS.
65
+ * Filters the features after load, if options.featuresFilter is given.
66
+ * Adds the features to the source.
67
+ * Errors are thrown through onErrorFn or if a network issue occurs.
68
+ *
69
+ * @param {string} url url to load features from
70
+ * @param {object} params extra loading parameters
71
+ * @param {ol.source.VectorSource} source the vector source
72
+ * @param {object} errorAndSuccessFns functions to handle error and success
73
+ * @param {function} errorAndSuccessFns.onErrorFn Calls options.onLoadingError and 'featuresloaderror' event will be fired by using failure callback.
74
+ * @param {function} errorAndSuccessFns.success callback: 'featuresloadend' event will be fired
75
+ * @param {function} errorAndSuccessFns.failure failure callback to ol.VectorLayer, fires 'featuresloaderror' event
76
+ * @param {options} [options] optional additional options
77
+ * @returns {void}
78
+ */
79
+ function loadWFS (url, params, source, {onErrorFn, success, failure}, options) {
80
+ fetch(url, params)
81
+ .then((response) => handleErrors(response, onErrorFn, options, failure))
82
+ .then((response) => response.text())
83
+ .then(responseString => source.getFormat().readFeatures(responseString))
84
+ .then(features => onLoad(source, features, onErrorFn, success, failure, options))
85
+ .catch((error) => {
86
+ console.error(error);
87
+ });
88
+ }
89
+ /**
90
+ * Loads the WFS filter. The content of the filter file will be sent to the wfs server as POST request.
91
+ * Filters the features after load, if options.featuresFilter is given.
92
+ * Adds the features to the source.
93
+ * Errors are thrown through onErrorFn or if a network issue occurs.
94
+ *
95
+ * @param {string} filter xml resource as wfs filter
96
+ * @param {string} url url to load features from
97
+ * @param {ol.source.VectorSource} source the vector source
98
+ * @param {object} errorAndSuccessFns functions to handle error and success
99
+ * @param {function} errorAndSuccessFns.onErrorFn Calls options.onLoadingError and 'featuresloaderror' event will be fired by using failure callback.
100
+ * @param {function} errorAndSuccessFns.success callback: 'featuresloadend' event will be fired
101
+ * @param {function} errorAndSuccessFns.failure failure callback to ol.VectorLayer, fires 'featuresloaderror' event
102
+ * @param {options} [options] optional additional options
103
+ * @returns {void}
104
+ */
105
+ function loadWFSFilter (filter, url, source, {onErrorFn, success, failure}, options) {
106
+ fetch(filter)
107
+ .then((response) => handleErrors(response, onErrorFn, options, failure))
108
+ .then((response) => response.text())
109
+ .then((payload) => loadWFS(url, getFilterRequestParams(payload), source, {onErrorFn, success, failure}, options))
110
+ .catch((error) => {
111
+ console.error(error);
112
+ });
113
+ }
114
+
115
+ /**
116
+ * Creates the url for request wfs.
117
+ * A distinction is made between the wfs versions.
118
+ * @param {rawLayer} rawLayer layer specification as in services.json
119
+ * @param {String} version version of wfs
120
+ * @param {String} projection projection of wfs
121
+ * @param {String} bboxParam bbox loading strategy
122
+ * @returns {string} the url
123
+ */
124
+ function createUrl (rawLayer, version, projection, bboxParam) {
125
+ let url = `${rawLayer.url}?service=WFS&version=${version}&request=GetFeature&srsName=${projection.getCode()}${bboxParam}`;
126
+
127
+ if (version === "1.0.0" || version === "1.1.0") {
128
+ url = `${url}&typeName=${rawLayer.featureType}`;
129
+ }
130
+ else if (version === "2.0.0") {
131
+ url = `${url}&typeNames=${rawLayer.featureType}`;
132
+ }
133
+
134
+ return url;
135
+ }
136
+
137
+ /**
138
+ * Creates an ol/source element for the rawLayer by using a loader.
139
+ * The 'featuresloadend' and 'featuresloaderror' events will be fired by using success and failure callbacks of the loader.
140
+ * @param {rawLayer} rawLayer layer specification as in services.json
141
+ * @param {options} [options] additional options
142
+ * {@link https://openlayers.org/en/latest/apidoc/module-ol_source_Vector-VectorSource.html failure/success see}
143
+ * @returns {(ol.source.VectorSource|ol.source.Cluster)} VectorSource or Cluster, depending on whether clusterDistance is set.
144
+ */
145
+ export function createLayerSource (rawLayer, options = {}) {
146
+ if (!options.loadingStrategy) {
147
+ options.loadingStrategy = bbox;
148
+ }
149
+
150
+ const version = getVersion(rawLayer, options),
151
+ format = new WFS({
152
+ featureNS: rawLayer.featureNS,
153
+ version: version
154
+ });
155
+ let source = null;
156
+
157
+ function loader (extent, resolution, projection, success, failure) {
158
+ if (options.doNotLoadInitially) {
159
+ onLoad(source, [], onError, success, failure, options);
160
+ }
161
+ else if (options.wfsFilter) {
162
+ loadWFSFilter(options.wfsFilter, rawLayer.url, source, {onErrorFn: onError, success, failure}, options);
163
+ }
164
+ else {
165
+ const bboxParam = options.loadingStrategy === bbox ? `&bbox=${extent.join(",")},${projection.getCode()}` : "";
166
+ let url = createUrl(rawLayer, version, projection, bboxParam);
167
+
168
+ if (options.loadingParams) {
169
+ for (const key in options.loadingParams) {
170
+ const option = Array.isArray(options.loadingParams[key]) ? options.loadingParams[key].join(",") : options.loadingParams[key];
171
+
172
+ if (option !== undefined && key === "bbox") {
173
+ url += `&${key}=${option},${projection.getCode()}`;
174
+ }
175
+ else if (option !== undefined) {
176
+ url += `&${key}=${option}`;
177
+ }
178
+ }
179
+ }
180
+ loadWFS(url, {}, source, {onErrorFn: onError, success, failure}, options);
181
+ }
182
+ }
183
+ format.featureType = rawLayer.featureType;
184
+ source = createVectorSource(loader, options.loadingStrategy, format);
185
+
186
+ if (options.beforeLoading) {
187
+ source.once("featuresloadstart", () => options.beforeLoading());
188
+ }
189
+
190
+ if (rawLayer.clusterDistance) {
191
+ return createClusterVectorSource(source, rawLayer.clusterDistance, options.clusterGeometryFunction);
192
+ }
193
+ return source;
194
+ }
195
+
196
+ /**
197
+ * Creates complete ol/Layer from rawLayer containing all required children.
198
+ * @param {rawLayer} rawLayer - layer specification as in services.json
199
+ * @param {object} [optionalParams] - optional params
200
+ * @param {object} [optionalParams.layerParams] - additional layer params
201
+ * @param {options} [optionalParams.options] - additional options
202
+ * @returns {ol.Layer} Layer that can be added to map.
203
+ */
204
+ export function createLayer (rawLayer = {}, {layerParams = {}, options = {}} = {}) {
205
+ const source = createLayerSource(rawLayer, options),
206
+ layer = new VectorLayer(Object.assign({
207
+ source,
208
+ id: rawLayer.id
209
+ }, layerParams));
210
+
211
+ if (options.style) {
212
+ layer.setStyle(options.style);
213
+ }
214
+ else if (rawLayer.style) {
215
+ layer.setStyle(rawLayer.style);
216
+ }
217
+ return layer;
218
+ }
@@ -0,0 +1,185 @@
1
+ import TileLayer from "ol/layer/Tile";
2
+ import ImageLayer from "ol/layer/Image";
3
+ import TileWMS from "ol/source/TileWMS.js";
4
+ import TileGrid from "ol/tilegrid/TileGrid";
5
+ import ImageWMS from "ol/source/ImageWMS.js";
6
+ import {get as getProjection} from "ol/proj";
7
+
8
+ import {getLayerWhere} from "../rawLayerList";
9
+
10
+ /** @returns {number} random session id in range (0, 9999999) */
11
+ export function generateSessionId () {
12
+ return Math.floor(Math.random() * 9999999);
13
+ }
14
+
15
+ /**
16
+ * Adds optional wms parameters to the given parameters.
17
+ * @param {object} params - the mandatory params of wms request url
18
+ * @param {object} rawLayer - layer specification as in services.json
19
+ * @param {boolean} [rawLayer.STYLES] - styles of the layer, if available
20
+ * @param {boolean} [rawLayer.TIME] - time attribute, if available
21
+ * @param {boolean} [rawLayer.CRS] - projection code, if available, else projection of map is used
22
+ * @returns {object} maps query extended parameter names to values
23
+ */
24
+ export function addOptionalParams (params, rawLayer) {
25
+ const extendedParams = {...params},
26
+ rawLayerParams = {},
27
+ optionalParams = [
28
+ "STYLES",
29
+ "TIME",
30
+ "CRS"
31
+ ];
32
+
33
+ Object.keys(rawLayer).forEach(key => {
34
+ rawLayerParams[key.toUpperCase()] = rawLayer[key];
35
+ });
36
+
37
+ optionalParams.forEach(optionalParam => {
38
+ if (rawLayer[optionalParam]) {
39
+ extendedParams[optionalParam] = rawLayer[optionalParam];
40
+ }
41
+ });
42
+
43
+ return extendedParams;
44
+ }
45
+
46
+ /**
47
+ * Creates query parameters for webservice requests from rawLayer.
48
+ * @param {object} rawLayer - layer specification as in services.json
49
+ * @param {string} [rawLayer.format="image/png"] - format of images requested
50
+ * @param {string} rawLayer.layers - comma-separated list of requested layers
51
+ * @param {version} rawLayer.version - webservice version as string, e.g. "1.1.1"
52
+ * @param {boolean} rawLayer.transparent - whether tiles from this service should have transparency where no information is available
53
+ * @param {boolean} rawLayer.singleTile - whether only one tile shall be requested that fills the whole view
54
+ * @param {boolean} rawLayer.STYLES - styles of the layer, if available
55
+ * @param {(string|number)} rawLayer.tilesize - if singleTile is true, this is the requested tilesize
56
+ * @returns {object} maps query parameter names to values
57
+ */
58
+ export function makeParams (rawLayer) {
59
+ let params = {};
60
+
61
+ params = Object.assign({
62
+ CACHEID: generateSessionId(),
63
+ FORMAT: rawLayer.format || "image/png",
64
+ LAYERS: rawLayer.layers,
65
+ VERSION: rawLayer.version,
66
+ TRANSPARENT: rawLayer.transparent,
67
+ SINGLETILE: rawLayer.singleTile
68
+ }, rawLayer.singleTile ? {} : {WIDTH: rawLayer.tilesize, HEIGHT: rawLayer.tilesize});
69
+
70
+ params = addOptionalParams(params, rawLayer);
71
+
72
+ return params;
73
+ }
74
+
75
+ /**
76
+ * Creates an ol/source element for the rawLayer.
77
+ * @param {object} rawLayer - layer specification as in services.json
78
+ * @param {string} [rawLayer.url] - WMS service URL
79
+ * @param {string} [rawLayer.serverType] - optional servertype definition: "geoserver" or "mapserver" or "qgis"
80
+ * @param {string} [rawLayer.gutter] - optional the size in pixels of the gutter around image tiles to ignore
81
+ * @param {function} [rawLayer.olAttribution] - optional function that takes a module:ol/PluggableMap~FrameState and returns a string or an array of strings representing source attributions
82
+ * @param {string|number} [rawLayer.tilesize] - optional needed to create the tileGrid
83
+ * @param {object} options - optional resolutions and origin to create the TileGrid
84
+ * @param {Array} [options.resolutions] - optional resolutions to create the TileGrid, must be in descending order
85
+ * @param {Array} [options.origin] - optional origin to create the TileGrid
86
+ * @param {string} [options.crs] - optional projection code, if not available, projection of map is used
87
+ * @returns {(ol.source.TileWMS|ol.source.ImageWMS)} TileWMS or ImageWMS, depending on whether singleTile is true.
88
+ */
89
+ export function createLayerSource (rawLayer, options) {
90
+ const params = makeParams(rawLayer),
91
+ projection = rawLayer.crs ? getProjection(rawLayer.crs) : undefined;
92
+ let tileGrid = null;
93
+
94
+ if (rawLayer.singleTile) {
95
+ return new ImageWMS({
96
+ url: rawLayer.url,
97
+ params,
98
+ serverType: rawLayer.serverType,
99
+ projection: projection,
100
+ attributions: rawLayer.olAttribution
101
+ });
102
+ }
103
+ if (options && options.resolutions) {
104
+ tileGrid = new TileGrid({
105
+ resolutions: options.resolutions,
106
+ origin: options.origin ? options.origin : undefined,
107
+ tileSize: parseInt(rawLayer.tilesize, 10)
108
+ });
109
+ }
110
+ return new TileWMS({
111
+ url: rawLayer.url,
112
+ params,
113
+ gutter: rawLayer.gutter || 0,
114
+ tileGrid: tileGrid,
115
+ projection: projection,
116
+ attributions: rawLayer.olAttribution
117
+ });
118
+ }
119
+
120
+ /**
121
+ * Creates complete ol/Layer from rawLayer containing source and all required children.
122
+ * @param {object} rawLayer - layer specification as in services.json
123
+ * @param {string} [rawLayer.id] - optional id of the layer, passed to help identification in services.json
124
+ * @param {string} [rawLayer.url] - WMS service URL
125
+ * @param {string} [rawLayer.minScale] - optional the minimal resolution
126
+ * @param {string} [rawLayer.maxScale] - optional the maximal resolution
127
+ * @param {string} [rawLayer.serverType] - optional servertype definition: "geoserver" or "mapserver" or "qgis"
128
+ * @param {string} [rawLayer.gutter] - optional the size in pixels of the gutter around image tiles to ignore
129
+ * @param {function} [rawLayer.olAttribution] - optional function that takes a module:ol/PluggableMap~FrameState and returns a string or an array of strings representing source attributions
130
+ * @param {string|number} [rawLayer.tilesize] - optional needed to create the tileGrid
131
+ * @param {object} layerParams - additional layer params
132
+ * @param {object} options - optional resolutions and origin to create the TileGrid
133
+ * @param {Array} [options.resolutions] - optional resolutions to create the TileGrid, must be in descending order
134
+ * @param {Array} [options.origin] - optional origin to create the TileGrid
135
+ * @returns {ol.Layer} Layer that can be added to map.
136
+ */
137
+ export function createLayer (rawLayer, layerParams = {}, options) {
138
+ const source = createLayerSource(rawLayer, options),
139
+ Layer = rawLayer.singleTile ? ImageLayer : TileLayer;
140
+
141
+ return new Layer(Object.assign({
142
+ source,
143
+ minResolution: rawLayer.minScale,
144
+ maxResolution: rawLayer.maxScale,
145
+ id: rawLayer.id
146
+ }, layerParams));
147
+ }
148
+
149
+ /**
150
+ * Forces an update by giving a layer a new sessionID.
151
+ * @param {ol.Layer} layer - the layer that is to be updated
152
+ * @returns {number} the new sessionID
153
+ */
154
+ export function updateSource (layer) {
155
+ const oldSessionId = layer.getSource().getParams().SESSIONID;
156
+ let newSessionId = oldSessionId;
157
+
158
+ // to avoid rolling the same ID again; never happens except in your presentation
159
+ while (oldSessionId === newSessionId) {
160
+ newSessionId = generateSessionId();
161
+ }
162
+
163
+ layer.getSource().updateParams({SESSIONID: newSessionId});
164
+ return newSessionId;
165
+ }
166
+
167
+ /**
168
+ * Creates the gfiURL from clicked layer, map, and coordinate.
169
+ * @param {ol.Layer} layer - what to get the gfiURL for
170
+ * @param {ol.Map} map - needed for resolution/projection
171
+ * @param {ol.coordinate} coordinate - which point to get the gfiURL for
172
+ * @returns {(string|undefined)} the gfiURL, or undefined if it could not be constructed
173
+ */
174
+ export function getGfiURL (layer, map, coordinate) {
175
+ const rawLayer = getLayerWhere({Id: layer.get("id")}),
176
+ resolution = map.getView().getResolution(),
177
+ projection = map.getView().getProjection(),
178
+ params = Object.assign({
179
+ INFO_FORMAT: (rawLayer && rawLayer.infoFormat) || "text/xml"
180
+ }, rawLayer && typeof rawLayer.featureCount !== "undefined"
181
+ ? {FEATURE_COUNT: rawLayer.featureCount}
182
+ : {});
183
+
184
+ return layer.getSource().getFeatureInfoUrl(coordinate, resolution, projection, params);
185
+ }
@@ -0,0 +1,15 @@
1
+ /**
2
+ * Parses stramed coordinates to pairs of coordinates.
3
+ * @param {(string[]|number[])} coordinates - flat coordinates where two following belong together
4
+ * @returns {Array.<number[]>} array of coordinate pairs
5
+ */
6
+ export default function coordsToPairs (coordinates) {
7
+ const floatCoords = coordinates.map(parseFloat),
8
+ result = [];
9
+
10
+ while (floatCoords.length) {
11
+ result.push(floatCoords.splice(0, 2));
12
+ }
13
+
14
+ return result;
15
+ }
@@ -0,0 +1,22 @@
1
+ import defaults from "../defaults";
2
+
3
+ /**
4
+ * Returns the layers to be initialized as soon as services are known.
5
+ * @param {object} config - configuration object
6
+ * @returns {object[]} array of layer initialization objects
7
+ * @ignore
8
+ */
9
+ export default function (config) {
10
+ // user specified what to do => use that
11
+ if (Array.isArray(config.layers)) {
12
+ return config.layers;
13
+ }
14
+
15
+ // user didn't specify, layerConf is lgv services => use default for lgv services
16
+ if (typeof config.layerConf === "undefined" || config.layerConf === defaults.layerConf) {
17
+ return defaults.layers;
18
+ }
19
+
20
+ // user didn't specify, layerConf is not known => don't set anything initially
21
+ return [];
22
+ }
@@ -0,0 +1,22 @@
1
+ /**
2
+ * Loads the specified JS and calls the given callback after the file has loaded. Modifies the DOM for loading.
3
+ * @param {String} fileName - The URL to Load
4
+ * @param {Function} callback - the Function to call after loading has completed
5
+ * @returns {undefined}
6
+ */
7
+ export function load3DScript (fileName, callback) {
8
+ const head = document.getElementsByTagName("head")[0],
9
+ script = document.createElement("script");
10
+
11
+ script.type = "text/javascript";
12
+ script.src = fileName;
13
+
14
+ script.onload = callback;
15
+ script.onreadystatechange = function () {
16
+ if (this.readyState === "complete") {
17
+ return callback();
18
+ }
19
+ return null;
20
+ };
21
+ head.appendChild(script);
22
+ }
@@ -0,0 +1,22 @@
1
+ /**
2
+ * Parsing the params object in to string for url
3
+ * @param {Object} params the paramters in object
4
+ * @returns {String} The url parameters in string format
5
+ */
6
+ function getParamsUrl (params) {
7
+ if (!params || typeof params !== "object" || params.constructor !== Object || !Object.keys(params).length) {
8
+ return "";
9
+ }
10
+
11
+ let paramsUrl = "";
12
+
13
+ Object.entries(params).forEach(([key, value]) => {
14
+ paramsUrl += "&" + key + "=" + value;
15
+ });
16
+
17
+ return paramsUrl;
18
+ }
19
+
20
+ export {
21
+ getParamsUrl
22
+ };
@@ -0,0 +1,23 @@
1
+ import defaults from "../defaults";
2
+
3
+ /**
4
+ * Sets the configured background image to the div the map is to be rendered in.
5
+ * @param {object} [config={}] - user configuration
6
+ * @param {string} [config.target] - id of map div
7
+ * @param {string} [config.backgroundImage] - image URL to set; "" means none, undefined means default
8
+ * @returns {undefined}
9
+ */
10
+ function setBackgroundImage ({backgroundImage, target} = {}) {
11
+ const div = document.getElementById(target || defaults.target);
12
+
13
+ if (div) {
14
+ const urlString = `url(${
15
+ // keep "" as explicit none
16
+ typeof backgroundImage === "string" ? backgroundImage : defaults.backgroundImage
17
+ })`;
18
+
19
+ div.style.backgroundImage = urlString;
20
+ }
21
+ }
22
+
23
+ export default setBackgroundImage;
@@ -0,0 +1,75 @@
1
+
2
+ /**
3
+ * Checks the version of the wfs against allowed versions and returns the version.
4
+ * If the version is not allowed, 1.1.0 is returned.
5
+ * @param {rawLayer} rawLayer layer specification as in services.json
6
+ * @param {options} [options] additional options
7
+ * @returns {String} The allowed version.
8
+ */
9
+ function getVersion (rawLayer, options) {
10
+ const allowedVersions = ["1.0.0", "1.1.0", "2.0.0"];
11
+ let version = options.version || rawLayer.version;
12
+
13
+ if (!allowedVersions.includes(version)) {
14
+ version = allowedVersions[1];
15
+ console.warn(`The "${rawLayer.typ}" layer with the id: "${rawLayer.id}" is configured in version: ${version}.`
16
+ + ` OpenLayers accepts "${rawLayer.typ}" only in the versions: ${allowedVersions},`
17
+ + ` It tries to load the layer with the id: "${rawLayer.id}" in version ${allowedVersions[1]}!`);
18
+ }
19
+
20
+ return version;
21
+ }
22
+
23
+ /**
24
+ * Logs and throws an Error.
25
+ * @param {string} error error message
26
+ * @param {object} [options] additional options
27
+ * @param {function} [options.onLoadingError] additional option, called with param error
28
+ * @param {function} failure failure callback to ol.VectorLayer, fires 'featuresloaderror' event
29
+ * {@link https://openlayers.org/en/latest/apidoc/module-ol_source_Vector-VectorSource.html failure: see}
30
+ * @returns {void}
31
+ */
32
+ function onError (error, options = {}, failure) {
33
+ if (options.onLoadingError) {
34
+ options.onLoadingError(error);
35
+ }
36
+ failure(error);
37
+ throw Error(error);
38
+ }
39
+
40
+ /**
41
+ * Called after features are loaded. Calls options.featuresFilter, success-callback and options.afterLoading.
42
+ * Features are added to source.
43
+ * @param {ol.source.VectorSource} source the vector source
44
+ * @param {Array.<module:ol/Feature~Feature>} features loaded features
45
+ * @param {function} onErrorFn Calls options.onLoadingError and 'featuresloaderror' event will be fired by using failure callback.
46
+ * @param {function} success callback: 'featuresloadend' event will be fired
47
+ * @param {function} failure failure callback to ol.VectorLayer, fires 'featuresloaderror' event
48
+ * @param {options} [options] additional options
49
+ * @returns {void}
50
+ */
51
+ function onLoad (source, features, onErrorFn, success, failure, options = {}) {
52
+ let filteredFeatures = features;
53
+
54
+ try {
55
+ if (options.featuresFilter) {
56
+ filteredFeatures = options.featuresFilter(features);
57
+ }
58
+ source.addFeatures(filteredFeatures);
59
+ if (options.afterLoading) {
60
+ options.afterLoading(filteredFeatures);
61
+ }
62
+ // success: see https://openlayers.org/en/latest/apidoc/module-ol_source_Vector-VectorSource.html
63
+ success(filteredFeatures);
64
+ }
65
+ catch (error) {
66
+ console.error(error);
67
+ onErrorFn(error, options, failure);
68
+ }
69
+ }
70
+
71
+ export {
72
+ getVersion,
73
+ onError,
74
+ onLoad
75
+ };
@@ -0,0 +1,40 @@
1
+ import Polygon from "ol/geom/Polygon";
2
+ import Point from "ol/geom/Point";
3
+
4
+ import coordsToPairs from "./coordsToPairs";
5
+
6
+ /**
7
+ * Zooms the map to a geometry.
8
+ * @param {ol/Map} map map object to zoom
9
+ * @param {(ol/geom/SimpleGeometry|ol/extent)} geometryOrExtent the geometry or extent to zoom to
10
+ * @param {object} [params={}] forwarded to ol/View.fit, may e.g. specify duration of animation {@link https://openlayers.org/en/latest/apidoc/module-ol_View.html#~FitOptions}
11
+ * @returns {void}
12
+ */
13
+ export default function zoomTo (map, geometryOrExtent, params = {}) {
14
+ map.getView().fit(geometryOrExtent, params);
15
+ }
16
+
17
+ /**
18
+ * Zooms the map to a searchResult.
19
+ * @param {ol/Map} map map object to zoom
20
+ * @param {SearchResult} searchResult result from searchAddress
21
+ * @param {object} [params={}] forwarded to ol/View.fit, may e.g. specify duration of animation {@link https://openlayers.org/en/latest/apidoc/module-ol_View.html#~FitOptions}
22
+ * @returns {void}
23
+ */
24
+ export function zoomToSearchResult (map, searchResult, params = {}) {
25
+ let geom;
26
+
27
+ switch (searchResult.geometry.type) {
28
+ case "Polygon":
29
+ geom = new Polygon([coordsToPairs(searchResult.geometry.coordinates)]);
30
+ break;
31
+ case "Point":
32
+ geom = new Point(searchResult.geometry.coordinates.map(parseFloat));
33
+ break;
34
+ default:
35
+ console.warn(`ZoomTo for type ${searchResult.geometry.type} not implemented.`);
36
+ return;
37
+ }
38
+
39
+ zoomTo(map, geom, params);
40
+ }
@@ -0,0 +1,5 @@
1
+ import {default as map} from "./map.js";
2
+
3
+ export default {
4
+ map
5
+ };