-
-
Notifications
You must be signed in to change notification settings - Fork 41
/
GPX_3.4.js
386 lines (343 loc) · 13.1 KB
/
GPX_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
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
lizMap.events.on({
'uicreated': function(e) {
// Activate GPX manager tool when the map loads
var activateGpxOnStartup = true;
// File format based on extension
var gpxFileFormat = new OpenLayers.Format.GPX();
var gpxFileExt = 'ext';
// Add Dock
addGpxDock();
// Activate tools
initGpxView(activateGpxOnStartup);
},
'minidockclosed': function(e) {
if ( e.id == 'gpx-manager' ) {
$("#gpx_none_toggle").click();
}
}
});
function addGpxDock(){
// Build HTML interface
var html = '';
html+= '<div id="gpx_form_container" style="">';
html+= '<form class="" id="gpx_form">';
html+= '<div class="control-group">';
html+= ' <div class="controls">';
html+= ' <h4 style="font-weight:bold !important;">Ajouter une trace GPX/KML/GeoJSON</h4>';
html+= ' <input type="file" style="margin-bottom:5px;" class="form-control-file" id="gpx_file" name="gpx_file" accept=".gpx, .kml, .geojson">';
html+= ' </div>';
html+= ' <div id="controlToggle" class="controls">';
html+= ' <h4 style="font-weight:bold !important;">Modifier les lignes</h4>';
html+= ' <div style="width:100%;">';
html+= ' <button type="button" class="btn btn-block" value="none" id="gpx_none_toggle" >Désactiver édition</button>';
html+= ' <button type="button" class="btn btn-block" value="gpx_line" id="gpx_line_toggle" >Dessiner une trace</button>';
html+= ' <button type="button" class="btn btn-block" value="gpx_modify" id="gpx_modify_toggle">Modifier une trace</button>';
html+= ' <button type="button" class="btn btn-block" value="gpx_delete" id="gpx_delete_toggle">Supprimer une trace</button>';
html+= ' <button type="button" class="btn btn-block" value="clear" id="gpx_clear_layer">Reinitialiser la couche</button>';
html+= ' </div>';
html+= ' </div>';
html+= ' <div class="controls">';
html+= ' <h4 style="font-weight:bold !important;">Exporter les données</h4>';
html+= ' <div style="width:100%;">';
html+= ' <button type="button" id="gpx_export" style="margin-top:10px;" class="btn btn-block">Exporter</button>';
html+= ' </div>';
html+= ' </div>';
html+= '</div>';
html+= '<div style="display:none;">';
html+= ' <input id="gpx_create_vertices" type="checkbox" checked';
html+= ' name="gpx_create_vertices" />';
html+= ' <label for="gpx_create_vertices">allow vertices creation</label>';
html+= '</div>';
html+= '</form>'
html+= '</div>'
// Add Lizmap minidock
lizMap.addDock(
'gpx-manager',
'Gestion de traces',
'minidock',
html,
'icon-road'
);
}
// Initialize GPX manager functions and handlers
function initGpxView(activateGpxOnStartup) {
var map, vectors, controls, fileName;
var gpxLayer;
var defaultFillColor = "yellow";
var defaultStrokeColor = "orange";
var defaultStrokeWidth = 5;
var selectFillColor = "orange";
var selectStrokeColor = "red";
var selectStrokeWidth = 5;
var pointRadius = 6;
var save = 0;
var center;
var editButton;
// Clear all controls
function clearControl(){
for(var key in controls) {
lizMap.map.removeControl(controls[key]);
}
}
// Create a new gpx OpenLayers vector layer
function createNewLayer(){
var myStyles = new OpenLayers.StyleMap({
"default": new OpenLayers.Style({
pointRadius: pointRadius,
fillColor: defaultFillColor,
strokeColor: defaultStrokeColor,
strokeWidth: defaultStrokeWidth
}),
"select": new OpenLayers.Style({
pointRadius: pointRadius,
fillColor: selectFillColor,
strokeColor: selectStrokeColor,
strokeWidth: selectStrokeWidth
})
});
vectors = new OpenLayers.Layer.Vector('gpxlayer', {
styleMap: myStyles
});
controls = {
gpx_point: new OpenLayers.Control.DrawFeature(
vectors,
OpenLayers.Handler.Point
),
gpx_line: new OpenLayers.Control.DrawFeature(
vectors,
OpenLayers.Handler.Path, {
eventListeners: {
featureadded: function(event) {
$('#lizmap-gpx-message').remove();
}
}
}
),
gpx_polygon: new OpenLayers.Control.DrawFeature(
vectors,
OpenLayers.Handler.Polygon
),
gpx_modify: new OpenLayers.Control.ModifyFeature(
vectors
),
gpx_delete: new OpenLayers.Control.SelectFeature(
vectors,
{
clickout: true, toggle: true,
multiple: false, hover: false,
box: false,
eventListeners: {
featurehighlighted: function overlay_delete(event) {
var feature = event.feature;
if( confirm('Delete selected line ?') ) {
vectors.removeFeatures( [ feature ] );
}else{
this.unselect(feature);
}
}
}
}
)
};
for(var key in controls) {
lizMap.map.addControl(controls[key]);
}
}
// Create gpx layer and add it to the map
createNewLayer();
lizMap.map.addLayer(vectors);
// Change modify feature control to allow updating feature
function update() {
controls.modify.mode = OpenLayers.Control.ModifyFeature.RESHAPE;
controls.modify.gpx_create_vertices = true;
return false;
}
// Toggle control based on element name
function toggleControl(element) {
for(key in controls) {
var control = controls[key];
if(element.value == key) {
control.activate();
} else {
control.deactivate();
}
}
}
// Deactivate edition
$("#gpx_none_toggle").click(function(){
if(editButton != undefined){
$("#" + editButton.id).removeClass("btn-info");
editButton = this;
}else{
editButton = this;
}
//console.log(this);
$("#" + this.id).addClass("btn-info");
toggleControl(this);
$('#lizmap-gpx-message').remove();
return false;
});
// Activate line drawing control
$("#gpx_line_toggle").click(function(){
if(editButton != undefined){
$("#" + editButton.id).removeClass("btn-info");
editButton = this;
}else{
editButton = this;
}
//console.log(this);
$("#" + this.id).addClass("btn-info");
toggleControl(this);
$('#lizmap-gpx-message').remove();
var msg = 'You can draw one or more lines. Click to add a node, double-click to finish a line';
lizMap.addMessage(msg,'info',true).attr('id','lizmap-gpx-message');
return false;
});
// Activate modify control
$("#gpx_modify_toggle").click(function(){
if(editButton != undefined){
$("#" + editButton.id).removeClass("btn-info");
editButton = this;
}else{
editButton = this;
}
$("#" + this.id).addClass("btn-info");
toggleControl(this);
$('#lizmap-gpx-message').remove();
var msg = 'You can click on one line then use the handles to modify the nodes: drag & drop nodes, clic on a node and use the Del key to delete the node.';
lizMap.addMessage(msg,'info',true).attr('id','lizmap-gpx-message');
return false;
});
// Activate delete feature control
$("#gpx_delete_toggle").click(function(){
if(editButton != undefined){
$("#" + editButton.id).removeClass("btn-info");
editButton = this;
}else{
editButton = this;
}
$("#" + this.id).addClass("btn-info");
toggleControl(this);
$('#lizmap-gpx-message').remove();
var msg = 'You can click on one line to select and delete it';
lizMap.addMessage(msg,'info',true).attr('id','lizmap-gpx-message');
return false;
});
$("#gpx_create_vertices").change(function(){
update();
return false;
});
// Read data from given file and add features to the map
function addLayerFile(){
var reader = new FileReader();
var fileInput = document.querySelector('#gpx_file');
var result;
var fileName = $('#gpx_file')[0].files[0].name;
var ext = fileName.split('.')[1];
fileName = fileName.split('.')[0];
if(ext.toLowerCase() == 'gpx'){
format = (new OpenLayers.Format.GPX());
}else if(ext.toLowerCase() == 'kml'){
format = (new OpenLayers.Format.KML());
}else if(ext.toLowerCase() == 'geojson'){
format = (new OpenLayers.Format.GeoJSON());
}
gpxFileFormat = format;
gpxFileExt = ext;
vectors.setName(fileName);
reader.addEventListener('load', function() {
result = reader.result;
var features = format.read(result);
for(var i in features){
feat = features[i];
feat.geometry.transform(
new OpenLayers.Projection("EPSG:4326"),
lizMap.map.getProjection()
);
}
vectors.addFeatures(features);
lizMap.map.zoomToExtent(vectors.getDataExtent());
$('#lizmap-gpx-message').remove();
var msg = 'Data from given file has been successfully added to the map. You can now edit the geometries.';
lizMap.addMessage(msg,'info',true).attr('id','lizmap-gpx-message');
});
reader.readAsText(fileInput.files[0]);
}
// Add file data on file change
$("#gpx_file").change(function(){
if(save == 1){
save = 0;
addLayerFile();
}else{
if( vectors.features.length > 0 ){
if(confirm('Une couche est déjà présente, voulez-vous la remplacer ?')){
vectors.destroyFeatures();
}
}
addLayerFile();
}
});
function export_file() {
fileName = prompt("Entrez le nom du fichier (avec extension .gpx, .geojson ou .kml)", fileName);
if(fileName != null && fileName != ""){
var features = vectors.features;
var features_clone = [];
for(var i in features){
feat = features[i];
feat_clone = feat.clone();
feat_clone.geometry.transform(
lizMap.map.getProjection(),
new OpenLayers.Projection("EPSG:4326")
);
features_clone.push(feat_clone);
}
var pieces = fileName.split('.');
var ext = pieces[pieces.length-1];
if(ext.toLowerCase() == 'gpx'){
format = (new OpenLayers.Format.GPX());
}else if(ext.toLowerCase() == 'kml'){
format = (new OpenLayers.Format.KML());
}else if(ext.toLowerCase() == 'geojson'){
format = (new OpenLayers.Format.GeoJSON());
}else {
alert("Veuillez saisir une extension parmi .gpx, .geojson ou .kml. Exemple: export.kml !")
export_file();
}
gpxFileFormat = format;
var gpxContent = gpxFileFormat.write(features_clone);
var element = document.createElement('a');
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(gpxContent));
element.setAttribute('download', fileName);
element.style.display = 'none';
document.body.appendChild(element);
save = 1;
element.click();
}else{
alert("Veuillez saisir un nom de fichier !")
}
}
// Export layers to GPX or KML or GeoJSON
$("#gpx_export").click(function(){
export_file();
});
// Clear layer: destroy all features
$("#gpx_clear_layer").click(function(){
if(lizMap.map.getLayer(vectors.id)){
if(save == 1){
vectors.destroyFeatures();
save = 0;
}else if(confirm("Une couche non exportée est présente. Souhaitez-vous néanmoins vider le projet ?")){
vectors.destroyFeatures();
}
}
});
// Activate drawing on start
if(activateGpxOnStartup){
// Show dock
if( !($('#gpx-manager').hasClass('active')) ){
$('#mapmenu li.gpx-manager:not(.active) a').click();
}
// Activate line drawing
$('#gpx_line_toggle').click();
}
}