| 1 |
#!/usr/bin/perl |
| 2 |
# |
| 3 |
# Written by Curtis L. Olson, started January 2003 |
| 4 |
# |
| 5 |
# This file is in the Public Domain and comes with no warranty. |
| 6 |
# |
| 7 |
# $Id$ |
| 8 |
# ---------------------------------------------------------------------------- |
| 9 |
|
| 10 |
|
| 11 |
# This script will calculate the flightgear ground elevation for a |
| 12 |
# serious of lon/lat pairs, given one per line via stdin. Result it |
| 13 |
# written to stdout. Lon/lat must be specified in decimal degrees, |
| 14 |
# i.e. "-110.2324 39.872" |
| 15 |
# |
| 16 |
# This requires a copy of flightgear running with "--fdm=null" on the |
| 17 |
# specified "$server" host name, at the specified "$port". |
| 18 |
# |
| 19 |
# I highly recommend that you if you plan to feed a large number of |
| 20 |
# coordinates through this script that you presort your list by tile id # |
| 21 |
# That will minimize the load on the FG tile pager since you will process |
| 22 |
# all coordinates for a particular tile before moving on to the next. |
| 23 |
# Also, there is a chance the next tile will already be loaded if it is near |
| 24 |
# the previous (which it will tend to be if you sort by tile id.) |
| 25 |
|
| 26 |
use strict; |
| 27 |
|
| 28 |
use Time::HiRes qw( usleep ); |
| 29 |
|
| 30 |
require "telnet.pl"; |
| 31 |
|
| 32 |
my( $server ) = "localhost"; |
| 33 |
my( $port ) = 5401; |
| 34 |
my( $timeout ) = 10; |
| 35 |
|
| 36 |
|
| 37 |
# open the connection to the running copy of flightgear |
| 38 |
my( $fgfs ); |
| 39 |
if ( !( $fgfs = &connect($server, $port, $timeout) ) ) { |
| 40 |
die "Error: can't open socket\n"; |
| 41 |
} |
| 42 |
&send( $fgfs, "data" ); # switch to raw data mode |
| 43 |
|
| 44 |
|
| 45 |
# elevate ourselves only to make the view more interesting, this |
| 46 |
# doesn't affect the results |
| 47 |
set_prop( $fgfs, "/position/altitude-ft", "5000" ); |
| 48 |
|
| 49 |
my( $last_lon ) = -1000.0; |
| 50 |
my( $last_lat ) = -1000.0; |
| 51 |
my( $last_elev ) = -1000.0; |
| 52 |
|
| 53 |
# iterate through the requested coordinates |
| 54 |
while ( <> ) { |
| 55 |
my( $lon, $lat ) = split; |
| 56 |
set_prop( $fgfs, "/position/longitude-deg", $lon ); |
| 57 |
set_prop( $fgfs, "/position/latitude-deg", $lat ); |
| 58 |
|
| 59 |
# wait 1 second for scenery to load |
| 60 |
usleep(500000); |
| 61 |
|
| 62 |
# then fetch ground elevation |
| 63 |
my( $elev ) = get_prop( $fgfs, "/position/ground-elev-m" ); |
| 64 |
|
| 65 |
if ( $lon != $last_lon || $lat != $last_lat ) { |
| 66 |
my($waitcount) = 0; |
| 67 |
while ( $elev == $last_elev && $waitcount < 5 ) { |
| 68 |
print "(WARNING: waiting an addition 1 second and requerying.)\n"; |
| 69 |
# same answer as last time, scenery is probably still loading, |
| 70 |
# let's wait 1 more seconds and hope we get it right the next |
| 71 |
# time, we bail after 5 seconds. |
| 72 |
usleep(1000000); |
| 73 |
$elev = get_prop( $fgfs, "/position/ground-elev-m" ); |
| 74 |
$waitcount++; |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
print "$lon $lat $elev\n"; |
| 79 |
|
| 80 |
$last_elev = $elev; |
| 81 |
$last_lon = $lon; |
| 82 |
$last_lat = $lat; |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
# shutdown our connection (this leaves FG running) |
| 87 |
&send( $fgfs, "quit"); |
| 88 |
close $fgfs; |