Statistics
| Revision:

root / trunk / libraries / libRaster / src / org / gvsig / raster / grid / roi / RasterROI.java @ 27361

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

    
20
package org.gvsig.raster.grid.roi;
21

    
22
import org.gvsig.raster.buffer.RasterBufferInvalidAccessException;
23
import org.gvsig.raster.buffer.RasterBufferInvalidException;
24
import org.gvsig.raster.dataset.IBuffer;
25
import org.gvsig.raster.grid.Grid;
26
import org.gvsig.raster.grid.GridException;
27
import org.gvsig.raster.grid.OutOfGridException;
28

    
29
/**
30
 * Clase que representa una regi?n de interes conformada por
31
 * un raster. 
32
 * 
33
 * @author Diego Guerrero Sevilla (diego.guerrero@uclm.es)
34
 *
35
 */
36
public class RasterROI extends ROI {
37
        
38
        /**
39
         * Grid que define el ROI a modo de m?scara
40
         * (NO_DATA -> no isInGrid)
41
         */
42
        private Grid maskGrid = null;
43

    
44
        public RasterROI(Grid grid) {
45
                super(grid);
46
                int bands[] = {1};
47
                try {
48
                        maskGrid = new Grid(grid.getGridExtent(), grid.getGridExtent(), IBuffer.TYPE_INT, bands);
49
                        getGridExtent().setXRange(maskGrid.getGridExtent().getMin().getX(), maskGrid.getGridExtent().getMax().getX());
50
                        getGridExtent().setYRange(maskGrid.getGridExtent().getMin().getY(), maskGrid.getGridExtent().getMax().getY());
51
                        for (int x = 0; x < maskGrid.getNX();x++)
52
                                for (int y = 0; y<maskGrid.getNY();y++)
53
                                        maskGrid.setNoData(x, y);
54
                } catch (RasterBufferInvalidException e) {
55
                        // TODO Auto-generated catch block
56
                        e.printStackTrace();
57
                } catch (InterruptedException e) {
58
                }
59
        }
60

    
61
        public boolean isInGrid(int x, int y) {
62
                try {
63
                        return (maskGrid.isInGrid(x, y) && !maskGrid.isNoDataValue(getValue(x, y)));
64
                } catch (GridException e) {
65
                        return false;
66
                } catch (InterruptedException e) {
67
                        return false;
68
                }
69
                
70
        }
71

    
72
        /**
73
         * 
74
         * @return Grid que define la ROI
75
         */
76
        public Grid getMaskGrid() {
77
                return maskGrid;
78
        }
79
        
80
        /**
81
         * Obtiene un valor de una celda del grid independientemente del tipo de dato.
82
         * @param x Posici?n en X
83
         * @param y Posici?n en Y
84
         * @return Valor de la celda en formato double
85
         * @throws InterruptedException 
86
         * @throws RasterBufferInvalidAccessException 
87
         * @throws RasterBufferInvalidAccessException
88
         */
89
        private double getValue(int x, int y) throws GridException, InterruptedException {
90
                switch(getMaskGrid().getDataType()) {
91
                case IBuffer.TYPE_BYTE:return (double)(getMaskGrid().getCellValueAsByte(x, y));
92
                case IBuffer.TYPE_SHORT:return (double)(getMaskGrid().getCellValueAsShort(x, y));
93
                case IBuffer.TYPE_INT:return (double)(getMaskGrid().getCellValueAsInt(x, y));
94
                case IBuffer.TYPE_FLOAT:return (double)getMaskGrid().getCellValueAsFloat(x, y);
95
                case IBuffer.TYPE_DOUBLE:return (double)getMaskGrid().getCellValueAsDouble(x, y);
96
                }
97
                return 0;
98
        }
99
        
100
        /**
101
         * A?ade el pixel con coordenadas <code>x</code>,<code>y</code> (coordenadas pixel) al ROI
102
         * 
103
         * @param x
104
         * @param y
105
         */
106
        public void addPoint(int x, int y) {
107
                try {
108
                        maskGrid.setCellValue(x, y, 1);
109
                        getStatistic().setStatisticsCalculated(false);
110
                        getStatistic().setAdvancedStatisticCalculated(false);
111
                } catch (OutOfGridException e) {
112
                        // TODO Auto-generated catch block
113
                        e.printStackTrace();
114
                } catch (InterruptedException e) {
115
                }
116
        }
117
        
118
        /**
119
         * Excluye al pixel con coordenadas <code>x</code>,<code>y</code> de su participaci?n en la ROI.
120
         * 
121
         * @param x
122
         * @param y
123
         * @throws InterruptedException 
124
         */
125
        public void removePoint(int x, int y) throws InterruptedException{
126
                        maskGrid.setNoData(x, y);
127
                        getStatistic().setStatisticsCalculated(false);
128
                        getStatistic().setAdvancedStatisticCalculated(false);
129
        }
130
        
131
        /**
132
         * Excluye a todos los p?xeles de la ROI
133
         * @throws InterruptedException 
134
         *
135
         */
136
        public void clear() throws InterruptedException{
137
                for(int x = 0; x <         getGridExtent().getNX(); x++)
138
                        for(int y = 0; y < getGridExtent().getNY(); y++)
139
                                maskGrid.setNoData(x, y);
140
                getStatistic().setStatisticsCalculated(false);
141
                getStatistic().setAdvancedStatisticCalculated(false);
142
        }
143

    
144
        /*
145
         * (non-Javadoc)
146
         * @see org.gvsig.raster.grid.roi.ROI#isInside(double, double, double, double)
147
         */
148
        public boolean isInside(double x, double y, double w, double h) {
149
                return false;
150
        }
151

    
152
}