Debian Codegraph @

scripts/perl/scenery/calc-tile.pl

raw
1 #!/usr/bin/perl -w
2 ########################################################################
3 # calc-tile.pl
4 #
5 # Synopsis: Calculate a FlightGear tile base on longitude and latitude.
6 # Usage: perl calc-tile.pl <lon> <lat>
7 ########################################################################
8
9 use strict;
10 use POSIX;
11
12
13 ########################################################################
14 # Constants.
15 ########################################################################
16
17 my $EPSILON = 0.0000001;
18 my $DIRSEP = '/';
19
20
21 ########################################################################
22 # Functions.
23 ########################################################################
24
25 #
26 # Calculate the number of columns of tiles in a degree of longitude.
27 #
28 sub bucket_span {
29 my ($lat) = (@_);
30 if ($lat>= 89.0 ) {
31 return 360.0;
32 } elsif ($lat>= 88.0 ) {
33 return 8.0;
34 } elsif ($lat>= 86.0 ) {
35 return 4.0;
36 } elsif ($lat>= 83.0 ) {
37 return 2.0;
38 } elsif ($lat>= 76.0 ) {
39 return 1.0;
40 } elsif ($lat>= 62.0 ) {
41 return 0.5;
42 } elsif ($lat>= 22.0 ) {
43 return 0.25;
44 } elsif ($lat>= -22.0 ) {
45 return 0.125;
46 } elsif ($lat>= -62.0 ) {
47 return 0.25;
48 } elsif ($lat>= -76.0 ) {
49 return 0.5;
50 } elsif ($lat>= -83.0 ) {
51 return 1.0;
52 } elsif ($lat>= -86.0 ) {
53 return 2.0;
54 } elsif ($lat>= -88.0 ) {
55 return 4.0;
56 } elsif ($lat>= -89.0 ) {
57 return 8.0;
58 } else {
59 return 360.0;
60 }
61 }
62
63 #
64 # Format longitude as e/w.
65 #
66 sub format_lon {
67 my ($lon) = (@_);
68 if ($lon < 0) {
69 return sprintf("w%03d", int(0-$lon));
70 } else {
71 return sprintf("e%03d", int($lon));
72 }
73 }
74
75 #
76 # Format latitude as n/s.
77 #
78 sub format_lat {
79 my ($lat) = (@_);
80 if ($lat < 0) {
81 return sprintf("s%02d", int(0-$lat));
82 } else {
83 return sprintf("n%02d", int($lat));
84 }
85 }
86
87 #
88 # Generate the directory name for a location.
89 #
90 sub directory_name {
91 my ($lon, $lat) = (@_);
92 my $lon_floor = POSIX::floor($lon);
93 my $lat_floor = POSIX::floor($lat);
94 my $lon_chunk = POSIX::floor($lon/10.0) * 10;
95 my $lat_chunk = POSIX::floor($lat/10.0) * 10;
96 return format_lon($lon_chunk) . format_lat($lat_chunk) . $DIRSEP
97 . format_lon($lon_floor) . format_lat($lat_floor);
98 }
99
100 #
101 # Generate the tile index for a location.
102 #
103 sub tile_index {
104 my ($lon, $lat) = (@_);
105 my $lon_floor = POSIX::floor($lon);
106 my $lat_floor = POSIX::floor($lat);
107 my $span = bucket_span($lat);
108
109 my $x;
110 if ($span < $EPSILON) {
111 $lon = 0;
112 $x = 0;
113 } elsif ($span <= 1.0) {
114 $x = int(($lon - $lon_floor) / $span);
115 } else {
116 if ($lon >= 0) {
117 $lon = int(int($lon/$span) * $span);
118 } else {
119 $lon = int(int(($lon+1)/$span) * $span - $span);
120 if ($lon < -180) {
121 $lon = -180;
122 }
123 }
124 $x = 0;
125 }
126
127 my $y;
128 $y = int(($lat - $lat_floor) * 8);
129
130
131 my $index = 0;
132 $index += ($lon_floor + 180) << 14;
133 $index += ($lat_floor + 90) << 6;
134 $index += $y << 3;
135 $index += $x;
136
137 return $index;
138 }
139
140
141 ########################################################################
142 # Main program.
143 ########################################################################
144
145 my ($lon, $lat) = (@ARGV);
146
147 my $dir = directory_name($lon, $lat);
148 my $index = tile_index($lon, $lat);
149 my $path = "$dir$DIRSEP$index.stg";
150
151 print "Longitude: $lon\n";
152 print "Latitude: $lat\n";
153 print "Tile: $index\n";
154 print "Path: \"$path\"\n";
155
156 1;