1 /******************************************************************************
2  * $Id: GDALOverviews.java 355b41831cd2685c85d1aabe5b95665a2c6e99b7 2019-06-19 17:07:04 +0200 Even Rouault $
3  *
4  * Name:     GDALOverviews.java
5  * Project:  GDAL Java Interface
6  * Purpose:  A sample app to create GDAL raster overviews.
7  * Author:   Even Rouault, <even dot rouault at spatialys.com>
8  *
9  * Port from GDALOverviews.cs by Tamas Szekeres
10  *
11  ******************************************************************************
12  * Copyright (c) 2009, Even Rouault
13  * Copyright (c) 2007, Tamas Szekeres
14  *
15  * Permission is hereby granted, free of charge, to any person obtaining a
16  * copy of this software and associated documentation files (the "Software"),
17  * to deal in the Software without restriction, including without limitation
18  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
19  * and/or sell copies of the Software, and to permit persons to whom the
20  * Software is furnished to do so, subject to the following conditions:
21  *
22  * The above copyright notice and this permission notice shall be included
23  * in all copies or substantial portions of the Software.
24  *
25  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
26  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
28  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
30  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
31  * DEALINGS IN THE SOFTWARE.
32  *****************************************************************************/
33 
34 import org.gdal.gdalconst.gdalconst;
35 import org.gdal.gdal.gdal;
36 import org.gdal.gdal.Dataset;
37 import org.gdal.gdal.Band;
38 import org.gdal.gdal.TermProgressCallback;
39 
40 
41 /**
42 
43  * <p>Title: GDAL Java GDALOverviews example.</p>
44  * <p>Description: A sample app to create GDAL raster overviews.</p>
45  * @author Tamas Szekeres (szekerest@gmail.com)
46  * @version 1.0
47  */
48 
49 
50 
51 /// <summary>
52 /// A Java based sample to create GDAL raster overviews.
53 /// </summary>
54 
55 class GDALOverviews {
56 
usage()57 	public static void usage()
58 
59 	{
60 		System.out.println("usage: gdaloverviews {GDAL dataset name} {resamplealg} {level1} {level2} ....");
61 		System.out.println("example: gdaloverviews sample.tif \"NEAREST\" 2 4");
62 		System.exit(-1);
63 	}
64 
main(String[] args)65     public static void main(String[] args)
66     {
67         /* -------------------------------------------------------------------- */
68         /*      Register driver(s).                                             */
69         /* -------------------------------------------------------------------- */
70         gdal.AllRegister();
71 
72         args = gdal.GeneralCmdLineProcessor(args);
73         if (args.length <= 2) usage();
74 
75         try
76         {
77             /* -------------------------------------------------------------------- */
78             /*      Open dataset.                                                   */
79             /* -------------------------------------------------------------------- */
80             Dataset ds = gdal.Open( args[0], gdalconst.GA_Update );
81 
82             if (ds == null)
83             {
84                 System.out.println("Can't open " + args[0]);
85                 System.exit(-1);
86             }
87 
88             System.out.println("Raster dataset parameters:");
89             System.out.println("  Projection: " + ds.GetProjectionRef());
90             System.out.println("  RasterCount: " + ds.getRasterCount());
91             System.out.println("  RasterSize (" + ds.getRasterXSize() + "," + ds.getRasterYSize() + ")");
92 
93             int[] levels = new int[args.length -2];
94 
95             System.out.println(levels.length);
96 
97             for (int i = 2; i < args.length; i++)
98             {
99                 levels[i-2] = Integer.parseInt(args[i]);
100             }
101 
102             if (ds.BuildOverviews(args[1], levels, new TermProgressCallback()) != gdalconst.CE_None)
103             {
104                 System.out.println("The BuildOverviews operation doesn't work");
105                 System.exit(-1);
106             }
107 
108             /* -------------------------------------------------------------------- */
109             /*      Displaying the raster parameters                                */
110             /* -------------------------------------------------------------------- */
111             for (int iBand = 1; iBand <= ds.getRasterCount(); iBand++)
112             {
113                 Band band = ds.GetRasterBand(iBand);
114                 System.out.println("Band " + iBand + " :");
115                 System.out.println("   DataType: " + band.getDataType());
116                 System.out.println("   Size (" + band.getXSize() + "," + band.getYSize() + ")");
117                 System.out.println("   PaletteInterp: " + gdal.GetColorInterpretationName(band.GetRasterColorInterpretation()));
118 
119                 for (int iOver = 0; iOver < band.GetOverviewCount(); iOver++)
120                 {
121                     Band over = band.GetOverview(iOver);
122                     System.out.println("      OverView " + iOver + " :");
123                     System.out.println("         DataType: " + over.getDataType());
124                     System.out.println("         Size (" + over.getXSize() + "," + over.getYSize() + ")");
125                     System.out.println("         PaletteInterp: " + gdal.GetColorInterpretationName(over.GetRasterColorInterpretation()));
126                 }
127             }
128 
129             /* explicit closing of dataset */
130             ds.delete();
131 
132             System.out.println("Completed.");
133             System.out.println("Use:  gdalread " + args[0] + " outfile.png [overview] to extract a particular overview!" );
134         }
135         catch (Exception e)
136         {
137             System.out.println("Application error: " + e.getMessage());
138         }
139     }
140 
141 }
142