Changeset 14793
- Timestamp:
- 06/30/08 17:05:15 (5 months ago)
- Files:
-
- trunk/gdal/apps/gdalbuildvrt.cpp (modified) (18 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/gdal/apps/gdalbuildvrt.cpp
r12120 r14793 28 28 ****************************************************************************/ 29 29 30 #include "gdal_pr iv.h"30 #include "gdal_proxy.h" 31 31 #include "cpl_string.h" 32 32 #include "ogrsf_frmts/shape/shapefil.h" 33 #include "vrt/ vrtdataset.h"33 #include "vrt/gdal_vrt.h" 34 34 35 35 CPL_CVSID("$Id: gdalbuildvrt.cpp rouault $"); … … 51 51 typedef struct 52 52 { 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; 56 60 57 61 typedef struct … … 91 95 } 92 96 97 93 98 void build_vrt(const char* pszOutputFilename, 94 99 int* pnInputFiles, char*** pppszInputFilenames, … … 96 101 { 97 102 char* projectionRef = NULL; 98 RasterSize* rasterSizes;99 double* adfGeoTransforms;100 103 int nBands = 0; 101 104 BandProperty* bandProperties = NULL; 102 int index = 0;103 105 double minX = 0, minY = 0, maxX = 0, maxY = 0; 104 106 int i,j; 105 int* isFileOk;106 107 double we_res = 0; 107 108 double ns_res = 0; 108 VRTDataset* ds;109 109 int rasterXSize; 110 110 int rasterYSize; 111 111 int nCount = 0; 112 int bFirst = TRUE; 113 VRTDatasetH hVRTDS = NULL; 114 112 115 char** ppszInputFilenames = *pppszInputFilenames; 113 116 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)); 118 120 119 121 for(i=0;i<nInputFiles;i++) … … 124 126 125 127 GDALDatasetH hDS = GDALOpen(ppszInputFilenames[i], GA_ReadOnly ); 126 isFileOk[i] = 0; 128 psDatasetProperties[i].isFileOK = FALSE; 129 127 130 if (hDS) 128 131 { … … 130 133 if( CSLCount(papszMetadata) > 0 ) 131 134 { 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 138 138 ppszInputFilenames = (char**)CPLRealloc(ppszInputFilenames, 139 139 sizeof(char*) * (nInputFiles+CSLCount(papszMetadata))); … … 156 156 157 157 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) 161 161 { 162 162 fprintf( stderr, "gdalbuildvrt does not support rotated geo transforms. Skipping %s\n", … … 165 165 continue; 166 166 } 167 if ( adfGeoTransforms[index * 6 +GEOTRSFRM_NS_RES] >= 0)167 if (psDatasetProperties[i].adfGeoTransform[GEOTRSFRM_NS_RES] >= 0) 168 168 { 169 169 fprintf( stderr, "gdalbuildvrt does not support positive NS resolution. Skipping %s\n", … … 172 172 continue; 173 173 } 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) 182 188 { 183 189 if (proj) … … 209 215 else 210 216 { 211 if ( proj != NULL && projectionRef == NULL||212 proj == NULL && projectionRef != NULL||217 if ((proj != NULL && projectionRef == NULL) || 218 (proj == NULL && projectionRef != NULL) || 213 219 (proj != NULL && projectionRef != NULL && EQUAL(proj, projectionRef) == FALSE)) 214 220 { … … 259 265 if (resolutionStrategy == AVERAGE_RESOLUTION) 260 266 { 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]; 263 269 } 264 270 else 265 271 { 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]; 270 276 } 271 277 else if (resolutionStrategy == HIGHEST_RESOLUTION) 272 278 { 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]); 275 281 } 276 282 else 277 283 { 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; 285 292 GDALClose(hDS); 286 293 } … … 294 301 *pnInputFiles = nInputFiles; 295 302 296 if ( index== 0)303 if (nCount == 0) 297 304 goto end; 298 305 299 306 if (resolutionStrategy == AVERAGE_RESOLUTION) 300 307 { 301 we_res /= index;302 ns_res /= index;308 we_res /= nCount; 309 ns_res /= nCount; 303 310 } 304 311 … … 306 313 rasterYSize = (int)(0.5 + (maxY - minY) / -ns_res); 307 314 308 ds = new VRTDataset(rasterXSize, rasterYSize);309 ds->SetDescription(pszOutputFilename);315 hVRTDS = VRTCreate(rasterXSize, rasterYSize); 316 GDALSetDescription(hVRTDS, pszOutputFilename); 310 317 311 318 if (projectionRef) 312 319 { 313 ds->SetProjection(projectionRef);320 GDALSetProjection(hVRTDS, projectionRef); 314 321 } 315 322 … … 321 328 adfGeoTransform[GEOTRSFRM_ROTATION_PARAM2] = 0; 322 329 adfGeoTransform[GEOTRSFRM_NS_RES] = ns_res; 323 ds->SetGeoTransform(adfGeoTransform);330 GDALSetGeoTransform(hVRTDS, adfGeoTransform); 324 331 325 332 for(j=0;j<nBands;j++) 326 333 { 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); 329 338 if (bandProperties[j].colorInterpretation == GCI_PaletteIndex) 330 339 { 331 ds->GetRasterBand(j+1)->SetColorTable((GDALColorTable*)bandProperties[j].colorTable);340 GDALSetRasterColorTable(hBand, bandProperties[j].colorTable); 332 341 } 333 342 if (bandProperties[j].bHasNoData) 334 ds->GetRasterBand(j+1)->SetNoDataValue(bandProperties[j].noDataValue);343 GDALSetRasterNoDataValue(hBand, bandProperties[j].noDataValue); 335 344 } 336 345 337 346 for(i=0;i<nInputFiles;i++) 338 347 { 339 if ( isFileOk[i]== 0)348 if (psDatasetProperties[i].isFileOK == 0) 340 349 continue; 341 350 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 343 367 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); 345 369 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); 347 371 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); 349 373 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); 351 375 352 376 for(j=0;j<nBands;j++) 353 377 { 354 VRTSourcedRasterBand *poBand = (VRTSourcedRasterBand*)ds->GetRasterBand( j + 1);378 VRTSourcedRasterBandH hVRTBand = (VRTSourcedRasterBandH)GDALGetRasterBand(hVRTDS, j + 1); 355 379 356 380 /* 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); 365 392 366 393 end: 367 CPLFree(rasterSizes); 368 CPLFree(adfGeoTransforms); 394 CPLFree(psDatasetProperties); 369 395 for(j=0;j<nBands;j++) 370 396 { … … 373 399 CPLFree(bandProperties); 374 400 CPLFree(projectionRef); 375 CPLFree(isFileOk);376 401 377 402 } … … 466 491 char ** ppszInputFilenames = NULL; 467 492 const char * pszOutputFilename = NULL; 468 int i ;493 int i, iArg; 469 494 470 495 GDALAllRegister(); … … 477 502 /* Parse commandline. */ 478 503 /* -------------------------------------------------------------------- */ 479 for( i nt iArg = 1; iArg < nArgc; iArg++ )504 for( iArg = 1; iArg < nArgc; iArg++ ) 480 505 { 481 506 if( strcmp(papszArgv[iArg],"-tileindex") == 0 ) … … 527 552 528 553 CSLDestroy( papszArgv ); 554 GDALDumpOpenDatasets( stderr ); 529 555 GDALDestroyDriverManager(); 530 556
