Statistics
| Revision:

root / trunk / libraries / libRaster / src-test / org / gvsig / raster / grid / TGOperations.java @ 12128

History | View | Annotate | Download (3.99 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

    
28
/**
29
 * Testea las operaciones de Grid. Las operaciones testeadas son las siguiente:
30
 * <UL>
31
 * <LI>Multiplica el grid por un valor</LI>
32
 * <LI>Suma de grids</LI>
33
 * <LI>Sobreescritura de un grid con los datos de otro</LI>
34
 * <LI>Ordenar celdas</LI>
35
 * <LI>Media, m?nimo y m?ximo de un grid</LI>
36
 * </UL>
37
 * 
38
 * @author Nacho Brodin (nachobrodin@gmail.com)
39
 *
40
 */
41
public class TGOperations extends TestCase{
42
        
43
        static{
44
                RasterLibrary.wakeUp();        
45
        }
46
        
47
        public void setUp() {
48
                System.err.println("TGOperations running...");
49
        }
50
        
51
        public void start(){
52
                this.setUp();
53
                this.testStack();
54
        }
55
        
56
        public void testStack(){
57
                try {
58
                        GridExtent layerExtent = new GridExtent(1000, 1000, 1500, 1500, 100);
59
                        Grid g1 = new Grid(layerExtent, layerExtent, IBuffer.TYPE_INT, new int[]{0, 1, 2});
60
                        init(g1);
61
                        
62
                        //Multiplicar por un entero
63
                        g1.multiply(2);
64
                        int[][] m1 = new int[][]{{0, 2, 4, 6, 8}, {2, 4, 6, 8, 10}, {4, 6, 8, 10, 12}, {6, 8, 10, 12, 14}, {8, 10, 12, 14, 16}};
65
                        compare(m1, g1);
66
                                        
67
                        //Sumar dos grids
68
                        Grid g2 = new Grid(layerExtent, layerExtent, IBuffer.TYPE_INT, new int[]{0, 1, 2});
69
                        init(g1);
70
                        init(g2);
71
                        g1.add(g2);
72
                        compare(m1, g1);
73
                        
74
                        //Sobreescritura
75
                        g2.assign(g1);
76
                        compare(m1, g1);
77
                        //print(g1);
78
                        
79
                        //Celdas ordenadas
80
                        GridCell[] cells = g1.getSortedArrayOfCells();
81
                        int[] values = new int[]{0, 2, 2, 4, 4, 4, 6, 6, 6, 6, 8, 8, 8, 8, 8, 10, 10, 10, 10, 12, 12, 12, 14, 14, 16};
82
                        compareArray(values, cells);
83
                        
84
                        //Media de un grid (getMeanValue)
85
                        init(g1);
86
                        g1.setBandToOperate(0);
87
                        assertEquals((int)g1.getMeanValue(), 4);
88
                        assertEquals((int)g1.getMinValue(), 0);
89
                        assertEquals((int)g1.getMaxValue(), 8);
90
                        
91
                        
92
                } catch (RasterBufferInvalidException e1) {
93
                        e1.printStackTrace();
94
                }  catch (OutOfGridException e3) {
95
                        e3.printStackTrace();
96
                } catch (RasterBufferInvalidAccessException e) {
97
                        e.printStackTrace();
98
                }
99
        }
100
        
101
        private void init(Grid g) throws OutOfGridException{
102
                for (int i = 0; i < g.getNX(); i++) {
103
                        for (int j = 0; j < g.getNY(); j++)
104
                                g.setCellValue(j, i, (int)(j + i));
105
                }
106
        }
107
        
108
        private void compare(int[][] m, Grid g) throws RasterBufferInvalidAccessException {
109
                for(int line = 0; line < g.getNY(); line++){
110
                        for(int col = 0; col < g.getNX(); col++)
111
                                assertEquals(g.getCellValueAsInt(col, line), m[line][col]);
112
                }
113
        }
114
        
115
        private void compareArray(int[] a, GridCell[] cells) throws RasterBufferInvalidAccessException {
116
                for (int i = 0; i < cells.length; i++) 
117
                        assertEquals((int)cells[i].getValue(), (int)cells[i].getValue());
118
        }
119
        
120
        /**
121
         * Imprime todos los pixels de la fuente de datos en RGB
122
         * @throws RasterBufferInvalidAccessException 
123
         */
124
        private void print(Grid g) throws RasterBufferInvalidAccessException {
125
                for(int line = 0; line < g.getNY(); line++){
126
                        for(int col = 0; col < g.getNX(); col++)
127
                                System.out.print(g.getCellValueAsInt(col, line) + " ");
128
                        System.out.println();
129
                }
130
                System.out.println();
131
        }
132
        
133
}