forked from Sibyx/phpGPX
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreateFileFromScratch.php
More file actions
131 lines (104 loc) · 3.5 KB
/
Copy pathCreateFileFromScratch.php
File metadata and controls
131 lines (104 loc) · 3.5 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
<?php
/**
* @author Jakub Dubec <jakub.dubec@gmail.com>
*/
use phpGPX\Models\GpxFile;
use phpGPX\Models\Link;
use phpGPX\Models\Metadata;
use phpGPX\Models\Point;
use phpGPX\Models\Segment;
use phpGPX\Models\Track;
use phpGPX\Models\Extensions;
use phpGPX\Models\Extensions\TrackPointExtension;
require_once '../vendor/autoload.php';
$sample_data = [
[
'longitude' => 9.860624216140083,
'latitude' => 54.9328621088893,
'elevation' => 0,
'aTemp' => 22,
'time' => new \DateTime("+ 1 MINUTE")
],
[
'latitude' => 54.83293237320851,
'longitude' => 9.76092208681491,
'elevation' => 10.0,
'aTemp' => 23,
'time' => new \DateTime("+ 2 MINUTE")
],
[
'latitude' => 54.73327743521187,
'longitude' => 9.66187816543752,
'elevation' => 42.42,
'aTemp' => 24,
'time' => new \DateTime("+ 3 MINUTE")
],
[
'latitude' => 54.63342326167919,
'longitude' => 9.562439849679859,
'elevation' => 12,
'aTemp' => 25,
'time' => new \DateTime("+ 4 MINUTE")
]
];
// Creating sample link object for metadata
$link = new Link();
$link->href = "https://sibyx.github.io/phpgpx";
$link->text = 'phpGPX Docs';
// GpxFile contains data and handles serialization of objects
$gpx_file = new GpxFile();
// Creating sample Metadata object
$gpx_file->metadata = new Metadata();
// Time attribute is always \DateTime object!
$gpx_file->metadata->time = new \DateTime();
// Description of GPX file
$gpx_file->metadata->description = "My pretty awesome GPX file, created using phpGPX library!";
// Adding link created before to links array of metadata
// Metadata of GPX file can contain more than one link
$gpx_file->metadata->links[] = $link;
// Creating track
$track = new Track();
// Name of track
$track->name = sprintf("Some random points in logical order. Input array should be already ordered!");
// Type of data stored in track
$track->type = 'RUN';
// Source of GPS coordinates
$track->source = sprintf("MySpecificGarminDevice");
// Creating Track segment
$segment = new Segment();
foreach ($sample_data as $sample_point) {
// Creating trackpoint
$point = new Point(Point::TRACKPOINT);
$point->latitude = $sample_point['latitude'];
$point->longitude = $sample_point['longitude'];
$point->elevation = $sample_point['elevation'];
$point->time = $sample_point['time'];
// Creating trackpoint extension
$point->extensions = new Extensions();
$trackPointExtension = new TrackPointExtension();
$trackPointExtension->aTemp = $sample_point['aTemp'];
$point->extensions->trackPointExtension = $trackPointExtension;
$segment->points[] = $point;
}
// Add segment to segment array of track
$track->segments[] = $segment;
// Add track to file
$gpx_file->tracks[] = $track;
// Create waypoint
$point = new Point(Point::WAYPOINT);
$point->name = 'Example Waypoint';
$point->latitude = $sample_point['latitude'];
$point->longitude = $sample_point['longitude'];
$point->elevation = $sample_point['elevation'];
$point->time = $sample_point['time'];
// Add waypoint to file
$gpx_file->waypoints[] = $point;
// GPX output
$gpx_file->save('CreateFileFromScratchExample.gpx', \phpGPX\phpGPX::XML_FORMAT);
// Serialized data as JSON
$gpx_file->save('CreateFileFromScratchExample.json', \phpGPX\phpGPX::JSON_FORMAT);
// Direct GPX output to browser
header("Content-Type: application/gpx+xml");
header("Content-Disposition: attachment; filename=CreatingFileFromScratchExample.gpx");
echo $gpx_file->toXML()->saveXML();
exit();