Changeset 12304
- Timestamp:
- 09/16/07 01:04:41 (1 year ago)
- Files:
-
- trunk/midcom/org.routamc.positioning/config/manifest.inc (modified) (2 diffs)
- trunk/midcom/org.routamc.positioning/exec/geocode.php (modified) (6 diffs)
- trunk/midcom/org.routamc.positioning/geocoder/city.php (modified) (3 diffs)
- trunk/midcom/org.routamc.positioning/geocoder/geonames.php (modified) (6 diffs)
- trunk/midcom/org.routamc.positioning/geocoder/yahoo.php (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/midcom/org.routamc.positioning/config/manifest.inc
r11719 r12304 9 9 0 => 'midcom_dba_classes.inc', 10 10 ), 11 'version' => '1.1.2beta 1',11 'version' => '1.1.2beta3', 12 12 'state' => 'beta', 13 13 'package.xml' => … … 21 21 'email' => 'henri.bergius@iki.fi', 22 22 'role' => 'lead', 23 ), 24 'w_i' => 25 array ( 26 'name' => 'Jerry Jalava', 27 'email' => 'jerry.jalava@gmail.com', 28 'role' => 'developer', 23 29 ), 24 30 ), trunk/midcom/org.routamc.positioning/exec/geocode.php
r12268 r12304 1 1 <?php 2 if (! isset($_GET['service']))2 if (! isset($_GET['service'])) 3 3 { 4 4 $service = 'city'; … … 9 9 } 10 10 11 if (! isset($_GET['dir']))11 if (! isset($_GET['dir'])) 12 12 { 13 13 $direction = ''; … … 16 16 { 17 17 $direction = 'reverse_'; 18 } 19 20 $options = array(); 21 if ( isset($_GET['options']) 22 && is_array($_GET['options'])) 23 { 24 foreach ($_GET['options'] as $key => $value) 25 { 26 $options[$key] = $value; 27 } 18 28 } 19 29 … … 51 61 $method_name = "{$direction}geocode"; 52 62 53 $position = $geocoder->$method_name($location);54 if (is_null($position ))63 $positions = $geocoder->$method_name($location, $options); 64 if (is_null($positions)) 55 65 { 56 66 $error_str = 'unknown'; … … 59 69 $error_str = $geocoder->error; 60 70 } 61 //$_MIDCOM->generate_error(MIDCOM_ERRCRIT, "Geocoding failed: {$error_str}");62 71 63 72 $_MIDCOM->header('HTTP/1.0 500 Server Error'); … … 71 80 $_MIDCOM->header("Content-type: text/xml; charset=UTF-8"); 72 81 73 echo "< position>\n";74 foreach ($position as $key => $value)82 echo "<results>\n"; 83 foreach ($positions as $position) 75 84 { 76 echo " <{$key}>{$value}</{$key}>\n"; 85 echo " <position>\n"; 86 foreach ($position as $key => $value) 87 { 88 if (is_array($value)) 89 { 90 echo " <{$key}>\n"; 91 foreach ($value as $sub_key => $sub_value) 92 { 93 echo " <{$sub_key}>{$sub_value}</{$sub_key}>\n"; 94 } 95 echo " </{$key}>\n"; 96 } 97 else 98 { 99 echo " <{$key}>{$value}</{$key}>\n"; 100 } 101 } 102 echo " </position>\n"; 77 103 } 78 echo "</ position>\n";104 echo "</results>\n"; 79 105 ?> trunk/midcom/org.routamc.positioning/geocoder/city.php
r12268 r12304 30 30 * @return Array containing geocoded information 31 31 */ 32 function geocode($location )32 function geocode($location, $options=array()) 33 33 { 34 $position = array 34 $results = array(); 35 36 $parameters = array 35 37 ( 36 'latitude' => null, 37 'longitude' => null, 38 'accuracy' => null, 38 'maxRows' => 1, 39 39 ); 40 41 if (! empty($options)) 42 { 43 foreach ($options as $key => $value) 44 { 45 if (isset($parameters[$key])) 46 { 47 $parameters[$key] = $value; 48 } 49 } 50 } 51 52 if ($parameters['maxRows'] < 1) 53 { 54 $parameters['maxRows'] = 1; 55 } 40 56 41 57 if (!isset($location['city'])) … … 53 69 $qb->add_constraint('country', '=', $location['country']); 54 70 } 71 72 $qb->set_limit($parameters['maxRows']); 55 73 $matches = $qb->execute(); 56 if (count($matches) > 0) 57 { 58 $city_entry = $matches[0]; 59 } 60 61 if (is_null($city_entry)) 74 75 if (count($matches) < 1) 62 76 { 63 77 // Seek the city entry by alternate names via a LIKE query … … 70 84 } 71 85 86 $qb->set_limit($parameters['maxRows']); 72 87 $matches = $qb->execute(); 73 if (count($matches) > 0) 88 89 if (count($matches) < 1) 74 90 { 75 $city_entry = $matches[0]; 91 $this->error = 'POSITIONING_CITY_NOT_FOUND'; 92 return null; 93 } 94 } 95 96 foreach ($matches as $city_entry) 97 { 98 $city_coordinates = array 99 ( 100 'latitude' => $city->latitude, 101 'longitude' => $city->longitude, 102 ); 103 104 $position = array(); 105 $position['latitude' ] = $city_entry->latitude; 106 $position['longitude' ] = $city_entry->longitude; 107 $position['distance'] = array 108 ( 109 'meters' => 0, 110 'bearing' => null, 111 ); 112 $position['city' ] = $city_entry->city; 113 $position['region' ] = $city_entry->region; 114 $position['country' ] = $city_entry->country; 115 $position['postalcode' ] = null; 116 $position['alternate_names'] = $city->alternatenames; 117 $position['accuracy'] = ORG_ROUTAMC_POSITIONING_ACCURACY_CITY; 118 119 $results[] = $position; 120 } 121 122 return $results; 123 } 124 125 /** 126 * @param Array $coordinates Contains latitude and longitude values 127 * @param Array $options 128 * @return Array containing geocoded information 129 */ 130 function reverse_geocode($coordinates, $options=array()) 131 { 132 $results = array(); 133 134 $parameters = array 135 ( 136 'maxRows' => 1, 137 ); 138 139 if (! empty($options)) 140 { 141 foreach ($options as $key => $value) 142 { 143 if (isset($parameters[$key])) 144 { 145 $parameters[$key] = $value; 146 } 76 147 } 77 148 } 78 79 if (is_null($city_entry)) 149 150 if ( !isset($coordinates['latitude']) 151 && !isset($coordinates['longitude'])) 80 152 { 81 $this->error = 'POSITIONING_ CITY_NOT_FOUND';153 $this->error = 'POSITIONING_MISSING_ATTRIBUTES'; 82 154 return null; 83 155 } 84 156 85 $position['latitude' ] = $city_entry->latitude; 86 $position['longitude' ] = $city_entry->longitude; 87 $position['city' ] = $city_entry->city; 88 $position['region' ] = $city_entry->region; 89 $position['country' ] = $city_entry->country; 90 $position['accuracy'] = ORG_ROUTAMC_POSITIONING_ACCURACY_CITY; 157 $closest = org_routamc_positioning_utils::get_closest('org_routamc_positioning_city_dba', $coordinates, $parameters['maxRows']); 91 158 92 return $position; 93 } 94 95 /** 96 * Empty default implementation, this won't do anything yet 97 * 98 * @param Array $coordinates Contains latitude and longitude values 99 * @return Array containing geocoded information 100 */ 101 function reverse_geocode($coordinates,$options=array()) 102 { 103 $this->error = 'METHOD_NOT_IMPLEMENTED'; 104 return null; 159 if (empty($closest)) 160 { 161 $this->error = 'POSITIONING_DETAILS_NOT_FOUND'; 162 return null; 163 } 164 165 foreach ($closest as $city) 166 { 167 $city_coordinates = array 168 ( 169 'latitude' => $city->latitude, 170 'longitude' => $city->longitude, 171 ); 172 173 $position = array(); 174 $position['latitude' ] = $city->latitude; 175 $position['longitude' ] = $city->longitude; 176 $position['distance'] = array 177 ( 178 'meters' => round( org_routamc_positioning_utils::get_distance($coordinates, $city_coordinates) * 1000 ), 179 'bearing' => org_routamc_positioning_utils::get_bearing($coordinates, $city_coordinates), 180 ); 181 $position['city'] = $city->city; 182 $position['region'] = $city->region; 183 $position['country'] = $city->country; 184 $position['alternate_names'] = $city->alternatenames; 185 $position['accuracy'] = ORG_ROUTAMC_POSITIONING_ACCURACY_GPS; 186 187 $results[] = $position; 188 } 189 190 return $results; 105 191 } 106 192 } trunk/midcom/org.routamc.positioning/geocoder/geonames.php
r12268 r12304 30 30 * @return Array containing geocoded information 31 31 */ 32 function geocode($location )32 function geocode($location, $options=array()) 33 33 { 34 $position = array 34 $results = array(); 35 36 $parameters = array 35 37 ( 36 ' latitude' => null,37 ' longitude' => null,38 ' accuracy' => null,38 'radius' => null, 39 'maxRows' => 1, 40 'style' => 'FULL', 39 41 ); 42 43 if (! empty($options)) 44 { 45 foreach ($options as $key => $value) 46 { 47 if (isset($parameters[$key])) 48 { 49 $parameters[$key] = $value; 50 } 51 } 52 } 40 53 41 54 if ( !isset($location['postalcode']) … … 58 71 { 59 72 $params[] = 'country=' . urlencode($location['country']); 73 } 74 75 foreach ($parameters as $key => $value) 76 { 77 if (! is_null($value)) 78 { 79 $params[] = "{$key}=" . urlencode($value); 80 } 60 81 } 61 82 … … 70 91 return null; 71 92 } 72 73 $city_entry = $simplexml->code[0]; 74 $position['latitude' ] = (float) $city_entry->lat; 75 $position['longitude' ] = (float) $city_entry->lng; 76 $position['city' ] = (string) $city_entry->name; 77 $position['region' ] = (string) $city_entry->adminName2; 78 $position['country' ] = (string) $city_entry->countryCode; 79 $position['postalcode' ] = (string) $city_entry->postalcode; 80 $position['accuracy'] = ORG_ROUTAMC_POSITIONING_ACCURACY_CITY; 81 82 return $position; 93 94 for ($i=0; $i<$parameters['maxRows']; $i++) 95 { 96 if (! isset($simplexml->code[$i])) 97 { 98 break; 99 } 100 $entry = $simplexml->code[$i]; 101 102 $position = array(); 103 $position['latitude' ] = (float) $entry->lat; 104 $position['longitude' ] = (float) $entry->lng; 105 $position['distance'] = array 106 ( 107 'meters' => round( (float) $entry->distance * 1000 ), 108 'bearing' => null, 109 ); 110 $position['city' ] = (string) $entry->name; 111 $position['region' ] = (string) $entry->adminName2; 112 $position['country' ] = (string) $entry->countryCode; 113 $position['postalcode' ] = (string) $entry->postalcode; 114 $position['alternate_names'] = (string) $entry->alternateNames; 115 $position['accuracy'] = ORG_ROUTAMC_POSITIONING_ACCURACY_CITY; 116 117 $results[] = $position; 118 } 119 120 return $results; 83 121 } 84 122 … … 88 126 * @return Array containing geocoded information 89 127 */ 90 function reverse_geocode($coordinates, $options=array())128 function reverse_geocode($coordinates, $options=array()) 91 129 { 130 $results = array(); 131 92 132 $parameters = array 93 133 ( 94 'radius' => null,95 'maxRows' => null,96 'style' => 'FULL' 134 'radius' => 10, 135 'maxRows' => 1, 136 'style' => 'FULL', 97 137 ); 98 138 … … 128 168 129 169 $http_request = new org_openpsa_httplib(); 130 $response = $http_request->get('http://ws.geonames.org/findNearbyPlaceName?' . implode('&', $params)); 170 $url = 'http://ws.geonames.org/findNearbyPlaceName?' . implode('&', $params); 171 $response = $http_request->get($url); 131 172 $simplexml = simplexml_load_string($response); 132 173 133 174 if ( !isset($simplexml->geoname) 134 175 || count($simplexml->geoname) == 0) … … 144 185 } 145 186 146 $entry = $simplexml->geoname[0]; 147 $position['latitude' ] = (float) $entry->lat; 148 $position['longitude' ] = (float) $entry->lng; 149 $position['city' ] = (string) $entry->name; 150 $position['region' ] = (string) $entry->adminName2; 151 $position['country' ] = (string) $entry->countryCode; 152 $position['accuracy'] = ORG_ROUTAMC_POSITIONING_ACCURACY_GPS; 153 154 return $position; 187 for ($i=0; $i<$parameters['maxRows']; $i++) 188 { 189 if (! isset($simplexml->geoname[$i])) 190 { 191 break; 192 } 193 194 $entry = $simplexml->geoname[$i]; 195 196 $entry_coordinates = array 197 ( 198 'latitude' => (float) $entry->lat, 199 'longitude' => (float) $entry->lng, 200 ); 201 202 $meters = round( org_routamc_positioning_utils::get_distance($coordinates, $entry_coordinates) * 1000 ); 203 $entry_meters = round( (float) $entry->distance * 1000 ); 204 205 if ($entry_meters < $meters) 206 { 207 $meters = $entry_meters; 208 } 209 210 $position = array(); 211 $position['latitude' ] = (float) $entry->lat; 212 $position['longitude' ] = (float) $entry->lng; 213 $position['distance'] = array 214 ( 215 'meters' => $meters, 216 'bearing' => org_routamc_positioning_utils::get_bearing($coordinates, $entry_coordinates), 217 ); 218 $position['city'] = (string) $entry->name; 219 $position['region'] = (string) $entry->adminName2; 220 $position['country'] = (string) $entry->countryCode; 221 $position['postalcode' ] = (string) $entry->postalcode; 222 $position['alternate_names'] = (string) $entry->alternateNames; 223 $position['accuracy'] = ORG_ROUTAMC_POSITIONING_ACCURACY_GPS; 224 225 $results[] = $position; 226 } 227 228 return $results; 155 229 } 156 230 } trunk/midcom/org.routamc.positioning/geocoder/yahoo.php
r12268 r12304 31 31 * @return Array containing geocoded information 32 32 */ 33 function geocode($location )33 function geocode($location, $options=array()) 34 34 { 35 $position = array 35 $results = array(); 36 37 $parameters = array 36 38 ( 37 'latitude' => null, 38 'longitude' => null, 39 'accuracy' => null, 39 'output' => 'xml', 40 'appid' => $this->_config->get('yahoo_application_key'), 40 41 ); 41 42 43 if (! empty($options)) 44 { 45 foreach ($options as $key => $value) 46 { 47 if (isset($parameters[$key])) 48 { 49 $parameters[$key] = $value; 50 } 51 } 52 } 53 42 54 if ( !isset($location['postalcode']) 43 55 && !isset($location['city'])) … … 47 59 } 48 60 $params = array(); 49 $params[] = 'appid=' . $this->_config->get('yahoo_application_key');50 $params[] = 'output=xml';51 61 52 62 if (isset($location['street'])) … … 65 75 { 66 76 $params[] = 'zip=' . urlencode($location['postalcode']); 77 } 78 79 foreach ($parameters as $key => $value) 80 { 81 if (! is_null($value)) 82 { 83 $params[] = "{$key}=" . urlencode($value); 84 } 67 85 } 68 86 69 87 $http_request = new org_openpsa_httplib(); 70 $response = $http_request->get('http://local.yahooapis.com/MapsService/V1/geocode?' . implode('&', $params)); 88 $url = 'http://local.yahooapis.com/MapsService/V1/geocode?' . implode('&', $params); 89 $response = $http_request->get($url); 71 90 $simplexml = simplexml_load_string($response); 72 91 73 if (!isset($simplexml->Result->Latitude)) 74 { 75 $this->error = 'POSITIONING_CITY_NOT_FOUND'; 76 return null; 77 } 78 79 $position['latitude' ] = (float) $simplexml->Result->Latitude; 80 $position['longitude' ] = (float) $simplexml->Result->Longitude; 81 $position['street' ] = (string) $simplexml->Result->Address; 82 $position['city' ] = (string) $simplexml->Result->City; 83 $position['region' ] = (string) $simplexml->Result->State; 84 $position['country' ] = (string) $simplexml->Result->Country; 85 $position['postalcode' ] = (string) $simplexml->Result->Zip; 86 $position['accuracy'] = ORG_ROUTAMC_POSITIONING_ACCURACY_CITY; 87 88 // Cleaner cases, Yahoo! returns uppercase 89 $position['street'] = ucwords(strtolower($position['street'])); 90 $position['city'] = ucwords(strtolower($position['city'])); 91 92 foreach ($simplexml->Result->attributes() as $key => $val) 93 { 94 if ($key == 'warning') 95 { 96 $this->error = $val; 97 } 98 if ($key == 'precision') 99 { 100 switch ($val) 101 { 102 case 'address': 103 $position['accuracy'] = ORG_ROUTAMC_POSITIONING_ACCURACY_ADDRESS; 104 break; 105 case 'street': 106 $position['accuracy'] = ORG_ROUTAMC_POSITIONING_ACCURACY_STREET; 107 break; 108 default: 109 $position['accuracy'] = ORG_ROUTAMC_POSITIONING_ACCURACY_CITY; 110 break; 111 } 112 } 113 } 114 115 return $position; 92 if ( !isset($simplexml->Result) 93 || empty($simplexml->Result)) 94 { 95 $this->error = 'POSITIONING_DETAILS_NOT_FOUND'; 96 return null; 97 } 98 99 if ( !isset($options['maxRows']) 100 || $options['maxRows'] < 0) 101 { 102 $options['maxRows'] = 1; 103 } 104 105 for ($i=0; $i<$options['maxRows']; $i++) 106 { 107 if (! isset($simplexml->Result[$i])) 108 { 109 break; 110 } 111 112 $entry = $simplexml->Result[$i]; 113 114 $position = array(); 115 $position['latitude'] = (float) $entry->Latitude; 116 $position['longitude'] = (float) $entry->Longitude; 117 $position['street'] = (string) $entry->Address; 118 $position['city'] = (string) $entry->City; 119 $position['region'] = (string) $entry->State; 120 $position['country'] = (string) $entry->Country; 121 $position['postalcode'] = (string) $entry->Zip; 122 $position['accuracy'] = ORG_ROUTAMC_POSITIONING_ACCURACY_CITY; 123 124 // Cleaner cases, Yahoo! returns uppercase 125 $position['street'] = ucwords(strtolower($position['street'])); 126 $position['city'] = ucwords(strtolower($position['city'])); 127 128 foreach ($entry->attributes() as $key => $val) 129 { 130 if ($key == 'warning') 131 { 132 $this->error = $val; 133 } 134 if ($key == 'precision') 135 { 136 switch ($val) 137 { 138 case 'address': 139 $position['accuracy'] = ORG_ROUTAMC_POSITIONING_ACCURACY_ADDRESS; 140 break; 141 case 'street': 142 $position['accuracy'] = ORG_ROUTAMC_POSITIONING_ACCURACY_STREET; 143 break; 144 default: 145 $position['accuracy'] = ORG_ROUTAMC_POSITIONING_ACCURACY_CITY; 146 break; 147 } 148 } 149 } 150 151 $results[] = $position; 152 } 153 154 return $results; 116 155 } 117 156 118 157 /** 119 158 * Empty default implementation, this won't do anything yet 159 * Temporarily added geonames reverse geocoding here so we get atleast some results... 120 160 * 121 161 * @param Array $coordinates Contains latitude and longitude values 122 162 * @return Array containing geocoded information 123 163 */ 124 function reverse_geocode($coordinates, $options=array())164 function reverse_geocode($coordinates, $options=array()) 125 165 { 126 $this->error = 'METHOD_NOT_IMPLEMENTED'; 127 return null; 166 // $this->error = 'METHOD_NOT_IMPLEMENTED'; 167 // return null; 168 $results = array(); 169 170 $parameters = array 171 ( 172 'radius' => 10, 173 'maxRows' => 1, 174 'style' => 'FULL', 175 ); 176 177 if (! empty($options)) 178 { 179 foreach ($options as $key => $value) 180 { 181 if (isset($parameters[$key])) 182 { 183 $parameters[$key] = $value; 184 } 185 } 186 } 187 188 if ( !isset($coordinates['latitude']) 189 && !isset($coordinates['longitude'])) 190 { 191 $this->error = 'POSITIONING_MISSING_ATTRIBUTES'; 192 return null; 193 } 194 $params = array(); 195 196 $params[] = 'lat=' . urlencode($coordinates['latitude']); 197 $params[] = 'lng=' . urlencode($coordinates['longitude']); 198 199 foreach ($parameters as $key => $value) 200 { 201 if (! is_null($value)) 202 { 203 $params[] = "{$key}=" . urlencode($value); 204 } 205 } 206 207 $http_request = new org_openpsa_httplib(); 208 $url = 'http://ws.geonames.org/findNearbyPlaceName?' . implode('&', $params); 209 $response = $http_request->get($url); 210 $simplexml = simplexml_load_string($response); 211 212 if ( !isset($simplexml->geoname) 213 || count($simplexml->geoname) == 0) 214 { 215 $this->error = 'POSITIONING_DETAILS_NOT_FOUND'; 216 217 if (isset($simplexml->status)) 218 { 219 $constant_name = strtoupper(str_replace(" ", "_",$simplexml->status)); 220 $this->error = $constant_name; 221 } 222 return null; 223 } 224 225 for ($i=0; $i<$parameters['maxRows']; $i++) 226 { 227 if (! isset($simplexml->geoname[$i])) 228 { 229 break; 230 } 231 232 $entry = $simplexml->geoname[$i]; 233 234 $entry_coordinates = array 235 ( 236 'latitude' => (float) $entry->lat, 237 'longitude' => (float) $entry->lng, 238 ); 239 240 $meters = round( org_routamc_positioning_utils::get_distance($coordinates, $entry_coordinates) * 1000 ); 241 $entry_meters = round( (float) $entry->distance * 1000 ); 242 243 if ($entry_meters < $meters) 244 { 245 $meters = $entry_meters; 246 } 247 248 $position = array(); 249 $position['latitude' ] = (float) $entry->lat; 250 $position['longitude' ] = (float) $entry->lng; 251 $position['distance'] = array 252 ( 253 'meters' => $meters, 254 'bearing' => org_routamc_positioning_utils::get_bearing($coordinates, $entry_coordinates), 255 ); 256 $position['city'] = (string) $entry->name; 257 $position['region'] = (string) $entry->adminName2; 258 $position['country'] = (string) $entry->countryCode; 259 $position['postalcode' ] = (string) $entry->postalcode; 260 $position['alternate_names'] = (string) $entry->alternateNames; 261 $position['accuracy'] = ORG_ROUTAMC_POSITIONING_ACCURACY_GPS; 262 263 $results[] = $position; 264 } 265 266 return $results; 128 267 } 129 268 }
