Statistics
| Revision:

root / trunk / libraries / libCq_CMS_praster / src / org / cresques / io / data / RasterizeGrid.java @ 8026

History | View | Annotate | Download (7.79 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2006 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 */
19
package org.cresques.io.data;
20

    
21
import org.cresques.io.Rasterizer;
22

    
23
/**
24
 * Salva a raster un objeto Grid.
25
 * Sirve datos solicitados por los drivers que salvan a raster. 
26
 * Hereda de Rasterizer y reescribe el m?todo readData que es el que 
27
 * ser? llamado desde el driver cada vez que vacie el buffer y 
28
 * necesite m?s datos.
29
 * 
30
 * @author Nacho Brodin (brodin_ign@gva.es)
31
 */
32
public class RasterizeGrid extends Rasterizer{
33
        
34
        public static final int                        BLOCKSIZE = 512;
35
        
36
        /**
37
         * N?mero de bloques totales a leer del grid
38
         */
39
        private int                                                nBlocks = 0;
40
        /**
41
         * Tama?o en pixeles del ?ltimo bloque
42
         */
43
        private int                                                sizeLastBlock = 0;
44
        /**
45
         * Posici?n y del grid donde se ha quedado la lectura para que el la siguiente
46
         * petici?n se sirva desde donde se qued? la ?ltima vez.
47
         */
48
        private int                                         line = 0;
49
        /**
50
         * Buffer con los datos del grid
51
         */
52
        private RasterBuf                                 rasterBuf = null;
53
        /**
54
         * Bandas a salvar. Si vale null se salvar?n todas las del rasterBuf sino se har?
55
         * una selecci?n. Cada elemento del array es una banda del rasterBuf y el valor del contenido
56
         * es la posici?n en la que se salva.
57
         */
58
        private int[]                                        bands = null;
59
        /**
60
         * Cuando se desea salvar un raster con una sola banda del rasterBuf bands valdr? null y se 
61
         * usar? band para saber que banda del rasterBuf se va a salvar. Esto indica usando el constructor
62
         * adecuado.
63
         */
64
        private int                                                band = 0;
65
        /**
66
         * Constructor
67
         * @param grid Grid desde donde se leen los datos
68
         * @param bands Bandas a salvar. Si vale null se salvar?n todas las del rasterBuf sino se har?
69
         * una selecci?n. Cada elemento del array es una banda del rasterBuf y el valor del contenido
70
         * es la posici?n en la que se salva.
71
         */
72
        public RasterizeGrid(RasterBuf rasterBuf, int[] bands){
73
                super(BLOCKSIZE);        
74
                this.bands = bands;
75
                
76
                //Cuando bands es null es que entran todas las bandas
77
                if(bands == null){
78
                        bands = new int[rasterBuf.getBandCount()];
79
                        for (int iBand = 0; iBand < rasterBuf.getBandCount(); iBand++)
80
                                bands[iBand] = iBand;
81
                }
82
                                
83
                init(rasterBuf);
84
        }
85
        
86
        /**
87
         * Constructor
88
         * @param rasterBuf RasterBuf desde donde se leen los datos
89
         * @param bands Bandas a salvar. Si vale null se salvar?n todas las del rasterBuf sino se har?
90
         * una selecci?n. Cada elemento del array es una banda del rasterBuf y el valor del contenido
91
         * es la posici?n en la que se salva.
92
         */
93
        public RasterizeGrid(RasterBuf rasterBuf, int band){
94
                super(BLOCKSIZE);
95
                this.band = band;
96
                init(rasterBuf);
97
        }
98
        
99
        /**
100
         * Inicializa el n?mero de bloques y el tama?o del ?ltimo. Asigna el grid
101
         * y el rasterbuf desde el que se obtienen los datos.
102
         * @param grid
103
         */
104
        public void init(RasterBuf rasterBuf){
105
                this.rasterBuf = rasterBuf;;
106
                nBlocks = (int)(rasterBuf.getHeight() / BLOCKSIZE);
107
                sizeLastBlock = (rasterBuf.getHeight() - (nBlocks * BLOCKSIZE));
108
                nBlocks ++;
109
        }
110
        
111
        /* (non-Javadoc)
112
         * @see org.cresques.io.IDataWriter#readData(int, int, int)
113
         */
114
        public int[] readARGBData(int sX, int sY, int nBand){
115
                return null;
116
        }
117
        
118
        /**
119
         * Lee los datos tipo byte del grid de ancho sizeX y alto sizeY desde la posici?n
120
         * indicada en la variable de instancia "line". El array bands dice que bandas del
121
         * rasterBuf va a ser salvadas, en caso de que bands sea null significa que solo vamos
122
         * a salvar una banda del rasterBuf con lo que usaremos la variable band que indica que
123
         * banda hay que salvar.
124
         */
125
        public byte[][] readByteData(int sizeX, int sizeY) {
126
                byte[][] buffer = null;
127
                int cont = 0;
128
                
129
                if(bands != null){
130
                        buffer = new byte[bands.length][sizeX * sizeY];
131
                        for (int iBand = 0; iBand < bands.length; iBand++){
132
                                cont = 0;
133
                                for(int iRow = line; iRow < (line + sizeY); iRow ++){
134
                                        for(int iCol = 0; iCol < sizeX; iCol ++){
135
                                                buffer[iBand][cont] = rasterBuf.getElemByte(iRow, iCol, iBand);
136
                                                cont ++;
137
                                        }
138
                                }
139
                        }
140
                }else{
141
                        buffer = new byte[1][sizeX * sizeY];
142
                        for(int iRow = line; iRow < (line + sizeY); iRow ++){
143
                                for(int iCol = 0; iCol < sizeX; iCol ++){
144
                                        buffer[0][cont] = rasterBuf.getElemByte(iRow, iCol, band);
145
                                        cont ++;
146
                                }
147
                        }
148
                }
149
                                
150
                line += sizeY;
151
                return buffer;
152
        }
153

    
154
        public short[][] readShortData(int sizeX, int sizeY) {
155
                short[][] buffer = null;
156
                int cont = 0;
157
                
158
                if(bands != null){
159
                        buffer = new short[bands.length][sizeX * sizeY];
160
                        for (int iBand = 0; iBand < bands.length; iBand++){
161
                                cont = 0;
162
                                for(int iRow = line; iRow < (line + sizeY); iRow ++){
163
                                        for(int iCol = 0; iCol < sizeX; iCol ++){
164
                                                buffer[iBand][cont] = rasterBuf.getElemShort(iRow, iCol, iBand);
165
                                                cont ++;
166
                                        }
167
                                }
168
                        }
169
                }else{
170
                        buffer = new short[1][sizeX * sizeY];
171
                        for(int iRow = line; iRow < (line + sizeY); iRow ++){
172
                                for(int iCol = 0; iCol < sizeX; iCol ++){
173
                                        buffer[0][cont] = rasterBuf.getElemShort(iRow, iCol, band);
174
                                        cont ++;
175
                                }
176
                        }
177
                }                
178
                                
179
                line += sizeY;
180
                return buffer;
181
        }
182

    
183
        public int[][] readIntData(int sizeX, int sizeY) {
184
                int[][] buffer = null;
185
                int cont = 0;
186
                
187
                if(bands != null){
188
                        buffer = new int[bands.length][sizeX * sizeY];
189
                        for (int iBand = 0; iBand < bands.length; iBand++){
190
                                cont = 0;
191
                                for(int iRow = line; iRow < (line + sizeY); iRow ++){
192
                                        for(int iCol = 0; iCol < sizeX; iCol ++){
193
                                                buffer[iBand][cont] = rasterBuf.getElemInt(iRow, iCol, iBand);
194
                                                cont ++;
195
                                        }
196
                                }
197
                        }
198
                }else{
199
                        buffer = new int[1][sizeX * sizeY];
200
                        for(int iRow = line; iRow < (line + sizeY); iRow ++){
201
                                for(int iCol = 0; iCol < sizeX; iCol ++){
202
                                        buffer[0][cont] = rasterBuf.getElemInt(iRow, iCol, band);
203
                                        cont ++;
204
                                }
205
                        }
206
                }                        
207
                
208
                line += sizeY;
209
                return buffer;
210
        }
211
        
212
        public float[][] readFloatData(int sizeX, int sizeY) {
213
                float[][] buffer = null;
214
                int cont = 0;
215
                
216
                if(bands != null){
217
                        buffer = new float[bands.length][sizeX * sizeY];
218
                        for (int iBand = 0; iBand < bands.length; iBand++){
219
                                cont = 0;
220
                                for(int iRow = line; iRow < (line + sizeY); iRow ++){
221
                                        for(int iCol = 0; iCol < sizeX; iCol ++){
222
                                                buffer[iBand][cont] = rasterBuf.getElemFloat(iRow, iCol, iBand);
223
                                                cont ++;
224
                                        }
225
                                }
226
                        }
227
                }else{
228
                        buffer = new float[1][sizeX * sizeY];
229
                        for(int iRow = line; iRow < (line + sizeY); iRow ++){
230
                                for(int iCol = 0; iCol < sizeX; iCol ++){
231
                                        buffer[0][cont] = rasterBuf.getElemFloat(iRow, iCol, band);
232
                                        cont ++;
233
                                }
234
                        }
235
                }                                
236
                
237
                line += sizeY;
238
                return buffer;
239
        }
240

    
241
        public double[][] readDoubleData(int sizeX, int sizeY) {
242
                double[][] buffer = null;
243
                int cont = 0;
244
                
245
                if(bands != null){
246
                        buffer = new double[bands.length][sizeX * sizeY];
247
                        for (int iBand = 0; iBand < bands.length; iBand++){
248
                                cont = 0;
249
                                for(int iRow = line; iRow < (line + sizeY); iRow ++){
250
                                        for(int iCol = 0; iCol < sizeX; iCol ++){
251
                                                buffer[iBand][cont] = rasterBuf.getElemDouble(iRow, iCol, iBand);
252
                                                cont ++;
253
                                        }
254
                                }
255
                        }
256
                }else{
257
                        buffer = new double[1][sizeX * sizeY];
258
                        for(int iRow = line; iRow < (line + sizeY); iRow ++){
259
                                for(int iCol = 0; iCol < sizeX; iCol ++){
260
                                        buffer[0][cont] = rasterBuf.getElemDouble(iRow, iCol, band);
261
                                        cont ++;
262
                                }
263
                        }
264
                }                                
265
                
266
                line += sizeY;
267
                return buffer;
268
        }
269

    
270
}