forked from Sibyx/phpGPX
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGpxFile.php
More file actions
184 lines (157 loc) · 4 KB
/
Copy pathGpxFile.php
File metadata and controls
184 lines (157 loc) · 4 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<?php
/**
* Created 17/02/2017 17:46
* @author Jakub Dubec <jakub.dubec@gmail.com>
*/
namespace phpGPX\Models;
use phpGPX\Helpers\SerializationHelper;
use phpGPX\Parsers\ExtensionParser;
use phpGPX\Parsers\MetadataParser;
use phpGPX\Parsers\PointParser;
use phpGPX\Parsers\RouteParser;
use phpGPX\Parsers\TrackParser;
use phpGPX\phpGPX;
/**
* Class GpxFile
* Representation of GPX file.
* @package phpGPX\Models
*/
class GpxFile implements Summarizable
{
/**
* A list of waypoints.
* @var Point[]
*/
public $waypoints;
/**
* A list of routes.
* @var Route[]
*/
public $routes;
/**
* A list of tracks.
* @var Track[]
*/
public $tracks;
/**
* Metadata about the file.
* The original GPX 1.1 attribute.
* @var Metadata|null
*/
public $metadata;
/**
* @var Extensions|null
*/
public $extensions;
/**
* Creator of GPX file.
* @var string|null
*/
public $creator;
/**
* GpxFile constructor.
*/
public function __construct()
{
$this->waypoints = [];
$this->routes = [];
$this->tracks = [];
$this->metadata = null;
$this->extensions = null;
$this->creator = null;
}
/**
* Serialize object to array
* @return array
*/
public function toArray()
{
return SerializationHelper::filterNotNull([
'creator' => SerializationHelper::stringOrNull($this->creator),
'metadata' => SerializationHelper::serialize($this->metadata),
'waypoints' => SerializationHelper::serialize($this->waypoints),
'routes' => SerializationHelper::serialize($this->routes),
'tracks' => SerializationHelper::serialize($this->tracks),
'extensions' => SerializationHelper::serialize($this->extensions)
]);
}
/**
* Return JSON representation of GPX file with statistics.
* @return string
*/
public function toJSON()
{
return json_encode($this->toArray(), phpGPX::$PRETTY_PRINT ? JSON_PRETTY_PRINT : null);
}
/**
* Create XML representation of GPX file.
* @return \DOMDocument
*/
public function toXML()
{
$document = new \DOMDocument("1.0", 'UTF-8');
$gpx = $document->createElementNS("http://www.topografix.com/GPX/1/1", "gpx");
$gpx->setAttribute("version", "1.1");
$gpx->setAttribute("creator", $this->creator ? $this->creator : phpGPX::getSignature());
ExtensionParser::$usedNamespaces = [];
if (!empty($this->metadata)) {
$gpx->appendChild(MetadataParser::toXML($this->metadata, $document));
}
foreach ($this->waypoints as $waypoint) {
$gpx->appendChild(PointParser::toXML($waypoint, $document));
}
foreach ($this->routes as $route) {
$gpx->appendChild(RouteParser::toXML($route, $document));
}
foreach ($this->tracks as $track) {
$gpx->appendChild(TrackParser::toXML($track, $document));
}
if (!empty($this->extensions)) {
$gpx->appendChild(ExtensionParser::toXML($this->extensions, $document));
}
// Namespaces
$schemaLocationArray = [
'http://www.topografix.com/GPX/1/1',
'http://www.topografix.com/GPX/1/1/gpx.xsd'
];
foreach (ExtensionParser::$usedNamespaces as $usedNamespace) {
$gpx->setAttributeNS(
"http://www.w3.org/2000/xmlns/",
sprintf("xmlns:%s", $usedNamespace['prefix']),
$usedNamespace['namespace']
);
$schemaLocationArray[] = $usedNamespace['namespace'];
$schemaLocationArray[] = $usedNamespace['xsd'];
}
$gpx->setAttributeNS(
'http://www.w3.org/2001/XMLSchema-instance',
'xsi:schemaLocation',
implode(" ", $schemaLocationArray)
);
$document->appendChild($gpx);
if (phpGPX::$PRETTY_PRINT) {
$document->formatOutput = true;
$document->preserveWhiteSpace = true;
}
return $document;
}
/**
* Save data to file according to selected format.
* @param string $path
* @param string $format
*/
public function save($path, $format)
{
switch ($format) {
case phpGPX::XML_FORMAT:
$document = $this->toXML();
$document->save($path);
break;
case phpGPX::JSON_FORMAT:
file_put_contents($path, $this->toJSON());
break;
default:
throw new \RuntimeException("Unsupported file format!");
};
}
}