forked from iRail/BikesResource
/
Velo.class.php
97 lines (81 loc) · 2.6 KB
/
Velo.class.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
<?php
/**
*
* @package packages/Bikes
* @copyright (C) 2012 by iRail vzw/asbl
* @license AGPLv3
* @author Pieter Colpaert <pieter aŧ iRail.be>
*/
class BikesVelo extends AReader
{
public function __construct($package, $resource, $RESTparameters)
{
parent::__construct($package, $resource, $RESTparameters);
$this->lat = null;
$this->long = null;
$this->offset = 0;
$this->rowcount = 1024;
}
public static function getParameters()
{
return [
"lat" => "Latitude",
"long" => "Longitude",
"offset" => "Offset",
"rowcount" => "Rowcount",
];
}
public static function getRequiredParameters()
{
return array();
}
public function setParameter($key, $val)
{
if ($key == "lat") {
$this->lat = $val;
} elseif ($key == "long") {
$this->long = $val;
} elseif ($key == "offset") {
$this->offset = $val;
} elseif ($key == "rowcount") {
$this->rowcount = $val;
}
}
public function read()
{
$data = TDT::HttpRequest("http://ClearChannelBikes:WbRpWHk6Ur@m.velo-antwerpen.be/data/stations.json");
$decoded = json_decode($data->data);
//todo: convert to wished format
$result = array();
$gpoint = new gPoint();
foreach ($decoded as $sourceStation) {
$station = new Object();
$station->name = $sourceStation->name;
$station->freebikes = $sourceStation->bikes;
$station->freespots = $sourceStation->slots;
$station->state = $sourceStation->status;
$station->latitude = $sourceStation->latitude;
$station->longitude = $sourceStation->longitude;
$gpoint->setLongLat($station->longitude, $station->latitude);
if ($this->lat != null && $this->long != null) {
$station->distance = $gpoint->distanceFrom($this->long, $this->lat);
}
array_push($result, $station);
}
function compare($a, $b)
{
if ($a->distance == $b->distance) {
return 0;
}
return ($a->distance < $b->distance) ? -1 : 1;
}
if ($this->lat != null && $this->long != null) {
usort($result, "compare");
}
return array_slice($result, $this->offset, $this->rowcount);
}
public static function getDoc()
{
return "This resource contains dynamic information about the availability of bikes in Antwerp";
}
}