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