Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libjni-gdal / src-test / Writetif.java @ 829

History | View | Annotate | Download (3.43 KB)

1
/**********************************************************************
2
 * $Id: Writetif.java 804 2004-12-30 13:16:13Z igbrotru $
3
 *
4
 * Name:     writetif.java
5
 * Project:  gvSIG - Generalitat Valenciana SIG
6
 * Purpose:  Ejemplo para la prueba del interfaz java de la libreria 
7
 *                         gdal de Frank Warmerdam
8
 * Author:   Nacho Brodin, brodin_ign@gva.es
9
 *
10
 **********************************************************************/
11

    
12
import es.gva.cit.jgdal.*;
13

    
14
public class Writetif{
15
        public static void main(String[] args){
16
 
17
          
18
          Gdal migdal=new Gdal();
19
          Gdal dset_dstno=null;
20
          GeoTransform gt=null;
21
          int ancho=0,alto=0;
22
          GdalRasterBand mirasterband=null;
23
          GdalRasterBand rband=null;
24
          int nxsize=0;
25
          int nysize=0;
26
          int rastercount=0;
27
          float total=0;
28
          
29
          
30
          try{
31
            if(args.length==3){
32
                    migdal.open(args[0],Gdal.GA_ReadOnly);
33
                    ancho=Integer.parseInt(args[1]);
34
                    alto=Integer.parseInt(args[2]);
35
            }else {
36
                    System.out.println("Params: filename Xsize Ysize"); 
37
                    System.exit(1);
38
            }
39
          }catch(Exception ge){
40
                        ge.printStackTrace();
41
                        //...
42
          }
43

    
44
          try{
45
                  gt=migdal.getGeoTransform();
46
                  System.out.println("Origin = ("+gt.adfgeotransform[0]+","+gt.adfgeotransform[3]+")");
47
                  System.out.println("Pixel Size = ("+gt.adfgeotransform[1]+","+gt.adfgeotransform[5]+")");
48
          }catch(GdalException e){
49
                          System.out.println("I can't obtain the array geoTransform for this image");
50
                        e.printStackTrace();
51
                        //...........
52
          }
53
          
54
          try{
55
                  rastercount=migdal.getRasterCount();
56
            nxsize = migdal.getRasterXSize();
57
            nysize = migdal.getRasterYSize();
58
            System.out.println("N BANDAS="+rastercount+" SIZEX="+nxsize+" SIZEY="+nysize);
59
            System.out.println("WINDOWX="+ancho+" WINDOWY="+alto);
60
            System.out.println(migdal.getProjectionRef());
61
            
62
            
63
            int ngcp=migdal.getGCPCount();
64
            System.out.println("NGCP="+ngcp);
65
            
66
          }catch(GdalException ge){
67
                                                          ge.printStackTrace();
68
                                                          //...
69
                                                     }
70
          
71
          
72
         
73
          
74
          try{
75
                          //Obtenemos el driver y creamos el dataset del destino
76

    
77
                          GdalDriver drv=Gdal.getDriverByName("GTiff");
78
                          String[] params={"TILED=YES","PHOTOMETRIC=RGB","TFW=WORLDFILE"};
79
                          dset_dstno=drv.create("salidatif.tif",ancho,alto,rastercount,Gdal.GDT_Byte,null);
80
                          
81
                          //A?adimos la informaci?n de proyecci?n (esta es de ejemplo. Hay que formatear la de la im?gen de entrada)
82
                          
83
                          OGRSpatialReference oSRS=new OGRSpatialReference();
84
                          oSRS.setUTM(11,true);
85
                          oSRS.setWellKnownGeogCS("NAD27");
86
                          System.out.println("Nueva Proyecci?n ==> "+oSRS.exportToWkt());
87
                          dset_dstno.setProjection(oSRS.exportToWkt());
88
                          
89
                          
90
                          //A?adimos el vector geoTransform de la imag?n fuente a la de destino
91
                          
92
                          dset_dstno.setGeoTransform(gt);
93
                                  
94
                          byte[] buf=null;
95
                          for(int iBand=0;iBand<rastercount;iBand++){
96
                                  
97
                                  System.out.println("Lectura/Escritura banda..."+iBand);
98
                                  mirasterband=migdal.getRasterBand(iBand+1);
99
                                  
100
                                  rband=dset_dstno.getRasterBand(iBand+1);
101
                                                                    
102
                                  buf = mirasterband.readRaster(0, 0, ancho, alto, ancho, alto, Gdal.GDT_Byte);
103
                          
104
                                  long inicio=System.currentTimeMillis();                          
105
                                  rband.writeRaster(0,0, ancho, alto, buf,Gdal.GDT_Byte);
106
                                  long fin=System.currentTimeMillis();
107
                                  total+=(fin-inicio);                 
108
                                                            
109
                          }
110
                  
111
                          System.out.println("TIEMPO DE ESCRITURA ="+(total/1000));                          
112
                          dset_dstno.close();
113
                          migdal.close();
114
                          oSRS=null;
115
                  
116
                  }catch(Exception e){
117
                          e.printStackTrace( );
118
                          //...
119
                  }
120
                  
121
          
122
        }  
123
}