forked from csev/py4e
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlocations.php
More file actions
84 lines (77 loc) · 3.12 KB
/
locations.php
File metadata and controls
84 lines (77 loc) · 3.12 KB
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
<?php
use \Tsugi\Util\Net;
use \Tsugi\Util\Mersenne_Twister;
$GEODATA = false;
$json_data = false;
if ( file_exists('locations.txt') ) {
$json_data = file_get_contents('locations.txt');
} else if (file_exists('../locations.txt') ) {
$json_data = file_get_contents('../locations.txt');
} else if (file_exists('data/locations.txt') ) {
$json_data = file_get_contents('data/locations.txt');
}
$json = null;
if ( $json_data != false ) {
$json = json_decode($json_data, true);
if ( is_array($json) && count($json) > 0 ) {
// OK
} else {
$json = null;
}
}
if ( $json !== null ) {
$LOCATIONS = array_keys($json);
$GEODATA = $json;
}
$LOCATIONS=array_unique($LOCATIONS);
sort($LOCATIONS);
// Need to do this more than once as data changes
function load_opengeo($code, $api_url) {
global $LOCATIONS;
$retval = false;
$MT = new Mersenne_Twister($code);
$sample = $MT->shuffle($LOCATIONS);
for ($i=0; $i< 2; $i++) {
$sample_location = $sample[$i];
// Retrieve the data
$sample_url = $api_url . '?q=' . urlencode($sample_location) . "&key=42";
$sample_data = Net::doGet($sample_url);
$sample_count = strlen($sample_data);
$response = Net::getLastHttpResponse();
$sample_json = json_decode($sample_data);
if ( $response != 200 || $sample_json == null || ( !isset($sample_json->features[0])) ||
! isset($sample_json->features[0]->properties) ||
! isset($sample_json->features[0]->properties->plus_code) ) {
echo("<pre>\n");echo(htmlentities($sample_data));echo("</pre>\n");
error_log("DIE: Load $i fail response=$response url=$sample_url json_error=".json_last_error_msg());
continue;
}
$sample_place = $sample_json->features[0]->properties->plus_code;
return array($sample_location, $sample_place, $sample_count, $sample_url);
}
die("Could not load sample response=$response url=$sample_url json_error=".json_last_error_msg());
}
// Need to do this more than once as data changes
function load_geo($code, $api_url) {
global $LOCATIONS;
$retval = false;
$MT = new Mersenne_Twister($code);
$sample = $MT->shuffle($LOCATIONS);
for ($i=0; $i< 10; $i++) {
$sample_location = $sample[$i];
// Retrieve the data
$sample_url = $api_url . '?address=' . urlencode($sample_location) . "&key=42";
$sample_data = Net::doGet($sample_url);
$sample_count = strlen($sample_data);
$response = Net::getLastHttpResponse();
$sample_json = json_decode($sample_data);
if ( $response != 200 || $sample_json == null || ( !isset($sample_json->results[0])) ||
! isset($sample_json->results[0]->place_id) ) {
error_log("DIE: Load $i fail response=$response url=$sample_url json_error=".json_last_error_msg());
continue;
}
$sample_place = $sample_json->results[0]->place_id;
return array($sample_location, $sample_place, $sample_count, $sample_url);
}
die("Could not load sample response=$response url=$sample_url json_error=".json_last_error_msg());
}