source: subversion/sites/namefinder/php/placeindex.php @ 17445

Last change on this file since 17445 was 8132, checked in by david, 11 years ago

namefinder version 2 - incremental updates; word indexes

File size: 2.9 KB
Line 
1<?php 
2
3class placeindex {
4
5  /* A class mapping directly to the database table of the same name,
6     duplicates a subset of the information in class named, both in
7     the fields it stores and that only true places 9rank > 0) are
8     indexed. This means when we want to seach specifically for aplace
9     we can quicklylocate it in this table and then use its id to
10     retrieve the full detaqils from the named table, There are very
11     many fewer places than nameds, and this makes for a major
12     efficiency improvement.
13
14     See named for definitions of fields, they are identical.
15     All fields present in database.
16  */
17
18  var $id;
19  var $region;
20  var $lat;
21  var $lon;
22  var $rank;    /* By definition, greater than zero only in this table */
23
24
25  // --------------------------------------------------
26  /* static */ function placeindexfromnamed($named) {
27    /* A factory for placeindex records, derived from named */
28    $placeindex = new placeindex();
29    $placeindex->id = $named->id;
30    $placeindex->region = $named->region;
31    $placeindex->lat = $named->lat;
32    $placeindex->lon = $named->lon;
33    $placeindex->rank = $named->rank;   
34    return $placeindex;
35  }
36
37  // --------------------------------------------------
38  function findnearbyplaces($rank, $maxresults) {
39    /* uses the place index to find the nearest place to this (excluding this,
40       of course).
41
42       rank: if non-zero only locates places for the particular rank
43       given. Can also be an array of ranks in which case we restrict
44       the search to any of those ranks
45
46       maxresults: the maximum number of results to return.
47
48       returns an array (possiby empty if no nearby places) of found placeindexes
49    */
50
51    global $db;
52    include_once('region.php');
53
54    $q = $db->query();
55
56    /* the place sought must be in the same or neighbouring region as this */
57    $region = new region($this->lat, $this->lon);
58    $regionnumbers = $region->considerregions();
59    $conditions = array();
60    foreach ($regionnumbers as $regionnumber) { 
61      $conditions[] = y_op::eq('region', $regionnumber); 
62    }
63    $conditions = count($conditions) == 1 ? $conditions[0] : y_op::oor($conditions);
64
65    /* limit the search by rank if necessary */
66    if (is_array($rank) || $rank > 0) {
67      //not me, and only a place of given rank
68      $conditions = y_op::aand(y_op::ne('id', $this->id), 
69                              $conditions, 
70                              y_op::eq('rank', $rank));
71    }
72
73    /* sort the result by increasing distance from this, and limit to max requested */
74    $q->where($conditions);
75    $q->ascending(canonical::distancerestriction($this->lat, $this->lon, 0));
76    $q->limit($maxresults);
77
78    $placeindex = new placeindex();
79    $nearbyplaces = array();
80
81    while ($q->select($placeindex) > 0) { $nearbyplaces[] = clone $placeindex; }
82    return $nearbyplaces;
83  }
84
85
86}
87
88?>
Note: See TracBrowser for help on using the repository browser.