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

Revision 15475, 6.9 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:     ogrinfo.cs
5  * Project:  GDAL CSharp Interface
6  * Purpose:  A sample app to dump information from a spatial data source.
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.OGR;
34 using OSGeo.OSR;
35
36
37 /**
38
39  * <p>Title: GDAL C# ogrinfo example.</p>
40  * <p>Description: A sample app to dump information from a spatial data source.</p>
41  * @author Tamas Szekeres (szekerest@gmail.com)
42  * @version 1.0
43  */
44
45
46
47 /// <summary>
48 /// A C# based sample to dump information from a data source.
49 /// </summary>
50
51 class OGRInfo {
52        
53         public static void usage()
54
55         {
56                 Console.WriteLine("usage: ogrinfo {data source name}");
57                 System.Environment.Exit(-1);
58         }
59  
60         public static void Main(string[] args) {
61
62                 if (args.Length != 1) usage();
63
64         // Using early initialization of System.Console
65         Console.WriteLine("");
66
67                 /* -------------------------------------------------------------------- */
68                 /*      Register format(s).                                             */
69                 /* -------------------------------------------------------------------- */
70                 Ogr.RegisterAll();
71
72                 /* -------------------------------------------------------------------- */
73                 /*      Open data source.                                               */
74                 /* -------------------------------------------------------------------- */
75                 DataSource ds = Ogr.Open( args[0], 0 );
76                
77                 if (ds == null) {
78                         Console.WriteLine("Can't open " + args[0]);
79                         System.Environment.Exit(-1);
80                 }
81
82                 /* -------------------------------------------------------------------- */
83                 /*      Get driver                                                      */
84                 /* -------------------------------------------------------------------- */     
85                 Driver drv = ds.GetDriver();
86
87                 if (drv == null)
88                 {
89                         Console.WriteLine("Can't get driver.");
90                         System.Environment.Exit(-1);
91                 }
92         // TODO: drv.name is still unsafe with lazy initialization (Bug 1339)
93         Console.WriteLine("Using driver " + drv.name);
94
95                 /* -------------------------------------------------------------------- */
96                 /*      Iterating through the layers                                    */
97                 /* -------------------------------------------------------------------- */     
98
99                 for( int iLayer = 0; iLayer < ds.GetLayerCount(); iLayer++ )
100                 {
101                         Layer layer = ds.GetLayerByIndex(iLayer);
102
103                         if( layer == null )
104                         {
105                                 Console.WriteLine( "FAILURE: Couldn't fetch advertised layer " + iLayer );
106                                 System.Environment.Exit(-1);
107                         }
108                         ReportLayer(layer);
109                 }
110         }
111
112         public static void ReportLayer(Layer layer)
113         {
114                 FeatureDefn def = layer.GetLayerDefn();
115                 Console.WriteLine( "Layer name: " + def.GetName() );
116                 Console.WriteLine( "Feature Count: " + layer.GetFeatureCount(1) );
117                 Envelope ext = new Envelope();
118                 layer.GetExtent(ext, 1);
119                 Console.WriteLine( "Extent: " + ext.MinX + "," + ext.MaxX + "," +
120                         ext.MinY + "," + ext.MaxY);
121                
122                 /* -------------------------------------------------------------------- */
123                 /*      Reading the spatial reference                                   */
124                 /* -------------------------------------------------------------------- */
125                 SpatialReference sr = layer.GetSpatialRef();
126                 string srs_wkt;
127                 if ( sr != null )
128                 {
129                         sr.ExportToPrettyWkt( out srs_wkt, 1 );
130                 }
131                 else
132                         srs_wkt = "(unknown)";
133
134
135         Console.WriteLine( "Layer SRS WKT: " + srs_wkt );
136
137                 /* -------------------------------------------------------------------- */
138                 /*      Reading the fields                                              */
139                 /* -------------------------------------------------------------------- */
140                 Console.WriteLine("Field definition:");
141                 for( int iAttr = 0; iAttr < def.GetFieldCount(); iAttr++ )
142                 {
143                         FieldDefn fdef = def.GetFieldDefn( iAttr );
144            
145                         Console.WriteLine( fdef.GetNameRef() + ": " +
146                                 fdef.GetFieldTypeName( fdef.GetFieldType() ) + " (" +
147                                 fdef.GetWidth() + "." +
148                                 fdef.GetPrecision() + ")");
149                 }
150
151                 /* -------------------------------------------------------------------- */
152                 /*      Reading the shapes                                              */
153                 /* -------------------------------------------------------------------- */
154                 Console.WriteLine( "" );
155                 Feature feat;
156                 while( (feat = layer.GetNextFeature()) != null )
157                 {
158                         ReportFeature(feat, def);
159                         feat.Dispose();
160                 }
161         }
162
163         public static void ReportFeature(Feature feat, FeatureDefn def)
164         {
165                 Console.WriteLine( "Feature(" + def.GetName() + "): " + feat.GetFID() );
166                 for( int iField = 0; iField < feat.GetFieldCount(); iField++ )
167                 {
168                         FieldDefn fdef = def.GetFieldDefn( iField );
169            
170                         Console.Write( fdef.GetNameRef() + " (" +
171                                 fdef.GetFieldTypeName(fdef.GetFieldType()) + ") = ");
172
173                         if( feat.IsFieldSet( iField ) )
174                                 Console.WriteLine( feat.GetFieldAsString( iField ) );
175                         else
176                                 Console.WriteLine( "(null)" );
177            
178                 }
179
180                 if( feat.GetStyleString() != null )
181                         Console.WriteLine( "  Style = " + feat.GetStyleString() );
182    
183                 Geometry geom = feat.GetGeometryRef();
184                 if( geom != null )
185                 {
186                         Console.WriteLine( "  " + geom.GetGeometryName() +
187                                 "(" + geom.GetGeometryType() + ")" );
188                         Geometry sub_geom;
189                         for (int i = 0; i < geom.GetGeometryCount(); i++)
190                         {
191                                 sub_geom = geom.GetGeometryRef(i);
192                                 if ( sub_geom != null )
193                                 {
194                                         Console.WriteLine( "  subgeom" + i + ": " + sub_geom.GetGeometryName() +
195                                                 "(" + sub_geom.GetGeometryType() + ")" );
196                                 }
197                         }
198             Envelope env = new Envelope();
199             geom.GetEnvelope(env);
200             Console.WriteLine("   ENVELOPE: " + env.MinX + "," + env.MaxX + "," +
201                 env.MinY + "," + env.MaxY);
202
203             string geom_wkt;
204             geom.ExportToWkt(out geom_wkt);
205             Console.WriteLine("  " + geom_wkt);
206                 }
207
208                 Console.WriteLine( "" );
209         }
210 }
Note: See TracBrowser for help on using the browser.