root/trunk/gdal/swig/csharp/apps/GDALCreateCopy.cs

Revision 13678, 4.4 kB (checked in by tamas, 10 months ago)

Update SVN props

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 /******************************************************************************
2  * $Id$
3  *
4  * Name:     GDALWrite.cs
5  * Project:  GDAL CSharp Interface
6  * Purpose:   sample app to write a GDAL raster.
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  * <p>Title: GDAL C# GDALCreateCopy example.</p>
38  * <p>Description: A sample app to write a GDAL raster using CreateCopy.</p>
39  * @author Tamas Szekeres (szekerest@gmail.com)
40  * @version 1.0
41  */
42
43
44
45 /// <summary>
46 /// A C# based sample to write a GDAL raster using CreateCopy.
47 /// </summary>
48
49 class GDALWrite {
50        
51         public static void usage()
52
53         {
54                 Console.WriteLine("usage: gdalcreatecopy {dataset name} {out file name}");
55                 System.Environment.Exit(-1);
56         }
57  
58     public static void Main(string[] args)
59     {
60
61         if (args.Length != 2) usage();
62
63         Console.WriteLine("");
64
65         try
66         {
67             /* -------------------------------------------------------------------- */
68             /*      Register driver(s).                                             */
69             /* -------------------------------------------------------------------- */
70             Gdal.AllRegister();
71            
72             /* -------------------------------------------------------------------- */
73             /*      Get driver                                                      */
74             /* -------------------------------------------------------------------- */ 
75             Driver drv = Gdal.GetDriverByName("GTiff");
76
77             if (drv == null)
78             {
79                 Console.WriteLine("Can't get driver.");
80                 System.Environment.Exit(-1);
81             }
82            
83             Console.WriteLine("Using driver " + drv.LongName);
84
85             /* -------------------------------------------------------------------- */
86             /*      Open dataset.                                                   */
87             /* -------------------------------------------------------------------- */
88             Dataset ds = Gdal.Open( args[0], Access.GA_ReadOnly );
89
90             if (ds == null)
91             {
92                 Console.WriteLine("Can't open source dataset " + args[1]);
93                 System.Environment.Exit(-1);
94             }
95
96             string[] options = new string [] {"TILED=YES"};
97             Dataset dso = drv.CreateCopy(args[1], ds, 0, options, new Gdal.GDALProgressFuncDelegate(ProgressFunc), "Sample Data");
98                
99             if (dso == null)
100             {
101                 Console.WriteLine("Can't create dest dataset " + args[1]);
102                 System.Environment.Exit(-1);
103             }
104         }
105         catch (Exception e)
106         {
107             Console.WriteLine("Application error: " + e.Message);
108         }
109     }
110
111     public static int ProgressFunc(double Complete, IntPtr Message, IntPtr Data)
112     {
113         Console.Write("Processing ... " + Complete * 100 + "% Completed.");
114         if (Message != IntPtr.Zero)
115             Console.Write(" Message:" + System.Runtime.InteropServices.Marshal.PtrToStringAnsi(Message));
116         if (Data != IntPtr.Zero)
117             Console.Write(" Data:" + System.Runtime.InteropServices.Marshal.PtrToStringAnsi(Data));
118        
119         Console.WriteLine("");
120         return 1;
121     }
122 }
Note: See TracBrowser for help on using the browser.