00001 /****************************************************************************** 00002 * $Id: gdaldataset_cpp-source.html,v 1.10 2002/04/16 13:11:48 warmerda Exp $ 00003 * 00004 * Project: GDAL Core 00005 * Purpose: Base class for raster file formats. 00006 * Author: Frank Warmerdam, warmerda@home.com 00007 * 00008 ****************************************************************************** 00009 * Copyright (c) 1998, 2000, Frank Warmerdam 00010 * 00011 * Permission is hereby granted, free of charge, to any person obtaining a 00012 * copy of this software and associated documentation files (the "Software"), 00013 * to deal in the Software without restriction, including without limitation 00014 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 00015 * and/or sell copies of the Software, and to permit persons to whom the 00016 * Software is furnished to do so, subject to the following conditions: 00017 * 00018 * The above copyright notice and this permission notice shall be included 00019 * in all copies or substantial portions of the Software. 00020 * 00021 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 00022 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00023 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 00024 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00025 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 00026 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 00027 * DEALINGS IN THE SOFTWARE. 00028 ****************************************************************************** 00029 * 00030 * $Log: gdaldataset_cpp-source.html,v $ 00030 * Revision 1.10 2002/04/16 13:11:48 warmerda 00030 * updated 00030 * 00031 * Revision 1.26 2001/11/16 21:36:01 warmerda 00032 * added the AddBand() method on GDALDataset 00033 * 00034 * Revision 1.25 2001/10/18 14:35:22 warmerda 00035 * avoid conflicts between parameters and member data 00036 * 00037 * Revision 1.24 2001/10/17 21:47:02 warmerda 00038 * added SetGCPs() on GDALDataset 00039 * 00040 * Revision 1.23 2001/07/18 04:04:30 warmerda 00041 * added CPL_CVSID 00042 * 00043 * Revision 1.22 2001/01/10 22:24:37 warmerda 00044 * Patched GDALDataset::FlushCache() to recover gracefully if papoBands 00045 * doesn't exist yet matching nBands. 00046 * 00047 * Revision 1.21 2000/10/06 15:27:13 warmerda 00048 * default bands to same access as dataset in SetBand() 00049 * 00050 * Revision 1.20 2000/08/09 16:26:00 warmerda 00051 * debug message on dataset cleanup 00052 * 00053 * Revision 1.19 2000/07/11 14:35:43 warmerda 00054 * added documentation 00055 * 00056 * Revision 1.18 2000/06/27 16:46:56 warmerda 00057 * default to using dummy progress func 00058 * 00059 * Revision 1.17 2000/06/26 21:44:50 warmerda 00060 * make progress func save for overviews 00061 * 00062 * Revision 1.16 2000/06/26 18:47:31 warmerda 00063 * added GDALBuildOverviews 00064 * 00065 * Revision 1.15 2000/04/21 21:56:23 warmerda 00066 * move metadata to GDALMajorObject, added BuildOverviews 00067 * 00068 * Revision 1.14 2000/03/31 13:42:06 warmerda 00069 * added gcp support methods 00070 * 00071 * Revision 1.13 2000/03/23 16:53:55 warmerda 00072 * default geotransform is 0,1,0,0,0,1 00073 * 00074 * Revision 1.12 2000/03/06 21:50:10 warmerda 00075 * fixed bug with setting nBands 00076 * 00077 * Revision 1.11 2000/03/06 02:20:56 warmerda 00078 * added reference counting 00079 * 00080 * Revision 1.10 2000/02/28 16:34:49 warmerda 00081 * set the nRasterX/YSize in bands 00082 * 00083 * Revision 1.9 1999/11/11 21:59:07 warmerda 00084 * added GetDriver() for datasets 00085 * 00086 * Revision 1.8 1999/10/01 14:44:02 warmerda 00087 * added documentation 00088 * 00089 * Revision 1.7 1999/05/17 01:43:10 warmerda 00090 * fixed GDALSetGeoTransform() 00091 * 00092 * Revision 1.6 1999/05/16 20:04:58 warmerda 00093 * Don't emit an error message when SetProjection() is called for datasets 00094 * that don't implement the call. 00095 * 00096 * Revision 1.5 1999/04/21 04:16:51 warmerda 00097 * experimental docs 00098 * 00099 * Revision 1.4 1999/01/11 15:37:55 warmerda 00100 * fixed log keyword 00101 */ 00102 00103 #include "gdal_priv.h" 00104 #include "cpl_string.h" 00105 00106 CPL_CVSID("$Id: gdaldataset_cpp-source.html,v 1.10 2002/04/16 13:11:48 warmerda Exp $"); 00107 00108 /************************************************************************/ 00109 /* GDALDataset() */ 00110 /************************************************************************/ 00111 00112 GDALDataset::GDALDataset() 00113 00114 { 00115 poDriver = NULL; 00116 eAccess = GA_ReadOnly; 00117 nRasterXSize = 512; 00118 nRasterYSize = 512; 00119 nBands = 0; 00120 papoBands = NULL; 00121 nRefCount = 1; 00122 } 00123 00124 /************************************************************************/ 00125 /* ~GDALDataset() */ 00126 /************************************************************************/ 00127 00137 GDALDataset::~GDALDataset() 00138 00139 { 00140 int i; 00141 00142 CPLDebug( "GDAL", "GDALClose(%s)\n", GetDescription() ); 00143 00144 /* -------------------------------------------------------------------- */ 00145 /* Destroy the raster bands if they exist. */ 00146 /* -------------------------------------------------------------------- */ 00147 for( i = 0; i < nBands && papoBands != NULL; i++ ) 00148 { 00149 if( papoBands[i] != NULL ) 00150 delete papoBands[i]; 00151 } 00152 00153 CPLFree( papoBands ); 00154 } 00155 00156 /************************************************************************/ 00157 /* GDALClose() */ 00158 /************************************************************************/ 00159 00160 void GDALClose( GDALDatasetH hDS ) 00161 00162 { 00163 delete ((GDALDataset *) hDS); 00164 } 00165 00166 /************************************************************************/ 00167 /* FlushCache() */ 00168 /************************************************************************/ 00169 00177 void GDALDataset::FlushCache() 00178 00179 { 00180 int i; 00181 00182 // This sometimes happens if a dataset is destroyed before completely 00183 // built. 00184 00185 if( papoBands == NULL ) 00186 return; 00187 00188 for( i = 0; i < nBands; i++ ) 00189 { 00190 if( papoBands[i] != NULL ) 00191 papoBands[i]->FlushCache(); 00192 } 00193 } 00194 00195 /************************************************************************/ 00196 /* RasterInitialize() */ 00197 /* */ 00198 /* Initialize raster size */ 00199 /************************************************************************/ 00200 00201 void GDALDataset::RasterInitialize( int nXSize, int nYSize ) 00202 00203 { 00204 CPLAssert( nXSize > 0 && nYSize > 0 ); 00205 00206 nRasterXSize = nXSize; 00207 nRasterYSize = nYSize; 00208 } 00209 00210 /************************************************************************/ 00211 /* AddBand() */ 00212 /************************************************************************/ 00213 00233 CPLErr GDALDataset::AddBand( GDALDataType eType, char **papszOptions ) 00234 00235 { 00236 CPLError( CE_Failure, CPLE_NotSupported, 00237 "Dataset does not support the AddBand() method." ); 00238 00239 return CE_Failure; 00240 } 00241 00242 /************************************************************************/ 00243 /* GDALAddBand() */ 00244 /************************************************************************/ 00245 00246 CPLErr GDALAddBand( GDALDatasetH hDataset, 00247 GDALDataType eType, char **papszOptions ) 00248 00249 { 00250 return ((GDALDataset *) hDataset)->AddBand( eType, papszOptions ); 00251 } 00252 00253 /************************************************************************/ 00254 /* SetBand() */ 00255 /* */ 00256 /* Set a band in the band array, updating the band count, and */ 00257 /* array size appropriately. */ 00258 /************************************************************************/ 00259 00260 void GDALDataset::SetBand( int nNewBand, GDALRasterBand * poBand ) 00261 00262 { 00263 /* -------------------------------------------------------------------- */ 00264 /* Do we need to grow the bands list? */ 00265 /* -------------------------------------------------------------------- */ 00266 if( nBands < nNewBand || papoBands == NULL ) { 00267 int i; 00268 00269 if( papoBands == NULL ) 00270 papoBands = (GDALRasterBand **) 00271 VSICalloc(sizeof(GDALRasterBand*), MAX(nNewBand,nBands)); 00272 else 00273 papoBands = (GDALRasterBand **) 00274 VSIRealloc(papoBands, sizeof(GDALRasterBand*) * 00275 MAX(nNewBand,nBands)); 00276 00277 for( i = nBands; i < nNewBand; i++ ) 00278 papoBands[i] = NULL; 00279 00280 nBands = MAX(nBands,nNewBand); 00281 } 00282 00283 /* -------------------------------------------------------------------- */ 00284 /* Set the band. Resetting the band is currently not permitted. */ 00285 /* -------------------------------------------------------------------- */ 00286 CPLAssert( papoBands[nNewBand-1] == NULL ); 00287 00288 papoBands[nNewBand-1] = poBand; 00289 00290 /* -------------------------------------------------------------------- */ 00291 /* Set back reference information on the raster band. Note */ 00292 /* that the GDALDataset is a friend of the GDALRasterBand */ 00293 /* specifically to allow this. */ 00294 /* -------------------------------------------------------------------- */ 00295 poBand->nBand = nNewBand; 00296 poBand->poDS = this; 00297 poBand->nRasterXSize = nRasterXSize; 00298 poBand->nRasterYSize = nRasterYSize; 00299 poBand->eAccess = eAccess; /* default access to be same as dataset */ 00300 } 00301 00302 /************************************************************************/ 00303 /* GetRasterXSize() */ 00304 /************************************************************************/ 00305 00316 int GDALDataset::GetRasterXSize() 00317 00318 { 00319 return nRasterXSize; 00320 } 00321 00322 /************************************************************************/ 00323 /* GDALGetRasterXSize() */ 00324 /************************************************************************/ 00325 00326 int GDALGetRasterXSize( GDALDatasetH hDataset ) 00327 00328 { 00329 return ((GDALDataset *) hDataset)->GetRasterXSize(); 00330 } 00331 00332 00333 /************************************************************************/ 00334 /* GetRasterYSize() */ 00335 /************************************************************************/ 00336 00347 int GDALDataset::GetRasterYSize() 00348 00349 { 00350 return nRasterYSize; 00351 } 00352 00353 /************************************************************************/ 00354 /* GDALGetRasterYSize() */ 00355 /************************************************************************/ 00356 00357 int GDALGetRasterYSize( GDALDatasetH hDataset ) 00358 00359 { 00360 return ((GDALDataset *) hDataset)->GetRasterYSize(); 00361 } 00362 00363 /************************************************************************/ 00364 /* GetRasterBand() */ 00365 /************************************************************************/ 00366 00380 GDALRasterBand * GDALDataset::GetRasterBand( int nBandId ) 00381 00382 { 00383 if( nBandId < 1 || nBandId > nBands ) 00384 { 00385 CPLError( CE_Fatal, CPLE_IllegalArg, 00386 "GDALDataset::GetRasterBand(%d) - Illegal band #\n", 00387 nBandId ); 00388 } 00389 00390 return( papoBands[nBandId-1] ); 00391 } 00392 00393 /************************************************************************/ 00394 /* GDALGetRasterBand() */ 00395 /************************************************************************/ 00396 00397 GDALRasterBandH GDALGetRasterBand( GDALDatasetH hDS, int nBandId ) 00398 00399 { 00400 return( (GDALRasterBandH) ((GDALDataset *) hDS)->GetRasterBand(nBandId) ); 00401 } 00402 00403 /************************************************************************/ 00404 /* GetRasterCount() */ 00405 /************************************************************************/ 00406 00415 int GDALDataset::GetRasterCount() 00416 00417 { 00418 return( nBands ); 00419 } 00420 00421 /************************************************************************/ 00422 /* GDALGetRasterCount() */ 00423 /************************************************************************/ 00424 00425 int GDALGetRasterCount( GDALDatasetH hDS ) 00426 00427 { 00428 return( ((GDALDataset *) hDS)->GetRasterCount() ); 00429 } 00430 00431 /************************************************************************/ 00432 /* GetProjectionRef() */ 00433 /************************************************************************/ 00434 00451 const char *GDALDataset::GetProjectionRef() 00452 00453 { 00454 return( "" ); 00455 } 00456 00457 /************************************************************************/ 00458 /* GDALGetProjectionRef() */ 00459 /************************************************************************/ 00460 00461 const char *GDALGetProjectionRef( GDALDatasetH hDS ) 00462 00463 { 00464 return( ((GDALDataset *) hDS)->GetProjectionRef() ); 00465 } 00466 00467 /************************************************************************/ 00468 /* SetProjection() */ 00469 /************************************************************************/ 00470 00486 CPLErr GDALDataset::SetProjection( const char * ) 00487 00488 { 00489 return CE_Failure; 00490 } 00491 00492 /************************************************************************/ 00493 /* GDALSetProjection() */ 00494 /************************************************************************/ 00495 00496 CPLErr GDALSetProjection( GDALDatasetH hDS, const char * pszProjection ) 00497 00498 { 00499 return( ((GDALDataset *) hDS)->SetProjection(pszProjection) ); 00500 } 00501 00502 /************************************************************************/ 00503 /* GetGeoTransform() */ 00504 /************************************************************************/ 00505 00536 CPLErr GDALDataset::GetGeoTransform( double * padfTransform ) 00537 00538 { 00539 CPLAssert( padfTransform != NULL ); 00540 00541 padfTransform[0] = 0.0; /* X Origin (top left corner) */ 00542 padfTransform[1] = 1.0; /* X Pixel size */ 00543 padfTransform[2] = 0.0; 00544 00545 padfTransform[3] = 0.0; /* Y Origin (top left corner) */ 00546 padfTransform[4] = 0.0; 00547 padfTransform[5] = 1.0; /* Y Pixel Size */ 00548 00549 return( CE_Failure ); 00550 } 00551 00552 /************************************************************************/ 00553 /* GDALGetGeoTransform() */ 00554 /************************************************************************/ 00555 00556 CPLErr GDALGetGeoTransform( GDALDatasetH hDS, double * padfTransform ) 00557 00558 { 00559 return( ((GDALDataset *) hDS)->GetGeoTransform(padfTransform) ); 00560 } 00561 00562 /************************************************************************/ 00563 /* SetGeoTransform() */ 00564 /************************************************************************/ 00565 00581 CPLErr GDALDataset::SetGeoTransform( double * ) 00582 00583 { 00584 CPLError( CE_Failure, CPLE_NotSupported, 00585 "SetGeoTransform() not supported for this dataset." ); 00586 00587 return( CE_Failure ); 00588 } 00589 00590 /************************************************************************/ 00591 /* GDALSetGeoTransform() */ 00592 /************************************************************************/ 00593 00594 CPLErr GDALSetGeoTransform( GDALDatasetH hDS, double * padfTransform ) 00595 00596 { 00597 return( ((GDALDataset *) hDS)->SetGeoTransform(padfTransform) ); 00598 } 00599 00600 /************************************************************************/ 00601 /* GetInternalHandle() */ 00602 /************************************************************************/ 00603 00615 void *GDALDataset::GetInternalHandle( const char * ) 00616 00617 { 00618 return( NULL ); 00619 } 00620 00621 /************************************************************************/ 00622 /* GDALGetInternalHandle() */ 00623 /************************************************************************/ 00624 00625 void *GDALGetInternalHandle( GDALDatasetH hDS, const char * pszRequest ) 00626 00627 { 00628 return( ((GDALDataset *) hDS)->GetInternalHandle(pszRequest) ); 00629 } 00630 00631 /************************************************************************/ 00632 /* GetDriver() */ 00633 /************************************************************************/ 00634 00644 GDALDriver * GDALDataset::GetDriver() 00645 00646 { 00647 return poDriver; 00648 } 00649 00650 /************************************************************************/ 00651 /* GDALGetDatasetDriver() */ 00652 /************************************************************************/ 00653 00654 GDALDriverH GDALGetDatasetDriver( GDALDatasetH hDataset ) 00655 00656 { 00657 return (GDALDriverH) ((GDALDataset *) hDataset)->GetDriver(); 00658 } 00659 00660 /************************************************************************/ 00661 /* Reference() */ 00662 /************************************************************************/ 00663 00674 int GDALDataset::Reference() 00675 00676 { 00677 return ++nRefCount; 00678 } 00679 00680 /************************************************************************/ 00681 /* GDALReferenceDataset() */ 00682 /************************************************************************/ 00683 00684 int GDALReferenceDataset( GDALDatasetH hDataset ) 00685 00686 { 00687 return ((GDALDataset *) hDataset)->Reference(); 00688 } 00689 00690 /************************************************************************/ 00691 /* Reference() */ 00692 /************************************************************************/ 00693 00705 int GDALDataset::Dereference() 00706 00707 { 00708 return --nRefCount; 00709 } 00710 00711 /************************************************************************/ 00712 /* GDALDereferenceDataset() */ 00713 /************************************************************************/ 00714 00715 int GDALDereferenceDataset( GDALDatasetH hDataset ) 00716 00717 { 00718 return ((GDALDataset *) hDataset)->Dereference(); 00719 } 00720 00721 /************************************************************************/ 00722 /* GetGCPCount() */ 00723 /************************************************************************/ 00724 00733 int GDALDataset::GetGCPCount() 00734 00735 { 00736 return 0; 00737 } 00738 00739 /************************************************************************/ 00740 /* GDALGetGCPCount() */ 00741 /************************************************************************/ 00742 00743 int GDALGetGCPCount( GDALDatasetH hDS ) 00744 00745 { 00746 return ((GDALDataset *) hDS)->GetGCPCount(); 00747 } 00748 00749 /************************************************************************/ 00750 /* GetGCPProjection() */ 00751 /************************************************************************/ 00752 00763 const char *GDALDataset::GetGCPProjection() 00764 00765 { 00766 return ""; 00767 } 00768 00769 /************************************************************************/ 00770 /* GDALGetGCPProjection() */ 00771 /************************************************************************/ 00772 00773 const char *GDALGetGCPProjection( GDALDatasetH hDS ) 00774 00775 { 00776 return ((GDALDataset *) hDS)->GetGCPProjection(); 00777 } 00778 00779 /************************************************************************/ 00780 /* GetGCPs() */ 00781 /************************************************************************/ 00782 00792 const GDAL_GCP *GDALDataset::GetGCPs() 00793 00794 { 00795 return NULL; 00796 } 00797 00798 /************************************************************************/ 00799 /* GDALGetGCPs() */ 00800 /************************************************************************/ 00801 00802 const GDAL_GCP *GDALGetGCPs( GDALDatasetH hDS ) 00803 00804 { 00805 return ((GDALDataset *) hDS)->GetGCPs(); 00806 } 00807 00808 00809 /************************************************************************/ 00810 /* SetGCPs() */ 00811 /************************************************************************/ 00812 00838 CPLErr GDALDataset::SetGCPs( int nGCPCount, const GDAL_GCP *pasGCPList, 00839 const char *pszGCPProjection ) 00840 00841 { 00842 return CE_Failure; 00843 } 00844 00845 /************************************************************************/ 00846 /* GDALSetGCPs() */ 00847 /************************************************************************/ 00848 00849 CPLErr GDALSetGCPs( GDALDatasetH hDS, int nGCPCount, 00850 const GDAL_GCP *pasGCPList, 00851 const char *pszGCPProjection ) 00852 00853 { 00854 return ((GDALDataset *) hDS)->SetGCPs( nGCPCount, pasGCPList, 00855 pszGCPProjection ); 00856 } 00857 00858 /************************************************************************/ 00859 /* BuildOverviews() */ 00860 /************************************************************************/ 00861 00890 CPLErr GDALDataset::BuildOverviews( const char *pszResampling, 00891 int nOverviews, int *panOverviewList, 00892 int nListBands, int *panBandList, 00893 GDALProgressFunc pfnProgress, 00894 void * pProgressData ) 00895 00896 { 00897 CPLErr eErr; 00898 int *panAllBandList = NULL; 00899 00900 if( nListBands == 0 ) 00901 { 00902 nListBands = GetRasterCount(); 00903 panAllBandList = (int *) CPLMalloc(sizeof(int) * nListBands); 00904 for( int i = 0; i < nListBands; i++ ) 00905 panAllBandList[i] = i+1; 00906 00907 panBandList = panAllBandList; 00908 } 00909 00910 if( pfnProgress == NULL ) 00911 pfnProgress = GDALDummyProgress; 00912 00913 eErr = IBuildOverviews( pszResampling, nOverviews, panOverviewList, 00914 nListBands, panBandList, pfnProgress, pProgressData ); 00915 00916 if( panAllBandList != NULL ) 00917 CPLFree( panAllBandList ); 00918 00919 return eErr; 00920 } 00921 00922 /************************************************************************/ 00923 /* GDALBuildOverviews() */ 00924 /************************************************************************/ 00925 00926 CPLErr GDALBuildOverviews( GDALDatasetH hDataset, 00927 const char *pszResampling, 00928 int nOverviews, int *panOverviewList, 00929 int nListBands, int *panBandList, 00930 GDALProgressFunc pfnProgress, 00931 void * pProgressData ) 00932 00933 { 00934 return ((GDALDataset *) hDataset)->BuildOverviews( 00935 pszResampling, nOverviews, panOverviewList, nListBands, panBandList, 00936 pfnProgress, pProgressData ); 00937 } 00938 00939 /************************************************************************/ 00940 /* IBuildOverviews() */ 00941 /* */ 00942 /* Default implementation. */ 00943 /************************************************************************/ 00944 00945 CPLErr GDALDataset::IBuildOverviews( const char *pszResampling, 00946 int nOverviews, int *panOverviewList, 00947 int nListBands, int *panBandList, 00948 GDALProgressFunc pfnProgress, 00949 void * pProgressData ) 00950 00951 { 00952 if( pfnProgress == NULL ) 00953 pfnProgress = GDALDummyProgress; 00954 00955 if( oOvManager.IsInitialized() ) 00956 return oOvManager.BuildOverviews( NULL, pszResampling, 00957 nOverviews, panOverviewList, 00958 nListBands, panBandList, 00959 pfnProgress, pProgressData ); 00960 else 00961 { 00962 CPLError( CE_Failure, CPLE_NotSupported, 00963 "BuildOverviews() not supported for this dataset." ); 00964 00965 return( CE_Failure ); 00966 } 00967 } 00968