-
Notifications
You must be signed in to change notification settings - Fork 410
/
Copy pathMap.jsx
478 lines (458 loc) · 17.4 KB
/
Map.jsx
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
/*
* Copyright 2017, GeoSolutions Sas.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
import PropTypes from 'prop-types';
import React from 'react';
import { connect, createPlugin } from '../utils/PluginsUtils';
import Spinner from 'react-spinkit';
import './map/css/map.css';
import Message from '../components/I18N/Message';
import ConfigUtils from '../utils/ConfigUtils';
import { setMapResolutions, mapPluginLoad } from '../actions/map';
import { isString } from 'lodash';
import selector from './map/selector';
import MapSettings from './map/mapsettings/MapSettings';
import mapReducer from "../reducers/map";
import layersReducer from "../reducers/layers";
import drawReducer from "../reducers/draw";
import boxReducer from '../reducers/box';
import highlightReducer from "../reducers/highlight";
import mapTypeReducer from "../reducers/maptype";
import additionalLayersReducer from "../reducers/additionallayers";
import mapEpics from "../epics/map";
import pluginsCreator from "./map/index";
import withScalesDenominators from "../components/map/enhancers/withScalesDenominators";
import { createFeatureFilter } from '../utils/FilterUtils';
import ErrorPanel from '../components/map/ErrorPanel';
import catalog from "../epics/catalog";
import backgroundSelector from "../epics/backgroundselector";
import API from '../api/catalog';
import { MapLibraries } from '../utils/MapTypeUtils';
import {getHighlightLayerOptions} from "../utils/HighlightUtils";
/**
* The Map plugin allows adding mapping library dependent functionality using support tools.
* Some are already available for the supported mapping libraries (openlayers, leaflet, cesium), but it's possible to develop new ones.
* The list of enabled tools can be configured using the tools property, as in the following example:
*
* ```
* {
* "name": "Map",
* "cfg": {
* "tools": ["overview", "scalebar", "draw", "highlight"]
* ...
* }
* }
* ```
* // Each tool can be configured using the toolsOptions. Tool configuration can be mapping library dependent:
* ```
* "toolsOptions": {
* "scalebar": {
* "leaflet": {
* "position": "bottomright"
* }
* ...
* }
* ...
* }
*
* ```
* or not
* ```
* "toolsOptions": {
* "scalebar": {
* "position": "bottomright"
* ...
* }
* ...
* }
* ```
* In addition to standard tools, you can also develop your own, ad configure them to be used.
*
* To do that you need to:
* - develop a tool Component, in JSX (e.g. TestSupport), for each supported mapping library
* ```
* const React = require('react');
* class TestSupport extends React.Component {
* static propTypes = {
* label: PropTypes.string
* }
* render() {
* alert(this.props.label);
* return null;
* }
* }
* module.exports = TestSupport;
* ```
* - include the tool(s) in the requires section of plugins.js amd give it a name:
* ```
* module.exports = {
* plugins: {
* MapPlugin: require('../plugins/Map'),
* ...
* },
* requires: {
* ...
* TestSupportLeaflet: require('../components/map/leaflet/TestSupport')
* }
* };
* ```
* - configure the Map plugin including the new tool and related options. You can configure the tool to be used for each mapping library, giving it a name and impl attributes, where:
* ```
* {
* "name": "Map",
* "cfg": {
* "tools": ["overview", "scalebar", "draw", {
* "leaflet": {
* "name": "test",
* "impl": "{context.TestSupportLeaflet}"
* }
* }],
* "toolsOptions": {
* "test": {
* "label": "Hello"
* }
* ...
* }
* }
* }
* ```
* - name is a unique name for the tool
* - impl is a placeholder (“{context.ToolName}”) where ToolName is the name you gave the tool in plugins.js (TestSupportLeaflet in our example)
*
* You can no longer specify a list of fonts that have to be loaded before map rendering, we are now only loading FontAwesome for the icons
* We will pre-load FontAwesome only if needed, i.e you need to show markers with symbols (e.g. Annotations).
*
* An additional feature to is limit the area and/or the minimum level of zoom in the localConfig.json file using "mapConstraints" property
*
* e.g
* ```json
* "mapConstraints": {
* "minZoom": 12, // minimal allowed zoom used by default
* "crs":"EPSG:3857", // crs of the restrictedExtent
* "restrictedExtent":[ // limits the area accessible to the user to this bounding box
* 1060334.456371965,5228292.734706056,
* 1392988.403469052,5503466.036532691
* ],
* "projectionsConstraints": {
* "EPSG:1234": { "minZoom": 5 } // customization of minZoom for different projections
* }
* }
* ```
*
* With this setup you can configure a restricted area and/or a minimum zoom level for the whole application.
* If you have different reference systems for your maps, for each of them you can even set a minimum zoom
* using the entry `projectionsConstraints` as written in the example.
*
* ```
*
* @memberof plugins
* @class Map
* @prop {array} additionalLayers static layers available in addition to those loaded from the configuration
* @prop {object} mapOptions map options grouped by map type
* @prop {boolean} mapOptions.cesium.navigationTools enable cesium navigation tool (default false)
* @prop {boolean} mapOptions.cesium.showSkyAtmosphere enable sky atmosphere of the globe (default true)
* @prop {boolean} mapOptions.cesium.showGroundAtmosphere enable ground atmosphere of the globe (default false)
* @prop {boolean} mapOptions.cesium.enableFog enable fog in the view (default false)
* @prop {boolean} mapOptions.cesium.depthTestAgainstTerrain if true all primitive 3d features will be tested against the terrain while if false they will be drawn on top of the terrain even if hidden by it (default true)
* @prop {number} mapOptions.cesium.maximumZoomDistance max zoom limit (in meter unit) to restrict the zoom out operation based on it
* @prop {number} mapOptions.cesium.minimumZoomDistance min zoom limit (in meter unit) to restrict the zoom in operation based on it
* @static
* @example
* // Adding a layer to be used as a source for the elevation (shown in the MousePosition plugin configured with showElevation = true)
* {
* "cfg": {
* "additionalLayers": [{
* "type": "wms",
* "url": "http://localhost:8090/geoserver/wms",
* "visibility": true,
* "title": "Elevation",
* "name": "topp:elevation",
* "format": "application/bil16",
* "useForElevation": true,
* "nodata": -9999,
* "littleendian": false,
* "hidden": true
* }]
* }
* }
*
*/
class MapPlugin extends React.Component {
static propTypes = {
mapType: PropTypes.string,
map: PropTypes.object,
layers: PropTypes.array,
additionalLayers: PropTypes.array,
zoomControl: PropTypes.bool,
mapLoadingMessage: PropTypes.string,
loadingSpinner: PropTypes.bool,
loadingError: PropTypes.string,
tools: PropTypes.array,
options: PropTypes.object,
mapOptions: PropTypes.object,
projectionDefs: PropTypes.array,
toolsOptions: PropTypes.object,
onResolutionsChange: PropTypes.func,
actions: PropTypes.object,
features: PropTypes.array,
securityToken: PropTypes.string,
elevationEnabled: PropTypes.bool,
isLocalizedLayerStylesEnabled: PropTypes.bool,
localizedLayerStylesName: PropTypes.string,
currentLocaleLanguage: PropTypes.string,
items: PropTypes.array,
onLoadingMapPlugins: PropTypes.func,
onMapTypeLoaded: PropTypes.func,
pluginsCreator: PropTypes.func
};
static defaultProps = {
mapType: MapLibraries.OPENLAYERS,
actions: {},
zoomControl: false,
mapLoadingMessage: "map.loading",
loadingSpinner: true,
tools: ["scalebar", "draw", "highlight", "popup", "box"],
options: {},
mapOptions: {},
toolsOptions: {
measurement: {},
locate: {},
scalebar: {
[MapLibraries.LEAFLET]: {
position: "bottomright"
}
},
overview: {
overviewOpt: {
position: 'bottomright',
collapsedWidth: 25,
collapsedHeight: 25,
zoomLevelOffset: -5,
toggleDisplay: true
},
layers: [{type: "osm"}]
}
},
securityToken: '',
additionalLayers: [],
elevationEnabled: false,
onResolutionsChange: () => {},
items: [],
onLoadingMapPlugins: () => {},
onMapTypeLoaded: () => {},
pluginsCreator
};
state = {};
UNSAFE_componentWillMount() {
// moved the font load of FontAwesome only to styleParseUtils (#9653)
this.updatePlugins(this.props);
this._isMounted = true;
}
UNSAFE_componentWillReceiveProps(newProps) {
if (newProps.mapType !== this.props.mapType || newProps.actions !== this.props.actions) {
this.updatePlugins(newProps);
}
}
componentWillUnmount() {
this._isMounted = false;
}
getHighlightLayer = (projection, index, env) => {
const plugins = this.state.plugins;
const {features, ...options} = getHighlightLayerOptions({features: this.props.features});
return (<plugins.Layer type="vector"
srs={projection}
position={index}
key="highlight"
env={env}
options={{
name: "highlight",
...options,
features
}} >
{features.map( (feature) => {
return (<plugins.Feature
msId={feature.id}
properties={feature.properties}
key={feature.id}
crs={projection}
type={feature.type}
style={feature.style || null }
geometry={feature.geometry}/>);
})}
</plugins.Layer>);
};
getTool = (tool) => {
if (isString(tool)) {
return {
name: tool,
impl: this.state.plugins.tools[tool]
};
}
return tool[this.props.mapType] || tool;
};
getConfigMapOptions = () => {
return this.props.mapOptions && this.props.mapOptions[this.props.mapType] ||
ConfigUtils.getConfigProp("defaultMapOptions") && ConfigUtils.getConfigProp("defaultMapOptions")[this.props.mapType] || {};
};
renderLayers = () => {
const projection = this.props.map.projection || 'EPSG:3857';
const env = [];
if (this.props.isLocalizedLayerStylesEnabled) {
env.push({
name: this.props.localizedLayerStylesName,
value: this.props.currentLocaleLanguage
});
}
const plugins = this.state.plugins;
// all layers must have a valid id to avoid useless re-render
return [...this.props.layers, ...this.props.additionalLayers.map(({ id, ...layer }, idx) => ({ ...layer, id: id ? id : `additional-layers-${idx}` }))].filter(this.filterLayer).map((layer, index) => {
return (
<plugins.Layer
type={layer.type}
srs={projection}
position={index}
key={layer.id || layer.name}
options={layer}
securityToken={this.props.securityToken}
env={env}
>
{this.renderLayerContent(layer, projection)}
</plugins.Layer>
);
}).concat(this.props.features && this.props.features.length && this.getHighlightLayer(projection, this.props.layers.length, env) || []);
};
renderLayerContent = (layer, projection) => {
const plugins = this.state.plugins;
if (layer.features) {
return layer.features.filter(createFeatureFilter(layer.filterObj)).map( (feature) => {
return (
<plugins.Feature
key={feature.id}
msId={feature.id}
type={feature.type}
crs={projection}
geometry={feature.geometry}
features={feature.features}
featuresCrs={ layer.featuresCrs || 'EPSG:4326' }
// FEATURE STYLE OVERWRITE LAYER STYLE
layerStyle={layer.style}
style={ feature.style || layer.style || null }
properties={feature.properties}/>
);
});
}
return null;
};
renderSupportTools = () => {
// Tools passed by other plugins
const toolsFromItems = this.props.items
.filter(({Tool}) => !!Tool)
.map(({Tool, name, cfg}) => <Tool {...cfg} key={name} mapType={this.props.mapType} />);
return this.props.tools.map((tool) => {
const Tool = this.getTool(tool);
const options = this.props.toolsOptions[Tool.name] && this.props.toolsOptions[Tool.name][this.props.mapType] || this.props.toolsOptions[Tool.name] || {};
return <Tool.impl key={Tool.name} {...options}/>;
}).concat(toolsFromItems);
};
render() {
if (this.isValidMapConfiguration(this.props.map) && this.state.plugins) {
const {mapOptions = {}} = this.props.map;
return (
<this.state.plugins.Map id="map"
{...this.props.options}
projectionDefs={this.props.projectionDefs}
{...this.props.map}
mapOptions={{...this.getConfigMapOptions(), ...mapOptions}}
zoomControl={this.props.zoomControl}
onResolutionsChange={this.props.onResolutionsChange}
errorPanel={ErrorPanel}
>
{this.renderLayers()}
{this.renderSupportTools()}
</this.state.plugins.Map>
);
}
if (this.props.loadingError) {
return (<div style={{
width: "100%",
height: "100%",
display: "flex",
justifyContent: "center",
alignItems: "center"
}} className="mapErrorMessage">
<Message msgId="map.loadingerror"/>:
{this.props.loadingError}
</div>);
}
return (<div style={{
width: "100%",
height: "100%",
display: "flex",
justifyContent: "center",
alignItems: "center"
}} className="mapLoadingMessage">
{this.props.loadingSpinner ? <Spinner spinnerName="circle" overrideSpinnerClassName="spinner"/> : null}
<Message msgId={this.props.mapLoadingMessage}/>
</div>);
}
filterLayer = (layer) => {
if (layer.useForElevation) {
return this.props.mapType === 'cesium' || this.props.elevationEnabled;
}
return layer.type !== 'elevation' || this.props.elevationEnabled;
};
updatePlugins = (props) => {
this.currentMapType = props.mapType;
props.onLoadingMapPlugins(true);
// reset the map plugins to avoid previous map library in children
this.setState({plugins: undefined });
this.props.pluginsCreator(props.mapType, props.actions).then((plugins) => {
// #6652 fix mismatch on multiple concurrent plugins loading
// to make the last mapType match the list of plugins
if (this._isMounted && plugins.mapType === this.currentMapType) {
this.setState({plugins});
props.onLoadingMapPlugins(false, props.mapType);
props.onMapTypeLoaded(true, props.mapType);
}
});
};
isValidMapConfiguration = (map) => {
// when the center is included inside the map config
// we know that the configuration has been loaded
// we should prevent to mount the map component
// in case we have a configuration like this one: { eventListeners: {}, mousePointer: '' }
// if we allow invalid configuration default props will be used instead
// initializing the map in the wrong position
return !!map?.center;
}
}
export default createPlugin('Map', {
component: connect(selector, {
onResolutionsChange: setMapResolutions,
onMapTypeLoaded: mapPluginLoad
})(withScalesDenominators(MapPlugin)),
reducers: {
map: mapReducer,
layers: layersReducer,
draw: drawReducer,
box: boxReducer,
highlight: highlightReducer,
maptype: mapTypeReducer,
additionallayers: additionalLayersReducer
},
epics: {
...mapEpics,
...backgroundSelector,
...catalog(API)
},
containers: {
Settings: () => ({
tool: <MapSettings />,
position: 2
})
}
});