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;
}
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;

}
Ejemplo n.º 3
0
bool Selection::execute(ExecutionContext *ctx, SymbolTable& symTable)
{
    if (_prepState == sNOTPREPARED)
        if((_prepState = prepare(ctx, symTable)) != sPREPARED)
            return false;
    IRasterCoverage outputRaster = _outputObj.as<RasterCoverage>();
    IRasterCoverage inputRaster = _inputObj.as<RasterCoverage>();

    quint32 rec = 0;
    quint32 colIndex = iUNDEF;

    std::unordered_map<quint32, quint32> coverageIndex;
    if ( _attribColumn != "") {
        ITable tbl = inputRaster->attributeTable();
        std::vector<QVariant> values = tbl->column(COVERAGEKEYCOLUMN);
        for(const QVariant& val : values) {
            coverageIndex[val.toInt()] = rec++;
        }
        colIndex  = tbl->columnIndex(_attribColumn);
    }


    BoxedAsyncFunc selection = [&](const BoundingBox& box ) -> bool {
        BoundingBox inpbox = box.size();
        inpbox += _base;
        inpbox += std::vector<qint32>{0, box.min_corner().y,0};
        if ( _zvalue == iUNDEF)
            inpbox.copyFrom(box, BoundingBox::dimZ);
        PixelIterator iterOut(outputRaster, box);
        PixelIterator iterIn(inputRaster, inpbox);

        double v_in = 0;
        std::for_each(iterOut, iterOut.end(), [&](double& v){
            v_in = *iterIn;
            if ( v_in != rUNDEF) {
                if ( _attribColumn != "") {
                    quint32 rec = coverageIndex[v_in];
                    QVariant var = inputRaster->attributeTable()->cell(colIndex, rec);
                    v = var.toDouble();
                    if ( isNumericalUndef(v))
                        v = rUNDEF;
                } else {
                    v = v_in;
                }
            }
            ++iterIn;
            ++iterOut;
        }
        );
        return true;
    };

    ctx->_threaded = false;
    bool resource = OperationHelperRaster::execute(ctx,selection, outputRaster, _box);

    if ( resource && ctx != 0) {
        QVariant value;
        value.setValue<IRasterCoverage>(outputRaster);
        ctx->setOutput(symTable, value, outputRaster->name(), itRASTER,outputRaster->source());
    }
    return resource;


}
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;
}