svn-gvsig-desktop / branches / v10 / libraries / libFMap / src / com / iver / cit / gvsig / fmap / drivers / raster / CmsRasterDriver.java @ 9056
History | View | Annotate | Download (12.5 KB)
1 |
/*
|
---|---|
2 |
* Created on 17-dic-2004
|
3 |
*/
|
4 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
5 |
*
|
6 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
7 |
*
|
8 |
* This program is free software; you can redistribute it and/or
|
9 |
* modify it under the terms of the GNU General Public License
|
10 |
* as published by the Free Software Foundation; either version 2
|
11 |
* of the License, or (at your option) any later version.
|
12 |
*
|
13 |
* This program is distributed in the hope that it will be useful,
|
14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
16 |
* GNU General Public License for more details.
|
17 |
*
|
18 |
* You should have received a copy of the GNU General Public License
|
19 |
* along with this program; if not, write to the Free Software
|
20 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
21 |
*
|
22 |
* For more information, contact:
|
23 |
*
|
24 |
* Generalitat Valenciana
|
25 |
* Conselleria d'Infraestructures i Transport
|
26 |
* Av. Blasco Ib??ez, 50
|
27 |
* 46010 VALENCIA
|
28 |
* SPAIN
|
29 |
*
|
30 |
* +34 963862235
|
31 |
* gvsig@gva.es
|
32 |
* www.gvsig.gva.es
|
33 |
*
|
34 |
* or
|
35 |
*
|
36 |
* IVER T.I. S.A
|
37 |
* Salamanca 50
|
38 |
* 46005 Valencia
|
39 |
* Spain
|
40 |
*
|
41 |
* +34 963163400
|
42 |
* dac@iver.es
|
43 |
*/
|
44 |
package com.iver.cit.gvsig.fmap.drivers.raster; |
45 |
|
46 |
import java.awt.Dimension; |
47 |
import java.awt.Graphics2D; |
48 |
import java.awt.Image; |
49 |
import java.awt.geom.AffineTransform; |
50 |
import java.awt.geom.Rectangle2D; |
51 |
import java.awt.image.BufferedImage; |
52 |
import java.awt.image.DataBuffer; |
53 |
import java.io.File; |
54 |
import java.io.IOException; |
55 |
import java.util.ArrayList; |
56 |
|
57 |
import org.cresques.cts.ICoordTrans; |
58 |
import org.cresques.cts.IProjection; |
59 |
import org.cresques.filter.RasterFilterStack; |
60 |
import org.cresques.geo.ViewPortData; |
61 |
import org.cresques.io.ErmapperWriter; |
62 |
import org.cresques.io.GdalFile; |
63 |
import org.cresques.io.GdalWriter; |
64 |
import org.cresques.io.GeoRasterFile; |
65 |
import org.cresques.io.JpegWriter; |
66 |
import org.cresques.io.data.Grid; |
67 |
import org.cresques.px.Extent; |
68 |
import org.cresques.px.PxRaster; |
69 |
|
70 |
import com.iver.cit.gvsig.fmap.ViewPort; |
71 |
import com.iver.cit.gvsig.fmap.drivers.DriverIOException; |
72 |
import com.iver.cit.gvsig.fmap.drivers.GeorreferencedRasterDriver; |
73 |
|
74 |
/**
|
75 |
* Driver de raster (tal y como los abre CMS.
|
76 |
* Primera aproximaci?n al raster. Borrador para revisar.
|
77 |
*
|
78 |
* @author Luis W. Sevilla (sevilla_lui@gva.es)
|
79 |
*/
|
80 |
public class CmsRasterDriver implements GeorreferencedRasterDriver { |
81 |
private File file = null; |
82 |
private IProjection proj = null; |
83 |
private GeoRasterFile rasterFile = null; |
84 |
private PxRaster raster = null; |
85 |
private Extent tempExtent = null; |
86 |
private Grid grid = null; |
87 |
|
88 |
int trans = 255; |
89 |
|
90 |
static {
|
91 |
// Fuerza la carga del soporte de img (gdal):
|
92 |
System.err.println("CmsRasterDriver: inicializando"); |
93 |
Class c[] = {GdalFile.class, GdalWriter.class, ErmapperWriter.class, JpegWriter.class}; |
94 |
} |
95 |
/* (non-Javadoc)
|
96 |
* @see com.hardcode.driverManager.Driver#getName()
|
97 |
*/
|
98 |
public String getName() { |
99 |
return "gvSIG Image Driver"; |
100 |
} |
101 |
|
102 |
/**
|
103 |
* @see com.iver.cit.gvsig.fmap.drivers.RasterDriver#open(java.io.File)
|
104 |
*/
|
105 |
public void open(File f) throws IOException { |
106 |
file = f; |
107 |
} |
108 |
|
109 |
/* (non-Javadoc)
|
110 |
* @see com.iver.cit.gvsig.fmap.drivers.RasterDriver#close()
|
111 |
*/
|
112 |
public void close() throws IOException { |
113 |
rasterFile.close(); |
114 |
} |
115 |
|
116 |
/* (non-Javadoc)
|
117 |
* @see com.iver.cit.gvsig.fmap.drivers.RasterDriver#initialize()
|
118 |
*/
|
119 |
public void initialize() throws IOException { |
120 |
if (proj != null){ |
121 |
String fName = file.getAbsolutePath();
|
122 |
int test = fName.indexOf("ecwp:"); |
123 |
|
124 |
if (test != -1) { |
125 |
String urlECW = fName.substring(test + 6); |
126 |
//urlECW.replaceAll("\\", "/");
|
127 |
fName = "ecwp://" + urlECW;
|
128 |
System.err.println(test+" "+fName); |
129 |
} |
130 |
rasterFile = GeoRasterFile.openFile(proj, fName); |
131 |
if(rasterFile == null) |
132 |
throw new IOException("Formato no valido"); |
133 |
grid = new Grid(rasterFile);
|
134 |
createPxRaster(); |
135 |
} else
|
136 |
throw new IOException("Proyecci?n no asignada"); |
137 |
System.out.println(getName()+": Inicializado (con PxRaster)"); |
138 |
} |
139 |
|
140 |
/**
|
141 |
* @see com.iver.cit.gvsig.fmap.drivers.GeorreferencedRasterDriver#initialize(org.cresques.cts.IProjection)
|
142 |
*/
|
143 |
public void initialize(IProjection proj) throws IOException { |
144 |
this.proj = proj;
|
145 |
rasterFile = GeoRasterFile.openFile(proj, file.getAbsolutePath()); |
146 |
if(rasterFile == null) |
147 |
return;
|
148 |
grid = new Grid(rasterFile);
|
149 |
createPxRaster(); |
150 |
} |
151 |
|
152 |
private void createPxRaster() { |
153 |
raster = new PxRaster(rasterFile, null, rasterFile.getExtent()); |
154 |
raster.setTransparency(false);
|
155 |
grid.addRenderizer(raster); |
156 |
} |
157 |
|
158 |
/**
|
159 |
* A?ade un fichero al PxRaste
|
160 |
* @param fileName Nombre del fichero a a?adir
|
161 |
*/
|
162 |
public void addFile(String fileName){ |
163 |
if(raster!=null){ |
164 |
GeoRasterFile grf = raster.addFile(fileName); |
165 |
grid.addFile(grf); |
166 |
} |
167 |
} |
168 |
|
169 |
/**
|
170 |
* Elimina un fichero al PxRaste
|
171 |
* @param fileName Nombre del fichero a a?adir
|
172 |
*/
|
173 |
public void delFile(String fileName){ |
174 |
if(raster!=null){ |
175 |
GeoRasterFile grf = raster.delFile(fileName); |
176 |
grid.removeFile(grf); |
177 |
} |
178 |
} |
179 |
|
180 |
/*
|
181 |
* @see com.iver.cit.gvsig.fmap.drivers.GeorreferencedRasterDriver#getFullExtent()
|
182 |
*/
|
183 |
public Rectangle2D getFullExtent() { |
184 |
return rasterFile.getExtent().toRectangle2D();
|
185 |
} |
186 |
|
187 |
public int getTransparency() { |
188 |
return 255-(raster.getAlpha()); |
189 |
} |
190 |
|
191 |
public void setTransparency(int trans) { |
192 |
this.trans = trans;
|
193 |
if (raster != null) { |
194 |
raster.setTransparency(trans); |
195 |
} |
196 |
} |
197 |
|
198 |
/* (non-Javadoc)
|
199 |
* @see com.iver.cit.gvsig.fmap.drivers.RasterDriver#draw(java.awt.image.BufferedImage, java.awt.Graphics2D, com.iver.cit.gvsig.fmap.ViewPort)
|
200 |
*/
|
201 |
public void draw(BufferedImage image, Graphics2D g, ViewPort vp) throws DriverIOException { |
202 |
//Comentamos temporalmente el c?digo de Vicente y restauramos el que habia
|
203 |
/*Dimension imgSz=new Dimension(image.getWidth(),image.getHeight());
|
204 |
Rectangle2D r=vp.getAdjustedExtent();
|
205 |
Extent e=new Extent(r);*/
|
206 |
Extent e = new Extent(vp.getAdjustedExtent());
|
207 |
Dimension imgSz = vp.getImageSize();
|
208 |
ViewPortData vp2 = new ViewPortData(vp.getProjection(), e, imgSz );
|
209 |
vp2.setMat(vp.getAffineTransform()); |
210 |
raster.draw(g, vp2); |
211 |
} |
212 |
|
213 |
|
214 |
/*
|
215 |
* @see com.iver.cit.gvsig.fmap.drivers.GeorreferencedRasterDriver#getProjection()
|
216 |
*/
|
217 |
public IProjection getProjection() {
|
218 |
return proj;
|
219 |
} |
220 |
|
221 |
/* (non-Javadoc)
|
222 |
* @see com.iver.cit.gvsig.fmap.drivers.GeorreferencedRasterDriver#setProjection(org.cresques.cts.IProjection)
|
223 |
*/
|
224 |
public void setProjection(IProjection proj) { |
225 |
this.proj = proj;
|
226 |
} |
227 |
|
228 |
/*
|
229 |
* @see com.iver.cit.gvsig.fmap.drivers.RasterDriver#getNumBands()
|
230 |
*/
|
231 |
public int getNumBands() { |
232 |
return rasterFile.getBandCount();
|
233 |
} |
234 |
|
235 |
/* (non-Javadoc)
|
236 |
* @see com.iver.cit.gvsig.fmap.drivers.RasterDriver#getPixel(int, int, byte[])
|
237 |
*/
|
238 |
public byte[] getPixel(int x, int y, byte[] dArray) { |
239 |
// TODO Auto-generated method stub
|
240 |
return null; |
241 |
} |
242 |
|
243 |
/* (non-Javadoc)
|
244 |
* @see com.iver.cit.gvsig.fmap.drivers.RasterDriver#getPixel(int, int, int[])
|
245 |
*/
|
246 |
public int[] getPixel(int x, int y, int[] dArray) { |
247 |
// TODO Auto-generated method stub
|
248 |
return null; |
249 |
} |
250 |
|
251 |
/* (non-Javadoc)
|
252 |
* @see com.iver.cit.gvsig.fmap.drivers.RasterDriver#getRasterDataType()
|
253 |
*/
|
254 |
public int getRasterDataType() { |
255 |
return raster.getDataType();
|
256 |
} |
257 |
|
258 |
/* (non-Javadoc)
|
259 |
* @see com.iver.cit.gvsig.fmap.drivers.RasterDriver#getData(int, int, int)
|
260 |
*/
|
261 |
public Object getData(int x, int y, int band) { |
262 |
return raster.getFiles()[0].getData(x, y, band); |
263 |
} |
264 |
|
265 |
/* (non-Javadoc)
|
266 |
* @see com.iver.cit.gvsig.fmap.drivers.RasterDriver#getDataAsByte(int, int, int)
|
267 |
*/
|
268 |
public byte getDataAsByte(int x, int y, int band) { |
269 |
// TODO Auto-generated method stub
|
270 |
return 0; |
271 |
} |
272 |
|
273 |
/* (non-Javadoc)
|
274 |
* @see com.iver.cit.gvsig.fmap.drivers.RasterDriver#getDataAsFloat(int, int, int)
|
275 |
*/
|
276 |
public float getDataAsFloat(int x, int y, int band) { |
277 |
// TODO Auto-generated method stub
|
278 |
return 0; |
279 |
} |
280 |
|
281 |
/* (non-Javadoc)
|
282 |
* @see com.iver.cit.gvsig.fmap.drivers.RasterDriver#getDataAsDouble(int, int, int)
|
283 |
*/
|
284 |
public double getDataAsDouble(int x, int y, int band) { |
285 |
// TODO Auto-generated method stub
|
286 |
return 0; |
287 |
} |
288 |
|
289 |
/* (non-Javadoc)
|
290 |
* @see com.iver.cit.gvsig.fmap.drivers.RasterDriver#getDataAsInt(int, int, int)
|
291 |
*/
|
292 |
public int getDataAsInt(int x, int y, int band) { |
293 |
// TODO Auto-generated method stub
|
294 |
return 0; |
295 |
} |
296 |
|
297 |
/* (non-Javadoc)
|
298 |
* @see com.iver.cit.gvsig.fmap.drivers.RasterDriver#fileAccepted(java.io.File)
|
299 |
*/
|
300 |
public boolean fileAccepted(File file) { |
301 |
return GeoRasterFile.fileIsSupported(file.getName());
|
302 |
} |
303 |
|
304 |
/* (non-Javadoc)
|
305 |
* @see com.iver.cit.gvsig.fmap.drivers.RasterDriver#getImage(java.awt.Dimension, java.awt.geom.Rectangle2D, org.cresques.cts.ICoordTrans)
|
306 |
*/
|
307 |
public Image getImage(Dimension size, Rectangle2D userSize, ICoordTrans rp) { |
308 |
// TODO Auto-generated method stub
|
309 |
return null; |
310 |
} |
311 |
|
312 |
/* (non-Javadoc)
|
313 |
* @see com.iver.cit.gvsig.fmap.drivers.RasterDriver#getAttributes()
|
314 |
* ?AVISO! Variar los tipos de datos devueltos puede hacer que alguna extensi?n
|
315 |
* no funcione bien. Se pueden a?adir atributos pero no es recomendable quitar ninguno.
|
316 |
*/
|
317 |
public ArrayList getAttributes() { |
318 |
ArrayList attr = new ArrayList(); |
319 |
String dataType = "Byte"; |
320 |
if (rasterFile.getDataType() == DataBuffer.TYPE_BYTE) dataType = "Byte"; |
321 |
else if (rasterFile.getDataType() == DataBuffer.TYPE_SHORT) |
322 |
dataType = "Short";
|
323 |
else if (rasterFile.getDataType() == DataBuffer.TYPE_USHORT) |
324 |
dataType = "Unsigned Short";
|
325 |
else if (rasterFile.getDataType() == DataBuffer.TYPE_INT) |
326 |
dataType = "Integer";
|
327 |
else if (rasterFile.getDataType() == DataBuffer.TYPE_FLOAT) |
328 |
dataType = "Float";
|
329 |
else if (rasterFile.getDataType() == DataBuffer.TYPE_DOUBLE) |
330 |
dataType = "Double";
|
331 |
else
|
332 |
dataType = "Unknown";
|
333 |
Object [][] a = { |
334 |
{"Filename",rasterFile.getName()},
|
335 |
{"Filesize",new Long(rasterFile.getFileSize())}, |
336 |
{"Width",new Integer(rasterFile.getWidth())}, |
337 |
{"Height", new Integer(rasterFile.getHeight())}, |
338 |
{"Bands", new Integer(rasterFile.getBandCount())} |
339 |
// {"BandDataType", dataType}
|
340 |
}; |
341 |
for (int i=0; i<a.length; i++) |
342 |
attr.add(a[i]); |
343 |
return attr;
|
344 |
} |
345 |
|
346 |
/**
|
347 |
* Devuelve el colorBand activo en la banda especificada.
|
348 |
* @param flag banda.
|
349 |
* @return color de banda activo
|
350 |
*/
|
351 |
public int getBand(int flag){ |
352 |
return raster.getBand(flag);
|
353 |
} |
354 |
|
355 |
/**
|
356 |
* Devuelve la posici?n del fichero para la banda especificada.
|
357 |
* @param flag banda.
|
358 |
* @return posici?n del fichero
|
359 |
*/
|
360 |
public int getPosFile(int flag){ |
361 |
return raster.getPosFile(flag);
|
362 |
} |
363 |
|
364 |
/**
|
365 |
* Activa o desactiva la transparencia
|
366 |
* @param t true activa la transparencia y false la desactiva
|
367 |
*/
|
368 |
public void setTransparency(boolean t){ |
369 |
raster.setTransparency(t); |
370 |
} |
371 |
|
372 |
/**
|
373 |
* Asocia un colorBand al rojo, verde o azul.
|
374 |
* @param flag cual (o cuales) de las bandas.
|
375 |
* @param nBand que colorBand
|
376 |
*/
|
377 |
public void setBand(int flag, int nBand){ |
378 |
raster.setBand(flag, nBand); |
379 |
} |
380 |
|
381 |
/**
|
382 |
* Obtiene la pila de filtros
|
383 |
* @return pila de filtros
|
384 |
*/
|
385 |
public RasterFilterStack getFilterStack(){
|
386 |
return raster.filterStack;
|
387 |
} |
388 |
|
389 |
/**
|
390 |
* Asigna la pila de filtros
|
391 |
* @return pila de filtros
|
392 |
*/
|
393 |
public void setFilterStack(RasterFilterStack stack){ |
394 |
raster.filterStack = stack; |
395 |
} |
396 |
|
397 |
/**
|
398 |
* Obtiene el valor del pixel del Image en la posici?n x,y
|
399 |
* @param x Posici?n x
|
400 |
* @param y Posici?n y
|
401 |
* @return valor de pixel
|
402 |
*/
|
403 |
public int[] getPixel(double wcx, double wcy){ |
404 |
return raster.getPixel(wcx, wcy);
|
405 |
} |
406 |
|
407 |
/**
|
408 |
*
|
409 |
*/
|
410 |
public GeoRasterFile [] getFiles(){ |
411 |
return raster.getFiles();
|
412 |
} |
413 |
|
414 |
/**
|
415 |
* Obtiene el flag que dice si la imagen est? o no georreferenciada
|
416 |
* @return true si est? georreferenciada y false si no lo est?.
|
417 |
*/
|
418 |
public boolean isGeoreferenced() { |
419 |
return rasterFile.isGeoreferenced();
|
420 |
} |
421 |
|
422 |
/**
|
423 |
* Asigna una transformaci?n al georrasterfile para la transformar la
|
424 |
* asignaci?n de setView. Del mismo modo asigna los extents a PxRaster para
|
425 |
* transformar la visualizaci?n.
|
426 |
* @param t Matriz de transformaci?n
|
427 |
*/
|
428 |
public void setAffineTransform(AffineTransform t){ |
429 |
for (int i = 0; i < raster.getFiles().length; i++) { |
430 |
raster.getFiles()[i].setAffineTransform(t); |
431 |
raster.setExtent(raster.getFiles()[i].getExtent()); |
432 |
raster.setExtentForRequest(raster.getFiles()[i].getExtentForRequest()); |
433 |
} |
434 |
} |
435 |
|
436 |
public AffineTransform getAffineTransform(){ |
437 |
return raster.getFiles()[0].getAffineTransform(); |
438 |
} |
439 |
|
440 |
/**
|
441 |
* Obtiene el grid asociado
|
442 |
* @return grid
|
443 |
*/
|
444 |
public Grid getGrid(){
|
445 |
return grid;
|
446 |
} |
447 |
|
448 |
/**
|
449 |
?* It returns a new file created from the selected
|
450 |
* raster file. e.g: When the user name chooses a
|
451 |
* RAW file the file to open is a VRT file. This
|
452 |
* method creates the new file and returns its name.
|
453 |
* @param file
|
454 |
* Selected File
|
455 |
* @return
|
456 |
* New File to load
|
457 |
*/
|
458 |
public File getTransformedFile(File file){ |
459 |
return null; |
460 |
} |
461 |
} |