forked from pietercolpaert/IWay-TDT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Radar.class.php
executable file
·175 lines (141 loc) · 4.59 KB
/
Radar.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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php
/* Copyright (C) 2011 by iRail vzw/asbl
*
* Author: Quentin Kaiser <[email protected]>
* License: AGPLv3
*
* This method of IWay will get all the radars of belgium territory
*/
include_once 'Geocoder.php';
include_once 'simple_html_dom.php';
class IWayRadar extends AResource{
private $from;
private $area;
private $max;
public static function getParameters(){
return array(
"max" => "Maximum of radars you want to retrieve",
"from" => "Geographic coordinates that you want data around (format : latitude,longitude)",
"offset" => "Offset let you request radars with pagination"
);
}
public static function getRequiredParameters(){
return array();
}
public function setParameter($key,$val){
if($key == "max"){
$this->max = $val;
}
else if($key == "from"){
$this->from = explode(",",$val);
}
else if($key == "area"){
$this->area = $val;
}
else if($key == "offset"){
$this->offset = $val;
}
}
private function getData(){
R::setup(Config::$DB, Config::$DB_USER, Config::$DB_PASSWORD);
$radars = R::find("radars", " name LIKE '%'");
$result = new stdClass();
for($i=0; $i < count($radars); $i++){
$result->item[$i] = new stdClass();
$result->item[$i]->name = $radars[$i]->name;
$result->item[$i]->address = $radars[$i]->address;
$result->item[$i]->lat = $radars[$i]->lat;
$result->item[$i]->lng = $radars[$i]->lon;
$result->item[$i]->date = date('d-m-Y');
$result->item[$i]->type = "fixed";
$result->item[$i]->speedLimit = $radars[$i]->speedLimit;
}
$url = "www.polfed-fedpol.be/verkeer/verkeer_radar_fr.php";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
$html = str_get_html($response);
$today = date('d');
$found = false;
$i = 0;
foreach($html->find('TABLE[width=600]') as $table){
foreach($table->find('span[class=textehome]') as $span){
foreach($span->find('a') as $a){
$date = $a->name."-".date('m-Y');
if($a->name == $today){
$found = true;
}
}
}
if($found){
foreach($table->find('TR') as $tr){
$name = "";
foreach($tr->find('TD[class=textehome]') as $td){
if($td->width == "143"){
$name = $td->plaintext;
}
else if($td->width!=25 && $td->width!=40 && $td->bgcolor == "#F5F5FC" && !strstr($td, 'center')){
$name .= " ".$td->plaintext;
}
}
if($name != ""){
$result->item[++$i] = new stdClass();
$result->item[$i]->date = $date;
$result->item[$i]->address = utf8_encode($name);
$result->item[$i]->type = "mobile";
$result->item[$i]->speedLimit = 0;
$result->item[$i]->name = utf8_encode($name);
$coordinates = Geocoder::geocode("Belgium, ".$result->item[$i]->name);
$result->item[$i]->lat = $coordinates["latitude"];
$result->item[$i]->lng = $coordinates["longitude"];
}
}
}
}
return $result;
}
public function call(){
$c = Cache::getInstance();
$element = $c->get("radars");
if(is_null($element)){
$element = $this->getData();
$c->set("radars", $element, 600);
}
/* From, area and proximity */
if($this->from != ""){
//workaround to return distance even if there is no area
if(!isset($this->area)){
$this->area = 500;
}
$items = array();
for($i = 0; $i < count($element->item); $i++){
$distance = Geocoder::distance(array("latitude"=>$this->from[0], "longitude"=>$this->from[1]),array("latitude"=>$element->item[$i]->lat, "longitude"=>$element->item[$i]->lng));
if($distance < $this->area){
$element->item[$i]->distance = $distance;
array_push($items, $element->item[$i]);
}
}
usort($items, 'Geocoder::cmpDistances');
$element->item = $items;
}
//numerotation
$i = 0;
foreach($element->item as $item){
$item->id = $i++;
}
/* Max parameter */
//As elements are stored in cache, if a user request items with max parameter there will be missing items for next requests
// so I use array_slice, that's NOT lazy :)
$element->item = array_slice($element->item, (isset($this->offset) ? $this->offset : 0), (isset($this->max) ? $this->max : count($element->item))+1);
return $element;
}
public static function getAllowedPrintMethods(){
return array("json","xml", "jsonp", "php", "html", "kml", "map");
}
public static function getDoc(){
return "Radar return a list of all known radars";
}
}
?>