#!/usr/bin/perl -w ############################################################################################### # Demonstration of how to make a web simple request to the GEOLocate web service in Perl # # Demonstration of how to consume the GEOLOcate Soap service within Perl # # Demos are using hard-coded inputs. # # Question & comments to Nelson Rios(nelson@museum.tulane.edu) ############################################################################################### use strict; use warnings; use LWP; # LWP (Version 6.02) from cpan: # http://search.cpan.org/~gaas/libwww-perl-6.02/ use SOAP::Lite; # SOAP:Lite version 0.712 from cpan: # http://search.cpan.org/~mkutter/SOAP-Lite-0.712/ my $doGEOLocatePOST = 0; my $doGEOLocateSOAP = 0; my $doTFP = 0; my $i = 0; if (@ARGV > 0) { foreach $i (0 .. $#ARGV) { if ($ARGV[$i] eq '-g') { $doGEOLocatePOST = 1; } if ($ARGV[$i] eq '-s') { $doGEOLocateSOAP = 1; } if ($ARGV[$i] eq '-t') { $doTFP = 1; } } } else { print "\nArguments required:\n" ."-g for GEOLocate. - Uses simple HTTP POST & dumps raw output from service.\n" ."-s for GEOLocate. - Uses HTTP SOAP request. Demonstrates how to call SOAP from Perl and format output from service.\n" ."\nNOTE: procedures are using hard coded parameters for demonstration purposes. Edit code to chage input\n"; } # georef2 POST vs SOAP. # see http://geo-locate.org/webservices/geolocatesvc/geolocatesvc.asmx if ($doGEOLocatePOST or $doGEOLocateSOAP) { # Create the user agent object my $uagent = LWP::UserAgent->new; $uagent->agent("GEOLocatePerlDemoClient/1.0 "); # Create the input data my $localitystring = '5 miles N of Bogalusa'; my $country = 'USA'; my $state = 'LA'; my $county = 'Washington'; my $findhwyx = 'true'; my $findwaterbody = 'true'; if ($doGEOLocatePOST) { # Create the request my $baseurl ='http://geo-locate.org/webservices/geolocatesvc/geolocatesvc.asmx/Georef2'; my $reqst = HTTP::Request->new(POST => $baseurl); $reqst->content_type('application/x-www-form-urlencoded'); $reqst->content('LocalityString='.$localitystring .'&Country='.$country .'&State='.$state .'&County='.$county .'&HwyX='.$findhwyx .'&FindWaterbody='.$findwaterbody); # Pass the request to the user agent and get a response back my $resp = $uagent->request($reqst); # Check the status of the response and display the results if successful if ($resp->is_success) { print $resp->content; # DO SOME XML PROCESSING HERE } else { print $resp->status_line, "\n"; } } if ($doGEOLocateSOAP) { my $soap = SOAP::Lite -> uri('http://geo-locate.org/webservices/') -> on_action( sub { return '"http://geo-locate.org/webservices/Georef2"' } ) -> proxy('http://geo-locate.org/webservices/geolocatesvc/geolocatesvc.asmx'); my $method = SOAP::Data->name('Georef2') ->attr({xmlns => 'http://geo-locate.org/webservices/'}); my @params = ( SOAP::Data->name(Country => $country), SOAP::Data->name(State => $state), SOAP::Data->name(County => $county), SOAP::Data->name(LocalityString => $localitystring), SOAP::Data->name(HwyX => $findhwyx), SOAP::Data->name(FindWaterbody => $findwaterbody)); # print $soap->call($method => @params)->result; my $result = $soap->call($method => @params); if ($result->fault) { print $result->faultstring; } else { #print $result->result; print 'Execution time(ms): ' . $result->valueof('//Georef2Response/Result/ExectutionTimems') . "\n"; print 'Num results: ' . $result->valueof('//Georef2Response/Result/NumResults') . "\n"; #print $result->valueof('//SnapPointToNearestFoundWaterBody2Response/WGS84Coordinate/Latitude'), "\n"; my @nodes = $result->valueof('//Georef2Response/Result/ResultSet'); foreach my $node (@nodes) { my $wgs = $node->{'WGS84Coordinate'}; print $wgs->{'Latitude'} . " : " . $wgs->{'Longitude'} . " : " . $node->{'ParsePattern'}, "\n"; } } } }