#!/usr/bin/perl use strict; use LWP::Simple; #environment specific my $target_dir = '/tmp/ms_tmp'; ################## #read input files to temp directory ################## my ($csv_url) = @ARGV; #csv_url #generate a random file handle concatenating a random number my $csv_filepath = "$target_dir/catalog_in_".int(rand(10000000)).".csv"; #print $csv_filepath."\n"; my $content = getstore($csv_url, $csv_filepath); die "Couldn't get $csv_url" unless defined $content; ################## #read input csv file to hash ################## my %HoH = (); my $rHoH = \%HoH; my %ObsTypes = (); my $rObsTypes = \%ObsTypes; open (CATALOG_IN,"$csv_filepath"); my $line; while ($line = ) { my ($version, $modifiedDate, $operator, $regionalAssociation, $localPlatformName, $longitude, $latitude, $z, $zPlaneOfReference, $westboundLongitude, $southboundLatitude, $eastboundLongitude, $northboundLatitude, $plannedStartDate, $plannedEndDate, $operatorURL, $platformURL, $dataURL, $observation, $observationStatus, $comments) = split(/,/, $line); #ignore lines which aren't version '_1.0' if (!($line =~ /^_1.0/)) { next; } #print "version:$version\n"; #using the below hash trick to get a list of unique ObsTypes $ObsTypes{ $observation } = 1; #'operator' and 'operatorURL' are missing in the sos_config.xml, which kills my kml functionality for breakout by operator # substituting regional args for now if (!($operator)) { $operator = $regionalAssociation; } if (!($operatorURL)) { $operatorURL = 'not available'; } #adding to list $HoH{ $operator }{ $localPlatformName }{ $observation }{ 'longitude' } = $longitude; $HoH{ $operator }{ $localPlatformName }{ $observation }{ 'latitude' } = $latitude; $HoH{ $operator }{ $localPlatformName }{ $observation }{ 'westboundLongitude' } = $westboundLongitude; $HoH{ $operator }{ $localPlatformName }{ $observation }{ 'southboundLatitude' } = $southboundLatitude; $HoH{ $operator }{ $localPlatformName }{ $observation }{ 'eastboundLongitude' } = $eastboundLongitude; $HoH{ $operator }{ $localPlatformName }{ $observation }{ 'northboundLatitude' } = $northboundLatitude; $HoH{ $operator }{ $localPlatformName }{ $observation }{ 'operatorURL' } = $operatorURL; $HoH{ $operator }{ $localPlatformName }{ $observation }{ 'platformURL' } = $platformURL; $HoH{ $operator }{ $localPlatformName }{ $observation }{ 'dataURL' } = $dataURL; $HoH{ $operator }{ $localPlatformName }{ $observation }{ 'observationStatus' } = $observationStatus; } close (CATALOG_IN); ################## #kml header info ################## my $kml_content = <<"END_OF_LIST"; IOOS Observation Catalog green = Operational
red = Offline
yellow = Planned
Development notes on this product can be found at ObsCatalog]]>
END_OF_LIST ################## #convert hash to kml #list by operator ################## $kml_content .= "List by operator1"; foreach my $operator ( sort keys %{$rHoH} ) { #print "operator:$operator\n"; foreach my $localPlatformName ( sort keys %{$rHoH->{$operator}} ) { #print "localPlatformName:$localPlatformName\n"; my $description = ''; my $operatorURL = ''; my $platformURL = ''; my $dataURL = ''; my $longitude = ''; my $latitude = ''; my $westboundLongitude = ''; my $southboundLatitude = ''; my $eastboundLongitude = ''; my $northboundLatitude = ''; my $styleUrl = ''; my $coverage_area = ''; foreach my $observation ( sort keys %{$rHoH->{$operator}{$localPlatformName}} ) { #print "observation:$observation\n"; #only need to do this initially once per platform if ($operatorURL eq '') { $operatorURL .= $HoH{$operator}{$localPlatformName}{$observation}{'operatorURL'}; $description .= "operatorURL]]>
"; $platformURL .= $HoH{$operator}{$localPlatformName}{$observation}{'platformURL'}; $description .= "platformURL]]>

"; } $dataURL .= $HoH{$operator}{$localPlatformName}{$observation}{'dataURL'}; $description .= "$observation]]>
"; $longitude = $HoH{$operator}{$localPlatformName}{$observation}{'longitude'}; $latitude = $HoH{$operator}{$localPlatformName}{$observation}{'latitude'}; $westboundLongitude = $HoH{$operator}{$localPlatformName}{$observation}{'westboundLongitude'}; $southboundLatitude = $HoH{$operator}{$localPlatformName}{$observation}{'southboundLatitude'}; $eastboundLongitude = $HoH{$operator}{$localPlatformName}{$observation}{'eastboundLongitude'}; $northboundLatitude = $HoH{$operator}{$localPlatformName}{$observation}{'northboundLatitude'}; $styleUrl = $HoH{$operator}{$localPlatformName}{$observation}{'observationStatus'}; if ($westboundLongitude ne '') { $coverage_area = <<"END_OF_FILE"; $operator\_$localPlatformName\_$observation\_coverage_area #$styleUrl $westboundLongitude,$southboundLatitude,0,$westboundLongitude,$northboundLatitude,0,$eastboundLongitude,$northboundLatitude,0,$eastboundLongitude,$southboundLatitude,0 END_OF_FILE } } $kml_content .= <<"END_OF_FILE"; $operator\_$localPlatformName $description #$styleUrl $longitude,$latitude,0 $coverage_area END_OF_FILE } #foreach $localPlatformName } #foreach $operator $kml_content .= "
"; ################## #convert hash to kml using control list of observations #list by observation ################## $kml_content .= "List by observation0"; foreach my $observationType ( sort keys %{$rObsTypes} ) { $kml_content .= "$observationType0"; foreach my $operator ( sort keys %{$rHoH} ) { #print "operator:$operator\n"; foreach my $localPlatformName ( sort keys %{$rHoH->{$operator}} ) { #print "localPlatformName:$localPlatformName\n"; my $description = ''; my $operatorURL = ''; my $platformURL = ''; my $dataURL = ''; my $longitude = ''; my $latitude = ''; my $westboundLongitude = ''; my $southboundLatitude = ''; my $eastboundLongitude = ''; my $northboundLatitude = ''; my $styleUrl = ''; my $coverage_area = ''; foreach my $observation ( sort keys %{$rHoH->{$operator}{$localPlatformName}} ) { #print "observation:$observation $observationType :: \n"; if ($observation eq $observationType) { #only need to do this initially once per platform if (!($operatorURL)) { $operatorURL .= $HoH{$operator}{$localPlatformName}{$observation}{'operatorURL'}; $description .= "operatorURL]]>
"; $platformURL .= $HoH{$operator}{$localPlatformName}{$observation}{'platformURL'}; $description .= "platformURL]]>

"; } $dataURL .= $HoH{$operator}{$localPlatformName}{$observation}{'dataURL'}; $description .= "$observation]]>
"; $longitude = $HoH{$operator}{$localPlatformName}{$observation}{'longitude'}; $latitude = $HoH{$operator}{$localPlatformName}{$observation}{'latitude'}; $westboundLongitude = $HoH{$operator}{$localPlatformName}{$observation}{'westboundLongitude'}; $southboundLatitude = $HoH{$operator}{$localPlatformName}{$observation}{'southboundLatitude'}; $eastboundLongitude = $HoH{$operator}{$localPlatformName}{$observation}{'eastboundLongitude'}; $northboundLatitude = $HoH{$operator}{$localPlatformName}{$observation}{'northboundLatitude'}; $styleUrl = $HoH{$operator}{$localPlatformName}{$observation}{'observationStatus'}; if ($westboundLongitude ne '') { $coverage_area = <<"END_OF_FILE"; $operator\_$localPlatformName\_$observation\_coverage_area #$styleUrl 0 $westboundLongitude,$southboundLatitude,0,$westboundLongitude,$northboundLatitude,0,$eastboundLongitude,$northboundLatitude,0,$eastboundLongitude,$southboundLatitude,0 END_OF_FILE } $kml_content .= <<"END_OF_FILE"; $operator\_$localPlatformName $description #$styleUrl 0 $longitude,$latitude,0 $coverage_area END_OF_FILE } #if $observationType } #foreach $observation } #foreach $localPlatformName } #foreach $operator $kml_content .= "
"; } #foreach $observationType $kml_content .= "
"; ################## #kml footer and close file ################## $kml_content .= <<"END_OF_FILE";
END_OF_FILE #kml output file #generate a random file handle concatenating a random number my $kml_filename = "catalog_out_".int(rand(10000000)).".kml"; my $kml_filepath = "$target_dir/$kml_filename"; open (FILE_KML,">$kml_filepath"); print FILE_KML $kml_content; close (FILE_KML); #give filehandle name back to calling php page print $kml_filename; exit 0;