Ejemplo n.º 1
0
void QgsRasterTransparencyWidget::apply()
{
  //set NoDataValue
  QgsRasterRangeList myNoDataRangeList;
  if ( "" != leNoDataValue->text() )
  {
    bool myDoubleOk = false;
    double myNoDataValue = leNoDataValue->text().toDouble( &myDoubleOk );
    if ( myDoubleOk )
    {
      QgsRasterRange myNoDataRange( myNoDataValue, myNoDataValue );
      myNoDataRangeList << myNoDataRange;
    }
  }
  for ( int bandNo = 1; bandNo <= mRasterLayer->dataProvider()->bandCount(); bandNo++ )
  {
    mRasterLayer->dataProvider()->setUserNoDataValue( bandNo, myNoDataRangeList );
    mRasterLayer->dataProvider()->setUseSourceNoDataValue( bandNo, mSrcNoDataValueCheckBox->isChecked() );
  }

  //transparency settings
  QgsRasterRenderer *rasterRenderer = mRasterLayer->renderer();
  if ( rasterRenderer )
  {
    rasterRenderer->setAlphaBand( cboxTransparencyBand->currentBand() );

    //Walk through each row in table and test value. If not valid set to 0.0 and continue building transparency list
    QgsRasterTransparency *rasterTransparency = new QgsRasterTransparency();
    if ( tableTransparency->columnCount() == 4 )
    {
      QgsRasterTransparency::TransparentThreeValuePixel myTransparentPixel;
      QList<QgsRasterTransparency::TransparentThreeValuePixel> myTransparentThreeValuePixelList;
      myTransparentThreeValuePixelList.reserve( tableTransparency->rowCount() );
      for ( int myListRunner = 0; myListRunner < tableTransparency->rowCount(); myListRunner++ )
      {
        myTransparentPixel.red = transparencyCellValue( myListRunner, 0 );
        myTransparentPixel.green = transparencyCellValue( myListRunner, 1 );
        myTransparentPixel.blue = transparencyCellValue( myListRunner, 2 );
        myTransparentPixel.percentTransparent = transparencyCellValue( myListRunner, 3 );
        myTransparentThreeValuePixelList.append( myTransparentPixel );
      }
      rasterTransparency->setTransparentThreeValuePixelList( myTransparentThreeValuePixelList );
    }
    else if ( tableTransparency->columnCount() == 3 )
    {
      QgsRasterTransparency::TransparentSingleValuePixel myTransparentPixel;
      QList<QgsRasterTransparency::TransparentSingleValuePixel> myTransparentSingleValuePixelList;
      myTransparentSingleValuePixelList.reserve( tableTransparency->rowCount() );
      for ( int myListRunner = 0; myListRunner < tableTransparency->rowCount(); myListRunner++ )
      {
        myTransparentPixel.min = transparencyCellValue( myListRunner, 0 );
        myTransparentPixel.max = transparencyCellValue( myListRunner, 1 );
        myTransparentPixel.percentTransparent = transparencyCellValue( myListRunner, 2 );

        myTransparentSingleValuePixelList.append( myTransparentPixel );
      }
      rasterTransparency->setTransparentSingleValuePixelList( myTransparentSingleValuePixelList );
    }

    rasterRenderer->setRasterTransparency( rasterTransparency );

    //set global transparency
    rasterRenderer->setOpacity( mOpacityWidget->opacity() );
  }
}
QgsRasterRenderer* QgsRasterRendererRegistry::defaultRendererForDrawingStyle( QgsRaster::DrawingStyle theDrawingStyle, QgsRasterDataProvider* provider ) const
{
    if ( !provider || provider->bandCount() < 1 )
    {
        return nullptr;
    }


    QgsRasterRenderer* renderer = nullptr;
    switch ( theDrawingStyle )
    {
    case QgsRaster::PalettedColor:
    {
        int grayBand = 1; //reasonable default
        QList<QgsColorRampShader::ColorRampItem> colorEntries = provider->colorTable( grayBand );

        //go through list and take maximum value (it could be that entries don't start at 0 or indices are not contiguous)
        int colorArraySize = 0;
        QList<QgsColorRampShader::ColorRampItem>::const_iterator colorIt = colorEntries.constBegin();
        for ( ; colorIt != colorEntries.constEnd(); ++colorIt )
        {
            if ( colorIt->value > colorArraySize )
            {
                colorArraySize = ( int )( colorIt->value );
            }
        }

        colorArraySize += 1; //usually starts at 0
        QColor* colorArray = new QColor[ colorArraySize ];
        colorIt = colorEntries.constBegin();
        QVector<QString> labels;
        for ( ; colorIt != colorEntries.constEnd(); ++colorIt )
        {
            int idx = ( int )( colorIt->value );
            colorArray[idx] = colorIt->color;
            if ( !colorIt->label.isEmpty() )
            {
                if ( labels.size() <= idx ) labels.resize( idx + 1 );
                labels[idx] = colorIt->label;
            }
        }

        renderer = new QgsPalettedRasterRenderer( provider,
                grayBand,
                colorArray,
                colorArraySize,
                labels );
    }
    break;
    case QgsRaster::MultiBandSingleBandGray:
    case QgsRaster::SingleBandGray:
    {
        int grayBand = 1;
        renderer = new QgsSingleBandGrayRenderer( provider, grayBand );

        QgsContrastEnhancement* ce = new QgsContrastEnhancement(( QGis::DataType )(
                    provider->dataType( grayBand ) ) );

// Default contrast enhancement is set from QgsRasterLayer, it has already setContrastEnhancementAlgorithm(). Default enhancement must only be set if default style was not loaded (to avoid stats calculation).
        (( QgsSingleBandGrayRenderer* )renderer )->setContrastEnhancement( ce );
        break;
    }
    case QgsRaster::SingleBandPseudoColor:
    {
        int bandNo = 1;
        double minValue = 0;
        double maxValue = 0;
        // TODO: avoid calculating statistics if not necessary (default style loaded)
        minMaxValuesForBand( bandNo, provider, minValue, maxValue );
        QgsRasterShader* shader = new QgsRasterShader( minValue, maxValue );
        renderer = new QgsSingleBandPseudoColorRenderer( provider, bandNo, shader );
        break;
    }
    case QgsRaster::MultiBandColor:
    {
        QSettings s;

        int redBand = s.value( "/Raster/defaultRedBand", 1 ).toInt();
        if ( redBand < 0 || redBand > provider->bandCount() )
        {
            redBand = -1;
        }
        int greenBand = s.value( "/Raster/defaultGreenBand", 2 ).toInt();
        if ( greenBand < 0 || greenBand > provider->bandCount() )
        {
            greenBand = -1;
        }
        int blueBand = s.value( "/Raster/defaultBlueBand", 3 ).toInt();
        if ( blueBand < 0 || blueBand > provider->bandCount() )
        {
            blueBand = -1;
        }

        renderer = new QgsMultiBandColorRenderer( provider, redBand, greenBand, blueBand );
        break;
    }
    case QgsRaster::SingleBandColorDataStyle:
    {
        renderer = new QgsSingleBandColorDataRenderer( provider, 1 );
        break;
    }
    default:
        return nullptr;
    }

    QgsRasterTransparency* tr = new QgsRasterTransparency(); //renderer takes ownership
    int bandCount = renderer->usesBands().size();
    if ( bandCount == 1 )
    {
        QList<QgsRasterTransparency::TransparentSingleValuePixel> transparentSingleList;
        tr->setTransparentSingleValuePixelList( transparentSingleList );
    }
    else if ( bandCount == 3 )
    {
        QList<QgsRasterTransparency::TransparentThreeValuePixel> transparentThreeValueList;
        tr->setTransparentThreeValuePixelList( transparentThreeValueList );
    }
    renderer->setRasterTransparency( tr );
    return renderer;
}
Ejemplo n.º 3
0
QgsRasterRenderer *QgsRasterRendererRegistry::defaultRendererForDrawingStyle( QgsRaster::DrawingStyle drawingStyle, QgsRasterDataProvider *provider ) const
{
  if ( !provider || provider->bandCount() < 1 )
  {
    return nullptr;
  }


  QgsRasterRenderer *renderer = nullptr;
  switch ( drawingStyle )
  {
    case QgsRaster::PalettedColor:
    {
      int grayBand = 1; //reasonable default
      QgsPalettedRasterRenderer::ClassData classes = QgsPalettedRasterRenderer::colorTableToClassData( provider->colorTable( grayBand ) );
      renderer = new QgsPalettedRasterRenderer( provider,
          grayBand,
          classes );
    }
    break;
    case QgsRaster::MultiBandSingleBandGray:
    case QgsRaster::SingleBandGray:
    {
      int grayBand = 1;
      renderer = new QgsSingleBandGrayRenderer( provider, grayBand );

      QgsContrastEnhancement *ce = new QgsContrastEnhancement( ( Qgis::DataType )(
            provider->dataType( grayBand ) ) );

// Default contrast enhancement is set from QgsRasterLayer, it has already setContrastEnhancementAlgorithm(). Default enhancement must only be set if default style was not loaded (to avoid stats calculation).
      ( ( QgsSingleBandGrayRenderer * )renderer )->setContrastEnhancement( ce );
      break;
    }
    case QgsRaster::SingleBandPseudoColor:
    {
      int bandNo = 1;
      double minValue = 0;
      double maxValue = 0;
      // TODO: avoid calculating statistics if not necessary (default style loaded)
      minMaxValuesForBand( bandNo, provider, minValue, maxValue );
      QgsRasterShader *shader = new QgsRasterShader( minValue, maxValue );
      renderer = new QgsSingleBandPseudoColorRenderer( provider, bandNo, shader );
      break;
    }
    case QgsRaster::MultiBandColor:
    {
      QgsSettings s;

      int redBand = s.value( QStringLiteral( "/Raster/defaultRedBand" ), 1 ).toInt();
      if ( redBand < 0 || redBand > provider->bandCount() )
      {
        redBand = -1;
      }
      int greenBand = s.value( QStringLiteral( "/Raster/defaultGreenBand" ), 2 ).toInt();
      if ( greenBand < 0 || greenBand > provider->bandCount() )
      {
        greenBand = -1;
      }
      int blueBand = s.value( QStringLiteral( "/Raster/defaultBlueBand" ), 3 ).toInt();
      if ( blueBand < 0 || blueBand > provider->bandCount() )
      {
        blueBand = -1;
      }

      renderer = new QgsMultiBandColorRenderer( provider, redBand, greenBand, blueBand );
      break;
    }
    case QgsRaster::SingleBandColorDataStyle:
    {
      renderer = new QgsSingleBandColorDataRenderer( provider, 1 );
      break;
    }
    default:
      return nullptr;
  }

  QgsRasterTransparency *tr = new QgsRasterTransparency(); //renderer takes ownership
  int bandCount = renderer->usesBands().size();
  if ( bandCount == 1 )
  {
    QList<QgsRasterTransparency::TransparentSingleValuePixel> transparentSingleList;
    tr->setTransparentSingleValuePixelList( transparentSingleList );
  }
  else if ( bandCount == 3 )
  {
    QList<QgsRasterTransparency::TransparentThreeValuePixel> transparentThreeValueList;
    tr->setTransparentThreeValuePixelList( transparentThreeValueList );
  }
  renderer->setRasterTransparency( tr );
  return renderer;
}