Changeset 13776

Show
Ignore:
Timestamp:
02/13/08 17:54:16 (3 months ago)
Author:
warmerdam
Message:

Major update to correct upside problems, removing Create (#2224, #1616, #2191)

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/gdal/frmts/gsg/gsagdataset.cpp

    r13734 r13776  
    8585 
    8686    static GDALDataset *Open( GDALOpenInfo * ); 
    87     static GDALDataset *Create( const char * pszFilename, 
    88                                 int nXSize, int nYSize, int nBands, 
    89                                 GDALDataType eType, 
    90                                 char **papszParmList ); 
    9187    static GDALDataset *CreateCopy( const char *pszFilename, 
    9288                                    GDALDataset *poSrcDS, 
     
    124120 
    125121    vsi_l_offset *panLineOffset; 
     122        int nLastReadLine; 
    126123 
    127124    double *padfRowMinZ; 
     
    192189    } 
    193190 
    194     panLineOffset[0] = nDataStart; 
     191        panLineOffset[poDS->nRasterYSize-1] = nDataStart; 
     192        nLastReadLine = poDS->nRasterYSize; 
    195193} 
    196194 
     
    309307 
    310308    if( nBlockYOff < 0 || nBlockYOff > nRasterYSize - 1 || nBlockXOff != 0 ) 
    311        return CE_Failure; 
     309        return CE_Failure; 
    312310 
    313311    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    } 
    315319 
    316320    if( panLineOffset[nBlockYOff] == 0 ) 
    317         return CE_Failure; 
    318  
     321        return CE_Failure; 
    319322    if( VSIFSeekL( poGDS->fp, panLineOffset[nBlockYOff], SEEK_SET ) != 0 ) 
    320323    { 
    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; 
    325328    } 
    326329 
     
    330333    size_t nCharsExamined = 0; 
    331334    /* 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] 
    336339                              - panLineOffset[nBlockYOff] + 1; 
    337340    } 
     
    525528        nMaxLineSize = nCharsExamined + 1; 
    526529 
    527     panLineOffset[nBlockYOff + 1] = panLineOffset[nBlockYOff] + nCharsExamined; 
     530    panLineOffset[nBlockYOff - 1] = panLineOffset[nBlockYOff] + nCharsExamined; 
     531        nLastReadLine = nBlockYOff; 
    528532 
    529533    VSIFree( szLineBuf ); 
     
    14531457 
    14541458/************************************************************************/ 
    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_UInt16 
    1474         && eType != GDT_Int16 && eType != GDT_Float64 && eType != GDT_Int32 
    1475         && 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 /************************************************************************/ 
    15571459/*                             CreateCopy()                             */ 
    15581460/************************************************************************/ 
     
    16611563    for( int iRow=0; iRow<nYSize; iRow++ ) 
    16621564    { 
    1663         CPLErr eErr = poSrcBand->RasterIO( GF_Read, 0, iRow
     1565        CPLErr eErr = poSrcBand->RasterIO( GF_Read, 0, nYSize-iRow-1
    16641566                                           nXSize, 1, pdfData, 
    16651567                                           nXSize, 1, GDT_Float64, 0, 0 ); 
     
    19041806                                   "Float32 Float64" ); 
    19051807 
    1906         poDriver->pfnOpen = GSAGDataset::Open; 
    1907         poDriver->pfnCreate = GSAGDataset::Create; 
     1808    poDriver->pfnOpen = GSAGDataset::Open; 
    19081809        poDriver->pfnCreateCopy = GSAGDataset::CreateCopy; 
    19091810        poDriver->pfnDelete = GSAGDataset::Delete;