svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.basicformats / src / main / java / org / gvsig / basicformats / impl / DefaultSTXFile.java @ 45318
History | View | Annotate | Download (7.31 KB)
1 | 44831 | jjdelcerro | |
---|---|---|---|
2 | package org.gvsig.basicformats.impl; |
||
3 | |||
4 | import java.io.File; |
||
5 | import java.io.IOException; |
||
6 | import java.util.ArrayList; |
||
7 | import java.util.Collections; |
||
8 | import java.util.List; |
||
9 | import org.apache.commons.io.FileUtils; |
||
10 | import org.apache.commons.io.FilenameUtils; |
||
11 | import org.apache.commons.lang3.StringUtils; |
||
12 | import org.gvsig.basicformats.STXFile; |
||
13 | import org.slf4j.Logger; |
||
14 | import org.slf4j.LoggerFactory; |
||
15 | |||
16 | |||
17 | 45318 | fdiaz | public class DefaultSTXFile extends AbstractFormatFile implements STXFile { |
18 | 44831 | jjdelcerro | |
19 | private static final Logger logger = LoggerFactory.getLogger(DefaultSTXFile.class); |
||
20 | |||
21 | |||
22 | private File source; |
||
23 | private List<STXBand> bands; |
||
24 | |||
25 | public static class DefaultSTXBand implements STXBand { |
||
26 | |||
27 | private int band; |
||
28 | private double minimum; |
||
29 | private double maximum; |
||
30 | private double mean; // Optional |
||
31 | private double std_deviation; // Optional |
||
32 | private double linear_stretch_min; // Optional |
||
33 | private double linear_stretch_max; // Optional |
||
34 | |||
35 | private boolean valid = false; |
||
36 | |||
37 | public DefaultSTXBand() {
|
||
38 | |||
39 | } |
||
40 | |||
41 | public DefaultSTXBand(
|
||
42 | int band,
|
||
43 | double minimum,
|
||
44 | double maximum,
|
||
45 | double mean,
|
||
46 | double std_deviation,
|
||
47 | double linear_stretch_min,
|
||
48 | double linear_stretch_max
|
||
49 | ) { |
||
50 | this();
|
||
51 | this.band = band;
|
||
52 | this.minimum = minimum;
|
||
53 | this.maximum = maximum;
|
||
54 | this.mean = mean;
|
||
55 | this.std_deviation = std_deviation;
|
||
56 | this.linear_stretch_min = linear_stretch_min;
|
||
57 | this.linear_stretch_max = linear_stretch_max;
|
||
58 | } |
||
59 | |||
60 | @SuppressWarnings("OverridableMethodCallInConstructor") |
||
61 | public DefaultSTXBand(String line) { |
||
62 | this();
|
||
63 | this.parse(line);
|
||
64 | } |
||
65 | |||
66 | @Override
|
||
67 | public void parse(String line) { |
||
68 | valid = false;
|
||
69 | String[] words = StringUtils.split(line.trim().toLowerCase()); |
||
70 | if( words.length < 7 ) { |
||
71 | throw new IllegalArgumentException("At least 7 parameters are needed ("+line+")."); |
||
72 | } |
||
73 | this.band = Integer.parseInt(words[0]); |
||
74 | this.minimum = Double.parseDouble(words[1]); |
||
75 | this.maximum = Double.parseDouble(words[2]); |
||
76 | if( "#".equals(words[3]) ) { |
||
77 | this.mean = Double.NaN; |
||
78 | } else {
|
||
79 | this.mean = Double.parseDouble(words[3]); |
||
80 | } |
||
81 | if( "#".equals(words[4]) ) { |
||
82 | this.std_deviation = Double.NaN; |
||
83 | } else {
|
||
84 | this.std_deviation = Double.parseDouble(words[4]); |
||
85 | } |
||
86 | if( "#".equals(words[5]) ) { |
||
87 | this.linear_stretch_min = Double.NaN; |
||
88 | } else {
|
||
89 | this.linear_stretch_min = Double.parseDouble(words[5]); |
||
90 | } |
||
91 | if( "#".equals(words[6]) ) { |
||
92 | this.linear_stretch_max = Double.NaN; |
||
93 | } else {
|
||
94 | this.linear_stretch_max = Double.parseDouble(words[6]); |
||
95 | } |
||
96 | valid = true;
|
||
97 | } |
||
98 | |||
99 | @Override
|
||
100 | public String toString() { |
||
101 | StringBuilder builder = new StringBuilder(); |
||
102 | builder.append(this.band).append(" "); |
||
103 | builder.append(this.minimum).append(" "); |
||
104 | builder.append(this.maximum).append(" "); |
||
105 | if( Double.isNaN(this.mean) ) { |
||
106 | builder.append("# ");
|
||
107 | } else {
|
||
108 | builder.append(this.mean).append(" "); |
||
109 | } |
||
110 | if( Double.isNaN(this.std_deviation) ) { |
||
111 | builder.append("# ");
|
||
112 | } else {
|
||
113 | builder.append(this.std_deviation).append(" "); |
||
114 | } |
||
115 | if( Double.isNaN(this.linear_stretch_min) ) { |
||
116 | builder.append("# ");
|
||
117 | } else {
|
||
118 | builder.append(this.linear_stretch_min).append(" "); |
||
119 | } |
||
120 | if( Double.isNaN(this.linear_stretch_max) ) { |
||
121 | builder.append("# ");
|
||
122 | } else {
|
||
123 | builder.append(this.linear_stretch_max).append(" "); |
||
124 | } |
||
125 | return builder.toString();
|
||
126 | } |
||
127 | |||
128 | @Override
|
||
129 | public int getBand() { |
||
130 | return band;
|
||
131 | } |
||
132 | |||
133 | @Override
|
||
134 | public double getMinimum() { |
||
135 | return minimum;
|
||
136 | } |
||
137 | |||
138 | @Override
|
||
139 | public double getMaximum() { |
||
140 | return maximum;
|
||
141 | } |
||
142 | |||
143 | @Override
|
||
144 | public double getMean() { |
||
145 | return mean;
|
||
146 | } |
||
147 | |||
148 | @Override
|
||
149 | public double getStdDeviation() { |
||
150 | return std_deviation;
|
||
151 | } |
||
152 | |||
153 | @Override
|
||
154 | public double getLinearStretchMin() { |
||
155 | return linear_stretch_min;
|
||
156 | } |
||
157 | |||
158 | @Override
|
||
159 | public double getLinearStretchMax() { |
||
160 | return linear_stretch_max;
|
||
161 | } |
||
162 | |||
163 | @Override
|
||
164 | public boolean isValid() { |
||
165 | return valid;
|
||
166 | } |
||
167 | } |
||
168 | |||
169 | public DefaultSTXFile() {
|
||
170 | this.source = null; |
||
171 | this.bands = new ArrayList<>(); |
||
172 | } |
||
173 | |||
174 | @Override
|
||
175 | public File getFile(File file) { |
||
176 | File f = new File(FilenameUtils.removeExtension(file.getAbsolutePath())+"."+FILE_EXTENSION); |
||
177 | return f;
|
||
178 | } |
||
179 | |||
180 | @Override
|
||
181 | public File getFile() { |
||
182 | return source;
|
||
183 | } |
||
184 | |||
185 | @Override
|
||
186 | public void read(File file) throws IOException { |
||
187 | File f = this.getFile(file); |
||
188 | if (f.exists()) {
|
||
189 | try {
|
||
190 | this.bands = new ArrayList<>(); |
||
191 | List<String> lines = FileUtils.readLines(f); |
||
192 | if (lines != null) { |
||
193 | this.source = f.getAbsoluteFile();
|
||
194 | int lineno = 1; |
||
195 | for (String line : lines) { |
||
196 | try {
|
||
197 | DefaultSTXBand band = new DefaultSTXBand(line.trim());
|
||
198 | this.bands.add(band);
|
||
199 | } catch(IllegalArgumentException e) { |
||
200 | logger.warn("Can't parse line '" + line
|
||
201 | + "' (lineno=+" + lineno
|
||
202 | + ", file=" + f.getAbsoluteFile()
|
||
203 | + ").", e);
|
||
204 | |||
205 | } |
||
206 | } |
||
207 | } |
||
208 | this.bands = Collections.unmodifiableList(this.bands); |
||
209 | } catch (IOException e) { |
||
210 | logger.warn("Couldn't read "+FILE_EXTENSION+" file (" + f.getAbsoluteFile() + ")", e); |
||
211 | throw e;
|
||
212 | } |
||
213 | } |
||
214 | } |
||
215 | |||
216 | @Override
|
||
217 | public List<STXBand> getBands() { |
||
218 | return this.bands; |
||
219 | } |
||
220 | |||
221 | @Override
|
||
222 | public void clear() { |
||
223 | this.bands = new ArrayList<>(); |
||
224 | } |
||
225 | |||
226 | @Override
|
||
227 | public void addBand( |
||
228 | int band,
|
||
229 | double minimum,
|
||
230 | double maximum,
|
||
231 | double mean,
|
||
232 | double std_deviation,
|
||
233 | double linear_stretch_min,
|
||
234 | double linear_stretch_max
|
||
235 | ) { |
||
236 | DefaultSTXBand b = new DefaultSTXBand(band, minimum, maximum, mean, std_deviation, linear_stretch_min, linear_stretch_max);
|
||
237 | this.bands.add(b);
|
||
238 | } |
||
239 | |||
240 | @Override
|
||
241 | public void write(File file) throws IOException { |
||
242 | File f = this.getFile(file); |
||
243 | List<String> lines = new ArrayList<>(); |
||
244 | for (STXBand band : this.bands) { |
||
245 | lines.add(band.toString()); |
||
246 | } |
||
247 | FileUtils.writeLines(f, lines); |
||
248 | this.source = f;
|
||
249 | } |
||
250 | } |