-
Notifications
You must be signed in to change notification settings - Fork 10
/
class.geo.php
290 lines (263 loc) · 7.29 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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
<?php
/**
* geo - Named place object, the instatiation of a woeid
* @package gplplanet
* @author Tyler Bell tylerwbell[at]gmail[dot]com
* @copyright 2009-2011 - 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,$fetch=false) {
$this->woeid = (int) $row['woeid'];
$this->name = $row['name'];
$this->contextName = $row['contextname'];
$this->placeTypeName = $row['placetypename'];
$this->placeType = (int) $row['placetype'];
$this->country = $row['country'];
//fetch coords from webservice on instantiation if not exist
if ($fetch && is_null($row['centroid_lon'])){
if (!$serviceGeo = $this->getEngine()->getAndRefreshCoords($this->woeid)){ //updates database with coords from web service
return false;
}
$this->updateInstanceCoords($serviceGeo); //update class properties with new coords
} else {
//null if no extant coords, cast as float otherwise
if (is_null($this->centroid['lon']) && is_null($row['bbox_ne_lon'])){
$this->bBox['ne_lat'] = null;
$this->bBox['ne_lon'] = null;
$this->bBox['sw_lat'] = null;
$this->bBox['sw_lon'] = null;
$this->centroid['lon'] = null;
$this->centroid['lat'] = null;
} else {
$this->bBox['ne_lat'] = (float) $row['bbox_ne_lat'];
$this->bBox['ne_lon'] = (float) $row['bbox_ne_lon'];
$this->bBox['sw_lat'] = (float) $row['bbox_sw_lat'];
$this->bBox['sw_lon'] = (float) $row['bbox_sw_lon'];
$this->centroid['lon'] = (float) $row['centroid_lon'];
$this->centroid['lat'] = (float) $row['centroid_lat'];
}
}
}
/**
* Get clean representation (not really an instance) for JSON representation and similar
* @param bool fetch get coordinates from web service if not exist
* @return array
*/
public function getCleanInstance($fetch=false){
$geo = array();
$geo['woeid'] = $this->woeid;
$geo['name'] = $this->name;
$geo['placetypename'] = $this->placeTypeName;
$geo['placetype'] = $this->placeType;
$geo['aliases'] = $this->getAliases();
//populate coords if not exist
if ($fetch && is_null($this->centroid['lon'])){
if (!$serviceGeo = $this->getEngine()->getAndRefreshCoords($this->woeid)){ //updates database with coords from web service
return false;
}
$this->updateInstanceCoords($serviceGeo); //update class properties with new coords
}
$geo['bbox']['ne_lat'] = $this->bBox['ne_lat'];
$geo['bbox']['ne_lon'] = $this->bBox['ne_lon'];
$geo['bbox']['sw_lat'] = $this->bBox['sw_lat'];
$geo['bbox']['sw_lon'] = $this->bBox['sw_lon'];
$geo['centroid']['lon'] = $this->centroid['lon'];
$geo['centroid']['lat'] = $this->centroid['lat'];
return $geo;
}
/** Get Bounding Box
* @return array
*/
public function getBbox() {
if (!is_null($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 'lon' & 'lat'
*/
public function getCentroid() {
if (!is_null($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;
}
/**
* Alias for getCentroid()
* @return array 'lon' & 'lat'
*/
public function getCoords(){
return $this->getCentroid();
}
/**
* 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['lon'];
$this->centroid['lat'] = $centroid['lat'];
$this->bBox['ne_lat'] = $bBox['ne_lat'];
$this->bBox['ne_lon'] = $bBox['ne_lon'];
$this->bBox['sw_lat'] = $bBox['sw_lat'];
$this->bBox['sw_lon'] = $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 country
* @return string placename
*/
public function getCountry() {
return $this->country;
}
/** 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;
}
}
/**
* Gets longitude
* @return real
*/
public function getLon(){
$coords = $this->getCentroid();
return $coords['lon'];
}
/**
* Gets latitude
* @return real
*/
public function getLat(){
$coords = $this->getCentroid();
return $coords['lat'];
}
/** Get type of this place
* @param Bool nameType 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;
}
/**
* Is this a leaf node (no children)
* @param int woeid
* @return array
*/
public function isLeafNode(){
return $this->getEngine()->isLeafNode($this->woeid);
}
/**
* 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,$this->placeType);
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;
}
}
?>