source: subversion/sites/namefinder/php/relation.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: 4.9 KB
Line 
1<?php
2
3include_once('tagged.php');
4include_once('canonical.php');
5
6class relation extends tagged {
7  var $representativemember;
8  var $nodes;
9  var $ways;
10  var $relations;
11
12  // --------------------------------------------------
13  function relation($id, $amended=FALSE) { 
14    if (! $amended) { $id = canonical::getuniqueid($id, 'relation'); }
15    $this->id = $id; 
16  }
17
18  // --------------------------------------------------
19  function add_node($osmid) { $this->nodes[] = canonical::getuniqueid($osmid, 'node'); }
20
21  // --------------------------------------------------
22  function add_way($osmid) { $this->ways[] = canonical::getuniqueid($osmid, 'way'); }
23
24  // --------------------------------------------------
25  function calc_latlong() { 
26    /* The lat/lon of a relation is detemrined to be the lat/lon of a
27       representative member: its first node if there is one, or if
28       not via middle way. In time we might want to refine this to
29       take roles and/or the type of relation into account */
30    if (! $this->get_representativemember()) { return FALSE; }
31    if (! $this->representativemember->calc_latlong()) { return FALSE; } 
32    $this->lat = $this->representativemember->lat;
33    $this->lon = $this->representativemember->lon;
34    return TRUE;
35  }
36
37  // --------------------------------------------------
38  function get_representativemember() {
39    global $db;
40    if (empty($this->nodes)) { 
41      if (empty($this->ways)) { return FALSE; }
42      $wayid = $this->ways[count($this->ways)/2];
43      $way_node = new way_node();
44      $way_node->way_id = $wayid;
45      if ($db->select($way_node) == 0) { return FALSE; }
46      $node = new node($way_node->node_id, TRUE);
47      if ($db->select($node) == 0) { return FALSE; }
48      $this->representativemember = $node;
49      return TRUE;
50    }
51    $nodeid = $this->nodes[0];
52    $node = new node($nodeid, TRUE);
53    if ($db->select($node) != 1) { return FALSE; }
54    $this->representativemember = $node;
55    return TRUE;
56  }
57
58  // --------------------------------------------------
59  function insert() {
60    global $db, $added;
61    // $db->insert($this); relations themselves not stored
62
63    // and its node, way and relation references
64    if (! empty($this->nodes)) { 
65      $relation_nodes = array();
66      $relation_node = new relation_node();
67      $relation_node->relation_id = $this->id;
68      foreach ($this->nodes as $nodeid) {
69        $relation_node->node_id = $nodeid;
70        $relation_nodes[] = clone $relation_node;
71      }
72      $db->insert($relation_nodes);
73    }
74
75    if (! empty($this->ways)) { 
76      $relation_ways = array();
77      $relation_way = new relation_way();
78      $relation_way->relation_id = $this->id;
79      foreach ($this->ways as $wayid) {
80        $relation_way->way_id = $wayid;
81        $relation_ways[] = clone $relation_way;
82      }
83      $db->insert($relation_ways);
84    }
85
86    if (! empty($this->relations)) {
87      $relation_relations = array();
88      $relation_relation = new relation_relation();
89      $relation_relation->relation_id = $this->id;
90      foreach ($this->relations as $relationid) {
91        $relation_relation->other_relation_id = $relationid;
92        $relation_relations[] = clone $relation_relation;
93      }
94      $db->insert($relation_relations);
95    }
96
97    $added['relation']++;
98  }
99
100  // --------------------------------------------------
101  function delete() {
102    global $db;
103    /* relations themselves not stored
104    $relation = new relation($this->id, TRUE);
105    $db->delete($relation, 'id');
106    */
107
108    $relation_node = new relation_node();
109    $relation_node->relation_id = $this->id;
110    $db->delete($relation_node, 'relation_id');
111
112    $relation_way = new relation_way();
113    $relation_way->relation_id = $this->id;
114    $db->delete($relation_way, 'relation_id');
115
116    $relation_relation = new relation_relation();
117    $relation_relation->relation_id = $this->id;
118    $db->delete($relation_relation, 'relation_id');
119  }
120
121  // --------------------------------------------------
122  function parent_ids() {
123    /* get parent relations recursively */
124    global $db;
125    $parent_ids = array();
126    $q = $db->query();
127    $relation_relation = new relation_relation();
128    $relation_relation->other_relation_id = $this->id;
129    $relation = new relation(0, TRUE);
130    while ($q->select($relation_relation) > 0) { 
131      $relation->id = $relation_relation->relation_id; 
132      $parent_ids[] = $relation->id;
133      /* someone could have created a mutual relationship parentage,
134         which would cause a loop if not checked for: */
135      if (in_array($relation->id, $parent_ids)) { continue; }
136      $relation_parent_ids = $relation->parent_ids();
137      if (! empty($relation_parent_ids)) { 
138        $parent_ids = array_merge($parent_ids, $relation_parent_ids); 
139      }
140    }
141    $parent_ids = array_unique($parent_ids);
142    return $parent_ids;
143  }
144}
145
146?>
Note: See TracBrowser for help on using the repository browser.