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