-
Notifications
You must be signed in to change notification settings - Fork 33
/
myroutes_add.php
111 lines (92 loc) · 3.54 KB
/
myroutes_add.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php
use src\Models\ApplicationContainer;
use src\Utils\Database\XDb;
require_once(__DIR__ . '/lib/common.inc.php');
//user logged in?
$loggedUser = ApplicationContainer::GetAuthorizedUser();
if (!$loggedUser) {
$target = urlencode(tpl_get_current_page());
tpl_redirect('login.php?target=' . $target);
exit;
}
$tplname = 'myroutes_add';
$user_id = $loggedUser->getUserId();
$name = $_POST['name'] ?? '';
tpl_set_var('name', htmlspecialchars($name, ENT_COMPAT, 'UTF-8'));
$desc = $_POST['desc'] ?? '';
tpl_set_var('desc', htmlspecialchars($desc, ENT_COMPAT, 'UTF-8'));
$radius = $_POST['radius'] ?? '0';
tpl_set_var('radius', $radius);
if (isset($_POST['back'])) {
tpl_redirect('myroutes.php');
exit;
}
// start submit
if (isset($_POST['submitform'])) {
// insert route name
XDb::xSql(
"INSERT INTO `routes` ( `route_id`, `user_id`, `name`, `description`, `radius` )
VALUES ('', ?, ?, ?, ?)",
$user_id, $name, $desc, $radius);
$upload_filename = $_FILES['file']['tmp_name'];
// get route_id
$route_id = XDb::xMultiVariableQueryValue(
"SELECT route_id FROM `routes`
WHERE name=:1 AND description=:2 AND user_id=:3",
0, $name, $desc, $user_id);
// Read file KML with route, load in the KML file through the my_routes page, and run that KML file through GPSBABEL which has a tool interpolate data points in the route.
exec("/usr/bin/gpsbabel -i kml -f " . $upload_filename . " -x interpolate,distance=0.25k -o kml -F " . $upload_filename);
$xml = simplexml_load_file($upload_filename);
// get length route
foreach ($xml->Document->Folder as $f) {
foreach ($f->Folder as $folder) {
$dis = $folder->description;
$dis1 = explode(" ", trim($dis));
$len = (float)$dis1[27];
XDb::xSql(
"UPDATE `routes` SET `length`=? WHERE `route_id`=?", $len, $route_id);
}
}
foreach ($xml->Document->Folder as $xmlelement) {
foreach ($xmlelement->Folder as $folder) {
foreach ($folder->Placemark->LineString->coordinates as $coordinates) {
if ($coordinates) {
$coords_raw = explode(" ", trim($coordinates));
foreach ($coords_raw as $coords_raw_part) {
if ($coords_raw_part) {
$coords_raw_parts = explode(",", $coords_raw_part);
$coords[] = $coords_raw_parts[0];
$coords[] = $coords_raw_parts[1];
}
}
}
}
}
}
// end of read
//we get the point data in to an array called $points:
if (isset($coords)) {
for ($i = 0; $i < count($coords) - 1; $i = $i + 2) {
$points[] = array("lon" => $coords[$i], "lat" => $coords[$i + 1]);
if (($coords[$i] + 0 == 0) or ($coords[$i + 1] + 0 == 0)) {
// $error .= "Invalid Co-ords found in import file.<br>\n";
break;
}
}
}
// add it to the route_points database:
$point_num = 0;
if (isset($points)) {
foreach ($points as $point) {
$point_num++;
$result = XDb::xSql(
"INSERT into route_points (route_id,point_nr,lat,lon)
VALUES (?,?,?,?)",
$route_id, $point_num, $point["lat"], $point["lon"]);
}
}
tpl_redirect('myroutes.php');
exit;
} //end submit
//make the template and send it out
tpl_BuildTemplate();