File: TileSet.java

package info (click to toggle)
gpsprune 19.2-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 4,516 kB
  • sloc: java: 42,704; sh: 25; makefile: 24; python: 15
file content (136 lines) | stat: -rw-r--r-- 3,382 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package tim.prune.function.cache;

import java.io.File;


/**
 * Class to hold information about a single tile set
 * within the overall Tile Cache.
 */
public class TileSet
{
	/** Summary row info for whole tileset */
	private RowInfo _rowInfo = null;
	/** Path relative to mapcache root */
	private String _path = null;
	/** Comma-separated list of configs using this tileset */
	private String _usedBy = null;


	/**
	 * Constructor
	 * @param inDir directory of tileset
	 * @param inPath String describing relative path from cache root
	 * @param inUsedBy String describing which configs use this Tileset
	 */
	public TileSet(File inDir, String inPath, String inUsedBy)
	{
		_path = inPath;
		_usedBy = inUsedBy;
		_rowInfo = new RowInfo();
		// Go through zoom directories and construct row info objects
		if (inDir != null && inDir.exists() && inDir.isDirectory() && inDir.canRead())
		{
			for (File subdir : inDir.listFiles())
			{
				if (subdir != null && subdir.exists() && subdir.isDirectory()
					&& subdir.canRead() && isNumeric(subdir.getName()))
				{
					RowInfo row = makeRowInfo(subdir);
					_rowInfo.addRow(row);
				}
			}
		}
	}

	/**
	 * Check if a directory name is numeric
	 * @param inName name of directory
	 * @return true if it only contains characters 0-9
	 */
	public static boolean isNumeric(String inName)
	{
		if (inName == null || inName.equals("")) return false;
		for (int i=0; i<inName.length(); i++)
		{
			char a = inName.charAt(i);
			if (a < '0' || a > '9') return false;
		}
		return true;
	}

	/**
	 * Check if a filename is numeric up until the first dot
	 * This appears to be much faster than scanning for a . with indexOf, then
	 * chopping to make a new String, and then calling isNumeric to scan through again
	 * @param inName name of file
	 * @return true if it only contains characters 0-9 before the first dot
	 */
	public static boolean isNumericUntilDot(String inName)
	{
		if (inName == null || inName.equals("") || inName.charAt(0) == '.') {
			return false;
		}
		try
		{
			char c = '.';
			int i = 0;
			do
			{
				c = inName.charAt(i);
				if (c == '.') return true; // found the dot, so stop
				if (c < '0' || c > '9') return false; // not numeric
				i++;
			} while (c != '\0');
		}
		catch (IndexOutOfBoundsException iobe) {}
		// Didn't find a dot, so can't be a valid name
		return false;
	}

	/**
	 * Make a RowInfo object from the given directory
	 * @param inDir directory for a single zoom level
	 * @return RowInfo object describing files and size
	 */
	private static RowInfo makeRowInfo(File inDir)
	{
		RowInfo row = new RowInfo();
		row.setZoom(Integer.parseInt(inDir.getName()));
		for (File subdir : inDir.listFiles())
		{
			if (subdir != null && subdir.exists() && subdir.isDirectory()
				&& subdir.canRead() && isNumeric(subdir.getName()))
			{
				// Found a directory of images (finally!)
				for (File f : subdir.listFiles())
				{
					if (f != null && f.exists() && f.isFile() && f.canRead())
					{
						if (isNumericUntilDot(f.getName())) {
							row.addTile(f.length());
						}
					}
				}
			}
		}
		return row;
	}

	/**
	 * @return row info object
	 */
	public RowInfo getRowInfo() {
		return _rowInfo;
	}

	/** @return relative path to tileset */
	public String getPath() {
		return _path;
	}

	/** @return users of tileset */
	public String getUsedBy() {
		return _usedBy;
	}
}