svn-gvsig-desktop / tags / v1_0_2_Build_915 / libraries / libjni-gdal / src-test / example.java @ 42039
History | View | Annotate | Download (7.64 KB)
1 |
/**********************************************************************
|
---|---|
2 |
* $Id: example.java 7765 2006-10-03 07:05:18Z 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 java.io.BufferedOutputStream; |
62 |
import java.io.DataOutputStream; |
63 |
import java.io.File; |
64 |
import java.io.FileOutputStream; |
65 |
import java.io.IOException; |
66 |
|
67 |
import es.gva.cit.jgdal.Gdal; |
68 |
import es.gva.cit.jgdal.GdalBuffer; |
69 |
import es.gva.cit.jgdal.GdalColorEntry; |
70 |
import es.gva.cit.jgdal.GdalColorTable; |
71 |
import es.gva.cit.jgdal.GdalException; |
72 |
import es.gva.cit.jgdal.GdalRasterBand; |
73 |
import es.gva.cit.jgdal.GeoTransform; |
74 |
|
75 |
public class example{ |
76 |
public static void main(String[] args){ |
77 |
|
78 |
|
79 |
Gdal migdal=new Gdal();
|
80 |
|
81 |
try{
|
82 |
if(args.length>0) |
83 |
migdal.open(args[0],Gdal.GA_ReadOnly);
|
84 |
else {
|
85 |
System.out.println("You must introduce the file name"); |
86 |
System.exit(1); |
87 |
} |
88 |
}catch(Exception ge){ |
89 |
ge.printStackTrace(); |
90 |
//...........
|
91 |
} |
92 |
|
93 |
//End Opening the file ----------------------------
|
94 |
|
95 |
|
96 |
GdalRasterBand mirasterband=null;
|
97 |
GdalRasterBand mioverview=null;
|
98 |
|
99 |
FileOutputStream s=null; |
100 |
DataOutputStream midataOS=null; |
101 |
BufferedOutputStream miBS=null; |
102 |
String st;
|
103 |
int nxsize=0; |
104 |
int nysize=0; |
105 |
int rastercount=0; |
106 |
String rasterband;
|
107 |
int xoverview=0; |
108 |
int yoverview=0; |
109 |
GeoTransform gt; |
110 |
int noverviews=0; |
111 |
|
112 |
//Getting dataset Information ----------------------------
|
113 |
|
114 |
try{
|
115 |
rastercount=migdal.getRasterCount(); |
116 |
nxsize = migdal.getRasterXSize(); |
117 |
nysize = migdal.getRasterYSize(); |
118 |
System.out.println("N BANDAS="+rastercount+" SIZEX="+nxsize+" SIZEY="+nysize); |
119 |
System.out.println(migdal.getProjectionRef());
|
120 |
}catch(GdalException ge){
|
121 |
ge.printStackTrace(); |
122 |
//...........
|
123 |
} |
124 |
|
125 |
|
126 |
|
127 |
try{
|
128 |
gt=migdal.getGeoTransform(); |
129 |
System.out.println("Origin = ("+gt.adfgeotransform[0]+","+gt.adfgeotransform[3]+")"); |
130 |
System.out.println("Pixel Size = ("+gt.adfgeotransform[1]+","+gt.adfgeotransform[5]+")"); |
131 |
}catch(GdalException e){
|
132 |
System.out.println("I can't obtain the array geoTransform for this image"); |
133 |
e.printStackTrace(); |
134 |
//...........
|
135 |
} |
136 |
|
137 |
//End Getting dataset Information ----------------------------
|
138 |
|
139 |
try{
|
140 |
File mifichero = new File("exit.raw"); |
141 |
s = new FileOutputStream(mifichero); |
142 |
}catch(Exception e){} |
143 |
|
144 |
|
145 |
for(int iBand=0;iBand<rastercount;iBand++){ |
146 |
|
147 |
System.out.println("-------"); |
148 |
|
149 |
//Informacion de banda
|
150 |
|
151 |
|
152 |
//Fetching a Raster Band ----------------------------
|
153 |
//Several binding are missing yet
|
154 |
|
155 |
int tipo=0; |
156 |
try{
|
157 |
mirasterband = migdal.getRasterBand(iBand+1);
|
158 |
System.out.println("Band Size: ("+mirasterband.getRasterBandXSize()+","+mirasterband.getRasterBandYSize()+")"); |
159 |
System.out.println("Block Size: ("+mirasterband.getBlockXSize()+","+mirasterband.getBlockYSize()+")"); |
160 |
noverviews = mirasterband.getOverviewCount(); |
161 |
System.out.println("Number of Overview: "+noverviews); |
162 |
tipo = mirasterband.getRasterDataType(); |
163 |
System.out.println("Band data type: "+tipo); |
164 |
double noData = mirasterband.getRasterNoDataValue();
|
165 |
if(noData >= 0) |
166 |
System.out.println("NoData="+noData); |
167 |
String[] metadata = mirasterband.getMetadata(); |
168 |
if(metadata.length > 0){ |
169 |
System.out.println("-METADATA-"); |
170 |
for(int i=0;i<metadata.length;i++) |
171 |
System.out.println(metadata[i]);
|
172 |
} |
173 |
|
174 |
//Palette
|
175 |
String colorInt = migdal.getColorInterpretationName(mirasterband.getRasterColorInterpretation());
|
176 |
System.out.println("ColorInterpretation: "+colorInt); |
177 |
if(colorInt.equals("Palette")){ |
178 |
GdalColorTable table = mirasterband.getRasterColorTable(); |
179 |
System.out.println("Number of entries: "+table.getColorEntryCount()); |
180 |
for(int nEntry = 0; nEntry < table.getColorEntryCount(); nEntry++ ){ |
181 |
GdalColorEntry sEntry; |
182 |
sEntry = table.getColorEntryAsRGB(nEntry); |
183 |
System.out.println(
|
184 |
nEntry + " (" +
|
185 |
sEntry.c1 + ", " +
|
186 |
sEntry.c2 + ", " +
|
187 |
sEntry.c3 + ", " +
|
188 |
sEntry.c4 + ")");
|
189 |
} |
190 |
|
191 |
} |
192 |
|
193 |
}catch(GdalException e){
|
194 |
e.printStackTrace(); |
195 |
System.exit(1); |
196 |
} |
197 |
|
198 |
//Fetching a Raster Band ----------------------------
|
199 |
|
200 |
System.out.println("BANDA->"+iBand); |
201 |
|
202 |
//Informaci?n de overview
|
203 |
|
204 |
try{
|
205 |
|
206 |
if(noverviews>0){ |
207 |
|
208 |
//Muestra el tama?o de cada overview y selecccionamos el 0
|
209 |
|
210 |
for(int t=0;t<noverviews;t++){ |
211 |
mioverview=mirasterband.getOverview(t); |
212 |
System.out.println("SIZE OVERVIEW=("+mioverview.getRasterBandXSize()+","+mioverview.getRasterBandYSize()+")"); |
213 |
} |
214 |
mioverview=mirasterband.getOverview(0);
|
215 |
xoverview=mioverview.getRasterBandXSize(); |
216 |
yoverview=mioverview.getRasterBandYSize(); |
217 |
}else{
|
218 |
System.out.println("This band haven't overviews"); |
219 |
xoverview=nxsize; |
220 |
yoverview=nysize; |
221 |
mioverview=mirasterband; |
222 |
|
223 |
} |
224 |
|
225 |
}catch(GdalException e){
|
226 |
e.printStackTrace(); |
227 |
System.exit(1); |
228 |
} |
229 |
|
230 |
|
231 |
|
232 |
|
233 |
|
234 |
|
235 |
|
236 |
//byte[] buf=null;
|
237 |
GdalBuffer buf=null;
|
238 |
|
239 |
for(int i=0; i<yoverview; i++){ |
240 |
|
241 |
//Reading Raster Data ----------------------------
|
242 |
|
243 |
try{
|
244 |
buf = mioverview.readRaster(0, i, xoverview, 1, xoverview, 1, tipo); |
245 |
}catch(GdalException e){
|
246 |
System.out.println("Error in BSBReadLine"); |
247 |
System.exit(1); |
248 |
} |
249 |
|
250 |
//End Reading Raster Data ----------------------------
|
251 |
|
252 |
try{
|
253 |
if(tipo==Gdal.GDT_Byte)s.write(buf.buffByte);
|
254 |
}catch(IOException e){ |
255 |
System.out.println("Error writing a line in the file\n"); |
256 |
System.exit(1); |
257 |
} |
258 |
|
259 |
} |
260 |
|
261 |
} |
262 |
|
263 |
|
264 |
|
265 |
try{
|
266 |
String[] metadata = migdal.getMetadata(); |
267 |
if(metadata.length > 0){ |
268 |
System.out.println("-METADATA-"); |
269 |
System.out.println("Number of values:"+metadata.length); |
270 |
for(int i=0;i<metadata.length;i++) |
271 |
System.out.println(metadata[i]);
|
272 |
} |
273 |
migdal.close(); |
274 |
}catch(GdalException e){
|
275 |
e.printStackTrace(); |
276 |
} |
277 |
|
278 |
|
279 |
} |
280 |
} |