forked from simplegeo/php-simplegeo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SimpleGeo.php
437 lines (303 loc) · 11 KB
/
SimpleGeo.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
<?
include 'OAuth.php';
include 'CURL.php';
/**
Contains a latitude and longitude. Simply represents a coordinate
**/
class GeoPoint {
public $lat;
public $lng;
}
/**
A record object contains data regarding an arbitrary object and the
layer it resides in
http://simplegeo.com/docs/getting-started/storage#what-record
**/
class Record {
private $Properties;
public $Layer;
public $ID;
public $Created;
public $Latitude;
public $Longitude;
/**
Create a record with, location is optional if not inserting/updating
@var string $layer The name of the layer
@var string $id The unique identifier of the record
@param double $lat Latitude
@param double $lng Longitude
**/
public function Record($layer, $id, $lat = NULL, $lng = NULL) {
$this->Layer = $layer;
$this->ID = $id;
$this->Latitude = $lat;
$this->Longitude = $lng;
$this->Properties = array();
$this->Created = time();
}
/**
Returns an array representation of the Record
**/
public function ToArray() {
return array(
'type' => 'Feature',
'id' => $this->ID,
'created' => $this->Created,
'geometry' => array(
'type' => 'Point',
'coordinates' => array($this->Latitude, $this->Longitude),
),
'properties' => (object) $this->Properties
);
}
public function __set($key, $value) {
$this->Properties[$key] = $value;
}
public function __get($key) {
return isset($this->Properties[$key]) ? $this->Properties[$key] : NULL;
}
}
class SimpleGeo extends CURL {
private $consumer;
private $token, $secret;
private $format;
const BASE_URL = 'http://api.simplegeo.com/';
const MONDAY = 'mon';
const TUESDAY = 'tue';
const WEDNESDAY = 'wed';
const THURSDAY = 'thu';
const FRIDAY = 'fri';
const SATURDAY = 'sat';
const SUNDAY = 'sun';
public function SimpleGeo($token = false, $secret = false) {
$this->format = 'json';
$this->token = $token;
$this->secret = $secret;
$this->consumer = new OAuthConsumer($this->token, $this->secret);
}
/**
Extracts the ID from a SimpleGeo ID (SG_XXXXXXXXXXXXXXXXXXXXXX)
@param string id The SimpleGeo ID of a feaure
**/
public static function ExtractID($id) {
preg_match('~SG_[A-Za-z0-9]{22}~', $id, $matches);
return isset($matches[0]) ? $matches[0] : false;
}
/**
Returns a list of all possible feature categories
**/
public function FeatureCategories() {
return $this->SendRequest('GET', '1.0/features/categories.json');
}
/**
Returns detailed information of a feature
@var string $handle Feature ID
**/
public function Feature($handle) {
return $this->SendRequest('GET', '1.0/features/' . $handle . '.json');
}
/**
Returns context of an IP
@var string $ip IP Address
**/
public function ContextIP($ip, $opts = false) {
return $this->SendRequest('GET', '1.0/context/' . $ip . '.json', $opts);
}
/**
Returns context of a coordinate
@var mixed $lat Latitude or GeoPoint
@var float $lng Longitude
**/
public function ContextCoord($lat, $lng = false, $opts = false) {
if ($lat instanceof GeoPoint) {
$lng = $lat->lng;
$lat = $lat->lat;
if (is_array($lng)) $opts = $lng;
}
return $this->SendRequest('GET', '1.0/context/' . $lat . ',' . $lng . '.json');
}
/**
Returns context of an address
@var string $address Human readable address (US only)
**/
public function ContextAddress($address, $opts = false) {
return $this->SendRequest('GET', '1.0/context/address.json', array('address' => $address));
}
/**
Returns places nearby an IP
@var string $ip IP Address
@param string q Search query
@param string category Filter by a classifer (see https://gist.github.com/732639)
@param float radius Radius in km (default=25)
**/
public function PlacesIP($ip, $opts = false) {
return $this->SendRequest('GET', '1.0/places/' . $ip, $opts . '.json');
}
/**
Returns places nearby a coordinate
@var mixed $lat Latitude or GeoPoint
@var float $lng Longitude
@param string q Search query
@param string category Filter by a classifer (see https://gist.github.com/732639)
@param float radius Radius in km (default=25)
**/
public function PlacesCoord($lat, $lng = false, $opts = false) {
if ($lat instanceof GeoPoint) {
if (is_array($lng)) $opts = $lng;
$lng = $lat->lng;
$lat = $lat->lat;
}
return $this->SendRequest('GET', '1.0/places/' . $lat . ',' . $lng . '.json', $opts);
}
/**
Returns places nearby an address
@var string $address Human readable address (US only)
@param string q Search query
@param string category Filter by a classifer (see https://gist.github.com/732639)
@param float radius Radius in km (default=25)
**/
public function PlacesAddress($address, $opts = array()) {
return $this->SendRequest('GET', '1.0/places/address.json', array_merge(array(
'address' => $address
), $opts));
}
/**
Use the Places endpoint to contribute a new feature to SimpleGeo Places. Each new feature
gets a unique, consistent handle; if you insert the same record twice, the more recent
insert overwrites the previous one.
When you add a feature, it is visible only to your application until approved for general
use. To keep your features private to your application, include "private": true in the GeoJSON
properties.
@var Feature $place A place object to insert
**/
public function CreatePlace(Place $place) {
$context = $this->ContextCoord($place->Latitude, $place->Longitude);
return $this->SendRequest('POST', '1.0/places', json_encode($place->ToArray()));
}
/**
Update a place
@var Feature $place A place object to modify
**/
public function UpdatePlace(Place $place) {
return $this->SendRequest('POST', '1.0/features/' . $place->ID . '.json', json_encode($place->ToArray()));
}
/**
Suggests that a Feature be deleted and effectively hides it from your view. Requires a handle.
Returns a status token.
@var Feature $place The place to delete
**/
public function DeletePlace(Place $place) {
return $this->SendRequest('DELETE', '1.0/features/' . $place->ID . '.json');
}
/**
Returns density information of a coordinate
@var string $dayname A day name (mon, tue, wed, thu, fri, sat, sun)
@var double $lat Latitude
@var double $lon Longitude
@param int $hour Hour of the day
**/
public function SpotRankDensity($dayname, $lat, $lon, $hour = false) {
$params = array();
if ($hour !== false) $params['hour'] = $hour;
return $this->SendRequest('GET', '1.0/density/' . $dayname . '/' . $hour . '/' . $lat . ',' . $lon . '.json', $params);
}
/**
Inserts a record according to the ID and Layer properties of the record
@var Record $record The record to insert
**/
public function PutRecord(Record $record) {
return $this->SendRequest('PUT', '0.1/records/' . $record->Layer . '/' . $record->ID . '.json', json_encode($record->ToArray()));
}
/**
Gets a record according to the ID and Layer properties of the record
@var Record $record The record to retrieve
**/
public function GetRecord(Record $record) {
return $this->SendRequest('GET', '0.1/records/' . $record->Layer . '/' . $record->ID . '.json');
}
/**
Delete a record according to the ID and Layer properties of the record
@var Record $record The record to delete
**/
public function DeleteRecord(Record $record) {
return $this->SendRequest('DELETE', '0.1/records/' . $record->Layer . '/' . $record->ID . '.json');
}
/**
Retrieve the history of an individual record
@var Record $record The record to retrieve the history of
**/
public function RecordHistory(Record $record) {
return $this->SendRequest('GET', '0.1/records/' . $record->Layer . '/' . $record->ID . '/history.json');
}
/**
Retrieve records nearby the coordinate given
@var string $layer The name of the layer to retrieve records from
@var double $lat Latitude
@var double $lat Longitude
@params array $params Additional parameters in an associate array (radius, limit, types, start, end)
**/
public function NearbyRecordsCoord($layer, $lat, $lng, $params = array()) {
return $this->SendRequest('GET', '0.1/records/' . $layer . '/nearby/' . $lat . ',' . $lng . '.json', $params);
}
/**
Retrieve records nearby a geohash
@var string $layer The name of the layer to retrieve records from
@var string $hash The geohash (see geohash.org) of the location
@params array $params Additional parameters in an associate array (radius, limit, types, start, end)
**/
public function NearbyRecordsGeohash($layer, $hash, $params = array()) {
return $this->SendRequest('GET', '0.1/records/' . $layer . '/nearby/' . $hash . '.json', $params);
}
/**
Retrieve records near an IP address
@var string $layer The name of the layer to retrieve records from
@var string $ip The IP address to search around
@params array $params Additional parameters in an associate array (radius, limit, types, start, end)
**/
public function NearbyRecordsIP($layer, $ip, $params = array()) {
return $this->SendRequest('GET', '0.1/records/' . $layer . '/nearby/' . $ip . '.json', $params);
}
/**
Include the OAuthHeader in the request
**/
private function IncludeAuthHeader() {
$request = OAuthRequest::from_consumer_and_token($this->consumer, NULL, $this->Method, $this->GetURL(), NULL);
$request->sign_request(new OAuthSignatureMethod_HMAC_SHA1(), $this->consumer, NULL);
$this->SetHeader('Authorization', $request->to_header(true, false));
}
/**
Take the results and return a JSON decoded version
**/
public function GetResults() {
return json_decode($this->Data, true);
}
private function SendRequest($method = 'GET', $endpoint, $data = array()) {
$this->Revert(self::BASE_URL . $endpoint . '.' . $this->format);
$this->SetMethod($method);
if (is_array($data)) $this->AddVars($data);
else if (is_string($data)) $this->SetBody($data);
$this->IncludeAuthHeader();
return $this->Get();
}
}
/*
(The MIT License)
Copyright (c) 2011 Rishi Ishairzay <rishi [at] ishairzay [dot] com>
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
?>