-
Notifications
You must be signed in to change notification settings - Fork 2
/
class.revgeocode.php
2361 lines (1997 loc) · 95.5 KB
/
class.revgeocode.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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* GeoRev class
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* For questions, help, comments, discussion, etc.,
*
* @link http://byte-consult.be/
* @version 1.0.0
* @copyright Copyright: 2011 Glenn Plas
* @author Glenn Plas <glenn AT byte-consult.be>
* @access public
*/
require_once('utf8_helper.php');
/*
* Uses the following web based json capable geocoding engines links, supports google V3 and V2 with premier id keys' (gme-[your_key])
* http://maps.google.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway&client=gme-yourclientid&sensor=true&signature=YOUR_URL_SIGNATURE
* http://where.yahooapis.com/geocode?q=%1$s,+%2$s&gflags=R&appid=[yourappidhere]
* http://dev.virtualearth.net/REST/v1/Locations/50.43434,4.5232323?o=json&key=[key_here]
* http://open.mapquestapi.com/nominatim/v1/reverse?format=json&json_callback=renderExampleThreeResults&lat=51.521435&lon=-0.162714
* http://api.geonames.org/findNearbyPlaceNameJSON?lat=%s&lng=%s&username=%s&style=full
* http://geocoding.cloudmade.com/<an_api_key>/geocoding/v2/find.js?object_type=address&around=51.0433583233,4.49876833333&distance=closest
*/
Class GeoRev {
// Defaults, base stuff we can't do without
private $settings= array(
'debug' => 0,
'verbose' => 0,
'use_yahoo' => 0,
'use_bing' => 0,
'use_geonames' => 0,
'use_nominatim' => 0,
'use_google' => 0,
'use_google_v3' => 0,
'use_yandex' => 0,
'use_cloudmade' => 0,
'sleep_bing' => '5000',
'sleep_yahoo' => '5000',
'sleep_google' => '5000',
'sleep_geonames' => '5000',
'sleep_nominatim' => '5000',
// Note yandex doesn't work too wel atm ...
'sleep_yandex' => '5000',
'sleep_cloudmade' => '5000',
'curl_connecttimeout' => '2',
'curl_connecttimeout_ms' => '2000',
'mc_compress' => 1,
'mc_compress' => 1,
'user_agent_string' => 'php-lib ( https://github.com/gplv2/PHP-GeoRev-Class )',
'mc_expire' => 500
);
// This kinda maps what belongs to who
private $service_variable_map = array (
'bing' => array('timer_name' => 'bi_timer','sleep_setting' => 'sleep_bing', 'page'=> 'bing_page'),
'yahoo' => array('timer_name' => 'ya_timer','sleep_setting' => 'sleep_yahoo', 'page'=> 'yahoo_page'),
'google' => array('timer_name' => 'go_timer','sleep_setting' => 'sleep_google', 'page'=> 'google_page'),
'geonames' => array('timer_name' => 'ge_timer','sleep_setting' => 'sleep_geonames', 'page'=> 'geonames_page'),
'nominatim' => array('timer_name' => 'no_timer','sleep_setting' => 'sleep_nominatim', 'page'=> 'nominatim_page'),
// 'yandex' => array('timer_name' => 'gp_timer','sleep_setting' => 'sleep_yandex', 'page'=> 'yandex_page'),
'cloudmade' => array('timer_name' => 'cm_timer','sleep_setting' => 'sleep_cloudmade', 'page'=> 'cloudmade_page')
);
// Runtime helper vars
private $eol;
private $trans;
private $MC;
// private $o_enc="utf8"; # output_encode "utf8" or "latin1" # not used
// The most important ones
private $lat;
private $lon;
// Runtime stats
private $counters=array();
// Raw return cache of the page, its' ok these are public for the example, they should be private really
public $google_page;
public $yahoo_page;
public $bing_page;
public $geonames_page;
public $nominatim_page;
public $yandex_page;
public $cloudmade_page;
// My own public postal code service AS IS for the time being (Belgium only, I'm redesigning this to fit postgis geometry types
private $gazzy_page;
// Dev aid
public $debug;
public $verbose;
// public $cli;
// Urls per service
private $urls=array();
private $hints=array();
public function __construct($conf_settings) {
/*
if (php_sapi_name()!='cli') {
// We are probably not running from a webserver so we need to take care of the STDOUT STDERR debug things
$this->cli=0;
}
*/
/* My own test if we are called from the CLI , could use PHP_EOL instead I know */
if (defined('STDIN')) {
$this->eol="\n";
} else {
$this->eol="<BR/>";
}
if (!isset($conf_settings)) {
echo __METHOD__ . ": config error\n";
exit(0);
}
/* Prepare this one already */
$this->trans= new Latin1UTF8();
/* check for curl */
if (!function_exists('curl_init')) {
throw new Exception(sprintf("cURL has to be installed in order to get %s class to work.",__CLASS__));
}
/* Determine the state of the engines from the settings */
$auto_settings['can_use_google_v3'] = (!empty($conf_settings['google_premierid']) and !empty($conf_settings['use_google']) ) ? 1 : 0;
$auto_settings['can_use_google'] = !empty($conf_settings['use_google']) ? 1 : 0;
$auto_settings['can_use_yahoo'] = !empty($conf_settings['key_yahoo']) ? 1 : 0;
$auto_settings['can_use_bing'] = !empty($conf_settings['key_bing']) ? 1 : 0;
$auto_settings['can_use_geonames'] = !empty($conf_settings['key_geonames']) ? 1 : 0;
$auto_settings['can_use_nominatim'] = !empty($conf_settings['key_nominatim']) ? 1 : 0;
$auto_settings['can_use_yandex'] = !empty($conf_settings['key_yandex']) ? 1 : 0;
$auto_settings['can_use_cloudmade'] = !empty($conf_settings['key_cloudmade']) ? 1 : 0;
// Merge the class defaults with the settings
$this->settings = array_merge($this->settings, $conf_settings);
// Merge the autosettings with the settings
$this->settings = array_merge($this->settings, $auto_settings);
// $this->debug(__METHOD__, "simple" , 1, "",1);
// Set the correct debug values
$this->verbose=$this->settings['verbose'];
$this->debug=$this->settings['debug'];
// $this->debug(__METHOD__, "simple" , 1, sprintf("%d / %d",$this->debug,$this->verbose));
$this->debug(__METHOD__, "simple" , 1, sprintf("Status geocode engines"));
$this->debug(__METHOD__, "simple" , 1, $auto_settings,1);
$this->debug(__METHOD__, "simple" , 1, $this->settings,1);
$this->count_engines_available();
/*
// Check for engine availability right off the bat before going further, we can't do anything meaningfull without atleast 1
if (!$this->count_engines_available()) {
$this->debug(__METHOD__, "simple", 0, sprintf("No geocoding engine available, check config file for key / parameter settings"));
exit;
}
*/
/* Analyse config settings for url servers */
if (isset($conf_settings['server_urls']) and is_array($conf_settings['server_urls'])) {
// We have some settings, lets try to see if they work.
$url_servers = array();
foreach($conf_settings['server_urls'] as $url_server){
if (strcmp($url_server['type'], "nominatim")== 0) {
$this->debug(__METHOD__, "simple" , 2, sprintf("Adding '%s' nominatim url",$url_server['url']));
$url_server['state']=1;
$url_server['last_error']='';
$url_servers[] = $url_server;
} else {
$this->debug(__METHOD__, "simple" , 2, sprintf("Ignoring '%s' entry",$url_server['url']));
}
}
$this->urls=$url_servers;
}
/* Analyse config settings for memcached servers */
if (isset($conf_settings['cacheservers']) and is_array($conf_settings['cacheservers'])) {
// We have some settings, lets try to see if they work.
$memc_servers = array();
foreach($conf_settings['cacheservers'] as $cache_server){
if (strcmp($cache_server['type'], "memcached")==0) {
$this->debug(__METHOD__, "simple" , 2, sprintf("Adding %s memcache host",$cache_server['host']));
$memc_servers[] = $cache_server;
} else {
$this->debug(__METHOD__, "simple" , 2, sprintf("Ignoring %s cache host",$cache_server['host']));
}
}
if(count($memc_servers)>0) {
include_once('class.memcached.php');
$this->MC = new MCache($memc_servers);
$memcached_counters= array ('memc_get','memc_set','memc_hit','memc_miss');
$this->register_counter($memcached_counters,0);
// var_dump($this->MC);
if (!$this->MC->getServerCount()) {
$this->debug(__METHOD__, "simple" , 2, sprintf("No Memcache servers candidates defined"));
unset($this->MC);
} else {
$this->debug(__METHOD__, "simple" , 2, sprintf("%d Memcache servers defined ...",$this->MC->getServerCount()));
if (!empty($conf_settings['test_memcache'])) {
$this->debug(__METHOD__, "simple" , 2, sprintf("Testing Memcache ..."));
// Create test data
$test_val = sprintf("%s%s", md5(json_encode($conf_settings['cacheservers'])),time());
$test_key = "GEOREVTEST";
// Set and get a known value
$this->MC->set($test_key, $test_val, $compress=1, $expire=60);
$result = $this->MC->get($test_key);
if (strcmp($result, $test_val) == 0) {
$this->debug(__METHOD__, "simple" , 2, sprintf("Memcache seems to be working."));
} else {
// Memcached doesn't seem to work
$this->debug(__METHOD__, "simple" , 2, sprintf("Disabling Memcache since its not working."));
unset($this->MC);
}
}
}
}
}
// Get some counters registered and initialised
$init_counters = array ('hit_google',
'hit_yahoo',
'hit_geonames',
'hit_bing',
'hit_nominatim',
'hit_yandex',
'hit_gazzy',
'hit_cloudmade',
'fail_yahoo',
'fail_bing',
'fail_geonames',
'fail_google',
'fail_nominatim',
'fail_yandex',
'fail_cloudmade',
'fail_gazzy',
'ok_bing',
'ok_google',
'ok_yahoo',
'ok_geonames',
'ok_yandex',
'ok_nominatim',
'ok_cloudmade',
'ok_gazzy'
);
$this->register_counter($init_counters,0);
// Register the timers for all services (used to be a bit more hardcoded)
foreach ($this->service_variable_map as $engine => $service ) {
// timers are = array ('go_timer','ya_timer','bi_timer','ge_timer','no_timer','gp_timer','cm_timer');
if ($this->get_engines_available($engine)) {
$sleep_service = $service['sleep_setting'];
$minimum_sleep = $sleep_service*1000;
$timer_name = $service['timer_name'];
$this->debug( __METHOD__, "simple", 3, sprintf("Registering timer -> %s",$engine));
// We have to substract the delay when setting the start value
$this->register_counter($timer_name, (microtime(true) - $minimum_sleep));
}
}
// $this->debug(__METHOD__, "simple", 1, $this->counters);
}
private function register_counter($name="dummy",$default_var=0) {
$this->debug(__METHOD__, "call",5);
if (!isset($default_var)) {
$default_var=0;
}
if (!is_array($name)) {
$name=(array)$name;
}
foreach($name as $i => $counter_name) {
if (strlen($counter_name) > 0) {
$this->debug(__METHOD__, "simple", 4, sprintf("Initializing %s = %s",$counter_name,$default_var));
$this->counters[$counter_name]=$default_var;
}
}
}
public function get_counters() {
return $this->counters;
}
public function set_coord($lat,$lon) {
$this->debug(__METHOD__, "call",5);
$retval=1;
$status="Coordinates ok";
if (!isset($lat) OR !isset($lon)) {
$retval= -1;
$status="Need atleast lat/lon pair to do meaningful things";
}
if(($lat >= -90) AND ($lat <= 90 )) {
$this->lat=$lat;
} else {
$retval= 0;
$status="Latitude outside boundaries";
}
if(($lon >= -180) AND ($lon <= 180 )) {
$this->lon=$lon;
} else {
$retval= 0;
$status="Longitude outside boundaries";
}
$this->debug(__METHOD__, "call",sprintf("code = %d ( %s )",$retval,$status));
//$ret = array ('code' => $retval, 'status' => $status);
$this->debug(__METHOD__, "hangup",5);
return $retval;
}
// GLENN
private function revgeocode_yandex() {
$this->debug(__METHOD__, "call",5);
$tag='yandex';
// The memcache key , trying to keep it somewhat short
$mckey=sprintf("RG%s_%s|%s",strtoupper(substr($tag,0,4)), $this->float_to_small($this->lat), $this->float_to_small($this->lon));
// Fetch from cache if we can
if(isset($this->MC)){
$this->debug(__METHOD__, "simple",3,"Checking cache...");
$this->debug(__METHOD__, "simple",3,sprintf("Memcache key %s",$mckey));
$cached = $this->MC->get($mckey);
$this->counters['memc_get']++;
$page = json_decode($cached['contents'],true);
if (is_array($page)) {
$this->debug(__METHOD__, "simple",3,"Cached data found.");
// Seems we have some valid cache data for this service
$this->yandex_page=$cached;
$this->counters['memc_hit']++;
return 1;
} else {
$this->counters['memc_miss']++;
}
}
if($this->counters['fail_yandex']>$this->settings['yandex_max_fail'] OR !$this->settings['can_use_yandex']) {
$this->settings['use_yandex']=0;
return "";
}
if (!empty($this->settings['dryrun'])) {
$this->debug(__METHOD__, "simple",5,"Dryrun active, not doing encode now.");
return "";
}
if(empty($this->settings['use_yandex'])) {
$this->debug(__METHOD__, "simple",0,sprintf("Engine %s is disabled",$tag));
return "";
}
// Delay as specified
$this->throttle_service($tag);
$this->debug( __METHOD__, "simple", 2, sprintf("Encoding with yandex"));
/* ll=
:geocode => query,
:format => "json",
:plng => "#{Geocoder::Configuration.language}", # supports ru, uk, be
:key => Geocoder::Configuration.api_key
*/
$baseurl = "http://geocode-maps.yandex.ru/1.x/?geocode=%s&format=json&plng=uk&key=%s";
$url = sprintf($baseurl, $this->lat, $this->lon);
// $url = sprintf($baseurl, $this->lat, $this->lon, $this->settings['key_yandex']);
$this->debug( __METHOD__, "simple", 2, sprintf("Geocoding url '%s'", $url));
$this->counters['hit_yandex']++;
/* Do it with curl */
$ch = curl_init($url);
// set user agent
curl_setopt($ch, CURLOPT_USERAGENT, $this->settings['user_agent_string']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER , true);
$server_output = curl_exec($ch);
$curlinfo = curl_getinfo($ch);
curl_close($ch);
$this->debug( __METHOD__, "simple", 5, $curlinfo,1);
if ($curlinfo['http_code']!=200) {
$this->counters['fail_yandex']++;
return "";
}
// $this->debug( __METHOD__, "simple", 5, $server_output);
// "text/javascript; charset=utf-8"
$contents = $server_output;
if (preg_match("/utf-8/", strtolower($curlinfo['content_type']), $matches)) {
if (!empty($matches[0])) {
$this->debug( __METHOD__, "simple", 2, "Encoding utf-8");
$contents = utf8_encode($server_output);
}
}
$this->yandex_page = array ('curlinfo' => $curlinfo, 'contents' => $contents);
// Store in the cache if we can
if(isset($this->MC)){
$this->debug(__METHOD__, "simple",2,"Saving in cache");
$this->MC->set($mckey, $this->yandex_page, $this->settings['mc_compress'], $this->settings['mc_expire']);
$this->counters['memc_set']++;
}
$this->counters['gp_timer']=microtime(true);
return 1;
}
public function getpostcodeinfo($postcode){
$tag='gzzy';
// The memcache key
$mckey=sprintf("ZZ%s_%s|%s",strtoupper(substr($tag,0,4)), $this->float_to_small($this->lat), $this->float_to_small($this->lon));
$this->debug(__METHOD__, "call",5);
//$url="http://gazzy.byte-consult.be/postcode.php?format=json&postcode=7100&accept-language=nl,en;q=0.8,fr;q=0.5";
$url=sprintf("http://gazzy.byte-consult.be/postcode.php?format=json&postcode=%d&accept-language=nl,en;q=0.8,fr;q=0.5",$postcode);
$this->debug( __METHOD__, "simple", 2, sprintf("Requesting postcode information from url '%s'", $url));
$this->counters['hit_gazzy']++;
/* Do it with curl */
$ch = curl_init($url);
// set user agent
curl_setopt($ch, CURLOPT_USERAGENT, $this->settings['user_agent_string']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER , true);
$server_output = curl_exec($ch);
$curlinfo = curl_getinfo($ch);
curl_close($ch);
$this->debug( __METHOD__, "simple", 5, $curlinfo,1);
if ($curlinfo['http_code']!=200) {
$this->counters['fail_gazzy']++;
return "";
}
$this->counters['ok_gazzy']++;
// $this->debug( __METHOD__, "simple", 5, $server_output);
// "text/javascript; charset=utf-8"
$contents = $server_output;
if (preg_match("/utf-8/", strtolower($curlinfo['content_type']), $matches)) {
if (!empty($matches[0])) {
$this->debug( __METHOD__, "simple", 2, "Encoding utf-8");
$contents = utf8_encode($server_output);
}
}
$this->gazzy_page = array ('curlinfo' => $curlinfo, 'contents' => $contents);
// Store in the cache if we can
if(isset($this->MC)){
$this->debug(__METHOD__, "simple",2,"Saving in cache");
$this->MC->set($mckey, $this->gazzy_page, $this->settings['mc_compress'], $this->settings['mc_expire']);
$this->counters['memc_set']++;
}
return($contents);
}
private function revgeocode_google () {
$this->debug(__METHOD__, "call",5);
$tag='google';
// The memcache key
$mckey=sprintf("RG%s_%s|%s",strtoupper(substr($tag,0,4)), $this->float_to_small($this->lat), $this->float_to_small($this->lon));
// Fetch from cache if we can
if(isset($this->MC)){
$this->debug(__METHOD__, "simple",3,"Checking cache...");
$this->debug(__METHOD__, "simple",3,sprintf("Memcache key %s",$mckey));
$cached = $this->MC->get($mckey);
$this->counters['memc_get']++;
$page = json_decode($cached['contents'],true);
if (is_array($page)) {
$this->debug(__METHOD__, "simple",3,"Cached data found.");
// Seems we have some valid cache data for this service
$this->google_page=$cached;
$this->counters['memc_hit']++;
return 1;
} else {
$this->counters['memc_miss']++;
}
}
if($this->counters['fail_google']>$this->settings['google_max_fail'] OR (!$this->settings['can_use_google'] AND !$this->settings['can_use_google_v3'])) {
$this->settings['use_google']=0;
return "";
}
if (!empty($this->settings['dryrun'])) {
$this->debug(__METHOD__, "simple",5,"Dryrun active, not doing encode now.");
return "";
}
if(empty($this->settings['use_google'])) {
$this->debug(__METHOD__, "simple",0,sprintf("Engine %s is disabled",$tag));
return "";
}
// Delay as specified
$this->throttle_service($tag);
$this->debug( __METHOD__, "simple", 2, sprintf("Encoding with Google maps"));
$baseurl = "http://maps.google.com";
if(!empty($this->settings['use_google_v3'])) {
// We need to url sign this request, like this example:
// http://maps.google.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway&client=gme-yourclientid&sensor=false&signature=YOUR_URL_SIGNATURE
$signurl = sprintf("/maps/geo?q=%s,%s&client=%s&output=json&sensor=false", $this->lat, $this->lon, $this->settings['google_premierid']);
$signature=$this->sign_url($baseurl . $signurl);
$url=$baseurl . $signurl . "&signature=" . $signature;
$this->debug(__METHOD__, "simple" , 2, sprintf("Google v3"));
} else {
$url = sprintf("%s/maps/geo?q=%s,%s&output=json&key=%s&sensor=false", $baseurl, $this->lat, $this->lon, $this->settings['key_google']);
$this->debug(__METHOD__, "simple" , 5, sprintf("Google v2"));
}
$this->debug( __METHOD__, "simple", 2, sprintf("Geocoding url '%s'", $url));
$this->counters['hit_google']++;
/* Do it with curl */
$ch = curl_init($url);
// set user agent
curl_setopt($ch, CURLOPT_USERAGENT, $this->settings['user_agent_string']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER , true);
$server_output = curl_exec($ch);
$curlinfo = curl_getinfo($ch);
curl_close($ch);
$this->debug( __METHOD__, "simple", 5, $curlinfo,1);
if ($curlinfo['http_code']!=200) {
$this->counters['fail_google']++;
return "";
}
// $this->debug( __METHOD__, "simple", 5, $server_output);
// "text/javascript; charset=utf-8"
$contents = $server_output;
if (preg_match("/utf-8/", strtolower($curlinfo['content_type']), $matches)) {
if (!empty($matches[0])) {
$this->debug( __METHOD__, "simple", 2, "Encoding utf-8");
$contents = utf8_encode($server_output);
}
}
$this->google_page = array ('curlinfo' => $curlinfo, 'contents' => $contents);
// Store in the cache if we can
if(isset($this->MC)){
$this->debug(__METHOD__, "simple",2,"Saving in cache");
$this->MC->set($mckey, $this->google_page, $this->settings['mc_compress'], $this->settings['mc_expire']);
$this->counters['memc_set']++;
}
$this->counters['go_timer']=microtime(true);
return 1;
}
private function revgeocode_bing () {
$this->debug(__METHOD__, "call",5);
$tag='bing';
// The memcache key
$mckey=sprintf("RG%s_%s|%s",strtoupper(substr($tag,0,4)), $this->float_to_small($this->lat), $this->float_to_small($this->lon));
// Fetch from cache if we can
if(isset($this->MC)){
$this->debug(__METHOD__, "simple",3,"Checking cache...");
$this->debug(__METHOD__, "simple",3,sprintf("Memcache key %s",$mckey));
$cached = $this->MC->get($mckey);
$this->counters['memc_get']++;
$page = json_decode($cached['contents'],true);
if (is_array($page)) {
$this->debug(__METHOD__, "simple",3,"Cached data found.");
// Seems we have some valid cache data for this service
$this->bing_page=$cached;
$this->counters['memc_hit']++;
return 1;
} else {
$this->counters['memc_miss']++;
}
}
if($this->counters['fail_bing']>$this->settings['bing_max_fail'] OR !$this->settings['can_use_bing']) {
$this->settings['use_bing']=0;
return "";
}
if (!empty($this->settings['dryrun'])) {
$this->debug(__METHOD__, "simple",5,"Dryrun active, not doing encode now.");
return "";
}
/* Bing license programs
Enterprise (formerly Commercial): This option is available to licensed commercial accounts only.
Developer: Application does not exceed 125,000 sessions or 500,000 transactions within a 12 month period.
Evaluation/Trial: Application is used for public or internal use during a 90 day evaluation period.
Broadcast: Application is used for public or internal-facing television, movies or similar.
Mobile: Application is used for publically available and installable applications on mobile handsets.
Education: Application is used for public use by schools, including faculty, staff and students.
Not-for-profit: Application is used by a tax-exempt organization.
*/
if(empty($this->settings['use_bing'])) {
$this->debug(__METHOD__, "simple",0,sprintf("Engine %s is disabled",$tag));
return "";
}
$this->throttle_service($tag);
$this->debug( __METHOD__, "simple", 2, sprintf("Encoding with Bing"));
$baseurl = "http://dev.virtualearth.net/REST/v1/Locations/%s,%s?o=json&key=%s";
$url = sprintf($baseurl,$this->lat,$this->lon,$this->settings['key_bing']);
$this->debug( __METHOD__, "simple", 2, sprintf("Geocoding url '%s'", $url));
$this->counters['hit_bing']++;
/* Do it with curl */
$ch = curl_init($url);
// set user agent
curl_setopt($ch, CURLOPT_USERAGENT, $this->settings['user_agent_string']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER , true);
$server_output = curl_exec($ch);
$curlinfo = curl_getinfo($ch);
curl_close($ch);
$this->debug( __METHOD__, "simple", 5, $curlinfo,1);
/*
[] content_type = application/json
[] http_code = 400
[] header_size = 335
[] request_size = 154
[] filetime = -1
[] ssl_verify_result = 0
[] redirect_count = 0
[] total_time = 0.493183
[] namelookup_time = 0.479224
[] connect_time = 0.480385
[] pretransfer_time = 0.480393
[] size_upload = 0
[] size_download = 589
[] speed_download = 1194
[] speed_upload = 0
[] download_content_length = -1
[] upload_content_length = 0
[] starttransfer_time = 0.490021
[] redirect_time = 0
*/
if ($curlinfo['http_code']!=200) {
$this->counters['fail_bing']++;
return "";
}
$this->debug( __METHOD__, "simple", 5, $server_output);
// "text/javascript; charset=utf-8"
$contents = $server_output;
if (preg_match("/utf-8/", strtolower($curlinfo['content_type']), $matches)) {
if (!empty($matches[0])) {
$this->debug( __METHOD__, "simple", 3, "Encoding utf-8");
$contents = utf8_encode($server_output);
}
}
$this->bing_page = array ('curlinfo' => $curlinfo, 'contents' => $contents);
// Store in the cache if we can
if(isset($this->MC)){
$this->debug(__METHOD__, "simple",2,"Saving in cache");
$this->MC->set($mckey, $this->bing_page, $this->settings['mc_compress'], $this->settings['mc_expire']);
$this->counters['memc_set']++;
}
$this->counters['bi_timer']=microtime(true);
return 1;
}
private function revgeocode_yahoo () {
$this->debug(__METHOD__, "call",5);
$tag='yahoo';
// The memcache key
$mckey=sprintf("RG%s_%s|%s",strtoupper(substr($tag,0,4)), $this->float_to_small($this->lat), $this->float_to_small($this->lon));
// Fetch from cache if we can
if(isset($this->MC)){
$this->debug(__METHOD__, "simple",3,"Checking cache...");
$this->debug(__METHOD__, "simple",3,sprintf("Memcache key %s",$mckey));
$cached = $this->MC->get($mckey);
$this->counters['memc_get']++;
$page = json_decode($cached['contents'],true);
if (is_array($page)) {
$this->debug(__METHOD__, "simple",3,"Cached data found.");
// Seems we have some valid cache data for this service
$this->yahoo_page=$cached;
$this->counters['memc_hit']++;
return 1;
} else {
$this->counters['memc_miss']++;
}
}
if($this->counters['fail_yahoo']>$this->settings['yahoo_max_fail'] OR !$this->settings['can_use_yahoo']) {
$this->settings['use_yahoo']=0;
return "";
}
if (!empty($this->settings['dryrun'])) {
$this->debug(__METHOD__, "simple",5,"Dryrun active, not doing encode now.");
return "";
}
if(empty($this->settings['use_yahoo'])) {
$this->debug(__METHOD__, "simple",0,sprintf("Engine %s is disabled",$tag));
return "";
}
$this->throttle_service($tag);
$this->debug( __METHOD__, "simple", 2, sprintf("Encoding with Yahoo placefinder"));
/* http://where.yahooapis.com/geocode?q=%1$s,+%2$s&gflags=R&appid=[yourappidhere] */
$baseurl = "http://where.yahooapis.com/geocode?q=";
$url = $baseurl . $this->lat .",".$this->lon."&gflags=R&flags=J&appid=" . $this->settings['key_yahoo'] . "&locale=nl";
$this->debug( __METHOD__, "simple", 2, sprintf("Geocoding url '%s'", $url));
$this->counters['hit_yahoo']++;
/* Do it with curl */
$ch = curl_init($url);
// set user agent
curl_setopt($ch, CURLOPT_USERAGENT, $this->settings['user_agent_string']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER , true);
$server_output = curl_exec($ch);
$curlinfo = curl_getinfo($ch);
curl_close($ch);
$this->debug( __METHOD__, "simple", 5, $curlinfo,1);
if ($curlinfo['http_code']!=200) {
$this->counters['fail_yahoo']++;
return "";
}
// $this->debug( __METHOD__, "simple", 5, $server_output);
// "text/javascript; charset=utf-8"
$contents = $server_output;
if (preg_match("/utf-8/", strtolower($curlinfo['content_type']), $matches)) {
if (!empty($matches[0])) {
$this->debug( __METHOD__, "simple", 3, "Encoding utf-8");
$contents = utf8_encode($server_output);
}
}
$this->yahoo_page = array ('curlinfo' => $curlinfo, 'contents' => $contents);
if(isset($this->MC)){
$this->debug(__METHOD__, "simple",2,"Saving in cache");
$this->MC->set($mckey, $this->yahoo_page, $this->settings['mc_compress'], $this->settings['mc_expire']);
$this->counters['memc_set']++;
}
$this->counters['ya_timer']=microtime(true);
return 1;
}
private function revgeocode_geonames () {
$this->debug(__METHOD__, "call",5);
$tag='geonames';
// The memcache key
$mckey=sprintf("RG%s_%s|%s",strtoupper(substr($tag,0,4)), $this->float_to_small($this->lat), $this->float_to_small($this->lon));
// Fetch from cache if we can
if(isset($this->MC)){
$this->debug(__METHOD__, "simple",3,"Checking cache...");
$this->debug(__METHOD__, "simple",3,sprintf("Memcache key %s",$mckey));
$cached = $this->MC->get($mckey);
$this->counters['memc_get']++;
$page = json_decode($cached['contents'],true);
if (is_array($page)) {
$this->debug(__METHOD__, "simple",3,"Cached data found.");
// Seems we have some valid cache data for this service
$this->geonames_page=$cached;
$this->counters['memc_hit']++;
return 1;
} else {
$this->counters['memc_miss']++;
}
}
if($this->counters['fail_geonames']>$this->settings['geonames_max_fail'] OR !$this->settings['can_use_geonames']) {
$this->settings['use_geonames']=0;
return "";
}
if (!empty($this->settings['dryrun'])) {
$this->debug(__METHOD__, "simple",5,"Dryrun active, not doing encode now.");
return "";
}
if(empty($this->settings['use_geonames'])) {
$this->debug(__METHOD__, "simple",0,sprintf("Engine %s is disabled",$tag));
return "";
}
$this->throttle_service($tag);
$this->debug( __METHOD__, "simple", 2, sprintf("Encoding with GeoNames JSON API"));
#$baseurl = "http://api.geonames.org/findNearbyPlaceNameJSON?lat=%s&lng=%s&username=%s&style=full";
$baseurl = "http://api.geonames.org/findNearbyStreetsOSMJSON?lat=%s&lng=%s&username=%s&style=full";
# http://api.geonames.org/findNearbyPlaceNameJSON?lat=50.974383&lng=4.467943&username=demo
# 51.158705&lon=4.99776166667
# view-source:http://api.geonames.org/findNearbyStreetsOSMJSON?lat=51.158705&lng=4.99776166667&username=demo%20%2051.158705&lon=4.99776166667
# http://api.geonames.org/findNearbyStreetsOSMJSON?lat=51.158705&lng=4.99776166667&username=demo%20%2051.158705&lon=4.99776166667
$url = sprintf($baseurl,$this->lat,$this->lon,$this->settings['key_geonames']);
$this->debug( __METHOD__, "simple", 2, sprintf("Geocoding url '%s'", $url));
$this->counters['hit_geonames']++;
/* Do it with curl */
$ch = curl_init($url);
// set user agent
curl_setopt($ch, CURLOPT_USERAGENT, $this->settings['user_agent_string']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER , true);
$server_output = curl_exec($ch);
$curlinfo = curl_getinfo($ch);
curl_close($ch);
$this->debug( __METHOD__, "simple", 5, $curlinfo,1);
if ($curlinfo['http_code']!=200) {
$this->counters['fail_geonames']++;
return "";
}
// $this->debug( __METHOD__, "simple", 5, $server_output);
// "text/javascript; charset=utf-8"
$contents = $server_output;
if (preg_match("/utf-8/", strtolower($curlinfo['content_type']), $matches)) {
if (!empty($matches[0])) {
$this->debug( __METHOD__, "simple", 3, "Encoding utf-8");
$contents = utf8_encode($server_output);
}
}
$this->geonames_page = array ('curlinfo' => $curlinfo, 'contents' => $contents);
if(isset($this->MC)){
$this->debug(__METHOD__, "simple",2,"Saving in cache");
$this->MC->set($mckey, $this->geonames_page, $this->settings['mc_compress'], $this->settings['mc_expire']);
$this->counters['memc_set']++;
}
$this->counters['ge_timer']=microtime(true);
return 1;
}
private function revgeocode_cloudmade () {
$this->debug(__METHOD__, "call",5);
$tag='cloudmade';
// The memcache key
$mckey=sprintf("RG%s_%s|%s",strtoupper(substr($tag,0,4)), $this->float_to_small($this->lat), $this->float_to_small($this->lon));
// Fetch from cache if we can
if(isset($this->MC)){
$this->debug(__METHOD__, "simple",3,"Checking cache...");
$this->debug(__METHOD__, "simple",3,sprintf("Memcache key %s",$mckey));
$cached = $this->MC->get($mckey);
$this->counters['memc_get']++;
$page = json_decode($cached['contents'],true);
if (is_array($page)) {
$this->debug(__METHOD__, "simple",3,"Cached data found.");
// Seems we have some valid cache data for this service
$this->cloudmade_page=$cached;
$this->counters['memc_hit']++;
return 1;
} else {
$this->counters['memc_miss']++;
}
}
if($this->counters['fail_cloudmade']>$this->settings['cloudmade_max_fail'] OR !$this->settings['can_use_cloudmade']) {
$this->settings['use_cloudmade']=0;
return "";
}
if (!empty($this->settings['dryrun'])) {
$this->debug(__METHOD__, "simple",1,"Dryrun active, not doing encode now.");
return "";
}
if(empty($this->settings['use_cloudmade'])) {
$this->debug(__METHOD__, "simple",0,sprintf("Engine %s is disabled",$tag));
return "";
}
$this->throttle_service($tag);
$this->debug( __METHOD__, "simple", 2, sprintf("Encoding with Cloudmade"));
// http://geocoding.cloudmade.com/<an_api_key>/geocoding/v2/find.js?object_type=address&around=51.0433583233,4.49876833333&distance=closest
$baseurl = "http://geocoding.cloudmade.com/%s/geocoding/v2/find.js?object_type=address&around=%s,%s&distance=closest&return_location=true";
// http://geocoding.cloudmade.com/YOUR-API-KEY/geocoding/v2/find.html?around=[object,point]&object_type=OBJECT_TYPE
$url = sprintf($baseurl,$this->settings['key_cloudmade'], $this->lat,$this->lon);
$this->debug( __METHOD__, "simple", 2, sprintf("Geocoding url '%s'", $url));
$this->counters['hit_cloudmade']++;
/* Do it with curl */
$ch = curl_init($url);
// set user agent
curl_setopt($ch, CURLOPT_USERAGENT, $this->settings['user_agent_string']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER , true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('HTTP_ACCEPT_LANGUAGE: UTF-8')); # We need this for the code I found in gazetteer to not throw errors
$server_output = curl_exec($ch);
$curlinfo = curl_getinfo($ch);
curl_close($ch);
$this->debug( __METHOD__, "simple", 5, $curlinfo,1);
if ($curlinfo['http_code']!=200) {
$this->counters['fail_cloudmade']++;
return "";
}
$this->debug( __METHOD__, "simple", 5, $server_output);
$contents = $server_output;
if (preg_match("/utf-8/", strtolower($curlinfo['content_type']), $matches)) {
if (!empty($matches[0])) {
$this->debug( __METHOD__, "simple", 3, "Encoding utf-8");
$contents = utf8_encode($server_output);
}
}
$this->cloudmade_page = array ('curlinfo' => $curlinfo, 'contents' => $contents);
if(isset($this->MC)){
$this->debug(__METHOD__, "simple",2,"Saving in cache");
$this->MC->set($mckey, $this->cloudmade_page, $this->settings['mc_compress'], $this->settings['mc_expire']);
$this->counters['memc_set']++;
}
$this->counters['cm_timer']=microtime(true);
return 1;
}
private function revgeocode_nominatim () {
// http://open.mapquestapi.com/nominatim/#reverse
$this->debug(__METHOD__, "call",5);
$tag='nominatim';
// The memcache key
$mckey=sprintf("RG%s_%s|%s",strtoupper(substr($tag,0,4)), $this->float_to_small($this->lat), $this->float_to_small($this->lon));
// Fetch from cache if we can
if(isset($this->MC)){
$this->debug(__METHOD__, "simple",3,"Checking cache...");
$this->debug(__METHOD__, "simple",3,sprintf("Memcache key %s",$mckey));
$cached = $this->MC->get($mckey);
$this->counters['memc_get']++;
$page = json_decode($cached['contents'],true);