| 1 | /******************************************************************************
|
|---|
| 2 | * $Id: ogrinfo.cs 35222 2016-08-28 06:06:11Z goatbar $
|
|---|
| 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 | OSGeo.OSR.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 | {
|
|---|
| 175 | if (fdef.GetFieldType() == FieldType.OFTStringList)
|
|---|
| 176 | {
|
|---|
| 177 | string[] sList = feat.GetFieldAsStringList(iField);
|
|---|
| 178 | foreach (string s in sList)
|
|---|
| 179 | {
|
|---|
| 180 | Console.Write("\"" + s + "\" ");
|
|---|
| 181 | }
|
|---|
| 182 | Console.WriteLine();
|
|---|
| 183 | }
|
|---|
| 184 | else if (fdef.GetFieldType() == FieldType.OFTIntegerList)
|
|---|
| 185 | {
|
|---|
| 186 | int count;
|
|---|
| 187 | int[] iList = feat.GetFieldAsIntegerList(iField, out count);
|
|---|
| 188 | for (int i = 0; i < count; i++)
|
|---|
| 189 | {
|
|---|
| 190 | Console.Write(iList[i] + " ");
|
|---|
| 191 | }
|
|---|
| 192 | Console.WriteLine();
|
|---|
| 193 | }
|
|---|
| 194 | else if (fdef.GetFieldType() == FieldType.OFTRealList)
|
|---|
| 195 | {
|
|---|
| 196 | int count;
|
|---|
| 197 | double[] iList = feat.GetFieldAsDoubleList(iField, out count);
|
|---|
| 198 | for (int i = 0; i < count; i++)
|
|---|
| 199 | {
|
|---|
| 200 | Console.Write(iList[i].ToString() + " ");
|
|---|
| 201 | }
|
|---|
| 202 | Console.WriteLine();
|
|---|
| 203 | }
|
|---|
| 204 | else
|
|---|
| 205 | Console.WriteLine(feat.GetFieldAsString(iField));
|
|---|
| 206 | }
|
|---|
| 207 | else
|
|---|
| 208 | Console.WriteLine( "(null)" );
|
|---|
| 209 |
|
|---|
| 210 | }
|
|---|
| 211 |
|
|---|
| 212 | if( feat.GetStyleString() != null )
|
|---|
| 213 | Console.WriteLine( " Style = " + feat.GetStyleString() );
|
|---|
| 214 |
|
|---|
| 215 | Geometry geom = feat.GetGeometryRef();
|
|---|
| 216 | if( geom != null )
|
|---|
| 217 | {
|
|---|
| 218 | Console.WriteLine( " " + geom.GetGeometryName() +
|
|---|
| 219 | "(" + geom.GetGeometryType() + ")" );
|
|---|
| 220 | Geometry sub_geom;
|
|---|
| 221 | for (int i = 0; i < geom.GetGeometryCount(); i++)
|
|---|
| 222 | {
|
|---|
| 223 | sub_geom = geom.GetGeometryRef(i);
|
|---|
| 224 | if ( sub_geom != null )
|
|---|
| 225 | {
|
|---|
| 226 | Console.WriteLine( " subgeom" + i + ": " + sub_geom.GetGeometryName() +
|
|---|
| 227 | "(" + sub_geom.GetGeometryType() + ")" );
|
|---|
| 228 | }
|
|---|
| 229 | }
|
|---|
| 230 | Envelope env = new Envelope();
|
|---|
| 231 | geom.GetEnvelope(env);
|
|---|
| 232 | Console.WriteLine(" ENVELOPE: " + env.MinX + "," + env.MaxX + "," +
|
|---|
| 233 | env.MinY + "," + env.MaxY);
|
|---|
| 234 |
|
|---|
| 235 | string geom_wkt;
|
|---|
| 236 | geom.ExportToWkt(out geom_wkt);
|
|---|
| 237 | Console.WriteLine(" " + geom_wkt);
|
|---|
| 238 | }
|
|---|
| 239 |
|
|---|
| 240 | Console.WriteLine( "" );
|
|---|
| 241 | }
|
|---|
| 242 | } |
|---|