-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHasSpatial.php
More file actions
99 lines (82 loc) · 2.24 KB
/
Copy pathHasSpatial.php
File metadata and controls
99 lines (82 loc) · 2.24 KB
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
<?php
/**
* Simple trait for coordinates posts
*
* User: Arushad
* Date: 06/10/2016
* Time: 16:28
*/
namespace Javaabu\Geospatial;
use Javaabu\Geospatial\Objects\Polygon;
use MatanYadaev\EloquentSpatial\Enums\Srid;
use MatanYadaev\EloquentSpatial\Objects\Geometry;
use MatanYadaev\EloquentSpatial\Traits\HasSpatial as EloquentHasSpatial;
use Javaabu\Geospatial\Objects\Point;
trait HasSpatial
{
use EloquentHasSpatial;
/**
* Get the latitude
*
* @param null $value
* @return float
*/
public function getLatAttribute($value = null)
{
$column = $this->getDefaultPointField();
return is_null($value) ? optional($this->{$column})->latitude : $value;
}
/**
* Get the longitude
*
* @param null $value
* @return float
*/
public function getLngAttribute($value = null)
{
$column = $this->getDefaultPointField();
return is_null($value) ? optional($this->{$column})->longitude : $value;
}
public function getDefaultPointField(): string
{
return 'coordinates';
}
public function getDefaultPolygonField(): string
{
return 'boundary';
}
public function setPoint($lat, $lng, string $column = '', $srid = Srid::WGS84)
{
if (! $column) {
$column = $this->getDefaultPointField();
}
$this->{$column} = new Point($lat, $lng, $srid);
}
public function setPolygon(string $wkt, string $column = '', $srid = Srid::WGS84) {
if (! $column) {
$column = $this->getDefaultPolygonField();
}
$this->{$column} = Polygon::fromWkt($wkt, $srid);
}
/**
* Within bounds
*
* @param $query
* @param $bounds
* @return mixed
*/
public function scopeWithinBounds($query, $bounds, string $column = '', $srid = Srid::WGS84)
{
if (! $bounds instanceof Polygon) {
$bounds = Polygon::fromWKT($bounds, $srid);
}
if (! $column) {
$column = $this->getDefaultPointField();
}
return $query->whereWithin($column, $bounds);
}
public function toTestDbString(Geometry $geometry)
{
return $geometry->toSqlExpression($this->getConnection());
}
}