-
-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathmultipleatlas_3.3.js
558 lines (466 loc) · 23.8 KB
/
multipleatlas_3.3.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
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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
var lizAtlas = function() {
lizMap.events.on({
'uicreated':function(evt){
// Configure here your atlas. Parameters are the same than the original Lizmap atlas ones.
// This is an example based on "Montpellier - Transports" project.
var lizAtlasLayers = {
layerOptions: {
VilleMTP_MTP_Quartiers_2011_432620130116112610876: {
atlasMaxWidth: 25,
atlasDuration: 5,
atlasFeatureLabel: "LIBQUART",
atlasZoom: "center",
atlasPrimaryKey: "QUARTMNO",
atlasTriggerFilter: "False",
atlasHighlightGeometry: "True",
atlasDisplayLayerDescription: "True",
atlasDisplayPopup: "False",
atlasSortField: "LIBQUART",
atlasAutoPlay: "False"
},
SousQuartiers20160121124316563: {
atlasMaxWidth: 25,
atlasDuration: 5,
atlasFeatureLabel: "LIBSQUART",
atlasZoom: "zoom",
atlasPrimaryKey: "SQUARTMNO",
atlasTriggerFilter: "False",
atlasHighlightGeometry: "True",
atlasDisplayLayerDescription: "True",
atlasDisplayPopup: "False",
atlasSortField: "LIBSQUART",
atlasAutoPlay: "False"
}
},
globalOptions: {
atlasShowAtStartup: "True"
}
}
var lizAtlasLayersCount = Object.keys(lizAtlasLayers.layerOptions).length;
var getFeatureDataCallbackCounter = 0;
var atlasGlobalOptions = lizAtlasLayers.globalOptions;
var lizAtlasGlobalConfig = {
'showAtStartup': atlasGlobalOptions['atlasShowAtStartup'] == 'True' ? true : false
};
var lizAtlasConfigArray = [];
var layerOrder = 0;
for(var layerId in lizAtlasLayers.layerOptions){
var getLayerConfig = lizMap.getLayerConfigById( layerId );
if ( !getLayerConfig )
return;
var layerConfig = getLayerConfig[1];
var featureType = getLayerConfig[0];
var atlasLayerOptions = lizAtlasLayers.layerOptions[layerId];
var primaryKey = atlasLayerOptions['atlasPrimaryKey'] != '' ? atlasLayerOptions['atlasPrimaryKey'] : null;
if(!primaryKey)
return;
var titleField = atlasLayerOptions['atlasFeatureLabel'] != '' ? atlasLayerOptions['atlasFeatureLabel'] : null;
if(!titleField)
return;
var sortField = atlasLayerOptions['atlasSortField'] != '' ? atlasLayerOptions['atlasSortField'] : titleField;
var lizAtlasConfig = {
'layername': featureType,
'layerId': layerConfig.id,
'displayLayerDescription': atlasLayerOptions['atlasDisplayLayerDescription'] == 'True' ? true : false,
'primaryKey': primaryKey,
'titleField': titleField,
'sortField': sortField,
'duration': atlasLayerOptions['atlasDuration'],
'autoPlay': atlasLayerOptions['atlasAutoPlay'] == 'True' ? true : false,
'maxWidth': atlasLayerOptions['atlasMaxWidth'] +'%',
'drawFeatureGeom': atlasLayerOptions['atlasHighlightGeometry'] == 'True' ? true : false,
'atlasDisplayPopup': atlasLayerOptions['atlasDisplayPopup'] == 'True' ? true : false,
'triggerFilter': atlasLayerOptions['atlasTriggerFilter'] == 'True' ? true : false,
'zoom': atlasLayerOptions['atlasZoom'] == '' ? false : atlasLayerOptions['atlasZoom']
};
var lizAtlasTimer;
// Launch Atlas feature
getAtlasData(lizAtlasConfig, layerOrder);
layerOrder++;
}
function getAtlasData(lizAtlasConfig, layerOrder) {
var featureType = lizAtlasConfig.layername;
// Get data
lizMap.getFeatureData(featureType, featureType+':', null, 'geom', false, null, null,
function(aName, aFilter, aFeatures, aAliases) {
lizAtlasConfig['features'] = aFeatures;
lizAtlasConfig['featureType'] = featureType;
prepareFeatures(lizAtlasConfig);
lizAtlasConfigArray[layerOrder] = lizAtlasConfig;
// Launch atlas when last ajax request had finished
getFeatureDataCallbackCounter++;
if(getFeatureDataCallbackCounter === lizAtlasLayersCount){
launchAtlas(lizAtlasConfigArray, lizAtlasGlobalConfig);
}
$('body').css('cursor', 'auto');
return false;
});
}
function updateAtlasData() {
// Get data
lizMap.getFeatureData(lizAtlasConfig['featureType'], lizAtlasConfig['featureType']+':', null, 'geom', false, null, null,
function(aName, aFilter, aFeatures, aAliases){
lizAtlasConfig['features'] = aFeatures;
prepareFeatures();
var options = '<option value="-1"> --- </option>';
var pkey_field = lizAtlasConfig['primaryKey'];
for(var i in lizAtlasConfig['features_sorted']){
var item = lizAtlasConfig['features_sorted'][i];
// Add option
options+= '<option value="'+i+'">';
options+= item[lizAtlasConfig['titleField']];
options+= '</option>';
}
var val = $('#liz-atlas-select').val();
$('#liz-atlas-select').html(options);
// reset val
$('#liz-atlas-select').val(val);
// get popup
$('#liz-atlas-select').change();
return false;
});
}
function prepareFeatures(lizAtlasConfig){
// Get and order features
lizAtlasConfig['features_with_pkey'] = {};
var items = [];
var pkey_field = lizAtlasConfig['primaryKey'];
var s_field = lizAtlasConfig['sortField'];
if ( !s_field )
s_field = pkey_field;
for(var i in lizAtlasConfig.features){
// Get feature
var feat = lizAtlasConfig.features[i];
var fid = feat.id.split('.').pop();
var pk_val = feat.properties[pkey_field];
// Add feature in dictionary for further ref
lizAtlasConfig['features_with_pkey'][pk_val] = feat;
// Add feature to sorted oject
items.push(feat.properties);
}
items.sort(function(a, b) {
var nameA = a[s_field];
var nameB = b[s_field];
if( typeof(nameA) == 'string' || typeof(nameB) == 'string' ){
if (!nameA)
nameA = '';
else
nameA = nameA.toUpperCase(); // ignore upper and lowercase
if (!nameB)
nameB = '';
else
nameB = nameB.toUpperCase(); // ignore upper and lowercase
}
if (nameA < nameB) {
return -1;
}
if (nameA > nameB) {
return 1;
}
// names must be equal
return 0;
});
lizAtlasConfig['features_sorted'] = items;
}
function getAtlasHome(lizAtlasConfig){
var home = '';
// Add description
if( lizAtlasConfig['displayLayerDescription'] ){
var labstract = lizMap.config.layers[lizAtlasConfig.layername]['abstract'];
if(labstract != ''){
home+= '<p id="liz-atlas-item-layer-abstract">' + lizMap.config.layers[lizAtlasConfig.layername]['abstract'] + '</p>';
}
}
// Add combobox with all data
home+= '<p style="padding:0px 10px;">';
home+= '<select id="liz-atlas-select">';
home+= '<option value="-1"> --- </option>';
var pkey_field = lizAtlasConfig['primaryKey'];
for(var i in lizAtlasConfig['features_sorted']){
var item = lizAtlasConfig['features_sorted'][i];
// Add option
home+= '<option value="'+i+'">';
home+= item[lizAtlasConfig['titleField']];
home+= '</option>';
}
home+= '</select>';
home+= '<br><span>';
home+= '<button class="btn btn-mini btn-primary liz-atlas-item" value="-1">'+lizDict['atlas.toolbar.prev']+'</button>';
home+= ' ';
home+= '<button class="btn btn-mini btn-primary liz-atlas-item" value="1">'+lizDict['atlas.toolbar.next']+'</button>';
home+= ' ';
home+= '<button class="btn btn-mini btn-wanrning liz-atlas-run" value="1">'+lizDict['atlas.toolbar.play']+'</button>';
home+= ' ';
home+= '</span>';
home+= '</span>';
home+= '</br>';
home+= '</p>';
home+= '<div id="liz-atlas-item-detail" style="display:none;">';
home+= '</div>';
home+= '</div>';
lizAtlasConfig.home = home;
return home;
}
function launchAtlas(lizAtlasConfigArray, lizAtlasGlobalConfig){
// Build select to choose between atlas layers
var select = '<select id="select-atlas-layer">';
for (var i = 0; i < lizAtlasConfigArray.length; i++) {
select += '<option value="'+lizAtlasConfigArray[i].layerId+'">'+lizMap.config.layers[lizAtlasConfigArray[i].layername]['title']+'</option>';
}
select += '</select>';
select += '<div id="atlas-content" style="border-top: #F0F0F0 dashed 1px;padding-top: 5px;"></div>';
// Add dock
lizMap.addDock(
'atlas',
lizDict['atlas.toolbar.title'],
'right-dock',
select,
'icon-globe'
);
$('#select-atlas-layer')
.change( function(){
// deactivate current atlas
deactivateAtlas();
var layerId = $(this).val();
for (var i = 0; i < lizAtlasConfigArray.length; i++) {
if(layerId === lizAtlasConfigArray[i].layerId){
lizAtlasConfig = lizAtlasConfigArray[i];
// Get Atlas home
var home = getAtlasHome(lizAtlasConfig);
$("#atlas-content").html(home);
// Add events
activateAtlasTrigger(lizAtlasConfig);
// Only if features in layer
if( lizAtlasConfig.features.length != 0 ) {
// Activate filter
if ( lizAtlasConfig.triggerFilter && lizAtlasConfig.hideFeaturesAtStratup ) {
// Select feature
lizMap.events.triggerEvent('layerfeatureselected',
{'featureType': lizAtlasConfig.featureType, 'fid': -99999, 'updateDrawing': false}
);
// Filter selected feature
lizMap.events.triggerEvent('layerfeaturefilterselected',
{'featureType': lizAtlasConfig.featureType}
);
}
}
lizMap.events.triggerEvent("uiatlascreated", lizAtlasConfig);
lizMap.events.on({
lizmapeditionfeaturecreated: function(e) {
if ( e.layerId == lizAtlasConfig.layerId )
updateAtlasData();
},
lizmapeditionfeaturemodified: function(e) {
if ( e.layerId == lizAtlasConfig.layerId )
updateAtlasData();
},
lizmapeditionfeaturedeleted: function(e) {
if ( e.layerId == lizAtlasConfig.layerId )
updateAtlasData();
}
});
// Start animation
if( lizAtlasConfig['autoPlay'] && !lizMap.checkMobile() ){
$('button.liz-atlas-run').click();
}
}
}
lizMap.events.triggerEvent("atlasready", lizAtlasConfig);
return false;
});
// Display first atlas layer
$('#select-atlas-layer').change();
// Show dock
if( lizAtlasGlobalConfig['showAtStartup'] && !lizMap.checkMobile() ){
$('#mapmenu li.atlas:not(.active) a').click();
// Hide legend
$('#mapmenu li.switcher.active a').click();
}
// Limit dock size
adaptAtlasSize();
}
function adaptAtlasSize(){
lizMap.events.on({
// Adapt dock size to display metadata
rightdockopened: function(e) {
if ( e.id == 'atlas') {
// Size : add class to content to enabled specific css to be applied
$('#content').addClass('atlas-visible');
lizMap.updateContentSize();
}
},
rightdockclosed: function(e) {
if ( e.id == 'atlas' ) {
// Set right-dock default size by removing #content class
$('#content').removeClass('atlas-visible');
lizMap.updateContentSize();
// Deactivate atlas and stop animation
deactivateAtlas();
}
}
});
}
function activateAtlasTrigger(){
$('#liz-atlas-select')
.change( function(){
var i = $(this).val();
var len = lizAtlasConfig['features_sorted'].length;
if( i == -1 ){
deactivateAtlas();
return false;
}
if( i && i>= 0 && i < len ){
var item = lizAtlasConfig['features_sorted'][i];
var pkey_field = lizAtlasConfig['primaryKey'];
if(item[pkey_field] in lizAtlasConfig['features_with_pkey'] ){
var feature = lizAtlasConfig['features_with_pkey'][item[pkey_field]];
runAtlasItem( feature );
}else{
console.log("no feature found");
}
}
return false;
});
$('#atlas div.menu-content button.liz-atlas-item')
.click(function(){
var a = parseInt($(this).val());
var curval = parseInt($('#liz-atlas-select').val());
var nextval = a + curval;
var len = lizAtlasConfig['features_sorted'].length;
if(nextval >= len){
nextval = 0;
}
if( nextval >= 0 && nextval < len){
$('#liz-atlas-select').val(nextval).change();
}
return false;
});
// Timer
$('#atlas div.menu-content button.liz-atlas-run').click(function(){
// Get button value
var a = $(this).val();
// Get animation duration
var duration = lizAtlasConfig.duration;
if( !(parseInt(duration) > 0) )
duration = 5;
var step = parseInt(duration) * 1000;
if(step < 2 || step > 60000){
step = 5000;
}
// Run or stop animation
if( a == '1' ){
// Click on the next button
$('button.liz-atlas-item[value="1"]').click();
// Change the run button value into 0
$(this).val(0);
// Change button look
$(this).text(lizDict['atlas.toolbar.stop']).addClass('btn-danger');
// Run timer
lizAtlasTimer = setInterval(function(){
// Click on then next button for each step
$('button.liz-atlas-item[value="1"]').click();
}, step);
}else{
deactivateAtlas();
}
});
}
function runAtlasItem(feature){
// Use OL tools to reproject feature geometry
var format = new OpenLayers.Format.GeoJSON();
var feat = format.read(feature)[0];
var f = feat.clone();
var proj = lizMap.config.layers[lizAtlasConfig.layername]['featureCrs'];
f.geometry.transform(proj, lizMap.map.getProjection());
// Zoom to feature
if( lizAtlasConfig['zoom']){
if( lizAtlasConfig['zoom'].toLowerCase() == 'center' ){
// center
var lonlat = f.geometry.getBounds().getCenterLonLat();
lizMap.map.setCenter(lonlat);
}
else{
// zoom
lizMap.map.zoomToExtent(f.geometry.getBounds());
}
}
// Draw feature geometry
var getLayer = lizMap.map.getLayersByName('locatelayer');
if ( lizAtlasConfig.drawFeatureGeom && getLayer.length > 0 ){
alayer = getLayer[0];
alayer.destroyFeatures();
alayer.addFeatures([f]);
}
// Display popup
if( lizAtlasConfig['atlasDisplayPopup'] ){
lizMap.getFeaturePopupContent(lizAtlasConfig.featureType, feature, function(data){
var popupContainerId = 'liz-atlas-item-detail';
// Add class to table
var popupReg = new RegExp('lizmapPopupTable', 'g');
text = data.replace( popupReg, 'table table-condensed lizmapPopupTable');
var text = '<div class="lizmapPopupContent">'+text+'</div>';
// Remove <h4> with layer title
var titleReg = new RegExp('<h4>.+</h4>');
text = text.replace(titleReg, '');
$('#'+popupContainerId).html(text).show();
// Trigger event ? a bit buggy
lizMap.events.triggerEvent("lizmappopupdisplayed", {'popup': null, 'containerId': popupContainerId} );
// Add children
lizMap.addChildrenFeatureInfo( data, popupContainerId );
// Display the plots of the children layers features filtered by popup item
lizMap.addChildrenDatavizFilteredByPopupFeature( data, popupContainerId );
});
}
// Trigger Filter
if( lizAtlasConfig['triggerFilter'] ){
var fid = feature.id.split('.').pop();
// Select feature
lizMap.events.triggerEvent('layerfeatureselected',
{'featureType': lizAtlasConfig.featureType, 'fid': fid, 'updateDrawing': false}
);
// Filter selected feature
lizMap.events.triggerEvent('layerfeaturefilterselected',
{'featureType': lizAtlasConfig.featureType}
);
}
}
function deactivateAtlas(){
// Stop animation
if( lizAtlasTimer ){
var btrun = $('#atlas div.menu-content button.liz-atlas-run');
// Change button value
btrun.val(1);
// Change button look
btrun.text(lizDict['atlas.toolbar.play']).removeClass('btn-danger');
// Reset interval and time
clearInterval(lizAtlasTimer);
lizAtlasTimer = null;
}
// Deactivate highlight
var layer = lizMap.map.getLayersByName('locatelayer');
if ( lizAtlasConfig.drawFeatureGeom && layer.length > 0 ){
layer = layer[0];
layer.destroyFeatures();
}
// Deactivate filter
if ( lizAtlasConfig.triggerFilter && lizMap.lizmapLayerFilterActive ){
if ( lizAtlasConfig.hideFeaturesAtStratup ) {
// Select feature
lizMap.events.triggerEvent('layerfeatureselected',
{'featureType': lizAtlasConfig.featureType, 'fid': -99999, 'updateDrawing': false}
);
// Filter selected feature
lizMap.events.triggerEvent('layerfeaturefilterselected',
{'featureType': lizAtlasConfig.featureType}
);
} else
lizMap.events.triggerEvent( "layerfeatureremovefilter",
{ 'featureType': lizAtlasConfig.featureType}
);
}
// Hide some containers
$('#liz-atlas-item-detail').hide();
}
} // uicreated
});
}();