Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
6a488035 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * | |
30 | * @package CRM | |
06b69b18 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | class CRM_Contact_BAO_ProximityQuery { | |
36 | ||
37 | /** | |
38 | * Trigonometry for calculating geographical distances. | |
6c57bee6 DL |
39 | * |
40 | * Modification made in: CRM-13904 | |
41 | * http://en.wikipedia.org/wiki/Great-circle_distance | |
42 | * http://www.movable-type.co.uk/scripts/latlong.html | |
43 | * | |
6a488035 TO |
44 | * All function arguments and return values measure distances in metres |
45 | * and angles in degrees. The ellipsoid model is from the WGS-84 datum. | |
46 | * Ka-Ping Yee, 2003-08-11 | |
6a488035 TO |
47 | * earth_radius_semimajor = 6378137.0; |
48 | * earth_flattening = 1/298.257223563; | |
49 | * earth_radius_semiminor = $earth_radius_semimajor * (1 - $earth_flattening); | |
50 | * earth_eccentricity_sq = 2*$earth_flattening - pow($earth_flattening, 2); | |
6a488035 TO |
51 | * This library is an implementation of UCB CS graduate student, Ka-Ping Yee (http://www.zesty.ca). |
52 | * This version has been taken from Drupal's location module: http://drupal.org/project/location | |
53 | **/ | |
54 | ||
55 | static protected $_earthFlattening; | |
56 | static protected $_earthRadiusSemiMinor; | |
57 | static protected $_earthRadiusSemiMajor; | |
58 | static protected $_earthEccentricitySQ; | |
59 | ||
00be9182 | 60 | public static function initialize() { |
6a488035 TO |
61 | static $_initialized = FALSE; |
62 | ||
63 | if (!$_initialized) { | |
64 | $_initialized = TRUE; | |
65 | ||
66 | self::$_earthFlattening = 1.0 / 298.257223563; | |
67 | self::$_earthRadiusSemiMajor = 6378137.0; | |
68 | self::$_earthRadiusSemiMinor = self::$_earthRadiusSemiMajor * (1.0 - self::$_earthFlattening); | |
69 | self::$_earthEccentricitySQ = 2 * self::$_earthFlattening - pow(self::$_earthFlattening, 2); | |
70 | } | |
71 | } | |
72 | ||
1054415f | 73 | /* |
6a488035 TO |
74 | * Latitudes in all of U. S.: from -7.2 (American Samoa) to 70.5 (Alaska). |
75 | * Latitudes in continental U. S.: from 24.6 (Florida) to 49.0 (Washington). | |
76 | * Average latitude of all U. S. zipcodes: 37.9. | |
77 | */ | |
78 | ||
1054415f CW |
79 | /** |
80 | * Estimate the Earth's radius at a given latitude. | |
81 | * Default to an approximate average radius for the United States. | |
82 | * | |
83 | * @param float $latitude | |
84 | * @return float | |
85 | */ | |
00be9182 | 86 | public static function earthRadius($latitude) { |
6a488035 TO |
87 | $lat = deg2rad($latitude); |
88 | ||
89 | $x = cos($lat) / self::$_earthRadiusSemiMajor; | |
90 | $y = sin($lat) / self::$_earthRadiusSemiMinor; | |
91 | return 1.0 / sqrt($x * $x + $y * $y); | |
92 | } | |
93 | ||
94 | /** | |
95 | * Convert longitude and latitude to earth-centered earth-fixed coordinates. | |
96 | * X axis is 0 long, 0 lat; Y axis is 90 deg E; Z axis is north pole. | |
1054415f CW |
97 | * |
98 | * @param float $longitude | |
99 | * @param float $latitude | |
100 | * @param float $height | |
101 | * @return array | |
6a488035 | 102 | */ |
00be9182 | 103 | public static function earthXYZ($longitude, $latitude, $height = 0) { |
6a488035 TO |
104 | $long = deg2rad($longitude); |
105 | $lat = deg2rad($latitude); | |
106 | ||
107 | $cosLong = cos($long); | |
353ffa53 | 108 | $cosLat = cos($lat); |
6a488035 | 109 | $sinLong = sin($long); |
353ffa53 | 110 | $sinLat = sin($lat); |
6a488035 TO |
111 | |
112 | $radius = self::$_earthRadiusSemiMajor / sqrt(1 - self::$_earthEccentricitySQ * $sinLat * $sinLat); | |
113 | ||
114 | $x = ($radius + $height) * $cosLat * $cosLong; | |
115 | $y = ($radius + $height) * $cosLat * $sinLong; | |
116 | $z = ($radius * (1 - self::$_earthEccentricitySQ) + $height) * $sinLat; | |
117 | ||
118 | return array($x, $y, $z); | |
119 | } | |
120 | ||
121 | /** | |
122 | * Convert a given angle to earth-surface distance. | |
1054415f CW |
123 | * |
124 | * @param float $angle | |
125 | * @param float $latitude | |
126 | * @return float | |
6a488035 | 127 | */ |
00be9182 | 128 | public static function earthArcLength($angle, $latitude) { |
6a488035 TO |
129 | return deg2rad($angle) * self::earthRadius($latitude); |
130 | } | |
131 | ||
6a488035 TO |
132 | /** |
133 | * Estimate the min and max longitudes within $distance of a given location. | |
1054415f CW |
134 | * |
135 | * @param float $longitude | |
136 | * @param float $latitude | |
137 | * @param float $distance | |
138 | * @return array | |
6a488035 | 139 | */ |
00be9182 | 140 | public static function earthLongitudeRange($longitude, $latitude, $distance) { |
353ffa53 TO |
141 | $long = deg2rad($longitude); |
142 | $lat = deg2rad($latitude); | |
6a488035 TO |
143 | $radius = self::earthRadius($latitude); |
144 | ||
353ffa53 TO |
145 | $angle = $distance / $radius; |
146 | $diff = asin(sin($angle) / cos($lat)); | |
6a488035 TO |
147 | $minLong = $long - $diff; |
148 | $maxLong = $long + $diff; | |
149 | ||
353ffa53 | 150 | if ($minLong < -pi()) { |
6a488035 TO |
151 | $minLong = $minLong + pi() * 2; |
152 | } | |
153 | ||
154 | if ($maxLong > pi()) { | |
155 | $maxLong = $maxLong - pi() * 2; | |
156 | } | |
157 | ||
353ffa53 TO |
158 | return array( |
159 | rad2deg($minLong), | |
6a488035 TO |
160 | rad2deg($maxLong), |
161 | ); | |
162 | } | |
163 | ||
164 | /** | |
165 | * Estimate the min and max latitudes within $distance of a given location. | |
1054415f CW |
166 | * |
167 | * @param float $longitude | |
168 | * @param float $latitude | |
169 | * @param float $distance | |
170 | * @return array | |
6a488035 | 171 | */ |
00be9182 | 172 | public static function earthLatitudeRange($longitude, $latitude, $distance) { |
353ffa53 TO |
173 | $long = deg2rad($longitude); |
174 | $lat = deg2rad($latitude); | |
6a488035 TO |
175 | $radius = self::earthRadius($latitude); |
176 | ||
353ffa53 TO |
177 | $angle = $distance / $radius; |
178 | $minLat = $lat - $angle; | |
179 | $maxLat = $lat + $angle; | |
6a488035 TO |
180 | $rightangle = pi() / 2.0; |
181 | ||
182 | // wrapped around the south pole | |
353ffa53 | 183 | if ($minLat < -$rightangle) { |
6a488035 TO |
184 | $overshoot = -$minLat - $rightangle; |
185 | $minLat = -$rightangle + $overshoot; | |
186 | if ($minLat > $maxLat) { | |
187 | $maxLat = $minLat; | |
188 | } | |
189 | $minLat = -$rightangle; | |
190 | } | |
191 | ||
192 | // wrapped around the north pole | |
193 | if ($maxLat > $rightangle) { | |
194 | $overshoot = $maxLat - $rightangle; | |
195 | $maxLat = $rightangle - $overshoot; | |
196 | if ($maxLat < $minLat) { | |
197 | $minLat = $maxLat; | |
198 | } | |
199 | $maxLat = $rightangle; | |
200 | } | |
201 | ||
353ffa53 TO |
202 | return array( |
203 | rad2deg($minLat), | |
6a488035 TO |
204 | rad2deg($maxLat), |
205 | ); | |
206 | } | |
207 | ||
86538308 | 208 | /** |
1054415f CW |
209 | * @param float $longitude |
210 | * @param float $latitude | |
211 | * @param float $distance | |
86538308 EM |
212 | * @param string $tablePrefix |
213 | * | |
214 | * @return string | |
215 | */ | |
00be9182 | 216 | public static function where($latitude, $longitude, $distance, $tablePrefix = 'civicrm_address') { |
6a488035 TO |
217 | self::initialize(); |
218 | ||
219 | $params = array(); | |
220 | $clause = array(); | |
221 | ||
84f81538 DL |
222 | list($minLongitude, $maxLongitude) = |
223 | self::earthLongitudeRange($longitude, | |
224 | $latitude, | |
225 | $distance | |
226 | ); | |
227 | list($minLatitude, $maxLatitude) = | |
228 | self::earthLatitudeRange( | |
229 | $longitude, | |
230 | $latitude, | |
231 | $distance | |
232 | ); | |
6a488035 | 233 | |
06415a75 PJ |
234 | // DONT consider NAN values (which is returned by rad2deg php function) |
235 | // for checking BETWEEN geo_code's criteria as it throws obvious 'NAN' field not found DB: Error | |
236 | $geoCodeWhere = array(); | |
237 | if (!is_nan($minLatitude)) { | |
238 | $geoCodeWhere[] = "{$tablePrefix}.geo_code_1 >= $minLatitude "; | |
239 | } | |
240 | if (!is_nan($maxLatitude)) { | |
241 | $geoCodeWhere[] = "{$tablePrefix}.geo_code_1 <= $maxLatitude "; | |
242 | } | |
243 | if (!is_nan($minLongitude)) { | |
244 | $geoCodeWhere[] = "{$tablePrefix}.geo_code_2 >= $minLongitude "; | |
245 | } | |
246 | if (!is_nan($maxLongitude)) { | |
247 | $geoCodeWhere[] = "{$tablePrefix}.geo_code_2 <= $maxLongitude "; | |
248 | } | |
249 | $geoCodeWhereClause = implode(' AND ', $geoCodeWhere); | |
250 | ||
6a488035 | 251 | $where = " |
06415a75 | 252 | {$geoCodeWhereClause} AND |
84f81538 DL |
253 | ACOS( |
254 | COS(RADIANS({$tablePrefix}.geo_code_1)) * | |
255 | COS(RADIANS($latitude)) * | |
256 | COS(RADIANS({$tablePrefix}.geo_code_2) - RADIANS($longitude)) + | |
257 | SIN(RADIANS({$tablePrefix}.geo_code_1)) * | |
258 | SIN(RADIANS($latitude)) | |
259 | ) * 6378137 <= $distance | |
6a488035 | 260 | "; |
6a488035 TO |
261 | return $where; |
262 | } | |
263 | ||
86538308 EM |
264 | /** |
265 | * @param $query | |
1054415f | 266 | * @param array $values |
86538308 EM |
267 | * |
268 | * @throws Exception | |
269 | */ | |
00be9182 | 270 | public static function process(&$query, &$values) { |
6a488035 TO |
271 | list($name, $op, $distance, $grouping, $wildcard) = $values; |
272 | ||
273 | // also get values array for all address related info | |
274 | $proximityVars = array( | |
275 | 'street_address' => 1, | |
276 | 'city' => 1, | |
277 | 'postal_code' => 1, | |
278 | 'state_province_id' => 0, | |
279 | 'country_id' => 0, | |
280 | 'state_province' => 0, | |
281 | 'country' => 0, | |
282 | 'distance_unit' => 0, | |
283 | ); | |
284 | ||
285 | $proximityAddress = array(); | |
286 | $qill = array(); | |
287 | foreach ($proximityVars as $var => $recordQill) { | |
288 | $proximityValues = $query->getWhereValues("prox_{$var}", $grouping); | |
289 | if (!empty($proximityValues) && | |
290 | !empty($proximityValues[2]) | |
291 | ) { | |
292 | $proximityAddress[$var] = $proximityValues[2]; | |
293 | if ($recordQill) { | |
294 | $qill[] = $proximityValues[2]; | |
295 | } | |
296 | } | |
297 | } | |
298 | ||
299 | if (empty($proximityAddress)) { | |
300 | return; | |
301 | } | |
302 | ||
303 | if (isset($proximityAddress['state_province_id'])) { | |
304 | $proximityAddress['state_province'] = CRM_Core_PseudoConstant::stateProvince($proximityAddress['state_province_id']); | |
305 | $qill[] = $proximityAddress['state_province']; | |
306 | } | |
307 | ||
b05e28de DL |
308 | $config = CRM_Core_Config::singleton(); |
309 | if (!isset($proximityAddress['country_id'])) { | |
310 | // get it from state if state is present | |
311 | if (isset($proximityAddress['state_province_id'])) { | |
af7c77f2 | 312 | $proximityAddress['country_id'] = CRM_Core_PseudoConstant::countryIDForStateID($proximityAddress['state_province_id']); |
b05e28de | 313 | } |
a5175a12 | 314 | elseif (isset($config->defaultContactCountry)) { |
b05e28de DL |
315 | $proximityAddress['country_id'] = $config->defaultContactCountry; |
316 | } | |
317 | } | |
318 | ||
6a488035 TO |
319 | if (isset($proximityAddress['country_id'])) { |
320 | $proximityAddress['country'] = CRM_Core_PseudoConstant::country($proximityAddress['country_id']); | |
321 | $qill[] = $proximityAddress['country']; | |
322 | } | |
323 | ||
6a488035 | 324 | |
b05e28de DL |
325 | if ( |
326 | isset($proximityAddress['distance_unit']) && | |
6a488035 TO |
327 | $proximityAddress['distance_unit'] == 'miles' |
328 | ) { | |
329 | $qillUnits = " {$distance} " . ts('miles'); | |
330 | $distance = $distance * 1609.344; | |
331 | } | |
332 | else { | |
333 | $qillUnits = " {$distance} " . ts('km'); | |
334 | $distance = $distance * 1000.00; | |
335 | } | |
336 | ||
337 | $qill = ts('Proximity search to a distance of %1 from %2', | |
338 | array( | |
339 | 1 => $qillUnits, | |
21dfd5f5 | 340 | 2 => implode(', ', $qill), |
6a488035 TO |
341 | ) |
342 | ); | |
343 | ||
b05e28de DL |
344 | $fnName = isset($config->geocodeMethod) ? $config->geocodeMethod : NULL; |
345 | if (empty($fnName)) { | |
346 | CRM_Core_Error::fatal(ts('Proximity searching requires you to set a valid geocoding provider')); | |
347 | } | |
348 | ||
6d6c8253 DL |
349 | $query->_tables['civicrm_address'] = $query->_whereTables['civicrm_address'] = 1; |
350 | ||
d3e86119 | 351 | require_once str_replace('_', DIRECTORY_SEPARATOR, $fnName) . '.php'; |
b05e28de DL |
352 | $fnName::format($proximityAddress); |
353 | if ( | |
354 | !is_numeric(CRM_Utils_Array::value('geo_code_1', $proximityAddress)) || | |
355 | !is_numeric(CRM_Utils_Array::value('geo_code_2', $proximityAddress)) | |
356 | ) { | |
357 | // we are setting the where clause to 0 here, so we wont return anything | |
358 | $qill .= ': ' . ts('We could not geocode the destination address.'); | |
359 | $query->_qill[$grouping][] = $qill; | |
360 | $query->_where[$grouping][] = ' (0) '; | |
361 | return; | |
362 | } | |
363 | ||
364 | $query->_qill[$grouping][] = $qill; | |
b05e28de DL |
365 | $query->_where[$grouping][] = self::where( |
366 | $proximityAddress['geo_code_1'], | |
6a488035 TO |
367 | $proximityAddress['geo_code_2'], |
368 | $distance | |
369 | ); | |
b05e28de | 370 | |
6a488035 TO |
371 | return; |
372 | } | |
373 | ||
86538308 | 374 | /** |
1054415f | 375 | * @param array $input |
86538308 | 376 | */ |
00be9182 | 377 | public static function fixInputParams(&$input) { |
6a488035 TO |
378 | foreach ($input as $param) { |
379 | if (CRM_Utils_Array::value('0', $param) == 'prox_distance') { | |
380 | // add prox_ prefix to these | |
381 | $param_alter = array('street_address', 'city', 'postal_code', 'state_province', 'country'); | |
382 | ||
383 | foreach ($input as $key => $_param) { | |
384 | if (in_array($_param[0], $param_alter)) { | |
385 | $input[$key][0] = 'prox_' . $_param[0]; | |
386 | ||
387 | // _id suffix where needed | |
388 | if ($_param[0] == 'country' || $_param[0] == 'state_province') { | |
389 | $input[$key][0] .= '_id'; | |
390 | ||
391 | // flatten state_province array | |
392 | if (is_array($input[$key][2])) { | |
393 | $input[$key][2] = $input[$key][2][0]; | |
394 | } | |
395 | } | |
396 | } | |
397 | } | |
398 | return; | |
399 | } | |
400 | } | |
401 | } | |
402 | } |