File: ogrinfo.java

package info (click to toggle)
gdal 1.10.1%2Bdfsg-8
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 84,320 kB
  • ctags: 74,726
  • sloc: cpp: 677,199; ansic: 162,820; python: 13,816; cs: 11,163; sh: 10,446; java: 5,279; perl: 4,429; php: 2,971; xml: 1,500; yacc: 934; makefile: 494; sql: 112
file content (396 lines) | stat: -rw-r--r-- 16,065 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/******************************************************************************
 * $Id: ogrinfo.java 23704 2012-01-06 10:27:08Z rouault $
 *
 * Name:     ogrinfo.java
 * Project:  GDAL SWIG Interface
 * Purpose:  Java port of ogrinfo application, simple client for viewing OGR driver data.
 * Author:   Even Rouault, <even dot rouault at mines dash paris dot org>
 *
 * Port from ogrinfo.cpp by Frank Warmerdam
 *
 ******************************************************************************
 * Copyright (c) 2009, Even Rouault
 * Copyright (c) 1999, Frank Warmerdam
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 ****************************************************************************/

import org.gdal.gdal.gdal;
import org.gdal.ogr.*;
import org.gdal.osr.*;
import java.util.*;


/* Note : this is the most direct port of ogrinfo.cpp possible */
/* It could be made much more java'ish ! */

public class ogrinfo
{
    static boolean     bReadOnly = false;
    static boolean     bVerbose = true;
    static boolean     bSummaryOnly = false;
    
    static final int OGRNullFID = -1;
    static int     nFetchFID = OGRNullFID;
    
    public static void main(String[] args)
    {
        String pszWHERE = null;
        String pszDataSource = null;
        Vector papszLayers = new Vector();
        Geometry poSpatialFilter = null;
        int         nRepeatCount = 1;
        boolean bAllLayers = false;
        String pszSQLStatement = null;
        String pszDialect = null;

        ogr.DontUseExceptions();

/* -------------------------------------------------------------------- */
/*      Register format(s).                                             */
/* -------------------------------------------------------------------- */
        if( ogr.GetDriverCount() == 0 )
            ogr.RegisterAll();

/* -------------------------------------------------------------------- */
/*      Processing command line arguments.                              */
/* -------------------------------------------------------------------- */
        args = ogr.GeneralCmdLineProcessor(args);

        for(int i=0;i<args.length;i++)
        {
            if (args[i].equals("-ro"))
                bReadOnly = true;
            else if (args[i].equals("-q"))
                bVerbose = false;
            else if (args[i].equals("-spat") && i + 4 < args.length)
            {
                Geometry oRing = new Geometry(ogrConstants.wkbLinearRing);
                double xmin = new Double(args[++i]).doubleValue();
                double ymin = new Double(args[++i]).doubleValue();
                double xmax = new Double(args[++i]).doubleValue();
                double ymax = new Double(args[++i]).doubleValue();
                oRing.AddPoint(xmin, ymin);
                oRing.AddPoint(xmin, ymax);
                oRing.AddPoint(xmax, ymax);
                oRing.AddPoint(xmax, ymin);
                oRing.AddPoint(xmin, ymin);
                
                poSpatialFilter = new Geometry(ogrConstants.wkbPolygon);
                poSpatialFilter.AddGeometry(oRing);
            }
            else if (args[i].equals("-where") && i + 1 < args.length)
            {
                pszWHERE = args[++i];
            }
            else if(args[i].equals("-sql") && i + 1 < args.length)
            {
                pszSQLStatement = args[++i];
            }
            else if(args[i].equals("-dialect") && i + 1 < args.length)
            {
                pszDialect = args[++i];
            }
            else if( args[i].equals("-rc") && i + 1 < args.length)
            {
                nRepeatCount = new Integer(args[++i]).intValue();
            }
            else if( args[i].equals("-al") )
            {
                bAllLayers = true;
            }
            else if( args[i].equals("-so")  || args[i].equals("-summary")  )
            {
                bSummaryOnly = true;
            }
            /*
            else if( EQUALN(papszArgv[iArg],"-fields=", strlen("-fields=")) )
            {
                char* pszTemp = (char*)CPLMalloc(32 + strlen(papszArgv[iArg]));
                sSystem.out.print(pszTemp, "DISPLAY_FIELDS=%s", papszArgv[iArg] + strlen("-fields="));
                papszOptions = CSLAddString(papszOptions, pszTemp);
                CPLFree(pszTemp);
            }
            else if( EQUALN(papszArgv[iArg],"-geom=", strlen("-geom=")) )
            {
                char* pszTemp = (char*)CPLMalloc(32 + strlen(papszArgv[iArg]));
                sSystem.out.print(pszTemp, "DISPLAY_GEOMETRY=%s", papszArgv[iArg] + strlen("-geom="));
                papszOptions = CSLAddString(papszOptions, pszTemp);
                CPLFree(pszTemp);
            }
            */
            else if( args[i].charAt(0) == '-' )
            {
                Usage();
                return;
            }
            else if( pszDataSource == null )
                pszDataSource = args[i];
            else
            {
                papszLayers.addElement( args[i] );
                bAllLayers = false;
            }
        }

        if( pszDataSource == null )
        {
            Usage();
            return;
        }
/* -------------------------------------------------------------------- */
/*      Open data source.                                               */
/* -------------------------------------------------------------------- */
        
        DataSource poDS = ogr.Open(pszDataSource, !bReadOnly);
        if (poDS == null && !bReadOnly)
        {
            poDS = ogr.Open(pszDataSource, false);
            if (poDS == null && bVerbose)
            {
                System.out.println( "Had to open data source read-only.");
            }
        }
        
/* -------------------------------------------------------------------- */
/*      Report failure                                                  */
/* -------------------------------------------------------------------- */
        if( poDS == null )
        {
            System.out.print( "FAILURE:\n" +
                "Unable to open datasource `" + pszDataSource + "' with the following drivers.\n");
            
            for( int iDriver = 0; iDriver < ogr.GetDriverCount(); iDriver++ )
            {
                System.out.println( "  -> " + ogr.GetDriver(iDriver).GetName() );
            }
            
            return;
        }
        
        Driver poDriver = poDS.GetDriver();

/* -------------------------------------------------------------------- */
/*      Some information messages.                                      */
/* -------------------------------------------------------------------- */
        if( bVerbose )
            System.out.println( "INFO: Open of `" + pszDataSource + "'\n" +
                    "      using driver `" + poDriver.GetName() + "' successful." );

/* -------------------------------------------------------------------- */
/*      Special case for -sql clause.  No source layers required.       */
/* -------------------------------------------------------------------- */
        if( pszSQLStatement != null )
        {
            nRepeatCount = 0;  // skip layer reporting.
    
            if( papszLayers.size() > 0 )
                System.out.println( "layer names ignored in combination with -sql." );
            
            Layer poResultSet = poDS.ExecuteSQL( pszSQLStatement, poSpatialFilter, pszDialect );
    
            if( poResultSet != null )
            {
                if( pszWHERE != null )
                {
                    if( poResultSet.SetAttributeFilter( pszWHERE ) != ogr.OGRERR_NONE )
                    {
                        System.err.println("FAILURE: SetAttributeFilter(" + pszWHERE + ") failed.");
                        return;
                    }
                }
    
                ReportOnLayer( poResultSet, null, null );
                poDS.ReleaseResultSet( poResultSet );
            }
        }

/* -------------------------------------------------------------------- */
/*      Process each data source layer.                                 */
/* -------------------------------------------------------------------- */
        //CPLDebug( "OGR", "GetLayerCount() = %d\n", poDS->GetLayerCount() );
    
        for( int iRepeat = 0; iRepeat < nRepeatCount; iRepeat++ )
        {
            if (papszLayers.size() == 0)
            {
/* -------------------------------------------------------------------- */
/*      Process each data source layer.                                 */
/* -------------------------------------------------------------------- */
                for( int iLayer = 0; iLayer < poDS.GetLayerCount(); iLayer++ )
                {
                    Layer        poLayer = poDS.GetLayer(iLayer);

                    if( poLayer == null )
                    {
                        System.out.println( "FAILURE: Couldn't fetch advertised layer " + iLayer + "!");
                        return;
                    }

                    if (!bAllLayers)
                    {
                        System.out.print(
                                (iLayer+1) + ": " + poLayer.GetLayerDefn().GetName() );

                        if( poLayer.GetLayerDefn().GetGeomType() != ogrConstants.wkbUnknown )
                            System.out.print( " (" +
                                    ogr.GeometryTypeToName(
                                        poLayer.GetLayerDefn().GetGeomType()) + ")" );

                        System.out.println();
                    }
                    else
                    {
                        if( iRepeat != 0 )
                            poLayer.ResetReading();

                        ReportOnLayer( poLayer, pszWHERE, poSpatialFilter );
                    }
                }
            }
            else
            {
/* -------------------------------------------------------------------- */
/*      Process specified data source layers.                           */
/* -------------------------------------------------------------------- */
                for(int i = 0; i < papszLayers.size(); i++)
                {
                    Layer poLayer = poDS.GetLayerByName((String)papszLayers.get(i));

                    if( poLayer == null )
                    {
                        System.out.println( "FAILURE: Couldn't fetch requested layer " +
                                            (String)papszLayers.get(i) + "!");
                        return;
                    }

                    if( iRepeat != 0 )
                        poLayer.ResetReading();

                    ReportOnLayer( poLayer, pszWHERE, poSpatialFilter );
                }
            }
        }

    }

/************************************************************************/
/*                               Usage()                                */
/************************************************************************/

    public static void Usage()
    {
        System.out.print( "Usage: ogrinfo [--help-general] [-ro] [-q] [-where restricted_where]\n" +
                "               [-spat xmin ymin xmax ymax] [-fid fid]\n" +
                "               [-sql statement] [-al] [-so]\n" +
                "               [--formats]\n" +
                "               datasource_name [layer [layer ...]]\n");
    }

/************************************************************************/
/*                           ReportOnLayer()                            */
/************************************************************************/

    static void ReportOnLayer(Layer poLayer, String pszWHERE, Geometry poSpatialFilter)
    {
        FeatureDefn poDefn = poLayer.GetLayerDefn();

/* -------------------------------------------------------------------- */
/*      Set filters if provided.                                        */
/* -------------------------------------------------------------------- */
        if( pszWHERE != null )
        {
            if( poLayer.SetAttributeFilter( pszWHERE ) != ogr.OGRERR_NONE )
            {
                System.err.println("FAILURE: SetAttributeFilter(" + pszWHERE + ") failed.");
                return;
            }
        }
    
        if( poSpatialFilter != null )
            poLayer.SetSpatialFilter( poSpatialFilter );

/* -------------------------------------------------------------------- */
/*      Report various overall information.                             */
/* -------------------------------------------------------------------- */
        System.out.println();
        
        System.out.println( "Layer name: "+  poDefn.GetName() );
    
        if( bVerbose )
        {
            System.out.println( "Geometry: " +
                    ogr.GeometryTypeToName( poDefn.GetGeomType() ) );
            
            System.out.println( "Feature Count: " + poLayer.GetFeatureCount() );
            
            double oExt[] = poLayer.GetExtent(true);
            if (oExt != null)
                System.out.println("Extent: (" + oExt[0] + ", " + oExt[2] + ") - (" + oExt[1] + ", " + oExt[3] + ")");
    
            String pszWKT;
            
            if( poLayer.GetSpatialRef() == null )
                pszWKT = "(unknown)";
            else
            {
                pszWKT = poLayer.GetSpatialRef().ExportToPrettyWkt();
            }            
    
            System.out.println( "Layer SRS WKT:\n" + pszWKT );
        
            if( poLayer.GetFIDColumn().length() > 0 )
                System.out.println( "FID Column = " + poLayer.GetFIDColumn() );
        
            if( poLayer.GetGeometryColumn().length() > 0 )
                System.out.println( "Geometry Column = " +  poLayer.GetGeometryColumn() );
    
            for( int iAttr = 0; iAttr < poDefn.GetFieldCount(); iAttr++ )
            {
                FieldDefn  poField = poDefn.GetFieldDefn( iAttr );
                
                System.out.println( poField.GetNameRef() + ": " + poField.GetFieldTypeName( poField.GetFieldType() ) + " (" + poField.GetWidth() + "." + poField.GetPrecision() + ")");
            }
        }
/* -------------------------------------------------------------------- */
/*      Read, and dump features.                                        */
/* -------------------------------------------------------------------- */
        Feature  poFeature;
    
        if( nFetchFID == OGRNullFID && !bSummaryOnly )
        {
            while( (poFeature = poLayer.GetNextFeature()) != null )
            {
                poFeature.DumpReadable();
            }
        }
        else if( nFetchFID != OGRNullFID )
        {
            poFeature = poLayer.GetFeature( nFetchFID );
            if( poFeature == null )
            {
                System.out.println( "Unable to locate feature id " + nFetchFID + " on this layer.");
            }
            else
            {
                poFeature.DumpReadable();
            }
        }
    }
}