Thank you for the excellent explanation, it all becomes quite clear !

I have made few tweak on your code and try to integrate my original code, which is to generate kml file for google display, here is my attempt, but unfortunately the code doesn't run as expected. I think the problem is at reading the file. i switch from using 'while' to 'foreach' to loop through the file, but it doesn't work. I must have done something silly. Any suggestions ?!

Also i have also replaced your 'next unless' statement to 'if', it should works the same.

#!/usr/bin/perl use strict; use POSIX qw(ceil floor); open(INPUT, $infile); my @lines = <INPUT>; close(INPUT); open(OUTPUT, ">$outfile"); my $timeCounter = 0; my $seconds = 0; my $minutes = 0; my $hours = 0; my $comma = ","; my $space = " "; print OUTPUT "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"."\n"; print OUTPUT "<kml xmlns=\"http://earth.google.com/kml/2.1\">"."\n"; print OUTPUT "<Folder>"."\n"; print OUTPUT "<LookAt><longitude>-0.45</longitude><latitude>51.4666667 +</latitude><altitude>1500</altitude><range>7000</range><tilt>65</tilt +><heading>0</heading></LookAt>"."\n"; my @headers = split ' ', do{ my $raw = <INPUT>; $raw =~ tr[",][ ]; $ra +w }; #" printf @headers; my %planes; foreach my $currentLine(@lines) { my @fields = split',', $currentLine; #--- At this point, we have the data from the current line. We nee +d to convert this to appropriate kml elements my %row; @row{ @headers } = @fields; #--- Get all the elements my $rowid = $row{rowid}; my $aid = $row{aircraft_id}; my $actual_date_time = $row{actual_date_time}; my $latitude = $row{latitude}; my $longitude = $row{longitude}; my $radio_altitude = $row{radio_altitude}; my $ground_speed = $row{ground_speed}; my $thrust_engine_1 = $row{thrust_engine_1}; my $thrust_engine_2 = $row{thrust_engine_2}; my $nox_total = $row{nox_total}; my $co_total = $row{co_total}; #--- Now, convert start and end times into UTC my @timeString = split(my $space,$actual_date_time); $actual_date_time = @timeString[0]."T".@timeString[1]; #--- Determine the heading push @{ $planes{ $aid } }, \%row; if ( @{ $planes{ $aid} } > 1) { my $lat1 = $planes{ $aid }[ -2 ]{latitude}; my $lat2 = $planes{ $aid }[ -1 ]{latitude}; my $dlat = $lat2 - $lat1; my $lon1 = $planes{ $aid }[ -2 ]{longitude}; my $lon2 = $planes{ $aid }[ -1 ]{longitude}; my $dlon = $lon2 - $lon1; my $y = sin( $dlon / 180 ) * cos( $lat2 /180 ); my $x = cos( $lat1 /180 ) * sin( $lat2/180 ) - sin( $lat1/180 ) * +cos( $lat2/180 ) * cos( $dlon/180 ); my $heading = atan2( $y, $x ) * 57.2957795; } else { my $heading= 0; } #--- (1) Determine the geometry my $icon = "./blue_plane.png"; print OUTPUT "<Placemark><Style><IconStyle><scale>1.0</scale><head +ing>"; print OUTPUT my $heading; print OUTPUT "</heading><Icon><href>$icon</href></Icon></IconStyle +></Style><Point><coordinates>"; print OUTPUT $longitude.$comma.$latitude.$comma.$radio_altitude; print OUTPUT "</coordinates><altitudeMode>relativeToGround</altitu +deMode></Point>"; print OUTPUT "<TimeStamp><when>$actual_date_time</when></TimeStamp +>"; print OUTPUT "<description>"."ID:".$rowid."<hr/>"."time:".$actual_ +date_time."<hr/>"."Ground_speed:".$ground_speed."<hr/>"."Thrust_engin +e_1:".$thrust_engine_1."<hr/>"."thrust_engine_2:".$thrust_engine_2."< +hr/>"."nox_total:".$nox_total."<hr/>"."co_total:".$co_total."<hr/>"." +longitude:".$longitude."<hr/>"."latitude:".$latitude."<hr/>"."radio_a +ltitude:".$radio_altitude."<hr/></description>"; print OUTPUT "</Placemark>"."\n"; } print OUTPUT "</Folder></kml>";

Thanks in Millions !

Si


In reply to Re^6: Calculate aircraft headings from GPS data for google kml display by hujunsimon
in thread Calculate aircraft headings from GPS data for google kml display by hujunsimon

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.