root / trunk / libraries / libRaster / src / org / gvsig / raster / grid / roi / RasterROI.java @ 17428
History | View | Annotate | Download (4.21 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 |
} |
58 |
} |
59 |
|
60 |
public boolean isInGrid(int x, int y) { |
61 |
try {
|
62 |
return (maskGrid.isInGrid(x, y) && !maskGrid.isNoDataValue(getValue(x, y)));
|
63 |
} catch (GridException e) {
|
64 |
return false; |
65 |
} |
66 |
|
67 |
} |
68 |
|
69 |
/**
|
70 |
*
|
71 |
* @return Grid que define la ROI
|
72 |
*/
|
73 |
public Grid getMaskGrid() {
|
74 |
return maskGrid;
|
75 |
} |
76 |
|
77 |
/**
|
78 |
* Obtiene un valor de una celda del grid independientemente del tipo de dato.
|
79 |
* @param x Posici?n en X
|
80 |
* @param y Posici?n en Y
|
81 |
* @return Valor de la celda en formato double
|
82 |
* @throws RasterBufferInvalidAccessException
|
83 |
* @throws RasterBufferInvalidAccessException
|
84 |
*/
|
85 |
private double getValue(int x, int y) throws GridException { |
86 |
switch(getMaskGrid().getDataType()) {
|
87 |
case IBuffer.TYPE_BYTE:return (double)(getMaskGrid().getCellValueAsByte(x, y)); |
88 |
case IBuffer.TYPE_SHORT:return (double)(getMaskGrid().getCellValueAsShort(x, y)); |
89 |
case IBuffer.TYPE_INT:return (double)(getMaskGrid().getCellValueAsInt(x, y)); |
90 |
case IBuffer.TYPE_FLOAT:return (double)getMaskGrid().getCellValueAsFloat(x, y); |
91 |
case IBuffer.TYPE_DOUBLE:return (double)getMaskGrid().getCellValueAsDouble(x, y); |
92 |
} |
93 |
return 0; |
94 |
} |
95 |
|
96 |
/**
|
97 |
* A?ade el pixel con coordenadas <code>x</code>,<code>y</code> (coordenadas pixel) al ROI
|
98 |
*
|
99 |
* @param x
|
100 |
* @param y
|
101 |
*/
|
102 |
public void addPoint(int x, int y) { |
103 |
try {
|
104 |
maskGrid.setCellValue(x, y, 1);
|
105 |
getStatistic().setStatisticsCalculated(false);
|
106 |
getStatistic().setAdvancedStatisticCalculated(false);
|
107 |
} catch (OutOfGridException e) {
|
108 |
// TODO Auto-generated catch block
|
109 |
e.printStackTrace(); |
110 |
} |
111 |
} |
112 |
|
113 |
/**
|
114 |
* Excluye al pixel con coordenadas <code>x</code>,<code>y</code> de su participaci?n en la ROI.
|
115 |
*
|
116 |
* @param x
|
117 |
* @param y
|
118 |
*/
|
119 |
public void removePoint(int x, int y){ |
120 |
maskGrid.setNoData(x, y); |
121 |
getStatistic().setStatisticsCalculated(false);
|
122 |
getStatistic().setAdvancedStatisticCalculated(false);
|
123 |
} |
124 |
|
125 |
/**
|
126 |
* Excluye a todos los p?xeles de la ROI
|
127 |
*
|
128 |
*/
|
129 |
public void clear(){ |
130 |
for(int x = 0; x < getGridExtent().getNX(); x++) |
131 |
for(int y = 0; y < getGridExtent().getNY(); y++) |
132 |
maskGrid.setNoData(x, y); |
133 |
getStatistic().setStatisticsCalculated(false);
|
134 |
getStatistic().setAdvancedStatisticCalculated(false);
|
135 |
} |
136 |
|
137 |
} |