svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.basicformats / src / main / java / org / gvsig / basicformats / impl / DefaultCLRFile.java @ 44831
History | View | Annotate | Download (3.85 KB)
1 |
package org.gvsig.basicformats.impl; |
---|---|
2 |
|
3 |
import java.awt.Color; |
4 |
import java.io.File; |
5 |
import java.io.IOException; |
6 |
import java.util.ArrayList; |
7 |
import java.util.List; |
8 |
import org.apache.commons.io.FileUtils; |
9 |
import org.apache.commons.io.FilenameUtils; |
10 |
import org.apache.commons.lang3.StringUtils; |
11 |
import org.gvsig.basicformats.CLRFile; |
12 |
import org.gvsig.raster.lib.legend.api.RasterLegendLocator; |
13 |
import org.gvsig.raster.lib.legend.api.RasterLegendManager; |
14 |
import org.gvsig.raster.lib.legend.api.colortable.ColorTable; |
15 |
import org.gvsig.raster.lib.legend.api.colortable.colortableclass.ColorTableClass; |
16 |
import org.slf4j.Logger; |
17 |
import org.slf4j.LoggerFactory; |
18 |
|
19 |
public class DefaultCLRFile implements CLRFile { |
20 |
|
21 |
private static final Logger logger = LoggerFactory.getLogger(DefaultCLRFile.class); |
22 |
|
23 |
private File source; |
24 |
private ColorTable colorTable;
|
25 |
|
26 |
public DefaultCLRFile() {
|
27 |
this.source = null; |
28 |
this.colorTable = null; |
29 |
} |
30 |
|
31 |
@Override
|
32 |
public File getFile(File file) { |
33 |
File f = new File(FilenameUtils.removeExtension(file.getAbsolutePath()) + "." + FILE_EXTENSION); |
34 |
return f;
|
35 |
} |
36 |
|
37 |
@Override
|
38 |
public File getFile() { |
39 |
return source;
|
40 |
} |
41 |
|
42 |
@Override
|
43 |
public ColorTable getColorTable() {
|
44 |
return this.colorTable; |
45 |
} |
46 |
|
47 |
@Override
|
48 |
public void setColorTable(ColorTable colorTable) { |
49 |
this.colorTable = colorTable;
|
50 |
} |
51 |
|
52 |
@Override
|
53 |
public void read(File file) throws IOException { |
54 |
File f = this.getFile(file); |
55 |
if (f.exists()) {
|
56 |
try {
|
57 |
RasterLegendManager legendManager = RasterLegendLocator.getRasterLegendManager(); |
58 |
List<ColorTableClass> colors = new ArrayList<>(); |
59 |
List<String> lines = FileUtils.readLines(f); |
60 |
if (lines != null) { |
61 |
this.source = f.getAbsoluteFile();
|
62 |
int lineno = 1; |
63 |
double interpolation = 50.0; |
64 |
for (String line : lines) { |
65 |
line = line.trim(); |
66 |
if (!Character.isDigit(line.charAt(0))) { |
67 |
continue;
|
68 |
} |
69 |
try {
|
70 |
String[] words = StringUtils.split(line); |
71 |
if (words.length < 4) { |
72 |
continue;
|
73 |
} |
74 |
int value = Integer.parseInt(words[0]); |
75 |
int r = Integer.parseInt(words[1]); |
76 |
int g = Integer.parseInt(words[2]); |
77 |
int b = Integer.parseInt(words[3]); |
78 |
Color color = new Color(r, g, b); |
79 |
|
80 |
ColorTableClass colorTableClass = legendManager.createColorTableClass( |
81 |
String.valueOf(value), value, interpolation, color);
|
82 |
|
83 |
colors.add(colorTableClass); |
84 |
} catch (Exception e) { |
85 |
logger.warn("Can't parse line '" + line
|
86 |
+ "' (lineno=+" + lineno
|
87 |
+ ", file=" + f.getAbsoluteFile()
|
88 |
+ ").", e);
|
89 |
|
90 |
} |
91 |
} |
92 |
} |
93 |
if( !colors.isEmpty() ) {
|
94 |
String colorTableName = FilenameUtils.getBaseName(f.getName());
|
95 |
this.colorTable = legendManager.createColorTable(colorTableName, colors, true); |
96 |
} |
97 |
} catch (IOException e) { |
98 |
logger.warn("Couldn't read " + FILE_EXTENSION + " file (" + f.getAbsoluteFile() + ")", e); |
99 |
throw e;
|
100 |
} |
101 |
} |
102 |
|
103 |
} |
104 |
|
105 |
@Override
|
106 |
public void write(File file) throws IOException { |
107 |
File f = this.getFile(file); |
108 |
// TODO: falta implementar guardar la CLR
|
109 |
} |
110 |
|
111 |
} |