forked from can-luckyshot/IMSpoorViewer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimx_render.js
460 lines (433 loc) · 12.2 KB
/
imx_render.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
function handleFileSelect(evt) {
evt.stopPropagation();
evt.preventDefault();
var files = evt.dataTransfer.files; // FileList object.
// files is a FileList of File objects. List some properties.
var output = [];
$.each(files, function (index, f) {
var reader = new FileReader();
reader.onload =
(function (file) {
var fileName = file.name;
console.log('file hallo: ' + fileName);
return function (event) {
var text = event.target.result;
var src = fileName;
var parser = new DOMParser();
var xmlDoc = parser.parseFromString(text, "text/xml");
parseAndRenderIMX(xmlDoc, src);
};
})(f);
reader.onerror = function (event) {
console.log('file-error: ' + event.target.error.code);
};
reader.readAsText(f);
});
}
function handleDragOver(evt) {
evt.stopPropagation();
evt.preventDefault();
evt.dataTransfer.dropEffect = 'copy'; // Explicitly show this is a copy.
}
function initDragAndDrop() {
// Setup the dnd listeners.
var dropZone = document.getElementById('map');
dropZone.addEventListener('dragover', handleDragOver, false);
dropZone.addEventListener('drop', handleFileSelect, false);
// setup projectie
}
// global for popup
var popup;
var pointLayers = makeGroup('Punt-Objecten');
var lineLayers = makeGroup('Lijn-Objecten');
var polygonLayers = makeGroup('Gebieden');
var baseLayers = makeGroup('Ondergrond');
var vectorLayers = new ol.layer.Group({
title: 'IMX Objecten',
layers: [
polygonLayers,
lineLayers,
pointLayers
]
});
function makeGroup(title) {
var group = new ol.layer.Group({
title: title,
layers: [
]
});
return group;
}
function initMap() {
console.log('initMap');
proj4.defs("EPSG:28992", "+proj=sterea +lat_0=52.15616055555555 +lon_0=5.38763888888889 +k=0.9999079 +x_0=155000 +y_0=463000 +ellps=bessel +towgs84=565.040,49.910,465.840,-0.40939,0.35971,-1.86849,4.0772");
ol.proj.proj4.register(proj4);
var tile = new ol.layer.Tile({
'title': 'Open Street Map',
source: new ol.source.OSM()
});
baseLayers.getLayers().push(tile);
map = new ol.WebGLMap({
target: 'map',
'title': 'Base map',
layers: [baseLayers,vectorLayers],
view: new ol.View({
center: ol.proj.fromLonLat([5.3, 52.23]),
zoom: 8
})
});
//var selectInteraction = new ol.interaction.Select();
//map.addInteraction(selectInteraction);
popup = new ol.Overlay({
element: document.getElementById('popup')
});
map.addOverlay(popup);
map.on('singleclick', popupSingleClick);
}
function popupSingleClick(evt) {
var element = $(popup.getElement());
console.log(evt);
var pixel = map.getPixelFromCoordinate(evt.coordinate);
var features = [];
map.forEachFeatureAtPixel(pixel, function (feature) {
if (feature.get('imxType') && feature.get('geometry').getType() !== 'Polygon') {
features.push(feature);
}
});
if (features.length > 0) {
var info = [];
var i,
ii;
for (i = 0, ii = features.length; i < ii; ++i) {
var f = features[i];
info.push(f.get('imxType') + ': ' + getIdent(f) + '<br/>');
}
element.popover('destroy');
element.popover({
'placement': 'top',
'html': true,
'content': info
});
popup.setPosition(evt.coordinate);
element.popover('show');
} else {
element.popover('destroy');
}
}
function getIdent(feature){
var name = feature.get('name');
if(name === undefined){
name = feature.get('id');
}
return name;
}
function loadDemoFile() {
$.get('file.xml', function (data) {
parseAndRenderIMX(data, 'file.xml');
});
}
function parseAndRenderIMX(xmlDoc, src) {
var objectsWithGeom = $(xmlDoc).find('GeographicLocation').parent().parent();
var typeMap = new Object();
var i = 0;
objectsWithGeom.each(function (index, objectWithGeom) {
var nodeName = objectWithGeom.nodeName;
var entry = typeMap[nodeName];
if (entry == undefined) {
var color = getColor(i++);
entry = new Object({
color: color,
list: []
});
typeMap[nodeName] = entry;
}
entry.list.push(objectWithGeom);
});
buildTypeLayers(typeMap);
setTableTypeMap(typeMap);
//buildScene(typeMap);
buildGraph();
updateLayerSwitcher();
}
function buildGraph() {}
function buildTypeLayers(typeMap) {
$.each(typeMap, function (type, entry) {
var color = entry.color;
var renderableObjects = entry.list;
//console.log(type + ' ' + renderableObjects.length + ' items');
var location = $(renderableObjects[0]).find('GeographicLocation')[0];
var geom = $(location).children()[0];
var vectorLayer = new ol.layer.Vector({
'title': type,
style: styleFunction,
source: new ol.source.Vector({}),
declutter: true
});
if (geom.nodeName == 'gml:LineString') {
createLineStringLayer(type, color, renderableObjects, vectorLayer)
} else if (geom.nodeName == 'gml:Point') {
createPointLayer(type, color, renderableObjects, vectorLayer);
} else if (geom.nodeName == 'gml:Polygon') {
createPolygonLayer(type, color, renderableObjects, vectorLayer);
} else if (geom.nodeName == 'gml:MultiPolygon') {
createMultiPolygonLayer(type, color, renderableObjects, vectorLayer);
} else if (geom.nodeName == 'gml:MultiLineString') {
createMultiLineStringLayer(type, color, renderableObjects, vectorLayer);
} else {
//console.log('onbekend: ' + geom.tagName);
}
});
}
function getColor(index) {
//var h = index * 50; // color hue between 1 and 360
//var s = 70; // saturation 30-100%
//var l = 40; // lightness 30-70%
//var color = 'hsl(' + h + ',' + s + '%,' + l + '%)';
//console.log('index: ' + index + ' - ' + color);
var num = Math.round(0xaaaaaa * Math.random());
var r = num >> 16;
var g = num >> 8 & 255;
var b = num & 255;
return 'rgb(' + r + ', ' + g + ', ' + b + ')';
return color;
}
function getPoslist($item) {
var locations = $item.find('GeographicLocation');
if (!locations) {
return undefined;
}
var geom = $(locations[0]).children()[0];
return $(geom).text().trim();
}
function getCoordinates(poslist) {
var coordinates = [];
var points = poslist.split(' ');
for (var j = 0; j < points.length; j++) {
var values = points[j].split(',')
coordinates.push([values[0], values[1]]);
}
return coordinates;
}
function getPuic($item) {
var puic = $item.attr('puic');
if (puic == undefined) {
puic = $item.attr('puicRef')
}
return puic;
}
function createPointLayer(title, color, items, vectorLayer) {
$.each(items, function (index, item) {
var $item = $(item);
var poslist = getPoslist($item);
if (poslist != undefined) {
var point = new ol.geom.Point(getCoordinates(poslist)[0]);
point.transform(ol.proj.get("EPSG:28992"), map.getView().getProjection());
var puic = getPuic($item);
if(puic == '0679c2ec-24a6-4c0b-8b29-340cf44c4740'){
console.log('stopbord: '+poslist+' '+getCoordinates(poslist));
}
var feature = new ol.Feature({
geometry: point,
id: puic,
name: $item.attr('name'),
label: $item.attr('name'),
imxType: title,
color: color
});
feature.setId(puic);
addAttributes(feature, item);
vectorLayer.getSource().addFeature(feature);
} else {
console.log('poslist undefined');
}
});
pointLayers.getLayers().push(vectorLayer);
}
function addAttributes(feature, item) {
$.each(item.attributes, function (name, value) {
if (name !== 'name' && name !== 'puic') {
feature[name] = value;
}
});
}
function createLineStringLayer(title, color, items, vectorLayer) {
$.each(items, function (index, item) {
var $item = $(item);
var poslist = getPoslist($item);
if (poslist != undefined) {
var line = new ol.geom.LineString(getCoordinates(poslist));
line.transform(ol.proj.get("EPSG:28992"), map.getView().getProjection());
var puic = getPuic($item);
var feature = new ol.Feature({
geometry: line,
id: puic,
name: $item.attr('name'),
label: $item.attr('name'),
imxType: title,
text_color: color,
stroke_color: color
});
feature.setId(puic);
addAttributes(feature, item);
vectorLayer.getSource().addFeature(feature);
} else {
console.log('poslist undefined');
}
});
lineLayers.getLayers().push(vectorLayer);
}
function createPolygonLayer(title, color, items, vectorLayer) {
//console.log('polygons: ' + title + ' itemCount: ' + items.length);
$.each(items, function (index, item) {
var $item = $(item);
var poslist = getPoslist($item);
if (poslist != undefined) {
var poly = new ol.geom.Polygon([getCoordinates(poslist)]);
poly.transform(ol.proj.get("EPSG:28992"), map.getView().getProjection());
var puic = getPuic($item);
var feature = new ol.Feature({
geometry: poly,
id: puic,
name: $item.attr('name'),
label: $item.attr('name'),
imxType: title,
text_color: color,
stroke_color: color
});
feature.setId(puic);
addAttributes(feature, item);
vectorLayer.getSource().addFeature(feature);
} else {
console.log('poslist undefined');
}
});
polygonLayers.getLayers().push(vectorLayer);
}
function createMultiPolygonLayer(title, color, items, vectorLayer) {
$.each(items, function (index, item) {
var $item = $(item);
var polys = $item.find('gml\\:Polygon');
var polyGeoms = [];
$.each(polys, function (index, poly) {
var poslist = $(poly).text().trim();
if (poslist != undefined) {
polyGeoms.push(getCoordinates(poslist));
}
});
var polygon = new ol.geom.MultiPolygon([polyGeoms]);
polygon.transform(ol.proj.get("EPSG:28992"), map.getView().getProjection());
var puic = getPuic($item);
var feature = new ol.Feature({
geometry: polygon,
id: puic,
name: $item.attr('name'),
label: $item.attr('name'),
imxType: title,
text_color: color,
stroke_color: color
});
feature.setId(puic);
addAttributes(feature, item);
vectorLayer.getSource().addFeature(feature);
});
polygonLayers.getLayers().push(vectorLayer);
}
function createMultiLineStringLayer(title, color, items, vectorLayer) {
$.each(items, function (index, item) {
var $item = $(item);
var lines = $item.find('gml\\:LineString');
var lineGeoms = [];
$.each(lines, function (index, line) {
var poslist = $(line).text().trim();
if (poslist != undefined) {
lineGeoms.push(getCoordinates(poslist));
}
});
var multiLineString = new ol.geom.MultiLineString(lineGeoms);
multiLineString.transform(ol.proj.get("EPSG:28992"), map.getView().getProjection());
var puic = getPuic($item);
var feature = new ol.Feature({
geometry: multiLineString,
id: puic,
name: $item.attr('name'),
label: $item.attr('name'),
imxType: title,
text_color: color,
stroke_color: color
});
feature.setId(puic);
addAttributes(feature, item);
vectorLayer.getSource().addFeature(feature);
});
lineLayers.getLayers().push(vectorLayer);
}
var nsResolver = function (element) {
return 'http://www.prorail.nl/IMSpoor';
};
var styleFunction = function (feature, resolution) {
var ft = feature.getGeometry().getType();
var style;
if (ft == 'Point') {
style = new ol.style.Style({
image: new ol.style.Circle({
radius: 4,
fill: new ol.style.Fill({
color: feature.get('color')
})
}),
text: new ol.style.Text({
text: feature.get('label'),
fill: new ol.style.Fill({
color: feature.get('color')
}),
offsetX: 0,
offsetY: -10,
})
});
} else if (ft == 'Polygon' || ft == 'MultiPolygon') {
style = new ol.style.Style({
stroke: new ol.style.Stroke({
color: feature.get('stroke_color'),
width: 1
}),
fill: new ol.style.Fill({
//color: feature.get('stroke_color').replace('hsl', 'hsla').replace(')', ',0.1)')
color: feature.get('stroke_color')
}),
text: new ol.style.Text({
text: feature.get('label'),
fill: new ol.style.Fill({
color: 'black'
}),
stroke: new ol.style.Stroke({
color: feature.get('stroke_color'),
width: 1
}),
offsetX: 0,
offsetY: 0,
})
});
} else if (ft == 'LineString' || ft == 'MultiLineString') {
style = new ol.style.Style({
stroke: new ol.style.Stroke({
color: feature.get('stroke_color'),
width: 2
}),
text: new ol.style.Text({
text: feature.get('label'),
fill: new ol.style.Fill({
color: feature.get('text_color')
}),
stroke: new ol.style.Stroke({
color: feature.get('text_color'),
width: 1
}),
offsetX: 0,
offsetY: -5,
})
});
} else {
console.log('unknown style');
}
return [style];
};