root / branches / v10 / libraries / libCq_CMS_praster / src / org / cresques / io / data / CutRasterProcess.java @ 10953
History | View | Annotate | Download (8.19 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 |
package org.cresques.io.data; |
20 |
|
21 |
import java.awt.geom.Rectangle2D; |
22 |
import java.io.IOException; |
23 |
|
24 |
import org.cresques.geo.ViewPortData; |
25 |
import org.cresques.io.GeoRasterWriter; |
26 |
import org.cresques.io.IDataWriter; |
27 |
import org.cresques.px.Extent; |
28 |
import org.cresques.util.Utilities; |
29 |
|
30 |
|
31 |
/**
|
32 |
* Clase que realiza el proceso de recorte de un raster a partir de unos datos
|
33 |
* de entrada. Para realizar este recorte necesita la extensi?n o coordenadas
|
34 |
* pixel de la imagen, el tama?o del recorte, el n?mero de bandas que intervienen
|
35 |
* desde la original y si se desea crear un raster por banda o todos en un mismo
|
36 |
* raster. Como fuente de datos de entrada estar? el grid que a trav?s de un proceso
|
37 |
* que muestra el incremento de la operaci?n crea el/los recorte/s. Debemos indicarle
|
38 |
* tambi?n el directorio donde queremos crear el/los raster y el nombre del mismo.
|
39 |
*
|
40 |
* En caso de generar varios (uno por banda) pondr? el mismo nomnbre a todos pero
|
41 |
* numerandolos (raster_1.tif, raster_2.tif, ...)
|
42 |
*
|
43 |
* @author Nacho Brodin (nachobrodin@gmail.com)
|
44 |
*/
|
45 |
public class CutRasterProcess extends Thread { |
46 |
|
47 |
/**
|
48 |
* Escritores para salvar los raster a disco. Uno por raster a salvar.
|
49 |
*/
|
50 |
private GeoRasterWriter[] writers = null; |
51 |
/**
|
52 |
* Bandas que se salvan y en el orden que se hace. Cada posici?n del array es
|
53 |
* una banda que se salvar? y el elemento que contiene el n?mero de banda del
|
54 |
* grid que va en esa posici?n.
|
55 |
*/
|
56 |
private int[] bands = null; |
57 |
/**
|
58 |
* Datos fuente
|
59 |
*/
|
60 |
private Grid grid = null; |
61 |
/**
|
62 |
* Nombre de el/los ficheros donde se salva.
|
63 |
*/
|
64 |
private volatile String[] fileNames = null; |
65 |
|
66 |
private ViewPortData vpData = null; |
67 |
/**
|
68 |
* Rasterizador de bandas
|
69 |
*/
|
70 |
private RasterizeGrid[] rasterizer = null; |
71 |
/**
|
72 |
* Clase que instancia a esta y que pone en marcha el thread para generar los recortes.
|
73 |
*/
|
74 |
private ICutRaster parent = null; |
75 |
/**
|
76 |
* Ancho y alto del grid resultante
|
77 |
*/
|
78 |
//private int width = 0, height = 0;
|
79 |
/**
|
80 |
* Informa de si hay un fichero por banda o todas las bandas en un fichero. True en el primer caso
|
81 |
* y false en el segundo.
|
82 |
*/
|
83 |
private boolean aFilePerBand = false; |
84 |
/**
|
85 |
* Nombre del fichero destino. En caso de crearse un fichero por banda, se numerar?n.
|
86 |
* (raster_1.tif, raster_2.tif, ...)
|
87 |
*/
|
88 |
private String fileName = null; |
89 |
|
90 |
/**
|
91 |
* Constructor cuando la selecci?n se hace por coordenadas pixel
|
92 |
* @param pixelCoord Coordenadas pixeles que representan los margenes del recorte
|
93 |
* @param bands Lista de bandas a salvar. Cada posici?n del array es una banda que se
|
94 |
* salvar? y el elemento que contiene el n?mero de banda del grid que va en esa posici?n
|
95 |
* por lo que si tenemos un grid de 3 bandas y queremos hacer un recorte con solo la
|
96 |
* banda tres pasaremos un array {3}
|
97 |
* @param grid Grid con los datos fuente
|
98 |
* @param fileName Nombre del fichero destino. En caso de crearse un fichero por banda,
|
99 |
* se numerar?n. (raster_1.tif, raster_2.tif, ...)
|
100 |
* @param aFilePerBand Variable que dice si hay un fichero por banda o todas las bandas
|
101 |
* van en un fichero. En el primer caso valdr? true y en el segundo false.
|
102 |
* @param w Ancho en pixeles del raster resultante
|
103 |
* @param h Alto en pixeles del raster resultante
|
104 |
* @param parent Objeto que implementa el interfaz ITailRaster que tiene los m?todos que
|
105 |
* son llamados cuando se finaliza el proceso de recortado.
|
106 |
*/
|
107 |
public CutRasterProcess(int[] pixelCoord, int[] bands, Grid grid, String fileName, boolean aFilePerBand, int w, int h, ICutRaster parent){ |
108 |
this.bands = bands;
|
109 |
this.grid = grid;
|
110 |
this.parent = parent;
|
111 |
this.aFilePerBand = aFilePerBand;
|
112 |
this.fileName = fileName;
|
113 |
|
114 |
grid.clearDrawableBand(); |
115 |
grid.addDrawableBands(bands); |
116 |
grid.setAreaOfInterest(pixelCoord[0], pixelCoord[1], w, h); |
117 |
|
118 |
//Hallamos primero las coordenadas del mapa que corresponden a esos pixeles y calculamos con ellos el extent
|
119 |
Rectangle2D extent = Utilities.getMapRectFromPxRect( grid.getExtent().toRectangle2D(), |
120 |
grid.getWidth(), grid.getHeight(), |
121 |
new Rectangle2D.Double(pixelCoord[0], pixelCoord[1], w, h)); |
122 |
|
123 |
vpData = new ViewPortData(null, new Extent(extent), null); |
124 |
|
125 |
//createWriters(grid.getRasterBuf());
|
126 |
} |
127 |
|
128 |
/**
|
129 |
* Constructor cuando la selecci?n se hace por coordenadas reales
|
130 |
* @param mapCoord Coordenadas del mundo real que representan los margenes del recorte
|
131 |
* @param bands Lista de bandas a salvar. Cada posici?n del array es una banda que se
|
132 |
* salvar? y el elemento que contiene el n?mero de banda del grid que va en esa posici?n
|
133 |
* por lo que si tenemos un grid de 3 bandas y queremos hacer un recorte con solo la
|
134 |
* banda tres pasaremos un array {3}
|
135 |
* @param grid Grid con los datos fuente
|
136 |
* @param fileName Nombre del fichero destino. En caso de crearse un fichero por banda,
|
137 |
* se numerar?n. (raster_1.tif, raster_2.tif, ...)
|
138 |
* @param aFilePerBand Variable que dice si hay un fichero por banda o todas las bandas
|
139 |
* van en un fichero. En el primer caso valdr? true y en el segundo false.
|
140 |
* @param w Ancho en pixeles del raster resultante
|
141 |
* @param h Alto en pixeles del raster resultante
|
142 |
* @param parent Objeto que implementa el interfaz ITailRaster que tiene los m?todos que
|
143 |
* son llamados cuando se finaliza el proceso de recortado.
|
144 |
*/
|
145 |
public CutRasterProcess(double[] mapCoord, int[] bands, Grid grid, String fileName, boolean aFilePerBand, int w, int h, ICutRaster parent){ |
146 |
this.bands = bands;
|
147 |
this.grid = grid;
|
148 |
this.parent = parent;
|
149 |
this.aFilePerBand = aFilePerBand;
|
150 |
this.fileName = fileName;
|
151 |
|
152 |
grid.clearDrawableBand(); |
153 |
grid.addDrawableBands(bands); |
154 |
grid.setAreaOfInterest(mapCoord[0], mapCoord[1], Math.abs(mapCoord[2] - mapCoord[0]), Math.abs(mapCoord[3] - mapCoord[1])); |
155 |
|
156 |
//Calculamos el Extent con las coordenadas del mapa pasadas
|
157 |
vpData = new ViewPortData(null, new Extent(mapCoord[0], mapCoord[1], mapCoord[2], mapCoord[3]), null); |
158 |
|
159 |
//createWriters(grid.getRasterBuf());
|
160 |
} |
161 |
|
162 |
/**
|
163 |
* Creador de writers. Genera uno por fichero a salvar. Si hay un solo fichero se pone
|
164 |
* el nombre pasado por par?metro, si hay uno por bandas ser? el nombre m?s un n?mero de
|
165 |
* secuencia (raster_1.tif, raster_2.tif, ...)
|
166 |
* @param rasterBuf Buffer desde donde se obtienen los datos.
|
167 |
*/
|
168 |
public String[] createWriters(RasterBuf rasterBuf, int w, int h){ |
169 |
if(aFilePerBand){
|
170 |
rasterizer = new RasterizeGrid[bands.length];
|
171 |
fileNames = new String[bands.length]; |
172 |
writers = new GeoRasterWriter[bands.length];
|
173 |
|
174 |
for(int iFile = 0; iFile < bands.length; iFile ++){ |
175 |
rasterizer[iFile] = new RasterizeGrid(rasterBuf, iFile);
|
176 |
fileNames[iFile] = fileName + "_" + iFile + ".tif"; |
177 |
writers[iFile] = GeoRasterWriter.getWriter((IDataWriter)rasterizer[iFile], fileNames[iFile], RasterizeGrid.BLOCKSIZE, |
178 |
1, vpData, 0, w, h, grid.getDataType()); |
179 |
} |
180 |
}else{
|
181 |
rasterizer = new RasterizeGrid[1]; |
182 |
rasterizer[0] = new RasterizeGrid(rasterBuf, bands); |
183 |
fileNames = new String[1]; |
184 |
fileNames[0] = fileName + ".tif"; |
185 |
writers = new GeoRasterWriter[1]; |
186 |
writers[0] = GeoRasterWriter.getWriter((IDataWriter)rasterizer[0], fileNames[0], RasterizeGrid.BLOCKSIZE, |
187 |
bands.length, vpData, 0, w, h, grid.getDataType());
|
188 |
} |
189 |
return fileNames;
|
190 |
} |
191 |
|
192 |
/**
|
193 |
* Proceso que escribe la/las imagen/es
|
194 |
*/
|
195 |
public synchronized void run(){ |
196 |
try{
|
197 |
for(int i = 0; i < writers.length; i ++){ |
198 |
writers[i].dataWrite(); |
199 |
writers[i].writeClose(); |
200 |
} |
201 |
}catch(IOException ev){ |
202 |
return;
|
203 |
} |
204 |
parent.cutFinalize(fileNames); |
205 |
} |
206 |
} |