-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlatitude_location.php
88 lines (68 loc) · 1.98 KB
/
latitude_location.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
<?php
public function find_country ( $limit = 10 ) {
$this->load->database();
$this->db->limit( $limit );
$this->db->select('id, lat,lng,loc_cc');
$this->db->from('photos');
$this->db->where("lat is not null");
$this->db->where("lat not in (0) ");
$this->db->where("lng is not null");
$this->db->where("lng not in (0) ");
$this->db->where('loc_cc is null');
$this->db->limit( $limit );
$this->db->order_by( "RAND(id)" );
$query = $this->db->get();
$arr = $query->result_array();
if(! $arr){
echo "All GEO location discovered. <br />\n";
return true;
}
foreach($arr as $row){
$lat = $row['lat'];
$lng = $row['lng'];
$rid = $row['id'];
echo "$lat, $lng";
$url = "http://ws.geonames.org/countryCode?type=json&lat=$lat&lng=$lng";
$content = file_get_contents ($url);
$loc_cc = '';
if($content){
$ojson = json_decode( $content );
if(! empty($ojson->countryCode)){
$loc_cc = $ojson->countryCode;
}
}
if(! $loc_cc ){
// Try the alternate countrySubdivision
// Find closest country
// Can't find countryCode of this one --> http://ws.geonames.org/countryCode?lat=-34.0595&lng=151.082
$url = "http://ws.geonames.org/countrySubdivisionJSON?maxRows=10&radius=40&lat=$lat&lng=$lng";
$content = file_get_contents ($url);
if($content){
$ojson = json_decode( $content );
if(! empty($ojson->countryCode)){
$loc_cc = $ojson->countryCode;
}
}
if(! $content ){
echo " = no content received... <br/>";
break;
}
}
if( $loc_cc ) {
if( strlen( $loc_cc ) > 2 ){
//maybe encountered some error!
$loc_cc = '-';
echo " = Err?";
//continue;
}
}
$is_au = $loc_cc == 'AU' ? 1 : 0;
$data = array(
'is_au' => $is_au
, 'loc_cc' => $loc_cc
);
$this->db->where('id', $rid );
$this->db->update( 'photos', $data);
usleep( rand( 300, 800 ) ); //be polite to the API server
}
}