bool QgsDecorationGrid::getIntervalFromCurrentLayer( double* values ) { // get current layer and make sure it is a raster layer and CRSs match QgsMapLayer* layer = QgisApp::instance()->mapCanvas()->currentLayer(); if ( ! layer ) { QMessageBox::warning( nullptr, tr( "Error" ), tr( "No active layer" ) ); return false; } if ( layer->type() != QgsMapLayer::RasterLayer ) { QMessageBox::warning( nullptr, tr( "Error" ), tr( "Please select a raster layer" ) ); return false; } QgsRasterLayer* rlayer = dynamic_cast<QgsRasterLayer*>( layer ); if ( !rlayer || rlayer->width() == 0 || rlayer->height() == 0 ) { QMessageBox::warning( nullptr, tr( "Error" ), tr( "Invalid raster layer" ) ); return false; } QgsCoordinateReferenceSystem layerCRS = layer->crs(); const QgsCoordinateReferenceSystem& mapCRS = QgisApp::instance()->mapCanvas()->mapSettings().destinationCrs(); // is this the best way to compare CRS? should we also make sure map has OTF enabled? // TODO calculate transformed values if necessary if ( layerCRS != mapCRS ) { QMessageBox::warning( nullptr, tr( "Error" ), tr( "Layer CRS must be equal to project CRS" ) ); return false; } // calculate interval // TODO add a function in QgsRasterLayer to get x/y resolution from provider, // because this might not be 100% accurate QgsRectangle extent = rlayer->extent(); values[0] = fabs( extent.xMaximum() - extent.xMinimum() ) / rlayer->width(); values[1] = fabs( extent.yMaximum() - extent.yMinimum() ) / rlayer->height(); // calculate offset - when using very high resolution rasters in geographic CRS // there seems to be a small shift, but this may be due to rendering issues and depends on zoom double ratio = extent.xMinimum() / values[0]; values[2] = ( ratio - floor( ratio ) ) * values[0]; ratio = extent.yMinimum() / values[1]; values[3] = ( ratio - floor( ratio ) ) * values[1]; QgsDebugMsg( QString( "xmax: %1 xmin: %2 width: %3 xInterval: %4 xOffset: %5" ).arg( extent.xMaximum() ).arg( extent.xMinimum() ).arg( rlayer->width() ).arg( values[0] ).arg( values[2] ) ); QgsDebugMsg( QString( "ymax: %1 ymin: %2 height: %3 yInterval: %4 yOffset: %5" ).arg( extent.yMaximum() ).arg( extent.yMinimum() ).arg( rlayer->height() ).arg( values[1] ).arg( values[3] ) ); return true; }
void QgsRasterCalcDialog::on_mCurrentLayerExtentButton_clicked() { QListWidgetItem* currentLayerItem = mRasterBandsListWidget->currentItem(); if ( currentLayerItem ) { QgsRasterLayer* rlayer = nullptr; QList<QgsRasterCalculatorEntry>::const_iterator rasterIt = mAvailableRasterBands.constBegin(); for ( ; rasterIt != mAvailableRasterBands.constEnd(); ++rasterIt ) { if ( rasterIt->ref == currentLayerItem->text() ) { rlayer = rasterIt->raster; } } if ( !rlayer ) { return; } QgsRectangle layerExtent = rlayer->extent(); mXMinSpinBox->setValue( layerExtent.xMinimum() ); mXMaxSpinBox->setValue( layerExtent.xMaximum() ); mYMinSpinBox->setValue( layerExtent.yMinimum() ); mYMaxSpinBox->setValue( layerExtent.yMaximum() ); mNColumnsSpinBox->setValue( rlayer->width() ); mNRowsSpinBox->setValue( rlayer->height() ); mCrsSelector->setCrs( rlayer->crs() ); } }
void QgsRasterCalcDialog::insertAvailableRasterBands() { const QMap<QString, QgsMapLayer*>& layers = QgsMapLayerRegistry::instance()->mapLayers(); QMap<QString, QgsMapLayer*>::const_iterator layerIt = layers.constBegin(); bool firstLayer = true; for ( ; layerIt != layers.constEnd(); ++layerIt ) { QgsRasterLayer* rlayer = dynamic_cast<QgsRasterLayer*>( layerIt.value() ); if ( rlayer && rlayer->dataProvider() && rlayer->dataProvider()->name() == "gdal" ) { if ( firstLayer ) //set bounding box / resolution of output to the values of the first possible input layer { mNColumnsSpinBox->setValue( rlayer->width() ); mNRowsSpinBox->setValue( rlayer->height() ); QgsRectangle bbox = rlayer->extent(); mXMinSpinBox->setValue( bbox.xMinimum() ); mXMaxSpinBox->setValue( bbox.xMaximum() ); mYMinSpinBox->setValue( bbox.yMinimum() ); mYMaxSpinBox->setValue( bbox.yMaximum() ); firstLayer = false; } //get number of bands for ( int i = 0; i < rlayer->bandCount(); ++i ) { QgsRasterCalculatorEntry entry; entry.raster = rlayer; entry.bandNumber = i + 1; entry.ref = rlayer->name() + '@' + QString::number( i + 1 ); mAvailableRasterBands.push_back( entry ); mRasterBandsListWidget->addItem( entry.ref ); } } } }
void QgsRasterCalcDialog::insertAvailableRasterBands() { const QMap<QString, QgsMapLayer *> &layers = QgsProject::instance()->mapLayers(); QMap<QString, QgsMapLayer *>::const_iterator layerIt = layers.constBegin(); for ( ; layerIt != layers.constEnd(); ++layerIt ) { QgsRasterLayer *rlayer = dynamic_cast<QgsRasterLayer *>( layerIt.value() ); if ( rlayer && rlayer->dataProvider() && rlayer->dataProvider()->name() == QLatin1String( "gdal" ) ) { if ( !mExtentSizeSet ) //set bounding box / resolution of output to the values of the first possible input layer { setExtentSize( rlayer->width(), rlayer->height(), rlayer->extent() ); mCrsSelector->setCrs( rlayer->crs() ); } //get number of bands for ( int i = 0; i < rlayer->bandCount(); ++i ) { QgsRasterCalculatorEntry entry; entry.raster = rlayer; entry.bandNumber = i + 1; entry.ref = rlayer->name() + '@' + QString::number( i + 1 ); mAvailableRasterBands.push_back( entry ); mRasterBandsListWidget->addItem( entry.ref ); } } } }
void QgsRasterCalcDialog::insertAvailableRasterBands() { mAvailableRasterBands = QgsRasterCalculatorEntry::rasterEntries().toList(); mRasterBandsListWidget->clear(); for ( const auto &entry : qgis::as_const( mAvailableRasterBands ) ) { QgsRasterLayer *rlayer = entry.raster; if ( rlayer && rlayer->dataProvider() && rlayer->dataProvider()->name() == QLatin1String( "gdal" ) ) { if ( !mExtentSizeSet ) //set bounding box / resolution of output to the values of the first possible input layer { setExtentSize( rlayer->width(), rlayer->height(), rlayer->extent() ); mCrsSelector->setCrs( rlayer->crs() ); } QListWidgetItem *item = new QListWidgetItem( entry.ref, mRasterBandsListWidget ); item->setData( Qt::ToolTipRole, rlayer->publicSource() ); mRasterBandsListWidget->addItem( item ); } } }