1
|
|
|
<?php |
2
|
|
|
namespace Ballen\Cartographer\Core; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* Cartographer |
6
|
|
|
* |
7
|
|
|
* Cartographer is a PHP library providing the ability to programmatically |
8
|
|
|
* generate GeoJSON objects. |
9
|
|
|
* |
10
|
|
|
* @author Bobby Allen <[email protected]> |
11
|
|
|
* @license http://www.gnu.org/licenses/gpl-3.0.html |
12
|
|
|
* @link https://github.com/allebb/cartographer |
13
|
|
|
* @link http://bobbyallen.me |
14
|
|
|
* |
15
|
|
|
*/ |
16
|
|
|
abstract class GeoJSON implements GeoJSONTypeInterface |
17
|
|
|
{ |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Supported GeoJSON types. |
21
|
|
|
*/ |
22
|
|
|
const TYPE_POINT = "Point"; |
23
|
|
|
const TYPE_MULTIPOINT = "MultiPoint"; |
24
|
|
|
const TYPE_LINESTRING = "LineString"; |
25
|
|
|
const TYPE_MULTILINESTRING = "MultiLineString"; |
26
|
|
|
const TYPE_POLYGON = "Polygon"; |
27
|
|
|
const TYPE_MULTIPOLYGON = "MultiPolygon"; |
28
|
|
|
const TYPE_GEOMETRYCOLLECTION = "GeometryCollection"; |
29
|
|
|
const TYPE_FEATURE = "Feature"; |
30
|
|
|
const TYPE_FEATURECOLLECTION = "FeatureCollection"; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* The GeoJSON Object Type |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
protected $type; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Generates the GeoJSON object. |
40
|
|
|
* @return string |
41
|
|
|
*/ |
42
|
22 |
|
public function generate() |
43
|
|
|
{ |
44
|
22 |
|
$this->validateSchema(); |
45
|
14 |
|
return $this->buildJson(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Generate a GeometryCollection member GeoJSON object. |
50
|
|
|
* @return array |
51
|
|
|
*/ |
52
|
10 |
|
public function generateMember() |
53
|
|
|
{ |
54
|
10 |
|
return array_merge(['type' => $this->type], $this->export()); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Validates the GeoJSON schema. |
59
|
|
|
* @throws InvalidObjectTypeException |
60
|
|
|
*/ |
61
|
22 |
|
private function validateSchema() |
62
|
|
|
{ |
63
|
22 |
|
$typeConstants = (new \ReflectionClass(__CLASS__))->getConstants(); |
64
|
22 |
|
if (!in_array($this->type, $typeConstants)) { |
65
|
|
|
throw new \Ballen\Cartographer\Exceptions\InvalidObjectTypeException(sprintf('The GeoJSON object type specified (%s) is not supported.', $this->type)); |
66
|
|
|
} |
67
|
22 |
|
if (!$this->validate()) { |
68
|
8 |
|
throw new \Ballen\Cartographer\Exceptions\TypeSchemaValidationException('The GeoJSON type object failed to validate.'); |
69
|
|
|
} |
70
|
14 |
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Constructs the JSON object. |
74
|
|
|
* @param boolean $pretty Enable "pretty" JSON outputting |
75
|
|
|
* @return string |
76
|
|
|
*/ |
77
|
14 |
|
private function buildJson($pretty = false) |
78
|
|
|
{ |
79
|
14 |
|
$data = array_merge(['type' => $this->type], $this->export()); |
80
|
14 |
|
if ($pretty) { |
81
|
|
|
return json_encode($data, JSON_PRETTY_PRINT); |
82
|
|
|
} |
83
|
14 |
|
return json_encode($data); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|