forked from aashish24/gdal-svn
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgdallocationinfo.cpp
634 lines (527 loc) · 21.9 KB
/
gdallocationinfo.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
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
/******************************************************************************
* $Id$
*
* Project: GDAL
* Purpose: Commandline raster query tool.
* Author: Frank Warmerdam <[email protected]>
*
******************************************************************************
* Copyright (c) 2010, Frank Warmerdam <[email protected]>
* Copyright (c) 2010-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 "gdal.h"
#include "cpl_string.h"
#include "ogr_spatialref.h"
#include "cpl_minixml.h"
#include <vector>
CPL_CVSID("$Id$");
/******************************************************************************/
/*! \page gdallocationinfo gdallocationinfo
raster query tool
\section gdallocationinfo_synopsis SYNOPSIS
\htmlonly
Usage:
\endhtmlonly
\verbatim
Usage: gdallocationinfo [--help-general] [-xml] [-lifonly] [-valonly]
[-b band]* [-overview overview_level]
[-l_srs srs_def] [-geoloc] [-wgs84]
[-oo NAME=VALUE]* srcfile [x y]
\endverbatim
\section gdallocationinfo_description DESCRIPTION
<p>
The gdallocationinfo utility provide a mechanism to query information about
a pixel given it's location in one of a variety of coordinate systems. Several
reporting options are provided.
<p>
<dl>
<dt> <b>-xml</b>:</dt>
<dd> The output report will be XML formatted for convenient post processing.</dd>
<dt> <b>-lifonly</b>:</dt>
<dd>The only output is filenames production from the LocationInfo request
against the database (ie. for identifying impacted file from VRT).</dd>
<dt> <b>-valonly</b>:</dt>
<dd>The only output is the pixel values of the selected pixel on each of
the selected bands.</dd>
<dt> <b>-b</b> <em>band</em>:</dt>
<dd>Selects a band to query. Multiple bands can be listed. By default all
bands are queried.</dd>
<dt> <b>-overview</b> <em>overview_level</em>:</dt>
<dd>Query the (overview_level)th overview (overview_level=1 is the 1st overview),
instead of the base band. Note that the x,y location (if the coordinate system is
pixel/line) must still be given with respect to the base band.</dd>
<dt> <b>-l_srs</b> <em>srs def</em>:</dt>
<dd> The coordinate system of the input x, y location.</dd>
<dt> <b>-geoloc</b>:</dt>
<dd> Indicates input x,y points are in the georeferencing system of the image.</dd>
<dt> <b>-wgs84</b>:</dt>
<dd> Indicates input x,y points are WGS84 long, lat.</dd>
<dt> <b>-oo</b> <em>NAME=VALUE</em>:</dt>
<dd>(starting with GDAL 2.0) Dataset open option (format specific)</dd>
<dt> <em>srcfile</em>:</dt><dd> The source GDAL raster datasource name.</dd>
<dt> <em>x</em>:</dt><dd> X location of target pixel. By default the
coordinate system is pixel/line unless -l_srs, -wgs84 or -geoloc supplied. </dd>
<dt> <em>y</em>:</dt><dd> Y location of target pixel. By default the
coordinate system is pixel/line unless -l_srs, -wgs84 or -geoloc supplied. </dd>
</dl>
This utility is intended to provide a variety of information about a
pixel. Currently it reports three things:
<ul>
<li> The location of the pixel in pixel/line space.
<li> The result of a LocationInfo metadata query against the datasource -
currently this is only implemented for VRT files which will report the
file(s) used to satisfy requests for that pixel.
<li> The raster pixel value of that pixel for all or a subset of the bands.
<li> The unscaled pixel value if a Scale and/or Offset apply to the band.
</ul>
The pixel selected is requested by x/y coordinate on the commandline, or read
from stdin. More than one coordinate pair can be supplied when reading
coordinatesis from stdin. By default pixel/line coordinates are expected.
However with use of the -geoloc, -wgs84, or -l_srs switches it is possible
to specify the location in other coordinate systems.
The default report is in a human readable text format. It is possible to
instead request xml output with the -xml switch.
For scripting purposes, the -valonly and -lifonly switches are provided to
restrict output to the actual pixel values, or the LocationInfo files
identified for the pixel.
It is anticipated that additional reporting capabilities will be added to
gdallocationinfo in the future.
<p>
\section gdallocationinfo_example EXAMPLE
Simple example reporting on pixel (256,256) on the file utm.tif.
\verbatim
$ gdallocationinfo utm.tif 256 256
Report:
Location: (256P,256L)
Band 1:
Value: 115
\endverbatim
Query a VRT file providing the location in WGS84, and getting the result in xml.
\verbatim
$ gdallocationinfo -xml -wgs84 utm.vrt -117.5 33.75
<Report pixel="217" line="282">
<BandReport band="1">
<LocationInfo>
<File>utm.tif</File>
</LocationInfo>
<Value>16</Value>
</BandReport>
</Report>
\endverbatim
\if man
\section gdallocationinfo_author AUTHORS
Frank Warmerdam <[email protected]>
\endif
*/
/************************************************************************/
/* Usage() */
/************************************************************************/
static void Usage()
{
printf( "Usage: gdallocationinfo [--help-general] [-xml] [-lifonly] [-valonly]\n"
" [-b band]* [-overview overview_level]\n"
" [-l_srs srs_def] [-geoloc] [-wgs84]\n"
" [-oo NAME=VALUE]* srcfile x y\n"
"\n" );
exit( 1 );
}
/************************************************************************/
/* SanitizeSRS */
/************************************************************************/
char *SanitizeSRS( const char *pszUserInput )
{
OGRSpatialReferenceH hSRS;
char *pszResult = NULL;
CPLErrorReset();
hSRS = OSRNewSpatialReference( NULL );
if( OSRSetFromUserInput( hSRS, pszUserInput ) == OGRERR_NONE )
OSRExportToWkt( hSRS, &pszResult );
else
{
CPLError( CE_Failure, CPLE_AppDefined,
"Translating source or target SRS failed:\n%s",
pszUserInput );
exit( 1 );
}
OSRDestroySpatialReference( hSRS );
return pszResult;
}
/************************************************************************/
/* main() */
/************************************************************************/
int main( int argc, char ** argv )
{
const char *pszLocX = NULL, *pszLocY = NULL;
const char *pszSrcFilename = NULL;
char *pszSourceSRS = NULL;
std::vector<int> anBandList;
bool bAsXML = false, bLIFOnly = false;
bool bQuiet = false, bValOnly = false;
int nOverview = -1;
char **papszOpenOptions = NULL;
GDALAllRegister();
argc = GDALGeneralCmdLineProcessor( argc, &argv, 0 );
if( argc < 1 )
exit( -argc );
/* -------------------------------------------------------------------- */
/* Parse arguments. */
/* -------------------------------------------------------------------- */
int i;
for( i = 1; i < argc; i++ )
{
if( EQUAL(argv[i], "--utility_version") )
{
printf("%s was compiled against GDAL %s and is running against GDAL %s\n",
argv[0], GDAL_RELEASE_NAME, GDALVersionInfo("RELEASE_NAME"));
return 0;
}
else if( EQUAL(argv[i],"-b") && i < argc-1 )
{
anBandList.push_back( atoi(argv[++i]) );
}
else if( EQUAL(argv[i],"-overview") && i < argc-1 )
{
nOverview = atoi(argv[++i]) - 1;
}
else if( EQUAL(argv[i],"-l_srs") && i < argc-1 )
{
CPLFree(pszSourceSRS);
pszSourceSRS = SanitizeSRS(argv[++i]);
}
else if( EQUAL(argv[i],"-geoloc") )
{
CPLFree(pszSourceSRS);
pszSourceSRS = CPLStrdup("-geoloc");
}
else if( EQUAL(argv[i],"-wgs84") )
{
CPLFree(pszSourceSRS);
pszSourceSRS = SanitizeSRS("WGS84");
}
else if( EQUAL(argv[i],"-xml") )
{
bAsXML = true;
}
else if( EQUAL(argv[i],"-lifonly") )
{
bLIFOnly = true;
bQuiet = true;
}
else if( EQUAL(argv[i],"-valonly") )
{
bValOnly = true;
bQuiet = true;
}
else if( EQUAL(argv[i], "-oo") && i < argc-1 )
{
papszOpenOptions = CSLAddString( papszOpenOptions,
argv[++i] );
}
else if( argv[i][0] == '-' && !isdigit(argv[i][1]) )
Usage();
else if( pszSrcFilename == NULL )
pszSrcFilename = argv[i];
else if( pszLocX == NULL )
pszLocX = argv[i];
else if( pszLocY == NULL )
pszLocY = argv[i];
else
Usage();
}
if( pszSrcFilename == NULL || (pszLocX != NULL && pszLocY == NULL) )
Usage();
/* -------------------------------------------------------------------- */
/* Open source file. */
/* -------------------------------------------------------------------- */
GDALDatasetH hSrcDS = NULL;
hSrcDS = GDALOpenEx( pszSrcFilename, GDAL_OF_RASTER, NULL,
(const char* const* )papszOpenOptions, NULL );
if( hSrcDS == NULL )
exit( 1 );
/* -------------------------------------------------------------------- */
/* Setup coordinate transformation, if required */
/* -------------------------------------------------------------------- */
OGRSpatialReferenceH hSrcSRS = NULL, hTrgSRS = NULL;
OGRCoordinateTransformationH hCT = NULL;
if( pszSourceSRS != NULL && !EQUAL(pszSourceSRS,"-geoloc") )
{
hSrcSRS = OSRNewSpatialReference( pszSourceSRS );
hTrgSRS = OSRNewSpatialReference( GDALGetProjectionRef( hSrcDS ) );
hCT = OCTNewCoordinateTransformation( hSrcSRS, hTrgSRS );
if( hCT == NULL )
exit( 1 );
}
/* -------------------------------------------------------------------- */
/* If no bands were requested, we will query them all. */
/* -------------------------------------------------------------------- */
if( anBandList.size() == 0 )
{
for( i = 0; i < GDALGetRasterCount( hSrcDS ); i++ )
anBandList.push_back( i+1 );
}
/* -------------------------------------------------------------------- */
/* Turn the location into a pixel and line location. */
/* -------------------------------------------------------------------- */
int inputAvailable = 1;
double dfGeoX;
double dfGeoY;
CPLString osXML;
if( pszLocX == NULL && pszLocY == NULL )
{
if (fscanf(stdin, "%lf %lf", &dfGeoX, &dfGeoY) != 2)
{
inputAvailable = 0;
}
}
else
{
dfGeoX = CPLAtof(pszLocX);
dfGeoY = CPLAtof(pszLocY);
}
while (inputAvailable)
{
int iPixel, iLine;
if (hCT)
{
if( !OCTTransform( hCT, 1, &dfGeoX, &dfGeoY, NULL ) )
exit( 1 );
}
if( pszSourceSRS != NULL )
{
double adfGeoTransform[6], adfInvGeoTransform[6];
if( GDALGetGeoTransform( hSrcDS, adfGeoTransform ) != CE_None )
{
CPLError(CE_Failure, CPLE_AppDefined, "Cannot get geotransform");
exit( 1 );
}
if( !GDALInvGeoTransform( adfGeoTransform, adfInvGeoTransform ) )
{
CPLError(CE_Failure, CPLE_AppDefined, "Cannot invert geotransform");
exit( 1 );
}
iPixel = (int) floor(
adfInvGeoTransform[0]
+ adfInvGeoTransform[1] * dfGeoX
+ adfInvGeoTransform[2] * dfGeoY );
iLine = (int) floor(
adfInvGeoTransform[3]
+ adfInvGeoTransform[4] * dfGeoX
+ adfInvGeoTransform[5] * dfGeoY );
}
else
{
iPixel = (int) floor(dfGeoX);
iLine = (int) floor(dfGeoY);
}
/* -------------------------------------------------------------------- */
/* Prepare report. */
/* -------------------------------------------------------------------- */
CPLString osLine;
if( bAsXML )
{
osLine.Printf( "<Report pixel=\"%d\" line=\"%d\">",
iPixel, iLine );
osXML += osLine;
}
else if( !bQuiet )
{
printf( "Report:\n" );
printf( " Location: (%dP,%dL)\n", iPixel, iLine );
}
int bPixelReport = TRUE;
if( iPixel < 0 || iLine < 0
|| iPixel >= GDALGetRasterXSize( hSrcDS )
|| iLine >= GDALGetRasterYSize( hSrcDS ) )
{
if( bAsXML )
osXML += "<Alert>Location is off this file! No further details to report.</Alert>";
else if( bValOnly )
printf("\n");
else if( !bQuiet )
printf( "\nLocation is off this file! No further details to report.\n");
bPixelReport = FALSE;
}
/* -------------------------------------------------------------------- */
/* Process each band. */
/* -------------------------------------------------------------------- */
for( i = 0; bPixelReport && i < (int) anBandList.size(); i++ )
{
GDALRasterBandH hBand = GDALGetRasterBand( hSrcDS, anBandList[i] );
int iPixelToQuery = iPixel;
int iLineToQuery = iLine;
if (nOverview >= 0 && hBand != NULL)
{
GDALRasterBandH hOvrBand = GDALGetOverview(hBand, nOverview);
if (hOvrBand != NULL)
{
int nOvrXSize = GDALGetRasterBandXSize(hOvrBand);
int nOvrYSize = GDALGetRasterBandYSize(hOvrBand);
iPixelToQuery = (int)(0.5 + 1.0 * iPixel / GDALGetRasterXSize( hSrcDS ) * nOvrXSize);
iLineToQuery = (int)(0.5 + 1.0 * iLine / GDALGetRasterYSize( hSrcDS ) * nOvrYSize);
if (iPixelToQuery >= nOvrXSize)
iPixelToQuery = nOvrXSize - 1;
if (iLineToQuery >= nOvrYSize)
iLineToQuery = nOvrYSize - 1;
}
else
{
CPLError(CE_Failure, CPLE_AppDefined,
"Cannot get overview %d of band %d",
nOverview + 1, anBandList[i] );
}
hBand = hOvrBand;
}
if (hBand == NULL)
continue;
if( bAsXML )
{
osLine.Printf( "<BandReport band=\"%d\">", anBandList[i] );
osXML += osLine;
}
else if( !bQuiet )
{
printf( " Band %d:\n", anBandList[i] );
}
/* -------------------------------------------------------------------- */
/* Request location info for this location. It is possible */
/* only the VRT driver actually supports this. */
/* -------------------------------------------------------------------- */
CPLString osItem;
osItem.Printf( "Pixel_%d_%d", iPixelToQuery, iLineToQuery );
const char *pszLI = GDALGetMetadataItem( hBand, osItem, "LocationInfo");
if( pszLI != NULL )
{
if( bAsXML )
osXML += pszLI;
else if( !bQuiet )
printf( " %s\n", pszLI );
else if( bLIFOnly )
{
/* Extract all files, if any. */
CPLXMLNode *psRoot = CPLParseXMLString( pszLI );
if( psRoot != NULL
&& psRoot->psChild != NULL
&& psRoot->eType == CXT_Element
&& EQUAL(psRoot->pszValue,"LocationInfo") )
{
CPLXMLNode *psNode;
for( psNode = psRoot->psChild;
psNode != NULL;
psNode = psNode->psNext )
{
if( psNode->eType == CXT_Element
&& EQUAL(psNode->pszValue,"File")
&& psNode->psChild != NULL )
{
char* pszUnescaped = CPLUnescapeString(
psNode->psChild->pszValue, NULL, CPLES_XML);
printf( "%s\n", pszUnescaped );
CPLFree(pszUnescaped);
}
}
}
CPLDestroyXMLNode( psRoot );
}
}
/* -------------------------------------------------------------------- */
/* Report the pixel value of this band. */
/* -------------------------------------------------------------------- */
double adfPixel[2];
if( GDALRasterIO( hBand, GF_Read, iPixelToQuery, iLineToQuery, 1, 1,
adfPixel, 1, 1, GDT_CFloat64, 0, 0) == CE_None )
{
CPLString osValue;
if( GDALDataTypeIsComplex( GDALGetRasterDataType( hBand ) ) )
osValue.Printf( "%.15g+%.15gi", adfPixel[0], adfPixel[1] );
else
osValue.Printf( "%.15g", adfPixel[0] );
if( bAsXML )
{
osXML += "<Value>";
osXML += osValue;
osXML += "</Value>";
}
else if( !bQuiet )
printf( " Value: %s\n", osValue.c_str() );
else if( bValOnly )
printf( "%s\n", osValue.c_str() );
// Report unscaled if we have scale/offset values.
int bSuccess;
double dfOffset = GDALGetRasterOffset( hBand, &bSuccess );
double dfScale = GDALGetRasterScale( hBand, &bSuccess );
if( dfOffset != 0.0 || dfScale != 1.0 )
{
adfPixel[0] = adfPixel[0] * dfScale + dfOffset;
adfPixel[1] = adfPixel[1] * dfScale + dfOffset;
if( GDALDataTypeIsComplex( GDALGetRasterDataType( hBand ) ) )
osValue.Printf( "%.15g+%.15gi", adfPixel[0], adfPixel[1] );
else
osValue.Printf( "%.15g", adfPixel[0] );
if( bAsXML )
{
osXML += "<DescaledValue>";
osXML += osValue;
osXML += "</DescaledValue>";
}
else if( !bQuiet )
printf( " Descaled Value: %s\n", osValue.c_str() );
}
}
if( bAsXML )
osXML += "</BandReport>";
}
osXML += "</Report>";
if( (pszLocX != NULL && pszLocY != NULL) ||
(fscanf(stdin, "%lf %lf", &dfGeoX, &dfGeoY) != 2) )
{
inputAvailable = 0;
}
}
/* -------------------------------------------------------------------- */
/* Finalize xml report and print. */
/* -------------------------------------------------------------------- */
if( bAsXML )
{
CPLXMLNode *psRoot;
char *pszFormattedXML;
psRoot = CPLParseXMLString( osXML );
pszFormattedXML = CPLSerializeXMLTree( psRoot );
CPLDestroyXMLNode( psRoot );
printf( "%s", pszFormattedXML );
CPLFree( pszFormattedXML );
}
/* -------------------------------------------------------------------- */
/* Cleanup */
/* -------------------------------------------------------------------- */
if (hCT) {
OSRDestroySpatialReference( hSrcSRS );
OSRDestroySpatialReference( hTrgSRS );
OCTDestroyCoordinateTransformation( hCT );
}
if (hSrcDS)
GDALClose(hSrcDS);
GDALDumpOpenDatasets( stderr );
GDALDestroyDriverManager();
CPLFree(pszSourceSRS);
CSLDestroy(papszOpenOptions);
CSLDestroy( argv );
return 0;
}