IlwisObject *InternalIlwisObjectFactory::createCsy(const Resource& resource, const IOOptions &options) const {
    CoordinateSystem *csy = 0;
    if ( resource.ilwisType() == itBOUNDSONLYCSY){ // by default to csy unknow but this may be changed later
        csy = createFromResource<BoundsOnlyCoordinateSystem>(resource, options);
        csy->name("unknown");
        csy->code("unknown");
        csy->setDescription(TR("Unknown coordinate system"));
    }else if ( resource.ilwisType() == itCONVENTIONALCOORDSYSTEM){
        csy = createFromResource<ConventionalCoordinateSystem>(resource, options);
        csy->name(resource.name());
        csy->code(resource.code());
    }
    return csy;
}
IlwisObject *InternalIlwisObjectFactory::createCsyFromCode(const Resource& resource, const IOOptions &options) const {
    QString code = resource.code();
    bool isUnknown = code == "unknown" || code == "csy:unknown";
    QString projParms = code;
    if ( code.left(6) == "proj4:"){
        projParms = code.mid(6);
    }else if(!isUnknown && code.left(5) == "epsg:"){
        QString query = QString("select * from projectedcsy where code='%1'").arg(code);
        InternalDatabaseConnection db;
        if ( db.exec(query)) {
            if (db.next()) {
                QSqlRecord rec = db.record();
                projParms = rec.value("proj_params").toString();
            } else {
                kernel()->issues()->log(TR(ERR_COULDNT_CREATE_OBJECT_FOR_2).arg("coordinatesystem", resource.name()));
                return 0;
            }
        }
    }
    CoordinateSystem *csy = 0;
    if ( isUnknown){
        csy = createFromResource<BoundsOnlyCoordinateSystem>(resource, options);
        csy->name("unknown");
        csy->code("unknown");
        csy->setDescription(TR("Unknown coordinate system"));
    }else {
        csy = createFromResource<ConventionalCoordinateSystem>(resource, options);
        csy->setDescription(resource.name());
        csy->prepare("proj4=" + projParms);
    }

    return csy;

}
bool CoordinateSystemConnector::loadMetaData(IlwisObject* data, const IOOptions& options)
{
    Ilwis3Connector::loadMetaData(data, options);
    CoordinateSystem *csy = static_cast<CoordinateSystem *>(data);
    QString ellipsoideName;

    IEllipsoid ell = getEllipsoid();
    GeodeticDatum *datum = getDatum(ellipsoideName);
    if ( !ell.isValid() && ellipsoideName != sUNDEF){
        QString ellres = QString("ilwis://tables/ellipsoid?code=%1").arg(ellipsoideName);
       if (!ell.prepare(ellres)) {
              return ERROR1("No ellipsoid for this code %1",ellipsoideName);
       }
    }

    QString cb = _odf->value("CoordSystem", "CoordBounds");
    QStringList cbparts = cb.split(" ");
    if ( cbparts.size() == 4 && cbparts[0] != "-1e+308") {
        bool ok1, ok2, ok3, ok4;
        Envelope box( Coordinate(
                                cbparts[0].toDouble(&ok1),
                                cbparts[1].toDouble(&ok2)),
                           Coordinate(
                                cbparts[2].toDouble(&ok3),
                                cbparts[3].toDouble(&ok4)));
        if ( !( ok1 && ok2 && ok3 && ok4)) {
            return ERROR2(ERR_NO_INITIALIZED_2, TR("envelop"), csy->name());
        }
        csy->envelope(box);
    } else {
        QString type = _odf->value("CoordSystem", "Type");
        if ( type == "LatLon") {
            Envelope box(Coordinate(-180,-90), Coordinate(180,90));
            csy->envelope(box);
        }
    }

    if ( type() == itCONVENTIONALCOORDSYSTEM ) {
        ConventionalCoordinateSystem *csycc = static_cast<ConventionalCoordinateSystem *>(csy);
        IProjection proj = getProjection(csycc);
        if ( !proj.isValid()) {
            return ERROR1(ERR_NO_INITIALIZED_1, "projection");
        }
        csycc->setDatum(datum);
        csycc->setEllipsoid(ell);
        csycc->setProjection(proj);
        proj->setCoordinateSystem(csycc);


        proj->setParameter(Projection::pvELLCODE, ell->toProj4());
        csycc->prepare();
    } else if ( type() == itUNKNOWN){
        //TODO: other types of csy
    }
    return true;
}