TableConnector *CoverageConnector::createTableConnector(ITable& attTable, Coverage *coverage, IlwisTypes tp) { QString dataFile = coverage->name(); QString attDom = dataFile; if ( hasType(tp,itRASTER)) { RasterCoverage *raster = static_cast<RasterCoverage *>(coverage); Resource resource = raster->datadef().domain()->source(); QFileInfo inf(resource.toLocalFile()); attDom = inf.fileName(); } int index = dataFile.lastIndexOf("."); if ( index != -1) { dataFile = dataFile.left(index); }else{ if ( tp == itPOLYGON) attDom += ".mpa"; if ( tp == itRASTER) attDom += ".mpr"; if ( tp == itPOINT) attDom += ".mpp"; if ( tp == itLINE) attDom += ".mps"; } _odf->setKeyValue("BaseMap","AttributeTable",dataFile + ".tbt"); attTable->setName(dataFile); QString dir = context()->workingCatalog()->location().toLocalFile(); QString filename = dir + "/" + dataFile + ".tbt"; TableConnector *conn = new TableConnector(Resource(QUrl::fromLocalFile(filename), itTABLE), false); //attribute domains are comming from ilwis4 always uniqueid and based on the map conn->attributeDomain(attDom); return conn; }
bool RasterCoverageConnector::loadData(IlwisObject* data, const IOOptions& options ){ quint32 bandindex = sourceRef().hasProperty("bandindex") ? sourceRef()["bandindex"].toUInt(): iUNDEF; auto layerHandle = gdal()->getRasterBand(_handle->handle(), bandindex != iUNDEF ? bandindex + 1 : 1); if (!layerHandle) { ERROR2(ERR_COULD_NOT_LOAD_2, "GDAL","layer"); return false; } RasterCoverage *raster = static_cast<RasterCoverage *>(data); UPGrid& grid = raster->gridRef(); quint32 linesPerBlock = grid->maxLines(); qint64 blockSizeBytes = grid->blockSize(0) * _typeSize; char *block = new char[blockSizeBytes]; quint64 totalLines =grid->size().ysize(); std::map<quint32, std::vector<quint32> > blocklimits; //blocklimits; key = band number, value= blocks needed from this band if ( bandindex == iUNDEF) blocklimits = grid->calcBlockLimits(options); else{ for(int i = 0; i < std::ceil((double)totalLines / linesPerBlock); ++i) blocklimits[bandindex].push_back(i); } for(const auto& layer : blocklimits){ quint64 linesLeft = totalLines - grid->maxLines() * layer.second[0]; //std::min((quint64)grid->maxLines(), totalLines - grid->maxLines() * layer.second[0]); if ( _colorModel == ColorRangeBase::cmNONE || raster->datadef().domain()->valueType() == itPALETTECOLOR){ // palette entries are just integers so we can use the numeric read for it layerHandle = gdal()->getRasterBand(_handle->handle(), layer.first + 1); int inLayerBlockIndex = layer.second[0] % grid->blocksPerBand(); // for(const auto& index : layer.second) { quint32 offsetIndex = bandindex == iUNDEF ? layer.first : (layer.first - bandindex); loadNumericBlock(layerHandle, index, inLayerBlockIndex, linesPerBlock, linesLeft, block, raster,offsetIndex ); if(!moveIndexes(linesPerBlock, linesLeft, inLayerBlockIndex)) break; } }else { // continous colorcase, combining 3/4 (gdal)layers into one int inLayerBlockIndex = layer.second[0] % grid->blocksPerBand(); // for(const auto& index : layer.second) { loadColorBlock(layer.first,index,inLayerBlockIndex,linesPerBlock,linesLeft,block,grid); if(!moveIndexes(linesPerBlock, linesLeft, inLayerBlockIndex)) // ensures that the administration with respect how much needs to be done is inorder break; } } } delete [] block; _binaryIsLoaded = true; return true; }
bool RasterCoverageConnector::store(IlwisObject *obj, int ) { if(!GdalConnector::store(obj, 0)) return false; RasterCoverage *raster = static_cast<RasterCoverage *>(obj); IDomain dom; if(!dom.prepare("code=value")) { //TODO for the moment only value maps in gdal return ERROR1(ERR_NO_INITIALIZED_1,obj->name()); } raster->datadef().domain(dom); Size sz = raster->size(); GDALDataType gdalType = ilwisType2GdalType(raster->datadef().range()->determineType()); GDALDriverH hdriver = gdal()->getGDALDriverByName(_gdalShortName.toLocal8Bit()); if ( hdriver == 0) { return ERROR2(ERR_COULDNT_CREATE_OBJECT_FOR_2, "driver",_gdalShortName); } QString filename = constructOutputName(hdriver); GDALDatasetH dataset = gdal()->create( hdriver, filename.toLocal8Bit(), sz.xsize(), sz.ysize(), sz.zsize(), gdalType, 0 ); if ( dataset == 0) { return ERROR2(ERR_COULDNT_CREATE_OBJECT_FOR_2, "data set",_filename); } bool ok = setGeotransform(raster, dataset); if (ok) ok = setSRS(raster, dataset); if (!ok) return false; switch(gdalType) { case GDT_Byte: ok = save<quint8>(raster, dataset,gdalType); break; case GDT_UInt16: ok = save<quint16>(raster, dataset,gdalType); break; case GDT_Int16: ok = save<qint16>(raster, dataset,gdalType); break; case GDT_Int32: ok = save<qint32>(raster, dataset,gdalType); break; case GDT_UInt32: ok = save<quint32>(raster, dataset,gdalType); break; case GDT_Float32: ok = save<float>(raster, dataset,gdalType); break; case GDT_Float64: ok = save<double>(raster, dataset,gdalType); break; default: ok= ERROR1(ERR_NO_INITIALIZED_1, "gdal Data type"); } gdal()->close(dataset); return ok; }
bool RasterCoverageConnector::store(IlwisObject *obj, const IOOptions & ) { if(!loadDriver()) return false; RasterCoverage *raster = static_cast<RasterCoverage *>(obj); DataDefinition currentDef = raster->datadefRef(); if (! hasType(raster->datadef().domain()->ilwisType(),itNUMERICDOMAIN | itCOLORDOMAIN)){ IDomain dom; QString code = raster->datadef().domain()->ilwisType() == itITEMDOMAIN ? "code=count" : "code=value"; if(!dom.prepare(code)) { //TODO: for the moment only value maps in gdal return ERROR1(ERR_NO_INITIALIZED_1,obj->name()); } currentDef.domain(dom); } Size<> sz = raster->size(); GDALDataType gdalType = ilwisType2GdalType(currentDef.range()->valueType()); QString filename = constructOutputName(_driver); bool isColorMap = currentDef.domain()->ilwisType() == itCOLORDOMAIN; bool ispaletteMap = currentDef.domain()->valueType() == itPALETTECOLOR; GDALDatasetH dataset = 0; if ( ispaletteMap && format() == "GTiff"){ char *options[] = {"PHOTOMETRIC=PALETTE", NULL}; dataset = gdal()->create( _driver, filename.toLocal8Bit(), sz.xsize(), sz.ysize(), sz.zsize(), gdalType, options ); }else dataset = gdal()->create( _driver, filename.toLocal8Bit(), sz.xsize(), sz.ysize(), isColorMap ? sz.zsize() * 3 : sz.zsize(), gdalType, 0 ); if ( dataset == 0) { return ERROR2(ERR_COULDNT_CREATE_OBJECT_FOR_2, "data set",_fileUrl.toLocalFile()); } bool ok = setGeotransform(raster, dataset); if (ok) ok = setSRS(raster, dataset); if (!ok) return false; if ( isColorMap ){ ok = storeColorRaster(raster, dataset) ; } else { switch(gdalType) { case GDT_Byte: ok = save<quint8>(raster, dataset,gdalType);break; case GDT_UInt16: ok = save<quint16>(raster, dataset,gdalType);break; case GDT_Int16: ok = save<qint16>(raster, dataset,gdalType);break; case GDT_Int32: ok = save<qint32>(raster, dataset,gdalType);break; case GDT_UInt32: ok = save<quint32>(raster, dataset,gdalType);break; case GDT_Float32: ok = save<float>(raster, dataset,gdalType);break; case GDT_Float64: ok = save<double>(raster, dataset,gdalType);break; default: ok= ERROR1(ERR_NO_INITIALIZED_1, "gdal Data type"); } } gdal()->close(dataset); return ok; }