source: src/webservice/htdocs/SiWeather/Webservice/GeoLocator.php@ e498070

Last change on this file since e498070 was 69c8290, checked in by Joachim Langenbach <joachim.langenbach@…>, 11 years ago
  • Property mode set to 100644
File size: 4.2 KB
RevLine 
[69c8290]1<?php
2/*
3 Copyright (C) 2015 EngSaS - Engineering Solutions and Services Langenbach. All rights reserved.
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18 MA 02110-1301 USA.
19*/
20
21namespace SiWeather\Webservice;
22
23/**
24 * @brief Retrieves geo location data for different input data
25 *
26 * Returns null if someting failed.
27 *
28 * @note Requires Services_GeoNames
29 */
30class GeoLocator
31{
32 private static $_serviceBaseUrl = "http://api.geonames.org/";
33
34 public static function configured(){
35 if(empty(self::$_serviceBaseUrl))
36 return false;
37 if(!self::getUsername())
38 return false;
39 return true;
40 }
41
42 /**
43 * @brief Get locations within radius @p radius around @p latitude and @p longitude.
44 *
45 * Returns an array of Location objects.
46 *
47 * @see Location
48 */
49 public static function locations($latitude, $longitude, $radius, &$errorMessage){
50 if(!Location::validLatitude($latitude)){
51 $errorMessage = "GeoLocator: ". $latitude ." is no valid latitude";
52 return false;
53 }
54 if(!Location::validLatitude($longitude)){
55 $errorMessage = "GeoLocator: ".$longitude ."is no valid longitude";
56 return false;
57 }
58 if(!$radius || !is_numeric($radius) || $radius < 1){
59 $errorMessage = "GeoLocator: No or invalid radius given (". $radius .")";
60 return false;
61 }
62
63 $user = self::getUsername();
64 if(!$user){
65 $errorMessage = "geonames webservice not configured";
66 return false;
67 }
68
69 $client = new \GuzzleHttp\Client
70 (
71 [
72 "base_uri" => self::$_serviceBaseUrl,
73 "timeout" => 2.0, # we need a very small timeout here, because otherwise the request of our caller function may time out!
74 "http_errors" => false,
75 ]
76 );
77 try{
78 $response = $client->get("findNearbyPlaceNameJSON?lat=". $latitude ."&lng=". $longitude ."&username=". $user ."&lang=local&radius=". $radius ."&style=full");
79 }
80 catch(Exception $exception){
81 $errorMessage = "GeoLocator: Fetched connect exception: ". $exception->getMessage();
82 error_log($errorMessage);
83 return false;
84 }
85 if(is_null($response)){
86 $errorMessage = "GeoLocator: Failed to fetch locations";
87 error_log($errorMessage);
88 return false;
89 }
90 if($response->getStatusCode() != 200){
91 if($response->getStatusCode() == 403){
92 $errorMessage = "GeoLocator: Probably too many ". $baseUrl ." requests (>2000/hour or >30000/day). Reason:". $response->getReasonPhrase();
93 }
94 else{
95 $errorMessage = "GeoLocator: Request failed with code ". $response->getStatusCode() ." and reason ". $response->getReasonPhrase();
96 }
97 error_log($errorMessage);
98 return false;
99 }
100
101 $body = $response->getBody();
102 if(empty($body)){
103 $errorMessage = "GeoLocator: Returned empty body";
104 error_log($errorMessage);
105 return false;
106 }
107 $json = json_decode($body, true);
108 if(is_null($json) || !$json || !is_array($json) || !array_key_exists("geonames", $json)){
109 $errorMessage = "GeoLocator: Could not convert answer into json data\n". $body;
110 error_log($errorMessage);
111 return false;
112 }
113
114 $result = array();
115 foreach($json["geonames"] as $geoname){
116 $result[] = new Location($geoname["name"], "",
117 $geoname["adminName1"], $geoname["adminCode1"],
118 $geoname["countryName"], $geoname["countryCode"],
119 $geoname["lat"], $geoname["lng"],
120 $geoname["timezone"]["timeZoneId"]);
121 }
122 return $result;
123 }
124
125 private static function getUsername(){
126 $webserviceConfig = new WebserviceConfig();
127 $user = $webserviceConfig->getConfig("geonamesUsername");
128 if(!$user || empty($user)){
129 error_log("GeoLocator: No geonamesUsername set. Please adjust your webservice configuration");
130 return false;
131 }
132 return $user;
133 }
134}
135
136?>
Note: See TracBrowser for help on using the repository browser.