/[drupal]/contributions/modules/letters/letters_location.inc
ViewVC logotype

Contents of /contributions/modules/letters/letters_location.inc

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.2 - (show annotations) (download) (as text)
Fri Mar 13 16:18:51 2009 UTC (8 months, 2 weeks ago) by seanr
Branch: MAIN
CVS Tags: DRUPAL-6--1-4, DRUPAL-6--1-0, DRUPAL-6--1-1, DRUPAL-6--1-2, DRUPAL-6--1-3, HEAD
Changes since 1.1: +167 -0 lines
File MIME type: text/x-php
location search
1 <?php
2
3
4 function letters_location_nearby_postalcodes_bylocation($location, $distance, $distance_unit = 'km') {
5 // DEBUG: commented code is for testing/debugging purposes
6 //$start_time = microtime();
7
8 $latlon = location_latlon_rough($location);
9
10 // If we could not get lat/lon coordinates for the given location, return an empty search result set.
11 if (!isset($latlon['lat']) || !isset($latlon['lon'])) {
12 return array();
13 }
14
15 // If the distance parameters did not make sense, return an empty search result set.
16 if (!($distance_float = _location_convert_distance_to_meters($distance, $distance_unit))) {
17 return array();
18 }
19
20 $search_results = _letters_location_nearby_postalcodes($latlon['lon'], $latlon['lat'], $distance_float);
21
22 //DEBUG: commented code is for testing/debugging
23 //$format_start_time = microtime();
24
25 _letters_location_format_search_result_distances($search_results, $distance_unit);
26
27 //$format_end_time = microtime();
28 //print 'Time for FORMATTING to complete: '. _letters_location_time_difference($format_end_time, $format_start_time) ."<br/>\n";
29
30 // DEBUG: commented code is for testing/debugging purposes
31 //$end_time = microtime();
32 //print 'Time for this search to complete: '. _letters_location_time_difference($end_time, $start_time) ."<br/>\n";
33
34 return $search_results;
35 }
36
37
38 function _letters_location_nearby_postalcodes($lon, $lat, $distance) {
39 # $search_results = _letters_location_search_results_from_cache($lon, $lat, $distance);
40
41 // If there were usable cached search_results then return those and go no further... awwwww yeah.
42 # if (count($search_results)) {
43 # return $search_results;
44 # }
45 //------------------------------------------------------------------------------------------
46 // Pulling from the cache takes place right here.
47 // The cache id ("cid", to be inserted into the cache table) will be a concatenation of
48 // -> "letters_location_prox_search:".
49 // -> round($lon, 3) . ':'.
50 // -> round($lat, 3) . ":".
51 // -> $distance
52 // The value of the cached item will be a serialize($result_array)
53 //
54 // The cache will be cleared of proximity searches when there is a change in countries that have
55 // been configured into the system.
56 //------------------------------------------------------------------------------------------
57
58 $search_results = array();
59
60 $latrange = earth_latitude_range($lon, $lat, $distance);
61 $lonrange = earth_longitude_range($lon, $lat, $distance);
62
63 //$query_start_time = microtime();
64
65 $result = db_query('SELECT zip, city, state, country, '. earth_distance_sql($lon, $lat) .' as distance FROM {zipcodes} WHERE latitude > %f AND latitude < %f AND longitude > %f AND longitude < %f AND '. earth_distance_sql($lon, $lat) .' < %f ORDER by distance', $latrange[0], $latrange[1], $lonrange[0], $lonrange[1], $distance);
66
67 while ($result_row = db_fetch_object($result)) {
68 $search_results[$result_row->country . $result_row->zip] = array('city' => $result_row->city, 'province' => $result_row->state, 'distance' => $result_row->distance);
69 }
70
71 //DEBUG: commented code for testing/debugging purposes
72 //$query_end_time = microtime();
73
74 //print 'TOTAL TIME FOR _letters_location_nearby_postalcodes() '. _letters_location_time_difference($query_end_time, $query_start_time) ."<br/>\n";
75 //--------------------------------------------------------------------------------------------
76 // This is the spot where search results are cached
77
78 #cache_set('letters_location_prox_search:'. round($lon, 3) .':'. round($lat, 3) .':'. $distance, 'cache', serialize($search_results));
79
80 // DEBUG: commented code is for testing/debugging purposes
81 //print 'POSTAL CODE SEARCH CACHING: Wrote new search results to cache'."<br/>\n";
82 //--------------------------------------------------------------------------------------------
83 return $search_results;
84 }
85
86
87
88 function _letters_location_search_results_from_cache($lon, $lat, $distance) {
89 $cache_id_prefix = 'letters_location_prox_search:'. round($lon, 3) .':'. round($lat, 3) .':';
90
91 $result = db_query("SELECT cid FROM {cache} WHERE cid LIKE '%s%%'", $cache_id_prefix);
92
93 if ($result_row = db_fetch_object($result)) {
94 // A previous search has been done on the same search point, possibily with the an equal or different
95 // search radius.
96 $cached_key_fields = explode(':', $result_row->cid);
97 $previous_search_radius = $cached_key_fields[3];
98
99 // If the search-radius is less than or equal to the previous search-radius, then just use
100 // the appropriate subset of the previous search result.
101 // This is very convenient since previous search results are sorted in ascending order
102 // by their distance from the search point.
103 if ($distance <= $previous_search_radius) {
104
105 $cached_search_results = cache_get($result_row->cid);
106 $cached_search_results = unserialize($cached_search_results->data);
107 // If the cached-search had the exact same search-radius, just return the entire search result's
108 // array from before,
109 // otherwise, go through the distance-sorted search results and pick them out until the distances
110 // of each search result start being something greater than the current search-radius
111 if ($distance == $previous_search_radius) {
112 // DEBUG: commented code is for testing/debugging purposes
113 //print 'POSTAL CODE SEARCH CACHING: Returning EXACT SAME of search results from before'."<br/>\n";
114 return $cached_search_results;
115 }
116 else {
117 $current_search_results = array();
118 foreach ($cached_search_results as $key => $cached_result) {
119 if ($cached_result['distance'] <= $distance) {
120 $current_search_results[$key] = $cached_result;
121 }
122 else {
123 break;
124 }
125 }
126 // DEBUG: commented code is for testing/debugging purposes
127 //print 'POSTAL CODE SEARCH CACHING: Returning SUBSET of a previous search\'s results'."<br/>\n";
128 return $current_search_results;
129 }
130 }
131 else {
132 // If the previous search-radius on the same point is smaller than the current search-radius,
133 // then delete the previous search from the cache to make way in the cache for the results of
134 // the current, more comprehensive search being done on the same point, but on a larger radius.
135 // Return an empty array to let the calling function know that it will have to do a new search.
136
137 // DEBUG: commented code is for testing/debugging purposes
138 //print 'POSTAL CODE SEARCH CACHING: Throwing out old search on a point because new search uses larger search-radius'."<br/>\n";
139 cache_clear_all($result_row->cid, 'cache');
140 return array();
141 }
142
143 }
144 else {
145 // This else-clause ties back to the first if-clause in this function.
146 // It executes if no search has been done on this point.
147 // If the {cache} table did not contain any useful cache id's, return the empty array.
148 // This will let the calling function know that it has to do an original search.
149 return array();
150 }
151 }
152
153
154 function _letters_location_format_search_result_distances(&$results_array, $distance_unit = 'km') {
155 if ($distance_unit != 'km' && $distance_unit != 'mile') {
156 $distance_unit = 'km';
157 }
158
159 // $conversion_factor = number to divide by to convert meters to $distance_unit
160 // At this point, $distance_unit == 'km' or 'mile' and nothing else
161 $conversion_factor = ($distance_unit == 'km') ? 1000.0 : 1609.347;
162
163 foreach ($results_array as $index => $single_result) {
164 $results_array[$index]['distance'] = round($single_result['distance']/$conversion_factor, 1);
165 }
166 }
167

  ViewVC Help
Powered by ViewVC 1.1.2