Main Page | Class Hierarchy | Class List | File List | Class Members | File Members | Related Pages

Sample Driver: jdemdataset.cpp

/****************************************************************************** * $Id: jdemdataset.cpp,v 1.8 2003/07/08 21:21:56 warmerda Exp $ * * Project: JDEM Reader * Purpose: All code for Japanese DEM Reader * Author: Frank Warmerdam, warmerdam@pobox.com * ****************************************************************************** * Copyright (c) 2000, Frank Warmerdam <warmerdam@pobox.com> * * 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: jdemdataset.cpp,v $ * Revision 1.8 2003/07/08 21:21:56 warmerda * avoid warnings * * Revision 1.7 2002/09/04 06:50:37 warmerda * avoid static driver pointers * * Revision 1.6 2002/06/12 21:12:25 warmerda * update to metadata based driver info * * Revision 1.5 2001/11/11 23:51:00 warmerda * added required class keyword to friend declarations * * Revision 1.4 2001/07/18 04:51:57 warmerda * added CPL_CVSID * * Revision 1.3 2001/06/21 19:59:51 warmerda * added help link * * Revision 1.2 2000/11/28 02:28:54 warmerda * Added error checks, GetGeoTransform and GetProjection * * Revision 1.1 2000/11/27 19:03:26 warmerda * New * */ #include "gdal_priv.h" CPL_CVSID("$Id: jdemdataset.cpp,v 1.8 2003/07/08 21:21:56 warmerda Exp $"); CPL_C_START void GDALRegister_JDEM(void); CPL_C_END /************************************************************************/ /* JDEMGetField() */ /************************************************************************/ static int JDEMGetField( char *pszField, int nWidth ) { char szWork[32]; CPLAssert( nWidth < (int) sizeof(szWork) ); strncpy( szWork, pszField, nWidth ); szWork[nWidth] = '\0'; return atoi(szWork); } /************************************************************************/ /* JDEMGetAngle() */ /************************************************************************/ static double JDEMGetAngle( char *pszField ) { int nAngle = JDEMGetField( pszField, 7 ); int nDegree, nMin, nSec; // Note, this isn't very general purpose, but it would appear // from the field widths that angles are never negative. Nice // to be a country in the "first quadrant". nDegree = nAngle / 10000; nMin = (nAngle / 100) % 100; nSec = nAngle % 100; return nDegree + nMin / 60.0 + nSec / 3600.0; } /************************************************************************/ /* ==================================================================== */ /* JDEMDataset */ /* ==================================================================== */ /************************************************************************/ class JDEMRasterBand; class JDEMDataset : public GDALDataset { friend class JDEMRasterBand; FILE *fp; GByte abyHeader[1012]; public: ~JDEMDataset(); static GDALDataset *Open( GDALOpenInfo * ); CPLErr GetGeoTransform( double * padfTransform ); const char *GetProjectionRef(); }; /************************************************************************/ /* ==================================================================== */ /* JDEMRasterBand */ /* ==================================================================== */ /************************************************************************/ class JDEMRasterBand : public GDALRasterBand { friend class JDEMDataset; public: JDEMRasterBand( JDEMDataset *, int ); virtual CPLErr IReadBlock( int, int, void * ); }; /************************************************************************/ /* JDEMRasterBand() */ /************************************************************************/ JDEMRasterBand::JDEMRasterBand( JDEMDataset *poDS, int nBand ) { this->poDS = poDS; this->nBand = nBand; eDataType = GDT_Float32; nBlockXSize = poDS->GetRasterXSize(); nBlockYSize = 1; } /************************************************************************/ /* IReadBlock() */ /************************************************************************/ CPLErr JDEMRasterBand::IReadBlock( int nBlockXOff, int nBlockYOff, void * pImage ) { JDEMDataset *poGDS = (JDEMDataset *) poDS; char *pszRecord; int nRecordSize = nBlockXSize*5 + 9 + 2; int i; VSIFSeek( poGDS->fp, 1011 + nRecordSize*nBlockYOff, SEEK_SET ); pszRecord = (char *) CPLMalloc(nRecordSize); VSIFRead( pszRecord, 1, nRecordSize, poGDS->fp ); if( !EQUALN((char *) poGDS->abyHeader,pszRecord,6) ) { CPLFree( pszRecord ); CPLError( CE_Failure, CPLE_AppDefined, "JDEM Scanline corrupt. Perhaps file was not transferred\n" "in binary mode?" ); return CE_Failure; } if( JDEMGetField( pszRecord + 6, 3 ) != nBlockYOff + 1 ) { CPLFree( pszRecord ); CPLError( CE_Failure, CPLE_AppDefined, "JDEM scanline out of order, JDEM driver does not\n" "currently support partial datasets." ); return CE_Failure; } for( i = 0; i < nBlockXSize; i++ ) ((float *) pImage)[i] = (float) (JDEMGetField( pszRecord + 9 + 5 * i, 5) * 0.1); return CE_None; } /************************************************************************/ /* ==================================================================== */ /* JDEMDataset */ /* ==================================================================== */ /************************************************************************/ /************************************************************************/ /* ~JDEMDataset() */ /************************************************************************/ JDEMDataset::~JDEMDataset() { if( fp != NULL ) VSIFClose( fp ); } /************************************************************************/ /* GetGeoTransform() */ /************************************************************************/ CPLErr JDEMDataset::GetGeoTransform( double * padfTransform ) { double dfLLLat, dfLLLong, dfURLat, dfURLong; dfLLLat = JDEMGetAngle( (char *) abyHeader + 29 ); dfLLLong = JDEMGetAngle( (char *) abyHeader + 36 ); dfURLat = JDEMGetAngle( (char *) abyHeader + 43 ); dfURLong = JDEMGetAngle( (char *) abyHeader + 50 ); padfTransform[0] = dfLLLong; padfTransform[3] = dfURLat; padfTransform[1] = (dfURLong - dfLLLong) / GetRasterXSize(); padfTransform[2] = 0.0; padfTransform[4] = 0.0; padfTransform[5] = -1 * (dfURLat - dfLLLat) / GetRasterYSize(); return CE_None; } /************************************************************************/ /* GetProjectionRef() */ /************************************************************************/ const char *JDEMDataset::GetProjectionRef() { return( "GEOGCS[\"Tokyo\",DATUM[\"Tokyo\",SPHEROID[\"Bessel 1841\",6377397.155,299.1528128,AUTHORITY[\"EPSG\",7004]],TOWGS84[-148,507,685,0,0,0,0],AUTHORITY[\"EPSG\",6301]],PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",8901]],UNIT[\"DMSH\",0.0174532925199433,AUTHORITY[\"EPSG\",9108]],AXIS[\"Lat\",NORTH],AXIS[\"Long\",EAST],AUTHORITY[\"EPSG\",4301]]" ); } /************************************************************************/ /* Open() */ /************************************************************************/ GDALDataset *JDEMDataset::Open( GDALOpenInfo * poOpenInfo ) { /* -------------------------------------------------------------------- */ /* Before trying JDEMOpen() we first verify that there is at */ /* least one "\n#keyword" type signature in the first chunk of */ /* the file. */ /* -------------------------------------------------------------------- */ if( poOpenInfo->fp == NULL || poOpenInfo->nHeaderBytes < 50 ) return NULL; /* check if century values seem reasonable */ if( (!EQUALN((char *)poOpenInfo->pabyHeader+11,"19",2) && !EQUALN((char *)poOpenInfo->pabyHeader+11,"20",2)) || (!EQUALN((char *)poOpenInfo->pabyHeader+15,"19",2) && !EQUALN((char *)poOpenInfo->pabyHeader+15,"20",2)) || (!EQUALN((char *)poOpenInfo->pabyHeader+19,"19",2) && !EQUALN((char *)poOpenInfo->pabyHeader+19,"20",2)) ) { return NULL; } /* -------------------------------------------------------------------- */ /* Create a corresponding GDALDataset. */ /* -------------------------------------------------------------------- */ JDEMDataset *poDS; poDS = new JDEMDataset(); poDS->fp = poOpenInfo->fp; poOpenInfo->fp = NULL; /* -------------------------------------------------------------------- */ /* Read the header. */ /* -------------------------------------------------------------------- */ VSIFSeek( poDS->fp, 0, SEEK_SET ); VSIFRead( poDS->abyHeader, 1, 1012, poDS->fp ); poDS->nRasterXSize = JDEMGetField( (char *) poDS->abyHeader + 23, 3 ); poDS->nRasterYSize = JDEMGetField( (char *) poDS->abyHeader + 26, 3 ); /* -------------------------------------------------------------------- */ /* Create band information objects. */ /* -------------------------------------------------------------------- */ poDS->SetBand( 1, new JDEMRasterBand( poDS, 1 )); return( poDS ); } /************************************************************************/ /* GDALRegister_JDEM() */ /************************************************************************/ void GDALRegister_JDEM() { GDALDriver *poDriver; if( GDALGetDriverByName( "JDEM" ) == NULL ) { poDriver = new GDALDriver(); poDriver->SetDescription( "JDEM" ); poDriver->SetMetadataItem( GDAL_DMD_LONGNAME, "Japanese DEM (.mem)" ); poDriver->SetMetadataItem( GDAL_DMD_HELPTOPIC, "frmt_various.html#JDEM" ); poDriver->SetMetadataItem( GDAL_DMD_EXTENSION, "mem" ); poDriver->pfnOpen = JDEMDataset::Open; GetGDALDriverManager()->RegisterDriver( poDriver ); } }
00001 /****************************************************************************** 00002 * $Id: jdemdataset.cpp,v 1.8 2003/07/08 21:21:56 warmerda Exp $ 00003 * 00004 * Project: JDEM Reader 00005 * Purpose: All code for Japanese DEM Reader 00006 * Author: Frank Warmerdam, warmerdam@pobox.com 00007 * 00008 ****************************************************************************** 00009 * Copyright (c) 2000, Frank Warmerdam <warmerdam@pobox.com> 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: jdemdataset.cpp,v $ 00031 * Revision 1.8 2003/07/08 21:21:56 warmerda 00032 * avoid warnings 00033 * 00034 * Revision 1.7 2002/09/04 06:50:37 warmerda 00035 * avoid static driver pointers 00036 * 00037 * Revision 1.6 2002/06/12 21:12:25 warmerda 00038 * update to metadata based driver info 00039 * 00040 * Revision 1.5 2001/11/11 23:51:00 warmerda 00041 * added required class keyword to friend declarations 00042 * 00043 * Revision 1.4 2001/07/18 04:51:57 warmerda 00044 * added CPL_CVSID 00045 * 00046 * Revision 1.3 2001/06/21 19:59:51 warmerda 00047 * added help link 00048 * 00049 * Revision 1.2 2000/11/28 02:28:54 warmerda 00050 * Added error checks, GetGeoTransform and GetProjection 00051 * 00052 * Revision 1.1 2000/11/27 19:03:26 warmerda 00053 * New 00054 * 00055 */ 00056 00057 #include "gdal_priv.h" 00058 00059 CPL_CVSID("$Id: jdemdataset.cpp,v 1.8 2003/07/08 21:21:56 warmerda Exp $"); 00060 00061 CPL_C_START 00062 void GDALRegister_JDEM(void); 00063 CPL_C_END 00064 00065 /************************************************************************/ 00066 /* JDEMGetField() */ 00067 /************************************************************************/ 00068 00069 static int JDEMGetField( char *pszField, int nWidth ) 00070 00071 { 00072 char szWork[32]; 00073 00074 CPLAssert( nWidth < (int) sizeof(szWork) ); 00075 00076 strncpy( szWork, pszField, nWidth ); 00077 szWork[nWidth] = '\0'; 00078 00079 return atoi(szWork); 00080 } 00081 00082 /************************************************************************/ 00083 /* JDEMGetAngle() */ 00084 /************************************************************************/ 00085 00086 static double JDEMGetAngle( char *pszField ) 00087 00088 { 00089 int nAngle = JDEMGetField( pszField, 7 ); 00090 int nDegree, nMin, nSec; 00091 00092 // Note, this isn't very general purpose, but it would appear 00093 // from the field widths that angles are never negative. Nice 00094 // to be a country in the "first quadrant". 00095 00096 nDegree = nAngle / 10000; 00097 nMin = (nAngle / 100) % 100; 00098 nSec = nAngle % 100; 00099 00100 return nDegree + nMin / 60.0 + nSec / 3600.0; 00101 } 00102 00103 /************************************************************************/ 00104 /* ==================================================================== */ 00105 /* JDEMDataset */ 00106 /* ==================================================================== */ 00107 /************************************************************************/ 00108 00109 class JDEMRasterBand; 00110 00111 class JDEMDataset : public GDALDataset 00112 { 00113 friend class JDEMRasterBand; 00114 00115 FILE *fp; 00116 GByte abyHeader[1012]; 00117 00118 public: 00119 ~JDEMDataset(); 00120 00121 static GDALDataset *Open( GDALOpenInfo * ); 00122 00123 CPLErr GetGeoTransform( double * padfTransform ); 00124 const char *GetProjectionRef(); 00125 }; 00126 00127 /************************************************************************/ 00128 /* ==================================================================== */ 00129 /* JDEMRasterBand */ 00130 /* ==================================================================== */ 00131 /************************************************************************/ 00132 00133 class JDEMRasterBand : public GDALRasterBand 00134 { 00135 friend class JDEMDataset; 00136 00137 public: 00138 00139 JDEMRasterBand( JDEMDataset *, int ); 00140 00141 virtual CPLErr IReadBlock( int, int, void * ); 00142 }; 00143 00144 00145 /************************************************************************/ 00146 /* JDEMRasterBand() */ 00147 /************************************************************************/ 00148 00149 JDEMRasterBand::JDEMRasterBand( JDEMDataset *poDS, int nBand ) 00150 00151 { 00152 this->poDS = poDS; 00153 this->nBand = nBand; 00154 00155 eDataType = GDT_Float32; 00156 00157 nBlockXSize = poDS->GetRasterXSize(); 00158 nBlockYSize = 1; 00159 } 00160 00161 /************************************************************************/ 00162 /* IReadBlock() */ 00163 /************************************************************************/ 00164 00165 CPLErr JDEMRasterBand::IReadBlock( int nBlockXOff, int nBlockYOff, 00166 void * pImage ) 00167 00168 { 00169 JDEMDataset *poGDS = (JDEMDataset *) poDS; 00170 char *pszRecord; 00171 int nRecordSize = nBlockXSize*5 + 9 + 2; 00172 int i; 00173 00174 VSIFSeek( poGDS->fp, 1011 + nRecordSize*nBlockYOff, SEEK_SET ); 00175 00176 pszRecord = (char *) CPLMalloc(nRecordSize); 00177 VSIFRead( pszRecord, 1, nRecordSize, poGDS->fp ); 00178 00179 if( !EQUALN((char *) poGDS->abyHeader,pszRecord,6) ) 00180 { 00181 CPLFree( pszRecord ); 00182 00183 CPLError( CE_Failure, CPLE_AppDefined, 00184 "JDEM Scanline corrupt. Perhaps file was not transferred\n" 00185 "in binary mode?" ); 00186 return CE_Failure; 00187 } 00188 00189 if( JDEMGetField( pszRecord + 6, 3 ) != nBlockYOff + 1 ) 00190 { 00191 CPLFree( pszRecord ); 00192 00193 CPLError( CE_Failure, CPLE_AppDefined, 00194 "JDEM scanline out of order, JDEM driver does not\n" 00195 "currently support partial datasets." ); 00196 return CE_Failure; 00197 } 00198 00199 for( i = 0; i < nBlockXSize; i++ ) 00200 ((float *) pImage)[i] = (float) 00201 (JDEMGetField( pszRecord + 9 + 5 * i, 5) * 0.1); 00202 00203 return CE_None; 00204 } 00205 00206 /************************************************************************/ 00207 /* ==================================================================== */ 00208 /* JDEMDataset */ 00209 /* ==================================================================== */ 00210 /************************************************************************/ 00211 00212 /************************************************************************/ 00213 /* ~JDEMDataset() */ 00214 /************************************************************************/ 00215 00216 JDEMDataset::~JDEMDataset() 00217 00218 { 00219 if( fp != NULL ) 00220 VSIFClose( fp ); 00221 } 00222 00223 /************************************************************************/ 00224 /* GetGeoTransform() */ 00225 /************************************************************************/ 00226 00227 CPLErr JDEMDataset::GetGeoTransform( double * padfTransform ) 00228 00229 { 00230 double dfLLLat, dfLLLong, dfURLat, dfURLong; 00231 00232 dfLLLat = JDEMGetAngle( (char *) abyHeader + 29 ); 00233 dfLLLong = JDEMGetAngle( (char *) abyHeader + 36 ); 00234 dfURLat = JDEMGetAngle( (char *) abyHeader + 43 ); 00235 dfURLong = JDEMGetAngle( (char *) abyHeader + 50 ); 00236 00237 padfTransform[0] = dfLLLong; 00238 padfTransform[3] = dfURLat; 00239 padfTransform[1] = (dfURLong - dfLLLong) / GetRasterXSize(); 00240 padfTransform[2] = 0.0; 00241 00242 padfTransform[4] = 0.0; 00243 padfTransform[5] = -1 * (dfURLat - dfLLLat) / GetRasterYSize(); 00244 00245 00246 return CE_None; 00247 } 00248 00249 /************************************************************************/ 00250 /* GetProjectionRef() */ 00251 /************************************************************************/ 00252 00253 const char *JDEMDataset::GetProjectionRef() 00254 00255 { 00256 return( "GEOGCS[\"Tokyo\",DATUM[\"Tokyo\",SPHEROID[\"Bessel 1841\",6377397.155,299.1528128,AUTHORITY[\"EPSG\",7004]],TOWGS84[-148,507,685,0,0,0,0],AUTHORITY[\"EPSG\",6301]],PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",8901]],UNIT[\"DMSH\",0.0174532925199433,AUTHORITY[\"EPSG\",9108]],AXIS[\"Lat\",NORTH],AXIS[\"Long\",EAST],AUTHORITY[\"EPSG\",4301]]" ); 00257 } 00258 00259 /************************************************************************/ 00260 /* Open() */ 00261 /************************************************************************/ 00262 00263 GDALDataset *JDEMDataset::Open( GDALOpenInfo * poOpenInfo ) 00264 00265 { 00266 /* -------------------------------------------------------------------- */ 00267 /* Before trying JDEMOpen() we first verify that there is at */ 00268 /* least one "\n#keyword" type signature in the first chunk of */ 00269 /* the file. */ 00270 /* -------------------------------------------------------------------- */ 00271 if( poOpenInfo->fp == NULL || poOpenInfo->nHeaderBytes < 50 ) 00272 return NULL; 00273 00274 /* check if century values seem reasonable */ 00275 if( (!EQUALN((char *)poOpenInfo->pabyHeader+11,"19",2) 00276 && !EQUALN((char *)poOpenInfo->pabyHeader+11,"20",2)) 00277 || (!EQUALN((char *)poOpenInfo->pabyHeader+15,"19",2) 00278 && !EQUALN((char *)poOpenInfo->pabyHeader+15,"20",2)) 00279 || (!EQUALN((char *)poOpenInfo->pabyHeader+19,"19",2) 00280 && !EQUALN((char *)poOpenInfo->pabyHeader+19,"20",2)) ) 00281 { 00282 return NULL; 00283 } 00284 00285 /* -------------------------------------------------------------------- */ 00286 /* Create a corresponding GDALDataset. */ 00287 /* -------------------------------------------------------------------- */ 00288 JDEMDataset *poDS; 00289 00290 poDS = new JDEMDataset(); 00291 00292 poDS->fp = poOpenInfo->fp; 00293 poOpenInfo->fp = NULL; 00294 00295 /* -------------------------------------------------------------------- */ 00296 /* Read the header. */ 00297 /* -------------------------------------------------------------------- */ 00298 VSIFSeek( poDS->fp, 0, SEEK_SET ); 00299 VSIFRead( poDS->abyHeader, 1, 1012, poDS->fp ); 00300 00301 poDS->nRasterXSize = JDEMGetField( (char *) poDS->abyHeader + 23, 3 ); 00302 poDS->nRasterYSize = JDEMGetField( (char *) poDS->abyHeader + 26, 3 ); 00303 00304 /* -------------------------------------------------------------------- */ 00305 /* Create band information objects. */ 00306 /* -------------------------------------------------------------------- */ 00307 poDS->SetBand( 1, new JDEMRasterBand( poDS, 1 )); 00308 00309 return( poDS ); 00310 } 00311 00312 /************************************************************************/ 00313 /* GDALRegister_JDEM() */ 00314 /************************************************************************/ 00315 00316 void GDALRegister_JDEM() 00317 00318 { 00319 GDALDriver *poDriver; 00320 00321 if( GDALGetDriverByName( "JDEM" ) == NULL ) 00322 { 00323 poDriver = new GDALDriver(); 00324 00325 poDriver->SetDescription( "JDEM" ); 00326 poDriver->SetMetadataItem( GDAL_DMD_LONGNAME, 00327 "Japanese DEM (.mem)" ); 00328 poDriver->SetMetadataItem( GDAL_DMD_HELPTOPIC, 00329 "frmt_various.html#JDEM" ); 00330 poDriver->SetMetadataItem( GDAL_DMD_EXTENSION, "mem" ); 00331 00332 poDriver->pfnOpen = JDEMDataset::Open; 00333 00334 GetGDALDriverManager()->RegisterDriver( poDriver ); 00335 } 00336 }

Generated on Thu Jul 29 19:47:53 2004 for GDAL by doxygen 1.3.7