void IlwisObjectModel::resetAttributeModel(const QString& attributeName){ auto setAttributeModel = [&](int i, const ColumnDefinition& coldef, const QString& attributeName){ if ( coldef.name() == attributeName){ AttributeModel *attribute = new AttributeModel(coldef, this, _ilwisobject); _attributes[i] = attribute; } }; IlwisTypes objecttype = _ilwisobject->ilwisType(); if ( objecttype == itRASTER){ IRasterCoverage raster = _ilwisobject.as<RasterCoverage>(); if ( raster->hasAttributes()){ for(int i = 0; i < raster->attributeTable()->columnCount(); ++i){ setAttributeModel(i,raster->attributeTable()->columndefinition(i), attributeName); } } } else if ( hasType(objecttype,itFEATURE)){ IFeatureCoverage features = _ilwisobject.as<FeatureCoverage>(); for(int i = 0; i < features->attributeDefinitions().definitionCount(); ++i){ setAttributeModel(i,features->attributeTable()->columndefinition(i), attributeName); } } else if ( hasType(objecttype,itTABLE)){ ITable tbl = _ilwisobject.as<Table>(); for(int i = 0; i < tbl->columnCount(); ++i){ setAttributeModel(i,tbl->columndefinition(i),attributeName); } } }
BoundingBox OperationHelperRaster::initialize(const IRasterCoverage &inputRaster, IRasterCoverage &outputRaster, quint64 what) { Resource resource(itRASTER); Size<> sz = inputRaster->size(); BoundingBox box(sz); if ( what & itRASTERSIZE) { resource.addProperty("size", IVARIANT(sz.toString())); } if ( what & itENVELOPE) { if ( box.isNull() || !box.isValid()) { sz = inputRaster->size(); box = BoundingBox(sz); } Envelope bounds = inputRaster->georeference()->pixel2Coord(box); resource.addProperty("envelope", IVARIANT(bounds.toString())); } if ( what & itCOORDSYSTEM) { resource.addProperty("coordinatesystem", IVARIANT(inputRaster->coordinateSystem()->id())); } if ( what & itGEOREF) { if ( box.isNull() || !box.isValid()) { sz = inputRaster->size(); box = BoundingBox(sz); } if ( sz.xsize() == box.xlength() && sz.ysize() == box.ylength()) resource.addProperty("georeference", IVARIANT(inputRaster->georeference()->id())); } if ( what & itDOMAIN) { resource.addProperty("domain", IVARIANT(inputRaster->datadef().domain<>()->id())); } resource.prepare(); outputRaster.prepare(resource); if ( what & itTABLE) { if ( inputRaster->attributeTable().isValid()) { if ( inputRaster->datadef().domain<>() == outputRaster->datadef().domain<>()) { if ( outputRaster.isValid()) outputRaster->setAttributes(inputRaster->attributeTable()); } } } if ( what & itDOMAIN){ for(quint32 i = 0; i < outputRaster->size().zsize(); ++i){ QString index = outputRaster->stackDefinition().index(i); outputRaster->setBandDefinition(index,DataDefinition(outputRaster->datadef().domain())); } } return box; }
void RasterLayerDrawer::setActiveVisualAttribute(const QString &attr) { IRasterCoverage raster = coverage().as<RasterCoverage>(); if ( attr == PIXELVALUE){ LayerDrawer::setActiveVisualAttribute(attr); } else if ( raster.isValid() ) { if(raster->hasAttributes()) { if ( raster->attributeTable()->columnIndex(attr) != iUNDEF){ IRepresentation newrpr = Representation::defaultRepresentation(raster->attributeTable()->columndefinition(attr).datadef().domain()); if ( newrpr.isValid()){ LayerDrawer::setActiveVisualAttribute(attr); } } } } }
QQmlListProperty<AttributeModel> IlwisObjectModel::attributes() { try { if ( _attributes.size() == 0){ if ( _ilwisobject.isValid()) { IlwisTypes objecttype = _ilwisobject->ilwisType(); if ( objecttype == itRASTER){ IRasterCoverage raster = _ilwisobject.as<RasterCoverage>(); if ( raster->hasAttributes()){ for(int i = 0; i < raster->attributeTable()->columnCount(); ++i){ AttributeModel *attribute = new AttributeModel(raster->attributeTable()->columndefinition(i), this, _ilwisobject); _attributes.push_back(attribute); } }else { AttributeModel *attribute = new AttributeModel(ColumnDefinition(PIXELVALUE, raster->datadef(),i64UNDEF), this, _ilwisobject); _attributes.push_back(attribute); } } else if ( hasType(objecttype,itFEATURE)){ IFeatureCoverage features = _ilwisobject.as<FeatureCoverage>(); for(int i = 0; i < features->attributeDefinitions().definitionCount(); ++i){ AttributeModel *attribute = new AttributeModel(features->attributeDefinitions().columndefinition(i), this, _ilwisobject); _attributes.push_back(attribute); } } else if ( hasType(objecttype,itTABLE)){ ITable tbl = _ilwisobject.as<Table>(); for(int i = 0; i < tbl->columnCount(); ++i){ AttributeModel *attribute = new AttributeModel(tbl->columndefinition(i), this, _ilwisobject); _attributes.push_back(attribute); } } } } if ( _attributes.size() > 0){ return QQmlListProperty<AttributeModel>(this, _attributes) ; } } catch(const ErrorObject& ){ // no exceptions may escape here } return QQmlListProperty<AttributeModel>(); }
QString IlwisObjectModel::value2string(const QVariant &value, const QString &attrName) { auto v2s = [](const ColumnDefinition& coldef, const QVariant& value)->QString { if ( coldef.isValid()){ if ( coldef.datadef().domain()->ilwisType() == itTEXTDOMAIN) return value.toString(); return coldef.datadef().domain()->impliedValue(value).toString(); } if ( value.toDouble() == rUNDEF) return sUNDEF; return value.toString(); }; if ( attrName != "") { IlwisTypes objectype = _ilwisobject->ilwisType(); if ( hasType(objectype, itFEATURE)){ IFeatureCoverage features = _ilwisobject.as<FeatureCoverage>(); ColumnDefinition coldef = features->attributeDefinitions().columndefinition(attrName); return v2s(coldef, value); }else if (hasType(objectype, itRASTER)){ IRasterCoverage raster = _ilwisobject.as<RasterCoverage>(); if ( raster->hasAttributes()){ ColumnDefinition coldef = raster->attributeTable()->columndefinition(attrName); return v2s(coldef, value); } if ( raster->datadef().domain()->ilwisType() == itCOLORDOMAIN){ auto clr = ColorRangeBase::toColor(value, ColorRangeBase::cmRGBA); return ColorRangeBase::toString(clr,ColorRangeBase::cmRGBA) ; } } } if ( value.toDouble() == rUNDEF) return sUNDEF; return value.toString(); }
Ilwis::OperationImplementation::State SelectionRaster::prepare(ExecutionContext *ctx, const SymbolTable &st) { OperationImplementation::prepare(ctx,st); if ( _expression.parameterCount() != 2) { ERROR3(ERR_ILLEGAL_NUM_PARM3,"rasvalue","1",QString::number(_expression.parameterCount())); return sPREPAREFAILED; } IlwisTypes inputType = itRASTER; QString raster = _expression.parm(0).value(); if (!_inputObj.prepare(raster, inputType)) { ERROR2(ERR_COULD_NOT_LOAD_2,raster,""); return sPREPAREFAILED; } IRasterCoverage inputRaster = _inputObj.as<RasterCoverage>(); _inputAttributeTable = inputRaster->attributeTable(); quint64 copylist = itCOORDSYSTEM ; QString selector = _expression.parm(1).value(); parseSelector(selector, inputRaster); std::vector<QString> selectionBands = bands(inputRaster); _box = boundingBox(_inputObj.as<RasterCoverage>()); bool useOldGrf ; if ( _box.isNull() || !_box.isValid()){ _box = inputRaster->size(); useOldGrf = true; } else useOldGrf = false; if ( useOldGrf){ copylist |= itGEOREF | itRASTERSIZE | itENVELOPE; } int selectedAttributes = attributeNames().size(); if (selectedAttributes != 1) copylist |= itDOMAIN; _outputObj = OperationHelperRaster::initialize(_inputObj,inputType, copylist); if ( !_outputObj.isValid()) { ERROR1(ERR_NO_INITIALIZED_1, "output coverage"); return sPREPAREFAILED; } IRasterCoverage outputRaster = _outputObj.as<RasterCoverage>(); QString outputName = _expression.parm(0,false).value(); if ( outputName != sUNDEF) _outputObj->name(outputName); if ( selectedAttributes > 1) { QString url = INTERNAL_CATALOG + "/" + outputName; Resource resource(url, itFLATTABLE); _attTable.prepare(resource); } if ( selectedAttributes == 1 && _inputAttributeTable.isValid()){ QStringList names = attributeNames(); //outputRaster->datadefRef().domain(_inputAttributeTable->columndefinition(names[0]).datadef().domain()); outputRaster->setDataDefintions(_inputAttributeTable->columndefinition(names[0]).datadef().domain(), selectionBands, inputRaster->stackDefinition().domain()); }else outputRaster->setDataDefintions(inputRaster->datadef().domain(), selectionBands, inputRaster->stackDefinition().domain()); if ( (copylist & itGEOREF) == 0) { Resource resource(QUrl(INTERNAL_CATALOG + "/" + outputRaster->name() + "_grf_" + QString::number(outputRaster->id())),itGEOREF); resource.addProperty("size", IVARIANT(_box.size())); auto envelope = inputRaster->georeference()->pixel2Coord(_box); resource.addProperty("envelope", IVARIANT(envelope)); resource.addProperty("coordinatesystem", IVARIANT(inputRaster->coordinateSystem())); resource.addProperty("name", _outputObj->name()); resource.addProperty("centerofpixel",inputRaster->georeference()->centerOfPixel()); IGeoReference grf; grf.prepare(resource); outputRaster->georeference(grf); outputRaster->envelope(envelope); } return sPREPARED; }
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; }
Ilwis::OperationImplementation::State Selection::prepare(ExecutionContext *, const SymbolTable &) { if ( _expression.parameterCount() != 2) { ERROR3(ERR_ILLEGAL_NUM_PARM3,"rasvalue","1",QString::number(_expression.parameterCount())); return sPREPAREFAILED; } IlwisTypes inputType = itRASTER; QString raster = _expression.parm(0).value(); if (!_inputObj.prepare(raster, inputType)) { ERROR2(ERR_COULD_NOT_LOAD_2,raster,""); return sPREPAREFAILED; } IRasterCoverage inputRaster = _inputObj.as<RasterCoverage>(); quint64 copylist = itCOORDSYSTEM; QString selector = _expression.parm(1).value(); selector = selector.remove('"'); int index = selector.indexOf("box="); Envelope box; if ( index != -1) { QString crdlist = "box(" + selector.mid(index+4) + ")"; _box = BoundingBox(crdlist); box = inputRaster->georeference()->pixel2Coord(_box); copylist |= itDOMAIN | itTABLE; std::vector<qint32> vec{_box.min_corner().x, _box.min_corner().y,_box.min_corner().z}; _base = vec; } index = selector.indexOf("polygon="); if ( index != -1) { //TODO: copylist |= itDOMAIN | itTABLE; } index = selector.indexOf("attribute="); if ( index != -1 ) { if (! inputRaster->attributeTable().isValid()) { ERROR2(ERR_NO_FOUND2,"attribute-table", "coverage"); return sPREPAREFAILED; } _attribColumn = selector.mid(index+10); copylist |= itRASTERSIZE | itGEOREF | itENVELOPE; } int indexindex = selector.indexOf("index="); if ( indexindex != -1) { copylist |= itDOMAIN | itGEOREF | itENVELOPE | itTABLE; _box = BoundingBox(inputRaster->size()); QString zvalues = selector.mid(6); bool ok; _zvalue = zvalues.toInt(&ok); if ( !ok || _zvalue < 0) { ERROR3(ERR_ILLEGAL_PARM_3, TR("layer index"), zvalues,"Selection"); return sPREPAREFAILED; } _box.min_corner().z = _zvalue; _box.max_corner().z = _zvalue; std::vector<qint32> vec{_box.min_corner().x, _box.min_corner().y,_box.min_corner().z}; _base = vec; } _outputObj = OperationHelperRaster::initialize(_inputObj,inputType, copylist); if ( !_outputObj.isValid()) { ERROR1(ERR_NO_INITIALIZED_1, "output coverage"); return sPREPAREFAILED; } IRasterCoverage outputRaster = _outputObj.as<RasterCoverage>(); if ( (copylist & itDOMAIN) == 0) { outputRaster->datadefRef() = _attribColumn != "" ? inputRaster->attributeTable()->columndefinition(_attribColumn).datadef() : outputRaster->datadefRef() = inputRaster->datadef(); } QString outputName = _expression.parm(0,false).value(); if ( outputName != sUNDEF) _outputObj->name(outputName); if ( (copylist & itGEOREF) == 0) { Resource resource(QUrl("ilwis://internalcatalog/georeference"),itGEOREF); resource.addProperty("size", IVARIANT(_box.size())); resource.addProperty("envelope", IVARIANT(box)); resource.addProperty("coordinatesystem", IVARIANT(inputRaster->coordinateSystem())); resource.addProperty("name", _outputObj->name()); resource.addProperty("centerofpixel",inputRaster->georeference()->centerOfPixel()); IGeoReference grf; grf.prepare(resource); outputRaster->georeference(grf); outputRaster->envelope(box); } if(indexindex != -1) { Size<> sz(outputRaster->size().xsize(),outputRaster->size().xsize(), 1); outputRaster->size(sz); } return sPREPARED; }