This repository has been archived by the owner on Jul 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.php
92 lines (74 loc) · 2.36 KB
/
index.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
<?php
//GET the data by a bounding box
header('Content-Type: application/json');
require "config.php";
// connect to database
$mysqli = new mysqli($DB_HOST, $DB_USER, $DB_PASSWORD, $DB_DATABASE);
if ($mysqli->connect_errno) {
echo $mysqli->connect_error;
exit(1);
}
// construct and check bbox
$bbox = str_to_bbox($_GET['bbox']);
if (!is_valid_bbox($bbox)) {
http_response_code(400);
echo json_encode(["status" => "The specified bounding box is not valid!"]);
die();
}
// get ways out of the db which lie in the bbox and construct result
$result = (object) new stdClass();
if (!($stmt = $mysqli->prepare("SELECT wayId, fromNodeId, toNodeId, fromLongitude, fromLatitude, toLongitude, toLatitude FROM oneway WHERE (latitude BETWEEN ? AND ?) AND (longitude BETWEEN ? and ?)"))) {
echo $mysqli->error;
exit(1);
}
if (!($stmt->bind_param("dddd", $bbox->bottom, $bbox->top, $bbox->left, $bbox->right))) {
echo $stmt->error;
exit(1);
}
if (!($stmt->execute())) {
echo $stmt->error;
exit(1);
}
$result->segments = array();
$stmt->bind_result($wayId, $fromNodeId, $toNodeId, $fromLongitude, $fromLatitude, $toLongitude, $toLatitude);
while ($stmt->fetch()) {
$segment = (object) new stdClass();
$segment->wayId = $wayId;
$segment->fromNodeId = $fromNodeId;
$segment->toNodeId = $toNodeId;
$segment->fromPosition = (object) new stdClass();
$segment->fromPosition->lon = $fromLongitude;
$segment->fromPosition->lat = $fromLatitude;
$segment->toPosition = (object) new stdClass();
$segment->toPosition->lon = $toLongitude;
$segment->toPosition->lat = $toLatitude;
$result->segments[] = $segment;
}
$stmt->close();
if (!($mysqli->close())) {
echo $mysqli->error;
exit(1);
}
// return the result
http_response_code(200);
echo json_encode($result, JSON_PRETTY_PRINT);
function str_to_bbox($string)
{
$array = explode(",", $string);
$bbox = (object) new stdClass();
$bbox->left = $array[0];
$bbox->bottom = $array[1];
$bbox->right = $array[2];
$bbox->top = $array[3];
return $bbox;
}
function is_valid_bbox($bbox)
{
if (isset($bbox) && isset($bbox->left) && isset($bbox->bottom) && isset($bbox->right) && isset($bbox->top) &&
abs($bbox->left) <= 180 && abs($bbox->bottom) <= 90 && abs($bbox->right) <= 180 && abs($bbox->top) <= 90 &&
$bbox->left < $bbox->right && $bbox->bottom < $bbox->top) {
return true;
}
return false;
}
?>