Statistics
| Revision:

root / trunk / libraries / libRaster / src-test / org / gvsig / raster / grid / TGEmptyBufferForWrite.java @ 11076

History | View | Annotate | Download (5.55 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2007 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.gvsig.raster.grid;
20

    
21
import junit.framework.TestCase;
22

    
23
import org.gvsig.raster.RasterLibrary;
24
import org.gvsig.raster.buffer.RasterBufferInvalidAccessException;
25
import org.gvsig.raster.buffer.RasterBufferInvalidException;
26
import org.gvsig.raster.dataset.IBuffer;
27
import org.gvsig.raster.grid.Grid;
28
import org.gvsig.raster.grid.GridExtent;
29
import org.gvsig.raster.grid.GridInterpolated;
30
import org.gvsig.raster.grid.OutOfGridException;
31

    
32
/**
33
 * Este test prueba el acceso a datos a traves de un grid.
34
 * 1-Crear? un buffer vacio para escritura con la extensi?n pasado por par?metro. El extent
35
 * completo ser? igual que el extent de la vista asignada (SIN INTERPOLACI?N).
36
 * 2-Comprueba que se calcule bien el n?mero de pixels
37
 * 3-Asigna datos al grid
38
 * 4-Los recupera comprobando que est?n bien asignados
39
 * 
40
 * 1-Crear? un buffer vacio para escritura con la extensi?n pasado por par?metro. El extent
41
 * completo ser? distinto que el extent de la vista asignada (CON INTERPOLACI?N).
42
 * 2-Comprueba que se calcule bien el n?mero de pixels
43
 * 3-Asigna datos al grid
44
 * 4-Los recupera con distintos m?todos de interpolaci?n comprobando que est?n bien asignados
45
 * 
46
 * @author Nacho Brodin (nachobrodin@gmail.com)
47
 *
48
 */
49
public class TGEmptyBufferForWrite extends TestCase{
50
        
51
        static{
52
                RasterLibrary.wakeUp();        
53
        }
54
        
55
        public void setUp() {
56
                System.err.println("TGEmptyBufferForWrite running...");
57
        }
58
        
59
        public void start(){
60
                this.setUp();
61
                this.testStack();
62
        }
63
        
64
        public void testStack(){
65
                try {
66
                        //SIN INTERPOLACI?N
67
                        GridExtent layerExtent = new GridExtent(1000, 1000, 1500, 1500, 50);
68
                        Grid g = new Grid(layerExtent, layerExtent, IBuffer.TYPE_INT, new int[]{0, 1, 2});
69

    
70
                        //Tama?o en pixels
71
                        assertEquals(layerExtent.getNX(), 10);
72
                        assertEquals(layerExtent.getNY(), 10);
73
                        assertEquals(g.getNX(), 10);
74
                        assertEquals(g.getNY(), 10);
75
                        
76
                        for (int i = 0; i < g.getNX(); i++) {
77
                                for (int j = 0; j < g.getNY(); j++)
78
                                        g.setCellValue(j, i, (int)(j * i));
79
                        }
80
                        //print(g);
81
                        for (int i = 0; i < g.getNX(); i++) {
82
                                for (int j = 0; j < g.getNY(); j++)
83
                                        assertEquals(g.getCellValueAsInt(j, i), (int)(j * i));
84
                        }
85
                        
86
                        //CON INTERPOLACI?N
87
                        GridExtent windowExtent = new GridExtent(1125, 1125, 1375, 1375, 50);
88
                        layerExtent = new GridExtent(1000, 1000, 1500, 1500, 50);
89
                        g = new Grid(layerExtent, windowExtent, IBuffer.TYPE_INT, new int[]{0, 1, 2});
90

    
91
                        //Tama?o en pixels
92
                        assertEquals(layerExtent.getNX(), 10);
93
                        assertEquals(layerExtent.getNY(), 10);
94
                        assertEquals(g.getNX(), 5);
95
                        assertEquals(g.getNY(), 5);
96
                        
97
                        for (int i = 0; i < g.getLayerNX(); i++) {
98
                                for (int j = 0; j < g.getLayerNY(); j++)
99
                                        g.setCellValue(j, i, (int)(j * i));
100
                        }
101
                        g.setInterpolationMethod(GridInterpolated.INTERPOLATION_BicubicSpline);
102
                        int[][] m1 = new int[][]{{9, 12, 15, 18, 21}, {12, 16, 20, 24, 28}, {15, 20, 25, 30, 35}, {18, 24, 30, 36, 42}, {21, 28, 35, 42, 49}};
103
                        //print(g);
104
                        compare(m1, g);
105
                        
106
                        g.setInterpolationMethod(GridInterpolated.INTERPOLATION_NearestNeighbour);
107
                        //print(g);
108
                        int[][] m2 = new int[][]{{9, 12, 15, 18, 21}, {12, 16, 20, 24, 28}, {15, 20, 25, 30, 35}, {18, 24, 30, 36, 42}, {21, 28, 35, 42, 49}};
109
                        compare(m2, g);
110
                        
111
                        g.setInterpolationMethod(GridInterpolated.INTERPOLATION_Bilinear);
112
                        //print(g);
113
                        int[][] m3 = new int[][]{{9, 12, 15, 18, 21}, {12, 16, 20, 24, 28}, {15, 20, 25, 30, 35}, {18, 24, 30, 36, 42}, {21, 28, 35, 42, 49}};
114
                        compare(m3, g);
115
                        
116
                        g.setInterpolationMethod(GridInterpolated.INTERPOLATION_BSpline);
117
                        //print(g);
118
                        int[][] m4 = new int[][]{{9, 11, 14, 18, 21}, {12, 15, 19, 23, 27}, {14, 20, 24, 29, 35}, {18, 23, 30, 36, 41}, {21, 27, 35, 42, 49}};
119
                        compare(m4, g);
120
                        
121
                        g.setInterpolationMethod(GridInterpolated.INTERPOLATION_InverseDistance);
122
                        //print(g);
123
                        int[][] m5 = new int[][]{{9, 12, 15, 18, 21}, {12, 16, 20, 24, 28}, {15, 20, 25, 30, 35}, {18, 24, 30, 36, 42}, {21, 28, 35, 42, 49}};
124
                        compare(m5, g);
125
                                                
126
                } catch (RasterBufferInvalidException e1) {
127
                        e1.printStackTrace();
128
                }  catch (OutOfGridException e3) {
129
                        e3.printStackTrace();
130
                } catch (RasterBufferInvalidAccessException e) {
131
                        e.printStackTrace();
132
                }
133
        }
134
        
135
        private void compare(int[][] m, Grid g) throws RasterBufferInvalidAccessException {
136
                for(int line = 0; line < g.getNY(); line++){
137
                        for(int col = 0; col < g.getNX(); col++)
138
                                assertEquals(g.getCellValueAsInt(col, line), m[line][col]);
139
                }
140
        }
141
        
142
        /**
143
         * Imprime todos los pixels de la fuente de datos en RGB
144
         * @throws RasterBufferInvalidAccessException 
145
         */
146
        private void print(Grid g) throws RasterBufferInvalidAccessException {
147
                for(int line = 0; line < g.getNY(); line++){
148
                        for(int col = 0; col < g.getNX(); col++)
149
                                System.out.print(g.getCellValueAsInt(col, line) + " ");
150
                        System.out.println();
151
                }
152
                System.out.println();
153
        }
154
        
155
}