Changeset 14793

Show
Ignore:
Timestamp:
06/30/08 17:05:15 (5 months ago)
Author:
rouault
Message:

Use new proxy API instead

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/gdal/apps/gdalbuildvrt.cpp

    r12120 r14793  
    2828 ****************************************************************************/ 
    2929 
    30 #include "gdal_priv.h" 
     30#include "gdal_proxy.h" 
    3131#include "cpl_string.h" 
    3232#include "ogrsf_frmts/shape/shapefil.h" 
    33 #include "vrt/vrtdataset.h" 
     33#include "vrt/gdal_vrt.h" 
    3434 
    3535CPL_CVSID("$Id: gdalbuildvrt.cpp rouault $"); 
     
    5151typedef struct 
    5252{ 
    53     int                    rasterXSize; 
    54     int                    rasterYSize; 
    55 } RasterSize; 
     53    int    isFileOK; 
     54    int    nRasterXSize; 
     55    int    nRasterYSize; 
     56    double adfGeoTransform[6]; 
     57    int    nBlockXSize; 
     58    int    nBlockYSize; 
     59} DatasetProperty; 
    5660 
    5761typedef struct 
     
    9195} 
    9296 
     97 
    9398void build_vrt(const char* pszOutputFilename, 
    9499               int* pnInputFiles, char*** pppszInputFilenames, 
     
    96101{ 
    97102    char* projectionRef = NULL; 
    98     RasterSize* rasterSizes; 
    99     double* adfGeoTransforms; 
    100103    int nBands = 0; 
    101104    BandProperty* bandProperties = NULL; 
    102     int index = 0; 
    103105    double minX = 0, minY = 0, maxX = 0, maxY = 0; 
    104106    int i,j; 
    105     int* isFileOk; 
    106107    double we_res = 0; 
    107108    double ns_res = 0; 
    108     VRTDataset* ds; 
    109109    int rasterXSize; 
    110110    int rasterYSize; 
    111      
     111    int nCount = 0; 
     112    int bFirst = TRUE; 
     113    VRTDatasetH hVRTDS = NULL; 
     114 
    112115    char** ppszInputFilenames = *pppszInputFilenames; 
    113116    int nInputFiles = *pnInputFiles; 
    114      
    115     rasterSizes = (RasterSize*)CPLMalloc(nInputFiles*sizeof(RasterSize)); 
    116     adfGeoTransforms = (double*)CPLMalloc(nInputFiles*6*sizeof(double)); 
    117     isFileOk = (int*)CPLMalloc(nInputFiles*sizeof(int)); 
     117 
     118    DatasetProperty* psDatasetProperties = 
     119            (DatasetProperty*) CPLMalloc(nInputFiles*sizeof(DatasetProperty)); 
    118120 
    119121    for(i=0;i<nInputFiles;i++) 
     
    124126 
    125127        GDALDatasetH hDS = GDALOpen(ppszInputFilenames[i], GA_ReadOnly ); 
    126         isFileOk[i] = 0; 
     128        psDatasetProperties[i].isFileOK = FALSE; 
     129 
    127130        if (hDS) 
    128131        { 
     
    130133            if( CSLCount(papszMetadata) > 0 ) 
    131134            { 
    132                 rasterSizes = (RasterSize*)CPLRealloc(rasterSizes, 
    133                                (nInputFiles+CSLCount(papszMetadata))*sizeof(RasterSize)); 
    134                 adfGeoTransforms = (double*)CPLRealloc(adfGeoTransforms, 
    135                                     (nInputFiles+CSLCount(papszMetadata))*6*sizeof(double)); 
    136                 isFileOk = (int*)CPLRealloc(isFileOk, 
    137                             (nInputFiles+CSLCount(papszMetadata))*sizeof(int)); 
     135                psDatasetProperties = 
     136                    (DatasetProperty*) CPLMalloc((nInputFiles+CSLCount(papszMetadata))*sizeof(DatasetProperty)); 
     137 
    138138                ppszInputFilenames = (char**)CPLRealloc(ppszInputFilenames, 
    139139                                        sizeof(char*) * (nInputFiles+CSLCount(papszMetadata))); 
     
    156156 
    157157            const char* proj = GDALGetProjectionRef(hDS); 
    158             GDALGetGeoTransform(hDS, adfGeoTransforms + index * 6); 
    159             if (adfGeoTransforms[index * 6 + GEOTRSFRM_ROTATION_PARAM1] != 0 || 
    160                 adfGeoTransforms[index * 6 + GEOTRSFRM_ROTATION_PARAM2] != 0) 
     158            GDALGetGeoTransform(hDS, psDatasetProperties[i].adfGeoTransform); 
     159            if (psDatasetProperties[i].adfGeoTransform[GEOTRSFRM_ROTATION_PARAM1] != 0 || 
     160                psDatasetProperties[i].adfGeoTransform[GEOTRSFRM_ROTATION_PARAM2] != 0) 
    161161            { 
    162162                fprintf( stderr, "gdalbuildvrt does not support rotated geo transforms. Skipping %s\n", 
     
    165165                continue; 
    166166            } 
    167             if (adfGeoTransforms[index * 6 + GEOTRSFRM_NS_RES] >= 0) 
     167            if (psDatasetProperties[i].adfGeoTransform[GEOTRSFRM_NS_RES] >= 0) 
    168168            { 
    169169                fprintf( stderr, "gdalbuildvrt does not support positive NS resolution. Skipping %s\n", 
     
    172172                continue; 
    173173            } 
    174             rasterSizes[index].rasterXSize = GDALGetRasterXSize(hDS); 
    175             rasterSizes[index].rasterYSize = GDALGetRasterYSize(hDS); 
    176             double product_minX = adfGeoTransforms[index * 6 + GEOTRSFRM_TOPLEFT_X]; 
    177             double product_maxY = adfGeoTransforms[index * 6 + GEOTRSFRM_TOPLEFT_Y]; 
    178             double product_maxX = product_minX + rasterSizes[index].rasterXSize * adfGeoTransforms[index * 6 + GEOTRSFRM_WE_RES]; 
    179             double product_minY = product_maxY + rasterSizes[index].rasterYSize * adfGeoTransforms[index * 6 + GEOTRSFRM_NS_RES]; 
    180              
    181             if (index == 0) 
     174            psDatasetProperties[i].nRasterXSize = GDALGetRasterXSize(hDS); 
     175            psDatasetProperties[i].nRasterYSize = GDALGetRasterYSize(hDS); 
     176            double product_minX = psDatasetProperties[i].adfGeoTransform[GEOTRSFRM_TOPLEFT_X]; 
     177            double product_maxY = psDatasetProperties[i].adfGeoTransform[GEOTRSFRM_TOPLEFT_Y]; 
     178            double product_maxX = product_minX + 
     179                        GDALGetRasterXSize(hDS) * psDatasetProperties[i].adfGeoTransform[GEOTRSFRM_WE_RES]; 
     180            double product_minY = product_maxY + 
     181                        GDALGetRasterYSize(hDS) * psDatasetProperties[i].adfGeoTransform[GEOTRSFRM_NS_RES]; 
     182 
     183            GDALGetBlockSize(GDALGetRasterBand( hDS, 1 ), 
     184                             &psDatasetProperties[i].nBlockXSize, 
     185                             &psDatasetProperties[i].nBlockYSize); 
     186 
     187            if (bFirst) 
    182188            { 
    183189                if (proj) 
     
    209215            else 
    210216            { 
    211                 if (proj != NULL && projectionRef == NULL || 
    212                     proj == NULL && projectionRef != NULL || 
     217                if ((proj != NULL && projectionRef == NULL) || 
     218                    (proj == NULL && projectionRef != NULL) || 
    213219                    (proj != NULL && projectionRef != NULL && EQUAL(proj, projectionRef) == FALSE)) 
    214220                { 
     
    259265            if (resolutionStrategy == AVERAGE_RESOLUTION) 
    260266            { 
    261                 we_res += adfGeoTransforms[index * 6 + GEOTRSFRM_WE_RES]; 
    262                 ns_res += adfGeoTransforms[index * 6 + GEOTRSFRM_NS_RES]; 
     267                we_res += psDatasetProperties[i].adfGeoTransform[GEOTRSFRM_WE_RES]; 
     268                ns_res += psDatasetProperties[i].adfGeoTransform[GEOTRSFRM_NS_RES]; 
    263269            } 
    264270            else 
    265271            { 
    266                 if (index == 0
    267                 { 
    268                     we_res = adfGeoTransforms[index * 6 + GEOTRSFRM_WE_RES]; 
    269                     ns_res = adfGeoTransforms[index * 6 + GEOTRSFRM_NS_RES]; 
     272                if (bFirst
     273                { 
     274                    we_res = psDatasetProperties[i].adfGeoTransform[GEOTRSFRM_WE_RES]; 
     275                    ns_res = psDatasetProperties[i].adfGeoTransform[GEOTRSFRM_NS_RES]; 
    270276                } 
    271277                else if (resolutionStrategy == HIGHEST_RESOLUTION) 
    272278                { 
    273                     we_res = MIN(we_res, adfGeoTransforms[index * 6 + GEOTRSFRM_WE_RES]); 
    274                     ns_res = MIN(ns_res, adfGeoTransforms[index * 6 + GEOTRSFRM_NS_RES]); 
     279                    we_res = MIN(we_res, psDatasetProperties[i].adfGeoTransform[GEOTRSFRM_WE_RES]); 
     280                    ns_res = MIN(ns_res, psDatasetProperties[i].adfGeoTransform[GEOTRSFRM_NS_RES]); 
    275281                } 
    276282                else 
    277283                { 
    278                     we_res = MAX(we_res, adfGeoTransforms[index * 6 + GEOTRSFRM_WE_RES]); 
    279                     ns_res = MAX(ns_res, adfGeoTransforms[index * 6 + GEOTRSFRM_NS_RES]); 
    280                 } 
    281             } 
    282  
    283             isFileOk[i] = 1; 
    284             index++; 
     284                    we_res = MAX(we_res, psDatasetProperties[i].adfGeoTransform[GEOTRSFRM_WE_RES]); 
     285                    ns_res = MAX(ns_res, psDatasetProperties[i].adfGeoTransform[GEOTRSFRM_NS_RES]); 
     286                } 
     287            } 
     288 
     289            psDatasetProperties[i].isFileOK = 1; 
     290            nCount ++; 
     291            bFirst = FALSE; 
    285292            GDALClose(hDS); 
    286293        } 
     
    294301    *pnInputFiles = nInputFiles; 
    295302     
    296     if (index == 0) 
     303    if (nCount == 0) 
    297304        goto end; 
    298305     
    299306    if (resolutionStrategy == AVERAGE_RESOLUTION) 
    300307    { 
    301         we_res /= index
    302         ns_res /= index
     308        we_res /= nCount
     309        ns_res /= nCount
    303310    } 
    304311     
     
    306313    rasterYSize = (int)(0.5 + (maxY - minY) / -ns_res); 
    307314     
    308     ds = new VRTDataset(rasterXSize, rasterYSize); 
    309     ds->SetDescription(pszOutputFilename); 
     315    hVRTDS = VRTCreate(rasterXSize, rasterYSize); 
     316    GDALSetDescription(hVRTDS, pszOutputFilename); 
    310317     
    311318    if (projectionRef) 
    312319    { 
    313         ds->SetProjection(projectionRef); 
     320        GDALSetProjection(hVRTDS, projectionRef); 
    314321    } 
    315322 
     
    321328    adfGeoTransform[GEOTRSFRM_ROTATION_PARAM2] = 0; 
    322329    adfGeoTransform[GEOTRSFRM_NS_RES] = ns_res; 
    323     ds->SetGeoTransform(adfGeoTransform); 
     330    GDALSetGeoTransform(hVRTDS, adfGeoTransform); 
    324331     
    325332    for(j=0;j<nBands;j++) 
    326333    { 
    327         ds->AddBand(bandProperties[j].dataType, NULL); 
    328         ds->GetRasterBand(j+1)->SetColorInterpretation(bandProperties[j].colorInterpretation); 
     334        GDALRasterBandH hBand; 
     335        GDALAddBand(hVRTDS, bandProperties[j].dataType, NULL); 
     336        hBand = GDALGetRasterBand(hVRTDS, j+1); 
     337        GDALSetRasterColorInterpretation(hBand, bandProperties[j].colorInterpretation); 
    329338        if (bandProperties[j].colorInterpretation == GCI_PaletteIndex) 
    330339        { 
    331             ds->GetRasterBand(j+1)->SetColorTable((GDALColorTable*)bandProperties[j].colorTable); 
     340            GDALSetRasterColorTable(hBand, bandProperties[j].colorTable); 
    332341        } 
    333342        if (bandProperties[j].bHasNoData) 
    334             ds->GetRasterBand(j+1)->SetNoDataValue(bandProperties[j].noDataValue); 
     343            GDALSetRasterNoDataValue(hBand, bandProperties[j].noDataValue); 
    335344    } 
    336345 
    337346    for(i=0;i<nInputFiles;i++) 
    338347    { 
    339         if (isFileOk[i] == 0) 
     348        if (psDatasetProperties[i].isFileOK == 0) 
    340349            continue; 
    341350        const char* dsFileName = ppszInputFilenames[i]; 
    342         GDALDatasetH hDS = GDALOpen(dsFileName, GA_ReadOnly );  
     351 
     352        GDALProxyPoolDatasetH hProxyDS = 
     353               GDALProxyPoolDatasetCreate(dsFileName, 
     354                                         psDatasetProperties[i].nRasterXSize, 
     355                                         psDatasetProperties[i].nRasterYSize, 
     356                                         GA_ReadOnly, TRUE, projectionRef, 
     357                                         psDatasetProperties[i].adfGeoTransform); 
     358 
     359        for(j=0;j<nBands;j++) 
     360        { 
     361            GDALProxyPoolDatasetAddSrcBandDescription(hProxyDS, 
     362                                            bandProperties[j].dataType, 
     363                                            psDatasetProperties[i].nBlockXSize, 
     364                                            psDatasetProperties[i].nBlockYSize); 
     365        } 
     366 
    343367        int xoffset = (int) 
    344                 (0.5 + (adfGeoTransforms[i * 6 + GEOTRSFRM_TOPLEFT_X] - minX) / we_res); 
     368                (0.5 + (psDatasetProperties[i].adfGeoTransform[GEOTRSFRM_TOPLEFT_X] - minX) / we_res); 
    345369        int yoffset = (int) 
    346                 (0.5 + (maxY - adfGeoTransforms[i * 6 + GEOTRSFRM_TOPLEFT_Y]) / -ns_res); 
     370                (0.5 + (maxY - psDatasetProperties[i].adfGeoTransform[GEOTRSFRM_TOPLEFT_Y]) / -ns_res); 
    347371        int dest_width = (int) 
    348                 (0.5 + rasterSizes[i].rasterXSize * adfGeoTransforms[i * 6 + GEOTRSFRM_WE_RES] / we_res); 
     372                (0.5 + psDatasetProperties[i].nRasterXSize * psDatasetProperties[i].adfGeoTransform[GEOTRSFRM_WE_RES] / we_res); 
    349373        int dest_height = (int) 
    350                 (0.5 + rasterSizes[i].rasterYSize * adfGeoTransforms[i * 6 + GEOTRSFRM_NS_RES] / ns_res); 
     374                (0.5 + psDatasetProperties[i].nRasterYSize * psDatasetProperties[i].adfGeoTransform[GEOTRSFRM_NS_RES] / ns_res); 
    351375 
    352376        for(j=0;j<nBands;j++) 
    353377        { 
    354             VRTSourcedRasterBand *poBand = (VRTSourcedRasterBand*)ds->GetRasterBand( j + 1 ); 
     378            VRTSourcedRasterBandH hVRTBand = (VRTSourcedRasterBandH)GDALGetRasterBand(hVRTDS, j + 1); 
    355379 
    356380            /* Place the raster band at the right position in the VRT */ 
    357             poBand->AddSimpleSource((GDALRasterBand*)GDALGetRasterBand(hDS, j + 1), 
    358                                     0, 0, rasterSizes[i].rasterXSize, rasterSizes[i].rasterYSize, 
    359                                     xoffset, yoffset, 
    360                                     dest_width, dest_height); 
    361         } 
    362         GDALClose(hDS); 
    363     } 
    364     delete ds; 
     381            VRTAddSimpleSource(hVRTBand, GDALGetRasterBand((GDALDatasetH)hProxyDS, j + 1), 
     382                               0, 0, 
     383                               psDatasetProperties[i].nRasterXSize, 
     384                               psDatasetProperties[i].nRasterYSize, 
     385                               xoffset, yoffset, 
     386                               dest_width, dest_height, "near", 
     387                               VRT_NODATA_UNSET); 
     388        } 
     389        GDALDereferenceDataset(hProxyDS); 
     390    } 
     391    GDALClose(hVRTDS); 
    365392 
    366393end: 
    367     CPLFree(rasterSizes); 
    368     CPLFree(adfGeoTransforms); 
     394    CPLFree(psDatasetProperties); 
    369395    for(j=0;j<nBands;j++) 
    370396    { 
     
    373399    CPLFree(bandProperties); 
    374400    CPLFree(projectionRef); 
    375     CPLFree(isFileOk); 
    376401 
    377402} 
     
    466491    char ** ppszInputFilenames = NULL; 
    467492    const char * pszOutputFilename = NULL; 
    468     int i
     493    int i, iArg
    469494 
    470495    GDALAllRegister(); 
     
    477502/*      Parse commandline.                                              */ 
    478503/* -------------------------------------------------------------------- */ 
    479     for( int iArg = 1; iArg < nArgc; iArg++ ) 
     504    for( iArg = 1; iArg < nArgc; iArg++ ) 
    480505    { 
    481506        if( strcmp(papszArgv[iArg],"-tileindex") == 0 ) 
     
    527552 
    528553    CSLDestroy( papszArgv ); 
     554    GDALDumpOpenDatasets( stderr ); 
    529555    GDALDestroyDriverManager(); 
    530556