Skip to content
Permalink
Browse files
Add method to load paletted renderer classes from file
  • Loading branch information
nyalldawson committed Apr 3, 2017
1 parent a6d3af7 commit d542a1a442540378d9e0c96a3e3e7fbb89e495b8
@@ -38,6 +38,7 @@ class QgsPalettedRasterRenderer : QgsRasterRenderer
QgsColorRamp *sourceColorRamp() const;
static QgsPalettedRasterRenderer::ClassData colorTableToClassData( const QList<QgsColorRampShader::ColorRampItem> &table );
static QgsPalettedRasterRenderer::ClassData classDataFromString( const QString &string );
static QgsPalettedRasterRenderer::ClassData classDataFromFile( const QString &path );

private:

@@ -362,6 +362,19 @@ QgsPalettedRasterRenderer::ClassData QgsPalettedRasterRenderer::classDataFromStr
return classes;
}

QgsPalettedRasterRenderer::ClassData QgsPalettedRasterRenderer::classDataFromFile( const QString &path )
{
QFile inputFile( path );
QString input;
if ( inputFile.open( QIODevice::ReadOnly ) )
{
QTextStream in( &inputFile );
input = in.readAll();
inputFile.close();
}
return classDataFromString( input );
}

void QgsPalettedRasterRenderer::updateArrays()
{
// find maximum color index
@@ -116,9 +116,17 @@ class CORE_EXPORT QgsPalettedRasterRenderer: public QgsRasterRenderer
/**
* Converts a \a string containing a color table or class data to to paletted renderer class data.
* @note added in QGIS 3.0
* @see classDataFromFile()
*/
static QgsPalettedRasterRenderer::ClassData classDataFromString( const QString &string );

/**
* Opens a color table file and returns corresponding paletted renderer class data.
* @note added in QGIS 3.0
* @see classDataFromString()
*/
static QgsPalettedRasterRenderer::ClassData classDataFromFile( const QString &path );

private:

int mBand;
@@ -500,6 +500,38 @@ def testLoadPalettedColorDataFromString(self):
classes = QgsPalettedRasterRenderer.classDataFromString(bad)
self.assertEqual(len(classes), 0)

def testLoadPalettedClassDataFromFile(self):
# bad file
classes = QgsPalettedRasterRenderer.classDataFromFile('ajdhjashjkdh kjahjkdhk')
self.assertEqual(len(classes), 0)

# good file!
path = os.path.join(unitTestDataPath('raster'),
'test.clr')
classes = QgsPalettedRasterRenderer.classDataFromFile(path)
self.assertEqual(len(classes), 10)
self.assertEqual(classes[0].value, 1)
self.assertEqual(classes[0].color.name(), '#000000')
self.assertEqual(classes[0].color.alpha(), 255)
self.assertEqual(classes[1].value, 2)
self.assertEqual(classes[1].color.name(), '#c8c8c8')
self.assertEqual(classes[2].value, 3)
self.assertEqual(classes[2].color.name(), '#006e00')
self.assertEqual(classes[3].value, 4)
self.assertEqual(classes[3].color.name(), '#6e4100')
self.assertEqual(classes[4].value, 5)
self.assertEqual(classes[4].color.name(), '#0000ff')
self.assertEqual(classes[4].color.alpha(), 255)
self.assertEqual(classes[5].value, 6)
self.assertEqual(classes[5].color.name(), '#0059ff')
self.assertEqual(classes[6].value, 7)
self.assertEqual(classes[6].color.name(), '#00aeff')
self.assertEqual(classes[7].value, 8)
self.assertEqual(classes[7].color.name(), '#00fff6')
self.assertEqual(classes[8].value, 9)
self.assertEqual(classes[8].color.name(), '#eeff00')
self.assertEqual(classes[9].value, 10)
self.assertEqual(classes[9].color.name(), '#ffb600')

if __name__ == '__main__':
unittest.main()

0 comments on commit d542a1a

Please sign in to comment.