-
Notifications
You must be signed in to change notification settings - Fork 0
/
ol-nino-zone-tool.js
322 lines (296 loc) · 8.85 KB
/
ol-nino-zone-tool.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
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
import React, { useEffect } from 'react';
import ReactDOM from 'react-dom';
import GeoJSON from 'ol/format/GeoJSON';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import { Vector as OlVectorLayer } from 'ol/layer';
import { Vector as OlVectorSource } from 'ol/source';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import Overlay from 'ol/Overlay';
import Projection from 'ol/proj/Projection';
import Static from 'ol/source/ImageStatic';
import ImageLayer from 'ol/layer/Image';
import {
Fill as OlStyleFill,
Stroke as OlStyleStroke,
Style as OlStyle,
} from 'ol/style';
import util from '../../util/util';
const { events } = util;
const geographicProj = 'EPSG:4326';
let tooltipElement;
let tooltipOverlay;
let init = false;
const allNinoZones = {};
const vectorLayers = {};
const sources = {};
const areaBgFill = new OlStyleFill({
color: 'rgba(213, 78, 33, 0.1)',
});
const solidBlackLineStroke = new OlStyleStroke({
color: 'rgba(0, 0, 0, 1)',
lineJoin: 'round',
width: 5,
});
const vectorStyles = [
new OlStyle({
fill: areaBgFill,
stroke: solidBlackLineStroke,
}),
new OlStyle({
stroke: new OlStyleStroke({
color: '#fff',
lineJoin: 'round',
width: 2,
}),
}),
];
//const months={'JAN':'01','FEB':'02','MAR':'03','APR':'04','MAY':'05','JUN':'06','JUL':'07','AUG':'08','SEP':'09','OCT':'10','NOV':'11','DEC':'12'};
//const current_timestamp = document.getElementById('year-timeline').value+'-'+months[document.getElementById('month-timeline').value]+'-'+document.getElementById('day-timeline').value+'T00:00:00Z';
var url = new URL(window.location.href);
var current_timestamp = url.searchParams.get("t");
const zones = {
'nino1+2': {
name: 'nino1+2',
image: 'http://ec2-35-156-132-64.eu-central-1.compute.amazonaws.com:8080/get_stats?minx=-90&miny=-10&maxx=-80&maxy=0×tamp='+current_timestamp,
url: 'http://ec2-35-156-132-64.eu-central-1.compute.amazonaws.com:8080/get_stats?minx=-90&miny=-10&maxx=-80&maxy=0×tamp='+current_timestamp, // Change URL
type: 'Feature',
maxx: 0,
minx: -10,
miny: -90,
maxy: -80,
geometry: {
type: 'Polygon',
coordinates: [
[
[-90, 0],
[-80, 0],
[-80, -10],
[-90, -10],
[-90, 0],
],
],
},
},
nino3: {
name: 'nino3',
type: 'Feature',
image: 'http://ec2-35-156-132-64.eu-central-1.compute.amazonaws.com:8080/get_stats?minx=-150&miny=-5&maxx=-90&maxy=5×tamp='+current_timestamp,
url: 'http://ec2-35-156-132-64.eu-central-1.compute.amazonaws.com:8080/get_stats?minx=-150&miny=-5&maxx=-90&maxy=5×tamp='+current_timestamp, // Change URL
maxx: 5,
minx: -5,
miny: -150,
maxy: -90,
geometry: {
type: 'Polygon',
coordinates: [
[
[-150, 5],
[-90, 5],
[-90, -5],
[-150, -5],
[-150, 5],
],
],
},
},
'nino3.4': {
name: 'nino3.4',
type: 'Feature',
image: 'https://imgs.xkcd.com/comics/online_communities.png',
url: 'http://ec2-35-156-132-64.eu-central-1.compute.amazonaws.com:8080/get_stats?minx=-170&miny=-5&maxx=-120&maxy=5×tamp='+current_timestamp, // Change URL
maxx: 5,
minx: -5,
miny: -170,
maxy: -120,
geometry: {
type: 'Polygon',
coordinates: [
[
[-170, 5],
[-120, 5],
[-120, -5],
[-170, -5],
[-170, 5],
],
],
},
},
nino4: {
name: 'nino4',
type: 'Feature',
image: 'https://imgs.xkcd.com/comics/online_communities.png',
url: 'http://ec2-35-156-132-64.eu-central-1.compute.amazonaws.com:8080/get_stats?minx=-180&miny=-5&maxx=-150&maxy=5×tamp='+current_timestamp, // Change URL
maxx: 5,
minx: -5,
miny: 160,
maxy: -150,
geometry: {
type: 'Polygon',
coordinates: [
[
[-200, 5],
[-150, 5],
[-150, -5],
[-200, -5],
[-200, 5],
],
],
},
},
};
const geojsonObject = {
type: 'FeatureCollection',
crs: {
type: 'name',
properties: {
name: geographicProj,
},
},
features: [],
};
/**
* A component to draw NinoZone functionality to the OL map
*/
function OlNinoZoneTool(props) {
const {
map, olMap, crs, projections,
} = props;
useEffect(() => {
if (!init) {
projections.forEach((key) => {
allNinoZones[key] = {};
vectorLayers[key] = null;
sources[key] = new OlVectorSource({ wrapX: false });
});
init = true;
}
}, [projections]);
useEffect(() => {
if (map && map.rendered) {
events.on('draw:ninoZone', initMarkNinoZones);
events.on('draw:clear', clearDraw);
}
return () => {
if (map && map.rendered) {
events.off('draw:ninoZone', initMarkNinoZones);
events.off('draw:clear', clearDraw);
}
};
}, [map]);
function initMarkNinoZones(zone) {
if (!vectorLayers[crs]) {
vectorLayers[crs] = new OlVectorLayer({
source: sources[crs],
style: vectorStyles,
map: olMap,
});
}
tooltipElement = document.createElement('div');
tooltipOverlay = new Overlay({
element: tooltipElement,
offset: [0, -15],
positioning: 'bottom-center',
stopEvent: false,
});
olMap.addOverlay(tooltipOverlay);
const fIndex = geojsonObject.features.findIndex((f) => f.name === zone);
if (fIndex < 0) {
geojsonObject.features.push(zones[zone]);
const features = new GeoJSON().readFeatures(geojsonObject);
// Add tooltip
const feature = features.filter((f) => {
const coordsArr = f.getGeometry().getCoordinates();
return JSON.stringify(zones[zone].geometry.coordinates) === JSON.stringify(coordsArr);
});
if (feature && feature[0]) {
//setFeatureImage(feature[0], zones[zone], olMap); // This sets an image inside the box
sources[crs].addFeature(feature[0]);
const tooltipCoord = feature[0].getGeometry().getExtent();
renderTooltip(feature[0], tooltipOverlay, zones[zone]);
tooltipOverlay.setPosition(tooltipCoord.slice(2, 4));
}
}
}
const setFeatureImage = (feature, zone) => {
const projection = new Projection({
code: 'xkcd-image',
units: 'pixels',
extent: feature.getGeometry().getExtent(),
});
const imgLayer = new ImageLayer({
source: new Static({
attributions: '© <a href="https://xkcd.com/license.html">xkcd</a>',
url: zone.image,
projection,
imageExtent: feature.getGeometry().getExtent(),
}),
});
imgLayer.set('name', `imgLayer${zone.name}`);
olMap.addLayer(imgLayer);
};
const renderTooltip = (feature, overlay, zone) => {
const removeFeature = () => {
geojsonObject.features.splice(geojsonObject.features.findIndex((f) => f.name === zone.name), 1);
sources[crs].removeFeature(feature);
olMap.removeOverlay(overlay);
// remove ImageLayer
const imgLayer = olMap.getLayers().getArray().find((l) => l.get('name') === `imgLayer${zone.name}`);
if (imgLayer) {
olMap.removeLayer(imgLayer);
}
};
ReactDOM.render((
<div className="tooltip-measure tooltip-custom-black tooltip-static">
{zone.name}
<br/><br/><p><img id={zone.name} src={zone.url} /><img id={zone.name+'_holder'} src="https://cdnjs.cloudflare.com/ajax/libs/galleriffic/2.0.1/css/loader.gif" style={{display: 'none'}}/></p>
<span className="close-tooltip" onClick={removeFeature} onTouchEnd={removeFeature}>
<FontAwesomeIcon icon="times" fixedWidth />
</span>
</div>
), overlay.getElement());
};
// {zone.url}<div id={zone.name}> </div> "http://ec2-3-65-18-201.eu-central-1.compute.amazonaws.com:8080/get_stats?minx={zone.minx}&miny={miny}&maxx={zone.maxx}&maxy={zone.maxy}×tamp=2016-06-09T00:00:00Z"
/**
* Clear all existing Draw on the current map projection
*/
function clearDraw() {
Object.values(allNinoZones[crs]).forEach(
({ feature, overlay }) => {
olMap.removeOverlay(overlay);
sources[crs].removeFeature(feature);
},
);
allNinoZones[crs] = {};
olMap.removeOverlay(tooltipOverlay);
if (vectorLayers[crs]) {
vectorLayers[crs].setMap(null);
vectorLayers[crs] = null;
}
}
return null;
}
OlNinoZoneTool.propTypes = {
map: PropTypes.object,
olMap: PropTypes.object,
crs: PropTypes.string,
toggleDrawActive: PropTypes.func,
};
const mapStateToProps = (state) => {
const {
map,
proj,
config,
} = state;
const { crs } = proj.selected;
const projections = Object.keys(config.projections).map((key) => config.projections[key].crs);
return {
map,
olMap: map.ui.selected,
crs,
projections,
};
};
export default connect(
mapStateToProps,
)(OlNinoZoneTool);