-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGeocoder.php
205 lines (164 loc) · 6.35 KB
/
Geocoder.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<?php
/* Copyright (C) 2011 by iRail vzw/asbl */
/*
This file is part of iWay.
iWay is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
iWay is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with iWay. If not, see <http://www.gnu.org/licenses/>.
http://www.beroads.com
Source available at http://github.com/QKaiser/IWay
*/
/**
* All functionnalities about geolocation (get coordinates from API like
* GMap, Bing or OSM; compute distance between coordinates).
*/
class Geocoder {
/*
static vars so when we are asking coordinates for a place that have been geocoded previously, we return coordinates
that we have stored before
*/
public static $from = array();
public static $from_coordinates = array();
public static function distance($from, $to){
$earth_radius = 6371.00; // km
$delta_lat = $to["lat"]-$from["lat"];
$delta_lon = $to["lng"]-$from["lng"];
$alpha = $delta_lat/2;
$beta = $delta_lon/2;
$a = sin(deg2rad($alpha)) * sin(deg2rad($alpha)) + cos(deg2rad($from["lat"])) * cos(deg2rad($to["lat"])) * sin(deg2rad($beta)) * sin(deg2rad($beta)) ;
$c = asin(min(1, sqrt($a)));
$distance = 2*$earth_radius * $c;
return round($distance);
}
public static function cmpDistances($a, $b){
if($a == $b)
return 0;
else
return ($a->distance < $b->distance) ? -1 : 1;
}
public static function sortByDistance($array){
usort($array, Geocoder::cmpDistances);
}
public static function geocode($address, $tool = "gmap") {
array_push(Geocoder::$from, $address);
//gmap api geocoding tool
if($tool=="gmap") {
$base_url = "http://maps.google.com/maps/geo?output=xml&key=ABQIAAAAnfs7bKE82qgb3Zc2YyS-oBT2yXp_ZAY8_ufC3CFXhHIE1NvwkxSySz_REpPq-4WZA27OwgbtyR3VcA";
$request_url = $base_url . "&q=" . urlencode(utf8_encode($address));
$data = TDT::HttpRequest($request_url);
$xml = simplexml_load_string($data->data);
$status = $xml->Response->Status->code;
//successful geocode
if (strcmp($status, "200") == 0) {
$geocode_pending = false;
$coordinates = $xml->Response->Placemark[0]->Point->coordinates;
$coordinates = explode(",", $coordinates);
array_push(Geocoder::$from_coordinates, array("lng"=> $coordinates[0], "lat" => $coordinates[1]));
}
//too much requests, gmap server can't handle it
else if (strcmp($status, "620") == 0) {
array_push(Geocoder::$from_coordinates, array("lng" => 0,"lat" => 0));
}
else {
array_push(Geocoder::$from_coordinates, array("lng" => 0,"lat" => 0));
}
return Geocoder::$from_coordinates[count(Geocoder::$from_coordinates)-1];
}
//openstreetmap geocoding tool (Nominatim)
else if($tool=="osm") {
$base_url = "http://nominatim.openstreetmap.org/search?q=".utf8_encode($address)."&format=xml&polygon=0&addressdetails=0";
$xml = simplexml_load_file($base_url) or die("url not loading");
if(!isset($xml->place)) {
array_push(Geocoder::$from_coordinates, array("lng" => 0,"lat" => 0));
}
else {
$place = $xml->place[0]->attributes();
array_push(Geocoder::$from_coordinates, array("lng" => (string)$place['lon'], "lat" => (string)$place['lat']));
}
return Geocoder::$from_coordinates[count(Geocoder::$from_coordinates)-1];
}
//bing map api geocoding tool
else if($tool=="bing") {
throw new Exception("Not yet implemented.");
}
else {
throw new Exception("Wrong tool parameter, please retry.");
}
}
public static function isGeocoded($address){
if($index = array_search($address, Geocoder::$from)){
return Geocoder::$from_coordinates[$index];
}else{
return false;
}
}
public static function geocodeData($data, $region, $language) {
if($region=="walloonia") {
$data = explode(" ", $data);
if($language == "EN" || $language == "NL" || $language == "DE"){
$highway = $data[4] . " " . $data[5] ." " . $data[6] ." " . (isset($data[7]) ? $data[7] : '');
}
else{
$highway = $data[3] . " " . $data[4] ." " . $data[5] ." " . $data[6];
}
//check of already geocoded
if($coords = Geocoder::isGeocoded($highway)){
return $coords;
}else{
return Geocoder::geocode("Belgium, " . $highway);
}
}
else if($region=="flanders") {
$data = str_replace(array("grens","(",")","hoflaan"), " ", $data);
$tab = explode("->", $data);
if(count($tab) >= 2){
$tab = preg_split('/(\d+)/', $tab[0], -1, PREG_SPLIT_OFFSET_CAPTURE);
if(isset($tab[2][0]) && $tab[2][0] != ''){
if($coords = Geocoder::isGeocoded($tab[2][0])){
return $coords;
}else{
return Geocoder::geocode("Belgium, " . $tab[2][0]);
}
}else{
if($coords = Geocoder::isGeocoded($tab[1][0])){
return $coords;
}else{
return Geocoder::geocode("Belgium, " . $tab[1][0]);
}
}
}else{
$tab = explode(" ", $data);
if($coords = Geocoder::isGeocoded($tab[1])){
return $coords;
}else{
return Geocoder::geocode("Belgium, " . $tab[1]);
}
}
}
else if($region=="federal"){
if(strstr($data," hauteur de") != false) {
$tab = explode(" hauteur de", $data);
$tab = explode(" ", $tab[1]);
if($coords = Geocoder::isGeocoded($tab[1])){
return $coords;
}else{
return Geocoder::geocode("Belgium, " . $tab[1]);
}
}
else {
return array("lng" => 0,"lat" => 0);
}
}
else{
throw new Exception("Wrong region parameter, please retry.");
}
}
};
?>