-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
234 lines (215 loc) · 6.36 KB
/
index.js
File metadata and controls
234 lines (215 loc) · 6.36 KB
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
import '@fortawesome/fontawesome-free/css/all.css';
import 'ol/ol.css';
import './legend.css';
import GeoJSON from 'ol/format/GeoJSON.js';
import controlLegend from './Legend.js';
import history from './maelbeek_history.json';
import hydrology from './maelbeek_hydrology.json';
import {Attribution} from 'ol/control';
import {Circle, Fill, Stroke, Style, Text} from 'ol/style';
import {Map, View} from 'ol';
import {Stamen, Vector as VectorSource} from 'ol/source.js';
import {Tile as TileLayer, Vector as VectorLayer} from 'ol/layer.js';
import {getVectorContext} from 'ol/render.js';
//A distinct className is required to use another canvas for the background
const background = new TileLayer({
opacity: 0.3,
className: 'stamen',
source: new Stamen({
layer: 'toner-lines',
}),
});
const base = new TileLayer({
opacity: 1,
source: new Stamen({
layer: 'toner-background',
}),
});
const clipLayer = new VectorLayer({
style: new Style({
stroke: new Stroke({
width: 3,
color: 'red',
}),
}),
source: new VectorSource({
attributions: 'Egebs BRUXELLES',
title: 'Hydrologie du Maelbeek',
features: new GeoJSON().readFeatures(hydrology, {
featureProjection: 'EPSG:3857',
}),
format: new GeoJSON(),
}),
});
clipLayer.getSource().set('title', 'Hydrologie du Maelbeek');
//Giving the clipped layer an extent is necessary to avoid rendering when the feature is outside the viewport
base.setExtent(clipLayer.getSource().getExtent());
const style = new Style({
fill: new Fill({
color: 'black',
}),
});
background.on('postrender', function (e) {
const vectorContext = getVectorContext(e);
e.context.globalCompositeOperation = 'destination-out';
clipLayer.getSource().forEachFeature(function (feature) {
vectorContext.drawFeature(feature, style);
});
e.context.globalCompositeOperation = 'source-over';
});
base.on('postrender', function (e) {
const vectorContext = getVectorContext(e);
e.context.globalCompositeOperation = 'destination-in';
clipLayer.getSource().forEachFeature(function (feature) {
vectorContext.drawFeature(feature, style);
});
e.context.globalCompositeOperation = 'source-over';
});
const getIcon = function (type) {
let icon;
switch (type) {
case 'source':
icon = '\u{f773}'; //https://fontawesome.com/icons/water?style=solid
break;
case 'moulin':
icon = '\u{f085}'; //https://fontawesome.com/icons/water?style=solid
break;
case 'etymologie':
icon = '\u{f129}'; //https://fontawesome.com/icons/water?style=solid
break;
case 'site remarquable':
icon = '\u{f02e}'; //https://fontawesome.com/icons/bookmark?style=regular
break;
case 'immeuble remarquable':
icon = '\u{f66f}'; //https://fontawesome.com/icons/landmark?style=solid
break;
case 'auberge':
icon = '\u{f0fc}'; //https://fontawesome.com/icons/beer?style=solid
break;
case 'manufacture':
icon = '\u{f275}'; //https://fontawesome.com/icons/industry?style=solid
break;
case 'chateau':
icon = '\u{f447}'; //https://fontawesome.com/icons/chess-rook?style=solid
break;
case 'centre villageois':
icon = '\u{f67f}'; //https://fontawesome.com/icons/fort-awesome?style=brands
break;
default:
icon = '\u{f129}'; //https://fontawesome.com/icons/info-circle?style=solid
break;
}
return icon;
};
const getColor = function (type) {
let color;
switch (type) {
case 'source':
color = 'red'; //https://fontawesome.com/icons/water?style=solid
break;
case 'moulin':
case 'immeuble remarquable':
case 'auberge':
case 'manufacture':
case 'centre villageois':
case 'chateau':
color = '#13505b'; //https://fontawesome.com/icons/water?style=solid
break;
case 'site remarquable':
case 'etymologie':
color = 'darkred'; //https://fontawesome.com/icons/water?style=solid
break;
default:
color = 'darkred'; //https://fontawesome.com/icons/info-circle?style=solid
break;
}
return color;
};
const createTextStyle = function (type) {
const iconStyle = new Style({
image: new Circle({
fill: new Fill({color: getColor(type)}),
stroke: new Stroke({color: 'black', width: '2'}),
points: 4,
radius: 12,
}),
text: new Text({
textAlign: 'center', //center, end, left, right, start
textBaseline: 'middle', //alphabetic, bottom, hanging, ideographic, middle, top
font: 'bold 12px / 1.6 "Font Awesome 5 Free"', //Arial, Courier New ,... //weight + ' ' + size + '/' + height + ' ' + dom.font.value
text: getIcon(type),
placement: 'point',
offsetX: 0,
offsetY: 0,
fill: new Fill({color: 'white'}),
stroke: new Stroke({color: 'black', width: 2}),
rotation: 0,
overflow: false,
}),
});
return iconStyle;
};
const dataLayer = new VectorLayer({
style: function (feature) {
return createTextStyle(feature.get('type'));
},
source: new VectorSource({
features: new GeoJSON().readFeatures(history, {
featureProjection: 'EPSG:3857',
}),
}),
});
/****/
clipLayer.getSource().getLegends = function () {
const legends = [
{
label: 'bassin versant',
geometry: 'Polygon',
style: new Style({
fill: new Fill({color: 'white'}),
stroke: new Stroke({color: 'red', width: '2'}),
}),
},
{
label: 'rivière',
geometry: 'LineString',
style: new Style({
stroke: new Stroke({color: 'red', width: '2'}),
}),
},
];
return legends;
};
dataLayer.getSource().set('title', 'Lieux historiques');
dataLayer.getSource().getLegends = function () {
const typologie = [
'source',
'etymologie',
'site remarquable',
'moulin',
'auberge',
'manufacture',
'chateau',
'centre villageois',
'immeuble remarquable',
];
const legends = [];
typologie.forEach((type) =>
legends.push({label: type, style: createTextStyle(type)})
);
return legends;
};
const olLegend = new controlLegend({collapsible: true, collapsed: false});
const extent = clipLayer.getSource().getExtent();
const map = new Map({
controls: [olLegend, new Attribution()],
//interactions:[],
layers: [background, base, clipLayer, dataLayer],
target: 'map1',
view: new View({
rotation: 1.2,
center: [0, 0],
zoom: 0,
}),
});
map.getView().fit(extent, {padding: [1, 1, 200, 1]});