-
Notifications
You must be signed in to change notification settings - Fork 0
/
import.php
54 lines (45 loc) · 1.51 KB
/
import.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
<?php
// Configuration
$importFile = '/whiterabbit.baart.ch/import/favourites.gpx';
$wptType = ['white rabbit'];
// read the OsmAnd favourites
$gpx = simplexml_load_file(dirname(__DIR__) . $importFile);
echo ('<pre>');
// new xml object
$newXML = new SimpleXMLElement("<?xml version='1.0' encoding='UTF-8' standalone='yes' ?><gpx></gpx>");
//sorting function
function cmp($a, $b)
{
return strcmp(($a['year'] . $a['month'] . $a['day']), ($b['year'] . $b['month'] . $b['day']));
}
// first put the necessary info into an array
// only an array can be sorted later
$tmpArr = array();
foreach ($gpx->wpt as $pt) {
// only take wanted types of waypoints
if (in_array($pt->type, $wptType)) {
$tmpArr[] = [
'lat' => (string) $pt['lat'],
'lon' => (string) $pt['lon'],
'name' => (string) htmlspecialchars($pt->name),
'type' => (string) htmlspecialchars($pt->type),
'day' => (string) substr($pt->name, 0, 2),
'month' => (string) substr($pt->name, 3, 2),
'year' => (string) substr($pt->name, 6, 2)
];
}
}
usort($tmpArr, "cmp");
//var_dump($tmpArr);
foreach ($tmpArr as $pt) {
$wpt = $newXML->addChild('wpt');
$wpt->addAttribute('lat', (string) $pt['lat']);
$wpt->addAttribute('lon', (string) $pt['lon']);
$wpt->addChild('name', $pt['name']);
$wpt->addChild('type', $pt['type']);
$wpt->addChild('day', $pt['day']);
$wpt->addChild('month', $pt['month']);
$wpt->addChild('year', $pt['year']);
print((string) $pt['name'] . '<br>');
}
var_dump($newXML->asXML('waypoints.gpx'));