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

Revision 15475, 8.8 kB (checked in by tamas, 2 months ago)

- Added support for using OGR.Layer in Gdal.Polygonize
- Changed the behaviour to use OSR.SpatialReference? and OSR.CoordinateTransformation? instead of defining the same classes in the OGR namespace
- Changed the scope from internal to public of the required functions

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 /******************************************************************************
2  * $Id$
3  *
4  * Name:     createdata.cs
5  * Project:  GDAL CSharp Interface
6  * Purpose:  A sample app to create a spatial data source and a layer.
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
32 using System;
33
34 using OSGeo.OGR;
35 using OSGeo.OSR;
36
37
38 /**
39
40  * <p>Title: GDAL C# createdata example.</p>
41  * <p>Description: A sample app to create a spatial data source and a layer.</p>
42  * @author Tamas Szekeres (szekerest@gmail.com)
43  * @version 1.0
44  */
45
46
47
48 /// <summary>
49 /// A C# based sample to create a layer.
50 /// </summary>
51
52 class CreateData {
53        
54         public static void usage()
55
56         {
57                 Console.WriteLine("usage: createdata {data source name} {layername}");
58                 System.Environment.Exit(-1);
59         }
60  
61         public static void Main(string[] args) {
62
63                 if (args.Length != 2) usage();
64
65         // Using early initialization of System.Console
66         Console.WriteLine("");
67
68                 /* -------------------------------------------------------------------- */
69                 /*      Register format(s).                                             */
70                 /* -------------------------------------------------------------------- */
71         Ogr.RegisterAll();
72
73                 /* -------------------------------------------------------------------- */
74                 /*      Get driver                                                      */
75                 /* -------------------------------------------------------------------- */     
76         Driver drv = Ogr.GetDriverByName("ESRI Shapefile");
77
78                 if (drv == null)
79                 {
80                         Console.WriteLine("Can't get driver.");
81             System.Environment.Exit(-1);
82                 }
83
84         // TODO: drv.name is still unsafe with lazy initialization (Bug 1339)
85         //string DriverName = drv.name;
86         //Console.WriteLine("Using driver " + DriverName);
87
88                 /* -------------------------------------------------------------------- */
89                 /*      Creating the datasource                                         */
90                 /* -------------------------------------------------------------------- */     
91
92         DataSource ds = drv.CreateDataSource( args[0], new string[] {} );
93         if (drv == null)
94         {
95             Console.WriteLine("Can't create the datasource.");
96             System.Environment.Exit(-1);
97         }
98
99         /* -------------------------------------------------------------------- */
100         /*      Creating the layer                                              */
101         /* -------------------------------------------------------------------- */
102
103         Layer layer;
104
105         layer = ds.CreateLayer( args[1], null, wkbGeometryType.wkbPoint, new string[] {} );
106         if( layer == null )
107         {
108             Console.WriteLine("Layer creation failed.");
109             System.Environment.Exit(-1);
110         }
111
112         /* -------------------------------------------------------------------- */
113         /*      Adding attribute fields                                         */
114         /* -------------------------------------------------------------------- */
115
116         FieldDefn fdefn = new FieldDefn( "Name", FieldType.OFTString );
117
118         fdefn.SetWidth(32);
119
120         if( layer.CreateField( fdefn, 1 ) != 0 )
121         {
122             Console.WriteLine("Creating Name field failed.");
123             System.Environment.Exit(-1);
124         }
125
126                 fdefn = new FieldDefn( "IntField", FieldType.OFTInteger );
127                 if( layer.CreateField( fdefn, 1 ) != 0 )
128                 {
129                         Console.WriteLine("Creating IntField field failed.");
130                         System.Environment.Exit(-1);
131                 }
132
133                 fdefn = new FieldDefn( "DoubleField", FieldType.OFTReal );
134                 if( layer.CreateField( fdefn, 1 ) != 0 )
135                 {
136                         Console.WriteLine("Creating DoubleField field failed.");
137                         System.Environment.Exit(-1);
138                 }
139
140                 fdefn = new FieldDefn( "DateField", FieldType.OFTDate );
141                 if( layer.CreateField( fdefn, 1 ) != 0 )
142                 {
143                         Console.WriteLine("Creating DateField field failed.");
144                         System.Environment.Exit(-1);
145                 }
146
147         /* -------------------------------------------------------------------- */
148         /*      Adding features                                                 */
149         /* -------------------------------------------------------------------- */
150
151         Feature feature = new Feature( layer.GetLayerDefn() );
152         feature.SetField( "Name", "value" );
153                 feature.SetField( "IntField", (int)123 );
154                 feature.SetField( "DoubleField", (double)12.345 );
155                 feature.SetField( "DateField", 2007, 3, 15, 18, 24, 30, 0 );
156
157         Geometry geom = Geometry.CreateFromWkt("POINT(47.0 19.2)");
158        
159         if( feature.SetGeometry( geom ) != 0 )
160         {
161             Console.WriteLine( "Failed add geometry to the feature" );
162             System.Environment.Exit(-1);
163         }
164
165         if( layer.CreateFeature( feature ) != 0 )
166         {
167             Console.WriteLine( "Failed to create feature in shapefile" );
168             System.Environment.Exit(-1);
169         }
170        
171                 ReportLayer(layer);
172         }
173
174         public static void ReportLayer(Layer layer)
175         {
176                 FeatureDefn def = layer.GetLayerDefn();
177                 Console.WriteLine( "Layer name: " + def.GetName() );
178                 Console.WriteLine( "Feature Count: " + layer.GetFeatureCount(1) );
179                 Envelope ext = new Envelope();
180                 layer.GetExtent(ext, 1);
181                 Console.WriteLine( "Extent: " + ext.MinX + "," + ext.MaxX + "," +
182                         ext.MinY + "," + ext.MaxY);
183                
184                 /* -------------------------------------------------------------------- */
185                 /*      Reading the spatial reference                                   */
186                 /* -------------------------------------------------------------------- */
187                 SpatialReference sr = layer.GetSpatialRef();
188                 string srs_wkt;
189                 if ( sr != null )
190                 {
191                         sr.ExportToPrettyWkt( out srs_wkt, 1 );
192                 }
193                 else
194                         srs_wkt = "(unknown)";
195
196
197         Console.WriteLine( "Layer SRS WKT: " + srs_wkt );
198
199                 /* -------------------------------------------------------------------- */
200                 /*      Reading the fields                                              */
201                 /* -------------------------------------------------------------------- */
202                 Console.WriteLine("Field definition:");
203                 for( int iAttr = 0; iAttr < def.GetFieldCount(); iAttr++ )
204                 {
205                         FieldDefn fdef = def.GetFieldDefn( iAttr );
206            
207                         Console.WriteLine( fdef.GetNameRef() + ": " +
208                                 fdef.GetFieldTypeName( fdef.GetFieldType() ) + " (" +
209                                 fdef.GetWidth() + "." +
210                                 fdef.GetPrecision() + ")");
211                 }
212
213                 /* -------------------------------------------------------------------- */
214                 /*      Reading the shapes                                              */
215                 /* -------------------------------------------------------------------- */
216                 Console.WriteLine( "" );
217                 Feature feat;
218                 while( (feat = layer.GetNextFeature()) != null )
219                 {
220                         ReportFeature(feat, def);
221                         feat.Dispose();
222                 }
223         }
224
225         public static void ReportFeature(Feature feat, FeatureDefn def)
226         {
227                 Console.WriteLine( "Feature(" + def.GetName() + "): " + feat.GetFID() );
228                 for( int iField = 0; iField < feat.GetFieldCount(); iField++ )
229                 {
230                         FieldDefn fdef = def.GetFieldDefn( iField );
231            
232                         Console.Write( fdef.GetNameRef() + " (" +
233                                 fdef.GetFieldTypeName(fdef.GetFieldType()) + ") = ");
234
235                         if( feat.IsFieldSet( iField ) )
236                                 Console.WriteLine( feat.GetFieldAsString( iField ) );
237                         else
238                                 Console.WriteLine( "(null)" );
239            
240                 }
241
242                 if( feat.GetStyleString() != null )
243                         Console.WriteLine( "  Style = " + feat.GetStyleString() );
244    
245                 Geometry geom = feat.GetGeometryRef();
246                 if( geom != null )
247                         Console.WriteLine( "  " + geom.GetGeometryName() +
248                                 "(" + geom.GetGeometryType() + ")" );
249
250                 Envelope env = new Envelope();
251                 geom.GetEnvelope(env);
252                 Console.WriteLine( "   ENVELOPE: " + env.MinX + "," + env.MaxX + "," +
253                         env.MinY + "," + env.MaxY);
254
255                 string geom_wkt;
256                 geom.ExportToWkt(out geom_wkt);
257                 Console.WriteLine( "  " + geom_wkt );
258
259                 Console.WriteLine( "" );
260         }
261 }
Note: See TracBrowser for help on using the browser.