forked from aashish24/gdal-svn
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathogrinfo.cpp
604 lines (533 loc) · 22.4 KB
/
ogrinfo.cpp
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
/******************************************************************************
* $Id$
*
* Project: OpenGIS Simple Features Reference Implementation
* Purpose: Simple client for viewing OGR driver data.
* Author: Frank Warmerdam, [email protected]
*
******************************************************************************
* Copyright (c) 1999, Frank Warmerdam
* Copyright (c) 2008-2013, Even Rouault <even dot rouault at mines-paris dot org>
*
* 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.
****************************************************************************/
#include "ogr_api.h"
#include "ogrsf_frmts.h"
#include "ogr_p.h"
#include "cpl_conv.h"
#include "cpl_string.h"
#include "cpl_multiproc.h"
#include "commonutils.h"
CPL_CVSID("$Id$");
int bReadOnly = FALSE;
int bVerbose = TRUE;
int bSummaryOnly = FALSE;
GIntBig nFetchFID = OGRNullFID;
char** papszOptions = NULL;
static void Usage(const char* pszErrorMsg = NULL);
static void ReportOnLayer( OGRLayer *, const char *, const char* pszGeomField,
OGRGeometry * );
/************************************************************************/
/* main() */
/************************************************************************/
#define CHECK_HAS_ENOUGH_ADDITIONAL_ARGS(nExtraArg) \
do { if (iArg + nExtraArg >= nArgc) \
Usage(CPLSPrintf("%s option requires %d argument(s)", papszArgv[iArg], nExtraArg)); } while(0)
int main( int nArgc, char ** papszArgv )
{
const char *pszWHERE = NULL;
const char *pszDataSource = NULL;
char **papszLayers = NULL;
OGRGeometry *poSpatialFilter = NULL;
int nRepeatCount = 1, bAllLayers = FALSE;
const char *pszSQLStatement = NULL;
const char *pszDialect = NULL;
int nRet = 0;
const char* pszGeomField = NULL;
char **papszOpenOptions = NULL;
/* Check strict compilation and runtime library version as we use C++ API */
if (! GDAL_CHECK_VERSION(papszArgv[0]))
exit(1);
EarlySetConfigOptions(nArgc, papszArgv);
/* -------------------------------------------------------------------- */
/* Register format(s). */
/* -------------------------------------------------------------------- */
OGRRegisterAll();
/* -------------------------------------------------------------------- */
/* Processing command line arguments. */
/* -------------------------------------------------------------------- */
nArgc = OGRGeneralCmdLineProcessor( nArgc, &papszArgv, 0 );
if( nArgc < 1 )
exit( -nArgc );
for( int iArg = 1; iArg < nArgc; iArg++ )
{
if( EQUAL(papszArgv[iArg], "--utility_version") )
{
printf("%s was compiled against GDAL %s and is running against GDAL %s\n",
papszArgv[0], GDAL_RELEASE_NAME, GDALVersionInfo("RELEASE_NAME"));
return 0;
}
else if( EQUAL(papszArgv[iArg],"--help") )
Usage();
else if( EQUAL(papszArgv[iArg],"-ro") )
bReadOnly = TRUE;
else if( EQUAL(papszArgv[iArg],"-q") || EQUAL(papszArgv[iArg],"-quiet"))
bVerbose = FALSE;
else if( EQUAL(papszArgv[iArg],"-fid") )
{
CHECK_HAS_ENOUGH_ADDITIONAL_ARGS(1);
nFetchFID = CPLAtoGIntBig(papszArgv[++iArg]);
}
else if( EQUAL(papszArgv[iArg],"-spat") )
{
CHECK_HAS_ENOUGH_ADDITIONAL_ARGS(4);
OGRLinearRing oRing;
oRing.addPoint( CPLAtof(papszArgv[iArg+1]), CPLAtof(papszArgv[iArg+2]) );
oRing.addPoint( CPLAtof(papszArgv[iArg+1]), CPLAtof(papszArgv[iArg+4]) );
oRing.addPoint( CPLAtof(papszArgv[iArg+3]), CPLAtof(papszArgv[iArg+4]) );
oRing.addPoint( CPLAtof(papszArgv[iArg+3]), CPLAtof(papszArgv[iArg+2]) );
oRing.addPoint( CPLAtof(papszArgv[iArg+1]), CPLAtof(papszArgv[iArg+2]) );
poSpatialFilter = new OGRPolygon();
((OGRPolygon *) poSpatialFilter)->addRing( &oRing );
iArg += 4;
}
else if( EQUAL(papszArgv[iArg],"-geomfield") )
{
CHECK_HAS_ENOUGH_ADDITIONAL_ARGS(1);
pszGeomField = papszArgv[++iArg];
}
else if( EQUAL(papszArgv[iArg],"-where") )
{
CHECK_HAS_ENOUGH_ADDITIONAL_ARGS(1);
pszWHERE = papszArgv[++iArg];
}
else if( EQUAL(papszArgv[iArg],"-sql") )
{
CHECK_HAS_ENOUGH_ADDITIONAL_ARGS(1);
pszSQLStatement = papszArgv[++iArg];
}
else if( EQUAL(papszArgv[iArg],"-dialect") )
{
CHECK_HAS_ENOUGH_ADDITIONAL_ARGS(1);
pszDialect = papszArgv[++iArg];
}
else if( EQUAL(papszArgv[iArg],"-rc") )
{
CHECK_HAS_ENOUGH_ADDITIONAL_ARGS(1);
nRepeatCount = atoi(papszArgv[++iArg]);
}
else if( EQUAL(papszArgv[iArg],"-al") )
{
bAllLayers = TRUE;
}
else if( EQUAL(papszArgv[iArg],"-so")
|| EQUAL(papszArgv[iArg],"-summary") )
{
bSummaryOnly = TRUE;
}
else if( EQUALN(papszArgv[iArg],"-fields=", strlen("-fields=")) )
{
char* pszTemp = (char*)CPLMalloc(32 + strlen(papszArgv[iArg]));
sprintf(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]));
sprintf(pszTemp, "DISPLAY_GEOMETRY=%s", papszArgv[iArg] + strlen("-geom="));
papszOptions = CSLAddString(papszOptions, pszTemp);
CPLFree(pszTemp);
}
else if( EQUAL(papszArgv[iArg], "-oo") )
{
CHECK_HAS_ENOUGH_ADDITIONAL_ARGS(1);
papszOpenOptions = CSLAddString( papszOpenOptions,
papszArgv[++iArg] );
}
else if( papszArgv[iArg][0] == '-' )
{
Usage(CPLSPrintf("Unknown option name '%s'", papszArgv[iArg]));
}
else if( pszDataSource == NULL )
pszDataSource = papszArgv[iArg];
else
{
papszLayers = CSLAddString( papszLayers, papszArgv[iArg] );
bAllLayers = FALSE;
}
}
if( pszDataSource == NULL )
Usage("No datasource specified.");
if( pszDialect != NULL && pszWHERE != NULL && pszSQLStatement == NULL )
printf("Warning: -dialect is ignored with -where. Use -sql instead");
/* -------------------------------------------------------------------- */
/* Open data source. */
/* -------------------------------------------------------------------- */
GDALDataset *poDS = NULL;
GDALDriver *poDriver = NULL;
poDS = (GDALDataset*) GDALOpenEx( pszDataSource,
(!bReadOnly ? GDAL_OF_UPDATE : GDAL_OF_READONLY) | GDAL_OF_VECTOR,
NULL, papszOpenOptions, NULL );
if( poDS == NULL && !bReadOnly )
{
poDS = (GDALDataset*) GDALOpenEx( pszDataSource,
GDAL_OF_READONLY | GDAL_OF_VECTOR, NULL, papszOpenOptions, NULL );
if( poDS != NULL && bVerbose )
{
printf( "Had to open data source read-only.\n" );
bReadOnly = TRUE;
}
}
if( poDS != NULL )
poDriver = poDS->GetDriver();
/* -------------------------------------------------------------------- */
/* Report failure */
/* -------------------------------------------------------------------- */
if( poDS == NULL )
{
OGRSFDriverRegistrar *poR = OGRSFDriverRegistrar::GetRegistrar();
printf( "FAILURE:\n"
"Unable to open datasource `%s' with the following drivers.\n",
pszDataSource );
for( int iDriver = 0; iDriver < poR->GetDriverCount(); iDriver++ )
{
printf( " -> %s\n", poR->GetDriver(iDriver)->GetDescription() );
}
nRet = 1;
goto end;
}
CPLAssert( poDriver != NULL);
/* -------------------------------------------------------------------- */
/* Some information messages. */
/* -------------------------------------------------------------------- */
if( bVerbose )
printf( "INFO: Open of `%s'\n"
" using driver `%s' successful.\n",
pszDataSource, poDriver->GetDescription() );
if( bVerbose && !EQUAL(pszDataSource,poDS->GetDescription()) )
{
printf( "INFO: Internal data source name `%s'\n"
" different from user name `%s'.\n",
poDS->GetDescription(), pszDataSource );
}
/* -------------------------------------------------------------------- */
/* Special case for -sql clause. No source layers required. */
/* -------------------------------------------------------------------- */
if( pszSQLStatement != NULL )
{
OGRLayer *poResultSet = NULL;
nRepeatCount = 0; // skip layer reporting.
if( CSLCount(papszLayers) > 0 )
printf( "layer names ignored in combination with -sql.\n" );
poResultSet = poDS->ExecuteSQL( pszSQLStatement,
(pszGeomField == NULL) ? poSpatialFilter: NULL,
pszDialect );
if( poResultSet != NULL )
{
if( pszWHERE != NULL )
{
if (poResultSet->SetAttributeFilter( pszWHERE ) != OGRERR_NONE )
{
printf( "FAILURE: SetAttributeFilter(%s) failed.\n", pszWHERE );
exit(1);
}
}
if( pszGeomField != NULL )
ReportOnLayer( poResultSet, NULL, pszGeomField, poSpatialFilter );
else
ReportOnLayer( poResultSet, NULL, NULL, NULL );
poDS->ReleaseResultSet( poResultSet );
}
}
CPLDebug( "OGR", "GetLayerCount() = %d\n", poDS->GetLayerCount() );
for( int iRepeat = 0; iRepeat < nRepeatCount; iRepeat++ )
{
if ( CSLCount(papszLayers) == 0 )
{
/* -------------------------------------------------------------------- */
/* Process each data source layer. */
/* -------------------------------------------------------------------- */
for( int iLayer = 0; iLayer < poDS->GetLayerCount(); iLayer++ )
{
OGRLayer *poLayer = poDS->GetLayer(iLayer);
if( poLayer == NULL )
{
printf( "FAILURE: Couldn't fetch advertised layer %d!\n",
iLayer );
exit( 1 );
}
if (!bAllLayers)
{
printf( "%d: %s",
iLayer+1,
poLayer->GetName() );
int nGeomFieldCount =
poLayer->GetLayerDefn()->GetGeomFieldCount();
if( nGeomFieldCount > 1 )
{
printf( " (");
for(int iGeom = 0;iGeom < nGeomFieldCount; iGeom ++ )
{
if( iGeom > 0 )
printf(", ");
OGRGeomFieldDefn* poGFldDefn =
poLayer->GetLayerDefn()->GetGeomFieldDefn(iGeom);
printf( "%s",
OGRGeometryTypeToName(
poGFldDefn->GetType() ) );
}
printf( ")");
}
else if( poLayer->GetGeomType() != wkbUnknown )
printf( " (%s)",
OGRGeometryTypeToName(
poLayer->GetGeomType() ) );
printf( "\n" );
}
else
{
if( iRepeat != 0 )
poLayer->ResetReading();
ReportOnLayer( poLayer, pszWHERE, pszGeomField, poSpatialFilter );
}
}
}
else
{
/* -------------------------------------------------------------------- */
/* Process specified data source layers. */
/* -------------------------------------------------------------------- */
char** papszIter = papszLayers;
for( ; *papszIter != NULL; papszIter++ )
{
OGRLayer *poLayer = poDS->GetLayerByName(*papszIter);
if( poLayer == NULL )
{
printf( "FAILURE: Couldn't fetch requested layer %s!\n",
*papszIter );
exit( 1 );
}
if( iRepeat != 0 )
poLayer->ResetReading();
ReportOnLayer( poLayer, pszWHERE, pszGeomField, poSpatialFilter );
}
}
}
/* -------------------------------------------------------------------- */
/* Close down. */
/* -------------------------------------------------------------------- */
end:
CSLDestroy( papszArgv );
CSLDestroy( papszLayers );
CSLDestroy( papszOptions );
CSLDestroy( papszOpenOptions );
if( poDS != NULL )
GDALClose( (GDALDatasetH)poDS );
if (poSpatialFilter)
OGRGeometryFactory::destroyGeometry( poSpatialFilter );
OGRCleanupAll();
return nRet;
}
/************************************************************************/
/* Usage() */
/************************************************************************/
static void Usage(const char* pszErrorMsg)
{
printf( "Usage: ogrinfo [--help-general] [-ro] [-q] [-where restricted_where]\n"
" [-spat xmin ymin xmax ymax] [-geomfield field] [-fid fid]\n"
" [-sql statement] [-dialect sql_dialect] [-al] [-so] [-fields={YES/NO}]\n"
" [-geom={YES/NO/SUMMARY}] [-formats] [[-oo NAME=VALUE] ...]\n"
" datasource_name [layer [layer ...]]\n");
if( pszErrorMsg != NULL )
fprintf(stderr, "\nFAILURE: %s\n", pszErrorMsg);
exit( 1 );
}
/************************************************************************/
/* ReportOnLayer() */
/************************************************************************/
static void ReportOnLayer( OGRLayer * poLayer, const char *pszWHERE,
const char* pszGeomField,
OGRGeometry *poSpatialFilter )
{
OGRFeatureDefn *poDefn = poLayer->GetLayerDefn();
/* -------------------------------------------------------------------- */
/* Set filters if provided. */
/* -------------------------------------------------------------------- */
if( pszWHERE != NULL )
{
if (poLayer->SetAttributeFilter( pszWHERE ) != OGRERR_NONE )
{
printf( "FAILURE: SetAttributeFilter(%s) failed.\n", pszWHERE );
exit(1);
}
}
if( poSpatialFilter != NULL )
{
if( pszGeomField != NULL )
{
int iGeomField = poDefn->GetGeomFieldIndex(pszGeomField);
if( iGeomField >= 0 )
poLayer->SetSpatialFilter( iGeomField, poSpatialFilter );
else
printf("WARNING: Cannot find geometry field %s.\n",
pszGeomField);
}
else
poLayer->SetSpatialFilter( poSpatialFilter );
}
/* -------------------------------------------------------------------- */
/* Report various overall information. */
/* -------------------------------------------------------------------- */
printf( "\n" );
printf( "Layer name: %s\n", poLayer->GetName() );
if( bVerbose )
{
int nGeomFieldCount =
poLayer->GetLayerDefn()->GetGeomFieldCount();
if( nGeomFieldCount > 1 )
{
for(int iGeom = 0;iGeom < nGeomFieldCount; iGeom ++ )
{
OGRGeomFieldDefn* poGFldDefn =
poLayer->GetLayerDefn()->GetGeomFieldDefn(iGeom);
printf( "Geometry (%s): %s\n", poGFldDefn->GetNameRef(),
OGRGeometryTypeToName( poGFldDefn->GetType() ) );
}
}
else
{
printf( "Geometry: %s\n",
OGRGeometryTypeToName( poLayer->GetGeomType() ) );
}
printf( "Feature Count: " CPL_FRMT_GIB "\n", poLayer->GetFeatureCount() );
OGREnvelope oExt;
if( nGeomFieldCount > 1 )
{
for(int iGeom = 0;iGeom < nGeomFieldCount; iGeom ++ )
{
if (poLayer->GetExtent(iGeom, &oExt, TRUE) == OGRERR_NONE)
{
OGRGeomFieldDefn* poGFldDefn =
poLayer->GetLayerDefn()->GetGeomFieldDefn(iGeom);
CPLprintf("Extent (%s): (%f, %f) - (%f, %f)\n",
poGFldDefn->GetNameRef(),
oExt.MinX, oExt.MinY, oExt.MaxX, oExt.MaxY);
}
}
}
else if (poLayer->GetExtent(&oExt, TRUE) == OGRERR_NONE)
{
CPLprintf("Extent: (%f, %f) - (%f, %f)\n",
oExt.MinX, oExt.MinY, oExt.MaxX, oExt.MaxY);
}
char *pszWKT;
if( nGeomFieldCount > 1 )
{
for(int iGeom = 0;iGeom < nGeomFieldCount; iGeom ++ )
{
OGRGeomFieldDefn* poGFldDefn =
poLayer->GetLayerDefn()->GetGeomFieldDefn(iGeom);
OGRSpatialReference* poSRS = poGFldDefn->GetSpatialRef();
if( poSRS == NULL )
pszWKT = CPLStrdup( "(unknown)" );
else
{
poSRS->exportToPrettyWkt( &pszWKT );
}
printf( "SRS WKT (%s):\n%s\n",
poGFldDefn->GetNameRef(), pszWKT );
CPLFree( pszWKT );
}
}
else
{
if( poLayer->GetSpatialRef() == NULL )
pszWKT = CPLStrdup( "(unknown)" );
else
{
poLayer->GetSpatialRef()->exportToPrettyWkt( &pszWKT );
}
printf( "Layer SRS WKT:\n%s\n", pszWKT );
CPLFree( pszWKT );
}
if( strlen(poLayer->GetFIDColumn()) > 0 )
printf( "FID Column = %s\n",
poLayer->GetFIDColumn() );
for(int iGeom = 0;iGeom < nGeomFieldCount; iGeom ++ )
{
OGRGeomFieldDefn* poGFldDefn =
poLayer->GetLayerDefn()->GetGeomFieldDefn(iGeom);
if( nGeomFieldCount == 1 &&
EQUAL(poGFldDefn->GetNameRef(), "") && poGFldDefn->IsNullable() )
break;
printf( "Geometry Column ");
if( nGeomFieldCount > 1 )
printf("%d ", iGeom + 1);
if( !poGFldDefn->IsNullable() )
printf("NOT NULL ");
printf("= %s\n", poGFldDefn->GetNameRef() );
}
for( int iAttr = 0; iAttr < poDefn->GetFieldCount(); iAttr++ )
{
OGRFieldDefn *poField = poDefn->GetFieldDefn( iAttr );
const char* pszType = (poField->GetSubType() != OFSTNone) ?
CPLSPrintf("%s(%s)",
poField->GetFieldTypeName( poField->GetType() ),
poField->GetFieldSubTypeName(poField->GetSubType())) :
poField->GetFieldTypeName( poField->GetType() );
printf( "%s: %s (%d.%d)",
poField->GetNameRef(),
pszType,
poField->GetWidth(),
poField->GetPrecision() );
if( !poField->IsNullable() )
printf(" NOT NULL");
if( poField->GetDefault() != NULL )
printf(" DEFAULT %s", poField->GetDefault() );
printf( "\n" );
}
}
/* -------------------------------------------------------------------- */
/* Read, and dump features. */
/* -------------------------------------------------------------------- */
OGRFeature *poFeature = NULL;
if( nFetchFID == OGRNullFID && !bSummaryOnly )
{
while( (poFeature = poLayer->GetNextFeature()) != NULL )
{
poFeature->DumpReadable( NULL, papszOptions );
OGRFeature::DestroyFeature( poFeature );
}
}
else if( nFetchFID != OGRNullFID )
{
poFeature = poLayer->GetFeature( nFetchFID );
if( poFeature == NULL )
{
printf( "Unable to locate feature id " CPL_FRMT_GIB " on this layer.\n",
nFetchFID );
}
else
{
poFeature->DumpReadable( NULL, papszOptions );
OGRFeature::DestroyFeature( poFeature );
}
}
}