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 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996
|
/* ****************************************************************************
* $Id: gdal_translate.cpp,v 1.33 2006/04/25 14:29:28 fwarmerdam Exp $
*
* Project: GDAL Utilities
* Purpose: GDAL Image Translator Program
* Author: Frank Warmerdam, warmerdam@pobox.com
*
* ****************************************************************************
* Copyright (c) 1998, 2002, 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.
* ****************************************************************************
*
* $Log: gdal_translate.cpp,v $
* Revision 1.33 2006/04/25 14:29:28 fwarmerdam
* Avoid warning.
*
* Revision 1.32 2005/05/17 19:04:47 fwarmerdam
* Make sure nodata value is set after copycommoninfo.
*
* Revision 1.31 2005/05/09 14:31:56 fwarmerdam
* Added optional elevation in -gcp usage message.
*
* Revision 1.30 2005/04/26 20:38:52 fwarmerdam
* Added main/ProxyMain distinction since calling main does not work on
* some platforms.
*
* Revision 1.29 2005/04/12 14:37:47 fwarmerdam
* Added the -a_nodata switch to assign nodata values to output bands.
*
* Revision 1.28 2005/01/27 20:58:11 fwarmerdam
* Added -a_ullr switch.
*
* Revision 1.27 2005/01/24 18:01:18 fwarmerdam
* Added -sds option for copying subdatasets.
*
* Revision 1.26 2004/10/12 14:15:22 fwarmerdam
* Added srcwin checking.
*
* Revision 1.25 2004/08/19 15:49:52 warmerda
* Don't continue if the srcwin falls outside the raster.
*
* Revision 1.24 2004/08/11 19:06:48 warmerda
* use CopyCommonInfoFrom() method on band
*
* Revision 1.23 2004/07/28 17:56:00 warmerda
* use return instead of exit() to avoid lame warnings on windows
*
* Revision 1.22 2004/07/28 17:51:09 warmerda
* updated to use VRTSourcedRasterBand
*
* Revision 1.21 2004/04/23 22:18:40 warmerda
* Another memory leak.
*
* Revision 1.20 2004/04/23 22:18:07 warmerda
* Avoid argument, and creation option memory leak.
*
* Revision 1.19 2004/04/02 17:33:22 warmerda
* added GDALGeneralCmdLineProcessor()
*
* Revision 1.18 2003/09/30 06:10:08 dron
* Copy NoData value over the virtual dataset.
*
* Revision 1.17 2003/09/19 18:30:02 warmerda
* Added -gcp switch to attached GCPs to an image.
*
* Revision 1.16 2003/08/28 13:42:20 warmerda
* support multiple -b switches to select a set of bands
*
* Revision 1.15 2003/07/27 12:31:14 dron
* Few memory leaks fixed.
*
* Revision 1.14 2003/04/17 12:55:40 dron
* Few memory leaks fixed.
*
* Revision 1.13 2003/03/03 16:29:35 warmerda
* Added the -quiet flag.
*
* Revision 1.12 2002/12/03 03:40:11 warmerda
* Avoid initialization warning.
*
* Revision 1.11 2002/11/24 04:26:42 warmerda
* Use AddComplexSource() for scaling, rid of Create() altogether.
*
* Revision 1.10 2002/11/21 21:07:37 warmerda
* ensure gcps are transformed as needed
*
* Revision 1.9 2002/11/21 20:48:06 warmerda
* added GCP copying when creating virtual dataset
*
* Revision 1.8 2002/11/06 15:05:55 warmerda
* added -a_srs
*
* Revision 1.7 2002/10/24 02:55:18 warmerda
* added the -mo to add metadata
*
* Revision 1.6 2002/10/04 20:47:31 warmerda
* fixed type override going through virtual db
*
* Revision 1.5 2002/09/11 14:22:33 warmerda
* make an effort to preserve band descriptions
*
* Revision 1.4 2002/08/27 16:49:28 dron
* Check for subdatasets in input file.
*
* Revision 1.3 2002/08/07 02:17:10 warmerda
* Fixed -projwin messages.
*
* Revision 1.2 2002/08/06 17:47:54 warmerda
* added the -projwin switch
*
* Revision 1.1 2002/06/24 19:59:57 warmerda
* New
*
* Revision 1.27 2002/06/24 19:36:25 warmerda
* carry over colortable
*
* Revision 1.26 2002/06/12 21:07:55 warmerda
* test create and createcopy capability
*
* Revision 1.25 2002/04/16 17:17:18 warmerda
* Ensure variables initialized.
*
* Revision 1.24 2002/04/16 14:00:25 warmerda
* added GDALVersionInfo
*
* Revision 1.23 2002/02/07 20:24:18 warmerda
* added -scale option
*
* Revision 1.22 2001/12/12 21:01:01 warmerda
* fixed bug in windowing logic
*
* Revision 1.21 2001/09/24 15:55:11 warmerda
* added cleanup percent done
*
* Revision 1.20 2001/08/23 03:23:46 warmerda
* added the -not_strict switch
*
* Revision 1.19 2001/07/18 05:05:12 warmerda
* added CPL_CSVID
*
* Revision 1.18 2001/03/21 15:00:49 warmerda
* Escape % signs in printf() format.
*
* Revision 1.17 2001/03/20 20:20:14 warmerda
* Added % handling, fixed completion, fixed adGeoTransform (Mark Salazar)
*
* Revision 1.16 2001/02/19 15:12:17 warmerda
* fixed windowing problem with -srcwin
*/
#include "cpl_vsi.h"
#include "cpl_conv.h"
#include "cpl_string.h"
#include "gdal_priv.h"
#include "ogr_spatialref.h"
#include "vrt/vrtdataset.h"
CPL_CVSID("$Id: gdal_translate.cpp,v 1.33 2006/04/25 14:29:28 fwarmerdam Exp $");
static int ArgIsNumeric( const char * );
static void AttachMetadata( GDALDatasetH, char ** );
static int bSubCall = FALSE;
/* ******************************************************************* */
/* Usage() */
/* ******************************************************************** */
static void Usage()
{
int iDr;
printf( "Usage: gdal_translate [--help-general]\n"
" [-ot {Byte/Int16/UInt16/UInt32/Int32/Float32/Float64/\n"
" CInt16/CInt32/CFloat32/CFloat64}] [-not_strict]\n"
" [-of format] [-b band] [-outsize xsize[%%] ysize[%%]]\n"
" [-scale [src_min src_max [dst_min dst_max]]]\n"
" [-srcwin xoff yoff xsize ysize] [-projwin ulx uly lrx lry]\n"
" [-a_srs srs_def] [-a_ullr ulx uly lrx lry] [-a_nodata value]\n"
" [-gcp pixel line easting northing [elevation]]*\n"
" [-mo \"META-TAG=VALUE\"]* [-quiet] [-sds]\n"
" [-co \"NAME=VALUE\"]*\n"
" src_dataset dst_dataset\n\n" );
printf( "%s\n\n", GDALVersionInfo( "--version" ) );
printf( "The following format drivers are configured and support output:\n" );
for( iDr = 0; iDr < GDALGetDriverCount(); iDr++ )
{
GDALDriverH hDriver = GDALGetDriver(iDr);
if( GDALGetMetadataItem( hDriver, GDAL_DCAP_CREATE, NULL ) != NULL
|| GDALGetMetadataItem( hDriver, GDAL_DCAP_CREATECOPY,
NULL ) != NULL )
{
printf( " %s: %s\n",
GDALGetDriverShortName( hDriver ),
GDALGetDriverLongName( hDriver ) );
}
}
}
/************************************************************************/
/* ProxyMain() */
/************************************************************************/
static int ProxyMain( int argc, char ** argv )
{
GDALDatasetH hDataset, hOutDS;
int i;
int nRasterXSize, nRasterYSize;
const char *pszSource=NULL, *pszDest=NULL, *pszFormat = "GTiff";
GDALDriverH hDriver;
int *panBandList = NULL, nBandCount = 0, bDefBands = TRUE;
double adfGeoTransform[6];
GDALDataType eOutputType = GDT_Unknown;
int nOXSize = 0, nOYSize = 0;
char *pszOXSize=NULL, *pszOYSize=NULL;
char **papszCreateOptions = NULL;
int anSrcWin[4], bStrict = TRUE;
const char *pszProjection;
int bScale = FALSE, bHaveScaleSrc = FALSE;
double dfScaleSrcMin=0.0, dfScaleSrcMax=255.0;
double dfScaleDstMin=0.0, dfScaleDstMax=255.0;
double dfULX, dfULY, dfLRX, dfLRY;
char **papszMetadataOptions = NULL;
char *pszOutputSRS = NULL;
int bQuiet = FALSE, bGotBounds = FALSE;
GDALProgressFunc pfnProgress = GDALTermProgress;
int nGCPCount = 0;
GDAL_GCP *pasGCPs = NULL;
int iSrcFileArg = -1, iDstFileArg = -1;
int bCopySubDatasets = FALSE;
double adfULLR[4] = { 0,0,0,0 };
int bSetNoData = FALSE;
double dfNoDataReal = 0.0;
anSrcWin[0] = 0;
anSrcWin[1] = 0;
anSrcWin[2] = 0;
anSrcWin[3] = 0;
dfULX = dfULY = dfLRX = dfLRY = 0.0;
/* -------------------------------------------------------------------- */
/* Register standard GDAL drivers, and process generic GDAL */
/* command options. */
/* -------------------------------------------------------------------- */
GDALAllRegister();
argc = GDALGeneralCmdLineProcessor( argc, &argv, 0 );
if( argc < 1 )
exit( -argc );
/* -------------------------------------------------------------------- */
/* Handle command line arguments. */
/* -------------------------------------------------------------------- */
for( i = 1; i < argc; i++ )
{
if( EQUAL(argv[i],"-of") && i < argc-1 )
pszFormat = argv[++i];
else if( EQUAL(argv[i],"-quiet") )
{
bQuiet = TRUE;
pfnProgress = GDALDummyProgress;
}
else if( EQUAL(argv[i],"-ot") && i < argc-1 )
{
int iType;
for( iType = 1; iType < GDT_TypeCount; iType++ )
{
if( GDALGetDataTypeName((GDALDataType)iType) != NULL
&& EQUAL(GDALGetDataTypeName((GDALDataType)iType),
argv[i+1]) )
{
eOutputType = (GDALDataType) iType;
}
}
if( eOutputType == GDT_Unknown )
{
printf( "Unknown output pixel type: %s\n", argv[i+1] );
Usage();
GDALDestroyDriverManager();
exit( 2 );
}
i++;
}
else if( EQUAL(argv[i],"-b") && i < argc-1 )
{
if( atoi(argv[i+1]) < 1 )
{
printf( "Unrecognizable band number (%s).\n", argv[i+1] );
Usage();
GDALDestroyDriverManager();
exit( 2 );
}
nBandCount++;
panBandList = (int *)
CPLRealloc(panBandList, sizeof(int) * nBandCount);
panBandList[nBandCount-1] = atoi(argv[++i]);
if( panBandList[nBandCount-1] != nBandCount )
bDefBands = FALSE;
}
else if( EQUAL(argv[i],"-not_strict") )
bStrict = FALSE;
else if( EQUAL(argv[i],"-sds") )
bCopySubDatasets = TRUE;
else if( EQUAL(argv[i],"-gcp") && i < argc - 4 )
{
/* -gcp pixel line easting northing [elev] */
nGCPCount++;
pasGCPs = (GDAL_GCP *)
CPLRealloc( pasGCPs, sizeof(GDAL_GCP) * nGCPCount );
GDALInitGCPs( 1, pasGCPs + nGCPCount - 1 );
pasGCPs[nGCPCount-1].dfGCPPixel = atof(argv[++i]);
pasGCPs[nGCPCount-1].dfGCPLine = atof(argv[++i]);
pasGCPs[nGCPCount-1].dfGCPX = atof(argv[++i]);
pasGCPs[nGCPCount-1].dfGCPY = atof(argv[++i]);
if( argv[i+1] != NULL
&& (atof(argv[i+1]) != 0.0 || argv[i+1][0] == '0') )
pasGCPs[nGCPCount-1].dfGCPZ = atof(argv[++i]);
/* should set id and info? */
}
else if( EQUAL(argv[i],"-a_nodata") && i < argc - 1 )
{
bSetNoData = TRUE;
dfNoDataReal = atof(argv[i+1]);
i += 1;
}
else if( EQUAL(argv[i],"-a_ullr") && i < argc - 4 )
{
adfULLR[0] = atof(argv[i+1]);
adfULLR[1] = atof(argv[i+2]);
adfULLR[2] = atof(argv[i+3]);
adfULLR[3] = atof(argv[i+4]);
bGotBounds = TRUE;
i += 4;
}
else if( EQUAL(argv[i],"-co") && i < argc-1 )
{
papszCreateOptions = CSLAddString( papszCreateOptions, argv[++i] );
}
else if( EQUAL(argv[i],"-scale") )
{
bScale = TRUE;
if( i < argc-2 && ArgIsNumeric(argv[i+1]) )
{
bHaveScaleSrc = TRUE;
dfScaleSrcMin = atof(argv[i+1]);
dfScaleSrcMax = atof(argv[i+2]);
i += 2;
}
if( i < argc-2 && bHaveScaleSrc && ArgIsNumeric(argv[i+1]) )
{
dfScaleDstMin = atof(argv[i+1]);
dfScaleDstMax = atof(argv[i+2]);
i += 2;
}
else
{
dfScaleDstMin = 0.0;
dfScaleDstMax = 255.999;
}
}
else if( EQUAL(argv[i],"-mo") && i < argc-1 )
{
papszMetadataOptions = CSLAddString( papszMetadataOptions,
argv[++i] );
}
else if( EQUAL(argv[i],"-outsize") && i < argc-2 )
{
pszOXSize = argv[++i];
pszOYSize = argv[++i];
}
else if( EQUAL(argv[i],"-srcwin") && i < argc-4 )
{
anSrcWin[0] = atoi(argv[++i]);
anSrcWin[1] = atoi(argv[++i]);
anSrcWin[2] = atoi(argv[++i]);
anSrcWin[3] = atoi(argv[++i]);
}
else if( EQUAL(argv[i],"-projwin") && i < argc-4 )
{
dfULX = atof(argv[++i]);
dfULY = atof(argv[++i]);
dfLRX = atof(argv[++i]);
dfLRY = atof(argv[++i]);
}
else if( EQUAL(argv[i],"-a_srs") && i < argc-1 )
{
OGRSpatialReference oOutputSRS;
if( oOutputSRS.SetFromUserInput( argv[i+1] ) != OGRERR_NONE )
{
fprintf( stderr, "Failed to process SRS definition: %s\n",
argv[i+1] );
GDALDestroyDriverManager();
exit( 1 );
}
oOutputSRS.exportToWkt( &pszOutputSRS );
i++;
}
else if( argv[i][0] == '-' )
{
printf( "Option %s incomplete, or not recognised.\n\n",
argv[i] );
Usage();
GDALDestroyDriverManager();
exit( 2 );
}
else if( pszSource == NULL )
{
iSrcFileArg = i;
pszSource = argv[i];
}
else if( pszDest == NULL )
{
pszDest = argv[i];
iDstFileArg = i;
}
else
{
printf( "Too many command options.\n\n" );
Usage();
GDALDestroyDriverManager();
exit( 2 );
}
}
if( pszDest == NULL )
{
Usage();
GDALDestroyDriverManager();
exit( 10 );
}
/* -------------------------------------------------------------------- */
/* Attempt to open source file. */
/* -------------------------------------------------------------------- */
hDataset = GDALOpenShared( pszSource, GA_ReadOnly );
if( hDataset == NULL )
{
fprintf( stderr,
"GDALOpen failed - %d\n%s\n",
CPLGetLastErrorNo(), CPLGetLastErrorMsg() );
GDALDestroyDriverManager();
exit( 1 );
}
/* -------------------------------------------------------------------- */
/* Handle subdatasets. */
/* -------------------------------------------------------------------- */
if( CSLCount(GDALGetMetadata( hDataset, "SUBDATASETS" )) > 0 )
{
if( !bCopySubDatasets )
{
fprintf( stderr,
"Input file contains subdatasets. Please, select one of them for reading.\n" );
}
else
{
char **papszSubdatasets = GDALGetMetadata(hDataset,"SUBDATASETS");
char *pszSubDest = (char *) CPLMalloc(strlen(pszDest)+32);
int i;
int bOldSubCall = bSubCall;
argv[iDstFileArg] = pszSubDest;
bSubCall = TRUE;
for( i = 0; papszSubdatasets[i] != NULL; i += 2 )
{
argv[iSrcFileArg] = strstr(papszSubdatasets[i],"=")+1;
sprintf( pszSubDest, "%s%d", pszDest, i/2 + 1 );
if( ProxyMain( argc, argv ) != 0 )
break;
}
bSubCall = bOldSubCall;
CPLFree( pszSubDest );
}
GDALClose( hDataset );
if( !bSubCall )
{
GDALDumpOpenDatasets( stderr );
GDALDestroyDriverManager();
}
return 1;
}
/* -------------------------------------------------------------------- */
/* Collect some information from the source file. */
/* -------------------------------------------------------------------- */
nRasterXSize = GDALGetRasterXSize( hDataset );
nRasterYSize = GDALGetRasterYSize( hDataset );
if( !bQuiet )
printf( "Input file size is %d, %d\n", nRasterXSize, nRasterYSize );
if( anSrcWin[2] == 0 && anSrcWin[3] == 0 )
{
anSrcWin[2] = nRasterXSize;
anSrcWin[3] = nRasterYSize;
}
/* -------------------------------------------------------------------- */
/* Build band list to translate */
/* -------------------------------------------------------------------- */
if( nBandCount == 0 )
{
nBandCount = GDALGetRasterCount( hDataset );
panBandList = (int *) CPLMalloc(sizeof(int)*nBandCount);
for( i = 0; i < nBandCount; i++ )
panBandList[i] = i+1;
}
else
{
for( i = 0; i < nBandCount; i++ )
{
if( panBandList[i] < 1 || panBandList[i] > GDALGetRasterCount(hDataset) )
{
fprintf( stderr,
"Band %d requested, but only bands 1 to %d available.\n",
panBandList[i], GDALGetRasterCount(hDataset) );
GDALDestroyDriverManager();
exit( 2 );
}
}
if( nBandCount != GDALGetRasterCount( hDataset ) )
bDefBands = FALSE;
}
/* -------------------------------------------------------------------- */
/* Compute the source window from the projected source window */
/* if the projected coordinates were provided. Note that the */
/* projected coordinates are in ulx, uly, lrx, lry format, */
/* while the anSrcWin is xoff, yoff, xsize, ysize with the */
/* xoff,yoff being the ulx, uly in pixel/line. */
/* -------------------------------------------------------------------- */
if( dfULX != 0.0 || dfULY != 0.0
|| dfLRX != 0.0 || dfLRY != 0.0 )
{
double adfGeoTransform[6];
GDALGetGeoTransform( hDataset, adfGeoTransform );
if( adfGeoTransform[2] != 0.0 || adfGeoTransform[4] != 0.0 )
{
fprintf( stderr,
"The -projwin option was used, but the geotransform is\n"
"rotated. This configuration is not supported.\n" );
GDALClose( hDataset );
CPLFree( panBandList );
GDALDestroyDriverManager();
exit( 1 );
}
anSrcWin[0] = (int)
((dfULX - adfGeoTransform[0]) / adfGeoTransform[1] + 0.001);
anSrcWin[1] = (int)
((dfULY - adfGeoTransform[3]) / adfGeoTransform[5] + 0.001);
anSrcWin[2] = (int) ((dfLRX - dfULX) / adfGeoTransform[1] + 0.5);
anSrcWin[3] = (int) ((dfLRY - dfULY) / adfGeoTransform[5] + 0.5);
if( !bQuiet )
fprintf( stdout,
"Computed -srcwin %d %d %d %d from projected window.\n",
anSrcWin[0],
anSrcWin[1],
anSrcWin[2],
anSrcWin[3] );
if( anSrcWin[0] < 0 || anSrcWin[1] < 0
|| anSrcWin[0] + anSrcWin[2] > GDALGetRasterXSize(hDataset)
|| anSrcWin[1] + anSrcWin[3] > GDALGetRasterYSize(hDataset) )
{
fprintf( stderr,
"Computed -srcwin falls outside raster size of %dx%d.\n",
GDALGetRasterXSize(hDataset),
GDALGetRasterYSize(hDataset) );
exit( 1 );
}
}
/* -------------------------------------------------------------------- */
/* Verify source window. */
/* -------------------------------------------------------------------- */
if( anSrcWin[0] < 0 || anSrcWin[1] < 0
|| anSrcWin[2] <= 0 || anSrcWin[3] <= 0
|| anSrcWin[0] + anSrcWin[2] > GDALGetRasterXSize(hDataset)
|| anSrcWin[1] + anSrcWin[3] > GDALGetRasterYSize(hDataset) )
{
fprintf( stderr,
"-srcwin %d %d %d %d falls outside raster size of %dx%d\n"
"or is otherwise illegal.\n",
anSrcWin[0],
anSrcWin[1],
anSrcWin[2],
anSrcWin[3],
GDALGetRasterXSize(hDataset),
GDALGetRasterYSize(hDataset) );
exit( 1 );
}
/* -------------------------------------------------------------------- */
/* Find the output driver. */
/* -------------------------------------------------------------------- */
hDriver = GDALGetDriverByName( pszFormat );
if( hDriver == NULL )
{
int iDr;
printf( "Output driver `%s' not recognised.\n", pszFormat );
printf( "The following format drivers are configured and support output:\n" );
for( iDr = 0; iDr < GDALGetDriverCount(); iDr++ )
{
GDALDriverH hDriver = GDALGetDriver(iDr);
if( GDALGetMetadataItem( hDriver, GDAL_DCAP_CREATE, NULL ) != NULL
|| GDALGetMetadataItem( hDriver, GDAL_DCAP_CREATECOPY,
NULL ) != NULL )
{
printf( " %s: %s\n",
GDALGetDriverShortName( hDriver ),
GDALGetDriverLongName( hDriver ) );
}
}
printf( "\n" );
Usage();
GDALClose( hDataset );
CPLFree( panBandList );
GDALDestroyDriverManager();
CSLDestroy( argv );
CSLDestroy( papszCreateOptions );
exit( 1 );
}
/* -------------------------------------------------------------------- */
/* The short form is to CreateCopy(). We use this if the input */
/* matches the whole dataset. Eventually we should rewrite */
/* this entire program to use virtual datasets to construct a */
/* virtual input source to copy from. */
/* -------------------------------------------------------------------- */
if( eOutputType == GDT_Unknown
&& !bScale && CSLCount(papszMetadataOptions) == 0 && bDefBands
&& anSrcWin[0] == 0 && anSrcWin[1] == 0
&& anSrcWin[2] == GDALGetRasterXSize(hDataset)
&& anSrcWin[3] == GDALGetRasterYSize(hDataset)
&& pszOXSize == NULL && pszOYSize == NULL
&& nGCPCount == 0 && !bGotBounds
&& pszOutputSRS == NULL && !bSetNoData )
{
hOutDS = GDALCreateCopy( hDriver, pszDest, hDataset,
bStrict, papszCreateOptions,
pfnProgress, NULL );
if( hOutDS != NULL )
GDALClose( hOutDS );
GDALClose( hDataset );
CPLFree( panBandList );
if( !bSubCall )
{
GDALDumpOpenDatasets( stderr );
GDALDestroyDriverManager();
}
CSLDestroy( argv );
CSLDestroy( papszCreateOptions );
return hOutDS == NULL;
}
/* -------------------------------------------------------------------- */
/* Establish some parameters. */
/* -------------------------------------------------------------------- */
if( pszOXSize == NULL )
{
nOXSize = anSrcWin[2];
nOYSize = anSrcWin[3];
}
else
{
nOXSize = (int) ((pszOXSize[strlen(pszOXSize)-1]=='%'
? atof(pszOXSize)/100*anSrcWin[2] : atoi(pszOXSize)));
nOYSize = (int) ((pszOYSize[strlen(pszOYSize)-1]=='%'
? atof(pszOYSize)/100*anSrcWin[3] : atoi(pszOYSize)));
}
/* ==================================================================== */
/* Create a virtual dataset. */
/* ==================================================================== */
VRTDataset *poVDS;
/* -------------------------------------------------------------------- */
/* Make a virtual clone. */
/* -------------------------------------------------------------------- */
poVDS = new VRTDataset( nOXSize, nOYSize );
if( nGCPCount == 0 )
{
if( pszOutputSRS != NULL )
{
poVDS->SetProjection( pszOutputSRS );
}
else
{
pszProjection = GDALGetProjectionRef( hDataset );
if( pszProjection != NULL && strlen(pszProjection) > 0 )
poVDS->SetProjection( pszProjection );
}
}
if( bGotBounds )
{
adfGeoTransform[0] = adfULLR[0];
adfGeoTransform[1] = (adfULLR[2] - adfULLR[0]) / nOXSize;
adfGeoTransform[2] = 0.0;
adfGeoTransform[3] = adfULLR[1];
adfGeoTransform[4] = 0.0;
adfGeoTransform[5] = (adfULLR[3] - adfULLR[1]) / nOYSize;
poVDS->SetGeoTransform( adfGeoTransform );
}
else if( GDALGetGeoTransform( hDataset, adfGeoTransform ) == CE_None
&& nGCPCount == 0 )
{
adfGeoTransform[0] += anSrcWin[0] * adfGeoTransform[1]
+ anSrcWin[1] * adfGeoTransform[2];
adfGeoTransform[3] += anSrcWin[0] * adfGeoTransform[4]
+ anSrcWin[1] * adfGeoTransform[5];
adfGeoTransform[1] *= anSrcWin[2] / (double) nOXSize;
adfGeoTransform[2] *= anSrcWin[3] / (double) nOYSize;
adfGeoTransform[4] *= anSrcWin[2] / (double) nOXSize;
adfGeoTransform[5] *= anSrcWin[3] / (double) nOYSize;
poVDS->SetGeoTransform( adfGeoTransform );
}
if( nGCPCount != 0 )
{
const char *pszGCPProjection = pszOutputSRS;
if( pszGCPProjection == NULL )
pszGCPProjection = GDALGetGCPProjection( hDataset );
if( pszGCPProjection == NULL )
pszGCPProjection = "";
poVDS->SetGCPs( nGCPCount, pasGCPs, pszGCPProjection );
GDALDeinitGCPs( nGCPCount, pasGCPs );
CPLFree( pasGCPs );
}
else if( GDALGetGCPCount( hDataset ) > 0 )
{
GDAL_GCP *pasGCPs;
int nGCPs = GDALGetGCPCount( hDataset );
pasGCPs = GDALDuplicateGCPs( nGCPs, GDALGetGCPs( hDataset ) );
for( i = 0; i < nGCPs; i++ )
{
pasGCPs[i].dfGCPPixel -= anSrcWin[0];
pasGCPs[i].dfGCPLine -= anSrcWin[1];
pasGCPs[i].dfGCPPixel *= (nOXSize / (double) anSrcWin[2] );
pasGCPs[i].dfGCPLine *= (nOYSize / (double) anSrcWin[3] );
}
poVDS->SetGCPs( nGCPs, pasGCPs,
GDALGetGCPProjection( hDataset ) );
GDALDeinitGCPs( nGCPs, pasGCPs );
CPLFree( pasGCPs );
}
poVDS->SetMetadata( ((GDALDataset*)hDataset)->GetMetadata() );
AttachMetadata( (GDALDatasetH) poVDS, papszMetadataOptions );
for( i = 0; i < nBandCount; i++ )
{
VRTSourcedRasterBand *poVRTBand;
GDALRasterBand *poSrcBand;
GDALDataType eBandType;
poSrcBand = ((GDALDataset *)
hDataset)->GetRasterBand(panBandList[i]);
/* -------------------------------------------------------------------- */
/* Select output data type to match source. */
/* -------------------------------------------------------------------- */
if( eOutputType == GDT_Unknown )
eBandType = poSrcBand->GetRasterDataType();
else
eBandType = eOutputType;
/* -------------------------------------------------------------------- */
/* Create this band. */
/* -------------------------------------------------------------------- */
poVDS->AddBand( eBandType, NULL );
poVRTBand = (VRTSourcedRasterBand *) poVDS->GetRasterBand( i+1 );
/* -------------------------------------------------------------------- */
/* Do we need to collect scaling information? */
/* -------------------------------------------------------------------- */
double dfScale=1.0, dfOffset=0.0;
if( bScale && !bHaveScaleSrc )
{
double adfCMinMax[2];
GDALComputeRasterMinMax( poSrcBand, TRUE, adfCMinMax );
dfScaleSrcMin = adfCMinMax[0];
dfScaleSrcMax = adfCMinMax[1];
}
if( bScale )
{
if( dfScaleSrcMax == dfScaleSrcMin )
dfScaleSrcMax += 0.1;
if( dfScaleDstMax == dfScaleDstMin )
dfScaleDstMax += 0.1;
dfScale = (dfScaleDstMax - dfScaleDstMin)
/ (dfScaleSrcMax - dfScaleSrcMin);
dfOffset = -1 * dfScaleSrcMin * dfScale + dfScaleDstMin;
}
/* -------------------------------------------------------------------- */
/* Create a simple or complex data source depending on the */
/* translation type required. */
/* -------------------------------------------------------------------- */
if( bScale )
{
poVRTBand->AddComplexSource( poSrcBand,
anSrcWin[0], anSrcWin[1],
anSrcWin[2], anSrcWin[3],
0, 0, nOXSize, nOYSize,
dfOffset, dfScale );
}
else
poVRTBand->AddSimpleSource( poSrcBand,
anSrcWin[0], anSrcWin[1],
anSrcWin[2], anSrcWin[3],
0, 0, nOXSize, nOYSize );
/* -------------------------------------------------------------------- */
/* copy over some other information of interest. */
/* -------------------------------------------------------------------- */
poVRTBand->CopyCommonInfoFrom( poSrcBand );
/* -------------------------------------------------------------------- */
/* Set a forcable nodata value? */
/* -------------------------------------------------------------------- */
if( bSetNoData )
poVRTBand->SetNoDataValue( dfNoDataReal );
}
/* -------------------------------------------------------------------- */
/* Write to the output file using CopyCreate(). */
/* -------------------------------------------------------------------- */
hOutDS = GDALCreateCopy( hDriver, pszDest, (GDALDatasetH) poVDS,
bStrict, papszCreateOptions,
pfnProgress, NULL );
if( hOutDS != NULL )
{
GDALClose( hOutDS );
}
GDALClose( (GDALDatasetH) poVDS );
GDALClose( hDataset );
CPLFree( panBandList );
if( !bSubCall )
{
GDALDumpOpenDatasets( stderr );
GDALDestroyDriverManager();
}
CSLDestroy( argv );
CSLDestroy( papszCreateOptions );
return hOutDS == NULL;
}
/************************************************************************/
/* ArgIsNumeric() */
/************************************************************************/
int ArgIsNumeric( const char *pszArg )
{
if( pszArg[0] == '-' )
pszArg++;
if( *pszArg == '\0' )
return FALSE;
while( *pszArg != '\0' )
{
if( (*pszArg < '0' || *pszArg > '9') && *pszArg != '.' )
return FALSE;
pszArg++;
}
return TRUE;
}
/************************************************************************/
/* AttachMetadata() */
/************************************************************************/
static void AttachMetadata( GDALDatasetH hDS, char **papszMetadataOptions )
{
int nCount = CSLCount(papszMetadataOptions);
int i;
for( i = 0; i < nCount; i++ )
{
char *pszKey = NULL;
const char *pszValue;
pszValue = CPLParseNameValue( papszMetadataOptions[i], &pszKey );
GDALSetMetadataItem(hDS,pszKey,pszValue,NULL);
CPLFree( pszKey );
}
CSLDestroy( papszMetadataOptions );
}
/************************************************************************/
/* main() */
/************************************************************************/
int main( int argc, char ** argv )
{
return ProxyMain( argc, argv );
}
|