gvsig-raster / org.gvsig.raster / branches / org.gvsig.raster.2.4 / org.gvsig.raster / org.gvsig.raster.gdal / org.gvsig.raster.gdal.provider.legend / src / main / java / org / gvsig / raster / gdal / provider / legend / RasterGdalGetColorTable.java @ 6298
History | View | Annotate | Download (3.99 KB)
1 | 6298 | dmartinezizquierdo | package org.gvsig.raster.gdal.provider.legend; |
---|---|---|---|
2 | |||
3 | import java.awt.Color; |
||
4 | import java.awt.image.ColorModel; |
||
5 | import java.awt.image.ComponentColorModel; |
||
6 | import java.awt.image.IndexColorModel; |
||
7 | import java.util.ArrayList; |
||
8 | import java.util.List; |
||
9 | |||
10 | import org.gdal.gdal.Band; |
||
11 | import org.gdal.gdal.Dataset; |
||
12 | import org.gvsig.fmap.dal.raster.api.RasterStore; |
||
13 | import org.gvsig.raster.gdal.provider.RasterGdalStoreProvider; |
||
14 | import org.gvsig.raster.lib.legend.api.ColorInterpretation; |
||
15 | import org.gvsig.raster.lib.legend.api.ColorTable; |
||
16 | import org.gvsig.raster.lib.legend.api.ColorTableClass; |
||
17 | import org.gvsig.raster.lib.legend.api.RasterLegend; |
||
18 | import org.gvsig.raster.lib.legend.api.RasterLegendLocator; |
||
19 | import org.gvsig.raster.lib.legend.api.RasterLegendManager; |
||
20 | import org.gvsig.tools.ToolsLocator; |
||
21 | import org.gvsig.tools.dynobject.DynClass; |
||
22 | import org.gvsig.tools.dynobject.DynMethod; |
||
23 | import org.gvsig.tools.dynobject.DynObject; |
||
24 | import org.gvsig.tools.dynobject.exception.DynMethodException; |
||
25 | import org.gvsig.tools.dynobject.exception.DynMethodNotSupportedException; |
||
26 | |||
27 | public class RasterGdalGetColorTable implements DynMethod { |
||
28 | |||
29 | private static Integer code = null; |
||
30 | |||
31 | static void register(DynClass storeClass) { |
||
32 | if (code != null) { |
||
33 | return;
|
||
34 | } |
||
35 | code = ToolsLocator.getDynObjectManager() |
||
36 | .registerDynMethod(storeClass, new RasterGdalGetColorTable());
|
||
37 | |||
38 | } |
||
39 | |||
40 | @Override
|
||
41 | public int getCode() throws DynMethodNotSupportedException { |
||
42 | return code;
|
||
43 | } |
||
44 | |||
45 | @Override
|
||
46 | public String getDescription() { |
||
47 | return "Raster GDAL Color Table"; |
||
48 | } |
||
49 | |||
50 | @Override
|
||
51 | public String getName() { |
||
52 | return RasterStore.DYNMETHOD_GETCOLORTABLE_NAME;
|
||
53 | } |
||
54 | |||
55 | @Override
|
||
56 | public Object invoke(DynObject self, Object[] args) |
||
57 | throws DynMethodException {
|
||
58 | ColorTable colorTable = null;
|
||
59 | |||
60 | |||
61 | try {
|
||
62 | RasterGdalStoreProvider rasterGdalProvider=(RasterGdalStoreProvider)self; |
||
63 | RasterLegendManager legendManager=RasterLegendLocator.getRasterLegendManager(); |
||
64 | |||
65 | Dataset gdalDataSet = rasterGdalProvider.getGdalDataSet(); |
||
66 | |||
67 | ColorInterpretation colorInterpretation =(ColorInterpretation)rasterGdalProvider. |
||
68 | invokeDynMethod(RasterStore.DYNMETHOD_GETCOLORINTERPRETATION_NAME, null);
|
||
69 | for (int i = 0; i < gdalDataSet.getRasterCount(); i++) { |
||
70 | if (ColorInterpretation.PALETTE_BAND.equals(colorInterpretation.get(i))){
|
||
71 | // Bands begin in 1 instead of 0
|
||
72 | int bandNumber = i + 1; |
||
73 | Band gdalBand=gdalDataSet.GetRasterBand(bandNumber); |
||
74 | org.gdal.gdal.ColorTable gdalBandColorTable = gdalBand.GetColorTable(); |
||
75 | if (gdalBandColorTable!=null){ |
||
76 | List<ColorTableClass> colorTableClasses=new ArrayList<ColorTableClass>(); |
||
77 | |||
78 | for (int j=0;j<gdalBandColorTable.GetCount();j++){ |
||
79 | String className=j+""; |
||
80 | double value=((byte)j); |
||
81 | double interpolation=50.0; |
||
82 | Color color=gdalBandColorTable.GetColorEntry(j);
|
||
83 | ColorTableClass colorTableClass = legendManager.createColorTableClass(className, value, interpolation, color); |
||
84 | colorTableClasses.add(colorTableClass); |
||
85 | } |
||
86 | String colorTableName = rasterGdalProvider.getName() + "_color_table"; |
||
87 | colorTable = legendManager.createColorTable(colorTableName, |
||
88 | colorTableClasses, true);
|
||
89 | //FIXME: De momento devolvemos el primero solo.
|
||
90 | return colorTable;
|
||
91 | } |
||
92 | } |
||
93 | |||
94 | } |
||
95 | } catch (DynMethodNotSupportedException e) {
|
||
96 | //FIXME
|
||
97 | throw new RuntimeException(e); |
||
98 | } |
||
99 | return null; |
||
100 | } |
||
101 | |||
102 | @Override
|
||
103 | public Object clone() throws CloneNotSupportedException { |
||
104 | return super.clone(); |
||
105 | } |
||
106 | } |