svn-gvsig-desktop / tags / v1_0_RELEASE / libraries / libjni-gdal-macosx / src-test / Writetif.java @ 43469
History | View | Annotate | Download (4.74 KB)
1 |
/**********************************************************************
|
---|---|
2 |
* $Id: Writetif.java 8767 2006-11-15 04:11:32Z jjdelcerro $
|
3 |
*
|
4 |
* Name: writetif.java
|
5 |
* Project: gvSIG - Generalitat Valenciana SIG
|
6 |
* Purpose: Escribe una im?gen en formato geotiff a partir de otra en
|
7 |
* otro formato. Los par?metros necesarios son im?gen fuente, ancho
|
8 |
* y alto de la escena a convertir.
|
9 |
* java Writetif imagen_src.sid 2000 1500
|
10 |
* Author: Nacho Brodin, brodin_ign@gva.es
|
11 |
*
|
12 |
**********************************************************************/
|
13 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
14 |
*
|
15 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
16 |
*
|
17 |
* This program is free software; you can redistribute it and/or
|
18 |
* modify it under the terms of the GNU General Public License
|
19 |
* as published by the Free Software Foundation; either version 2
|
20 |
* of the License, or (at your option) any later version.
|
21 |
*
|
22 |
* This program is distributed in the hope that it will be useful,
|
23 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
24 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
25 |
* GNU General Public License for more details.
|
26 |
*
|
27 |
* You should have received a copy of the GNU General Public License
|
28 |
* along with this program; if not, write to the Free Software
|
29 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
30 |
*
|
31 |
* For more information, contact:
|
32 |
*
|
33 |
* Generalitat Valenciana
|
34 |
* Conselleria d'Infraestructures i Transport
|
35 |
* Av. Blasco Ib??ez, 50
|
36 |
* 46010 VALENCIA
|
37 |
* SPAIN
|
38 |
*
|
39 |
* +34 963862235
|
40 |
* gvsig@gva.es
|
41 |
* www.gvsig.gva.es
|
42 |
*
|
43 |
* or
|
44 |
*
|
45 |
* IVER T.I. S.A
|
46 |
* Salamanca 50
|
47 |
* 46005 Valencia
|
48 |
* Spain
|
49 |
*
|
50 |
* +34 963163400
|
51 |
* dac@iver.es
|
52 |
*/
|
53 |
|
54 |
import es.gva.cit.jgdal.*; |
55 |
import es.gva.cit.jogr.*; |
56 |
|
57 |
public class Writetif{ |
58 |
public static void main(String[] args){ |
59 |
|
60 |
|
61 |
Gdal migdal=new Gdal();
|
62 |
Gdal dset_dstno=null;
|
63 |
GeoTransform gt=null;
|
64 |
int ancho=0,alto=0; |
65 |
GdalRasterBand mirasterband=null;
|
66 |
GdalRasterBand rband=null;
|
67 |
int nxsize=0; |
68 |
int nysize=0; |
69 |
int rastercount=0; |
70 |
float total=0; |
71 |
|
72 |
|
73 |
try{
|
74 |
if(args.length==3){ |
75 |
migdal.open(args[0],Gdal.GA_ReadOnly);
|
76 |
ancho=Integer.parseInt(args[1]); |
77 |
alto=Integer.parseInt(args[2]); |
78 |
}else {
|
79 |
System.out.println("Params: filename Xsize Ysize"); |
80 |
System.exit(1); |
81 |
} |
82 |
}catch(Exception ge){ |
83 |
ge.printStackTrace(); |
84 |
//...
|
85 |
} |
86 |
|
87 |
try{
|
88 |
gt=migdal.getGeoTransform(); |
89 |
System.out.println("Origin = ("+gt.adfgeotransform[0]+","+gt.adfgeotransform[3]+")"); |
90 |
System.out.println("Pixel Size = ("+gt.adfgeotransform[1]+","+gt.adfgeotransform[5]+")"); |
91 |
}catch(GdalException e){
|
92 |
System.out.println("I can't obtain the array geoTransform for this image"); |
93 |
e.printStackTrace(); |
94 |
//...........
|
95 |
} |
96 |
|
97 |
try{
|
98 |
rastercount=migdal.getRasterCount(); |
99 |
nxsize = migdal.getRasterXSize(); |
100 |
nysize = migdal.getRasterYSize(); |
101 |
System.out.println("N BANDAS="+rastercount+" SIZEX="+nxsize+" SIZEY="+nysize); |
102 |
System.out.println("WINDOWX="+ancho+" WINDOWY="+alto); |
103 |
System.out.println(migdal.getProjectionRef());
|
104 |
|
105 |
|
106 |
int ngcp=migdal.getGCPCount();
|
107 |
System.out.println("NGCP="+ngcp); |
108 |
|
109 |
}catch(GdalException ge){
|
110 |
ge.printStackTrace(); |
111 |
//...
|
112 |
} |
113 |
|
114 |
|
115 |
|
116 |
|
117 |
try{
|
118 |
//Obtenemos el driver y creamos el dataset del destino
|
119 |
|
120 |
GdalDriver drv=Gdal.getDriverByName("GTiff");
|
121 |
String[] params={"TILED=YES","PHOTOMETRIC=RGB","TFW=WORLDFILE"}; |
122 |
dset_dstno=drv.create("salidatif.tif",ancho,alto,rastercount,Gdal.GDT_Byte,null); |
123 |
|
124 |
//A?adimos la informaci?n de proyecci?n (esta es de ejemplo. Hay que formatear la de la im?gen de entrada)
|
125 |
|
126 |
//OGRSpatialReference oSRS=new OGRSpatialReference();
|
127 |
//oSRS.setUTM(11,true);
|
128 |
//oSRS.setWellKnownGeogCS("NAD27");
|
129 |
//System.out.println("Nueva Proyecci?n ==> "+oSRS.exportToWkt());
|
130 |
//dset_dstno.setProjection(oSRS.exportToWkt());
|
131 |
|
132 |
|
133 |
//A?adimos el vector geoTransform de la imag?n fuente a la de destino
|
134 |
|
135 |
dset_dstno.setGeoTransform(gt); |
136 |
|
137 |
GdalBuffer buf=null;
|
138 |
for(int iBand=0;iBand<rastercount;iBand++){ |
139 |
|
140 |
System.out.println("Lectura/Escritura banda..."+iBand); |
141 |
mirasterband=migdal.getRasterBand(iBand+1);
|
142 |
|
143 |
rband=dset_dstno.getRasterBand(iBand+1);
|
144 |
|
145 |
buf = mirasterband.readRaster(0, 0, ancho, alto, ancho, alto, Gdal.GDT_Byte); |
146 |
|
147 |
long inicio=System.currentTimeMillis(); |
148 |
rband.writeRaster(0,0, ancho, alto, buf,Gdal.GDT_Byte); |
149 |
long fin=System.currentTimeMillis(); |
150 |
total+=(fin-inicio); |
151 |
|
152 |
} |
153 |
|
154 |
System.out.println("TIEMPO DE ESCRITURA ="+(total/1000)); |
155 |
dset_dstno.close(); |
156 |
migdal.close(); |
157 |
//oSRS=null;
|
158 |
|
159 |
}catch(Exception e){ |
160 |
e.printStackTrace( ); |
161 |
//...
|
162 |
} |
163 |
|
164 |
|
165 |
} |
166 |
} |