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

Revision 14912, 4.7 kB (checked in by tamas, 4 months ago)

Added examples for Dataset.GetGCPs, Dataset.SetGCPs and GCPsToGeoTransform.

  • 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# GDALWrite example.</p>
38  * <p>Description: A sample app to write a GDAL raster.</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.
47 /// </summary>
48
49 class GDALWrite {
50        
51         public static void usage()
52
53         {
54                 Console.WriteLine("usage: gdalwrite {dataset name}");
55                 System.Environment.Exit(-1);
56         }
57  
58     public static void Main(string[] args)
59     {
60
61         if (args.Length != 1) usage();
62
63         // Using early initialization of System.Console
64         Console.WriteLine("Writing sample: " + args[0]);
65
66         int bXSize, bYSize;
67         int w, h;
68
69         w = 100;
70         h = 100;
71
72         bXSize = w;
73         bYSize = 1;
74
75         //try
76         {
77             /* -------------------------------------------------------------------- */
78             /*      Register driver(s).                                             */
79             /* -------------------------------------------------------------------- */
80             Gdal.AllRegister();
81            
82             /* -------------------------------------------------------------------- */
83             /*      Get driver                                                      */
84             /* -------------------------------------------------------------------- */ 
85             Driver drv = Gdal.GetDriverByName("GTiff");
86
87             if (drv == null)
88             {
89                 Console.WriteLine("Can't get driver.");
90                 System.Environment.Exit(-1);
91             }
92            
93             Console.WriteLine("Using driver " + drv.LongName);
94
95             /* -------------------------------------------------------------------- */
96             /*      Open dataset.                                                   */
97             /* -------------------------------------------------------------------- */
98             string[] options = new string [] {"BLOCKXSIZE=" + bXSize, "BLOCKYSIZE=" + bYSize};
99             Dataset ds = drv.Create(args[0], w, h, 1, DataType.GDT_Byte, options);
100                
101             if (ds == null)
102             {
103                 Console.WriteLine("Can't open " + args[0]);
104                 System.Environment.Exit(-1);
105             }
106
107             /* -------------------------------------------------------------------- */
108             /*      Setting corner GCPs.                                            */
109             /* -------------------------------------------------------------------- */
110             GCP[] GCPs = new GCP[] {
111                 new GCP(44.5, 27.5, 0, 0, 0, "info0", "id0"),
112                 new GCP(45.5, 27.5, 0, 100, 0, "info1", "id1"),
113                 new GCP(44.5, 26.5, 0, 0, 100, "info2", "id2"),
114                 new GCP(45.5, 26.5, 0, 100, 100, "info3", "id3")
115             };
116             ds.SetGCPs(GCPs, "");
117
118             Band ba = ds.GetRasterBand(1);
119
120             byte [] buffer = new byte [w * h];
121
122             for (int i = 0; i < buffer.Length; i++)
123                 buffer[i] = (byte)(i*256/buffer.Length);
124
125             ba.WriteRaster(0, 0, w, h, buffer, w, h, 0, 0);
126
127             ba.FlushCache();
128             ds.FlushCache();
129
130         }
131         /*catch (Exception e)
132         {
133             Console.WriteLine("Application error: " + e.Message);
134         }*/
135     }
136 }
Note: See TracBrowser for help on using the browser.