-
Notifications
You must be signed in to change notification settings - Fork 14
/
updateGeo.php
152 lines (136 loc) · 3.85 KB
/
updateGeo.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
<?php
#var_dump(getGeo('2750', '135'));
#die();
$mysqli = new mysqli('localhost', 'ctt', 'dm9WYUEC64bEbFv6', 'CTT');
$qry = "select distinct CP4, CP3 from codigosPostais where LONGITUDE = '' AND LATITUDE = '' order by CP4 asc, CP3 asc limit 2500";
$qid = $mysqli->query($qry);
if($qid->num_rows == 0){
echo "No results in database to update\n";
die();
}
while($row = $qid->fetch_object()){
$geo = getGeo($row->CP4, $row->CP3);
if(!isset($geo->lat)){
echo "$row->CP4-$row->CP3|error|$geo\n";
continue;
}
$updQry = "update codigosPostais SET LONGITUDE=$geo->lat, LATITUDE=$geo->lng WHERE CP4=$row->CP4 AND CP3='$row->CP3'";
$updQid = $mysqli->query($updQry);
$aff = $mysqli->affected_rows;
echo "$row->CP4-$row->CP3|$aff\n";
#die();
}
function getGeo($cp4, $cp3){
$url = "http://maps.googleapis.com/maps/api/geocode/json?address=$cp4-$cp3&sensor=false&components=country:PT";
#$url = "http://ditu.google.cn//maps/api/geocode/json?address=$cp4-$cp3&sensor=false&components=country:PT";
/* IP CHANGING STUFF
*
* Uncomment this and the comment block a few lines below
* only if you understand what it does, and after reading
* changeIp() function comments
*
$i = 0;
if(!file_exists('/ramdrive/googleip.txt')){
file_put_contents('/ramdrive/googleip.txt','10.100.100.15');
$currentIp = '10.100.100.16'; // default IP
}else{
$currentIp = file_get_contents('/ramdrive/googleip.txt');
}
$opts = array(
'socket' => array(
'bindto' => "$currentIp:0",
),
);
$context = stream_context_create($opts);
again:
$data = json_decode(file_get_contents($url,false,$context));
*/
$data = @file_get_contents($url,false);
if($data == false){
return "error fetching $url";
}
$data = json_decode($data);
if($data->status == 'OVER_QUERY_LIMIT'){
/* IP Changing stuff
if($i > 6){
die('OVER_QUERY_LIMIT');
}
$i++;
$context = stream_context_create(changeIp());
goto again:
*/
echo "Google returned over query limit\n";
die();
}
//$geo = $data->results[0]->geometry->location;
$maxKey = max(array_keys($data->results));
$geo = $data->results[$maxKey]->geometry->location;
foreach($data->results as $possibleResult){ // match best results according to http://www.portugal-a-programar.pt/forums/topic/74313-pesquisa-por-latitude-e-longitude-por-c%C3%B3digos-postais/#comment-599669
if(substr($possibleResult->formatted_address,0,8)=="$cp4-$cp3"){
// preferable
$geo = $possibleResult->geometry->location;
break;
}
if(property_exists($possibleResult, 'partial_match')){
// avoid
continue;
}
if(property_exists($possibleResult, 'types')){
if(in_array('postal_code_prefix', $possibleResult->types)){
// avoid
continue;
}
}
// not avoided (or broken) so far? use it!!
$geo = $possibleResult->geometry->location;
}
return $geo;
}
function changeIp(){
/*
* This will return context options for file_get_contents
* the following IP list will basically be bound by the router
* with a netmap rule, NATing to different external IP's
* thus, allowing to increase google's limit of 2500 requests
* per IP per day.
*
* IP List
*
* 10.100.101.10
* 192.168.3.210
* 192.168.3.211
* 192.168.3.212
* 192.168.3.213
* 192.168.3.214
*/
$lastIp = trim(file_get_contents('/ramdrive/googleip.txt')); // get last
$newIp = $lastIp; // in case for some reason it remains unchanged
switch($lastIp){
case '10.100.101.10':
$newIp = '192.168.3.210';
break;
case '192.168.3.210':
$newIp = '192.168.3.211';
break;
case '192.168.3.211':
$newIp = '192.168.3.212';
break;
case '192.168.3.212':
$newIp = '192.168.3.213';
break;
case '192.168.3.213':
$newIp = '192.168.3.214';
break;
case '192.168.3.214':
$newIp = '10.100.101.10';
break;
}
file_put_contents('/ramdrive/googleip.txt',$newIp);
$opts = array(
'socket' => array(
'bindto' => "$newIp:0",
),
);
return $opts;
}
?>