Beispiel #1
0
QgsSingleBandPseudoColorRenderer* QgsSingleBandPseudoColorRenderer::clone() const
{
  QgsRasterShader *shader = nullptr;

  if ( mShader )
  {
    shader = new QgsRasterShader( mShader->minimumValue(), mShader->maximumValue() );

    // Shader function
    const QgsColorRampShader* origColorRampShader = dynamic_cast<const QgsColorRampShader*>( mShader->rasterShaderFunction() );

    if ( origColorRampShader )
    {
      QgsColorRampShader * colorRampShader = new QgsColorRampShader( mShader->minimumValue(), mShader->maximumValue() );

      if ( origColorRampShader->sourceColorRamp() )
      {
        colorRampShader->setSourceColorRamp( origColorRampShader->sourceColorRamp()->clone() );
      }
      colorRampShader->setColorRampType( origColorRampShader->colorRampType() );
      colorRampShader->setClip( origColorRampShader->clip() );
      colorRampShader->setColorRampItemList( origColorRampShader->colorRampItemList() );
      shader->setRasterShaderFunction( colorRampShader );
    }
  }
  QgsSingleBandPseudoColorRenderer * renderer = new QgsSingleBandPseudoColorRenderer( nullptr, mBand, shader );
  renderer->copyCommonProperties( this );

  return renderer;
}
Beispiel #2
0
void QgsRasterShader::readXml( const QDomElement& elem )
{
    //only colorrampshader
    QDomElement colorRampShaderElem = elem.firstChildElement( QStringLiteral( "colorrampshader" ) );
    if ( !colorRampShaderElem.isNull() )
    {
        QgsColorRampShader* colorRampShader = new QgsColorRampShader();

        // try to load color ramp (optional)
        QDomElement sourceColorRampElem = colorRampShaderElem.firstChildElement( QStringLiteral( "colorramp" ) );
        if ( !sourceColorRampElem.isNull() && sourceColorRampElem.attribute( QStringLiteral( "name" ) ) == QLatin1String( "[source]" ) )
        {
            colorRampShader->setSourceColorRamp( QgsSymbolLayerUtils::loadColorRamp( sourceColorRampElem ) );
        }

        colorRampShader->setColorRampType( colorRampShaderElem.attribute( QStringLiteral( "colorRampType" ), QStringLiteral( "INTERPOLATED" ) ) );
        colorRampShader->setClassificationMode( static_cast< QgsColorRampShader::ClassificationMode >( colorRampShaderElem.attribute( QStringLiteral( "classificationMode" ), QStringLiteral( "1" ) ).toInt() ) );
        colorRampShader->setClip( colorRampShaderElem.attribute( QStringLiteral( "clip" ), QStringLiteral( "0" ) ) == QLatin1String( "1" ) );

        QList<QgsColorRampShader::ColorRampItem> itemList;
        QDomElement itemElem;
        QString itemLabel;
        double itemValue;
        QColor itemColor;

        QDomNodeList itemNodeList = colorRampShaderElem.elementsByTagName( QStringLiteral( "item" ) );
        itemList.reserve( itemNodeList.size() );
        for ( int i = 0; i < itemNodeList.size(); ++i )
        {
            itemElem = itemNodeList.at( i ).toElement();
            itemValue = itemElem.attribute( QStringLiteral( "value" ) ).toDouble();
            itemLabel = itemElem.attribute( QStringLiteral( "label" ) );
            itemColor.setNamedColor( itemElem.attribute( QStringLiteral( "color" ) ) );
            itemColor.setAlpha( itemElem.attribute( QStringLiteral( "alpha" ), QStringLiteral( "255" ) ).toInt() );

            itemList.push_back( QgsColorRampShader::ColorRampItem( itemValue, itemColor, itemLabel ) );
        }
        colorRampShader->setColorRampItemList( itemList );
        setRasterShaderFunction( colorRampShader );
    }
}