IRasterCoverage operator*(const IRasterCoverage &raster1, const IRasterCoverage &raster2) { QString name = ANONYMOUS_PREFIX; QString stmt = QString("script %1=%2 * %3").arg(name).arg(raster1->name()).arg(raster2->name()); return doRasterOperation(stmt); }
IRasterCoverage operator*(const IRasterCoverage &raster1, const IRasterCoverage &raster2) { QString name = Identity::newAnonymousName();; QString stmt = QString("script %1=%2 * %3").arg(name).arg(raster1->name()).arg(raster2->name()); return doRasterOperation(stmt); }
IRasterCoverage operator<(const IRasterCoverage &raster1, double v) { QString name = Identity::newAnonymousName(); QString stmt = QString("script %1=%2 < %3").arg(name).arg(raster1->name()).arg(v); return doRasterOperation(stmt); }
bool RasterLayerDrawer::prepare(DrawerInterface::PreparationType prepType, const IOOptions &options) { if(!LayerDrawer::prepare(prepType, options)) return false; IRasterCoverage raster = coverage().as<RasterCoverage>(); if ( !_rasterImage){ setActiveVisualAttribute(PIXELVALUE); _visualAttribute = visualProperty(activeAttribute()); _rasterImage.reset(RasterImageFactory::create(raster->datadef().domain()->ilwisType(), rootDrawer(),raster,_visualAttribute,IOOptions())); if (!_rasterImage){ ERROR2(ERR_NO_INITIALIZED_2,"RasterImage",raster->name()); return false; } } if ( hasType(prepType, ptSHADERS) && !isPrepared(ptSHADERS)){ _texcoordid = _shaders.attributeLocation("texCoord"); _textureid = _shaders.uniformLocation( "tex" ); _prepared |= DrawerInterface::ptSHADERS; } if ( hasType(prepType, DrawerInterface::ptGEOMETRY) && !isPrepared(DrawerInterface::ptGEOMETRY)){ Envelope env = rootDrawer()->coordinateSystem()->convertEnvelope(raster->coordinateSystem(), raster->envelope()); _vertices.resize(6); _vertices[0] = QVector3D(env.min_corner().x, env.min_corner().y, 0); _vertices[1] = QVector3D(env.max_corner().x, env.min_corner().y, 0); _vertices[2] = QVector3D(env.min_corner().x, env.max_corner().y, 0); _vertices[3] = QVector3D(env.max_corner().x, env.min_corner().y, 0); _vertices[4] = QVector3D(env.max_corner().x, env.max_corner().y, 0); _vertices[5] = QVector3D(env.min_corner().x, env.max_corner().y, 0); _texcoords.resize(6); _texcoords = {{0,1},{1,1},{0,0}, {1,1},{1,0},{0,0} }; _prepared |= ( DrawerInterface::ptGEOMETRY); } if ( hasType(prepType, DrawerInterface::ptRENDER) && !isPrepared(DrawerInterface::ptRENDER)){ setActiveVisualAttribute(PIXELVALUE); _visualAttribute = visualProperty(activeAttribute()); _rasterImage->visualAttribute(_visualAttribute); _prepared |= ( DrawerInterface::ptRENDER); } if (_rasterImage->prepare((int)prepType)){ _texture.reset( new QOpenGLTexture(*(_rasterImage->image().get()))); _texture->setMinMagFilters(QOpenGLTexture::Nearest,QOpenGLTexture::Nearest); } return true; }
bool AggregateRaster::execute(ExecutionContext *ctx, SymbolTable& symTable) { if (_prepState == sNOTPREPARED) if((_prepState = prepare(ctx,symTable)) != sPREPARED) return false; IRasterCoverage outputRaster = _outputObj.as<RasterCoverage>(); quint64 currentCount = 0; BoxedAsyncFunc aggregateFun = [&](const BoundingBox& box) -> bool { //Size sz = outputRaster->size(); PixelIterator iterOut(outputRaster, box); BoundingBox inpBox(Pixel(box.min_corner().x, box.min_corner().y * groupSize(1), box.min_corner().z * groupSize(2)), Pixel((box.max_corner().x+1) * groupSize(0) - 1, (box.max_corner().y + 1) * groupSize(1) - 1, (box.max_corner().z + 1) * groupSize(2) - 1) ); BlockIterator blockIter(_inputObj.as<RasterCoverage>(),Size<>(groupSize(0),groupSize(1), groupSize(2)), inpBox); NumericStatistics stats; PixelIterator iterEnd = iterOut.end(); while(iterOut != iterEnd) { GridBlock& block = *blockIter; stats.calculate(block.begin(), block.end(), _method); double v = stats[_method]; *iterOut = v; ++iterOut; ++blockIter; updateTranquilizer(currentCount++, 1000); } return true; }; bool res = OperationHelperRaster::execute(ctx, aggregateFun, outputRaster); if ( res && ctx != 0) { QVariant value; value.setValue<IRasterCoverage>(outputRaster); ctx->setOutput(symTable,value,outputRaster->name(), itRASTER, outputRaster->source() ); } return res; }
bool SelectionRaster::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>(); std::map<Raw, quint32> raw2record; int keyColumn = _inputAttributeTable.isValid() ? _inputAttributeTable->columnIndex(inputRaster->primaryKey()) : iUNDEF; if (keyColumn != iUNDEF){ std::vector<QVariant> values = _inputAttributeTable->column(keyColumn); for(quint32 rec=0; rec < values.size(); ++rec){ Raw r = values[rec].toDouble(); if ( !isNumericalUndef(r)){ raw2record[r] = rec; } } } std::vector<int> extraAtrrib = organizeAttributes(); std::vector<QString> selectionBands = bands(inputRaster); initialize(outputRaster->size().linearSize() * selectionBands.size()); PixelIterator iterOut(outputRaster); int count = 0; bool numeric = outputRaster->datadef().domain()->ilwisType() == itNUMERICDOMAIN; for(QString band : selectionBands){ PixelIterator iterIn = inputRaster->band(band, _box); PixelIterator iterEnd = iterIn.end(); while(iterIn != iterEnd) { bool ok = true; double pixValue = *iterIn; double matchValue = pixValue; for(const auto& epart : _expressionparts){ bool partOk = epart.match(iterIn.position(), matchValue,this); if ( epart._andor != loNONE) ok = epart._andor == loAND ? ok && partOk : ok || partOk; else ok &= partOk; if (epart._type == ExpressionPart::ptATTRIBUTE && extraAtrrib.size() == 1){ if ( pixValue < 0 || pixValue >= _inputAttributeTable->recordCount()){ ok = false; continue; } // pixValue == ID; ID < zero means undef, ID's start at zero. if (pixValue >= 0) { if (keyColumn != iUNDEF){ auto iter = raw2record.find(pixValue); if ( iter != raw2record.end()){ quint32 rec = iter->second; const Record& record = _inputAttributeTable->record(rec); pixValue = record.cell(extraAtrrib[0]).toDouble(); }else pixValue = rUNDEF; } } else pixValue = rUNDEF; } } if ( ok){ *iterOut = pixValue; }else *iterOut = rUNDEF; ++iterIn; ++iterOut; updateTranquilizer(++count, 100); } // if there is an attribute table we must copy the correct attributes and records if ( keyColumn != iUNDEF && _attTable.isValid()){ for(int recIndex=0; recIndex < _inputAttributeTable->recordCount(); ++recIndex){ const Record& rec = _inputAttributeTable->record(recIndex); for(int i=0; i < extraAtrrib.size(); ++i){ _attTable->setCell(i, recIndex, rec.cell(extraAtrrib[i])); } } } } if ( numeric) outputRaster->statistics(ContainerStatistics<double>::pBASIC); outputRaster->setAttributes(_attTable); QVariant value; value.setValue<IRasterCoverage>(outputRaster); logOperation(outputRaster, _expression); ctx->setOutput(symTable, value, outputRaster->name(), itRASTER,outputRaster->resource()); return true; }
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; }
IRasterCoverage operator-(double number,const IRasterCoverage &raster1) { QString name = ANONYMOUS_PREFIX; QString stmt = QString("script %1=%2 - %3").arg(name).arg(number).arg(raster1->name()); return doRasterOperation(stmt); }
bool PercentileFilterStretch::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>(); IRasterCoverage zoneRaster = _inputZones.as<RasterCoverage>(); ITable low = _lowPercentile.as<Table>(); ITable high = _highPercentile.as<Table>(); PixelIterator iterIn(inputRaster, BoundingBox(), PixelIterator::fZXY); PixelIterator iterZone(zoneRaster, BoundingBox(), PixelIterator::fXYZ); // only one layer so Z is irrelevant PixelIterator iterOut(outputRaster, BoundingBox(), PixelIterator::fZXY); PixelIterator inEnd = iterIn.end(); _nb = inputRaster->size().zsize(); int rows = inputRaster->size().xsize(); int cols = inputRaster->size().ysize(); std::vector<double> slice(_nb); std::vector<int> percentage(_nb); int totalRows = low->recordCount(); // same as high->recordCount() int totalCols = low->columnCount(); // same as high->columnCount() std::vector<double> lowtab(low->columnCount() * low->recordCount()); std::vector<double> hightab(high->columnCount() * high->recordCount()); for (int row = 0; row < totalRows; ++row) for (int col = 0; col < totalCols; ++col) { // Let the first column in the percentile table match the start of time series int actCol = (col + totalCols - _startDekad) % totalCols; lowtab[actCol + row * totalCols] = low->cell(col, row).toDouble(); hightab[actCol + row * totalCols] = high->cell(col, row).toDouble(); } // timeseries are assumed to be 10 day periods. int pixCount = 0; while (iterIn != inEnd) { trq()->update(pixCount++); // get the time slice at the current location std::copy(iterIn, iterIn + _nb, slice.begin()); // get the zone at the current location double dzone = *iterZone; // row index into low and high percentile tables if (dzone == rUNDEF) { // for out of area locations set to zero percent std::fill(percentage.begin(), percentage.end(), 0); } else { int zone = (long) dzone; std::vector<double>::const_iterator liter = lowtab.begin() + zone * totalCols; std::vector<double>::const_iterator hiter = hightab.begin() + zone * totalCols; std::vector<int>::iterator piter = percentage.begin(); for (std::vector<double>::const_iterator siter = slice.begin(); siter != slice.end(); siter++) { *piter = std::max(0, std::min(100, int(100.0 * (*hiter - *siter) / (*hiter - *liter)))); if (*piter <= 5) *piter = 0; else if (*piter <= 10) *piter = 10; piter++; liter++; hiter++; } } std::copy(percentage.begin(), percentage.end(), iterOut); iterIn += _nb; iterOut += _nb; iterZone += 1; } trq()->update(rows * cols); trq()->inform("\nWriting...\n"); trq()->stop(); bool resource = true; if ( resource && ctx != 0) { QVariant value; value.setValue<IRasterCoverage>(outputRaster); ctx->setOutput(symTable, value, outputRaster->name(), itRASTER, outputRaster->resource() ); } return resource; }
IRasterCoverage operator/(double number,const IRasterCoverage &raster1) { QString name = Identity::newAnonymousName();; QString stmt = QString("script %1=%2 / %3").arg(name).arg(number).arg(raster1->name()); return doRasterOperation(stmt); }
OperationImplementation::State MapCalc::prepare(ExecutionContext *ctx,const SymbolTable &st) { OperationImplementation::prepare(ctx,st); QString expr = _expression.input<QString>(0); RasterStackDefinition stackdef; for(int parmIndex = 1 ; parmIndex < _expression.parameterCount(); ++parmIndex){ Parameter parm = _expression.parm(parmIndex); if ( hasType(parm.valuetype(), itRASTER)){ QString url = parm.value(); IRasterCoverage raster; if(!raster.prepare(url)){ return sPREPAREFAILED; } if ( stackdef.isValid()){ if(!stackdef.checkStackDefintion(raster->stackDefinition())){ kernel()->issues()->log(TR("Incompatible stack definition for ") +raster->name() ) ; return sPREPAREFAILED; } }else if ( raster->stackDefinition().domain()->code().indexOf("count") == -1) stackdef = raster->stackDefinition(); _inputRasters[parmIndex] = PixelIterator(raster); }else if ( hasType(parm.valuetype(), itNUMBER)){ bool ok; double v = parm.value().toDouble(&ok); if (!ok){ return sPREPAREFAILED; } _inputNumbers[parmIndex] = v; } } OperationHelperRaster helper; helper.initialize((*_inputRasters.begin()).second.raster(), _outputRaster, itRASTERSIZE | itENVELOPE | itCOORDSYSTEM | itGEOREF); if ( stackdef.isValid()){ _outputRaster->stackDefinitionRef() = stackdef; } IDomain outputDomain; try { outputDomain = linearize(shuntingYard(expr)); if( !outputDomain.isValid()) return sPREPAREFAILED; } catch(ErrorObject& err){ return sPREPAREFAILED; } _outputRaster->datadefRef().domain(outputDomain); for(quint32 i = 0; i < _outputRaster->size().zsize(); ++i){ QString index = _outputRaster->stackDefinition().index(i); _outputRaster->setBandDefinition(index,DataDefinition(outputDomain)); } initialize(_outputRaster->size().linearSize()); return sPREPARED; }