1
|
|
|
<?php |
2
|
|
|
namespace Ballen\Cartographer; |
3
|
|
|
|
4
|
|
|
use Ballen\Cartographer\Core\GeoJSON; |
5
|
|
|
use Ballen\Cartographer\Core\LatLong; |
6
|
|
|
use Ballen\Collection\Collection; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Cartographer |
10
|
|
|
* |
11
|
|
|
* Cartographer is a PHP library providing the ability to programmatically |
12
|
|
|
* generate GeoJSON objects. |
13
|
|
|
* |
14
|
|
|
* @author Bobby Allen <[email protected]> |
15
|
|
|
* @license http://www.gnu.org/licenses/gpl-3.0.html |
16
|
|
|
* @link https://github.com/allebb/cartographer |
17
|
|
|
* @link http://bobbyallen.me |
18
|
|
|
* |
19
|
|
|
*/ |
20
|
|
|
class MultiPoint extends GeoJSON |
21
|
|
|
{ |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* The GeoJSON schema type |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
protected $type = GeoJSON::TYPE_MULTIPOINT; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* The coordinates collection (of LatLong objects) |
31
|
|
|
* @var Collection |
32
|
|
|
*/ |
33
|
|
|
private $coordinates; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* |
37
|
|
|
*/ |
38
|
|
|
public function __construct($init) |
39
|
|
|
{ |
40
|
|
|
$this->coordinates = new Collection; |
41
|
|
|
|
42
|
|
|
if (is_array($init)) { |
43
|
|
|
array_walk($init, function($item) { |
44
|
|
|
if (is_a($item, LatLong::class)) { |
45
|
|
|
$this->addCoordinate($item); |
46
|
|
|
} |
47
|
|
|
}); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Add a new coordinate to the LineString sequence. |
53
|
|
|
* @param LatLong $coordinate |
54
|
|
|
*/ |
55
|
|
|
public function addCoordinate(LatLong $coordinate) |
56
|
|
|
{ |
57
|
|
|
$this->coordinates->push($coordinate); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Exports the type specific schema element(s). |
62
|
|
|
* @return array |
63
|
|
|
*/ |
64
|
|
|
public function export() |
65
|
|
|
{ |
66
|
|
|
$coords = []; |
67
|
|
|
foreach ($this->coordinates->all()->toArray() as $coord) { |
68
|
|
|
$coords[] = $coord->lngLatArray(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return [ |
72
|
|
|
'coordinates' => $coords, |
73
|
|
|
]; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Validate the type specific schema element(s). |
78
|
|
|
* @return boolean |
79
|
|
|
*/ |
80
|
|
|
public function validate() |
81
|
|
|
{ |
82
|
|
|
// MultiPoint Type can have one or more lat/lng coordinates. |
83
|
|
|
if ($this->coordinates->count() < 1) { |
84
|
|
|
return false; |
85
|
|
|
} |
86
|
|
|
return true; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|