| 1 |
|
<?php |
| 2 |
|
/** |
| 3 |
|
* Nominatim.php |
| 4 |
|
* 20-Mar-2012 |
| 5 |
|
* |
| 6 |
|
* PHP Version 5 |
| 7 |
|
* |
| 8 |
|
* @category Services |
| 9 |
|
* @package Services_OpenStreetMap |
| 10 |
|
* @author Ken Guest <kguest@php.net> |
| 11 |
|
* @license BSD http://www.opensource.org/licenses/bsd-license.php |
| 12 |
|
* @link Nominatim.php |
| 13 |
|
*/ |
| 14 |
|
|
| 15 |
|
/** |
| 16 |
|
* Services_OpenStreetMap_Nominatim |
| 17 |
|
* |
| 18 |
|
* @category Services |
| 19 |
|
* @package Services_OpenStreetMap |
| 20 |
|
* @author Ken Guest <kguest@php.net> |
| 21 |
|
* @license BSD http://www.opensource.org/licenses/bsd-license.php |
| 22 |
|
* @link Nominatim.php |
| 23 |
|
*/ |
| 24 |
|
class Services_OpenStreetMap_Nominatim |
| 25 |
|
{ |
| 26 |
|
protected $server = 'http://nominatim.openstreetmap.org/'; |
| 27 |
|
protected $format = 'xml'; |
| 28 |
|
protected $addresssdetails = 0; |
| 29 |
|
protected $accept_language = 'en'; |
| 30 |
|
protected $polygon = null; |
| 31 |
|
protected $viewbox = null; |
| 32 |
|
protected $bounded = null; |
| 33 |
|
protected $dedupe = null; |
| 34 |
|
|
| 35 |
|
protected $limit = null; |
| 36 |
|
|
| 37 |
|
protected $transport = null; |
| 38 |
|
|
| 39 |
|
/** |
| 40 |
|
* __construct |
| 41 |
|
* |
| 42 |
|
* @param Services_OpenStreetMap_Transport $transport Transport instance. |
| 43 |
|
* |
| 44 |
|
* @return Services_OpenStreetMap_Nominatim |
| 45 |
|
*/ |
| 46 |
|
public function __construct($transport) |
| 47 |
|
{ |
| 48 |
14 |
$this->setTransport($transport); |
| 49 |
|
} |
| 50 |
|
|
| 51 |
|
/** |
| 52 |
|
* Build query portion for request. |
| 53 |
|
* |
| 54 |
|
* @param string $place Name of location/place to search for |
| 55 |
|
* |
| 56 |
|
* @return string |
| 57 |
|
*/ |
| 58 |
|
private function _buildQuery($place) |
| 59 |
|
{ |
| 60 |
6 |
$format = $this->format; |
| 61 |
6 |
$limit = $this->limit; |
| 62 |
6 |
$accept_language = $this->accept_language; |
| 63 |
6 |
$polygon = $this->polygon; |
| 64 |
6 |
$viewbox = $this->viewbox; |
| 65 |
6 |
$bounded = $this->bounded; |
| 66 |
6 |
$dedupe = $this->dedupe; |
| 67 |
|
|
| 68 |
6 |
$q = $place; |
| 69 |
|
|
| 70 |
6 |
$query = http_build_query( |
| 71 |
6 |
compact( |
| 72 |
6 |
'q', |
| 73 |
6 |
'accept_language', |
| 74 |
6 |
'format', |
| 75 |
6 |
'limit', |
| 76 |
6 |
'polygon', |
| 77 |
6 |
'viewbox', |
| 78 |
6 |
'bounded', |
| 79 |
|
'dedupe' |
| 80 |
6 |
) |
| 81 |
6 |
); |
| 82 |
6 |
return $query; |
| 83 |
|
} |
| 84 |
|
|
| 85 |
|
/** |
| 86 |
|
* search |
| 87 |
|
* |
| 88 |
|
* @param string $place Name of place to geocode |
| 89 |
|
* @param integer $limit Maximum number of results to retrieve (optional) |
| 90 |
|
* |
| 91 |
|
* @return void |
| 92 |
|
*/ |
| 93 |
|
public function search($place, $limit = null) |
| 94 |
|
{ |
| 95 |
6 |
if ($limit !== null) { |
| 96 |
6 |
$this->setLimit($limit); |
| 97 |
6 |
} |
| 98 |
|
|
| 99 |
6 |
$format = $this->format; |
| 100 |
6 |
$query = $this->_buildQuery($place); |
| 101 |
6 |
$url = $this->server . 'search?' . $query; |
| 102 |
|
|
| 103 |
6 |
$response = $this->getTransport()->getResponse($url); |
| 104 |
6 |
if ($format == 'xml') { |
| 105 |
4 |
$xml = simplexml_load_string($response->getBody()); |
| 106 |
4 |
$places = $xml->xpath('//place'); |
| 107 |
4 |
return $places; |
| 108 |
2 |
} elseif ( $format == 'json' ) { |
| 109 |
1 |
$places = json_decode($response->getBody()); |
| 110 |
1 |
return $places; |
| 111 |
1 |
} elseif ($format == 'html') { |
| 112 |
1 |
return $response->getBody(); |
| 113 |
1 |
} |
| 114 |
|
} |
| 115 |
|
|
| 116 |
|
/** |
| 117 |
|
* setFormat |
| 118 |
|
* |
| 119 |
|
* @param string $format Set format for data to be received in (html, json, xml) |
| 120 |
|
* |
| 121 |
|
* @return Services_OpenStreetMap_Nominatim |
| 122 |
|
* @throws Services_OpenStreetMap_RuntimeException If the specified format |
| 123 |
|
* is not supported. |
| 124 |
|
*/ |
| 125 |
|
public function setFormat($format) |
| 126 |
|
{ |
| 127 |
|
switch($format) { |
| 128 |
6 |
case 'html': |
| 129 |
6 |
case 'json': |
| 130 |
6 |
case 'xml': |
| 131 |
5 |
$this->format = $format; |
| 132 |
5 |
break; |
| 133 |
1 |
default: |
| 134 |
1 |
throw new Services_OpenStreetMap_RuntimeException( |
| 135 |
1 |
sprintf('Unrecognised format (%s)', $format) |
| 136 |
1 |
); |
| 137 |
1 |
} |
| 138 |
5 |
return $this; |
| 139 |
|
} |
| 140 |
|
|
| 141 |
|
/** |
| 142 |
|
* get which format is set for this instance (xml, json, html) |
| 143 |
|
* |
| 144 |
|
* @return string |
| 145 |
|
*/ |
| 146 |
|
public function getFormat() |
| 147 |
|
{ |
| 148 |
3 |
return $this->format; |
| 149 |
|
} |
| 150 |
|
|
| 151 |
|
/** |
| 152 |
|
* setLimit |
| 153 |
|
* |
| 154 |
|
* @param integer $limit Maximum number of entries to retrieve |
| 155 |
|
* |
| 156 |
|
* @return Services_OpenStreetMap_Nominatim |
| 157 |
|
*/ |
| 158 |
|
public function setLimit($limit) |
| 159 |
|
{ |
| 160 |
8 |
if (is_numeric($limit)) { |
| 161 |
7 |
$this->limit = $limit; |
| 162 |
7 |
} else { |
| 163 |
1 |
throw new Services_OpenStreetMap_RuntimeException( |
| 164 |
|
'Limit must be a numeric value' |
| 165 |
1 |
); |
| 166 |
|
} |
| 167 |
7 |
return $this; |
| 168 |
|
} |
| 169 |
|
|
| 170 |
|
/** |
| 171 |
|
* get Limit |
| 172 |
|
* |
| 173 |
|
* @return integer |
| 174 |
|
*/ |
| 175 |
|
public function getLimit() |
| 176 |
|
{ |
| 177 |
1 |
return $this->limit; |
| 178 |
|
} |
| 179 |
|
|
| 180 |
|
/** |
| 181 |
|
* set Transport object. |
| 182 |
|
* |
| 183 |
|
* @param Services_OpenStreetMap_Transport $transport transport object |
| 184 |
|
* |
| 185 |
|
* @return Services_OpenStreetMap_Nominatim |
| 186 |
|
*/ |
| 187 |
|
public function setTransport($transport) |
| 188 |
|
{ |
| 189 |
14 |
$this->transport = $transport; |
| 190 |
14 |
return $this; |
| 191 |
|
} |
| 192 |
|
|
| 193 |
|
/** |
| 194 |
|
* Get current Transport object. |
| 195 |
|
* |
| 196 |
|
* @return Services_OpenStreetMap_Transport |
| 197 |
|
*/ |
| 198 |
|
public function getTransport() |
| 199 |
|
{ |
| 200 |
6 |
return $this->transport; |
| 201 |
|
} |
| 202 |
|
|
| 203 |
|
/** |
| 204 |
|
* Set which server to connect to. |
| 205 |
|
* |
| 206 |
|
* Possible values are 'nominatim', 'mapquest' and any other valid |
| 207 |
|
* endpoint specified as an URL. |
| 208 |
|
* |
| 209 |
|
* @param string $server Server URL or shorthand (nominatim / mapquest) |
| 210 |
|
* |
| 211 |
|
* @return Services_OpenStreetMap_Nominatim |
| 212 |
|
*/ |
| 213 |
|
public function setServer($server) |
| 214 |
|
{ |
| 215 |
|
switch($server) { |
| 216 |
2 |
case 'nominatim': |
| 217 |
1 |
$this->server = 'http://nominatim.openstreetmap.org/'; |
| 218 |
1 |
return $this; |
| 219 |
1 |
break; |
| 220 |
2 |
case 'mapquest': |
| 221 |
1 |
$this->server = 'http://open.mapquestapi.com/nominatim/v1/'; |
| 222 |
1 |
return $this; |
| 223 |
1 |
break; |
| 224 |
2 |
default: |
| 225 |
2 |
$parsed = parse_url($server); |
| 226 |
2 |
if (isset($parsed['scheme']) |
| 227 |
2 |
&& isset($parsed['host']) |
| 228 |
2 |
&& isset($parsed['path']) |
| 229 |
2 |
) { |
| 230 |
1 |
$this->server = $server; |
| 231 |
1 |
} else { |
| 232 |
1 |
throw new Services_OpenStreetMap_RuntimeException( |
| 233 |
|
'Server endpoint invalid' |
| 234 |
1 |
); |
| 235 |
|
} |
| 236 |
1 |
return $this; |
| 237 |
2 |
} |
| 238 |
|
} |
| 239 |
|
|
| 240 |
|
/** |
| 241 |
|
* Retrieve server endpoint. |
| 242 |
|
* |
| 243 |
|
* @return string |
| 244 |
|
*/ |
| 245 |
|
public function getServer() |
| 246 |
|
{ |
| 247 |
1 |
return $this->server; |
| 248 |
|
} |
| 249 |
|
} |
| 250 |
|
|
| 251 |
|
?> |