1 /****************************************************************************** 2 * $Id: GDALOverviews.cs 13678 2008-02-02 23:29:37Z tamas $ 3 * 4 * Name: GDALOverviews.cs 5 * Project: GDAL CSharp Interface 6 * Purpose: A sample app to get the band histograms. 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# GDALGetHistogram example.</p> 39 * <p>Description: A sample app to get the band histograms.</p> 40 * @author Tamas Szekeres (szekerest@gmail.com) 41 * @version 1.0 42 */ 43 44 45 46 /// <summary> 47 /// A C# based sample to get the band histograms. 48 /// </summary> 49 50 class GDALGetHistogram { 51 usage()52 public static void usage() 53 54 { 55 Console.WriteLine("usage: gdalgethistogram {GDAL dataset name}"); 56 System.Environment.Exit(-1); 57 } 58 Main(string[] args)59 public static void Main(string[] args) 60 { 61 if (args.Length < 1) usage(); 62 63 Console.WriteLine(""); 64 65 try 66 { 67 /* -------------------------------------------------------------------- */ 68 /* Register driver(s). */ 69 /* -------------------------------------------------------------------- */ 70 Gdal.AllRegister(); 71 72 /* -------------------------------------------------------------------- */ 73 /* Open dataset. */ 74 /* -------------------------------------------------------------------- */ 75 Dataset ds = Gdal.Open( args[0], Access.GA_ReadOnly ); 76 77 if (ds == null) 78 { 79 Console.WriteLine("Can't open " + args[0]); 80 System.Environment.Exit(-1); 81 } 82 83 Console.WriteLine("Raster dataset parameters:"); 84 Console.WriteLine(" Projection: " + ds.GetProjectionRef()); 85 Console.WriteLine(" RasterCount: " + ds.RasterCount); 86 Console.WriteLine(" RasterSize (" + ds.RasterXSize + "," + ds.RasterYSize + ")"); 87 88 89 /* -------------------------------------------------------------------- */ 90 /* Get the Histogram arrays */ 91 /* -------------------------------------------------------------------- */ 92 for (int iBand = 1; iBand <= ds.RasterCount; iBand++) 93 { 94 Console.WriteLine("Band " + iBand + ":"); 95 int[] histData = new int[256]; 96 Band band = ds.GetRasterBand(iBand); 97 double pdfMin, pdfMax, pdfMean, pdfStdDev; 98 band.GetStatistics(0, 1, out pdfMin, out pdfMax, out pdfMean, out pdfStdDev); 99 Console.WriteLine("Min=" + pdfMin); 100 Console.WriteLine("Max=" + pdfMax); 101 Console.WriteLine("Mean=" + pdfMean); 102 Console.WriteLine("StdDev=" + pdfStdDev); 103 band.GetHistogram(-0.5, 255.5, 255, histData, 0, 1, 104 new Gdal.GDALProgressFuncDelegate(ProgressFunc), ""); 105 for (int i = 0; i < 255; i++) 106 Console.Write(String.Format(" {0:x}", histData[i])); 107 Console.WriteLine(""); 108 } 109 Console.WriteLine("Completed."); 110 } 111 catch (Exception e) 112 { 113 Console.WriteLine("Application error: " + e.Message); 114 } 115 } 116 ProgressFunc(double Complete, IntPtr Message, IntPtr Data)117 public static int ProgressFunc(double Complete, IntPtr Message, IntPtr Data) 118 { 119 Console.Write("Processing ... " + Complete * 100 + "% Completed."); 120 if (Message != IntPtr.Zero) 121 Console.Write(" Message:" + System.Runtime.InteropServices.Marshal.PtrToStringAnsi(Message)); 122 if (Data != IntPtr.Zero) 123 Console.Write(" Data:" + System.Runtime.InteropServices.Marshal.PtrToStringAnsi(Data)); 124 125 Console.WriteLine(""); 126 return 1; 127 } 128 }