Changeset 13776
- Timestamp:
- 02/13/08 17:54:16 (3 months ago)
- Files:
-
- trunk/gdal/frmts/gsg/gsagdataset.cpp (modified) (9 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/gdal/frmts/gsg/gsagdataset.cpp
r13734 r13776 85 85 86 86 static GDALDataset *Open( GDALOpenInfo * ); 87 static GDALDataset *Create( const char * pszFilename,88 int nXSize, int nYSize, int nBands,89 GDALDataType eType,90 char **papszParmList );91 87 static GDALDataset *CreateCopy( const char *pszFilename, 92 88 GDALDataset *poSrcDS, … … 124 120 125 121 vsi_l_offset *panLineOffset; 122 int nLastReadLine; 126 123 127 124 double *padfRowMinZ; … … 192 189 } 193 190 194 panLineOffset[0] = nDataStart; 191 panLineOffset[poDS->nRasterYSize-1] = nDataStart; 192 nLastReadLine = poDS->nRasterYSize; 195 193 } 196 194 … … 309 307 310 308 if( nBlockYOff < 0 || nBlockYOff > nRasterYSize - 1 || nBlockXOff != 0 ) 311 return CE_Failure;309 return CE_Failure; 312 310 313 311 if( panLineOffset[nBlockYOff] == 0 ) 314 IReadBlock( nBlockXOff, nBlockYOff-1, NULL ); 312 { 313 // Discover the last read block 314 for ( int iFoundLine = nLastReadLine - 1; iFoundLine > nBlockYOff; iFoundLine--) 315 { 316 IReadBlock( nBlockXOff, iFoundLine, NULL); 317 } 318 } 315 319 316 320 if( panLineOffset[nBlockYOff] == 0 ) 317 return CE_Failure; 318 321 return CE_Failure; 319 322 if( VSIFSeekL( poGDS->fp, panLineOffset[nBlockYOff], SEEK_SET ) != 0 ) 320 323 { 321 CPLError( CE_Failure, CPLE_FileIO,322 "Can't seek to offset %ld to read grid row %d.",323 panLineOffset[nBlockYOff], nBlockYOff );324 return CE_Failure;324 CPLError( CE_Failure, CPLE_FileIO, 325 "Can't seek to offset %ld to read grid row %d.", 326 panLineOffset[nBlockYOff], nBlockYOff ); 327 return CE_Failure; 325 328 } 326 329 … … 330 333 size_t nCharsExamined = 0; 331 334 /* If we know the offsets, we can just read line directly */ 332 if( panLineOffset[nBlockYOff+1] != 0)333 { 334 assert(panLineOffset[nBlockYOff +1] > panLineOffset[nBlockYOff]);335 nLineBufSize = panLineOffset[nBlockYOff +1]335 if( (nBlockYOff > 0) && ( panLineOffset[nBlockYOff-1] != 0 ) ) 336 { 337 assert(panLineOffset[nBlockYOff-1] > panLineOffset[nBlockYOff]); 338 nLineBufSize = panLineOffset[nBlockYOff-1] 336 339 - panLineOffset[nBlockYOff] + 1; 337 340 } … … 525 528 nMaxLineSize = nCharsExamined + 1; 526 529 527 panLineOffset[nBlockYOff + 1] = panLineOffset[nBlockYOff] + nCharsExamined; 530 panLineOffset[nBlockYOff - 1] = panLineOffset[nBlockYOff] + nCharsExamined; 531 nLastReadLine = nBlockYOff; 528 532 529 533 VSIFree( szLineBuf ); … … 1453 1457 1454 1458 /************************************************************************/ 1455 /* Create() */1456 /************************************************************************/1457 1458 GDALDataset *GSAGDataset::Create( const char * pszFilename,1459 int nXSize, int nYSize, int nBands,1460 GDALDataType eType,1461 char **papszParmList )1462 1463 {1464 if( nXSize <= 0 || nYSize <= 0 )1465 {1466 CPLError( CE_Failure, CPLE_IllegalArg,1467 "Unable to create grid, both X and Y size must be "1468 "non-negative.\n" );1469 1470 return NULL;1471 }1472 1473 if( eType != GDT_Byte && eType != GDT_Float32 && eType != GDT_UInt161474 && eType != GDT_Int16 && eType != GDT_Float64 && eType != GDT_Int321475 && eType != GDT_UInt32 )1476 {1477 CPLError( CE_Failure, CPLE_AppDefined,1478 "Golden Software ASCII Grid only supports Byte, Int16, "1479 "Uint16, Int32, Uint32, Float32, and Float64 datatypes. "1480 "Unable to create with type %s.\n",1481 GDALGetDataTypeName( eType ) );1482 1483 return NULL;1484 }1485 1486 FILE *fp = VSIFOpenL( pszFilename, "w+b" );1487 1488 if( fp == NULL )1489 {1490 CPLError( CE_Failure, CPLE_OpenFailed,1491 "Attempt to create file '%s' failed.\n",1492 pszFilename );1493 return NULL;1494 }1495 1496 std::ostringstream ssHeader;1497 1498 ssHeader << "DSAA\x0D\x0A";1499 ssHeader << nXSize << " " << nYSize << "\x0D\x0A";1500 ssHeader << 0.0 << " " << nXSize << "\x0D\x0A";1501 ssHeader << 0.0 << " " << nYSize << "\x0D\x0A";1502 ssHeader << 0.0 << " " << 0.0 << "\x0D\x0A";1503 1504 if( VSIFWriteL( (void *)ssHeader.str().c_str(), 1, ssHeader.str().length(),1505 fp ) != ssHeader.str().length() )1506 {1507 VSIFCloseL( fp );1508 CPLError( CE_Failure, CPLE_FileIO,1509 "Unable to create copy, writing header failed.\n" );1510 return NULL;1511 }1512 1513 /* grid data, row major, max 10 values per line (to mimic Surfer) */1514 std::ostringstream ssOutStream;1515 ssOutStream.precision( nFIELD_PRECISION );1516 ssOutStream.setf( std::ios::uppercase );1517 ssOutStream << dfNODATA_VALUE << " ";1518 CPLString sOut = ssOutStream.str();1519 const char *szOut = sOut.c_str();1520 size_t nOutLen = sOut.length();1521 for( int iRow=0; iRow<nYSize; iRow++ )1522 {1523 for( int iLine=0; iLine<nXSize/10+1; iLine++ )1524 {1525 for( int iCol=0; iCol<10 && iLine*10+iCol<nXSize; iCol++ )1526 {1527 if( VSIFWriteL( szOut, 1, nOutLen, fp ) != nOutLen )1528 {1529 CPLError( CE_Failure, CPLE_FileIO,1530 "Unable to write grid cell. Disk full?\n" );1531 return NULL;1532 }1533 }1534 1535 if( VSIFWriteL( (void *)"\x0D\x0A", 1, 2, fp ) != 2 )1536 {1537 CPLError( CE_Failure, CPLE_FileIO,1538 "Unable to finish write of grid line. Disk full?\n" );1539 return NULL;1540 }1541 }1542 1543 if( VSIFWriteL( (void *)"\x0D\x0A", 1, 2, fp ) != 2 )1544 {1545 CPLError( CE_Failure, CPLE_FileIO,1546 "Unable to finish write of grid row. Disk full?\n" );1547 return NULL;1548 }1549 }1550 1551 VSIFCloseL( fp );1552 1553 return (GDALDataset *)GDALOpen( pszFilename, GA_Update );1554 }1555 1556 /************************************************************************/1557 1459 /* CreateCopy() */ 1558 1460 /************************************************************************/ … … 1661 1563 for( int iRow=0; iRow<nYSize; iRow++ ) 1662 1564 { 1663 CPLErr eErr = poSrcBand->RasterIO( GF_Read, 0, iRow,1565 CPLErr eErr = poSrcBand->RasterIO( GF_Read, 0, nYSize-iRow-1, 1664 1566 nXSize, 1, pdfData, 1665 1567 nXSize, 1, GDT_Float64, 0, 0 ); … … 1904 1806 "Float32 Float64" ); 1905 1807 1906 poDriver->pfnOpen = GSAGDataset::Open; 1907 poDriver->pfnCreate = GSAGDataset::Create; 1808 poDriver->pfnOpen = GSAGDataset::Open; 1908 1809 poDriver->pfnCreateCopy = GSAGDataset::CreateCopy; 1909 1810 poDriver->pfnDelete = GSAGDataset::Delete;
