2 ########################################################################
5 # Synopsis: Calculate a FlightGear tile base on longitude and latitude.
6 # Usage: perl calc-tile.pl <lon> <lat>
7 ########################################################################
14 ########################################################################
16 ########################################################################
18 my $EPSILON = 0.0000001;
23 ########################################################################
25 ########################################################################
28 # Calculate the number of columns of tiles in a degree of longitude.
34 } elsif ($lat>= 88.0 ) {
36 } elsif ($lat>= 86.0 ) {
38 } elsif ($lat>= 83.0 ) {
40 } elsif ($lat>= 76.0 ) {
42 } elsif ($lat>= 62.0 ) {
44 } elsif ($lat>= 22.0 ) {
46 } elsif ($lat>= -22.0 ) {
48 } elsif ($lat>= -62.0 ) {
50 } elsif ($lat>= -76.0 ) {
52 } elsif ($lat>= -83.0 ) {
54 } elsif ($lat>= -86.0 ) {
56 } elsif ($lat>= -88.0 ) {
58 } elsif ($lat>= -89.0 ) {
66 # Format longitude as e/w.
71 return sprintf("w%03d", int(0-$lon));
73 return sprintf("e%03d", int($lon));
78 # Format latitude as n/s.
83 return sprintf("s%02d", int(0-$lat));
85 return sprintf("n%02d", int($lat));
90 # Generate the directory name for a location.
93 my ($lon, $lat) = (@_);
94 my $lon_floor = POSIX::floor($lon);
95 my $lat_floor = POSIX::floor($lat);
96 my $lon_chunk = POSIX::floor($lon/10.0) * 10;
97 my $lat_chunk = POSIX::floor($lat/10.0) * 10;
98 return format_lon($lon_chunk) . format_lat($lat_chunk) . $DIRSEP
99 . format_lon($lon_floor) . format_lat($lat_floor);
103 # Generate the tile index for a location.
106 my ($lon, $lat) = (@_);
107 my $lon_floor = POSIX::floor($lon);
108 my $lat_floor = POSIX::floor($lat);
109 my $span = bucket_span($lat);
112 if ($span < $EPSILON) {
115 } elsif ($span <= 1.0) {
116 $x = int(($lon - $lon_floor) / $span);
119 $lon = int(int($lon/$span) * $span);
121 $lon = int(int(($lon+1)/$span) * $span - $span);
130 $y = int(($lat - $lat_floor) * 8);
134 $index += ($lon_floor + 180) << 14;
135 $index += ($lat_floor + 90) << 6;
144 ########################################################################
146 ########################################################################
148 my ($lon, $lat) = (@ARGV);
150 my $dir = directory_name($lon, $lat);
151 my $index = tile_index($lon, $lat);
152 my $path = "$dir$DIRSEP$index.stg";
154 print "Longitude: $lon\n";
155 print "Latitude: $lat\n";
156 print "Tile: $index\n";
157 print "Path: \"$path\"\n";