QString Ilwis3Connector::writeCsy(IlwisObject *obj, const ICoordinateSystem & csy) const
{
    QString csyName;
    if ( csy->code() != "unknown"){
        if ( csy->code() != "epsg:4326"){
            csyName = Resource::toLocalFile(csy->resource().url(true),true, "csy");
            if ( csy->isInternalObject()){
                QString csyFile = Resource::toLocalFile(sourceRef().url(),false, "csy");
                QString name = csy->name().trimmed();
                if (name.size() > 3 && !csy->isAnonymous()) { // threshold of min 4 chars for a credible csy name
                    name = name.replace(QRegExp("[/ .'\"]"),"_");
                    csyName =  QFileInfo(csyFile).absolutePath() + "/" + name + ".csy";
                } else
                    csyName = csyFile;
            }
            else if ( csyName == sUNDEF || csyName == "") {
                QString path = context()->workingCatalog()->filesystemLocation().toLocalFile() + "/";
                QString name = csy->name();
                if ( !csy->isAnonymous()) {
                    name = name.replace(QRegExp("[/ .'\"]"),"_");
                }
                csyName = path + name;
                if ( !csyName.endsWith(".csy"))
                    csyName += ".csy";
                //return ERROR2(ERR_NO_INITIALIZED_2, "CoordinateSystem", coverage->name());
            }
            int index = _odf->url().lastIndexOf("/");
            QString csyurl = _odf->url().left(index) + "/" + csyName;
            QFileInfo csyinf(QUrl(csyurl).toLocalFile());
            if ( !csyinf.exists()) { // if filepath doesnt exist we create if from scratch

                bool mustWriteCsyFile = csy.isValid();

                if (mustWriteCsyFile) {
                    QUrl url = csyurl; // new attempt to create a suitable path;
                    csy->connectTo(url,"coordsystem","ilwis3", IlwisObject::cmOUTPUT);
                    if(!csy->store({"storemode",Ilwis::IlwisObject::smMETADATA})){ // fail, we default to unknown
                        csyName = "Unknown.csy";
                        WARN2(ERR_NO_INITIALIZED_2,"CoordinateSystem",obj->name());
                    } else {
                        csyName = url.toLocalFile();
                    }
                }else
                    csyName = "Unknown.csy";
            }
        }else
            csyName = "LatLonWGS84.csy";
    }else
        csyName = "unknown.csy";
    return QFileInfo(csyName).fileName();
}