Personal tools
You are here: Home Old Content - To be removed SOS Perl Cookbook Version 0.1 SOS Clients getObservation.pl

getObservation.pl

Simple script to parse the SOS GetCapbabilities response

getObservation.pl — text/x-perl, 1Kb

File contents

#!/usr/bin/perl
use strict; 
use XML::XPath; 
use LWP::Simple;

# pass in a SOS GetObservation URL
# you may need to put single quotes around the url to escape it from the shell.

error("GetObservation URL is required.\nSample Usage: $0 'http://www.gomoos.org/cgi-bin/sos/oostethys_sos?request=GetObservation&offering=A01&observedProperty=sea_water_salinity'\n")
	if (@ARGV != 1);

my $xml_url = shift;

my $xml = LWP::Simple::get($xml_url);

error("Could not fetch $xml_url.\n") if(!$xml);

my $xp = XML::XPath->new(xml => $xml);

# we reuse this.
my $node;

my $i = 0;

my $org = $xp->find("//om:CommonObservation/gml:name")->string_value;
my $desc = $xp->find("//om:CommonObservation/gml:description")->string_value;
$node = $xp->find("//om:CommonObservation/om:observedProperty")->get_node(1);
my $observedProperty =  $node->getAttribute('xlink:href');

print "$org $desc\n";
print "$observedProperty\n";

my $time = $xp->find("//om:eventTime/gml:TimeInstant/gml:timePosition")->string_value;

print "$time\n";

# List of data headings being returned
my @components = ();
foreach my $node ($xp->find("//swe:component")->get_nodelist){
	push @components, $node->getAttribute('name');
}
print "Component Count: " . @components . "\n";

$node = $xp->find("//om:resultDefinition/swe:DataDefinition/swe:encoding/swe:AsciiBlock")->get_node(1);
my $tokenSeparator =  $node->getAttribute('tokenSeparator');
my $tupleSeparator =  $node->getAttribute('tupleSeparator');

my @tuples = split( $tupleSeparator, $xp->getNodeText("//om:result") );
print "tuple count: " . @tuples . "\n"; 
foreach (@components){
	print $_ . ' | ';
}
print "\n";
foreach my $line (@tuples){
	my @vals = split($tokenSeparator, $line);
	foreach (@vals){
		# this removes any newlines. Some serializers may add newlines for pretty xml but really should not.
		s/\n//g;
		print "$_ | ";	
	}
	print "\n";
}

exit;

#######################################
sub error
{
	my $msg = shift;
	print $msg;
	exit 1;
}
Document Actions