-
Notifications
You must be signed in to change notification settings - Fork 1
/
WorkerTrackingFullDemo.html
615 lines (532 loc) · 22.3 KB
/
WorkerTrackingFullDemo.html
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
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
<!DOCTYPE html>
<html>
<head>
<title>Worker Tracking</title>
<link rel="stylesheet" href="http://js.arcgis.com/3.8/js/esri/css/esri.css">
<link rel="stylesheet" href="css/worker.css" />
<script src="http://js.arcgis.com/3.8/"></script>
<script>
require(["esri/map",
"esri/TimeExtent",
"esri/graphic",
"esri/layers/StreamLayer",
"esri/layers/FeatureLayer",
"esri/layers/ArcGISTiledMapServiceLayer",
"esri/InfoTemplate",
"esri/symbols/SimpleMarkerSymbol",
"esri/symbols/PictureMarkerSymbol",
"esri/symbols/TextSymbol",
"esri/symbols/Font",
"esri/symbols/SimpleLineSymbol",
"esri/renderers/SimpleRenderer",
"esri/renderers/ClassBreaksRenderer",
"esri/renderers/UniqueValueRenderer",
"dojo/_base/Color",
"dojo/dom",
"dojo/on",
"dojo/domReady!"
], function(Map, TimeExtent, Graphic, StreamLayer, FeatureLayer, ArcGISTiledMapServiceLayer, InfoTemplate, SimpleMarkerSymbol, PictureMarkerSymbol, TextSymbol, Font, SimpleLineSymbol, SimpleRenderer, ClassBreaksRenderer, UniqueValueRenderer, Color, dom, on){
var map,
trackedIds = {},
historyId,
streamLayerIncident, streamLayerWorker,
featureLayerWorker,
workerRenderer;
//make map with gray canvas basemap
map = new Map("map",{
basemap: "gray",
center: [-81.1, 37.9],
zoom: 9
});
map.on("load", function(){
//add the historic feature layer and the quarantine zone map service layer
addZoneAndHistoricLayers();
//add incident stream layer
addIncidentStreamLayer();
//add worker tracking stream layer
addWorkerStreamLayer();
});
/*
* Adds FeatureLayer to show worker track points and dynamic map layer to show quarantine zones
*/
function addZoneAndHistoricLayers(){
var mapLayerUrlZones = "http://tiles.arcgis.com/tiles/bkrWlSKcjUDFDtgw/arcgis/rest/services/QuarantineRingsMap/MapServer",
mapLayerZones = new ArcGISTiledMapServiceLayer(mapLayerUrlZones, {
opacity: 0.9
});
mapLayerZones.on("load", function(){
map.addLayer(mapLayerZones);
});
var featureLayerUrlWorker = "http://services.arcgis.com/bkrWlSKcjUDFDtgw/arcgis/rest/services/AidWorkerTracks/FeatureServer/0";
featureLayerWorker = new FeatureLayer(featureLayerUrlWorker, {
mode: FeatureLayer.MODE_SNAPSHOT
});
featureLayerWorker.on("load", function(){
workerRenderer = featureLayerWorker.renderer;
featureLayerWorker.setDefinitionExpression("0=1");
map.addLayer(featureLayerWorker);
});
}
/********************************************************************************
*
* Functions for creating Worker stream layer and handling events on the layer
*
********************************************************************************/
function addWorkerStreamLayer(){
trackedIds = {};
var url = "ws://localhost:6180/workerlocations";
//define schema of data and time info
var layerDefinition = {
"geometryType": "esriGeometryPoint",
"timeInfo": {
"startTimeField": "StartTime",
"endTimeField": null,
"trackIdField": "Name",
},
"fields": [
{
name: "ObjectId",
type: "esriFieldTypeOID",
alias: "ObjectId"
},
{
name: "Id",
type: "esriFieldTypeLong",
alias: "Id"
},
{
name: "Name",
type: "esriFieldTypeString",
alias: "Name"
},
{
name: "StartTime",
type: "esriFieldTypeString",
alias: "StartTime"
},
{
name: "EndTime",
type: "esriFieldTypeDate",
alias: "EndTime"
},
{
name: "Lat",
type: "esriFieldTypeDouble",
alias: "Lat"
},
{
name: "Lon",
type: "esriFieldTypeDouble",
alias: "Lon"
},
{
name: "Sort",
type: "esriFieldTypeLong",
alias: "Sort"
},
{
name: "UniqueId",
type: "esriFieldTypeString",
alias: "UniqueId"
}
]
};
//Make FeatureCollection to define layer without using url
var featureCollection = {
"layerDefinition": layerDefinition,
"featureSet": null
};
streamLayerWorker = new StreamLayer(featureCollection, {
socketUrl: url,
purgeOptions: { displayCount: 500 },
infoTemplate: new InfoTemplate("Name: ${Name}", "Time: ${StartTime} " )
});
//set renderer
streamLayerWorker.setRenderer( makeWorkerRenderer() );
//handle stream layer events
streamLayerWorker.on("graphic-add", processGraphicAddWorker);
streamLayerWorker.on("connect", function(){
dojo.byId("txtWsUrlWorker").style.backgroundColor = "#00ff00";
});
streamLayerWorker.on("disconnect", function(){
dojo.byId("txtWsUrlWorker").style.backgroundColor = "#ff0000";
});
//add layer to map to initiate connection to GeoEvent Processor
map.addLayer( streamLayerWorker );
}
//This function gets called when streamLayerWorker emits 'graphic-add' event
function processGraphicAddWorker(g){
var workerid,
trackIdField,
timeField,
graphic = g.graphic;
if (! streamLayerWorker.timeInfo || ! streamLayerWorker.timeInfo.trackIdField){
return false;
}
//get trackId of graphic
trackIdField = streamLayerWorker.timeInfo.trackIdField;
workerid = graphic.attributes[trackIdField];
/*
* Add div to left panel to show worker name if we have not started tracking worker.
* Also add worker to key/value object used to keep reference to incidents associated with worker
* and reference to current position of worker
*/
if (! trackedIds[workerid]){
trackedIds[workerid] = {};
makeDriverDiv(workerid);
}
//remove graphic that shows old position of worker
if (trackedIds[workerid].current){
//NOTE: This is for demo purposes. Resets worker state if the worker is at the start of the data loop
timeField = streamLayerWorker.timeInfo.startTimeField;
if (trackedIds[workerid].current.attributes[timeField] > graphic.attributes[timeField]){
console.log("Reset worker state!");
resetIncidents(workerid);
}
streamLayerWorker.remove(trackedIds[workerid].current);
delete(trackedIds[workerid].current);
}
//Add label for worker if it does not exist.
if (! trackedIds[workerid].label){
var font = new Font("10pt", Font.STYLE_NORMAL,
Font.VARIANT_NORMAL, Font.WEIGHT_BOLD,"Courier");
var text = new TextSymbol(workerid, font, new Color([0,0,139]));
text.setAlign(TextSymbol.ALIGN_START);
text.setOffset(10,2);
var tGraphic = new Graphic(graphic.geometry, text);
map.graphics.add(tGraphic);
trackedIds[workerid].label = tGraphic;
}
/*
* Add reference to current worker position graphic to the key/value object
* Update label geometry to new position of worker
* If worker has ident property set, update the position of the halo that is highlighting the worker position
*/
//
trackedIds[workerid].current = graphic;
if (trackedIds[workerid].label){
trackedIds[workerid].label.setGeometry(graphic.geometry);
}
if (trackedIds[workerid].ident){
trackedIds[workerid].ident.setGeometry(graphic.geometry);
}
}
function makeDriverDiv(workerid){
var div1,
div2,
tb2;
if (dojo.byId(workerid)){
return false;
}
div1 = dojo.create("div", {"id": workerid, "class": "vehicle", "innerHTML": "<span>" + workerid + "</span>"});
tb2 = dojo.create("input", {type: "button", id: "cmdhighlight-" + workerid, "class": "ident-button", value: "Identify Worker"});
dojo.place(tb2, div1);
dojo.connect(tb2, "onclick", function(){
toggleWorkerIdent(workerid);
});
dojo.place(div1, "workers", "after");
}
/**********************************************************************************
*
* Functions for creating Incident stream layer and handling events on the layer
*
**********************************************************************************/
function addIncidentStreamLayer(){
var url = "ws://localhost:6180/workerincident";
var layerDefinition = {
"geometryType": "esriGeometryPoint",
"timeInfo": {
"startTimeField": "timestamp",
"endTimeField": null,
"trackIdField": "trackId"
},
"fields": [
{
name: "ObjectId",
type: "esriFieldTypeOID",
alias: "ObjectId"
},
{
name: "id",
type: "esriFieldTypeString",
alias: "id"
},
{
name: "name",
type: "esriFieldTypeString",
alias: "name"
},
{
name: "timestamp",
type: "esriFieldTypeDate",
alias: "timestamp"
},
{
name: "type",
type: "esriFieldTypeString",
alias: "type"
},
{
name: "status",
type: "esriFieldTypeString",
alias: "status"
},
{
name: "alertType",
type: "esriFieldTypeString",
alias: "alertType"
},
{
name: "description",
type: "esriFieldTypeString",
alias: "description"
},
{
name: "trackId",
type: "esriFieldTypeString",
alias: "trackId"
}
]
};
var featureCollection = {
"layerDefinition": layerDefinition,
"featureSet": null
};
streamLayerIncident = new StreamLayer(featureCollection, {
socketUrl: url,
purgeOptions: { displayCount: 500 },
infoTemplate: new InfoTemplate("Name: ${trackId}", "Description: ${name} ")
});
//Make renderer and apply it to StreamLayer. Use unique value renderer with picture marker symbols
var alertRendererJson = {
type: "uniqueValue",
field1: "alertType",
fieldDelimiter: ", ",
defaultSymbol: null,
defaultLabel: null,
uniqueValueInfos: []
};
var alertRenderer = new UniqueValueRenderer(alertRendererJson);
var marker = new PictureMarkerSymbol("images/demo_biohazard_yellow.jpg",20,20);
alertRenderer.addValue("Urgent", marker);
marker = new PictureMarkerSymbol("images/demo_biohazard_red.gif",20,20);
alertRenderer.addValue("Warning", marker);
streamLayerIncident.setRenderer( alertRenderer );
streamLayerIncident.on("connect", function(){
dojo.byId("txtWsUrlIncident").style.backgroundColor = "#00ff00";
});
streamLayerIncident.on("disconnect", function(){
dojo.byId("txtWsUrlIncident").style.backgroundColor = "#ff0000";
});
streamLayerIncident.on("graphic-add", processGraphicAddIncident);
map.addLayer( streamLayerIncident );
}
//This function gets called when streamLayerIncident emits 'graphic-add' event
function processGraphicAddIncident(g){
var workerid,
trackIdField,
alertType,
graphic = g.graphic;
if (! streamLayerIncident.timeInfo || ! streamLayerIncident.timeInfo.trackIdField){
return false;
}
trackIdField = streamLayerIncident.timeInfo.trackIdField;
workerid = graphic.attributes[trackIdField];
if (! trackedIds[workerid]){
return false;
}
alertType = graphic.attributes.alertType;
//NOTE: This is for demo - to make sure there is a Warning incident paired with the Urgent incident
if (alertType === "Urgent" && (! trackedIds[workerid] || ! trackedIds[workerid].Warning)){
console.log("Not adding urgent");
streamLayerIncident.remove(graphic);
return false;
}
//only keep one of each incident type for each worker so map does not get cluttered
if (trackedIds[workerid] && trackedIds[workerid][alertType]){
streamLayerIncident.remove(trackedIds[workerid][alertType]);
delete(trackedIds[workerid][alertType]);
}
//add reference to incident to key/value object so can reference it later for showing worker track points
trackedIds[workerid][alertType] = graphic;
//update div in left panel to show new incident info
makeWarningDiv(graphic);
}
function makeWarningDiv(incidentGraphic){
if (! streamLayerIncident.timeInfo || ! streamLayerIncident.timeInfo.trackIdField || ! streamLayerIncident.timeInfo.startTimeField){
return false;
}
var workerid,
ts,
desc,
alertType,
trackIdField,
timeField,
div1,
alertdiv,
tb;
trackIdField = streamLayerIncident.timeInfo.trackIdField;
timeField = streamLayerIncident.timeInfo.startTimeField;
workerid = incidentGraphic.attributes[trackIdField];
ts = incidentGraphic.attributes[timeField];
desc = incidentGraphic.attributes.name;
alertType = incidentGraphic.attributes.alertType;
div1 = dojo.byId(workerid);
if (! div1){
makeDriverDiv(workerid);
}
div1 = dojo.byId(workerid);
dojo.replaceClass(workerid, alertType.toLowerCase());
alertdiv = dojo.byId("alert-" + workerid);
if (alertdiv){
dojo.destroy(alertdiv);
}
alertdiv = dojo.create("div", {"id": "alert-" + workerid, "innerHTML": "<p>" + desc + "</p>"});
tb = dojo.create("input", {type: "button", id: "cmdhistory-" + workerid, "class": "smallButton", value: "Show History"});
if (alertType !== "Notification"){
dojo.place(tb, alertdiv);
dojo.connect(tb, "onclick", function(){
if (dojo.attr(this, "value") === "Clear History"){
dojo.attr(this, "value", "Show History");
setLayerDef(null);
historyId = null;
}
else{
if(historyId){
var cmd = dojo.byId("cmdhistory-" + historyId);
if (cmd){
dojo.attr(cmd, "value", "Show History");
}
}
dojo.attr(this, "value", "Clear History");
historyId = workerid;
setLayerDef(workerid);
}
});
}
dojo.place(alertdiv, div1);
}
function resetIncidents(workerid){
var w,
alertDiv,
graphic;
if (trackedIds[workerid]){
for (w in trackedIds[workerid]){
graphic = trackedIds[workerid][w];
if (graphic.attributes && graphic.attributes.alertType){
streamLayerIncident.remove(graphic);
delete(trackedIds[workerid][w]);
}
}
}
alertdiv = dojo.byId("alert-" + workerid);
if (alertdiv){
dojo.destroy(alertdiv);
}
dojo.replaceClass(workerid, "vehicle");
}
/*
* Functions to show worker track history and to highlight a specific worker
*/
function toggleWorkerIdent(workerid){
var font,
textSymbol,
identGraphic,
buttonText,
button;
if (trackedIds[workerid].ident){
buttonText = "Identify Worker";
map.graphics.remove(trackedIds[workerid].ident);
delete(trackedIds[workerid].ident);
}
else{
var fillcolor = new Color([0, 255, 255, 0.3]),
outlinecolor = new Color([0, 255, 255]),
outline = new SimpleLineSymbol(SimpleLineSymbol.STYLE_SOLID, outlinecolor, 3),
symbol;
symbol = new SimpleMarkerSymbol(SimpleMarkerSymbol.STYLE_CIRCLE, 25, outline, fillcolor);
identGraphic = new Graphic(trackedIds[workerid].current.geometry, symbol);
map.graphics.add(identGraphic);
trackedIds[workerid].ident = identGraphic;
buttonText = "Remove Identify";
}
button = dojo.byId("cmdhighlight-" + workerid);
dojo.attr(button, "value", buttonText);
}
function setLayerDef(name){
var trackIdField,
timeField,
def,
msStart,
msEnd,
dtStart,
dtEnd,
strtsStart = null,
strtsEnd = null;
if (! streamLayerWorker.timeInfo || ! streamLayerWorker.timeInfo.trackIdField || ! streamLayerWorker.timeInfo.startTimeField){
return false;
}
if (! name){
def = "0=1";
}
else{
trackIdField = streamLayerWorker.timeInfo.trackIdField;
timeField = streamLayerWorker.timeInfo.startTimeField;
if (trackedIds[name].Warning){
msStart = trackedIds[name].Warning.attributes.timestamp;
}
if (trackedIds[name].Urgent){
msEnd = trackedIds[name].Urgent.attributes.timestamp;
}
dtStart = msStart ? new Date(msStart) : null;
dtEnd = msEnd ? new Date(msEnd) : null;
if (dtStart && ! dtEnd){
dtEnd = new Date(trackedIds[name].current.attributes[timeField]);
}
if (dtEnd){
strtsEnd = dtEnd.getUTCFullYear() + "-" + (dtEnd.getUTCMonth() + 1) + "-" + dtEnd.getUTCDate()
+ " " + dtEnd.getUTCHours() + ":" + dtEnd.getUTCMinutes() + ":" + dtEnd.getUTCSeconds();
}
if (dtStart){
strtsStart = dtStart.getUTCFullYear() + "-" + (dtStart.getUTCMonth() + 1) + "-" + dtStart.getUTCDate()
+ " " + dtStart.getUTCHours() + ":" + dtStart.getUTCMinutes() + ":" + dtStart.getUTCSeconds();
}
def = trackIdField + " = '" + name + "'";
if (strtsStart){
//def += " AND " + timeField + " > timestamp '" + strtsStart + "'";
def += " AND " + timeField + " > '" + strtsStart + "'";
}
if (strtsEnd){
//def += " AND " + timeField + " < timestamp '" + strtsEnd + "'";
def += " AND " + timeField + " < '" + strtsEnd + "'";
}
}
featureLayerWorker.setDefinitionExpression(def);
}
function makeWorkerRenderer(){
var fillcolor = new Color([5, 112, 176, 0.8]),
outlinecolor = new Color([255, 255, 255]),
outline = new SimpleLineSymbol(SimpleLineSymbol.STYLE_SOLID, outlinecolor, 1),
symbol,
obsrenderer;
symbol = new SimpleMarkerSymbol(SimpleMarkerSymbol.STYLE_DIAMOND, 20, outline, fillcolor);
obsRenderer = new esri.renderer.SimpleRenderer(symbol);
return obsRenderer;
}
});
</script>
</head>
<body>
<div id="leftPane" style="float: left; width: 20%">
<div id="trackStatus" style="padding: 5px; margin: 2px; background-color: #dcdcdc">
<h3>Feeds</h3>
<span id="txtWsUrlWorker" style="margin-left: 10px; width:75px"><span value="Workers">Workers</span></span>
<span id="txtWsUrlIncident" style="margin-left: 10px; width:75px"><span value="Warnings">Incidents</span></span>
</div>
<div style="padding: 5px; background-color: #dcdcdc; margin: 2px; min-height: 150px">
<h3 id="workers">Workers</h3>
</div>
</div>
<div id="map"></div>
</body>
</html>