]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/function/cache/TileSet.java
Version 13, August 2011
[GpsPrune.git] / tim / prune / function / cache / TileSet.java
1 package tim.prune.function.cache;
2
3 import java.io.File;
4 import java.util.ArrayList;
5
6
7 /**
8  * Class to hold information about a single tile set
9  * within the overall Tile Cache.
10  */
11 public class TileSet
12 {
13         /** Summary row info for whole tileset */
14         private RowInfo _rowInfo = null;
15         /** Path relative to mapcache root */
16         private String _path = null;
17         /** Comma-separated list of configs using this tileset */
18         private String _usedBy = null;
19         /** List of infos for each zoom level */
20         private ArrayList<RowInfo> _zoomLevels = null;
21
22
23         /**
24          * Constructor
25          * @param inDir directory of tileset
26          * @param inPath String describing relative path from cache root
27          * @param inUsedBy String describing which configs use this Tileset
28          */
29         public TileSet(File inDir, String inPath, String inUsedBy)
30         {
31                 _path = inPath;
32                 _usedBy = inUsedBy;
33                 _rowInfo = new RowInfo();
34                 _zoomLevels = new ArrayList<RowInfo>();
35                 // Go through zoom directories and construct row info objects
36                 if (inDir != null && inDir.exists() && inDir.isDirectory() && inDir.canRead())
37                 {
38                         for (File subdir : inDir.listFiles())
39                         {
40                                 if (subdir != null && subdir.exists() && subdir.isDirectory()
41                                         && subdir.canRead() && isNumeric(subdir.getName()))
42                                 {
43                                         RowInfo row = makeRowInfo(subdir);
44                                         _zoomLevels.add(row);
45                                         _rowInfo.addRow(row);
46                                 }
47                         }
48                 }
49         }
50
51         /**
52          * Check if a directory name is numeric
53          * @param inName name of directory
54          * @return true if it only contains characters 0-9
55          */
56         public static boolean isNumeric(String inName)
57         {
58                 if (inName == null || inName.equals("")) return false;
59                 for (int i=0; i<inName.length(); i++)
60                 {
61                         char a = inName.charAt(i);
62                         if (a < '0' || a > '9') return false;
63                 }
64                 return true;
65         }
66
67         /**
68          * Make a RowInfo object from the given directory
69          * @param inDir directory for a single zoom level
70          * @return RowInfo object describing files and size
71          */
72         private static RowInfo makeRowInfo(File inDir)
73         {
74                 RowInfo row = new RowInfo();
75                 row.setZoom(Integer.parseInt(inDir.getName()));
76                 for (File subdir : inDir.listFiles())
77                 {
78                         if (subdir != null && subdir.exists() && subdir.isDirectory()
79                                 && subdir.canRead() && isNumeric(subdir.getName()))
80                         {
81                                 // Found a directory of images (finally!)
82                                 for (File f : subdir.listFiles())
83                                 {
84                                         if (f != null && f.exists() && f.isFile() && f.canRead())
85                                         {
86                                                 final String filename = f.getName();
87                                                 int dotpos = filename.lastIndexOf('.');
88                                                 if (dotpos > 0 && isNumeric(filename.substring(0, dotpos))) {
89                                                         row.addTile(f.length());
90                                                 }
91                                         }
92                                 }
93                         }
94                 }
95                 return row;
96         }
97
98         /**
99          * @return row info object
100          */
101         public RowInfo getRowInfo() {
102                 return _rowInfo;
103         }
104
105         /** @return relative path to tileset */
106         public String getPath() {
107                 return _path;
108         }
109
110         /** @return users of tileset */
111         public String getUsedBy() {
112                 return _usedBy;
113         }
114 }