use strict; use XML::LibXML; use LWP::Simple; =comment to create an xml element structure within each existing element like follows: wind_speed m_s-1 3.10 3 wind_gust m_s-1 4.44 3 =cut ################## #read input files to temp directory ################## my $target_dir = '/tmp/ms_tmp'; my $xml_url = 'http://destinsharks.com/kmz/poi/vos.kml'; #my $xml_url = 'http://carocoops.org/obskml/feeds/vos.kml'; #generate a random file handle concatenating a random number my $xml_filepath = "$target_dir/vos_in_".int(rand(10000000)).".xml"; #print $xml_filepath."\n"; my $content = getstore($xml_url, $xml_filepath); die "Couldn't get $xml_url" unless defined $content; #must reformat xmlns for bug with LibXML `cd $target_dir ; perl -pi -e 's/xmlns/xmlns:kml/g' $xml_filepath`; my $xp = XML::LibXML->new->parse_file($xml_filepath); #my $xp = XML::LibXML->new->parse_file('./vos_metadata.kml'); my ($metadata,$operatorURL,$platformDescription,$obsList,$obs,$observation,$value,$type,$uom,$elev); foreach my $node ( $xp->findnodes("//Placemark") ){ #print "debug\n"; $node->setAttribute('id', 'vos.none.ship'); $metadata = $xp->createElement('Metadata'); $node->appendChild($metadata); $obsList = $xp->createElement('obsList'); $metadata->appendChild($obsList); #$obs->setAttribute('xmlns', 'http://carocoops.org/obsrss/obs_kml_simple.xsd'); #$obs->setAttribute('xmlns:obsTypes', 'http://carocoops.org/obsrss/observation_types.xml'); #$obs->setAttribute('xmlns:uomTypes', 'http://carocoops.org/obsrss/uom_types.xml'); $operatorURL = $xp->createElement('operatorURL'); $operatorURL->appendText('http://www.vos.noaa.gov'); $obsList->appendChild($operatorURL); $platformDescription = $xp->createElement('platformDescription'); $platformDescription->appendText('SHIP'); $obsList->appendChild($platformDescription); #print "yes\n"; my $description = $node->findvalue('description'); $description =~ s/<([^>]|\n)*>//g; #strip html tags my @desc = split(/\s+/,$description); #print $description."\n"; #my $i = 0; #foreach my $element (@desc) { print "$i:@desc[$i]\n"; $i++; } #note that you could setup a subroutine to handle the below repeated code in adding observations, just didn't get around to it for this version ############# #air_temp my $air_temp = @desc[17]; $air_temp =~ s/F//g; #strip F if ($air_temp ne 'NR') { #NR means missing value $air_temp = sprintf("%.2f",($air_temp-32)*5/9); #convert fahrenheit to celsius #print $air_temp."\n"; $observation = $xp->createElement('obs'); $obsList->appendChild($observation); $type = $xp->createElement('obsType'); $type->appendText('air_temperature'); $observation->appendChild($type); $uom = $xp->createElement('uomType'); $uom->appendText('celsius'); $observation->appendChild($uom); $value = $xp->createElement('value'); $value->appendText($air_temp); $observation->appendChild($value); $elev = $xp->createElement('elev'); $observation->appendChild($elev); } ############# #water_temp my $water_temp = @desc[23]; $water_temp =~ s/F//g; #strip F if ($water_temp ne 'NR') { #NR means missing value #print "bf:".$water_temp."\n"; $water_temp = sprintf("%.2f",($water_temp-32)*5/9); #convert fahrenheit to celsius #print "af:".$water_temp."\n"; $observation = $xp->createElement('obs'); $obsList->appendChild($observation); $type = $xp->createElement('obsType'); $type->appendText('water_temperature'); $observation->appendChild($type); $uom = $xp->createElement('uomType'); $uom->appendText('celsius'); $observation->appendChild($uom); $value = $xp->createElement('value'); $value->appendText($water_temp); $observation->appendChild($value); $elev = $xp->createElement('elev'); $observation->appendChild($elev); } ############# #wind_from_direction my $wind_from_direction = @desc[7]; if ($wind_from_direction ne 'NR') { #NR means missing value $observation = $xp->createElement('obs'); $obsList->appendChild($observation); $type = $xp->createElement('obsType'); $type->appendText('wind_from_direction'); $observation->appendChild($type); $uom = $xp->createElement('uomType'); $uom->appendText('degrees_true'); $observation->appendChild($uom); $value = $xp->createElement('value'); $value->appendText($wind_from_direction); $observation->appendChild($value); $elev = $xp->createElement('elev'); $observation->appendChild($elev); } ############# #wind_speed my $wind_speed = @desc[9]; $wind_speed =~ s/mph//g; if ($wind_speed ne 'NR') { #NR means missing value $wind_speed = sprintf("%.2f",($wind_speed*0.447)); #convert mph to mps $observation = $xp->createElement('obs'); $obsList->appendChild($observation); $type = $xp->createElement('obsType'); $type->appendText('wind_speed'); $observation->appendChild($type); $uom = $xp->createElement('uomType'); $uom->appendText('m_s-1'); $observation->appendChild($uom); $value = $xp->createElement('value'); $value->appendText($wind_speed); $observation->appendChild($value); $elev = $xp->createElement('elev'); $observation->appendChild($elev); } ############# #air_pressure my $air_pressure = @desc[12]; if ($air_pressure ne 'NR') { #NR means missing value $air_pressure = sprintf("%.2f",($air_pressure*33.86)); #convert inHg to millibar $observation = $xp->createElement('obs'); $obsList->appendChild($observation); $type = $xp->createElement('obsType'); $type->appendText('air_pressure'); $observation->appendChild($type); $uom = $xp->createElement('uomType'); $uom->appendText('millibar'); $observation->appendChild($uom); $value = $xp->createElement('value'); $value->appendText($air_pressure); $observation->appendChild($value); $elev = $xp->createElement('elev'); $observation->appendChild($elev); } ############# #dew_point my $dew_point = @desc[20]; $dew_point =~ s/F//g; #strip F if ($dew_point ne 'NR') { #NR means missing value $dew_point = sprintf("%.2f",($dew_point-32)*5/9); #convert fahrenheit to celsius $observation = $xp->createElement('obs'); $obsList->appendChild($observation); $type = $xp->createElement('obsType'); $type->appendText('dew_point'); $observation->appendChild($type); $uom = $xp->createElement('uomType'); $uom->appendText('celsius'); $observation->appendChild($uom); $value = $xp->createElement('value'); $value->appendText($dew_point); $observation->appendChild($value); $elev = $xp->createElement('elev'); $observation->appendChild($elev); } ############# #visibility my $visibility = @desc[25]; $visibility =~ s/sm//g; if ($visibility ne 'NR') { #NR means missing value $visibility = sprintf("%.2f",($visibility*0.869)); #convert statute to nautical miles $observation = $xp->createElement('obs'); $obsList->appendChild($observation); $type = $xp->createElement('obsType'); $type->appendText('visibility'); $observation->appendChild($type); $uom = $xp->createElement('uomType'); $uom->appendText('nautical_miles'); $observation->appendChild($uom); $value = $xp->createElement('value'); $value->appendText($visibility); $observation->appendChild($value); $elev = $xp->createElement('elev'); $observation->appendChild($elev); } ############# #wave_height my $wave_height = @desc[30]; $wave_height =~ s/ft//g; if ($wave_height ne 'NR') { #NR means missing value $wave_height = sprintf("%.2f",($wave_height*0.3048)); #convert feet to meter $observation = $xp->createElement('obs'); $obsList->appendChild($observation); $type = $xp->createElement('obsType'); $type->appendText('wave_height'); $observation->appendChild($type); $uom = $xp->createElement('uomType'); $uom->appendText('m'); $observation->appendChild($uom); $value = $xp->createElement('value'); $value->appendText($wave_height); $observation->appendChild($value); $elev = $xp->createElement('elev'); $observation->appendChild($elev); } ############# #wave_period my $wave_period = @desc[32]; $wave_period =~ s/s//g; if ($wave_period ne 'NR') { #NR means missing value $observation = $xp->createElement('obs'); $obsList->appendChild($observation); $type = $xp->createElement('obsType'); $type->appendText('wave_period'); $observation->appendChild($type); $uom = $xp->createElement('uomType'); $uom->appendText('s'); $observation->appendChild($uom); $value = $xp->createElement('value'); $value->appendText($wave_period); $observation->appendChild($value); $elev = $xp->createElement('elev'); $observation->appendChild($elev); } ############# #swell_height my $swell_height = @desc[34]; $swell_height =~ s/ft//g; if ($swell_height ne 'NR') { #NR means missing value $swell_height = sprintf("%.2f",($swell_height*0.3048)); #convert feet to meter $observation = $xp->createElement('obs'); $obsList->appendChild($observation); $type = $xp->createElement('obsType'); $type->appendText('swell_height'); $observation->appendChild($type); $uom = $xp->createElement('uomType'); $uom->appendText('m'); $observation->appendChild($uom); $value = $xp->createElement('value'); $value->appendText($swell_height); $observation->appendChild($value); $elev = $xp->createElement('elev'); $observation->appendChild($elev); } ############# #swell_period my $swell_period = @desc[36]; $swell_period =~ s/s//g; if ($swell_period ne 'NR') { #NR means missing value $observation = $xp->createElement('obs'); $obsList->appendChild($observation); $type = $xp->createElement('obsType'); $type->appendText('swell_period'); $observation->appendChild($type); $uom = $xp->createElement('uomType'); $uom->appendText('s'); $observation->appendChild($uom); $value = $xp->createElement('value'); $value->appendText($swell_period); $observation->appendChild($value); $elev = $xp->createElement('elev'); $observation->appendChild($elev); } ############# #swell_from_direction my $swell_from_direction = @desc[38]; if ($swell_from_direction ne 'NR') { #NR means missing value $observation = $xp->createElement('obs'); $obsList->appendChild($observation); $type = $xp->createElement('obsType'); $type->appendText('swell_from_direction'); $observation->appendChild($type); $uom = $xp->createElement('uomType'); $uom->appendText('degrees_true'); $observation->appendChild($uom); $value = $xp->createElement('value'); $value->appendText($swell_from_direction); $observation->appendChild($value); $elev = $xp->createElement('elev'); $observation->appendChild($elev); } ############# } ##write file my $xml_content = $xp->serialize; my $current_date = `date -u +"%Y%m%d%H%M%S"`; chomp($current_date); my $output_name = 'vos'; ##write file open (FILE_XML,">/var/www/html/obskml/feeds/$output_name/archive/$output_name\_metadata_$current_date.kml"); print FILE_XML $xml_content; close (FILE_XML); ##zip file `cd /var/www/html/obskml/feeds/$output_name/archive ; zip -m $output_name\_metadata_$current_date.kmz $output_name\_metadata_$current_date.kml`; ##copy file to latest `cp -f /var/www/html/obskml/feeds/$output_name/archive/$output_name\_metadata_$current_date.kmz /var/www/html/obskml/feeds/$output_name/$output_name\_metadata_latest.kmz`; #remove original file `rm $xml_filepath`; exit 0;