bool InternalIlwisObjectFactory::createCoverage(const Resource& resource, Coverage *coverage, const IOOptions &options) const {

    if (!coverage->prepare())
        return false;

    //coverage->setName(QString("%1%2").arg(ANONYMOUS_PREFIX).arg(coverage->id()));

    ICoordinateSystem csy;
    QString typnm = resource["coordinatesystem"].typeName();
    if (typnm == "Ilwis::ICoordinateSystem")
        csy = resource["coordinatesystem"].value<Ilwis::ICoordinateSystem>();
    else if( typnm == "QString" &&
             resource["coordinatesystem"].toString() != sUNDEF  ) {
        Resource newresource = resource.property2Resource("coordinatesystem", itCOORDSYSTEM);
        if ( newresource.isValid()) {
            if (!csy.prepare(newresource,options))
                return false;
        }
    } else if ( typnm == "qulonglong"){
        if(!csy.prepare(resource["coordinatesystem"].value<quint64>()))
            return 0;
    }
    if ( csy.isValid()){
        coverage->coordinateSystem(csy);
    }

    Envelope bounds;
    QString envType = resource["envelope"].typeName();
    if ( envType == "Ilwis::Box<double>" || envType == "Ilwis::Envelope") {
        bounds = resource["envelope"].value<Envelope>();
    }else if (QString(resource["envelope"].typeName()) == "QString" &&
              resource["envelope"].toString() != sUNDEF) {
        bounds = Envelope(resource["envelope"].toString());
    }
    if ( bounds.isValid()) {
        coverage->envelope(bounds);
    }
    if ( resource.ilwisType() == itRASTER) {
        IDomain dom;
        QString tpname = resource["domain"].typeName();
        if (tpname == "Ilwis::IDomain")
            dom = resource["domain"].value<Ilwis::IDomain>();
        else if( tpname == "QString" &&
                 resource["domain"].toString() != sUNDEF  ) {
            Resource newresource = resource.property2Resource("domain", itDOMAIN);
            if ( newresource.isValid()) {
                if (!dom.prepare(newresource, options))
                    return false;
            }
        } else if ( tpname == "qulonglong"){
            if(!dom.prepare(resource["domain"].value<quint64>()))
                return 0;
        }

        if ( dom.isValid()){
            RasterCoverage *raster = static_cast<RasterCoverage *>(coverage);
            raster->datadefRef().domain(dom);
        }
    }
    return true;
}
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;
}