This repository has been archived by the owner on Aug 28, 2020. It is now read-only.
/
SpeciesController.php
616 lines (522 loc) · 22.8 KB
/
SpeciesController.php
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
616
<?php
App::uses('AppController', 'Controller');
App::uses('User', 'Model');
App::uses('Vetting', 'Model');
/**
* Species Controller
*
* @property Species $Species
*/
class SpeciesController extends AppController {
public $components = array('RequestHandler');
public $helpers = array('Form', 'Html', 'Js', 'Time');
const DOWNLOAD_URL_PREFIX = "https://eresearch.jcu.edu.au/tdh/datasets/Edgar/";
// Don't allow a user to insert a vetting unless they are logged in.
public function beforeFilter() {
parent::beforeFilter();
$this->Auth->deny(array('add_vetting'));
}
/**
* index method
*
* @return void
*/
public function index() {
$this->set('title_for_layout', 'Species - Index');
$this->set('species', $this->paginate());
// Specify the output for the json view.
$this->set('_serialize', 'species');
}
/**
* view method
*
* @param string $id
* @return void
*/
public function view($id = null) {
$this->set('title_for_layout', 'Species - View');
$this->Species->recursive = 1;
$this->Species->id = $id;
if (!$this->Species->exists()) {
throw new NotFoundException(__('Invalid species'));
}
$this->set('species', $this->Species->read(null, $id));
// Specify the output for the json view.
$this->set('_serialize', 'species');
}
/**
* occurrences method
*
* @param string $id
* @return void
*/
public function occurrences($id = null) {
$this->set('title_for_layout', 'Species - Occurrences');
$this->Species->id = $id;
if (!$this->Species->exists()) {
throw new NotFoundException(__('Invalid species'));
}
$species = $this->Species->read(null, $id);
$occurrences = $species['Occurrence'];
$this->set('species', $species);
$this->set('occurrences', $occurrences);
// Specify the output for the json view.
$this->set('_serialize', 'occurrences');
}
/**
* geo_json_occurrences method
*
* Takes the following http_request_params:
* * bbox
*
* bbox is a comma seperated string representing the bounds of the user's view.
* e.g. whole world, zoomed out:
* -607.5,-405,607.5,405
*
* e.g. bottom left corner of world, zoomed in:
* -198.984375,-102.65625,-123.046875,-52.03125
*
* 1st pair is the bottom left corner of the bounds.
* 2nd pair is the top right corner of the bounds.
*
* Produces a GeoJSON object of type FeatureCollection.
* Should only produce, at most, 1000 features.
*
* @param string $id
* @return void
*/
public function geo_json_occurrences($id = null) {
$this->set('title_for_layout', 'Species - GeoJSON Occurrences');
$this->Species->id = $id;
if (!$this->Species->exists()) {
throw new NotFoundException(__('Invalid species'));
}
// Look up the species provided
$species = $this->Species->read(null, $id);
// Check if we were provided a bbox (bounding box)
$bbox = array();
if ( array_key_exists('bbox', $this->request->query) ) {
$bbox_param_string = $this->params->query['bbox'];
$bbox_param_as_array = explode(',', $bbox_param_string);
if ( sizeof($bbox_param_as_array) == 4 ) {
$bbox = array(
"min_longitude" => $bbox_param_as_array[0],
"min_latitude" => $bbox_param_as_array[1],
"max_longitude" => $bbox_param_as_array[2],
"max_latitude" => $bbox_param_as_array[3],
);
}
}
// Check if clustered was set to true.
// Default clustered to true
$cluster_type = 'dotradius';
if ( array_key_exists('clustered', $this->request->query) ) {
$cluster_type = $this->request->query['clustered'];
}
$limit = null;
if ( array_key_exists('limit', $this->request->query) ) {
$limit = $this->request->query['limit'];
}
$offset = 0;
if ( array_key_exists('offset', $this->request->query) ) {
$offset = $this->request->query['offset'];
}
$this->set('geo_object', $this->Species->toGeoJSONArray($bbox, $cluster_type, $offset, $limit));
// Specify the output for the json view.
$this->set('_serialize', 'geo_object');
}
/*
Get the vetting geojson for the given species.
By default, gets all vettings for given species id.
Can be filtered to only include vettings by a specific user.
Filter can be inversed (i.e. all vettings NOT by specific user).
TODO: Remove magic numbers from the SQL LIMIT
params:
* species_id: the species to get the vettings for
* by_user_id: get vettings by a specific user_id
* inverse_user_id_filter: if set, will get vettings NOT by by_user_id
*/
public function vetting_geo_json($species_id = null) {
$this->Species->id = $species_id;
if (!$this->Species->exists()) {
throw new NotFoundException(__('Invalid species'));
}
// Process any filter variables
$by_user_id = false;
$inverse_user_id_filter = false;
if (array_key_exists('by_user_id', $this->request->query)) {
$by_user_id = (int)$this->request->query['by_user_id'];
}
if(array_key_exists('inverse_user_id_filter', $this->request->query)) {
$inverse_user_id_filter = $this->request->query['inverse_user_id_filter'];
}
$results = null;
if ($by_user_id and $inverse_user_id_filter) {
$results = $this->Species->getDataSource()->execute(
'SELECT vettings.id, ST_AsGeoJSON(area), classification, comment, fname, lname '.
'FROM vettings INNER JOIN users ON user_id = users.id '.
'WHERE species_id = ? AND user_id <> ? AND deleted is NULL '.
'LIMIT 1000',
array(),
array($species_id, $by_user_id)
);
} elseif ($by_user_id) {
$results = $this->Species->getDataSource()->execute(
'SELECT vettings.id, ST_AsGeoJSON(area), classification, comment, fname, lname '.
'FROM vettings INNER JOIN users ON user_id = users.id '.
'WHERE species_id = ? AND user_id = ? AND deleted is NULL '.
'LIMIT 1000',
array(),
array($species_id, $by_user_id)
);
} else {
$results = $this->Species->getDataSource()->execute(
'SELECT vettings.id, ST_AsGeoJSON(area), classification, comment, fname, lname '.
'FROM vettings INNER JOIN users ON user_id = users.id '.
'WHERE species_id = ? AND deleted is NULL '.
'LIMIT 1000',
array(),
array($species_id)
);
}
// Convert the received vettings into a geometry collection.
$geo_json_features_array = array();
if($results) {
while ($row = $results->fetch(PDO::FETCH_NUM, PDO::FETCH_ORI_NEXT)) {
$vetting_id = $row[0];
$area_json = $row[1];
$classification = $row[2];
$comment = $row[3];
$user = $row[4] . ' ' . $row[5];
$properties_json_array = Vetting::getPropertiesJSONObject($classification);
$properties_json_array['classification'] = $classification;
$properties_json_array['vetting_id'] = $vetting_id;
$properties_json_array['comment'] = $comment;
$properties_json_array['user'] = $user;
// decode the json
array_push($geo_json_features_array, array('type' => 'Fetaure', 'geometry' => json_decode($area_json), 'properties' => $properties_json_array));
}
}
$geo_json_object = array(
'type' => 'FeatureCollection',
'features' => $geo_json_features_array
);
$geo_json = json_encode($geo_json_object);
$this->set('geo_json', $geo_json_object);
$this->set('_serialize', 'geo_json');
}
/*
Insert vetting geojson for the given species.
*/
public function add_vetting($species_id = null) {
// Get the auth'd user.
// NOTE: Prefilter means the user can't be here unless they are logged in.
$user_id = $this->Auth->user('id');
if ($this->request->is('post')) {
$this->Species->recursive = 0;
$this->Species->id = $species_id;
if (!$this->Species->exists()) {
throw new NotFoundException(__('Invalid species'));
}
$this->set('species', $this->Species->read(null, $species_id));
// Specify the output for the json view.
$this->set('_serialize', 'species');
$jsonData = json_decode(utf8_encode(trim(file_get_contents('php://input'))), true);
if ($jsonData !== null) {
// At this point, we have the json data.
// Do the work on it.
$this->set('jsonData', $jsonData);
$area = $jsonData['area'];
$this->set('_serialize', 'jsonData');
$comment = $jsonData['comment'];
$classification = $jsonData['classification'];
$dbo = $this->Species->Vetting->getDataSource();
$escaped_area = $dbo->value($area);
$dbo->execute(
"INSERT INTO vettings (user_id, species_id, comment, classification, area) VALUES ( ? , ? , ? , ? , ST_Multi(ST_Buffer( ( ST_GeomFromText(".$escaped_area.", 4326) ), 0) ) )",
array(),
array($user_id, $species_id, $comment, $classification)
);
$this->Species->markAsNeedingVetting($species_id);
$this->dieWithStatus(200, '{ "result": "success" }');
} else {
$this->dieWithStatus(400, "Bad JSON Input");
}
}
}
/**
* single_upload_json method
*
* @return void
*/
public function single_upload_json() {
$this->set('title_for_layout', 'Species - Single Species Upload (JSON)');
// If user did a HTTP_POST, process the upload file
if ($this->request->is('post')) {
// File: data['Species']['upload_file'] => (array)
$file = $this->request->data['Species']['upload_file'];
// File's array:
// name => (name of file)
// type => (file type, e.g. image/jpeg)
// tmp_name => (file_path)
// error => 0
// size => (file_size in Bytes)
$file_name = $file['name'];
$file_type = $file['type'];
$tmp_file_path= $file['tmp_name'];
// Expected file type is application/json
$file_contents = file_get_contents($tmp_file_path);
$json_decoded_file_contents = json_decode($file_contents, true);
if ($this->Species->saveAssociated($json_decoded_file_contents)) {
$this->Session->setFlash(__('The species has been saved'));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The species could not be saved. Please, try again.'));
}
}
// Else -> Fall through to render view (form to upload a single species json file)
}
/**
* map method
*
* @param string $id
* @return void
*/
public function map($id = null) {
$this->set('title_for_layout', 'Species - Map');
if ($id == null) {
$this->set('species', null);
} else {
$result = $this->Species->find('first', array(
'conditions' => array('id' => $id)
));
if($result !== false){
$this->set('species', $this->_speciesToJson($result['Species']));
} else {
throw new NotFoundException(__('Invalid species'));
}
}
// use the fullscreen map view
$this->render('fullscreenmap');
}
/**
* Returns JSON for a jQuery UI autocomplete box
*/
public function autocomplete() {
// get what the user typed in
$partial = strtolower($this->request->query['term']);
// query db
$results = $this->Species->getDataSource()->execute(
'SELECT * FROM '.
' (SELECT *, GREATEST(SIMILARITY(?, scientific_name), SIMILARITY(?, common_name)) AS match '.
' FROM species '.
' WHERE has_occurrences) AS matched_and_filtered_species '.
'WHERE match > 0 '.
'ORDER BY match DESC '.
'LIMIT 20',
array(),
array($partial, $partial)
);
// convert $matched_species into json format expected by jquery ui
$matched_species = array();
foreach($results as $species){
$matched_species[] = $this->_speciesToJson($species);
}
// render json
$this->set('results', $matched_species);
$this->set('_serialize', 'results');
}
/**
* Return the next species to run a modelling job for.
* Assumes requestor will start the job.
*/
public function get_next_job_and_assume_queued() {
if ($this->request->is('post')) {
$species = $this->_next_job();
if($species){
// Update current model info for species
$species['Species']['current_model_status'] = "QUEUED";
// Record when the job started
$species['Species']['current_model_queued_time'] = date(DATE_ISO8601);
// Record the importance of the job
if($species['Species']['first_requested_remodel'] !== null) {
$species['Species']['current_model_importance'] = 2;
} else {
$species['Species']['current_model_importance'] = 1;
}
$this->Species->save($species);
$this->dieWithStatus(200, $species['Species']['id']);
} else {
$this->dieWithStatus(204, 'No species modelling required.');
}
}
}
/**
* Return next species to run a modelling job for
*/
public function next_job() {
$species = $this->_next_job();
if($species){
$this->dieWithStatus(200, $species['Species']['id']);
} else {
$this->dieWithStatus(204, 'No species modelling required.');
}
}
/**
* Updates the status of a running modelling job
*
* TODO: any auth on this? looks like a minor security hole
* TODO: how do we handle failure? Just ignored it for the moment.
*
* Takes POST/PUT params:
* - job_status - a status code string
* - job_status_message - human readable (?) message for job_status
* - dirty_occurrences - value of num_dirty_occurrences when the job started
*/
public function job_status($species_id) {
$species = $this->Species->find('first', array(
'conditions' => array('id' => $species_id)
));
if($species === false)
$this->dieWithStatus(404, 'No species found with given id');
$jobStatus = $this->request->data('job_status');
// Only process it if the jobStatus has changed.
if ($jobStatus !== $species['Species']['current_model_status']) {
// Starting a JOB
if($jobStatus === 'QUEUED') {
// Update current model info for species
$species['Species']['current_model_status'] = $jobStatus;
// Record when the job started
$species['Species']['current_model_queued_time'] = date(DATE_ISO8601);
// Record the importance of the job
if($species['Species']['first_requested_remodel'] !== null) {
$species['Species']['current_model_importance'] = 2;
} else {
$species['Species']['current_model_importance'] = 1;
}
// Finished a JOB
} elseif($jobStatus === 'FINISHED_SUCCESS' or $jobStatus === 'FINISHED_FAILURE') {
// Get job status message
$jobStatusMessage = $this->request->data('job_status_message');
// Update last completed model info
$species['Species']['last_completed_model_queued_time'] = $species['Species']['current_model_queued_time'];
$species['Species']['last_completed_model_finish_time'] = date(DATE_ISO8601);
$species['Species']['last_completed_model_importance'] = $species['Species']['current_model_importance'];
$species['Species']['last_completed_model_status'] = $jobStatus;
$species['Species']['last_completed_model_status_reason'] = $jobStatusMessage;
// Update last successfully completed model info
if($jobStatus === 'FINISHED_SUCCESS') {
$species['Species']['last_successfully_completed_model_queued_time'] = $species['Species']['last_completed_model_queued_time'];
$species['Species']['last_successfully_completed_model_finish_time'] = $species['Species']['last_completed_model_finish_time'];
$species['Species']['last_successfully_completed_model_importance'] = $species['Species']['last_completed_model_importance'];
// Only update the dirty_occurrences record if
// the model that was ran cleared all our dirty occurrences.
$occurrencesCleared = (int)$this->request->data('dirty_occurrences');
if ($species['Species']['num_dirty_occurrences'] === $occurrencesCleared) {
$species['Species']['num_dirty_occurrences'] = 0;
}
}
$species['Species']['current_model_status'] = null;
$species['Species']['current_model_importance'] = null;
$species['Species']['current_model_queued_time'] = null;
$species['Species']['first_requested_remodel'] = null;
// Part way through a JOB
} else {
$species['Species']['current_model_status'] = $jobStatus;
}
$this->Species->save($species);
}
$this->dieWithStatus(200);
}
/**
* Called when the user requests remodelling for a species
*/
public function request_model_rerun($species_id) {
// if(!User::canRequestRemodel(AuthComponent::user()))
// $this->dieWithStatus(403);
$species = $this->Species->findById($species_id);
if($species === False)
$this->dieWithStatus(404, 'No species found with id = ' . $species_id);
if($species['Species']['first_requested_remodel'] !== NULL)
$this->dieWithStatus(200, 'Modelling already requested previously');
$species['Species']['first_requested_remodel'] = date(DATE_ISO8601);
$this->Species->save($species);
$this->dieWithStatus(200, 'Request processed');
}
/**
* bounce the user's download request to the right URL to get the file
*/
public function download_occurrences($species_id) {
$this->Species->recursive = 0;
$this->Species->id = $species_id;
if (!$this->Species->exists()) {
throw new NotFoundException('No species found with id = ' . $species_id);
}
$this->Species->read();
$this->redirect(SpeciesController::DOWNLOAD_URL_PREFIX . $this->Species->canonicalName() . '/occurrences/latest-occurrences.zip');
}
/**
* bounce the user's download request to the right URL to get the file
*/
public function download_climate($species_id) {
$this->Species->recursive = 0;
$this->Species->id = $species_id;
if (!$this->Species->exists()) {
throw new NotFoundException('No species found with id = ' . $species_id);
}
$this->Species->read();
$this->redirect(SpeciesController::DOWNLOAD_URL_PREFIX . $this->Species->canonicalName() . '/projected-distributions/latest-projected-distributions.zip');
}
private function _speciesToJson($species) {
return array(
'id' => $species['id'],
'scientificName' => $species['scientific_name'],
'commonName' => $species['common_name'],
'numDirtyOccurrences' => $species['num_dirty_occurrences'],
'canRequestRemodel' => (bool)($species['num_dirty_occurrences'] > 0 && $species['first_requested_remodel'] === null),
'remodelStatus' => $this->_speciesRemodelStatusMessage($species),
'hasDownloadables' => ($species['last_successfully_completed_model_finish_time'] != null),
'label' => $species['common_name'].' - '.$species['scientific_name']
);
}
private function _speciesRemodelStatusMessage($species) {
if($species['num_dirty_occurrences'] <= 0)
return 'Up to date';
if($species['current_model_status'] !== null)
return 'Remodelling running with status: ' . $species['current_model_status'];
if($species['first_requested_remodel'] !== null)
return 'Priority queued for remodelling';
return 'Automatically queued for remodelling';
}
private function _next_job() {
$species = $this->Species->find('first', array(
'fields' => array('*', 'first_requested_remodel IS NULL AS is_null'),
'conditions' => array(
'OR' => array(
// Run any models that haven't started yet...
array(
'num_dirty_occurrences >' => 0,
'current_model_status' => null
),
// Run old models again
array(
'num_dirty_occurrences >' => 0,
'current_model_queued_time <' => date(DATE_ISO8601, strtotime("-1 days"))
),
// Run any models with a status, but no recorded queued time
// (these should never happen)
array(
'current_model_status <>' => null,
'current_model_queued_time' => null
)
)
),
'order' => array(
'is_null' => 'ASC',
'first_requested_remodel' => 'ASC',
'num_dirty_occurrences' => 'DESC',
)
));
return $species;
}
}