Hello Monks !

I have a question of regarding to calculate headings of several aircrafts movements from a series of GPS data, then i can use perl subroutine to convert the code into kml and display it on GoogleEarth, so the aircraft icon could rotate as time shifts.

i have already written some code to convert the code to kml, but not the headings, i know the method i should use to calculate the heading is something like following (very crude method), but i don't know how to implement into perl. it needs to use kind of array to store current point and way point, then computer the headings from two points.

I need helps, any clever Perlmonks out there could provide me some wisdom. below provide code i written so far and input data sample

Many thanks in advance !

Psuedo code for heading calculation

dlat = lat2 - lat1 dlon = lon2 - lon1 y = sin(lon2-lon1)*cos(lat2) x = cos(lat1)*sin(lat2)-sin(lat1)*cos(lat2)*cos(lon2-lon1) if y > 0 then if x > 0 then tc1 = arctan(y/x) if x < 0 then tc1 = 180 - arctan(-y/x) if x = 0 then tc1 = 90 if y < 0 then if x > 0 then tc1 = -arctan(-y/x) if x < 0 then tc1 = arctan(y/x)-180 if x = 0 then tc1 = 270 if y = 0 then if x > 0 then tc1 = 0 if x < 0 then tc1 = 180 if x = 0 then [the 2 points are the same]

perl code

#!/usr/bin/perl use POSIX qw(ceil floor); open(INPUT, $infile); @lines = <INPUT>; close(INPUT); open(OUTPUT, ">$outfile"); sub trim($); $timeCounter = 0; $seconds = 0; $minutes = 0; $hours = 0; $comma = ","; $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"; foreach $currentLine(@lines) { @lineBuffer = split($delimiter,$currentLine); #--- At this point, we have the data from the current line. #--- Get all the elements $rowid = trim(@lineBuffer[0]); $aircraftid = trim(@lineBuffer[1]); $actual_date_time = trim(@lineBuffer[2]); #$end_time = trim(@lineBuffer[3]); $latitude = trim(@lineBuffer[3]); $longitude = trim(@lineBuffer[4]); $radio_altitude = trim(@lineBuffer[5]); $ground_speed = trim(@lineBuffer[6]); $thrust_engine_1 = trim(@lineBuffer[7]); $thrust_engine_2 = trim(@lineBuffer[8]); #--- Now, convert start and end times into UTC @timeString = split($space,$actual_date_time); $actual_date_time = @timeString[0]."T".@timeString[1]; #--- Determine the bearing $icon = "./blue_plane.png"; print OUTPUT "<Placemark><Style><IconStyle><scale>1.0</scale><head +ing>90</heading><Icon><href>$icon</href></Icon></IconStyle></Style><P +oint><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>";

Data sample of several aircraft GPS data:

"rowid","aircraft_id","actual_date_time","latitude","longitude","radio +_altitude","ground_speed","thrust_engine_1","thrust_engine_2" 3828994,599,2012-11-04 14:00:00,51.47592545,-0.437049866,-9.0,2.0,0.04 +4653355,0.041778761,1.74208527619370935228499282740898271690901934191 +164070390564,15.22092881857486030932218666006017736321211426212568671 +811012 3828995,591,2012-11-04 14:00:00,51.47598267,-0.435331702,-0.875,0.0,0. +066208174,0.068682498,0.913095793953437657822846430541224159086453865 +40627114668956,4.8940906898850944664550883969746212941234464464906348 +3492349 3828996,599,2012-11-04 14:00:01,51.47592545,-0.437049866,-9.0,2.0,0.04 +4653355,0.041778761,1.74208527619370935228499282740898271690901934191 +164070390564,15.22092881857486030932218666006017736321211426212568671 +811012 3828997,591,2012-11-04 14:00:01,51.47598267,-0.435331702,-0.75,0.0,0.0 +64970428,0.068682498,0.9055060385715144395793861983988312690635603870 +3789731965392,4.92096836684002218971971504299700119099094370033875757 +916224 3828998,599,2012-11-04 14:00:02,51.47592545,-0.437049866,-9.0,2.0,0.04 +4653355,0.041778761,1.74208527619370935228499282740898271690901934191 +164070390564,15.22092881857486030932218666006017736321211426212568671 +811012 3828999,591,2012-11-04 14:00:02,51.47598267,-0.435331702,-0.625,0.0,0. +066208174,0.068682498,0.913095793953437657822846430541224159086453865 +40627114668956,4.8940906898850944664550883969746212941234464464906348 +3492349 3829000,599,2012-11-04 14:00:03,51.47592545,-0.437049866,-9.0,2.0,0.04 +4653355,0.041

In reply to 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.