]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/function/srtm/gen/GenerateTileLookup.java
Version 11, August 2010
[GpsPrune.git] / tim / prune / function / srtm / gen / GenerateTileLookup.java
1 package tim.prune.function.srtm.gen;
2
3 import java.io.BufferedReader;
4 import java.io.File;
5 import java.io.FileOutputStream;
6 import java.io.FileReader;
7
8 /**
9  * Class used to generate a lookup file to find the URLs of SRTM tiles.
10  * The tiles are split into directories for each continent, so we need some way
11  * of going from coordinates to directories.
12  * This class reads the directory listings from the files tiles1.txt, tiles2.txt etc
13  * and combines the result into a binary array
14  */
15 public class GenerateTileLookup
16 {
17
18         /**
19          * Main method for generating the array
20          * @param args ignored
21          */
22         public static void main(String[] args)
23         {
24                 System.out.println("Generate tile lookup");
25                 byte[] lookup = new byte[360 * 120]; // +/- 180 degrees longitude, +/- 60 degrees latitude
26                 for (int f=1; f<= 6; f++)
27                 {
28                         try
29                         {
30                                 BufferedReader r = new BufferedReader(new FileReader(new File("tim/prune/function/srtm/gen/tiles" + f + ".txt")));
31                                 String line = r.readLine();
32                                 System.out.println("Read continent: '" + line + "'");
33                                 while ((line = r.readLine()) != null) {
34                                         if (line.length() == 7)
35                                         {
36                                                 boolean north = (line.charAt(0) == 'N');
37                                                 int lat = Integer.parseInt(line.substring(1, 3));
38                                                 if (!north) {lat = -lat;}
39                                                 boolean east = (line.charAt(3) == 'E');
40                                                 int lon = Integer.parseInt(line.substring(4));
41                                                 if (!east) {lon = -lon;}
42                                                 // Store in lookup
43                                                 int arrindex = (lat+59)*360 + (lon+180);
44                                                 lookup[arrindex] = (byte) f;
45                                         }
46                                 }
47                         }
48                         catch (Exception e) {
49                                 e.printStackTrace();
50                         }
51                 }
52                 // Now f should be populated
53                 StringBuilder b = new StringBuilder();
54                 for (int l=-180; l<180; l++) {
55                         int i = 59 * 360 + (l+180);
56                         b.append("" + lookup[i]);
57                 }
58                 System.out.println("equator: " + b.toString());
59
60                 // Write bytes to file
61                 try {
62                         FileOutputStream out = new FileOutputStream(new File("srtmtiles.dat"));
63                         out.write(lookup);
64                         out.close();
65                 }
66                 catch (Exception e) {
67                         e.printStackTrace();
68                 }
69         }
70 }