source: trunk/zoo-project/zoo-services/gdal/translate/service.c @ 348

Last change on this file since 348 was 348, checked in by neteler, 12 years ago

set correctly svn propset

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-csrc
File size: 27.7 KB
RevLine 
[1]1/******************************************************************************
2 * $Id$
3 *
4 * Project:  GDAL Utilities
5 * Purpose:  GDAL Image Translator Program
6 * Author:   Frank Warmerdam, warmerdam@pobox.com
7 *
8 ******************************************************************************
9 * Copyright (c) 1998, 2002, Frank Warmerdam
10 *
11 * Permission is hereby granted, free of charge, to any person obtaining a
12 * copy of this software and associated documentation files (the "Software"),
13 * to deal in the Software without restriction, including without limitation
14 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15 * and/or sell copies of the Software, and to permit persons to whom the
16 * Software is furnished to do so, subject to the following conditions:
17 *
18 * The above copyright notice and this permission notice shall be included
19 * in all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27 * DEALINGS IN THE SOFTWARE.
28 ****************************************************************************/
29
30#include "cpl_vsi.h"
31#include "cpl_conv.h"
32#include "cpl_string.h"
33#include "gdal_priv.h"
34#include "ogr_spatialref.h"
35#include "vrtdataset.h"
36
37#include "service.h"
38
39
40CPL_CVSID("$Id$");
41
42extern "C" {
43
44
45  static void AttachMetadata( GDALDatasetH, char ** );
46  static int bSubCall = FALSE;
47
48  /************************************************************************/
49  /*                          Gdal_Translate()                            */
50  /************************************************************************/
51
52  int Gdal_Translate(maps*& conf,maps*& inputs,maps*& outputs)
53  {
54   
55    fprintf(stderr,"STARTING GDAL TRANSLATE\n");
56    fflush(stderr);
57
58    GDALDatasetH        hDataset, hOutDS;
59    int                 i;
60    int                 nRasterXSize, nRasterYSize;
61    const char          *pszSource=NULL, *pszDest=NULL, *pszFormat = "GTiff";
62    GDALDriverH         hDriver;
63    int                 *panBandList = NULL, nBandCount = 0, bDefBands = TRUE;
64    double              adfGeoTransform[6];
65    GDALDataType        eOutputType = GDT_Unknown;
66    int                 nOXSize = 0, nOYSize = 0;
67    char                *pszOXSize=NULL, *pszOYSize=NULL;
68    char                **papszCreateOptions = NULL;
69    int                 anSrcWin[4], bStrict = FALSE;
70    const char          *pszProjection;
71    int                 bScale = FALSE, bHaveScaleSrc = FALSE;
72    double              dfScaleSrcMin=0.0, dfScaleSrcMax=255.0;
73    double              dfScaleDstMin=0.0, dfScaleDstMax=255.0;
74    double              dfULX, dfULY, dfLRX, dfLRY;
75    char                **papszMetadataOptions = NULL;
76    char                *pszOutputSRS = NULL;
77    int                 bQuiet = TRUE, bGotBounds = FALSE;
78    GDALProgressFunc    pfnProgress = GDALDummyProgress;
79    int                 nGCPCount = 0;
80    GDAL_GCP            *pasGCPs = NULL;
81    int                 iSrcFileArg = -1, iDstFileArg = -1;
82    int                 bCopySubDatasets = FALSE;
83    double              adfULLR[4] = { 0,0,0,0 };
84    int                 bSetNoData = FALSE;
85    double              dfNoDataReal = 0.0;
86    int                 nRGBExpand = 0;
87
88    anSrcWin[0] = 0;
89    anSrcWin[1] = 0;
90    anSrcWin[2] = 0;
91    anSrcWin[3] = 0;
92
93    dfULX = dfULY = dfLRX = dfLRY = 0.0;
94
95    /* ----------------------------------------------------------------- */
96    /*      Register standard GDAL drivers, and process generic GDAL     */
97    /* ----------------------------------------------------------------- */
98    GDALAllRegister();
99
100    /* ----------------------------------------------------------------- */
101    /* Extract Format, InputDSN, OutputDSN parameters                    */
102    /* ----------------------------------------------------------------- */
103
104    map* tmpMap=NULL;
105
106    char dataPath[1024];
107    tmpMap=getMapFromMaps(conf,"main","dataPath");
108    if(tmpMap!=NULL)
109      sprintf(dataPath,"%s",tmpMap->value);
110    tmpMap=NULL;
111
112    char tempPath[1024];
113    tmpMap=getMapFromMaps(conf,"main","tmpPath");
114    if(tmpMap!=NULL){
115      sprintf(tempPath,"%s",tmpMap->value);
116    }
117    tmpMap=NULL;
118
119    tmpMap=getMapFromMaps(inputs,"Format","value");
120    if(tmpMap!=NULL){
121      pszFormat=tmpMap->value;
122    }
123    tmpMap=NULL;
124    tmpMap=getMapFromMaps(inputs,"InputDSN","value");
125    if(tmpMap!=NULL){
126      pszSource=(char*)malloc(sizeof(char)*(strlen(dataPath)+strlen(tmpMap->value)+4));
127      sprintf((char*)pszSource,"%s/%s.tif",dataPath,tmpMap->value);
128    }
129    tmpMap=NULL;
130    tmpMap=getMapFromMaps(inputs,"OutputDSN","value");
131    if(tmpMap!=NULL){
132      pszDest=(char*)malloc(sizeof(char)*(strlen(tempPath)+strlen(tmpMap->value)+4));
133      char *ext=new char[4];
134      ext="tif";
[284]135      if(strncasecmp(pszFormat,"AAIGRID",7)==0)
[1]136        ext="csv";
137      else 
[284]138        if(strncasecmp(pszFormat,"PNG",3)==0)
[1]139          ext="png";
140        else
[284]141          if(strncasecmp(pszFormat,"GIF",3)==0)
[1]142            ext="gif";
143          else
[284]144            if(strncasecmp(pszFormat,"JPEG",4)==0)
[1]145              ext="jpg";
146      sprintf((char*)pszDest,"%s/%s.%s",tempPath,tmpMap->value,ext);
147      fprintf(stderr,"DEBUG pszDest : %s\n",pszDest);
148    }
149    tmpMap=NULL;
150    tmpMap=getMapFromMaps(inputs,"ProjWin","value");
151    if(tmpMap!=NULL){
152      char *tmp=tmpMap->value;
153      char *t=strtok(tmp,",");
154      int cnt=0;
155      while(t!=NULL){
156        switch(cnt){
157        case 0:
158          dfULX = atof(t);
159          break;
160        case 1:
161          dfULY = atof(t);
162          break;
163        case 2:
164          dfLRX = atof(t);
165          break;
166        case 3:
167          dfLRY = atof(t);
168          break;
169        }
170        fprintf(stderr,"%s\n\n",t);
171        fprintf(stderr,"%f - %f - %f - %f\n\n",dfULX,dfULY,dfLRX,dfLRY);
172        t=strtok(NULL,",");
173        cnt++;
174      }
175    }
176    tmpMap=NULL;
177    tmpMap=getMapFromMaps(inputs,"SRS","value");
178    if(tmpMap!=NULL){
179      OGRSpatialReference oOutputSRS;
180      if( oOutputSRS.SetFromUserInput( tmpMap->value ) != OGRERR_NONE )
181        {
182          fprintf( stderr, "Failed to process SRS definition: %s\n", 
183                   tmpMap->value );
184            /**
185             * Avoiding GDALDestroyDriverManager() call
186             */
187          exit( 1 );
188        }
189      oOutputSRS.exportToWkt( &pszOutputSRS );
190    }
191    tmpMap=NULL;
192    tmpMap=getMapFromMaps(inputs,"Type","value");
193    if(tmpMap!=NULL){
194      int       iType;
195     
196      for( iType = 1; iType < GDT_TypeCount; iType++ )
197        {
198          if( GDALGetDataTypeName((GDALDataType)iType) != NULL
199              && EQUAL(GDALGetDataTypeName((GDALDataType)iType),
200                       tmpMap->value) )
201            {
202              eOutputType = (GDALDataType) iType;
203            }
204        }
205     
206      if( eOutputType == GDT_Unknown )
207        {
208          printf( "Unknown output pixel type: %s\n", tmpMap->value );
209          /**
210           * Avoiding GDALDestroyDriverManager() call
211           */
212          exit( 2 );
213        }
214    }
215    fprintf(stderr,"==%s %s %s %==\n",pszFormat,pszSource,pszDest);
216    fflush(stderr);
217
218    if( pszDest == NULL ){
219        fprintf(stderr,"exit line 416");
220        fflush(stderr);
221        /**
222         * Avoiding GDALDestroyDriverManager() call
223         */
224        exit( 10 );
225      }
226
227    if ( strcmp(pszSource, pszDest) == 0)
228      {
229        fprintf(stderr, "Source and destination datasets must be different.\n");
230        fflush(stderr);
231        /**
232         * Avoiding GDALDestroyDriverManager() call
233         */
234        exit( 1 );
235      }
236
237    /* ----------------------------------------------------------------- */
238    /*      Attempt to open source file.                                 */
239    /* ----------------------------------------------------------------- */
240
241    hDataset = GDALOpenShared( pszSource, GA_ReadOnly );
242   
243    if( hDataset == NULL ){
244        fprintf( stderr,
245                 "GDALOpen failed - %d\n%s\n",
246                 CPLGetLastErrorNo(), CPLGetLastErrorMsg() );
247        fflush(stderr);
248        /**
249         * Avoiding GDALDestroyDriverManager() call
250         */
251        exit( 1 );
252      }
253
254    /* ----------------------------------------------------------------- */
255    /*      Handle subdatasets.                                          */
256    /* ----------------------------------------------------------------- */
257    if( !bCopySubDatasets
258        && CSLCount(GDALGetMetadata( hDataset, "SUBDATASETS" )) > 0 
259        && GDALGetRasterCount(hDataset) == 0 )
260      {
261        fprintf( stderr,
262                 "Input file contains subdatasets. Please, select one of them for reading.\n" );
263        fflush(stderr);
264        GDALClose( hDataset );
265        /**
266         * Avoiding GDALDestroyDriverManager() call
267         */
268        exit( 1 );
269      }
270
271    if( CSLCount(GDALGetMetadata( hDataset, "SUBDATASETS" )) > 0 
272        && bCopySubDatasets )
273      {
274        char **papszSubdatasets = GDALGetMetadata(hDataset,"SUBDATASETS");
275        char *pszSubDest = (char *) CPLMalloc(strlen(pszDest)+32);
276        int i;
277        int bOldSubCall = bSubCall;
278       
279        //argv[iDstFileArg] = pszSubDest;
280        bSubCall = TRUE;
281        for( i = 0; papszSubdatasets[i] != NULL; i += 2 )
282          {
283            //argv[iSrcFileArg] = strstr(papszSubdatasets[i],"=")+1;
284            sprintf( pszSubDest, "%s%d", pszDest, i/2 + 1 );
285            /*if( ProxyMain( argc, argv ) != 0 )
286              break;*/
287          }
288       
289        bSubCall = bOldSubCall;
290        CPLFree( pszSubDest );
291
292        GDALClose( hDataset );
293
294        if( !bSubCall )
295          {
296            GDALDumpOpenDatasets( stderr );
297            fflush(stderr);
298            /**
299             * Avoiding GDALDestroyDriverManager() call
300             */
301          }
302        return 1;
303      }
304
305    /* ----------------------------------------------------------------- */
306    /*      Collect some information from the source file.               */
307    /* ----------------------------------------------------------------- */
308    nRasterXSize = GDALGetRasterXSize( hDataset );
309    nRasterYSize = GDALGetRasterYSize( hDataset );
310
311    if( !bQuiet )
312      printf( "Input file size is %d, %d\n", nRasterXSize, nRasterYSize );
313
314    if( anSrcWin[2] == 0 && anSrcWin[3] == 0 ){
315        anSrcWin[2] = nRasterXSize;
316        anSrcWin[3] = nRasterYSize;
317      }
318
319    /* ----------------------------------------------------------------- */
320    /*  Build band list to translate                                     */
321    /* ----------------------------------------------------------------- */
322    if( nBandCount == 0 ){
323        nBandCount = GDALGetRasterCount( hDataset );
324        if( nBandCount == 0 ){
325            fprintf( stderr, "Input file has no bands, and so cannot be translated.\n" );
326            fflush(stderr);
327            /**
328             * Avoiding GDALDestroyDriverManager() call
329             */
330            exit(1 );
331          }
332
333        panBandList = (int *) CPLMalloc(sizeof(int)*nBandCount);
334        for( i = 0; i < nBandCount; i++ )
335          panBandList[i] = i+1;
336      }
337    else
338      {
339        for( i = 0; i < nBandCount; i++ )
340          {
341            if( panBandList[i] < 1 || panBandList[i] > GDALGetRasterCount(hDataset) )
342              {
343                fprintf( stderr, 
344                         "Band %d requested, but only bands 1 to %d available.\n",
345                         panBandList[i], GDALGetRasterCount(hDataset) );
346                fflush(stderr);
347                /**
348                 * Avoiding GDALDestroyDriverManager() call
349                 */
350                exit( 2 );
351              }
352          }
353
354        if( nBandCount != GDALGetRasterCount( hDataset ) )
355          bDefBands = FALSE;
356      }
357
358    /* ----------------------------------------------------------------- */
359    /*   Compute the source window from the projected source window      */
360    /*   if the projected coordinates were provided.  Note that the      */
361    /*   projected coordinates are in ulx, uly, lrx, lry format,         */
362    /*   while the anSrcWin is xoff, yoff, xsize, ysize with the         */
363    /*   xoff,yoff being the ulx, uly in pixel/line.                     */
364    /* ----------------------------------------------------------------- */
365    if( dfULX != 0.0 || dfULY != 0.0 
366        || dfLRX != 0.0 || dfLRY != 0.0 )
367      {
368        double  adfGeoTransform[6];
369
370        GDALGetGeoTransform( hDataset, adfGeoTransform );
371
372        if( adfGeoTransform[2] != 0.0 || adfGeoTransform[4] != 0.0 ){
373            fprintf( stderr, 
374                     "The -projwin option was used, but the geotransform is\n"
375                     "rotated.  This configuration is not supported.\n" );
376            GDALClose( hDataset );
377            CPLFree( panBandList );
378            fflush(stderr);
379            /**
380             * Avoiding GDALDestroyDriverManager() call
381             */
382            exit( 1 );
383          }
384
385        anSrcWin[0] = (int) 
386          ((dfULX - adfGeoTransform[0]) / adfGeoTransform[1] + 0.001);
387        anSrcWin[1] = (int) 
388          ((dfULY - adfGeoTransform[3]) / adfGeoTransform[5] + 0.001);
389
390        anSrcWin[2] = (int) ((dfLRX - dfULX) / adfGeoTransform[1] + 0.5);
391        anSrcWin[3] = (int) ((dfLRY - dfULY) / adfGeoTransform[5] + 0.5);
392
393        if( !bQuiet )
394          fprintf( stdout, 
395                   "Computed -srcwin %d %d %d %d from projected window.\n",
396                   anSrcWin[0], 
397                   anSrcWin[1], 
398                   anSrcWin[2], 
399                   anSrcWin[3] );
400       
401        if( anSrcWin[0] < 0 || anSrcWin[1] < 0 
402            || anSrcWin[0] + anSrcWin[2] > GDALGetRasterXSize(hDataset) 
403            || anSrcWin[1] + anSrcWin[3] > GDALGetRasterYSize(hDataset) )
404          {
405            fprintf( stderr, 
406                     "Computed -srcwin falls outside raster size of %dx%d.\n",
407                     GDALGetRasterXSize(hDataset), 
408                     GDALGetRasterYSize(hDataset) );
409            exit( 1 );
410          }
411      }
412
413    /* ----------------------------------------------------------------- */
414    /*      Verify source window.                                        */
415    /* ----------------------------------------------------------------- */
416    if( anSrcWin[0] < 0 || anSrcWin[1] < 0 
417        || anSrcWin[2] <= 0 || anSrcWin[3] <= 0
418        || anSrcWin[0] + anSrcWin[2] > GDALGetRasterXSize(hDataset) 
419        || anSrcWin[1] + anSrcWin[3] > GDALGetRasterYSize(hDataset) )
420      {
421        fprintf( stderr, 
422                 "-srcwin %d %d %d %d falls outside raster size of %dx%d\n"
423                 "or is otherwise illegal.\n",
424                 anSrcWin[0],
425                 anSrcWin[1],
426                 anSrcWin[2],
427                 anSrcWin[3],
428                 GDALGetRasterXSize(hDataset), 
429                 GDALGetRasterYSize(hDataset) );
430        exit( 1 );
431      }
432
433    /* ----------------------------------------------------------------- */
434    /*      Find the output driver.                                      */
435    /* ----------------------------------------------------------------- */
436    hDriver = GDALGetDriverByName( pszFormat );
437    if( hDriver == NULL )
438      {
439        int     iDr;
440       
441        printf( "Output driver `%s' not recognised.\n", pszFormat );
442        printf( "The following format drivers are configured and support output:\n" );
443        for( iDr = 0; iDr < GDALGetDriverCount(); iDr++ )
444          {
445            GDALDriverH hDriver = GDALGetDriver(iDr);
446
447            if( GDALGetMetadataItem( hDriver, GDAL_DCAP_CREATE, NULL ) != NULL
448                || GDALGetMetadataItem( hDriver, GDAL_DCAP_CREATECOPY,
449                                        NULL ) != NULL )
450              {
451                printf( "  %s: %s\n",
452                        GDALGetDriverShortName( hDriver  ),
453                        GDALGetDriverLongName( hDriver ) );
454              }
455          }
456        printf( "\n" );
457       
458        GDALClose( hDataset );
459        CPLFree( panBandList );
460        fflush(stderr);
461        /**
462         * Avoiding GDALDestroyDriverManager() call
463         */
464        CSLDestroy( papszCreateOptions );
465        exit( 1 );
466      }
467
468    /* ----------------------------------------------------------------- */
469    /*   The short form is to CreateCopy().  We use this if the input    */
470    /*   matches the whole dataset.  Eventually we should rewrite        */
471    /*   this entire program to use virtual datasets to construct a      */
472    /*   virtual input source to copy from.                              */
473    /* ----------------------------------------------------------------- */
474    if( eOutputType == GDT_Unknown
475        && !bScale && CSLCount(papszMetadataOptions) == 0 && bDefBands
476        && anSrcWin[0] == 0 && anSrcWin[1] == 0 
477        && anSrcWin[2] == GDALGetRasterXSize(hDataset)
478        && anSrcWin[3] == GDALGetRasterYSize(hDataset) 
479        && pszOXSize == NULL && pszOYSize == NULL 
480        && nGCPCount == 0 && !bGotBounds
481        && pszOutputSRS == NULL && !bSetNoData
482        && nRGBExpand == 0)
483      {
484       
485        hOutDS = GDALCreateCopy( hDriver, pszDest, hDataset, 
486                                 bStrict, papszCreateOptions, 
487                                 pfnProgress, NULL );
488
489        if( hOutDS != NULL )
490          GDALClose( hOutDS );
491       
492        GDALClose( hDataset );
493
494        CPLFree( panBandList );
495
496        if( !bSubCall )
497          {
498            GDALDumpOpenDatasets( stderr );
499            /**
500             * Avoiding GDALDestroyDriverManager() call
501             */
502          }
503
504        CSLDestroy( papszCreateOptions );
505        outputs=(maps*)malloc(sizeof(maps*));
506        outputs->name="OutputedPolygon";
507        outputs->content=createMap("value",(char*)pszDest);
508        outputs->next=NULL;
509       
510        return SERVICE_SUCCEEDED;
511      }
512    fprintf(stderr,"==%s %s %s %==\n",pszFormat,pszSource,pszDest);
513    fflush(stderr);
514
515    /* ----------------------------------------------------------------- */
516    /*      Establish some parameters.                                   */
517    /* ----------------------------------------------------------------- */
518    if( pszOXSize == NULL )
519      {
520        nOXSize = anSrcWin[2];
521        nOYSize = anSrcWin[3];
522      }
523    else
524      {
525        nOXSize = (int) ((pszOXSize[strlen(pszOXSize)-1]=='%' 
526                          ? atof(pszOXSize)/100*anSrcWin[2] : atoi(pszOXSize)));
527        nOYSize = (int) ((pszOYSize[strlen(pszOYSize)-1]=='%' 
528                          ? atof(pszOYSize)/100*anSrcWin[3] : atoi(pszOYSize)));
529      }
530    fprintf(stderr,"==%s %s %s %==\n",pszFormat,pszSource,pszDest);
531    fflush(stderr);
532
533    /* ================================================================= */
534    /*      Create a virtual dataset.                                    */
535    /* ================================================================= */
536    VRTDataset *poVDS;
537       
538    /* ----------------------------------------------------------------- */
539    /*      Make a virtual clone.                                        */
540    /* ----------------------------------------------------------------- */
541    poVDS = (VRTDataset *) VRTCreate( nOXSize, nOYSize );
542
543    if( nGCPCount == 0 )
544      {
545        if( pszOutputSRS != NULL )
546          {
547            poVDS->SetProjection( pszOutputSRS );
548          }
549        else
550          {
551            pszProjection = GDALGetProjectionRef( hDataset );
552            if( pszProjection != NULL && strlen(pszProjection) > 0 )
553              poVDS->SetProjection( pszProjection );
554          }
555      }
556
557    if( bGotBounds )
558      {
559        adfGeoTransform[0] = adfULLR[0];
560        adfGeoTransform[1] = (adfULLR[2] - adfULLR[0]) / nOXSize;
561        adfGeoTransform[2] = 0.0;
562        adfGeoTransform[3] = adfULLR[1];
563        adfGeoTransform[4] = 0.0;
564        adfGeoTransform[5] = (adfULLR[3] - adfULLR[1]) / nOYSize;
565
566        poVDS->SetGeoTransform( adfGeoTransform );
567      }
568
569    else if( GDALGetGeoTransform( hDataset, adfGeoTransform ) == CE_None
570             && nGCPCount == 0 )
571      {
572        adfGeoTransform[0] += anSrcWin[0] * adfGeoTransform[1]
573          + anSrcWin[1] * adfGeoTransform[2];
574        adfGeoTransform[3] += anSrcWin[0] * adfGeoTransform[4]
575          + anSrcWin[1] * adfGeoTransform[5];
576       
577        adfGeoTransform[1] *= anSrcWin[2] / (double) nOXSize;
578        adfGeoTransform[2] *= anSrcWin[3] / (double) nOYSize;
579        adfGeoTransform[4] *= anSrcWin[2] / (double) nOXSize;
580        adfGeoTransform[5] *= anSrcWin[3] / (double) nOYSize;
581       
582        poVDS->SetGeoTransform( adfGeoTransform );
583      }
584
585    if( nGCPCount != 0 )
586      {
587        const char *pszGCPProjection = pszOutputSRS;
588
589        if( pszGCPProjection == NULL )
590          pszGCPProjection = GDALGetGCPProjection( hDataset );
591        if( pszGCPProjection == NULL )
592          pszGCPProjection = "";
593
594        poVDS->SetGCPs( nGCPCount, pasGCPs, pszGCPProjection );
595
596        GDALDeinitGCPs( nGCPCount, pasGCPs );
597        CPLFree( pasGCPs );
598      }
599
600    else if( GDALGetGCPCount( hDataset ) > 0 )
601      {
602        GDAL_GCP *pasGCPs;
603        int       nGCPs = GDALGetGCPCount( hDataset );
604
605        pasGCPs = GDALDuplicateGCPs( nGCPs, GDALGetGCPs( hDataset ) );
606
607        for( i = 0; i < nGCPs; i++ )
608          {
609            pasGCPs[i].dfGCPPixel -= anSrcWin[0];
610            pasGCPs[i].dfGCPLine  -= anSrcWin[1];
611            pasGCPs[i].dfGCPPixel *= (nOXSize / (double) anSrcWin[2] );
612            pasGCPs[i].dfGCPLine  *= (nOYSize / (double) anSrcWin[3] );
613          }
614           
615        poVDS->SetGCPs( nGCPs, pasGCPs,
616                        GDALGetGCPProjection( hDataset ) );
617
618        GDALDeinitGCPs( nGCPs, pasGCPs );
619        CPLFree( pasGCPs );
620      }
621
622    /* ----------------------------------------------------------------- */
623    /*      Transfer generally applicable metadata.                      */
624    /* ----------------------------------------------------------------- */
625    poVDS->SetMetadata( ((GDALDataset*)hDataset)->GetMetadata() );
626    AttachMetadata( (GDALDatasetH) poVDS, papszMetadataOptions );
627    fprintf(stderr,"Transfer generally applicable metadata.\n");
628    fflush(stderr);
629
630    /* ----------------------------------------------------------------- */
631    /*      Transfer metadata that remains valid if the spatial          */
632    /*      arrangement of the data is unaltered.                        */
633    /* ----------------------------------------------------------------- */
634    if( anSrcWin[0] == 0 && anSrcWin[1] == 0 
635        && anSrcWin[2] == GDALGetRasterXSize(hDataset)
636        && anSrcWin[3] == GDALGetRasterYSize(hDataset) 
637        && pszOXSize == NULL && pszOYSize == NULL )
638      {
639        char **papszMD;
640
641        papszMD = ((GDALDataset*)hDataset)->GetMetadata("RPC");
642        if( papszMD != NULL )
643          poVDS->SetMetadata( papszMD, "RPC" );
644      }
645
646    if (nRGBExpand != 0)
647      nBandCount += nRGBExpand - 1;
648
649    /* ================================================================= */
650    /*      Process all bands.                                           */
651    /* ================================================================= */
652    for( i = 0; i < nBandCount; i++ )
653      {
654        VRTSourcedRasterBand   *poVRTBand;
655        GDALRasterBand  *poSrcBand;
656        GDALDataType    eBandType;
657
658        if (nRGBExpand != 0 && i < nRGBExpand)
659          {
660            poSrcBand = ((GDALDataset *) 
661                         hDataset)->GetRasterBand(panBandList[0]);
662            if (poSrcBand->GetColorTable() == NULL)
663              {
664                fprintf(stderr, "Error : band %d has no color table\n", panBandList[0]);
665                GDALClose( hDataset );
666                CPLFree( panBandList );
667                fflush(stderr);
668                /**
669                 * Avoiding GDALDestroyDriverManager() call
670                 */
671                CSLDestroy( papszCreateOptions );
672                exit( 1 );
673              }
674          }
675        else
676          poSrcBand = ((GDALDataset *) 
677                       hDataset)->GetRasterBand(panBandList[i]);
678
679        /* ------------------------------------------------------------ */
680        /*      Select output data type to match source.                */
681        /* ------------------------------------------------------------ */
682        if( eOutputType == GDT_Unknown )
683          eBandType = poSrcBand->GetRasterDataType();
684        else
685          eBandType = eOutputType;
686
687        /* ------------------------------------------------------------ */
688        /*      Create this band.                                       */
689        /* ------------------------------------------------------------ */
690        poVDS->AddBand( eBandType, NULL );
691        poVRTBand = (VRTSourcedRasterBand *) poVDS->GetRasterBand( i+1 );
692           
693        /* ------------------------------------------------------------ */
694        /*      Do we need to collect scaling information?              */
695        /* ------------------------------------------------------------ */
696        double dfScale=1.0, dfOffset=0.0;
697
698        if( bScale && !bHaveScaleSrc )
699          {
700            double      adfCMinMax[2];
701            GDALComputeRasterMinMax( poSrcBand, TRUE, adfCMinMax );
702            dfScaleSrcMin = adfCMinMax[0];
703            dfScaleSrcMax = adfCMinMax[1];
704          }
705
706        if( bScale )
707          {
708            if( dfScaleSrcMax == dfScaleSrcMin )
709              dfScaleSrcMax += 0.1;
710            if( dfScaleDstMax == dfScaleDstMin )
711              dfScaleDstMax += 0.1;
712
713            dfScale = (dfScaleDstMax - dfScaleDstMin) 
714              / (dfScaleSrcMax - dfScaleSrcMin);
715            dfOffset = -1 * dfScaleSrcMin * dfScale + dfScaleDstMin;
716          }
717
718        /* ------------------------------------------------------------ */
719        /*      Create a simple or complex data source depending on the */
720        /*      translation type required.                              */
721        /* ------------------------------------------------------------ */
722        if( bScale || (nRGBExpand != 0 && i < nRGBExpand) )
723          {
724            poVRTBand->AddComplexSource( poSrcBand,
725                                         anSrcWin[0], anSrcWin[1], 
726                                         anSrcWin[2], anSrcWin[3], 
727                                         0, 0, nOXSize, nOYSize,
728                                         dfOffset, dfScale,
729                                         VRT_NODATA_UNSET,
730                                         (nRGBExpand != 0 && i < nRGBExpand) ? i + 1 : 0 );
731          }
732        else
733          poVRTBand->AddSimpleSource( poSrcBand,
734                                      anSrcWin[0], anSrcWin[1], 
735                                      anSrcWin[2], anSrcWin[3], 
736                                      0, 0, nOXSize, nOYSize );
737
738        /* In case of color table translate, we only set the color interpretation */
739        /* other info copied by CopyCommonInfoFrom are not relevant in RGB expansion */
740        if (nRGBExpand != 0 && i < nRGBExpand)
741          {
742            poVRTBand->SetColorInterpretation( (GDALColorInterp) (GCI_RedBand + i) );
743          }
744        else
745          {
746            /* --------------------------------------------------------- */
747            /*      copy over some other information of interest.        */
748            /* --------------------------------------------------------- */
749            poVRTBand->CopyCommonInfoFrom( poSrcBand );
750          }
751
752        /* ------------------------------------------------------------- */
753        /*      Set a forcable nodata value?                             */
754        /* ------------------------------------------------------------- */
755        if( bSetNoData )
756          poVRTBand->SetNoDataValue( dfNoDataReal );
757      }
758
759    /* ----------------------------------------------------------------- */
760    /*      Write to the output file using CopyCreate().                 */
761    /* ----------------------------------------------------------------- */
762    fprintf(stderr,"DEBUG pszDest %s\n",pszDest);
763    hOutDS = GDALCreateCopy( hDriver, pszDest, (GDALDatasetH) poVDS,
764                             bStrict, papszCreateOptions, 
765                             pfnProgress, NULL );
766    fprintf(stderr,"DEBUG pszDest %s\n",pszDest);
767    fflush(stderr);
768
769    if( hOutDS != NULL )
770      {
771        GDALClose( hOutDS );
772      }
773   
774    GDALClose( (GDALDatasetH) poVDS );
775       
776    GDALClose( hDataset );
777
778    CPLFree( panBandList );
779   
780    CPLFree( pszOutputSRS );
781
782    if( !bSubCall )
783      {
784        GDALDumpOpenDatasets( stderr );
785        fflush(stderr);
786        /**
787         * Avoiding GDALDestroyDriverManager() call
788         */
789      }
790
791    CSLDestroy( papszCreateOptions );
792   
793    outputs=(maps*)malloc(sizeof(maps*));
794    outputs->name="OutputedPolygon";
795    outputs->content=createMap("value",(char*)pszDest);
796    outputs->next=NULL;
797
798    return SERVICE_SUCCEEDED;
799  }
800
801
802  /************************************************************************/
803  /*                           AttachMetadata()                           */
804  /************************************************************************/
805
806  static void AttachMetadata( GDALDatasetH hDS, char **papszMetadataOptions )
807
808  {
809    int nCount = CSLCount(papszMetadataOptions);
810    int i;
811
812    for( i = 0; i < nCount; i++ )
813      {
814        char    *pszKey = NULL;
815        const char *pszValue;
816       
817        pszValue = CPLParseNameValue( papszMetadataOptions[i], &pszKey );
818        GDALSetMetadataItem(hDS,pszKey,pszValue,NULL);
819        CPLFree( pszKey );
820      }
821
822    CSLDestroy( papszMetadataOptions );
823  }
824
825}
Note: See TracBrowser for help on using the repository browser.

Search

ZOO Sponsors

http://www.zoo-project.org/trac/chrome/site/img/geolabs-logo.pnghttp://www.zoo-project.org/trac/chrome/site/img/neogeo-logo.png http://www.zoo-project.org/trac/chrome/site/img/apptech-logo.png http://www.zoo-project.org/trac/chrome/site/img/3liz-logo.png http://www.zoo-project.org/trac/chrome/site/img/gateway-logo.png

Become a sponsor !

Knowledge partners

http://www.zoo-project.org/trac/chrome/site/img/ocu-logo.png http://www.zoo-project.org/trac/chrome/site/img/gucas-logo.png http://www.zoo-project.org/trac/chrome/site/img/polimi-logo.png http://www.zoo-project.org/trac/chrome/site/img/fem-logo.png http://www.zoo-project.org/trac/chrome/site/img/supsi-logo.png http://www.zoo-project.org/trac/chrome/site/img/cumtb-logo.png

Become a knowledge partner

Related links

http://zoo-project.org/img/ogclogo.png http://zoo-project.org/img/osgeologo.png