svn-gvsig-desktop / branches / org.gvsig.desktop-2018a / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.file / org.gvsig.fmap.dal.file.bsq / src / main / java / org / gvsig / fmap / dal / fileutils / impl / DefaultSTXFile.java @ 43873
History | View | Annotate | Download (7.4 KB)
1 | 43867 | jjdelcerro | |
---|---|---|---|
2 | package org.gvsig.fmap.dal.fileutils.impl; |
||
3 | |||
4 | import org.gvsig.fmap.dal.fileutils.STXFile; |
||
5 | import java.io.File; |
||
6 | import java.io.IOException; |
||
7 | import java.util.ArrayList; |
||
8 | import java.util.Collections; |
||
9 | import java.util.List; |
||
10 | import org.apache.commons.io.FileUtils; |
||
11 | import org.apache.commons.io.FilenameUtils; |
||
12 | import org.apache.commons.lang3.StringUtils; |
||
13 | import org.slf4j.Logger; |
||
14 | import org.slf4j.LoggerFactory; |
||
15 | |||
16 | |||
17 | public class DefaultSTXFile implements STXFile { |
||
18 | |||
19 | private static final Logger logger = LoggerFactory.getLogger(DefaultSTXFile.class); |
||
20 | |||
21 | |||
22 | private File source; |
||
23 | private List<STXBand> bands; |
||
24 | 43873 | fdiaz | |
25 | 43867 | jjdelcerro | public static class DefaultSTXBand implements STXBand { |
26 | 43873 | fdiaz | |
27 | 43867 | jjdelcerro | 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 | 43873 | fdiaz | |
35 | 43867 | jjdelcerro | private boolean valid = false; |
36 | 43873 | fdiaz | |
37 | 43867 | jjdelcerro | public DefaultSTXBand() {
|
38 | 43873 | fdiaz | |
39 | 43867 | jjdelcerro | } |
40 | |||
41 | public DefaultSTXBand(
|
||
42 | 43873 | fdiaz | int band,
|
43 | double minimum,
|
||
44 | double maximum,
|
||
45 | double mean,
|
||
46 | double std_deviation,
|
||
47 | double linear_stretch_min,
|
||
48 | 43867 | jjdelcerro | 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 | 43873 | fdiaz | |
60 | 43867 | jjdelcerro | @SuppressWarnings("OverridableMethodCallInConstructor") |
61 | public DefaultSTXBand(String line) { |
||
62 | this();
|
||
63 | this.parse(line);
|
||
64 | } |
||
65 | 43873 | fdiaz | |
66 | 43867 | jjdelcerro | @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 | 43873 | fdiaz | this.band = Integer.parseInt(words[0]); |
74 | 43867 | jjdelcerro | 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 | 43873 | fdiaz | |
128 | 43867 | jjdelcerro | @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 | @SuppressWarnings("OverridableMethodCallInConstructor") |
||
175 | public DefaultSTXFile(File file) { |
||
176 | this();
|
||
177 | this.read(file);
|
||
178 | } |
||
179 | 43873 | fdiaz | |
180 | 43867 | jjdelcerro | @Override
|
181 | public File getFile(File file) { |
||
182 | File f = new File(FilenameUtils.removeExtension(file.getAbsolutePath())+"."+FILE_EXTENSION); |
||
183 | return f;
|
||
184 | } |
||
185 | 43873 | fdiaz | |
186 | 43867 | jjdelcerro | @Override
|
187 | public File getFile() { |
||
188 | return source;
|
||
189 | } |
||
190 | |||
191 | @Override
|
||
192 | public void read(File file) { |
||
193 | File f = this.getFile(file); |
||
194 | if (f.exists()) {
|
||
195 | try {
|
||
196 | this.bands = new ArrayList<>(); |
||
197 | List<String> lines = FileUtils.readLines(f); |
||
198 | if (lines != null) { |
||
199 | this.source = f.getAbsoluteFile();
|
||
200 | int lineno = 1; |
||
201 | for (String line : lines) { |
||
202 | try {
|
||
203 | DefaultSTXBand band = new DefaultSTXBand(line.trim());
|
||
204 | this.bands.add(band);
|
||
205 | } catch(IllegalArgumentException e) { |
||
206 | logger.warn("Can't parse line '" + line
|
||
207 | + "' (lineno=+" + lineno
|
||
208 | + ", file=" + f.getAbsoluteFile()
|
||
209 | + ").", e);
|
||
210 | 43873 | fdiaz | |
211 | 43867 | jjdelcerro | } |
212 | } |
||
213 | } |
||
214 | this.bands = Collections.unmodifiableList(this.bands); |
||
215 | } catch (IOException e) { |
||
216 | logger.warn("Couldn't read "+FILE_EXTENSION+" file (" + f.getAbsoluteFile() + ")", e); |
||
217 | } |
||
218 | } |
||
219 | } |
||
220 | |||
221 | @Override
|
||
222 | public List<STXBand> getBands() { |
||
223 | return this.bands; |
||
224 | } |
||
225 | 43873 | fdiaz | |
226 | 43867 | jjdelcerro | @Override
|
227 | public void clear() { |
||
228 | this.bands = new ArrayList<>(); |
||
229 | } |
||
230 | 43873 | fdiaz | |
231 | 43867 | jjdelcerro | @Override
|
232 | public void addBand( |
||
233 | 43873 | fdiaz | int band,
|
234 | double minimum,
|
||
235 | double maximum,
|
||
236 | double mean,
|
||
237 | double std_deviation,
|
||
238 | double linear_stretch_min,
|
||
239 | 43867 | jjdelcerro | double linear_stretch_max
|
240 | ) { |
||
241 | DefaultSTXBand b = new DefaultSTXBand(band, minimum, maximum, mean, std_deviation, linear_stretch_min, linear_stretch_max);
|
||
242 | this.bands.add(b);
|
||
243 | } |
||
244 | |||
245 | @Override
|
||
246 | public void write(File file) throws IOException { |
||
247 | File f = this.getFile(file); |
||
248 | List<String> lines = new ArrayList<>(); |
||
249 | for (STXBand band : this.bands) { |
||
250 | lines.add(band.toString()); |
||
251 | } |
||
252 | FileUtils.writeLines(f, lines); |
||
253 | this.source = f;
|
||
254 | } |
||
255 | } |