深入FDO----处理栅格图像

FDO不仅支持几何数据,而支持栅格数据,用户可以通过Raster ProviderGDAL Provider读取栅格数据。而且,这两种Provider都支持函数MOSAICCLIPRESAMPLE,可以将多张栅格图像合并起来,截取栅格图像的一部分。

FDO使用类FdoIRaster来表示栅格图像,使用这个类可以读取和设置图像的位置、大小、图像中包含数据的类型、图像的存储和组织模型等信息。调用方法FdoIRaster::GetImageXSize()FdoIRaster::SetImageXSize(...)可以读取和设置图像水平方向的像素数,调用方法FdoIRaster::GetImageYSize()FdoIRaster::SetImageYSize(...)可以读取和设置图像垂直方向的像素数,调用方法FdoIRaster::GetBounds()FdoIRaster::SetBounds(...)可以读取和设置图像地理位置,调用方法FdoIRaster::GetDataModel()FdoIRaster::SetDataModel(...)可以读取和设置图像数据模型,调用方法FdoIRaster::GetStreamReader()可以读取图像中的数据。

FDO使用类FdoRasterDataModel表示图像的数据模型。它定义了图像数据模型的类型,例如BitonalDataGrayPaletteRGBRGBA等;也定义了图像数据的类型,例如DoubleFloatIntegerUnsignedInteger等;还定义了每个像素占用的字节数等信息。在调用方法FdoIRaster::GetStreamReader()读取图像中的数据时,我们往往需要借助FdoRasterDataModel来解释图像数据。使用类FdoRasterDataModel还可以设置图像的数据模型,假设原始图像使用Palette数据模型,每个像素使用一个字节,如果设置图像的数据模型为RGBA,每个像素使用四个字节,那么在读取图像数据时会将图像数据转换为四字节的RGBA格式的数据。

有时候图像不是矩形,或者图像中有一些空洞,但是方法FdoIRaster::GetStreamReader()始终返回一个矩形区域数据,这时我们就需要一个值表示某个像素是一个无意义的值,调用方法FdoIRaster::GetNullPixelValue()可以得到这个值。

给定一个区域(dMinX, dMinY)~(dMaxX, dMaxY),如下的代码展示了如何截取并读取此区域的图像。

// 创建FDO连接

FdoPtr<IConnectionManager> manager =

FdoFeatureAccessManager::GetConnectionManager();

FdoPtr<FdoIConnection> connection = manager->CreateConnection (L"OSGeo.Gdal");

// 设置配置文件

FdoPtr<FdoIoStream> stream = FdoIoFileStream::Create(L"config.xml", L"r");

connection->SetConfiguration(stream);

// 打开连接

connection->Open();

// 创建命令

FdoICommand* cmd = connection->CreateCommand(FdoCommandType_Select);

FdoPtr<FdoISelect> cmdSelect = static_cast<FdoISelect*>(cmd);

// 设置查询的要素类名称

cmdSelect->SetFeatureClassName(L"Photo");

// 创建和设置过滤器

FdoPtr<FdoFgfGeometryFactory> agfFactory = FdoFgfGeometryFactory::GetInstance();

FdoPtr<FdoIEnvelope> envelope =

agfFactory->CreateEnvelopeXY(dMinX, dMinY, dMaxX, dMaxY);

FdoPtr<FdoIGeometry> geometry = agfFactory->CreateGeometry(envelope);

FdoPtr<FdoByteArray> byteArray = agfFactory->GetFgf(geometry);

FdoPtr<FdoGeometryValue> geoValue = FdoGeometryValue::Create(byteArray);

FdoPtr<FdoFilter> filter =

FdoSpatialCondition::Create(L"Image", FdoSpatialOperations_Intersects, geoValue);

cmdSelect->SetFilter(filter);

// 创建函数resample(Image, minX, minY, maxX, maxY, height, width)

FdoPtr<FdoExpressionCollection> funcParams = FdoExpressionCollection::Create();

FdoPtr<FdoIdentifier> rasterProp = FdoIdentifier::Create(L"Image");

funcParams->Add(rasterProp);

FdoPtr<FdoDataValue> minX = FdoDataValue::Create(dMinX, FdoDataType_Double);

funcParams->Add(minX);

FdoPtr<FdoDataValue> minY = FdoDataValue::Create(dMinY, FdoDataType_Double);

funcParams->Add(minY);

FdoPtr<FdoDataValue> maxX = FdoDataValue::Create(dMaxX, FdoDataType_Double);

funcParams->Add(maxX);

FdoPtr<FdoDataValue> maxY = FdoDataValue::Create(dMaxY, FdoDataType_Double);

funcParams->Add(maxY);

FdoPtr<FdoDataValue> height = FdoDataValue::Create(500);

funcParams->Add(height);

FdoPtr<FdoDataValue> width = FdoDataValue::Create(500);

funcParams->Add(width);

FdoPtr<FdoFunction> resampleFunc = FdoFunction::Create(L"RESAMPLE", funcParams);

FdoPtr<FdoComputedIdentifier> resampleIdentifier =

FdoComputedIdentifier::Create(L"resampledRaster", resampleFunc );

// 将属性resampledRaster加入返回的属性列表中

FdoPtr<FdoIdentifierCollection> propsToSelect = cmdSelect->GetPropertyNames();

propsToSelect->Add(resampleIdentifier);

// 执行查询

FdoPtr<FdoIFeatureReader> featReader = cmdSelect->Execute();

// 得到栅格属性值

featReader->ReadNext();

FdoPtr<FdoIRaster> raster = featReader->GetRaster(L"resampledRaster");

// 将栅格图像的数据模型转换为RGBA

FdoPtr<FdoRasterDataModel> dataModel = m_ raster->GetDataModel();

dataModel->SetDataModelType(FdoRasterDataModelType_RGBA);

dataModel->SetBitsPerPixel(32);

// 准备读取栅格数据

FdoIStreamReader* streamReader = raster->GetStreamReader();

FdoPtr<FdoIStreamReaderTmpl<FdoByte> > reader =

static_cast<FdoIStreamReaderTmpl<FdoByte>*>(streamReader); 

FdoInt32 bytesPerRow = 500*4;

FdoByte* buffer = new FdoByte[bytesPerRow];

// 按行读取栅格数据

FdoInt32 numRead = 0;

do {

FdoInt32 bytesRead = reader->ReadNext(buffer, 0, bytesPerRow);

       if (bytesRead == 0)       break;

// 处理图像数据

......

} while (true);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值