root / tags / Root_v05 / libraries / libjni-gdal / src-test / example.java @ 34764
History | View | Annotate | Download (6.05 KB)
1 |
/**********************************************************************
|
---|---|
2 |
* $Id: example.java 3540 2006-01-09 12:51:24Z nacho $
|
3 |
*
|
4 |
* Name: example.java
|
5 |
* Project: JGDAL. Interface java to gdal (Frank Warmerdam).
|
6 |
* Purpose: Example code to test java interface of gdal.
|
7 |
* Author: Nacho Brodin, brodin_ign@gva.es
|
8 |
*
|
9 |
**********************************************************************/
|
10 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
11 |
*
|
12 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
13 |
*
|
14 |
* This program is free software; you can redistribute it and/or
|
15 |
* modify it under the terms of the GNU General Public License
|
16 |
* as published by the Free Software Foundation; either version 2
|
17 |
* of the License, or (at your option) any later version.
|
18 |
*
|
19 |
* This program is distributed in the hope that it will be useful,
|
20 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
21 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
22 |
* GNU General Public License for more details.
|
23 |
*
|
24 |
* You should have received a copy of the GNU General Public License
|
25 |
* along with this program; if not, write to the Free Software
|
26 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
27 |
*
|
28 |
* For more information, contact:
|
29 |
*
|
30 |
* Generalitat Valenciana
|
31 |
* Conselleria d'Infraestructures i Transport
|
32 |
* Av. Blasco Ib??ez, 50
|
33 |
* 46010 VALENCIA
|
34 |
* SPAIN
|
35 |
*
|
36 |
* +34 963862235
|
37 |
* gvsig@gva.es
|
38 |
* www.gvsig.gva.es
|
39 |
*
|
40 |
* or
|
41 |
*
|
42 |
* IVER T.I. S.A
|
43 |
* Salamanca 50
|
44 |
* 46005 Valencia
|
45 |
* Spain
|
46 |
*
|
47 |
* +34 963163400
|
48 |
* dac@iver.es
|
49 |
*/
|
50 |
|
51 |
|
52 |
|
53 |
/**
|
54 |
* Sections that correspond with GDAL Api Tutorial (publicated in
|
55 |
* http://www.remotesensing.org:16080/gdal/gdal_tutorial.html) have
|
56 |
* been marked.
|
57 |
*/
|
58 |
|
59 |
//Opening the file ----------------------------
|
60 |
|
61 |
import es.gva.cit.jgdal.*; |
62 |
|
63 |
import java.io.*; |
64 |
|
65 |
public class example{ |
66 |
public static void main(String[] args){ |
67 |
|
68 |
|
69 |
Gdal migdal=new Gdal();
|
70 |
|
71 |
try{
|
72 |
if(args.length>0) |
73 |
migdal.open(args[0],Gdal.GA_ReadOnly);
|
74 |
else {
|
75 |
System.out.println("You must introduce the file name"); |
76 |
System.exit(1); |
77 |
} |
78 |
}catch(Exception ge){ |
79 |
ge.printStackTrace(); |
80 |
//...........
|
81 |
} |
82 |
|
83 |
//End Opening the file ----------------------------
|
84 |
|
85 |
|
86 |
GdalRasterBand mirasterband=null;
|
87 |
GdalRasterBand mioverview=null;
|
88 |
|
89 |
FileOutputStream s=null; |
90 |
DataOutputStream midataOS=null; |
91 |
BufferedOutputStream miBS=null; |
92 |
String st;
|
93 |
int nxsize=0; |
94 |
int nysize=0; |
95 |
int rastercount=0; |
96 |
String rasterband;
|
97 |
int xoverview=0; |
98 |
int yoverview=0; |
99 |
GeoTransform gt; |
100 |
int noverviews=0; |
101 |
|
102 |
//Getting dataset Information ----------------------------
|
103 |
|
104 |
try{
|
105 |
rastercount=migdal.getRasterCount(); |
106 |
nxsize = migdal.getRasterXSize(); |
107 |
nysize = migdal.getRasterYSize(); |
108 |
System.out.println("N BANDAS="+rastercount+" SIZEX="+nxsize+" SIZEY="+nysize); |
109 |
System.out.println(migdal.getProjectionRef());
|
110 |
}catch(GdalException ge){
|
111 |
ge.printStackTrace(); |
112 |
//...........
|
113 |
} |
114 |
|
115 |
|
116 |
|
117 |
try{
|
118 |
gt=migdal.getGeoTransform(); |
119 |
System.out.println("Origin = ("+gt.adfgeotransform[0]+","+gt.adfgeotransform[3]+")"); |
120 |
System.out.println("Pixel Size = ("+gt.adfgeotransform[1]+","+gt.adfgeotransform[5]+")"); |
121 |
}catch(GdalException e){
|
122 |
System.out.println("I can't obtain the array geoTransform for this image"); |
123 |
e.printStackTrace(); |
124 |
//...........
|
125 |
} |
126 |
|
127 |
//End Getting dataset Information ----------------------------
|
128 |
|
129 |
try{
|
130 |
File mifichero=new File("exit.raw"); |
131 |
s = new FileOutputStream(mifichero); |
132 |
}catch(Exception e){} |
133 |
|
134 |
|
135 |
for(int iBand=0;iBand<rastercount;iBand++){ |
136 |
|
137 |
System.out.println("-------"); |
138 |
|
139 |
//Informacion de banda
|
140 |
|
141 |
|
142 |
//Fetching a Raster Band ----------------------------
|
143 |
//Several binding are missing yet
|
144 |
|
145 |
int tipo=0; |
146 |
try{
|
147 |
mirasterband=migdal.getRasterBand(iBand+1);
|
148 |
System.out.println("SIZE BANDA=("+mirasterband.getRasterBandXSize()+","+mirasterband.getRasterBandYSize()+")"); |
149 |
System.out.println("SIZE BLOQUE BANDA=("+mirasterband.getBlockXSize()+","+mirasterband.getBlockYSize()+")"); |
150 |
noverviews=mirasterband.getOverviewCount(); |
151 |
System.out.println("N? OVERVIEWS="+noverviews); |
152 |
tipo = mirasterband.getRasterDataType(); |
153 |
System.out.println("TIPO DE DATOS BANDA="+tipo); |
154 |
}catch(GdalException e){
|
155 |
e.printStackTrace(); |
156 |
System.exit(1); |
157 |
} |
158 |
|
159 |
//Fetching a Raster Band ----------------------------
|
160 |
|
161 |
|
162 |
System.out.println("BANDA->"+iBand); |
163 |
|
164 |
//Informaci?n de overview
|
165 |
|
166 |
try{
|
167 |
|
168 |
if(noverviews>0){ |
169 |
|
170 |
//Muestra el tama?o de cada overview y selecccionamos el 0
|
171 |
|
172 |
for(int t=0;t<noverviews;t++){ |
173 |
mioverview=mirasterband.getOverview(t); |
174 |
System.out.println("SIZE OVERVIEW=("+mioverview.getRasterBandXSize()+","+mioverview.getRasterBandYSize()+")"); |
175 |
} |
176 |
mioverview=mirasterband.getOverview(0);
|
177 |
xoverview=mioverview.getRasterBandXSize(); |
178 |
yoverview=mioverview.getRasterBandYSize(); |
179 |
}else{
|
180 |
System.out.println("This band haven't overviews"); |
181 |
xoverview=nxsize; |
182 |
yoverview=nysize; |
183 |
mioverview=mirasterband; |
184 |
|
185 |
} |
186 |
|
187 |
}catch(GdalException e){
|
188 |
e.printStackTrace(); |
189 |
System.exit(1); |
190 |
} |
191 |
|
192 |
|
193 |
|
194 |
|
195 |
|
196 |
|
197 |
|
198 |
//byte[] buf=null;
|
199 |
GdalBuffer buf=null;
|
200 |
|
201 |
for(int i=0; i<yoverview; i++){ |
202 |
|
203 |
//Reading Raster Data ----------------------------
|
204 |
|
205 |
try{
|
206 |
buf = mioverview.readRaster(0, i, xoverview, 1, xoverview, 1, tipo); |
207 |
}catch(GdalException e){
|
208 |
System.out.println("Error in BSBReadLine"); |
209 |
System.exit(1); |
210 |
} |
211 |
|
212 |
//End Reading Raster Data ----------------------------
|
213 |
|
214 |
try{
|
215 |
if(tipo==Gdal.GDT_Byte)s.write(buf.buffByte);
|
216 |
}catch(IOException e){ |
217 |
System.out.println("Error writing a line in the file\n"); |
218 |
System.exit(1); |
219 |
} |
220 |
|
221 |
} |
222 |
|
223 |
} |
224 |
|
225 |
|
226 |
|
227 |
try{
|
228 |
System.out.println("-METADATOS-"); |
229 |
String[] metadatos = migdal.getMetadata(); |
230 |
for(int i=0;i<metadatos.length;i++)System.out.println(metadatos[i]); |
231 |
|
232 |
migdal.close(); |
233 |
}catch(GdalException e){
|
234 |
e.printStackTrace(); |
235 |
} |
236 |
|
237 |
|
238 |
} |
239 |
} |