forked from twbell/GPLplanet
/
class.geo.php
201 lines (181 loc) · 4.77 KB
/
class.geo.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<?php
/**
* geo - Named place object, the instatiation of woeid
* @package gplplanet
* @author Tyler Bell tylerwbell[at]gmail[dot]com
* @copyright 2009,2010 - Tyler Bell
* @license GNU General Public License
*/
class geo {
//properties
private $woeid;
private $name;
private $contextName;
private $placeTypeName;
private $placeType;
private $centroid;
private $bBox;
private $country;
//object cache
private $aliases;
private $children;
private $siblings;
private $parent;
private $belongTo;
private $consistOf;
private $adjacencies;
//===================================
/**
* Constructor
* @param array row assoc. array from places table
* @param obj geo Geo object
* @return obj Place Object
*/
function __construct($row) {
$this->woeid = $row['woeid'];
$this->name = $row['name'];
$this->contextName = $row['contextname'];
$this->placeTypeName = $row['placetypename'];
$this->placeType = $row['placetype'];
$this->country = $row['country'];
$this->bBox['ne_lat'] = $row['bbox_ne_lat'];
$this->bBox['ne_lon'] = $row['bbox_ne_lon'];
$this->bBox['sw_lat'] = $row['bbox_sw_lat'];
$this->bBox['sw_lon'] = $row['bbox_sw_lon'];
$this->centroid['lon'] = $row['centroid_lon'];
$this->centroid['lat'] = $row['centroid_lat'];
}
/** Get Bounding Box
* @return array
*/
public function getBbox() {
if (isset ($this->bBox['sw_lon'])) {
return $this->bBox;
} else {
$geo = $this->getEngine()->getAndRefreshCoords($this->woeid); //updates database with coords from web service
if (!$geo) {
return false;
}
$this->updateInstanceCoords($geo); //update class properties with new coords
}
return $this->bBox;
}
/** Get coordinates of centroid
* @return array
*/
public function getCentroid() {
if (isset ($this->centroid['lon'])) {
return $this->centroid;
} else {
$geo = $this->getEngine()->getAndRefreshCoords($this->woeid); //updates database with coords from web service
if (!$geo) {
return false;
}
$this->updateInstanceCoords($geo); //update class properties with new coords
}
return $this->centroid;
}
/**
* Updates coordinates of this instance with coordinates from another geo object
* @return bool
*/
protected function updateInstanceCoords($geo) {
$centroid = $geo->getCentroid();
$bBox = $geo->getBbox();
unset ($geo);
//assign coordinates to this instance of object
$this->centroid['lon'] = $centroid['centroid_lon'];
$this->centroid['lat'] = $centroid['centroid_lat'];
$this->bBox['ne_lat'] = $bBox['bbox_ne_lat'];
$this->bBox['ne_lon'] = $bBox['bbox_ne_lon'];
$this->bBox['sw_lat'] = $bBox['bbox_sw_lat'];
$this->bBox['sw_lon'] = $bBox['bbox_sw_lon'];
return true;
}
/** Get woeid
* @return int woeid
*/
public function getWoeid() {
return $this->woeid;
}
/** Get placename
* @return string placename
*/
public function getName() {
return $this->name;
}
/** Get placename with geographic context or qualifier
* Falls back to normal name if not context name available
* @param Bool nameType return string label of place type
* @return string
*/
public function getContextName() {
if ($this->contextName) {
return $this->contextName;
} else {
return $this->name;
}
}
/** Get type of this place
* @param Bool nameType return string label instead of place code
* @return mixed interger or string placetype as requested
*/
public function getPlaceType($nameType = false) {
if ($nameType) {
return $this->placeTypeName;
} else {
return $this->placeType;
}
}
/** Get children of this place
* @return array Array of woeids
*/
public function getChildren() {
if ($this->children) {
return $this->children;
}
$this->children = $this->getEngine()->getChildren($this->woeid);
return $this->children;
}
/**
* Gets geo engine singleton
* @return obj geo engine object
*/
public function getEngine() {
if (!class_exists("geoengine")) {
require_once ('class.geoengine.php');
}
return geoengine :: getInstance();
}
/** Get siblings of this place (places with same parent of same type)
* @return array Array of woeids
*/
public function getSiblings() {
if ($this->siblings) {
return $this->siblings;
}
$this->siblings = $this->getEngine()->getSiblings($this->woeid);
return $this->siblings;
}
/** Get adjacencies (neighbors) of this place
* @return array Array of woeids
*/
public function getAdjacencies() {
if ($this->adjacencies) {
return $this->adjacencies;
}
$this->adjacencies = $this->getEngine()->getAdjacencies($this->woeid);
return $this->adjacencies;
}
/** Get alternative names for this place
* @return array Array of placenames
*/
public function getAliases() {
if ($this->aliases) {
return $this->aliases;
}
$this->aliases = $this->getEngine()->getAliases($this->woeid);
return $this->aliases;
}
}
?>