Skip to content
Permalink
Browse files

Fix test

  • Loading branch information
nyalldawson committed Sep 6, 2020
1 parent 1c683e2 commit c0ebc68eff9630b4b49659162c474f9ccc9d3e8a
@@ -242,23 +242,27 @@ Parses a symbol layer.
%End


static QgsProperty parseInterpolateColorByZoom( const QVariantMap &json, QgsMapBoxGlStyleConversionContext &context, QColor *defaultColor = 0 );
static QgsProperty parseInterpolateColorByZoom( const QVariantMap &json, QgsMapBoxGlStyleConversionContext &context, QColor *defaultColor /Out/ = 0 );
%Docstring
Parses a color value which is interpolated by zoom range.

:param json: definition of color interpolation
:param context: conversion context
:param defaultColor: optional storage for a reasonable "default" color representing the overall property.

:return: - QgsProperty representing interpolation settings
- defaultColor: storage for a reasonable "default" color representing the overall property.
%End

static QgsProperty parseInterpolateByZoom( const QVariantMap &json, QgsMapBoxGlStyleConversionContext &context, double multiplier = 1, double *defaultNumber = 0 );
static QgsProperty parseInterpolateByZoom( const QVariantMap &json, QgsMapBoxGlStyleConversionContext &context, double multiplier = 1, double *defaultNumber /Out/ = 0 );
%Docstring
Parses a numeric value which is interpolated by zoom range.

:param json: definition of interpolation
:param context: conversion context
:param multiplier: optional multiplication factor
:param defaultNumber: optional storage for a reasonable "default" number representing the overall property.

:return: - QgsProperty representing interpolation settings
- defaultNumber: storage for a reasonable "default" number representing the overall property.
%End

static QgsProperty parseInterpolateOpacityByZoom( const QVariantMap &json );
@@ -293,8 +297,8 @@ Parses a list of interpolation stops
%End

static QgsProperty parseInterpolateListByZoom( const QVariantList &json, PropertyType type, QgsMapBoxGlStyleConversionContext &context, double multiplier = 1,
QColor *defaultColor = 0,
double *defaultNumber = 0 );
QColor *defaultColor /Out/ = 0,
double *defaultNumber /Out/ = 0 );
%Docstring
Interpolates a list which starts with the interpolate function.

@@ -262,8 +262,10 @@ class CORE_EXPORT QgsMapBoxGlStyleConverter
* \param json definition of color interpolation
* \param context conversion context
* \param defaultColor optional storage for a reasonable "default" color representing the overall property.
*
* \returns QgsProperty representing interpolation settings
*/
static QgsProperty parseInterpolateColorByZoom( const QVariantMap &json, QgsMapBoxGlStyleConversionContext &context, QColor *defaultColor = nullptr );
static QgsProperty parseInterpolateColorByZoom( const QVariantMap &json, QgsMapBoxGlStyleConversionContext &context, QColor *defaultColor SIP_OUT = nullptr );

/**
* Parses a numeric value which is interpolated by zoom range.
@@ -272,8 +274,10 @@ class CORE_EXPORT QgsMapBoxGlStyleConverter
* \param context conversion context
* \param multiplier optional multiplication factor
* \param defaultNumber optional storage for a reasonable "default" number representing the overall property.
*
* \returns QgsProperty representing interpolation settings
*/
static QgsProperty parseInterpolateByZoom( const QVariantMap &json, QgsMapBoxGlStyleConversionContext &context, double multiplier = 1, double *defaultNumber = nullptr );
static QgsProperty parseInterpolateByZoom( const QVariantMap &json, QgsMapBoxGlStyleConversionContext &context, double multiplier = 1, double *defaultNumber SIP_OUT = nullptr );

/**
* Interpolates opacity with either scale_linear() or scale_exp() (depending on base value).
@@ -308,8 +312,8 @@ class CORE_EXPORT QgsMapBoxGlStyleConverter
* \warning This is private API only, and may change in future QGIS versions
*/
static QgsProperty parseInterpolateListByZoom( const QVariantList &json, PropertyType type, QgsMapBoxGlStyleConversionContext &context, double multiplier = 1,
QColor *defaultColor = nullptr,
double *defaultNumber = nullptr );
QColor *defaultColor SIP_OUT = nullptr,
double *defaultNumber SIP_OUT = nullptr );

/**
* Parses a \a color in one of these supported formats:
@@ -46,22 +46,27 @@ def testColorAsHslaComponents(self):

def testParseInterpolateColorByZoom(self):
conversion_context = QgsMapBoxGlStyleConversionContext()
self.assertEqual(QgsMapBoxGlStyleConverter.parseInterpolateColorByZoom({}, conversion_context).isActive(),
props, default_col = QgsMapBoxGlStyleConverter.parseInterpolateColorByZoom({}, conversion_context)
self.assertEqual(props.isActive(),
False)
self.assertEqual(QgsMapBoxGlStyleConverter.parseInterpolateColorByZoom({'base': 1,
'stops': [[0, '#f1f075'],
[150, '#b52e3e'],
[250, '#e55e5e']]
},
conversion_context).expressionString(),
props, default_col = QgsMapBoxGlStyleConverter.parseInterpolateColorByZoom({'base': 1,
'stops': [[0, '#f1f075'],
[150, '#b52e3e'],
[250, '#e55e5e']]
},
conversion_context)
self.assertEqual(props.expressionString(),
'CASE WHEN @zoom_level >= 0 AND @zoom_level < 150 THEN color_hsla(scale_linear(@zoom_level, 0, 150, 59, 352), scale_linear(@zoom_level, 0, 150, 81, 59), scale_linear(@zoom_level, 0, 150, 70, 44), scale_linear(@zoom_level, 0, 150, 255, 255)) WHEN @zoom_level >= 150 AND @zoom_level < 250 THEN color_hsla(scale_linear(@zoom_level, 150, 250, 352, 0), scale_linear(@zoom_level, 150, 250, 59, 72), scale_linear(@zoom_level, 150, 250, 44, 63), scale_linear(@zoom_level, 150, 250, 255, 255)) WHEN @zoom_level >= 250 THEN color_hsla(0, 72, 63, 255) ELSE color_hsla(0, 72, 63, 255) END')
self.assertEqual(QgsMapBoxGlStyleConverter.parseInterpolateColorByZoom({'base': 2,
'stops': [[0, '#f1f075'],
[150, '#b52e3e'],
[250, '#e55e5e']]
},
conversion_context).expressionString(),
self.assertEqual(default_col.name(), '#f1f075')
props, default_col = QgsMapBoxGlStyleConverter.parseInterpolateColorByZoom({'base': 2,
'stops': [[0, '#f1f075'],
[150, '#b52e3e'],
[250, '#e55e5e']]
},
conversion_context)
self.assertEqual(props.expressionString(),
'CASE WHEN @zoom_level >= 0 AND @zoom_level < 150 THEN color_hsla(59 + 293 * (2^(@zoom_level-0)-1)/(2^(150-0)-1), 81 + -22 * (2^(@zoom_level-0)-1)/(2^(150-0)-1), 70 + -26 * (2^(@zoom_level-0)-1)/(2^(150-0)-1), 255 + 0 * (2^(@zoom_level-0)-1)/(2^(150-0)-1)) WHEN @zoom_level >= 150 AND @zoom_level < 250 THEN color_hsla(352 + -352 * (2^(@zoom_level-150)-1)/(2^(250-150)-1), 59 + 13 * (2^(@zoom_level-150)-1)/(2^(250-150)-1), 44 + 19 * (2^(@zoom_level-150)-1)/(2^(250-150)-1), 255 + 0 * (2^(@zoom_level-150)-1)/(2^(250-150)-1)) WHEN @zoom_level >= 250 THEN color_hsla(0, 72, 63, 255) ELSE color_hsla(0, 72, 63, 255) END')
self.assertEqual(default_col.name(), '#f1f075')

def testParseStops(self):
conversion_context = QgsMapBoxGlStyleConversionContext()
@@ -74,22 +79,28 @@ def testParseStops(self):

def testInterpolateByZoom(self):
conversion_context = QgsMapBoxGlStyleConversionContext()
self.assertEqual(QgsMapBoxGlStyleConverter.parseInterpolateByZoom({'base': 1,
'stops': [[0, 11],
[150, 15],
[250, 22]]
}, conversion_context).expressionString(),
prop, default_val = QgsMapBoxGlStyleConverter.parseInterpolateByZoom({'base': 1,
'stops': [[0, 11],
[150, 15],
[250, 22]]
}, conversion_context)
self.assertEqual(prop.expressionString(),
'CASE WHEN @zoom_level > 0 AND @zoom_level <= 150 THEN scale_linear(@zoom_level, 0, 150, 11, 15) * 1 WHEN @zoom_level > 150 AND @zoom_level <= 250 THEN scale_linear(@zoom_level, 150, 250, 15, 22) * 1 END')
self.assertEqual(QgsMapBoxGlStyleConverter.parseInterpolateByZoom({'base': 1,
'stops': [[0, 11],
[150, 15]]
}, conversion_context).expressionString(),
self.assertEqual(default_val, 11.0)
prop, default_val = QgsMapBoxGlStyleConverter.parseInterpolateByZoom({'base': 1,
'stops': [[0, 11],
[150, 15]]
}, conversion_context)
self.assertEqual(prop.expressionString(),
'scale_linear(@zoom_level, 0, 150, 11, 15) * 1')
self.assertEqual(QgsMapBoxGlStyleConverter.parseInterpolateByZoom({'base': 2,
'stops': [[0, 11],
[150, 15]]
}, conversion_context).expressionString(),
self.assertEqual(default_val, 11.0)
prop, default_val = QgsMapBoxGlStyleConverter.parseInterpolateByZoom({'base': 2,
'stops': [[0, 11],
[150, 15]]
}, conversion_context)
self.assertEqual(prop.expressionString(),
'11 + 4 * (2^(@zoom_level-0)-1)/(2^(150-0)-1)* 1')
self.assertEqual(default_val, 11.0)

def testInterpolateOpacityByZoom(self):
self.assertEqual(QgsMapBoxGlStyleConverter.parseInterpolateOpacityByZoom({'base': 1,
@@ -111,7 +122,7 @@ def testInterpolateOpacityByZoom(self):

def testInterpolateListByZoom(self):
conversion_context = QgsMapBoxGlStyleConversionContext()
self.assertEqual(QgsMapBoxGlStyleConverter.parseInterpolateListByZoom([
prop, default_color, default_val = QgsMapBoxGlStyleConverter.parseInterpolateListByZoom([
"interpolate",
["linear"],
["zoom"],
@@ -121,9 +132,11 @@ def testInterpolateListByZoom(self):
0.3,
18,
0.6
], QgsMapBoxGlStyleConverter.Opacity, conversion_context, 2).expressionString(),
"CASE WHEN @zoom_level < 10 THEN set_color_part(@symbol_color, 'alpha', 0.1 * 255) WHEN @zoom_level >= 10 AND @zoom_level < 15 THEN set_color_part(@symbol_color, 'alpha', scale_linear(@zoom_level, 10, 15, 0.1 * 255, 0.3 * 255)) WHEN @zoom_level >= 15 AND @zoom_level < 18 THEN set_color_part(@symbol_color, 'alpha', scale_linear(@zoom_level, 15, 18, 0.3 * 255, 0.6 * 255)) WHEN @zoom_level >= 18 THEN set_color_part(@symbol_color, 'alpha', 0.6) END")
self.assertEqual(QgsMapBoxGlStyleConverter.parseInterpolateListByZoom([
], QgsMapBoxGlStyleConverter.Opacity, conversion_context, 2)
self.assertEqual(prop.expressionString(),
"CASE WHEN @zoom_level < 10 THEN set_color_part(@symbol_color, 'alpha', 0.1 * 255) WHEN @zoom_level >= 10 AND @zoom_level < 15 THEN set_color_part(@symbol_color, 'alpha', scale_linear(@zoom_level, 10, 15, 0.1 * 255, 0.3 * 255)) WHEN @zoom_level >= 15 AND @zoom_level < 18 THEN set_color_part(@symbol_color, 'alpha', scale_linear(@zoom_level, 15, 18, 0.3 * 255, 0.6 * 255)) WHEN @zoom_level >= 18 THEN set_color_part(@symbol_color, 'alpha', 0.6) END")

prop, default_color, default_val = QgsMapBoxGlStyleConverter.parseInterpolateListByZoom([
"interpolate",
["linear"],
["zoom"],
@@ -133,8 +146,10 @@ def testInterpolateListByZoom(self):
0.3,
18,
0.6
], QgsMapBoxGlStyleConverter.Numeric, conversion_context, 2).expressionString(),
"CASE WHEN @zoom_level > 10 AND @zoom_level <= 15 THEN scale_linear(@zoom_level, 10, 15, 0.1, 0.3) * 2 WHEN @zoom_level > 15 AND @zoom_level <= 18 THEN scale_linear(@zoom_level, 15, 18, 0.3, 0.6) * 2 END")
], QgsMapBoxGlStyleConverter.Numeric, conversion_context, 2)
self.assertEqual(prop.expressionString(),
"CASE WHEN @zoom_level > 10 AND @zoom_level <= 15 THEN scale_linear(@zoom_level, 10, 15, 0.1, 0.3) * 2 WHEN @zoom_level > 15 AND @zoom_level <= 18 THEN scale_linear(@zoom_level, 15, 18, 0.3, 0.6) * 2 END")
self.assertEqual(default_val, 0.2)

def testParseExpression(self):
conversion_context = QgsMapBoxGlStyleConversionContext()

0 comments on commit c0ebc68

Please sign in to comment.
You can’t perform that action at this time.