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