gvsig-raster / org.gvsig.raster / branches / org.gvsig.raster.2.4 / org.gvsig.raster / org.gvsig.raster.lib / org.gvsig.raster.lib.legend / org.gvsig.raster.lib.legend.impl / src / main / java / org / gvsig / raster / lib / legend / impl / DefaultRasterLegendManager.java @ 6897
History | View | Annotate | Download (8.5 KB)
1 |
package org.gvsig.raster.lib.legend.impl; |
---|---|
2 |
|
3 |
import java.awt.Color; |
4 |
import java.io.File; |
5 |
import java.util.ArrayList; |
6 |
import java.util.Collection; |
7 |
import java.util.HashMap; |
8 |
import java.util.List; |
9 |
import java.util.Map; |
10 |
|
11 |
import org.gvsig.raster.lib.buffer.api.FilterList; |
12 |
import org.gvsig.raster.lib.legend.api.ColorInterpretation; |
13 |
import org.gvsig.raster.lib.legend.api.ColorTable; |
14 |
import org.gvsig.raster.lib.legend.api.ColorTableClass; |
15 |
import org.gvsig.raster.lib.legend.api.ColorTableIO; |
16 |
import org.gvsig.raster.lib.legend.api.ColorTableIOFactory; |
17 |
import org.gvsig.raster.lib.legend.api.RasterLegend; |
18 |
import org.gvsig.raster.lib.legend.api.RasterLegendManager; |
19 |
import org.gvsig.raster.lib.legend.api.Transparency; |
20 |
import org.gvsig.raster.lib.legend.api.TransparencyRange; |
21 |
import org.gvsig.raster.lib.legend.impl.io.gvSIGColorTableIO; |
22 |
|
23 |
import org.apache.commons.lang3.tuple.ImmutablePair; |
24 |
import org.apache.commons.lang3.tuple.Pair; |
25 |
import org.slf4j.Logger; |
26 |
import org.slf4j.LoggerFactory; |
27 |
|
28 |
/**
|
29 |
* Default implementation of {@link RasterLegendManager}.
|
30 |
*
|
31 |
* @author <a href="mailto:lmarques@disid.com">Lluis Marques</a>
|
32 |
*
|
33 |
*/
|
34 |
public class DefaultRasterLegendManager implements RasterLegendManager { |
35 |
|
36 |
private static final Logger LOG = LoggerFactory.getLogger(DefaultRasterLegendManager.class); |
37 |
|
38 |
private Map<String, ColorTableIOFactory> colorTableIOFactories = |
39 |
new HashMap<String, ColorTableIOFactory>(); |
40 |
|
41 |
@Override
|
42 |
public RasterLegend createLegend() {
|
43 |
return new DefaultRasterLegend(); |
44 |
} |
45 |
|
46 |
@Override
|
47 |
public RasterLegend createLegend(ColorInterpretation colorInterpretation) {
|
48 |
return new DefaultRasterLegend(colorInterpretation); |
49 |
} |
50 |
|
51 |
@Override
|
52 |
public RasterLegend createLegend(ColorInterpretation colorInterpretation,
|
53 |
ColorTable colorTable, Transparency transparency, FilterList filters) {
|
54 |
return new DefaultRasterLegend(colorInterpretation, colorTable, transparency, filters); |
55 |
} |
56 |
|
57 |
@Override
|
58 |
public List<Pair<File, ColorTable>> getColorTables(File folder) { |
59 |
|
60 |
if (folder.isFile()) {
|
61 |
throw new IllegalArgumentException("File object has to be a directory"); |
62 |
} |
63 |
|
64 |
if (!folder.canRead()) {
|
65 |
throw new IllegalArgumentException( |
66 |
"It is necessary read permissions to get files and load color tables");
|
67 |
} |
68 |
|
69 |
List<Pair<File, ColorTable>> colorTables = new ArrayList<Pair<File, ColorTable>>(); |
70 |
for (File file : listFilesForFolder(folder)) { |
71 |
|
72 |
Collection<ColorTableIOFactory> factories = this.colorTableIOFactories.values(); |
73 |
for (ColorTableIOFactory colorTableIOFactory : factories) {
|
74 |
|
75 |
if(colorTableIOFactory.accept(file)){
|
76 |
ColorTableIO colorTableIO = colorTableIOFactory.create(); |
77 |
try{
|
78 |
ColorTable colorTable = colorTableIO.read(file); |
79 |
colorTables.add(new ImmutablePair<File, ColorTable>(file,colorTable)); |
80 |
break;
|
81 |
} catch (Exception e){ |
82 |
// Log warn but continue reading files.
|
83 |
LOG.warn(String.format(
|
84 |
"%1s factory accepts file but it can not get ColorTable from file",
|
85 |
colorTableIOFactory.getName()), e); |
86 |
} |
87 |
} |
88 |
} |
89 |
} |
90 |
|
91 |
return colorTables;
|
92 |
} |
93 |
|
94 |
private List<File> listFilesForFolder(File folder) { |
95 |
List<File> files = new ArrayList<File>(); |
96 |
for (File fileEntry : folder.listFiles()) { |
97 |
if (fileEntry.isDirectory()) {
|
98 |
listFilesForFolder(fileEntry); |
99 |
} else {
|
100 |
files.add(fileEntry); |
101 |
} |
102 |
} |
103 |
return files;
|
104 |
} |
105 |
|
106 |
@Override
|
107 |
public ColorTable createColorTable() {
|
108 |
return new DefaultColorTable(); |
109 |
} |
110 |
|
111 |
@Override
|
112 |
public ColorTable createColorTable(String name, List<ColorTableClass> colorTableClasses, |
113 |
boolean interpolated) {
|
114 |
return new DefaultColorTable(name, colorTableClasses, interpolated); |
115 |
} |
116 |
|
117 |
@Override
|
118 |
public List<ColorTableClass> createListColorTableClasses(double minimum, double maximum, int intervals, Color fromColor, Color toColor){ |
119 |
double intervalSize = (maximum-minimum)/intervals;
|
120 |
return createListColorTableClasses(minimum, maximum, intervalSize, fromColor, toColor);
|
121 |
|
122 |
} |
123 |
|
124 |
@Override
|
125 |
public List<ColorTableClass> createListColorTableClasses(double minimum, double maximum, double intervalSize, Color fromColor, Color toColor){ |
126 |
List<ColorTableClass> colorTableClasses = new ArrayList<ColorTableClass>(); |
127 |
|
128 |
int counter = 0; |
129 |
double value = minimum;
|
130 |
|
131 |
Color color = fromColor;
|
132 |
while(value<maximum){
|
133 |
double proportion = (value-minimum)/(maximum-minimum);
|
134 |
color = getInterpolatedColor(fromColor, toColor, proportion); |
135 |
ColorTableClass colorTableClass = this.createColorTableClass(new Integer(counter).toString(), value, 50.0, color); |
136 |
colorTableClasses.add(colorTableClass); |
137 |
value = value + intervalSize; |
138 |
counter++; |
139 |
} |
140 |
value = maximum; |
141 |
ColorTableClass colorTableClass = this.createColorTableClass(new Integer(counter).toString(), value, 50.0, toColor); |
142 |
colorTableClasses.add(colorTableClass); |
143 |
|
144 |
return colorTableClasses;
|
145 |
} |
146 |
|
147 |
private Color getInterpolatedColor(Color fromColor, Color toColor, double proportion){ |
148 |
int red = fromColor.getRed();
|
149 |
int green = fromColor.getGreen();
|
150 |
int blue = fromColor.getBlue();
|
151 |
int alpha = fromColor.getAlpha();
|
152 |
|
153 |
int rangeRed = toColor.getRed()-fromColor.getRed();
|
154 |
red = (int)Math.round(fromColor.getRed()+(rangeRed*proportion)); |
155 |
|
156 |
int rangeGreen = toColor.getGreen()-fromColor.getGreen();
|
157 |
green = (int)Math.round(fromColor.getGreen()+(rangeGreen*proportion)); |
158 |
|
159 |
int rangeBlue = toColor.getBlue()-fromColor.getBlue();
|
160 |
blue = (int)Math.round(fromColor.getBlue()+(rangeBlue*proportion)); |
161 |
|
162 |
int rangeAlpha = toColor.getAlpha()-fromColor.getAlpha();
|
163 |
alpha = (int)Math.round(fromColor.getAlpha()+(rangeAlpha*proportion)); |
164 |
|
165 |
return new Color(red, green, blue, alpha); |
166 |
} |
167 |
|
168 |
@Override
|
169 |
public ColorTableIO getColorTableIO() {
|
170 |
ColorTableIOFactory defaultFactory = this.colorTableIOFactories.get(gvSIGColorTableIO.NAME);
|
171 |
if (defaultFactory == null) { |
172 |
if (this.colorTableIOFactories.isEmpty()) { |
173 |
LOG.warn("Any default Color Table IO factory registered");
|
174 |
return null; |
175 |
} |
176 |
} |
177 |
return defaultFactory.create();
|
178 |
} |
179 |
|
180 |
@Override
|
181 |
public List<ColorTableIOFactory> getColorTableIOFactories() { |
182 |
return new ArrayList<ColorTableIOFactory>(this.colorTableIOFactories.values()); |
183 |
} |
184 |
|
185 |
@Override
|
186 |
public ColorTableIOFactory getColorTableIOFactory(String name) { |
187 |
return this.colorTableIOFactories.get(name); |
188 |
} |
189 |
|
190 |
@Override
|
191 |
public void registerColorTableIOFactory(String name, ColorTableIOFactory colorTableIOFactory) { |
192 |
colorTableIOFactories.put(name, colorTableIOFactory); |
193 |
} |
194 |
|
195 |
@Override
|
196 |
public ColorTableClass createColorTableClass() {
|
197 |
return new DefaultColorTableClass(); |
198 |
} |
199 |
|
200 |
@Override
|
201 |
public ColorTableClass createColorTableClass(String className, double value, |
202 |
double interpolated, Color color) { |
203 |
return new DefaultColorTableClass(className, value, color, interpolated); |
204 |
} |
205 |
|
206 |
@Override
|
207 |
public ColorInterpretation createColorInterpretation(String[] coloInterpretations) { |
208 |
return new DefaultColorInterpretation(coloInterpretations); |
209 |
} |
210 |
|
211 |
@Override
|
212 |
public ColorInterpretation createColorInterpretation(String definedColorInterpretation) { |
213 |
return new DefaultColorInterpretation(definedColorInterpretation); |
214 |
} |
215 |
|
216 |
@Override
|
217 |
public Transparency createTransparency() { |
218 |
return new DefaultTransparency(); |
219 |
} |
220 |
|
221 |
@Override
|
222 |
public Transparency createTransparency(int transparency, |
223 |
List<TransparencyRange> transparencyRanges) {
|
224 |
return new DefaultTransparency(transparency, transparencyRanges); |
225 |
} |
226 |
|
227 |
@Override
|
228 |
public TransparencyRange createTransparencyRange() {
|
229 |
return new DefaultTransparencyRange(); |
230 |
} |
231 |
|
232 |
@Override
|
233 |
public TransparencyRange createTransparencyRange(int[] redRange, int[] greenRange, int[] blueRange, int alpha, |
234 |
boolean isAnd) {
|
235 |
return new DefaultTransparencyRange(redRange, greenRange, blueRange, alpha, isAnd); |
236 |
} |
237 |
|
238 |
} |