bool FeatureConnector::loadBinaryData(Ilwis::IlwisObject *obj) {
    if ( obj == nullptr)
        return false;

    FeatureCoverage *fcoverage = static_cast<FeatureCoverage *>(obj);

    QString file = _odf->value("BaseMap", "AttributeTable");
    ITable extTable;
    if ( file != sUNDEF) {
        if(!extTable.prepare(file)){
            kernel()->issues()->log(file,TR(ERR_NO_INITIALIZED_1).arg(file),IssueObject::itWarning);
            return false;
        }
    }
    bool ok = false;
     if (fcoverage->featureTypes() == itPOINT)
        ok = loadBinaryPoints(fcoverage);
    else if (fcoverage->featureTypes() == itLINE)
        ok = loadBinarySegments(fcoverage);
    else if (fcoverage->featureTypes() == itPOLYGON)
        ok = loadBinaryPolygons(fcoverage);
    if ( ok && extTable.isValid()) {
        ITable attTbl = fcoverage->attributeTable();
        quint32 keyIndex = attTbl->columnIndex(COVERAGEKEYCOLUMN);
        for(quint32 rowExt=0; rowExt < extTable->records(); ++rowExt) {
            vector<QVariant> rec = extTable->record(rowExt);
            for(quint32 rowAtt = 0; rowAtt < attTbl->records(); ++rowAtt ) {
                if ( attTbl->cell(keyIndex, rowAtt) == rowExt + 1) {
                    attTbl->record(rowAtt,rec);
                }
            }
        }
    }
    return ok;
}
bool GdalFeatureConnector::loadData(IlwisObject* data, const IOOptions &){

    if(!GdalConnector::loadMetaData(data, IOOptions()))
        return false;

    bool ok = true;
    FeatureCoverage *fcoverage = static_cast<FeatureCoverage *>(data);
    if ( fcoverage->isValid() ) {
        ITable attTable = fcoverage->attributeTable();
        if (!attTable.isValid()){
            ERROR2(ERR_NO_INITIALIZED_2,"attribute table",_filename.toString());
            return false;
        }
        fcoverage->setFeatureCount(itFEATURE, iUNDEF, FeatureInfo::ALLFEATURES); // metadata already set it to correct number, creating new features will up the count agains; so reset to 0.

        OGRLayerH hLayer = getLayerHandle();
        if ( hLayer) {
            GdalTableLoader loader;
            attTable->dataLoaded(true); // new table, dont want any loading behaviour
            loader.setColumnCallbacks(attTable.ptr(), hLayer);
            std::vector<QVariant> record(attTable->columnCount());
            OGRFeatureH hFeature = 0;
            gdal()->resetReading(hLayer);
            //each FEATURE
            try {
                while( (hFeature = gdal()->getNextFeature(hLayer)) != NULL){
                    loader.loadRecord(attTable.ptr(), hFeature, record);
                    geos::geom::Geometry * geometry = fillFeature(fcoverage, gdal()->getGeometryRef(hFeature));
                    if (geometry){
                        auto feature = fcoverage->newFeature(geometry, false);
                        feature->record(record);
                    }else{
                        ERROR1("GDAL error during load of binary data: no geometry detected for feature in %1", _filename.toString());
                    }
                    gdal()->destroyFeature( hFeature );
                }
            } catch (FeatureCreationError& ) {
                gdal()->destroyFeature( hFeature );
                ok = false;
            }
        }
        //layer envelopes/extents
        Envelope bbox;
        OGREnvelope envelope;//might sometimes be supported as 3D now only posssible from OGRGeometry
        OGRErr err = gdal()->getLayerExtent(hLayer, &envelope , TRUE);//TRUE to FORCE
        if (err != OGRERR_NONE && fcoverage->featureCount() != 0){
            ERROR0(QString("Couldn't load extent of a layer from %1 after binary was loaded: %2").arg(_filename.toString()).arg(gdal()->translateOGRERR(err)));
        }else{
            bbox = Envelope(Coordinate(envelope.MinX,envelope.MinY),Coordinate(envelope.MaxX,envelope.MaxY));
        }
        fcoverage->envelope(bbox);
    }
    gdal()->closeFile(sourceRef().toLocalFile(), data->id());
    _binaryIsLoaded = ok;
    return ok;
}
bool CoverageConnector::storeBinaryData(IlwisObject *obj, IlwisTypes tp)
{
    Coverage *coverage = static_cast<Coverage *>(obj);
    ITable attTable = coverage->attributeTable();
    if ( attTable.isValid()) {
        QScopedPointer<TableConnector> conn(createTableConnector(attTable, coverage, tp));
        return conn->storeBinaryData(attTable.ptr());

    }

    return false;
}
bool CoverageConnector::loadMetaData(Ilwis::IlwisObject *data)
{
    Ilwis3Connector::loadMetaData(data);

    Coverage *coverage = static_cast<Coverage *>(data);
    QString csyName = _odf->value("BaseMap","CoordSystem");
    if ( csyName.toLower() == "latlonwgs84.csy")
        csyName = "code=epsg:4326";
    ICoordinateSystem csy;
    if ( !csy.prepare(csyName)) {
        kernel()->issues()->log(csyName,TR("Coordinate system couldnt be initialized, defaulting to 'unknown'"),IssueObject::itWarning);
        QString resource = QString("ilwis://file/unknown.csy");
        if (!csy.prepare(resource)) {
            kernel()->issues()->log(TR("Fallback to 'unknown failed', corrupt system files defintion"));
            return false;
        }
    }
    coverage->setCoordinateSystem(csy);


    QString attfile = _odf->value("BaseMap", "AttributeTable");
    QString basemaptype = _odf->value("BaseMap", "Type");
    // feature coverages always have an attribute table; rasters might have
    if ( basemaptype != "Map" || attfile != sUNDEF) {
        ITable attTable = prepareAttributeTable(attfile, basemaptype);
        if (!attTable.isValid())
            return false;

        coverage->attributeTable(attTable);
    }

    QString cbounds = _odf->value("BaseMap","CoordBounds");
    QStringList parts = cbounds.split(" ");
    if ( parts.size() == 4) {
        double minx = parts[0].toDouble();
        double miny = parts[1].toDouble();
        double maxx = parts[2].toDouble();
        double maxy = parts[3].toDouble();
        Box2D<double> env(Coordinate(minx, miny), Coordinate(maxx, maxy));
        coverage->envelope(env);
    } else {
        kernel()->issues()->log(TR(ERR_INVALID_PROPERTY_FOR_2).arg("Coordinate boundaries", data->name()), IssueObject::itWarning);
    }


    return true;
}
ITable CoverageConnector::prepareAttributeTable(const QString& file, const QString& basemaptype) const{

    ITable extTable;
    if ( file != sUNDEF) {
        if(!extTable.prepare(file)){
            kernel()->issues()->log(file,TR(ERR_NO_INITIALIZED_1).arg(file),IssueObject::itWarning);
            return ITable();
        }
    }

    IDomain covdom;
    if (!covdom.prepare("count")){
        return ITable();
    }

    ITable attTable;
    if ( basemaptype != "Map" ) {
        Resource resource(QUrl(QString("ilwis://internal/%1").arg(_odf->fileinfo().baseName())), itFLATTABLE);
        if(!attTable.prepare(resource)) {
            ERROR1(ERR_NO_INITIALIZED_1,resource.name());
            return ITable();
        }
        if ( extTable.isValid()) {
            for(int i=0; i < extTable->columns(); ++i) {
                attTable->addColumn(extTable->columndefinition(i));
            }
        }
    } else {
        attTable = extTable;
    }
    if ( attTable->columnIndex(FEATUREIDCOLUMN) == iUNDEF) { // external tables might already have these
        attTable->addColumn(COVERAGEKEYCOLUMN,covdom);
        attTable->addColumn(FEATUREIDCOLUMN,covdom);
    }

    bool isNumeric = _odf->value("BaseMap","Range") != sUNDEF;
    if ( isNumeric) {
        IDomain featuredom;
        if (!featuredom.prepare("value")){
            return ITable();
        }
        attTable->addColumn(FEATUREVALUECOLUMN,featuredom);
    }
    return attTable;

}
bool FeatureConnector::loadData(Ilwis::IlwisObject *obj, const IOOptions &) {
    if ( obj == nullptr)
        return false;

    FeatureCoverage *fcoverage = static_cast<FeatureCoverage *>(obj);

    QString file = _odf->value("BaseMap", "AttributeTable");
    ITable extTable;
    if ( file != sUNDEF) {
        if(!extTable.prepare(file)){
            kernel()->issues()->log(file,TR(ERR_NO_INITIALIZED_1).arg(file),IssueObject::itWarning);
            return false;
        }
    }
    bool ok = false;

    try {
        _binaryIsLoaded = true; // to prevent any subsequent calls to this routine while loading (which mat trigger it).
         if (fcoverage->featureTypes() == itPOINT)
             ok = loadBinaryPoints(fcoverage);
         else if (fcoverage->featureTypes() == itLINE)
             ok = loadBinarySegments(fcoverage);
         else if (fcoverage->featureTypes() == itPOLYGON)
             ok = loadBinaryPolygons(fcoverage);

         _binaryIsLoaded = ok;

         if ( ok && extTable.isValid()) {
             ITable attTbl = fcoverage->attributeTable();
             quint32 nrAttrCols = std::min(attTbl->columnCount(),extTable->columnCount());
            // quint32 keyIndex = extTable->columnIndex(COVERAGEKEYCOLUMN);
             for(quint32 rowExt=0; rowExt < extTable->recordCount(); ++rowExt) {
                 if ( rowExt < fcoverage->featureCount()){
                     vector<QVariant> rec = extTable->record(rowExt);
                     rec.resize(nrAttrCols); // extTable received an extra "Domain" column, which is not there (and will not be there) in attTbl
                     attTbl->record(rowExt,rec);
                 }
             }
         }
    } catch (FeatureCreationError& ) {
    }
    if ( ok)
        _binaryIsLoaded = true;
    return ok;
}
Ejemplo n.º 7
0
void OperationWorker::process(){
    try {
        Operation op(_expression);
        SymbolTable tbl;
        ExecutionContext ctx;

        if(op->execute(&ctx, tbl)){
            if ( ctx._results.size() > 0){
                for(auto resultName : ctx._results){
                    Symbol symbol = tbl.getSymbol(resultName);
                    if ( hasType(symbol._type, itNUMBER)){
                        _result += symbol._var.toDouble();
                    }else if ( hasType(symbol._type, itSTRING)){
                        _result += symbol._var.toString();
                    }else if ( hasType(symbol._type, (itCOVERAGE | itTABLE))){
                        if ( symbol._type == itRASTER){
                            IRasterCoverage raster = symbol._var.value<IRasterCoverage>();
                            if ( raster.isValid())
                                _result = raster->resource().url().toString();
                        }else if(symbol._type == itTABLE){
                            ITable table = symbol._var.value<ITable>();
                            if(table.isValid())
                                _result = table->resource().url().toString();
                        }
                    }
                }
            }

            kernel()->issues()->log(QString(TR("Operation has executed succesfully")), IssueObject::itMessage);
        }else {
            qDebug() << "operation failed";
        }
        emit finished();
    }catch(const ErrorObject& err){

    }
    emit finished();
}
bool FeatureConnector::storeBinaryDataTable(IlwisObject *obj, IlwisTypes tp, const QString& baseName)
{
    FeatureCoverage *fcoverage = static_cast<FeatureCoverage *>(obj);
    ITable attTable = fcoverage->attributeTable();
    if ( attTable.isValid() && attTable->columnCount() > 0) {
        QFileInfo basename (baseName);
        QScopedPointer<TableConnector> conn(createTableStoreConnector(attTable, fcoverage, tp, basename.baseName()));
        IFeatureCoverage cov(fcoverage);
        FeatureIterator iter(cov);
        quint32 i = 0;
        std::vector<quint32> recordnr(fcoverage->featureCount(tp));
        for(quint32 rec=0; rec < fcoverage->featureCount(); ++rec){
            if ( hasType((*iter)->geometryType(), tp))
                recordnr[i++] = rec;
            ++iter;
        };
        conn->selectedRecords(recordnr);
        return conn->storeBinaryData(attTable.ptr());

    }

    return true; // no store needed
}
bool CoverageConnector::storeMetaData(IlwisObject *obj, IlwisTypes type, const DataDefinition& datadef)
{
    bool ok = Ilwis3Connector::storeMetaData(obj, type);
    if ( !ok)
        return false;

    Coverage *coverage = static_cast<Coverage *>(obj);

    const ICoordinateSystem csy = coverage->coordinateSystem();
    if (!csy.isValid())
        return ERROR2(ERR_NO_INITIALIZED_2, "CoordinateSystem", coverage->name());

    QString localName = Resource::toLocalFile(csy->source().url(),true);
    if ( localName == sUNDEF) {
        localName = CoordinateSystemConnector::createCsyFromCode(csy->code());
    }
    if ( localName == sUNDEF) {
        return ERROR2(ERR_NO_INITIALIZED_2, "CoordinateSystem", coverage->name());
    }
    _odf->setKeyValue("BaseMap","CoordSystem", localName);
    Box2D<double> bounds = coverage->envelope();
    if(!bounds.isValid())
        return ERROR2(ERR_NO_INITIALIZED_2, "Bounds", coverage->name());

    _odf->setKeyValue("BaseMap","CoordBounds",QString("%1 %2 %3 %4").
                      arg(bounds.min_corner().x(),10,'f').
                      arg(bounds.min_corner().y(),10,'f').
                      arg(bounds.max_corner().x(),10,'f').
                      arg(bounds.max_corner().y(),10,'f'));

    const IDomain dom = datadef.domain();
    if (!dom.isValid())
        return ERROR2(ERR_NO_INITIALIZED_2, "Domain", coverage->name());

    calcStatics(obj,NumericStatistics::pBASIC);
    if ( dom->ilwisType() == itNUMERICDOMAIN) {

        quint16 digits = coverage->statistics().significantDigits();
        qint32 delta = coverage->statistics()[NumericStatistics::pDELTA];
        if ( delta >= 0 && delta < 256 && digits == 0){
            if ( delta >= 0 && delta < 256 && digits == 0){
                if ( datadef.domain()->code() == "boolean"){
                    QString domInfo = QString("bool.dom;Byte;bool;0;;");
                    _odf->setKeyValue("BaseMap","DomainInfo",domInfo);
                    _odf->setKeyValue("BaseMap","Range","0:1:offset=-1");
                    _odf->setKeyValue("BaseMap","Domain","bool.dom");
                }
                else{
                    QString domInfo = QString("Image.dom;Byte;image;0;;");
                    _odf->setKeyValue("BaseMap","DomainInfo",domInfo);
                    _odf->setKeyValue("BaseMap","Range","0:255:offset=0");
                    _odf->setKeyValue("BaseMap","MinMax","0:255");
                    _odf->setKeyValue("BaseMap","Domain","Image.dom");
                }
            }
        }
        else {
            const NumericStatistics& stats = coverage->statistics();
            int digits = stats.significantDigits();
            RawConverter conv(stats[NumericStatistics::pMIN], stats[NumericStatistics::pMAX],pow(10, - digits));
            QString rangeString = QString("%1:%2:%3:offset=%4").arg(stats[NumericStatistics::pMIN]).arg(stats[NumericStatistics::pMAX]).arg(conv.scale()).arg(conv.offset());
            _odf->setKeyValue("BaseMap","Range",rangeString);
            _odf->setKeyValue("BaseMap","Domain","value.dom");

            _odf->setKeyValue("BaseMap","MinMax",QString("%1:%2").arg(stats[NumericStatistics::pMIN]).arg(stats[NumericStatistics::pMAX]));
            QString domInfo = QString("value.dom;Long;value;0;-9999999.9:9999999.9:0.1:offset=0");
            _odf->setKeyValue("BaseMap","DomainInfo",domInfo);
        }
    } if ( dom->ilwisType() == itITEMDOMAIN) {
        QString source = Resource::toLocalFile(dom->source().url(), true);
        if ( dom->valueType() == itTHEMATICITEM && coverage->ilwisType() == itRASTER) {
            IThematicDomain themdom = dom.get<ThematicDomain>();
            if ( themdom.isValid()) {
                QString domInfo = QString("%1;Byte;class;%2;;").arg(source).arg(themdom->count());
                _odf->setKeyValue("BaseMap","DomainInfo",domInfo);
                _odf->setKeyValue("BaseMap","Domain",source);
            }
        } else if(dom->valueType() == itINDEXEDITEM) {
            QString domName = _odf->fileinfo().fileName();
            QString domInfo = QString("%1;Long;UniqueID;0;;").arg(domName);
            _odf->setKeyValue("BaseMap","DomainInfo",domInfo);
            _odf->setKeyValue("BaseMap","Domain",domName);
        } else if ( dom->valueType() == itNAMEDITEM) {
            INamedIdDomain iddom = dom.get<NamedIdDomain>();
            QString domName = _odf->fileinfo().fileName();
            int index;
            if ( (index=domName.lastIndexOf("."))!= -1)             {
                domName = domName.left(index);
            }
            QString domInfo = QString("%1;;Int;id;%2;;").arg(domName).arg(iddom->count());
            _odf->setKeyValue("BaseMap","DomainInfo",domInfo);
            _odf->setKeyValue("BaseMap","Domain",domName);
            iddom->connectTo(QUrl(),"domain","ilwis3", IlwisObject::cmOUTPUT);
            iddom->store(Ilwis::IlwisObject::smMETADATA | Ilwis::IlwisObject::smBINARYDATA);
        }
    }

    ITable attTable = coverage->attributeTable();
    if ( attTable.isValid()) {
        QScopedPointer<TableConnector> conn(createTableConnector(attTable, coverage, type));
        conn->storeMetaData(attTable.ptr());
    }
    return true;
}
bool FeatureConnector::storeMetaData(FeatureCoverage *fcov, IlwisTypes type) {
    if ( type == itUNKNOWN)
        return true;//if type is itUNKNOWN we dont store
    DataDefinition datadef;

    ITable attTable = fcov->attributeTable();
    QString primkey = attTable->primaryKey();
    if (primkey == sUNDEF)
        primkey = COVERAGEKEYCOLUMN;
    int index = attTable->columnIndex(primkey);
    if ( index != iUNDEF ) {
        const ColumnDefinition& coldef = attTable->columndefinitionRef(index);
        if ( coldef.datadef().domain<>()->ilwisType() == itITEMDOMAIN)
            datadef = DataDefinition(coldef.datadef().domain(),coldef.datadef().range()->clone());
    }
    if ( !datadef.isValid()) {
        INamedIdDomain indexdom;
        indexdom.prepare();
        indexdom->name(fcov->name());
        NamedIdentifierRange range;
        for(quint32 i=0; i < fcov->featureCount(type); ++i){
            QStringList parts = Ilwis3Connector::ilwis3ClassName(type).split(" ");
            QString itemname = QString("%1_%2").arg(parts[0]).arg(i);
            range << itemname;
        }
        indexdom->setRange(range);
        datadef.domain(indexdom);
        QFileInfo inf ( _resource.url(true).toLocalFile());
        QString filename = context()->workingCatalog()->filesystemLocation().toLocalFile() + "/" + inf.baseName() + ".dom";
        indexdom->connectTo(filename,"domain","ilwis3", Ilwis::IlwisObject::cmOUTPUT);
        indexdom->store();
    }
    bool isMulti = (fcov->featureTypes() & (fcov->featureTypes() - 1)) != 0;
    QString baseName = Ilwis3Connector::outputNameFor(fcov, isMulti, type);
    index = baseName.lastIndexOf(".");
    if ( index != -1) {
        baseName = baseName.left(index);
    }

    bool ok = CoverageConnector::storeMetaData(fcov, type, datadef, baseName);
    if ( !ok)
        return false;

    if ( datadef.domain()->valueType() == itINDEXEDITEM) {
        _odf->setKeyValue("Domain","Type","DomainUniqueID");
        _odf->setKeyValue("DomainSort","Sorting","AlphaNumeric");
        _odf->setKeyValue("DomainSort","Prefix","feature");
        _odf->setKeyValue("DomainSort","Class","Domain UniqueID");
        _odf->setKeyValue("DomainIdentifier","Nr",IniFile::FormatElement(fcov->featureCount(type)));
    }

    Envelope bounds = fcov->envelope();
    if ( bounds.isNull() || !bounds.isValid())
        bounds = fcov->coordinateSystem()->envelope();

    _odf->setKeyValue("BaseMap","CoordBounds",QString("%1 %2 %3 %4").
                      arg(bounds.min_corner().x,0,'f',10).
                      arg(bounds.max_corner().y,0,'f',10).
                      arg(bounds.max_corner().x,0,'f',10).
                      arg(bounds.min_corner().y,0,'f',10));

    QString ext = "mpa";
    if ( hasType(type, itPOLYGON)){
        ok = storeMetaPolygon(fcov, baseName);
    }
    if ( hasType(type, itLINE)){
        ok = storeMetaLine(fcov, baseName);
        ext = "mps";
    }
    if ( hasType(type, itPOINT)){
        ok = storeMetaPoint(fcov, baseName);
        ext = "mpp";
    }
    if ( attTable.isValid() && attTable->columnCount() > 0) {
        QFileInfo basename (baseName);
        QScopedPointer<TableConnector> conn(createTableStoreConnector(attTable, fcov, type, basename.baseName()));
        std::vector<quint32> recs(_itemCount);
        conn->selectedRecords(recs);
        conn->storeMetaData(attTable.ptr());
    }

    _odf->store(ext, QFileInfo(baseName));
    return ok;
}