svn-gvsig-desktop / tags / v1_1_1_Build_1019 / libraries / libjni-ecwcompress / src-test / EcwBandasLandsat.java @ 28163
History | View | Annotate | Download (4.67 KB)
1 | 1937 | igbrotru | import es.gva.cit.jecwcompress.*; |
---|---|---|---|
2 | import es.gva.cit.jgdal.*; |
||
3 | |||
4 | public class EcwBandasLandsat{ |
||
5 | |||
6 | public static void Uso(){ |
||
7 | System.out.println("Uso 1: Comprime una ventana de la imagen."); |
||
8 | System.out.println(" java TifToEcw file-src file-dst compresi?n num_bandas(3 ? 5)"); |
||
9 | } |
||
10 | |||
11 | public static void main(String[] args){ |
||
12 | |||
13 | Gdal[] gdalimgs=null; |
||
14 | |||
15 | int nbandas=0; |
||
16 | |||
17 | NCSEcwCompressClient client; |
||
18 | ReadImgs lectura=null;
|
||
19 | String[] images; |
||
20 | |||
21 | if(args.length != 3){ |
||
22 | EcwBandasLandsat.Uso(); |
||
23 | return;
|
||
24 | } |
||
25 | |||
26 | nbandas = Integer.parseInt(args[2]); |
||
27 | if(nbandas!=3 && nbandas!=6){ |
||
28 | EcwBandasLandsat.Uso(); |
||
29 | return;
|
||
30 | } |
||
31 | |||
32 | |||
33 | images=new String[nbandas]; |
||
34 | gdalimgs =new Gdal[nbandas];
|
||
35 | for(int i=0;i<nbandas;i++)gdalimgs[i]=new Gdal(); |
||
36 | |||
37 | if(nbandas==6){ |
||
38 | for(int i=0;i<nbandas-1;i++) |
||
39 | images[i] = new String(args[0]+(i+1)+"0.tif"); |
||
40 | images[5] = new String(args[0]+"70.tif"); |
||
41 | }else if(nbandas==3){ |
||
42 | images[0] = new String(args[0]+"30.tif"); |
||
43 | images[1] = new String(args[0]+"20.tif"); |
||
44 | images[2] = new String(args[0]+"10.tif"); |
||
45 | } |
||
46 | |||
47 | try{
|
||
48 | for(int i=0;i<nbandas;i++){ |
||
49 | gdalimgs[i].open(images[i],Gdal.GA_ReadOnly); |
||
50 | System.out.println(images[i]);
|
||
51 | } |
||
52 | }catch(Exception ge){ |
||
53 | ge.printStackTrace(); |
||
54 | //...........
|
||
55 | } |
||
56 | |||
57 | GdalRasterBand mirasterband=null;
|
||
58 | String st;
|
||
59 | int nxsize=0; |
||
60 | int nysize=0; |
||
61 | int widthcompress = 0; |
||
62 | int heightcompress = 0; |
||
63 | int rastercount=nbandas;
|
||
64 | String rasterband;
|
||
65 | GeoTransform gt=null;
|
||
66 | |||
67 | try{
|
||
68 | nxsize = gdalimgs[0].getRasterXSize();
|
||
69 | nysize = gdalimgs[0].getRasterYSize();
|
||
70 | System.out.println("N BANDAS="+rastercount+" SIZEX="+nxsize+" SIZEY="+nysize); |
||
71 | }catch(GdalException ge){
|
||
72 | ge.printStackTrace(); |
||
73 | //...........
|
||
74 | } |
||
75 | |||
76 | widthcompress = nxsize; |
||
77 | heightcompress = nysize; |
||
78 | |||
79 | try{
|
||
80 | gt = gdalimgs[0].getGeoTransform();
|
||
81 | System.out.println("Origins = ("+gt.adfgeotransform[0]+","+gt.adfgeotransform[3]+")"); |
||
82 | System.out.println("Pixel Size = ("+gt.adfgeotransform[1]+","+gt.adfgeotransform[5]+")"); |
||
83 | }catch(GdalException e){
|
||
84 | System.out.println("No puedo obtener el array geoTransform."); |
||
85 | e.printStackTrace(); |
||
86 | //...........
|
||
87 | } |
||
88 | |||
89 | try{
|
||
90 | client = new NCSEcwCompressClient();
|
||
91 | |||
92 | if(nbandas==6)client.setOutputFilename(args[0]+"123457multibanda.ecw"); |
||
93 | else if(nbandas==3)client.setOutputFilename(args[0]+"321multibanda.ecw"); |
||
94 | client.setInputFilename("");
|
||
95 | client.setTargetCompress(Double.parseDouble(args[1])); |
||
96 | client.setInOutSizeX(widthcompress); |
||
97 | client.setInOutSizeY(heightcompress); |
||
98 | client.setInputBands(rastercount); |
||
99 | |||
100 | |||
101 | client.setCompressFormat(CompressFormat.COMPRESS_MULTI); |
||
102 | |||
103 | |||
104 | client.setCellIncrementX(gt.adfgeotransform[1]);
|
||
105 | client.setCellIncrementY(gt.adfgeotransform[5]);
|
||
106 | client.setOriginX(gt.adfgeotransform[0]);
|
||
107 | client.setOriginY(gt.adfgeotransform[3]);
|
||
108 | client.setCellSizeUnits(1);
|
||
109 | |||
110 | |||
111 | lectura = new ReadImgs(gdalimgs, client, widthcompress, heightcompress, nbandas);
|
||
112 | client.NCSEcwCompressOpen(false);
|
||
113 | client.NCSEcwCompress(lectura); |
||
114 | client.NCSEcwCompressClose(); |
||
115 | |||
116 | }catch(EcwException e){
|
||
117 | e.printStackTrace(); |
||
118 | //...........
|
||
119 | } |
||
120 | |||
121 | |||
122 | try{
|
||
123 | for(int i=0;i<nbandas;i++)gdalimgs[i].close(); |
||
124 | }catch(GdalException e){
|
||
125 | e.printStackTrace(); |
||
126 | } |
||
127 | |||
128 | |||
129 | } |
||
130 | } |
||
131 | |||
132 | /**
|
||
133 | *
|
||
134 | * Para la lectura hay que hacer una clase que extienda de JniObject e implemente ReadCallBack
|
||
135 | * . Esto obliga a crear un m?todo loadBuffer. En el hay que meter la funcionalidad para que
|
||
136 | * llene el buffer. El buffer esta en la clase cliente y tendr? una longitud de
|
||
137 | * ancho de una l?nea * n?mero de bandas
|
||
138 | */
|
||
139 | |||
140 | class ReadImgs extends JniObject implements ReadCallBack { |
||
141 | |||
142 | private GdalBuffer[] buffers; |
||
143 | private GdalRasterBand mirasterband=null; |
||
144 | private Gdal[] gdalimgs=null; |
||
145 | private NCSEcwCompressClient client=null; |
||
146 | private int width, height; |
||
147 | |||
148 | public ReadImgs(Gdal[] g, NCSEcwCompressClient client, int width, int height, int nbandas){ |
||
149 | gdalimgs = g; |
||
150 | this.client = client;
|
||
151 | this.width = width;
|
||
152 | this.height = height;
|
||
153 | buffers = new GdalBuffer[nbandas];
|
||
154 | |||
155 | } |
||
156 | |||
157 | public void loadBuffer(){ |
||
158 | |||
159 | try{
|
||
160 | for(int iBand=0;iBand<gdalimgs.length;iBand++){ |
||
161 | mirasterband=gdalimgs[iBand].getRasterBand(1);
|
||
162 | buffers[iBand] = mirasterband.readRaster(0, nNextLine, width, 1, width, 1, 1); |
||
163 | for(int pos=0; pos<width; pos++){ |
||
164 | client.buffer[pos+(width*iBand)]=buffers[iBand].buffByte[pos]; |
||
165 | } |
||
166 | } |
||
167 | }catch(GdalException e){
|
||
168 | System.out.println("Error en la lectura de lineas con Gdal."); |
||
169 | System.exit(1); |
||
170 | } |
||
171 | |||
172 | |||
173 | |||
174 | } |
||
175 | |||
176 | public void updatePercent(){ |
||
177 | System.out.println(client.getPercent()+"%"); |
||
178 | } |
||
179 | } |