-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGeodistanceBehavior.php
84 lines (73 loc) · 1.86 KB
/
GeodistanceBehavior.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
<?php
/**
* GeoDistanceBehavior
* Finds the closets rows of a given point defined by latitude and longitude
* in a rounded area
*/
class GeoDistanceBehavior extends ModelBehavior {
/**
* Settings to configure the behavior
*
* @var array
*/
public $settings = array();
/**
* Initiate behaviour
*
* @param object $Model
* @param array $settings
*/
public function setup(Model $Model, $settings = array()) {
if(!array_key_exists("lat_field",$settings) || !array_key_exists("lon_field",$settings)) {
die("Latitude and longitude fields are not defined");
}
if (!isset($this->settings[$Model->alias])) {
$this->settings[$Model->alias] = $settings;
}
}
/**
* Find the closests rows around an area
*
* @param object $Model
* @param array $options
*/
public function findClosest(Model $Model, $options) {
if(!$options['latitude'] || !$options['longitude']) {
return null;
}
$settings = array_merge($this->settings[$Model->alias], $options);
if($settings['unit'] == 'miles') {
$earth = 3959;
}else {
$earth = 6371;
}
if(!isset($settings['conditions'])){
$settings['conditions'] = null;
}
$data = $Model->find('all', [
'group' => ["distance HAVING distance < $settings[radius]"],
'fields' => [
'*',
"($earth * acos(
cos( radians($settings[latitude]) )
* cos( radians( $settings[lat_field] ) )
* cos( radians( $settings[lon_field] )
- radians($settings[longitude]) )
+ sin( radians($settings[latitude]) )
* sin( radians( $settings[lat_field] ) )
) ) AS distance",
],
'conditions' => $settings['conditions'],
'order' => ['distance ASC'],
'limit' => 10
]);
if(!empty($data)) {
foreach($data as $index => $row) {
$row['Sitio']['distance'] = $row[0]['distance'];
unset($row[0]);
$data[$index] = $row;
}
}
return $data;
}
}