[gdal-dev] Loading from a stream/memory rather than a file
Martin Chapman
mchapman at texelinc.com
Fri Mar 7 17:48:51 EST 2008
Cristiaan,
Here's another way to do it that is really easy too...maybe it is the same
thing? I create a MEM dataset from a raster file but you could easily load
it with bytes from memory.
GDALDataset* pDataSource = (GDALDataset*) GDALOpen(sFileName.c_str(),
(GDALAccess) 0);
if (!pDataSource)
return AtlReportError(GetObjectCLSID(), _T((TCHAR*)
CPLGetLastErrorMsg()));
int nBandCount = pDataSource->GetRasterCount();
int nWidth = pDataSource->GetRasterXSize();
int nHeight = pDataSource->GetRasterYSize();
GDALDataType eBandType = pDataSource->GetRasterBand(1)->GetRasterDataType();
GDALDriver* pMemDriver = GetGDALDriverManager()->GetDriverByName("MEM");
if (!pMemDriver)
return AtlReportError(GetObjectCLSID(), _T((TCHAR*)
CPLGetLastErrorMsg()));
GDALDataset* pMemDataset = pMemDriver->Create(sStreamId.str().c_str(),
nWidth, nHeight, nBandCount, eBandType, NULL);
if (!pMemDataset)
return AtlReportError(GetObjectCLSID(), _T((TCHAR*)
CPLGetLastErrorMsg()));
for (int i = 0; i < nBandCount; i++ )
{
GDALRasterBand* pMemBand = pMemDataset->GetRasterBand(i + 1);
GDALRasterBand* pRasterBand = pDataSource->GetRasterBand(i + 1);
pMemBand->SetColorInterpretation(pRasterBand->GetColorInterpretation());
pMemBand->SetOffset(pRasterBand->GetOffset());
pMemBand->SetScale(pRasterBand->GetScale());
pMemBand->SetUnitType(pRasterBand->GetUnitType());
BOOL bSuccess = FALSE;
double nNullValue = pRasterBand->GetNoDataValue(&bSuccess);
if (bSuccess == TRUE)
{
GDALColorTable* pColorTable = pRasterBand->GetColorTable();
if (pColorTable != NULL)
{
GDALColorEntry colorEntry;
pColorTable->GetColorEntryAsRGB((int) nNullValue,
&colorEntry);
hr = pMemBand->SetNoDataValue((double)
colorEntry.c4);
if (FAILED(hr)) return hr;
}
else
{
hr = pMemBand->SetNoDataValue((double) nNullValue);
if (FAILED(hr)) return hr;
}
}
if (!pMemBand->GetColorTable())
{
GDALColorTable* pColorTable = pRasterBand->GetColorTable();
pMemBand->SetColorTable(pColorTable != NULL ?
pColorTable->Clone() : NULL);
}
unsigned char* pBytes = (unsigned char*) malloc(sizeof(unsigned
char) * nWidth * nHeight);
memset(pBytes, 0, nWidth * nHeight);
CPLErr err = pRasterBand->RasterIO(GF_Read, 0, 0, nWidth, nHeight,
pBytes, nWidth, nHeight, eBandType, 0, 0);
if (err != CE_None)
return AtlReportError(GetObjectCLSID(), _T((TCHAR*)
CPLGetLastErrorMsg()));
err = pMemBand->RasterIO(GF_Write, 0, 0, nWidth, nHeight, pBytes,
nWidth, nHeight, eBandType, 0, 0);
if (err != CE_None)
return AtlReportError(GetObjectCLSID(), _T((TCHAR*)
CPLGetLastErrorMsg()));
if (pBytes) delete [] pBytes;
}
if (pDataSource)
GDALClose(pDataSource);
pDataSource = NULL;
if (m_pMemDataset)
GDALClose(m_pMemDataset);
m_pMemDataset = pMemDataset;
Martin Chapman
-----Original Message-----
From: gdal-dev-bounces at lists.osgeo.org
[mailto:gdal-dev-bounces at lists.osgeo.org] On Behalf Of Frank Warmerdam
Sent: Friday, March 07, 2008 10:22 AM
To: Christiaan Janssen
Cc: gdal-dev at lists.osgeo.org
Subject: Re: [gdal-dev] Loading from a stream/memory rather than a file
Christiaan Janssen wrote:
> Does anyone know if it is possible to create/open an image with GDAL using
a
> stream or chunk of memory instead of a file? This would be useful in
writing
> a caching mechanism but also to do in memory and other non file based
> processing.
Cristiaan,
Some driver utilize the VSI*L "virtual io" layer for file access, and then
you can either right custom file handlers, or more likely use the existing
in-memory VSI*L implementation.
This is likely the best explanation of the mechanism:
http://www.gdal.org/cpl__vsi_8h.html#66e2e6f093fd42f8a941b962d4c8a19e
gdal/frmts/wcs/httpdriver.cpp is a simple example of it's use for reading
from memory.
Best regards,
--
---------------------------------------+------------------------------------
--
I set the clouds in motion - turn up | Frank Warmerdam,
warmerdam at pobox.com
light and sound - activate the windows | http://pobox.com/~warmerdam
and watch the world go round - Rush | President OSGeo, http://osgeo.org
_______________________________________________
gdal-dev mailing list
gdal-dev at lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/gdal-dev
More information about the gdal-dev
mailing list