-
Notifications
You must be signed in to change notification settings - Fork 0
/
geojsonRenderer.class.php
executable file
·97 lines (77 loc) · 2.09 KB
/
geojsonRenderer.class.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
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
<?php
/**
* Class to convert a data structure (e.g. a set of geographical points) into a GeoJSON standard data structure
* https://geojson.org/geojson-spec.html
*/
class geojsonRenderer
{
# Registry of features
private $features = array ();
# Public getter, returning the data structure
public function getData ()
{
# Assemble the data
$data = array (
'type' => 'FeatureCollection'
);
# Always include Features
$data['features'] = $this->features;
# Return the data
return $data;
}
# Public getter, returning the data structure as a string
public function getGeojson ()
{
# Return the JSON encoded data
return json_encode ($this->getData ());
}
/**
* Adds a point
* @param float $longitude
* @param float $latitude
* @param array $properties (or just the name as a string)
* @return void
*/
public function point ($longitude, $latitude, $properties)
{
# Assemble the co-ordinate as an array
$coordinate = array ((float) $longitude, (float) $latitude);
# Add
$this->addFeature (ucfirst (__FUNCTION__), $coordinate, $properties);
}
/**
* Adds a linestring
* @param array $coordinates in format: 'x1 y1', 'x2 y2', ... , 'xn yn'
* @param array $properties
* @return void
*/
public function lineString ($coordinates, $properties)
{
# Convert all to numbers
foreach ($coordinates as $index => $coordinate) {
$coordinates[$index] = array ((float) $coordinate[0], (float) $coordinate[1]);
}
# Add
$this->addFeature (ucfirst (__FUNCTION__), $coordinates, $properties);
}
/**
* Helper function adds the feature to the registry
*/
private function addFeature ($geojsonClass, $coordinates, $properties)
{
# Assemble the feature
$feature = array ('type' => 'Feature');
# Add any properties
if ($properties) {
$feature['properties'] = $properties;
}
# Add geometry after propertes to make them easier to find in raw listings
$feature['geometry'] = array (
'type' => $geojsonClass,
'coordinates' => $coordinates,
);
# Add the feature to the registry
$this->features[] = $feature;
}
}
?>