root/trunk/gdal/swig/java/apps/gdalinfo.java

Revision 14319, 15.9 kB (checked in by warmerdam, 3 months ago)

removed colortable from java bindings for now (#2231)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 /******************************************************************************
2  * $Id$
3  *
4  * Name:     gdalinfo.java
5  * Project:  GDAL SWIG Interface
6  * Purpose:  Java port of gdalinfo application
7  * Author:   Benjamin Collins, The MITRE Corporation
8  *
9  * ****************************************************************************
10  * Copyright (c) 1998, Frank Warmerdam
11  * Copyright (c) 2006, The MITRE Corporation
12  *
13  * Permission is hereby granted, free of charge, to any person obtaining a
14  * copy of this software and associated documentation files (the "Software"),
15  * to deal in the Software without restriction, including without limitation
16  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
17  * and/or sell copies of the Software, and to permit persons to whom the
18  * Software is furnished to do so, subject to the following conditions:
19  *
20  * The above copyright notice and this permission notice shall be included
21  * in all copies or substantial portions of the Software.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29  * DEALINGS IN THE SOFTWARE.
30  ****************************************************************************/
31
32
33 import java.util.Enumeration;
34 import java.util.Hashtable;
35 import java.util.Vector;
36
37 import org.gdal.gdal.Band;
38 //import org.gdal.gdal.ColorTable;
39 import org.gdal.gdal.Dataset;
40 import org.gdal.gdal.Driver;
41 import org.gdal.gdal.GCP;
42 import org.gdal.gdal.gdal;
43 import org.gdal.gdalconst.gdalconstConstants;
44 import org.gdal.osr.CoordinateTransformation;
45 import org.gdal.osr.SpatialReference;
46
47 public class gdalinfo {
48
49         /************************************************************************/
50         /*                               Usage()                                */
51         /************************************************************************/
52
53         public static void Usage()
54
55         {
56                 System.out
57                                 .println("Usage: gdalinfo [--help-general] [-mm] [-nogcp] [-nomd] "
58                                                 + "datasetname");
59                 System.exit(1);
60         }
61
62         /************************************************************************/
63         /*                                main()                                */
64         /************************************************************************/
65
66         public static void main(String[] args) {
67                 {
68                         Dataset hDataset;
69                         Band hBand;
70                         int i, iBand;
71                         double[] adfGeoTransform = new double[6];
72                         Driver hDriver;
73                         Hashtable papszMetadata;
74                         boolean bComputeMinMax = false, bSample = false;
75                         boolean bShowGCPs = true, bShowMetadata = true;
76                         boolean bStats = false;
77                         String pszFilename = null;
78
79                         gdal.AllRegister();
80
81                         if (args.length < 1) {
82                                 Usage();
83                                 System.exit(0);
84                         }
85
86                         /* -------------------------------------------------------------------- */
87                         /*      Parse arguments.                                                */
88                         /* -------------------------------------------------------------------- */
89                         for (i = 0; i < args.length; i++) {
90                                 if (args[i].equals("-mm"))
91                                         bComputeMinMax = true;
92                                 else if (args[i].equals("-stats"))
93                                         bStats = true;
94                                 else if (args[i].equals("-sample"))
95                                         bSample = true;
96                                 else if (args[i].equals("-nogcp"))
97                                         bShowGCPs = false;
98                                 else if (args[i].equals("-nomd"))
99                                         bShowMetadata = false;
100                                 else if (args[i].startsWith("-"))
101                                         Usage();
102                                 else if (pszFilename == null)
103                                         pszFilename = args[i];
104                                 else
105                                         Usage();
106                         }
107
108                         if (pszFilename == null)
109                                 Usage();
110
111                         /* -------------------------------------------------------------------- */
112                         /*      Open dataset.                                                   */
113                         /* -------------------------------------------------------------------- */
114                         hDataset = gdal.Open(pszFilename, gdalconstConstants.GA_ReadOnly);
115
116                         if (hDataset == null) {
117                                 System.err
118                                                 .println("GDALOpen failed - " + gdal.GetLastErrorNo());
119                                 System.err.println(gdal.GetLastErrorMsg());
120
121                                 //gdal.DumpOpenDatasets( stderr );
122
123                                 //gdal.DestroyDriverManager();
124
125                                 //gdal.DumpSharedList( null );
126
127                                 System.exit(1);
128                         }
129
130                         /* -------------------------------------------------------------------- */
131                         /*      Report general info.                                            */
132                         /* -------------------------------------------------------------------- */
133                         hDriver = hDataset.GetDriver();
134                         System.out.println("Driver: " + hDriver.getShortName() + "/"
135                                         + hDriver.getLongName());
136
137                         System.out.println("Size is " + hDataset.getRasterXSize() + ", "
138                                         + hDataset.getRasterYSize());
139
140                         /* -------------------------------------------------------------------- */
141                         /*      Report projection.                                              */
142                         /* -------------------------------------------------------------------- */
143                         if (hDataset.GetProjectionRef() != null) {
144                                 SpatialReference hSRS;
145                                 String pszProjection;
146
147                                 pszProjection = hDataset.GetProjectionRef();
148
149                                 hSRS = new SpatialReference(pszProjection);
150                                 if (hSRS != null) {
151                                         String[] pszPrettyWkt = new String[1];
152
153                                         hSRS.ExportToPrettyWkt(pszPrettyWkt, 0);
154                                         System.out.println("Coordinate System is:");
155                                         System.out.println(pszPrettyWkt[0]);
156                                         //gdal.CPLFree( pszPrettyWkt );
157                                 } else
158                                         System.out.println("Coordinate System is `"
159                                                         + hDataset.GetProjectionRef() + "'");
160
161                                 hSRS.delete();
162                         }
163
164                         /* -------------------------------------------------------------------- */
165                         /*      Report Geotransform.                                            */
166                         /* -------------------------------------------------------------------- */
167                         hDataset.GetGeoTransform(adfGeoTransform);
168                         {
169                                 if (adfGeoTransform[2] == 0.0 && adfGeoTransform[4] == 0.0) {
170                                         System.out.println("Origin = (" + adfGeoTransform[0] + ","
171                                                         + adfGeoTransform[3] + ")");
172
173                                         System.out.println("Pixel Size = (" + adfGeoTransform[1]
174                                                         + "," + adfGeoTransform[5] + ")");
175                                 } else
176                                         System.out.println("GeoTransform =");
177                                 System.out.println("  " + adfGeoTransform[0] + ", "
178                                                 + adfGeoTransform[1] + ", " + adfGeoTransform[2]);
179                                 System.out.println("  " + adfGeoTransform[3] + ", "
180                                                 + adfGeoTransform[4] + ", " + adfGeoTransform[5]);
181                         }
182
183                         /* -------------------------------------------------------------------- */
184                         /*      Report GCPs.                                                    */
185                         /* -------------------------------------------------------------------- */
186                         if (bShowGCPs && hDataset.GetGCPCount() > 0) {
187                                 System.out.println("GCP Projection = "
188                                                 + hDataset.GetGCPProjection());
189
190                                 int count = 0;
191                                 Vector GCPs = new Vector();
192                                 hDataset.GetGCPs(GCPs);
193
194                                 Enumeration e = GCPs.elements();
195                                 while (e.hasMoreElements()) {
196                                         GCP gcp = (GCP) e.nextElement();
197                                         System.out.println("GCP[" + (count++) + "]: Id="
198                                                         + gcp.getId() + ", Info=" + gcp.getInfo());
199                                         System.out.println("    (" + gcp.getGCPPixel() + ","
200                                                         + gcp.getGCPLine() + ") (" + gcp.getGCPX() + ","
201                                                         + gcp.getGCPY() + "," + gcp.getGCPZ() + ")");
202                                 }
203
204                         }
205
206                         /* -------------------------------------------------------------------- */
207                         /*      Report metadata.                                                */
208                         /* -------------------------------------------------------------------- */
209                         papszMetadata = hDataset.GetMetadata_Dict("");
210                         if (bShowMetadata && papszMetadata.size() > 0) {
211                                 Enumeration keys = papszMetadata.keys();
212                                 System.out.println("Metadata:");
213                                 while (keys.hasMoreElements()) {
214                                         String key = (String) keys.nextElement();
215                                         System.out.println("  " + key + "="
216                                                         + papszMetadata.get(key));
217                                 }
218                         }
219
220                         /* -------------------------------------------------------------------- */
221                         /*      Report subdatasets.                                             */
222                         /* -------------------------------------------------------------------- */
223                         papszMetadata = hDataset.GetMetadata_Dict("SUBDATASETS");
224                         if (papszMetadata.size() > 0) {
225                                 System.out.println("Subdatasets:");
226                                 Enumeration keys = papszMetadata.keys();
227                                 while (keys.hasMoreElements()) {
228                                         String key = (String) keys.nextElement();
229                                         System.out.println("  " + key + "="
230                                                         + papszMetadata.get(key));
231                                 }
232                         }
233
234                         /* -------------------------------------------------------------------- */
235                         /*      Report corners.                                                 */
236                         /* -------------------------------------------------------------------- */
237                         System.out.println("Corner Coordinates:\n");
238                         GDALInfoReportCorner(hDataset, "Upper Left", 0.0, 0.0);
239                         GDALInfoReportCorner(hDataset, "Lower Left", 0.0, hDataset
240                                         .getRasterYSize());
241                         GDALInfoReportCorner(hDataset, "Upper Right", hDataset
242                                         .getRasterXSize(), 0.0);
243                         GDALInfoReportCorner(hDataset, "Lower Right", hDataset
244                                         .getRasterXSize(), hDataset.getRasterYSize());
245                         GDALInfoReportCorner(hDataset, "Center",
246                                         hDataset.getRasterXSize() / 2.0,
247                                         hDataset.getRasterYSize() / 2.0);
248
249                         /* ==================================================================== */
250                         /*      Loop over bands.                                                */
251                         /* ==================================================================== */
252                         for (iBand = 0; iBand < hDataset.getRasterCount(); iBand++) {
253                                 Double[] pass1 = new Double[1], pass2 = new Double[1];
254                                 double[] adfCMinMax = new double[2];
255 //                              ColorTable hTable;
256
257                                 hBand = hDataset.GetRasterBand(iBand + 1);
258
259                                 /*if( bSample )
260                                  {
261                                  float[] afSample = new float[10000];
262                                  int   nCount;
263
264                                  nCount = hBand.GetRandomRasterSample( 10000, afSample );
265                                  System.out.println( "Got " + nCount + " samples." );
266                                  }*/
267
268                                 System.out.println("Band "
269                                                 + (iBand+1)
270                                                 + " Type="
271                                                 + gdal.GetDataTypeName(hBand.getDataType())
272                                                 + ", ColorInterp="
273                                                 + gdal.GetColorInterpretationName(hBand
274                                                                 .GetRasterColorInterpretation()));
275
276                                 String hBandDesc = hBand.GetDescription();
277                                 if (hBandDesc != null && hBandDesc.length() > 0)
278                                         System.out.println("  Description = " + hBandDesc);
279
280                                 hBand.GetMinimum(pass1);
281                                 hBand.GetMaximum(pass2);
282                                 if(pass1[0] != null || pass2[0] != null || bComputeMinMax) {
283                                         System.out.println("  Min=" + pass1[0] + " Max="
284                                                         + pass2[0]);
285                                 }
286                                 if (bComputeMinMax) {
287                                         hBand.ComputeRasterMinMax(adfCMinMax, 0);
288                                         System.out.println("   Computed Min/Max=" + adfCMinMax[0]
289                                                         + "," + adfCMinMax[1]);
290                                 }
291
292                                 /*eErr = hBand.GetRasterStatistics( hBand, false, bStats,
293                                  dfMin, dfMax, dfMean, dfStdDev );
294                                  if( eErr == CE_None )
295                                  {
296                                  System.out.println( "  Minimum=%.3f, Maximum=%.3f, Mean=%.3f, StdDev=%.3f\n",
297                                  dfMin, dfMax, dfMean, dfStdDev );
298                                  }*/
299
300                                 hBand.GetNoDataValue(pass1);
301                                 if(pass1[0] != null)
302                                 {
303                                         System.out.println("  NoData Value=" + pass1[0]);
304                                 }
305
306                                 if (hBand.GetOverviewCount() > 0) {
307                                         int iOverview;
308
309                                         System.out.println("  Overviews: ");
310                                         for (iOverview = 0; iOverview < hBand.GetOverviewCount(); iOverview++) {
311                                                 Band hOverview;
312
313                                                 if (iOverview != 0)
314                                                         System.out.print(", ");
315
316                                                 hOverview = hBand.GetOverview(iOverview);
317                                                 System.out.print(hOverview.getXSize() + "x"
318                                                                 + hOverview.getYSize());
319                                         }
320                                         System.out.println("");
321                                 }
322
323                                 /*if( GDALHasArbitraryOverviews( hBand ) )
324                                  {
325                                  System.out.println( "  Overviews: arbitrary\n" );
326                                  }*/
327
328                                 /*if( strlen(GDALGetRasterUnitType(hBand)) > 0 )
329                                  {
330                                  System.out.println( "  Unit Type: %s\n", GDALGetRasterUnitType(hBand) );
331                                  }*/
332
333                                 /*if( GDALGetRasterCategoryNames(hBand) != null )
334                                  {
335                                  String[][] papszCategories = GDALGetRasterCategoryNames(hBand);
336                                  int i;
337
338                                  System.out.println( "  Categories:\n" );
339                                  for( i = 0; papszCategories[i] != null; i++ )
340                                  System.out.println( "    %3d: %s\n", i, papszCategories[i] );
341                                  }*/
342
343                                 hBand.GetScale(pass1);
344                                 if(pass1[0] != null) {
345                                         System.out.print("  Offset: " + pass1[0]);
346                                 }
347                                 hBand.GetOffset(pass1);
348                                 if(pass1[0] != null) {
349                                         System.out.println(",   Scale:" + pass1[0]);
350                                 }
351
352                                 papszMetadata = hBand.GetMetadata_Dict("");
353                                  if( bShowMetadata && papszMetadata.size() > 0 ) {
354                                                 Enumeration keys = papszMetadata.keys();
355                                                 System.out.println("Metadata:");
356                                                 while (keys.hasMoreElements()) {
357                                                         String key = (String) keys.nextElement();
358                                                         System.out.println("  " + key + "="
359                                                                         + papszMetadata.get(key));
360                                                 }
361                                  }
362
363 /** ColorTable not supported in GDAL 1.5 and 1.6.
364
365                                 if (hBand.GetRasterColorInterpretation() == gdalconstConstants.GCI_PaletteIndex
366                                                 && (hTable = hBand.GetRasterColorTable()) != null) {
367                                         int count;
368
369                                         System.out.println("  Color Table ("
370                                                         + gdal.GetPaletteInterpretationName(hTable
371                                                                         .GetPaletteInterpretation()) + " with "
372                                                         + hTable.GetCount() + " entries)");
373
374                                         for (count = 0; count < hTable.GetCount(); count++) {
375                                                 System.out.println(" " + count + ": "
376                                                                 + hTable.GetColorEntry(count));
377                                         }
378                                 }
379 **/
380                         }
381
382                         hDataset.delete();
383
384                         //gdal.DestroyDriverManager();
385
386                         System.exit(0);
387                 }
388         }
389
390         /************************************************************************/
391         /*                        GDALInfoReportCorner()                        */
392         /************************************************************************/
393
394         static boolean GDALInfoReportCorner(Dataset hDataset, String corner_name,
395                         double x, double y)
396
397         {
398                 double dfGeoX, dfGeoY;
399                 String pszProjection;
400                 double[] adfGeoTransform = new double[6];
401                 CoordinateTransformation hTransform = null;
402
403                 System.out.print(corner_name + " ");
404
405                 /* -------------------------------------------------------------------- */
406                 /*      Transform the point into georeferenced coordinates.             */
407                 /* -------------------------------------------------------------------- */
408                 hDataset.GetGeoTransform(adfGeoTransform);
409                 {
410                         pszProjection = hDataset.GetProjectionRef();
411
412                         dfGeoX = adfGeoTransform[0] + adfGeoTransform[1] * x
413                                         + adfGeoTransform[2] * y;
414                         dfGeoY = adfGeoTransform[3] + adfGeoTransform[4] * x
415                                         + adfGeoTransform[5] * y;
416                 }
417
418                 if (adfGeoTransform[0] == 0 && adfGeoTransform[1] == 0
419                                 && adfGeoTransform[2] == 0 && adfGeoTransform[3] == 0
420                                 && adfGeoTransform[4] == 0 && adfGeoTransform[5] == 0) {
421                         System.out.println("(" + x + "," + y + ")");
422                         return false;
423                 }
424
425                 /* -------------------------------------------------------------------- */
426                 /*      Report the georeferenced coordinates.                           */
427                 /* -------------------------------------------------------------------- */
428                 System.out.print("(" + dfGeoX + "," + dfGeoY + ") ");
429
430                 /* -------------------------------------------------------------------- */
431                 /*      Setup transformation to lat/long.                               */
432                 /* -------------------------------------------------------------------- */
433                 if (pszProjection != null && pszProjection.length() > 0) {
434                         SpatialReference hProj, hLatLong = null;
435
436                         hProj = new SpatialReference(pszProjection);
437                         if (hProj != null)
438                                 hLatLong = hProj.CloneGeogCS();
439
440                         if (hLatLong != null) {
441                                 //CPLPushErrorHandler( gdalconstConstants.CPLQuietErrorHandler );
442                                 hTransform = new CoordinateTransformation(hProj, hLatLong);
443                                 //CPLPopErrorHandler();
444                                 hLatLong.delete();
445                         }
446
447                         if (hProj != null)
448                                 hProj.delete();
449                 }
450
451                 /* -------------------------------------------------------------------- */
452                 /*      Transform to latlong and report.                                */
453                 /* -------------------------------------------------------------------- */
454                 if (hTransform != null) {
455                         double[] transPoint = new double[3];
456                         hTransform.TransformPoint(transPoint, dfGeoX, dfGeoY, 0);
457                         System.out.print("(" + gdal.DecToDMS(transPoint[0], "Long", 2));
458                         System.out
459                                         .print("," + gdal.DecToDMS(transPoint[1], "Lat", 2) + ")");
460                 }
461
462                 if (hTransform != null)
463                         hTransform.delete();
464
465                 System.out.println("");
466
467                 return true;
468         }
469 }
Note: See TracBrowser for help on using the browser.