-
Notifications
You must be signed in to change notification settings - Fork 0
/
gfbio_search_visualization.js
255 lines (231 loc) · 6.88 KB
/
gfbio_search_visualization.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
/**
* This singleton encapsulates the GFBio Search Visualization.
*/
var SearchVisualization = (function() {
"use strict";
/**
* Background Layer
* @type {ol.layer.Layer}
*/
const backgroundLayer = new ol.layer.Tile({
source: new ol.source.OSM({
wrapX: false
})
});
/**
* Maximum Zoom Level
* @const {number}
*/
const maximumZoomLevel = 3;
/**
* Source SRS
* @const {ol.Projection}
*/
const sourceSrs = ol.proj.get('EPSG:4326');
/**
* Map SRS
* @const {ol.Projection}
*/
const mapSrs = ol.proj.get('EPSG:3857');
/**
* The map reference.
* @type {ol.Map}
*/
let map;
/**
* The globally visible extent.
* @type {ol.Extent}
*/
let extent = undefined;
/**
* Initialize the map.
*/
function initialize() {
map = new ol.Map({
layers: [
backgroundLayer
],
target: 'map',
view: new ol.View({
center: [0, 0],
zoom: 2,
projection: mapSrs,
extent: mapSrs.getExtent()
})
});
}
/**
* Receive an event from the portal.
* @param event
*/
function receiveMessage(event) {
// reset map layers and extent
map.getLayers().clear();
map.getLayers().push(backgroundLayer);
extent = undefined;
const datasets = event.data.selected;
if (datasets && datasets.length > 0) {
datasets.forEach(processDataset);
focus();
}
}
/**
* Focus on the current extent.
*/
function focus() {
const easingDurationMs = 500;
map.getView().fit(extent, {
size: map.getSize(),
maxZoom: maximumZoomLevel,
duration: easingDurationMs,
});
}
/**
* Adds the boundaries of a dataset to the map.
* @param {{
* minLongitude: number,
* maxLongitude: number,
* minLatitude: number,
* maxLatitude: number,
* title: string,
* authors: string,
* dataCenter: string,
* color: string,
* metadataLink: string
* }} dataset
*/
function processDataset(dataset) {
const vectorSource = new ol.source.Vector({
wrapX: false
});
const geometry = getGeometry(dataset.minLongitude, dataset.maxLongitude, dataset.minLatitude, dataset.maxLatitude);
geometry.applyTransform(ol.proj.getTransform(sourceSrs, mapSrs));
vectorSource.addFeature(
new ol.Feature({
"geometry": geometry,
"Dataset Title" : dataset.title,
"Author" : dataset.authors,
"Data Center" : dataset.dataCenter
})
);
if(extent){
ol.extent.extend(extent, vectorSource.getExtent());
} else {
extent = vectorSource.getExtent();
}
const layer = new ol.layer.Vector({
source: vectorSource,
style: styleFunction(dataset.color.replace('0x', '#'))
});
// append layer
map.addLayer(layer);
}
/**
* Returns a geometry depending on the extension of the bounds.
* @param minLon
* @param maxLon
* @param minLat
* @param maxLat
* @returns {ol.geom.Geometry}
*/
function getGeometry(minLon, maxLon, minLat, maxLat) {
const equalLon = minLon === maxLon;
const equalLat = minLat === maxLat;
// restrict since -90 and 90 cause problems in openlayer's web mercator projection
minLat = Math.max(minLat, -89.99999);
maxLat = Math.min(maxLat, 89.99999);
if (equalLon && equalLat) {
return new ol.geom.Point([minLon, minLat]);
} else {
return new ol.geom.Polygon([[[minLon, minLat], [minLon, maxLat], [maxLon, maxLat], [maxLon, minLat]]]);
}
}
/**
* Converts a hex string to rgb.
* @param hex
* @returns {{r: number, g: number, b: number}}
*/
function hexToRgb(hex) {
const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
return result ? {
r: parseInt(result[1], 16),
g: parseInt(result[2], 16),
b: parseInt(result[3], 16)
} : undefined;
}
/**
* Returns a style function depending on the fill color.
* @param fillColor
* @returns {Function}
*/
function styleFunction(fillColor) {
const rgbColor = hexToRgb(fillColor);
const styles = {
'MultiPoint': [new ol.style.Style({
image: new ol.style.Circle({
radius: 5,
fill: new ol.style.Fill({
color: fillColor,
opacity: 0.6
}),
stroke: new ol.style.Stroke({
color: '#000000',
opacity: 0.5,
width: 1
})
})
})],
'Point': [new ol.style.Style({
image: new ol.style.Circle({
radius: 5,
fill: new ol.style.Fill({
color: fillColor,
opacity: 0.6
}),
stroke: new ol.style.Stroke({
color: '#000000',
opacity: 0.5,
width: 1
})
})
})],
'MultiPolygon': [new ol.style.Style({
fill: new ol.style.Fill({
color: 'rgba(' + rgbColor.r + ',' + rgbColor.g + ',' + rgbColor.b + ',0.25)'
}),
stroke: new ol.style.Stroke({
color: fillColor,
width: 1
})
})],
'Polygon': [new ol.style.Style({
fill: new ol.style.Fill({
color: 'rgba(' + rgbColor.r + ',' + rgbColor.g + ',' + rgbColor.b + ',0.25)'
}),
stroke: new ol.style.Stroke({
color: fillColor,
width: 1
})
})],
'LineString': [new ol.style.Style({
stroke: new ol.style.Stroke({
color: fillColor,
width: 3
})
})],
'MultiLineString': [new ol.style.Style({
stroke: new ol.style.Stroke({
color: fillColor,
width: 3
})
})]
};
return function(feature) {
return styles[feature.getGeometry().getType()];
};
}
return {
initialize: initialize,
receiveMessage: receiveMessage
}
})();