1# 2# Routino generic Search Perl script 3# 4# Part of the Routino routing software. 5# 6# This file Copyright 2012-2014, 2016 Andrew M. Bishop 7# 8# This program is free software: you can redistribute it and/or modify 9# it under the terms of the GNU Affero General Public License as published by 10# the Free Software Foundation, either version 3 of the License, or 11# (at your option) any later version. 12# 13# This program is distributed in the hope that it will be useful, 14# but WITHOUT ANY WARRANTY; without even the implied warranty of 15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16# GNU Affero General Public License for more details. 17# 18# You should have received a copy of the GNU Affero General Public License 19# along with this program. If not, see <http://www.gnu.org/licenses/>. 20# 21 22use strict; 23 24# Use the directory paths script 25require "./paths.pl"; 26 27# Use the perl encoding/decoding functions 28use Encode qw(decode encode); 29 30# Use the perl URI module 31use URI::Escape; 32 33# Use the perl LWP module 34use LWP::UserAgent; 35 36# Use the perl JSON module 37use JSON::PP; 38 39# Use the perl Time::HiRes module 40use Time::HiRes qw(gettimeofday tv_interval); 41 42my $t0 = [gettimeofday]; 43 44 45# 46# Run the search 47# 48 49sub RunSearch 50 { 51 my($search,$lonmin,$lonmax,$latmax,$latmin)=@_; 52 53 # Perform the search based on the type 54 55 my $message=""; 56 my @places=[]; 57 58 if($main::search_type eq "nominatim") 59 { 60 ($message,@places)=DoNominatimSearch($search,$lonmin,$lonmax,$latmax,$latmin); 61 } 62 else 63 { 64 $message="Unknown search type '$main::search_type'"; 65 } 66 67 my(undef,undef,$cuser,$csystem) = times; 68 my $time=sprintf "time: %.3f CPU / %.3f elapsed",$cuser+$csystem,tv_interval($t0); 69 70 # Return the results 71 72 return($time,$message,@places); 73 } 74 75 76# 77# Fetch the search URL from Nominatim 78# 79 80sub DoNominatimSearch 81 { 82 my($search,$lonmin,$lonmax,$latmax,$latmin)=@_; 83 84 $search = uri_escape($search); 85 86 my $url; 87 88 if($lonmin && $lonmax && $latmax && $latmin) 89 { 90 $url="$main::search_baseurl?format=json&viewbox=$lonmin,$latmax,$lonmax,$latmin&q=$search"; 91 } 92 else 93 { 94 $url="$main::search_baseurl?format=json&q=$search"; 95 } 96 97 my $ua=LWP::UserAgent->new; 98 99 my $res=$ua->get($url); 100 101 if(!$res->is_success) 102 { 103 return($res->status_line); 104 } 105 106 my $result=decode_json($res->content); 107 108 my @places=(); 109 110 foreach my $place (@$result) 111 { 112 my $lat=$place->{"lat"}; 113 my $lon=$place->{"lon"}; 114 my $name=encode('utf8',$place->{"display_name"}); 115 116 push(@places,"$lat $lon $name"); 117 } 118 119 return("",@places); 120 } 121 122 1231; 124