Skip to content

Commit

Permalink
raster renderer edge cases (#206)
Browse files Browse the repository at this point in the history
  • Loading branch information
KatKatKateryna authored Jun 30, 2024
1 parent 1127ea1 commit 822b1cb
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 40 deletions.
87 changes: 57 additions & 30 deletions speckle/converter/features/feature_conversions.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,38 +305,65 @@ def get_raster_colors(
plugin,
):
list_colors = []
if rendererType == "multibandcolor": # RGB

vals_range0 = rasterBandMaxVal[0] - rasterBandMinVal[0]
vals_range1 = rasterBandMaxVal[1] - rasterBandMinVal[1]
vals_range2 = rasterBandMaxVal[2] - rasterBandMinVal[2]
if rendererType == "multibandcolor":

Check warning on line 308 in speckle/converter/features/feature_conversions.py

View check run for this annotation

Codecov / codecov/patch

speckle/converter/features/feature_conversions.py#L307-L308

Added lines #L307 - L308 were not covered by tests

# mock values for R,G,B channels
vals_red = [0 for _ in rasterBandVals[0]]
vals_green = [0 for _ in rasterBandVals[0]]
vals_blue = [0 for _ in rasterBandVals[0]]

Check warning on line 313 in speckle/converter/features/feature_conversions.py

View check run for this annotation

Codecov / codecov/patch

speckle/converter/features/feature_conversions.py#L311-L313

Added lines #L311 - L313 were not covered by tests

vals_range_red = 1
vals_range_green = 1
vals_range_blue = 1

Check warning on line 317 in speckle/converter/features/feature_conversions.py

View check run for this annotation

Codecov / codecov/patch

speckle/converter/features/feature_conversions.py#L315-L317

Added lines #L315 - L317 were not covered by tests

val_min_red = 0
val_min_green = 0
val_min_blue = 0

Check warning on line 321 in speckle/converter/features/feature_conversions.py

View check run for this annotation

Codecov / codecov/patch

speckle/converter/features/feature_conversions.py#L319-L321

Added lines #L319 - L321 were not covered by tests

val_na_red = None
val_na_green = None
val_na_blue = None

Check warning on line 325 in speckle/converter/features/feature_conversions.py

View check run for this annotation

Codecov / codecov/patch

speckle/converter/features/feature_conversions.py#L323-L325

Added lines #L323 - L325 were not covered by tests

# get band index for each color channel
bandRed = int(layer.renderer().redBand())
bandGreen = int(layer.renderer().greenBand())
bandBlue = int(layer.renderer().blueBand())

Check warning on line 330 in speckle/converter/features/feature_conversions.py

View check run for this annotation

Codecov / codecov/patch

speckle/converter/features/feature_conversions.py#L328-L330

Added lines #L328 - L330 were not covered by tests

# assign correct values to R,G,B channels, where available
for band_index in range(len(rasterBandVals)):

Check warning on line 333 in speckle/converter/features/feature_conversions.py

View check run for this annotation

Codecov / codecov/patch

speckle/converter/features/feature_conversions.py#L333

Added line #L333 was not covered by tests
# if statements are not exclusive, as QGIS allows to assugn 1 band to several color channels
if band_index + 1 == bandRed:
vals_red = rasterBandVals[band_index]
vals_range_red = (

Check warning on line 337 in speckle/converter/features/feature_conversions.py

View check run for this annotation

Codecov / codecov/patch

speckle/converter/features/feature_conversions.py#L335-L337

Added lines #L335 - L337 were not covered by tests
rasterBandMaxVal[band_index] - rasterBandMinVal[band_index]
)
val_min_red = rasterBandMinVal[band_index]
val_na_red = rasterBandNoDataVal[band_index]
if band_index + 1 == bandGreen:
vals_green = rasterBandVals[band_index]
vals_range_green = (

Check warning on line 344 in speckle/converter/features/feature_conversions.py

View check run for this annotation

Codecov / codecov/patch

speckle/converter/features/feature_conversions.py#L340-L344

Added lines #L340 - L344 were not covered by tests
rasterBandMaxVal[band_index] - rasterBandMinVal[band_index]
)
val_min_green = rasterBandMinVal[band_index]
val_na_green = rasterBandNoDataVal[band_index]
if band_index + 1 == bandBlue:
vals_blue = rasterBandVals[band_index]
vals_range_blue = (

Check warning on line 351 in speckle/converter/features/feature_conversions.py

View check run for this annotation

Codecov / codecov/patch

speckle/converter/features/feature_conversions.py#L347-L351

Added lines #L347 - L351 were not covered by tests
rasterBandMaxVal[band_index] - rasterBandMinVal[band_index]
)
val_min_blue = rasterBandMinVal[band_index]
val_na_blue = rasterBandNoDataVal[band_index]

Check warning on line 355 in speckle/converter/features/feature_conversions.py

View check run for this annotation

Codecov / codecov/patch

speckle/converter/features/feature_conversions.py#L354-L355

Added lines #L354 - L355 were not covered by tests

list_colors = [

Check warning on line 357 in speckle/converter/features/feature_conversions.py

View check run for this annotation

Codecov / codecov/patch

speckle/converter/features/feature_conversions.py#L357

Added line #L357 was not covered by tests
(
(255 << 24)
| (
int(
255
* (rasterBandVals[0][ind] - rasterBandMinVal[0])
/ vals_range0
)
<< 16
)
| (
int(
255
* (rasterBandVals[1][ind] - rasterBandMinVal[1])
/ vals_range1
)
<< 8
)
| int(
255 * (rasterBandVals[2][ind] - rasterBandMinVal[2]) / vals_range2
)
| (int(255 * (vals_red[ind] - val_min_red) / vals_range_red) << 16)
| (int(255 * (vals_green[ind] - val_min_green) / vals_range_green) << 8)
| int(255 * (vals_blue[ind] - val_min_blue) / vals_range_blue)
if (
rasterBandVals[0][ind] != rasterBandNoDataVal[0]
and rasterBandVals[1][ind] != rasterBandNoDataVal[1]
and rasterBandVals[2][ind] != rasterBandNoDataVal[2]
vals_red[ind] != val_na_red
and vals_green[ind] != val_na_green
and vals_blue[ind] != val_na_blue
)
else (0 << 24) + (0 << 16) + (0 << 8) + 0
)
Expand Down Expand Up @@ -379,7 +406,7 @@ def get_raster_colors(
except Exception as e:

Check warning on line 406 in speckle/converter/features/feature_conversions.py

View check run for this annotation

Codecov / codecov/patch

speckle/converter/features/feature_conversions.py#L406

Added line #L406 was not covered by tests
# log warning, but don't prevent conversion
logToUser(

Check warning on line 408 in speckle/converter/features/feature_conversions.py

View check run for this annotation

Codecov / codecov/patch

speckle/converter/features/feature_conversions.py#L408

Added line #L408 was not covered by tests
f"Raster renderer of type 'paletted' couldn't read the renderer class values, default renderer will be applied: {e}",
f"Raster renderer of type '{rendererType}' couldn't read the renderer class values, default renderer will be applied: {e}",
level=1,
func=inspect.stack()[0][3],
plugin=plugin.dockwidget,
Expand Down Expand Up @@ -431,7 +458,7 @@ def get_raster_colors(
except Exception as e:

Check warning on line 458 in speckle/converter/features/feature_conversions.py

View check run for this annotation

Codecov / codecov/patch

speckle/converter/features/feature_conversions.py#L458

Added line #L458 was not covered by tests
# log warning, but don't prevent conversion
logToUser(

Check warning on line 460 in speckle/converter/features/feature_conversions.py

View check run for this annotation

Codecov / codecov/patch

speckle/converter/features/feature_conversions.py#L460

Added line #L460 was not covered by tests
f"Raster renderer of type 'paletted' couldn't read the renderer class values, default renderer will be applied: {e}",
f"Raster renderer of type '{rendererType}' couldn't read the renderer class values, default renderer will be applied: {e}",
level=1,
func=inspect.stack()[0][3],
plugin=plugin.dockwidget,
Expand All @@ -450,7 +477,7 @@ def get_raster_colors(
else: # greyscale
if rendererType != "singlebandgray":
logToUser(

Check warning on line 479 in speckle/converter/features/feature_conversions.py

View check run for this annotation

Codecov / codecov/patch

speckle/converter/features/feature_conversions.py#L478-L479

Added lines #L478 - L479 were not covered by tests
f"Raster renderer type {rendererType} is not supported, rendering the raster in greyscale",
f"Raster renderer type {rendererType} is not supported, default renderer will be applied",
level=1,
func=inspect.stack()[0][3],
)
Expand Down
16 changes: 6 additions & 10 deletions speckle/converter/layers/symbology.py
Original file line number Diff line number Diff line change
Expand Up @@ -606,36 +606,32 @@ def rendererToSpeckle(
redBand = renderer.redBand()
greenBand = renderer.greenBand()
blueBand = renderer.blueBand()
redContrast = (
redMin
) = (
redMax
) = (
greenContrast
) = greenMin = greenMax = blueContrast = blueMin = blueMax = None
redContrast = redMin = redMax = greenContrast = greenMin = greenMax = (

Check warning on line 609 in speckle/converter/layers/symbology.py

View check run for this annotation

Codecov / codecov/patch

speckle/converter/layers/symbology.py#L609

Added line #L609 was not covered by tests
blueContrast
) = blueMin = blueMax = None
try:
redContrast = (
renderer.redContrastEnhancement().contrastEnhancementAlgorithm()
)
redMin = renderer.redContrastEnhancement().minimumValue()
redMax = renderer.redContrastEnhancement().maximumValue()
except:
except: # AttributeError: 'NoneType' object has no attribute 'contrastEnhancementAlgorithm'

Check warning on line 618 in speckle/converter/layers/symbology.py

View check run for this annotation

Codecov / codecov/patch

speckle/converter/layers/symbology.py#L618

Added line #L618 was not covered by tests
pass
try:
greenContrast = (
renderer.greenContrastEnhancement().contrastEnhancementAlgorithm()
)
greenMin = renderer.greenContrastEnhancement().minimumValue()
greenMax = renderer.greenContrastEnhancement().maximumValue()
except:
except: # AttributeError

Check warning on line 626 in speckle/converter/layers/symbology.py

View check run for this annotation

Codecov / codecov/patch

speckle/converter/layers/symbology.py#L626

Added line #L626 was not covered by tests
pass
try:
blueContrast = (
renderer.blueContrastEnhancement().contrastEnhancementAlgorithm()
)
blueMin = renderer.blueContrastEnhancement().minimumValue()
blueMax = renderer.blueContrastEnhancement().maximumValue()
except:
except: # AttributeError

Check warning on line 634 in speckle/converter/layers/symbology.py

View check run for this annotation

Codecov / codecov/patch

speckle/converter/layers/symbology.py#L634

Added line #L634 was not covered by tests
pass
layerRenderer.update(
{
Expand Down

0 comments on commit 822b1cb

Please sign in to comment.