-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
Spatial.php
51 lines (44 loc) · 1.25 KB
/
Spatial.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
<?php
namespace TCG\Voyager\Traits;
use Illuminate\Support\Facades\DB;
trait Spatial
{
/**
* Get location as WKT from Geometry for given field.
*
* @param string $column
*
* @return string
*/
public function getLocation($column)
{
$model = self::select(DB::Raw('ST_AsText('.$column.') AS '.$column))
->where('id', $this->id)
->first();
return isset($model) ? $model->$column : '';
}
/**
* Format and return array of (lat,lng) pairs of points fetched from the database.
*
* @return array $coords
*/
public function getCoordinates()
{
$coords = [];
if (!empty($this->spatial)) {
foreach ($this->spatial as $column) {
$clear = trim(preg_replace('/[a-zA-Z\(\)]/', '', $this->getLocation($column)));
if (!empty($clear)) {
foreach (explode(',', $clear) as $point) {
list($lat, $lng) = explode(' ', $point);
$coords[] = [
'lat' => $lat,
'lng' => $lng,
];
}
}
}
}
return $coords;
}
}