/
create-project.controller.js
515 lines (468 loc) · 19.8 KB
/
create-project.controller.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
(function () {
'use strict';
/**
* Create project controller which manages creating a new project
*/
angular
.module('taskingManager')
.controller('createProjectController', ['$scope', '$location', 'mapService', 'drawService', 'projectService', 'geospatialService', 'accountService', 'authService', createProjectController]);
function createProjectController($scope, $location, mapService, drawService, projectService, geospatialService, accountService, authService) {
var vm = this;
vm.map = null;
// Wizard
vm.currentStep = '';
vm.projectName = '';
vm.projectNameForm = {};
vm.taskType = 'square-grid';
// AOI
vm.AOI = null;
vm.isDrawnAOI = false;
vm.isImportedAOI = false;
vm.clipTasksToAoi = true;
// Grid
vm.isTaskGrid = false;
vm.isTaskArbitrary = false;
vm.sizeOfTasks = 0;
vm.MAX_SIZE_OF_TASKS = 1000; //in square kilometers
vm.numberOfTasks = 0;
vm.MAX_NUMBER_OF_TASKS_SPLIT = 1500; // by limiting the split button, the user will not likely create a project with more than the max number of tasks
// Variables for the zoom level used for creating the grid
vm.DEFAULT_ZOOM_LEVEL_OFFSET = 2;
vm.initialZoomLevelForTaskGridCreation = 0;
vm.userZoomLevelOffset = 0;
// Validation
vm.isAOIValid = false;
vm.AOIValidationMessage = '';
vm.isSplitPolygonValid = true;
vm.splitPolygonValidationMessage = '';
vm.isimportError = false;
vm.nonPolygonError = false;
vm.selfIntersectionError = false;
vm.createProjectFail = false;
vm.createProjectFailReason = '';
vm.createProjectSuccess = false;
// Split tasks
vm.drawAndSelectPolygon = null;
vm.drawAndSelectPoint = null;
// Draw interactions
vm.modifyInteraction = null;
vm.drawPolygonInteraction = null;
//waiting spinner
vm.waiting = false;
vm.trimError = false;
vm.trimErrorReason = '';
activate();
function activate() {
// Check if cloning a project or creating a new one
vm.cloneProjectId = $location.search().projectId;
vm.cloneProjectName = $location.search().projectName;
if (vm.cloneProjectId) {
vm.isCloneProject = true;
// Clear the URL parameters
$location.search('projectId', null);
$location.search('projectName', null);
}
// Check if the user has the PROJECT_MANAGER or ADMIN role. If not, redirect
var session = authService.getSession();
if (session) {
var resultsPromise = accountService.getUser(session.username);
resultsPromise.then(function (user) {
// Returned the user successfully. Check the user's role
if (user.role !== 'PROJECT_MANAGER' && user.role !== 'ADMIN') {
$location.path('/');
}
}, function () {
// an error occurred, navigate to homepage
$location.path('/');
});
}
vm.currentStep = 'area';
mapService.createOSMMap('map');
mapService.addGeocoder();
vm.map = mapService.getOSMMap();
drawService.initInteractions(true, false, false, false, false, true);
vm.modifyInteraction = drawService.getModifyInteraction();
vm.drawPolygonInteraction = drawService.getDrawPolygonInteraction();
vm.drawPolygonInteraction.on('drawstart', function () {
drawService.getSource().clear();
});
projectService.initDraw(vm.map);
}
/**
* Move the wizard to appropriate step for type of tasks selected
*/
vm.setWizardStepAfterTaskTypeSelection = function () {
if (vm.taskType === 'square-grid') {
vm.createTaskGrid();
vm.setWizardStep('taskSize');
}
else if (vm.taskType === 'arbitrary-tasks') {
vm.createArbitaryTasks();
vm.setWizardStep('review');
}
};
/**
* Set the current wizard step in the process of creating a project
* @param wizardStep the step in the wizard the user wants to go to
*/
vm.setWizardStep = function (wizardStep) {
if (wizardStep === 'area') {
vm.isTaskGrid = false;
vm.isTaskArbitrary = false;
projectService.removeTaskGrid();
vm.currentStep = wizardStep;
if (vm.isDrawnAOI) {
vm.drawPolygonInteraction.setActive(true);
vm.modifyInteraction.setActive(true);
}
}
else if (wizardStep === 'tasks') {
setSplitToolsActive_(false);
vm.zoomLevelForTaskGridCreation = mapService.getOSMMap().getView().getZoom()
+ vm.DEFAULT_ZOOM_LEVEL_OFFSET;
// Reset the user zoom level offset
vm.userZoomLevelOffset = 0;
if (vm.isDrawnAOI) {
var aoiValidationResult = projectService.validateAOI(drawService.getSource().getFeatures());
vm.isAOIValid = aoiValidationResult.valid;
vm.AOIValidationMessage = aoiValidationResult.message;
if (vm.isAOIValid) {
vm.map.getView().fit(drawService.getSource().getExtent());
// Use the current zoom level + a standard offset to determine the default task grid size for the AOI
vm.currentStep = wizardStep;
vm.drawPolygonInteraction.setActive(false);
vm.modifyInteraction.setActive(false);
}
}
if (vm.isImportedAOI) {
// TODO: validate AOI - depends on what API supports! Self-intersecting polygons?
vm.drawPolygonInteraction.setActive(false);
vm.map.getView().fit(drawService.getSource().getExtent());
vm.currentStep = wizardStep;
vm.drawPolygonInteraction.setActive(false);
vm.modifyInteraction.setActive(false);
}
}
else if (wizardStep === 'taskSize') {
var grid = projectService.getTaskGrid();
if (grid) {
vm.currentStep = wizardStep;
}
}
else if (wizardStep === 'trim') {
vm.trimError = false;
vm.trimErrorReason = '';
vm.currentStep = wizardStep;
}
else if (wizardStep === 'review') {
setSplitToolsActive_(false);
vm.createProjectFailed = false;
vm.createProjectFailReason = '';
vm.currentStep = wizardStep;
}
else {
vm.currentStep = wizardStep;
}
};
/**
* Decides if a step should be shown as completed in the progress bar
* @param wizardStep
* @returns {boolean}
*/
vm.showWizardStep = function (wizardStep) {
var showStep = false;
if (wizardStep === 'area') {
if (vm.currentStep === 'area' || vm.currentStep === 'tasks' || vm.currentStep === 'taskSize' || vm.currentStep === 'trim' || vm.currentStep === 'review') {
showStep = true;
}
}
else if (wizardStep === 'tasks') {
if (vm.currentStep === 'tasks' || vm.currentStep === 'taskSize' || vm.currentStep === 'trim' || vm.currentStep === 'review') {
showStep = true;
}
}
else if (wizardStep === 'taskSize') {
if (vm.currentStep === 'taskSize' || vm.currentStep === 'trim' || vm.currentStep === 'review') {
showStep = true;
}
}
else if (wizardStep === 'trim') {
if (vm.currentStep === 'trim' || vm.currentStep === 'review') {
showStep = true;
}
}
else if (wizardStep === 'review') {
if (vm.currentStep === 'review') {
showStep = true;
}
}
else {
showStep = false;
}
return showStep;
};
/**
* Draw Area of Interest
*/
vm.drawAOI = function () {
vm.drawPolygonInteraction.setActive(true);
vm.isDrawnAOI = true;
vm.isImportedAOI = false;
};
/**
* Trim the task grid to the AOI
*/
vm.trimTaskGrid = function () {
var taskGrid = projectService.getTaskGrid();
vm.waiting = true;
var trimTaskGridPromise = projectService.trimTaskGrid(vm.clipTasksToAoi);
trimTaskGridPromise.then(function (data) {
vm.waiting = false;
vm.trimError = false;
vm.trimErrorReason = '';
projectService.removeTaskGrid();
var tasksGeoJson = geospatialService.getFeaturesFromGeoJSON(data, 'EPSG:3857');
projectService.setTaskGrid(tasksGeoJson);
projectService.addTaskGridToMap();
// Get the number of tasks in project
vm.numberOfTasks = projectService.getNumberOfTasks();
}, function (reason) {
vm.waiting = false;
vm.trimError = true;
vm.trimErrorReason = reason.status
})
};
/**
* Create arbitrary tasks
*/
vm.createArbitaryTasks = function () {
if (vm.isImportedAOI) {
vm.isTaskGrid = false;
vm.isTaskArbitrary = true;
projectService.removeTaskGrid();
// Get and set the AOI
var areaOfInterest = drawService.getSource().getFeatures();
projectService.setAOI(areaOfInterest);
// Get the number of tasks in project
vm.numberOfTasks = drawService.getSource().getFeatures().length;
}
}
/**
* Create a task grid
*/
vm.createTaskGrid = function () {
vm.isTaskGrid = true;
vm.isTaskArbitrary = false;
// Remove existing task grid
projectService.removeTaskGrid();
// Get and set the AOI
var areaOfInterest = drawService.getSource().getFeatures();
projectService.setAOI(areaOfInterest);
// Create a task grid
if (vm.isDrawnAOI || vm.isImportedAOI) {
var aoiExtent = drawService.getSource().getExtent();
var taskGrid = projectService.createTaskGrid(aoiExtent, vm.zoomLevelForTaskGridCreation + vm.userZoomLevelOffset);
projectService.setTaskGrid(taskGrid);
projectService.addTaskGridToMap();
// Get the number of tasks in project
vm.numberOfTasks = projectService.getNumberOfTasks();
// Get the size of the tasks
vm.sizeOfTasks = projectService.getTaskSize();
}
};
/**
* Change the size of the tasks in the grid by increasing or decreasing the zoom level
* @param zoomLevelOffset
*/
vm.changeSizeTaskGrid = function (zoomLevelOffset) {
vm.userZoomLevelOffset += zoomLevelOffset;
vm.createTaskGrid();
};
/**
* Import a GeoJSON, KML or Shapefile and add it to the map
* TODO: add more error handling
* @param file
*/
vm.import = function (file) {
// Set drawing an AOI to inactive
vm.drawPolygonInteraction.setActive(false);
vm.isimportError = false;
vm.nonPolygonError = false;
vm.selfIntersectionError = false;
if (file) {
drawService.getSource().clear();
var fileReader = new FileReader();
fileReader.onloadend = function (e) {
var data = e.target.result;
var uploadedFeatures = null;
if (file.name.substr(-4).toLowerCase() === 'json') {
uploadedFeatures = geospatialService.getFeaturesFromGeoJSON(data);
setUploadedFeatures(uploadedFeatures);
}
else if (file.name.substr(-3).toLowerCase() === 'kml') {
uploadedFeatures = geospatialService.getFeaturesFromKML(data);
setUploadedFeatures(uploadedFeatures);
}
else if (file.name.substr(-3).toLowerCase() === 'zip') {
// Use the Shapefile.js library to read the zipped Shapefile (with GeoJSON as output)
shp(data).then(function (geojson) {
var uploadedFeatures = geospatialService.getFeaturesFromGeoJSON(geojson);
setUploadedFeatures(uploadedFeatures);
});
}
};
if (file.name.substr(-4).toLowerCase() === 'json') {
fileReader.readAsText(file);
}
else if (file.name.substr(-3).toLowerCase() === 'kml') {
fileReader.readAsText(file);
}
else if (file.name.substr(-3).toLowerCase() === 'zip') {
fileReader.readAsArrayBuffer(file);
}
else {
vm.isImportError = true;
vm.nonPolygonError = false;
vm.selfIntersectionError = false;
}
}
};
/**
* Set the uploaded features
* @param uploadedFeatures
*/
function setUploadedFeatures(uploadedFeatures) {
if (uploadedFeatures) {
var aoiValidationResult = projectService.validateAOI(uploadedFeatures);
if (aoiValidationResult.valid) {
setImportedAOI_(uploadedFeatures)
}
else {
if (aoiValidationResult.message == 'CONTAINS_NON_POLYGON_FEATURES') {
vm.nonPolygonError = true;
}
else if (aoiValidationResult.message == 'SELF_INTERSECTIONS') {
vm.selfIntersectionError = true;
}
}
}
}
/**
* Set the AOI to the imported AOI
* @param features
* @private
*/
function setImportedAOI_(features) {
vm.isImportedAOI = true;
vm.isDrawnAOI = false;
projectService.setAOI(features);
drawService.getSource().addFeatures(features);
vm.map.getView().fit(drawService.getSource().getExtent());
}
/**
* Lets the user draw an area (polygon).
* After drawing it, the polygon is validated before splitting the intersecting
* tasks into smaller tasks
*/
vm.drawAndSplitAreaPolygon = function () {
setSplitToolsActive_(false);
// Draw and select interaction - Polygon
if (!vm.drawAndSelectPolygon) {
var map = mapService.getOSMMap();
vm.drawAndSelectPolygon = new ol.interaction.Draw({
type: "Polygon"
});
map.addInteraction(vm.drawAndSelectPolygon);
// After drawing the polygon, validate it and split if valid
vm.drawAndSelectPolygon.on('drawend', function (event) {
var aoiValidationResult = projectService.validateAOI([event.feature]);
// Start an Angular digest cycle manually to update the view
$scope.$apply(function () {
vm.isSplitPolygonValid = aoiValidationResult.valid;
vm.splitPolygonValidationMessage = aoiValidationResult.message;
if (vm.isSplitPolygonValid) {
projectService.splitTasks(event.feature);
// Get the number of tasks in project
vm.numberOfTasks = projectService.getNumberOfTasks();
}
});
});
}
vm.drawAndSelectPolygon.setActive(true);
};
/**
* Lets the user draw point.
* After drawing it, the point is validated before splitting the intersecting
* tasks into smaller tasks
*/
vm.drawAndSplitAreaPoint = function () {
setSplitToolsActive_(false);
// Draw and select interaction - Point
if (!vm.drawAndSelectPoint) {
var map = mapService.getOSMMap();
vm.drawAndSelectPoint = new ol.interaction.Draw({
type: "Point"
});
map.addInteraction(vm.drawAndSelectPoint);
// After drawing the point, split it
vm.drawAndSelectPoint.on('drawend', function (event) {
// Start an Angular digest cycle manually to update the view
$scope.$apply(function () {
projectService.splitTasks(event.feature);
// Get the number of tasks in project
vm.numberOfTasks = projectService.getNumberOfTasks();
});
});
}
vm.drawAndSelectPoint.setActive(true);
};
/**
* Create a new project with a project name
*/
vm.createProject = function () {
var cloneProjectId = null;
if (vm.isCloneProject) {
cloneProjectId = vm.cloneProjectId;
}
vm.createProjectFail = false;
vm.createProjectSuccess = false;
if (vm.projectNameForm.$valid) {
vm.waiting = true;
var resultsPromise = projectService.createProject(vm.projectName, vm.isTaskGrid, cloneProjectId);
resultsPromise.then(function (data) {
vm.waiting = false;
// Project created successfully
vm.createProjectFail = false;
vm.createProjectSuccess = true;
vm.createProjectFailReason = '';
// Navigate to the edit project page
$location.path('/admin/edit-project/' + data.projectId);
}, function (reason) {
vm.waiting = false;
// Project not created successfully
vm.createProjectFail = true;
vm.createProjectSuccess = false;
vm.createProjectFailReason = reason.status
});
}
else {
vm.projectNameForm.submitted = true;
}
};
/**
* Set split tools to active/inactive
* @param boolean
*/
function setSplitToolsActive_(boolean) {
if (vm.drawAndSelectPolygon) {
vm.drawAndSelectPolygon.setActive(boolean);
}
if (vm.drawAndSelectPoint) {
vm.drawAndSelectPoint.setActive(boolean);
}
}
vm.toggleClipTasksToAoi = function () {
vm.clipTasksToAoi = !vm.clipTasksToAoi;
};
}
})();