Revision 3578 trunk/libraries/libCq CMS for java.old/src/org/cresques/io/GdalWriter.java
GdalWriter.java | ||
---|---|---|
23 | 23 |
*/ |
24 | 24 |
package org.cresques.io; |
25 | 25 |
|
26 |
import java.io.BufferedOutputStream; |
|
27 |
import java.io.DataOutputStream; |
|
28 |
import java.io.File; |
|
29 |
import java.io.FileOutputStream; |
|
30 |
import java.io.IOException; |
|
31 |
|
|
32 |
import org.cresques.cts.IProjection; |
|
33 |
import org.cresques.px.PxRaster; |
|
34 |
|
|
26 | 35 |
import es.gva.cit.jecwcompress.EcwException; |
27 | 36 |
import es.gva.cit.jgdal.Gdal; |
28 | 37 |
import es.gva.cit.jgdal.GdalBuffer; |
... | ... | |
32 | 41 |
import es.gva.cit.jgdal.GeoTransform; |
33 | 42 |
import es.gva.cit.jogr.OGRSpatialReference; |
34 | 43 |
|
35 |
import org.cresques.px.PxRaster; |
|
36 | 44 |
|
37 |
import java.io.IOException; |
|
38 |
|
|
39 |
|
|
40 | 45 |
/** |
41 | 46 |
* Driver para la escritura a trav?s de Gdal. |
42 | 47 |
* Puede exportar un fichero de un formato a otro desde un GeoRasterFile |
... | ... | |
55 | 60 |
public class GdalWriter extends GeoRasterWriter { |
56 | 61 |
static { |
57 | 62 |
GeoRasterWriter.registerWriterExtension("tif", GdalWriter.class); |
63 |
GeoRasterWriter.registerWriterExtension("jpg", GdalWriter.class); |
|
64 |
GeoRasterWriter.registerWriterExtension("jpeg", GdalWriter.class); |
|
58 | 65 |
} |
59 | 66 |
|
60 | 67 |
public final int windowSizeX = 386; |
... | ... | |
66 | 73 |
private Gdal dset_destino = null; |
67 | 74 |
private GdalRasterBand rband = null; |
68 | 75 |
private String drvType = null; //Tipo de driver |
69 |
private String[] supportedDrv = { "GTiff" }; //Lista de drivers de escritura que soporta |
|
76 |
private String[] supportedDrv = { "GTiff", "JPEG" }; //Lista de drivers de escritura que soporta
|
|
70 | 77 |
private GeoTransform geot = null; //Datos de georeferenciaci?n |
71 | 78 |
private OGRSpatialReference oSRS; //Datos de proyecci?n |
72 | 79 |
private GdalBuffer buf = null; //Buffer de origen de gdal |
... | ... | |
86 | 93 |
public GdalWriter(String drvType) { |
87 | 94 |
this.support = new GdalSupportOptions(drvType); |
88 | 95 |
this.ident = this.drvType = drvType; |
89 |
this.driver = "tif"; |
|
96 |
if(drvType.equals("GTiff")) |
|
97 |
this.driver = "tif"; |
|
98 |
else if(drvType.equals("JPEG")) |
|
99 |
this.driver = "jpg"; |
|
90 | 100 |
this.support.setBlockSize(64); |
91 | 101 |
this.support.setPhotometric("RGB"); |
92 | 102 |
this.support.setInterleave("BAND"); |
... | ... | |
108 | 118 |
String drvType) throws GdalException, IOException { |
109 | 119 |
this.support = new GdalSupportOptions(drvType); |
110 | 120 |
this.ident = this.drvType = drvType; |
111 |
this.driver = "tif"; |
|
112 |
|
|
121 |
if(drvType.equals("GTiff")) |
|
122 |
this.driver = "tif"; |
|
123 |
else if(drvType.equals("JPEG")) |
|
124 |
this.driver = "jpg"; |
|
125 |
|
|
113 | 126 |
this.outfilename = outfilename; |
114 | 127 |
this.infilename = infilename; |
115 | 128 |
this.currentRaster = raster; |
... | ... | |
134 | 147 |
|
135 | 148 |
nBands = currentRaster.getBandCount(); |
136 | 149 |
|
137 |
this.support.setBlockSize(currentRaster.getBlockSize());
|
|
150 |
this.support.setBlockSize(64/*currentRaster.getBlockSize()*/);
|
|
138 | 151 |
|
139 | 152 |
if ((sizeWindowX < 0) || (sizeWindowY < 0)) { |
140 | 153 |
throw new IOException("Tama?o del fichero de salida erroneo."); |
... | ... | |
187 | 200 |
throws GdalException, IOException { |
188 | 201 |
this.support = new GdalSupportOptions(drvType); |
189 | 202 |
this.ident = this.drvType = drvType; |
190 |
this.driver = "tif"; |
|
203 |
if(drvType.equals("GTiff")) |
|
204 |
this.driver = "tif"; |
|
205 |
else if(drvType.equals("JPEG")) |
|
206 |
this.driver = "jpg"; |
|
191 | 207 |
|
192 | 208 |
this.dataWriter = dataWriter; |
193 | 209 |
this.outfilename = outFilename; |
... | ... | |
286 | 302 |
} |
287 | 303 |
|
288 | 304 |
//Obtenemos el driver y creamos el dataset del destino |
305 |
drv = Gdal.getDriverByName(drvType); |
|
289 | 306 |
|
290 |
drv = Gdal.getDriverByName(drvType); |
|
291 |
|
|
307 |
/*try{ |
|
308 |
drv = Gdal.getDriverByName("JPEG"); |
|
309 |
Gdal src = new Gdal(); |
|
310 |
src.open("/home/nacho/imagenes/q101866.tif", 0); |
|
311 |
drv.createCopy( "/home/nacho/outgvGIG.jpg", src, false ); |
|
312 |
}catch(IOException e){} |
|
313 |
return;*/ |
|
314 |
|
|
292 | 315 |
//System.out.println("++++++>"+params.length); |
293 | 316 |
//for(int i=0;i<params.length;i++)System.out.println("=====>"+params[i]); |
317 |
|
|
294 | 318 |
if (dset_destino != null) { |
295 | 319 |
dset_destino.close(); |
296 | 320 |
dset_destino = null; |
... | ... | |
513 | 537 |
} |
514 | 538 |
|
515 | 539 |
/** |
540 |
* Realiza una copia en el formato especificado. |
|
541 |
* @throws IOException |
|
542 |
*/ |
|
543 |
public static void createCopy(GdalDriver driverDst, String dst, String src, boolean bstrict, String[] params, IProjection proj) throws IOException, GdalException { |
|
544 |
if (dst == null || src == null) { |
|
545 |
throw new IOException("No se ha asignado un fichero de entrada."); |
|
546 |
} |
|
547 |
|
|
548 |
GdalFile gdalFile = new GdalFile(proj, src); |
|
549 |
driverDst.createCopy(dst, gdalFile.file, bstrict, params); |
|
550 |
if(dst.endsWith(".jpg") || dst.endsWith(".jpeg")) |
|
551 |
GdalWriter.createWorldFile(dst, gdalFile); |
|
552 |
gdalFile.close(); |
|
553 |
} |
|
554 |
|
|
555 |
/** |
|
556 |
* Crea un fichero de georeferenciaci?n |
|
557 |
* @param img |
|
558 |
* @param name |
|
559 |
* @return |
|
560 |
* @throws IOException |
|
561 |
*/ |
|
562 |
private static void createWorldFile(String name, GdalFile gdalFile) throws IOException{ |
|
563 |
File tfw = null; |
|
564 |
|
|
565 |
String extWorldFile = ".wld"; |
|
566 |
if(name.endsWith("tif")) |
|
567 |
extWorldFile = ".tfw"; |
|
568 |
if(name.endsWith("jpg") || name.endsWith("jpeg")) |
|
569 |
extWorldFile = ".jpgw"; |
|
570 |
|
|
571 |
tfw = new File(name.substring(0, name.lastIndexOf(".")) + extWorldFile); |
|
572 |
|
|
573 |
//Generamos un world file para gdal |
|
574 |
DataOutputStream dos = new DataOutputStream( new BufferedOutputStream(new FileOutputStream(tfw)) ); |
|
575 |
dos.writeBytes((gdalFile.getExtent().getMax().getX() - gdalFile.getExtent().getMin().getX())/gdalFile.getWidth()+"\n"); |
|
576 |
dos.writeBytes("0.0\n"); |
|
577 |
dos.writeBytes("0.0\n"); |
|
578 |
dos.writeBytes((gdalFile.getExtent().getMax().getY() - gdalFile.getExtent().getMin().getY())/gdalFile.getHeight()+"\n"); |
|
579 |
dos.writeBytes(""+gdalFile.getExtent().getMin().getX()+"\n"); |
|
580 |
dos.writeBytes(""+gdalFile.getExtent().getMin().getY()+"\n"); |
|
581 |
dos.close(); |
|
582 |
} |
|
583 |
|
|
584 |
|
|
585 |
/** |
|
516 | 586 |
* Realiza la escritura de datos con los datos que le pasa el cliente. |
517 | 587 |
* @throws IOException |
518 | 588 |
*/ |
Also available in: Unified diff