-
-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathselectionFromPopupGeometry_3.4.js
183 lines (152 loc) · 6.65 KB
/
selectionFromPopupGeometry_3.4.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
/**
* Add the select by geometry button for the given popup element
*
* @param {HTMLElement} featureToolbar The feature toolbar HTML element of the popup
* @param {string} featureGeometry The geometry of the feature in WKT
* @param {string} featureCrs The CRS of the geometry
*/
function addSelectionButton(featureToolbar, featureGeometry, featureCrs) {
const featureToolbarDiv = featureToolbar.querySelector('div.feature-toolbar');
// Get the button if it already exists
const buttonValue = `${featureGeometry}@${featureCrs}`;
// Selection button HTML
const selectionButtonHtml = `
<button class="btn btn-mini popup-select-by-geometry" value="${buttonValue}" type="button" data-original-title="Selection" title="Selection">
<i class="icon-star"></i>
</button>
`;
const existingButton = featureToolbarDiv.querySelector(`button.popup-select-by-geometry[value="${buttonValue}"]`);
if (existingButton) {
return false;
}
// Append the button to the toolbar
featureToolbarDiv.insertAdjacentHTML('beforeend', selectionButtonHtml);
const selectionButton = featureToolbarDiv.querySelector(`button.popup-select-by-geometry[value="${buttonValue}"]`);
// Trigger the action when clicking on button
selectionButton.addEventListener('click', popupSelectionButtonClickHandler);
}
/**
* Function which wait for te given ms before returning the promise
*
* @param {integer} time - The time in milliseconds
*
*/
function delay(time) {
return new Promise(resolve => setTimeout(resolve, time));
}
/**
* Reacts to the click on a popup select by geometry/
*
* @param event The event triggered on the button click
*/
function popupSelectionButtonClickHandler(event) {
// Only go on when the button has been clicked
// not the child <i> icon
let target = event.target;
if (!event.target.matches('.popup-select-by-geometry')) {
target = target.parentNode;
}
// Get the button which triggered the click event
const button = target;
const value = button.value;
const wkt = value.split('@')[0];
const crs = value.split('@')[1];
const featureToolbar = button.closest('lizmap-feature-toolbar');
const featureToolbarValue = featureToolbar.getAttribute('value');
// check geometry type
let isPolygon = (wkt.toUpperCase().indexOf('POLYGON') !== -1);
let isLinestring = (wkt.toUpperCase().indexOf('LINE') !== -1);
let isPoint = (wkt.toUpperCase().indexOf('POINT') !== -1);
// Create a temporary layer from the WKT
const temporaryVector = lizMap.mainLizmap.layers.addLayerFromWKT(wkt, crs, null);
// Get the features of this layer
const features = temporaryVector.getSource().getFeatures();
// Open the selection tool to activate it
const selectionMenu = document.getElementById('mapmenu').querySelector('li.selectiontool a');
let selectionMenuIsActive = selectionMenu.parentElement.classList.contains("active");
if (!selectionMenuIsActive) {
selectionMenu.click();
}
// Clear the previously drawn geometries
lizMap.mainLizmap.digitizing.drawLayer.getSource().clear();
// Add the feature created from the popup
lizMap.mainLizmap.digitizing.drawLayer.getSource().addFeature(features[0]);
// For polygons
// We must add a buffer 1 to force the transformation of multipolygon
// into compatible types. If not done, we get a <multisurface>
// built up by OpenLayers and sent to the GetFeature request, which fails
// and return no features
let hadBuffer = false;
let hadBufferValue = false;
if (isPolygon) {
const selectionToolDiv = document.getElementById('selectiontool');
const bufferInput = selectionToolDiv.querySelector('div.selectiontool-buffer input');
hadBufferValue = bufferInput.value;
if (bufferInput.value == 0) {
lizMap.mainLizmap.selectionTool._bufferValue = 0.00001;
hadBuffer = true;
}
}
// Select only inside
let oldOperator = lizMap.mainLizmap.selectionTool._geomOperator;
if (isPolygon) {
lizMap.mainLizmap.selectionTool._geomOperator = 'within';
}
// Send the event to let Lizmap run the selection
lizMap.mainEventDispatcher.dispatch('digitizing.featureDrawn');
// Remove the temporary vector layer
lizMap.mainLizmap.layers.removeLayer(temporaryVector);
// Set back the buffer if necessary
if (isPolygon && hadBuffer) {
lizMap.mainLizmap.selectionTool._bufferValue = hadBufferValue;
}
if (isPolygon) {
lizMap.mainLizmap.selectionTool._geomOperator = oldOperator;
}
// Remove the selection geometry
lizMap.mainLizmap.digitizing.drawLayer.getSource().clear();
// Deactivate back the selection tool
if (!selectionMenuIsActive) {
selectionMenu.click();
}
// // Deselect the feature which triggered the selection
delay(5000).then(() => {
// Get the popup again (it could have disappeared and reappeared after the selection
const parentToolbar = document.querySelector(`lizmap-feature-toolbar[value="${featureToolbarValue}"]`);
if (!parentToolbar) return;
const unselectButton = parentToolbar.querySelector('button.feature-select');
if (!unselectButton) return;
if (unselectButton.classList.contains('btn-primary')) {
unselectButton.click();
}
});
}
lizMap.events.on({
lizmappopupdisplayed: function (e) {
// Loop through each layer+feature item
const popupContainer = document.getElementById(e.containerId);
const items = popupContainer.querySelectorAll('div.lizmapPopupDiv');
items.forEach(function (item) {
// Get the layer ID
const featureId = item.querySelector('input.lizmap-popup-layer-feature-id').value;
// Check if there is a geometry
const featureGeometryInput = item.querySelector('input.lizmap-popup-layer-feature-geometry');
if (!featureGeometryInput) {
return;
}
const featureGeometry = featureGeometryInput.value;
// Geometry CRS
let featureCrs = 'EPSG:4326';
const featureCrsInput = item.querySelector('input.lizmap-popup-layer-feature-crs');
if (featureCrsInput) {
featureCrs = featureCrsInput.value
}
// Get feature toolbar
const featureToolbar = item.querySelector(`lizmap-feature-toolbar[value="${featureId}"]`);
// Add a button in the popup feature bar
if (featureGeometry && featureCrs) {
addSelectionButton(featureToolbar, featureGeometry, featureCrs);
}
});
}
});