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