Statistics
| Revision:

svn-gvsig-desktop / tags / v1_1_Build_1006 / libraries / libCq_CMS_praster / src / org / cresques / util / Utilities.java @ 12458

History | View | Annotate | Download (11.5 KB)

1
package org.cresques.util;
2

    
3
import java.awt.geom.Point2D;
4
import java.awt.geom.Rectangle2D;
5
import java.io.File;
6
import java.io.FileInputStream;
7
import java.io.FileNotFoundException;
8
import java.io.FileOutputStream;
9
import java.io.IOException;
10
import java.io.InputStream;
11
import java.io.OutputStream;
12

    
13
import org.cresques.io.data.RasterBuf;
14
import org.cresques.px.Extent;
15

    
16
import es.gva.cit.jgdal.Gdal;
17

    
18
/**
19
 * 
20
 * @author Nacho Brodin (nachobrodin@gmail.com)
21
 *
22
 */
23
public class Utilities {
24
        
25
        public static final        int MAX_BYTE_BIT_VALUE        = 255;
26
        public static final        int MAX_SHORT_BIT_VALUE        = 65535;
27
            
28
    //---------------------------------------------------------------
29
    //TIPOS DE DATOS
30
    
31
    /**
32
     * Conversi?n de los tipos de datos de gdal a los tipos de datos de 
33
     * RasterBuf
34
     * @param gdalType Tipo de dato de gdal
35
     * @return Tipo de dato de RasterBuf
36
     */
37
    public static int getRasterBufTypeFromGdalType(int gdalType){
38
            switch(gdalType){
39
                case 1:return  RasterBuf.TYPE_BYTE;//GDT_BYTE 
40
                case 2://GDT_UInt16
41
                case 8://GDT_CInt16
42
                case 3:return RasterBuf.TYPE_SHORT;//GDT_Int16        
43
                case 4://GDT_UInt32
44
                case 9://GDT_CInt32
45
                case 5:return RasterBuf.TYPE_INT;//GDT_Int32
46
                case 10://GDT_CFloat32
47
                case 6:return RasterBuf.TYPE_FLOAT;//GDT_Float32
48
                case 11://GDT_CFloat64
49
                case 7:return RasterBuf.TYPE_DOUBLE;//GDT_Float64
50
                }
51
            return RasterBuf.TYPE_UNDEFINED;
52
    }
53
    
54
    /**
55
     * Conversi?n de los tipos de datos de RasterBuf en los de gdal.
56
     * @param rasterBufType Tipo de dato de RasterBuf
57
     * @return Tipo de dato de Gdal
58
     */
59
    public static int getGdalTypeFromRasterBufType(int rasterBufType){
60
            switch(rasterBufType){
61
                case RasterBuf.TYPE_BYTE:return Gdal.GDT_Byte;
62
                case RasterBuf.TYPE_USHORT: return Gdal.GDT_UInt16;
63
                case RasterBuf.TYPE_SHORT: return Gdal.GDT_Int16;
64
                case RasterBuf.TYPE_INT: return Gdal.GDT_Int32;
65
                case RasterBuf.TYPE_FLOAT: return Gdal.GDT_Float32;
66
                case RasterBuf.TYPE_DOUBLE: return Gdal.GDT_Float64;
67
                case RasterBuf.TYPE_UNDEFINED: return Gdal.GDT_Unknown;
68
                case RasterBuf.TYPE_IMAGE: return Gdal.GDT_Byte;
69
                }
70
            return Gdal.GDT_Unknown;
71
    }
72
    
73
    /**
74
     * Conversi?n de los tipos de datos de MrSID a los tipos de datos de 
75
     * RasterBuf
76
     * @param mrsidType Tipo de dato de MrSID
77
     * @return Tipo de dato de RasterBuf
78
     */
79
    public static int getRasterBufTypeFromMrSIDType(int mrsidType){
80
            switch(mrsidType){
81
            case 0:return RasterBuf.TYPE_UNDEFINED;//INVALID
82
                case 1://UINT8 
83
                case 2:return  RasterBuf.TYPE_BYTE;//SINT8
84
                case 3://UINT16
85
                case 4:return RasterBuf.TYPE_SHORT;//SINT16        
86
                case 5://UINT32
87
                case 6:return RasterBuf.TYPE_INT;//SINT32        
88
                case 7:return RasterBuf.TYPE_FLOAT;//FLOAT32
89
                case 8:return RasterBuf.TYPE_DOUBLE;//FLOAT64
90
                }
91
            return RasterBuf.TYPE_UNDEFINED;
92
    }
93
    
94
    //---------------------------------------------------------------
95
    //ESPACIO DE COLOR
96
    
97
    /**
98
     * Descompone un entero que representa un ARGB en sus 4 valores byte
99
     * Obtiene un array de 4 elementos donde el elemento 0 es el Rojo, el 1 es el
100
     * verde, el 2 el azul y el 3 el alpha.
101
     * @param rgb Entero con el valor ARGB a descomponer;
102
     * @return Array de cuatro elementos
103
     */
104
    public static byte[] getARGBFromIntToByteArray(int rgb){
105
            byte[] b = new byte[4];
106
            b[0] = (byte)((rgb & 0x00ff0000) >> 16);
107
            b[1] = (byte)((rgb & 0x0000ff00) >> 8);
108
            b[2] = (byte)(rgb & 0x000000ff);
109
            b[3] = (byte)((rgb & 0xff000000) >> 24);
110
            return b;
111
    }
112
    
113
    /**
114
     * Descompone un entero que representa un ARGB en sus 4 valores byte
115
     * Obtiene un array de 4 elementos donde el elemento 0 es el Rojo, el 1 es el
116
     * verde, el 2 el azul y el 3 el alpha.
117
     * @param rgb Entero con el valor ARGB a descomponer;
118
     * @return
119
     */
120
    public static int[] getARGBFromIntToIntArray(int rgb){
121
            int[] i = new int[4];
122
            i[0] = (((rgb & 0x00ff0000) >> 16) & 0xff);
123
            i[1] = (((rgb & 0x0000ff00) >> 8) & 0xff);
124
            i[2] = ((rgb & 0x000000ff) & 0xff);
125
            i[3] = (((rgb & 0xff000000) >> 24) & 0xff);
126
            return i;
127
    }
128
    
129
    /**
130
     * Obtiene un entero con los valores ARGB pasados por par?metro
131
     * @param a Valor de alpha
132
     * @param r Valor del rojo
133
     * @param g Valor del verde
134
     * @param b Valor del azul
135
     * @return entero con la mezcla de valores
136
     */
137
    public static int getIntFromARGB(int a, int r, int g, int b){
138
              return (((a & 0xff) << 24) + ((r & 0xff) << 16) + ((g & 0xff) << 8) + (b & 0xff));
139
    }
140
    
141
    //---------------------------------------------------------------
142
    //CONVERSI?N DE COORDENADAS
143
    
144
    /**
145
     * Convierte una ventana en coordenadas del mundo real a sus coordenadas relativas en pixels teniendo
146
     * en cuenta que la coordenada superior izquierda es 0,0 y la inferior derecha es maxX y maY
147
     * @param extent Extent de la imagen original
148
     * @param widthPx Ancho en pixeles de la imagen original
149
     * @param heightPx Alto en pixeles de la imagen original
150
     * @param window Ventana en coordenadas reales a transportar a coordenadas pixel
151
     * @return Ventana en coordenadas pixel
152
     */
153
    public static Rectangle2D getPxRectFromMapRect(Rectangle2D extent, double widthPx, double heightPx, Rectangle2D window){
154
            double widthWC = extent.getWidth();
155
                double heightWC = extent.getHeight();
156
                
157
                double wWindowWC = Math.abs(window.getMaxX() - window.getMinX());
158
                   double hWindowWC = Math.abs(window.getMaxY() - window.getMinY());
159
                   
160
                   double wWindowPx = ((wWindowWC * widthPx) /  widthWC);
161
                   double hWindowPx = ((hWindowWC * heightPx) /  heightWC);
162
                   
163
                   double initDistanceX = Math.abs(window.getMinX() - extent.getMinX());
164
                   double initDistanceY = Math.abs(window.getMaxY() - extent.getMaxY());
165
                   
166
                   double initPxX = ((initDistanceX * widthPx) /  widthWC);
167
                   double initPxY = ((initDistanceY * heightPx) /  heightWC);
168
                   
169
                   Rectangle2D pxRec = new Rectangle2D.Double(        initPxX, 
170
                                                                                                           initPxY,
171
                                                                                                           wWindowPx,
172
                                                                                                           hWindowPx); 
173
                   return pxRec;
174
    }
175
    
176
    /**
177
     * Convierte una ventana en coordenadas del mundo real a sus coordenadas relativas en pixels teniendo
178
     * en cuenta que la coordenada superior izquierda es 0,0 y la inferior derecha es maxX y maY
179
     * @param extent Extent de la imagen original
180
     * @param widthPx Ancho en pixeles de la imagen original
181
     * @param heightPx Alto en pixeles de la imagen original
182
     * @param window Ventana en coordenadas reales a transportar a coordenadas pixel
183
     * @return Ventana en coordenadas pixel
184
     */
185
    public static Rectangle2D getMapRectFromPxRect(Rectangle2D extent, double widthPx, double heightPx, Rectangle2D pxWindow){
186
                   double wWindowWC = ((pxWindow.getWidth() * extent.getWidth()) /  widthPx);
187
                   double hWindowWC = ((pxWindow.getHeight() * extent.getHeight()) /  heightPx);
188
                   
189
                   double initWCX = extent.getMinX() + ((pxWindow.getMinX() * extent.getWidth()) /  widthPx);
190
                   double initWCY = extent.getMaxY() - ((pxWindow.getMinY() * extent.getHeight()) /  heightPx);
191
                   
192
                   Rectangle2D mapRec = new Rectangle2D.Double(initWCX, 
193
                                                                                                           initWCY - hWindowWC,
194
                                                                                                           wWindowWC,
195
                                                                                                           hWindowWC); 
196
                   return mapRec;
197
    }
198
    
199
    /**
200
     * Convierte un punto en coordenadas del mundo a coordenadas pixel
201
     * @param p Punto a convertir
202
     * @param ext Extent completo de la imagen
203
     * @return Punto en coordenadas pixel
204
     */
205
    public static Point2D worldPointToRaster(Point2D p, Extent ext, int pxWidth, int pxHeight) { 
206
            double x = p.getX() - ext.getMin().getX();
207
            double y = p.getY() - ext.getMin().getY();
208
            int pxX = (int)((x * pxWidth) / ext.width());
209
            int pxY = (int)((y * pxHeight) / ext.height());
210
            return new Point2D.Double(pxX, pxY);
211
    }
212
    
213
    /**
214
     * Ajusta la extensi?n pasada por par?metro a los valores m?ximos y m?nimos de la 
215
     * imagen. Esto sirve para que la petici?n al driver nunca sobrepase los l?mites 
216
     * de la imagen tratada aunque la vista donde se dibuje sea de mayor tama?o.
217
     * 
218
     * @param imgExt Extent completo de la vista donde se va a dibujar.
219
     * @param extToAdj Extent a ajustar.
220
     * @return adjustedExtent Extent ajustado a m?ximos y m?nimos
221
     */
222
    public static Extent calculateAdjustedView(Extent extToAdj, Extent imgExt) { 
223
        double vx = extToAdj.minX();
224
        double vy = extToAdj.minY();
225
        double vx2 = extToAdj.maxX();
226
        double vy2 = extToAdj.maxY();
227

    
228
        if (extToAdj.minX() < imgExt.minX())
229
            vx = imgExt.minX();
230
        
231
        if (extToAdj.minY() < imgExt.minY()) 
232
            vy = imgExt.minY();
233
        
234
        if (extToAdj.maxX() > imgExt.maxX()) 
235
            vx2 = imgExt.maxX();
236
        
237
        if (extToAdj.maxY() > imgExt.maxY())
238
            vy2 = imgExt.maxY();
239
        
240
        Extent adjustedExtent = new Extent(vx, vy, vx2, vy2);
241
        return adjustedExtent;
242
    }
243
    
244
    /**
245
     * Compara dos extents y devuelve true si son iguales
246
     * @param e1 Extent a comparar
247
     * @param e2 Extent a comparar
248
     * @return true si los extents pasados por par?metro son iguales y false si no lo son
249
     */
250
    public static boolean compareExtents(Extent e1, Extent e2){
251
            return ((e1.getMin().getX() == e2.getMin().getX()) && (e1.getMin().getY() == e2.getMin().getY()) &&
252
                            (e1.getMax().getX() == e2.getMax().getX())) && (e1.getMax().getY() == e2.getMax().getY());
253
    }
254
    
255
    /**
256
     * Comprueba si un extent est? contenido dentro de otro y devuelve true en este caso.
257
     * @param e1 Extent a comprobar si est? contenido en e2
258
     * @param e2 Extent sobre el que se comprueba si e1 est? dentro
259
     * @return true si e1 est? dentro de e1
260
     */
261
    public static boolean isInside(Extent e1, Extent e2){
262
            return ((e1.getMin().getX() >= e2.getMin().getX()) && (e1.getMin().getY() >= e2.getMin().getY()) &&
263
                            (e1.getMax().getX() <= e2.getMax().getX()) && (e1.getMax().getY() <= e2.getMax().getY()));
264
    }
265
    
266
    /**
267
     * Comprueba si alguna parte de un extent est? fuera del extent que tenemos como referencia.
268
     * @param e1 Extent a comprobar si est? fuera
269
     * @param ref Extent de referencia
270
     * @return Devuelve true si alguna parte de e1 cae fuera de ref y false si no tiene ninguna fuera.
271
     */
272
    public static boolean isOutside(Extent e1, Extent ref){
273
            return ((e1.getMin().getX() < ref.getMin().getX()) || (e1.getMin().getY() < ref.getMin().getY()) ||
274
                            (e1.getMax().getX() > ref.getMax().getX()) || (e1.getMax().getY() > ref.getMax().getY()));
275
    }
276
    //---------------------------------------------------------------
277
    //TRATAMIENTO DE FICHEROS
278
    
279
    /**
280
     * Copia de ficheros
281
     * @param pathOrig Ruta de origen
282
     * @param pathDst Ruta de destino.
283
     */
284
    public static void copyFile(String pathOrig, String pathDst) 
285
            throws FileNotFoundException, IOException{
286
             InputStream in;
287
             OutputStream out;
288
             
289
             if(pathOrig == null || pathDst == null){
290
                     System.err.println("Error en path");
291
                     return;
292
             }
293
             
294
             File orig = new File(pathOrig);
295
             if(!orig.exists() || !orig.isFile() || !orig.canRead()){
296
                     System.err.println("Error en fichero de origen");
297
                     return;
298
             }
299
             
300
             File dest = new File(pathDst);
301
             String file = pathOrig.substring(pathOrig.lastIndexOf(File.separator), pathOrig.length());
302
             if(dest.isDirectory())
303
                     pathDst += file;
304
            
305
             in = new FileInputStream(pathOrig);
306
             out = new FileOutputStream(pathDst);
307
            
308
             byte[] buf = new byte[1024];
309
             int len;
310
             
311
             while ((len = in.read(buf)) > 0)
312
                        out.write(buf, 0, len);
313
                
314
             in.close();
315
             out.close();
316
    }
317
}