Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature] New custom function to get the colors from categories used #288

Merged
merged 2 commits into from
Aug 20, 2022

Conversation

ghtmtt
Copy link
Owner

@ghtmtt ghtmtt commented Jun 8, 2022

Adds a new custom function to the expression engine that can be used to get the colors of the category used. It works only for the Categorized renderer.

@Gustry @nyalldawson can I have your bless here? :)

ezgif com-gif-maker

@katagen
Copy link

katagen commented Jun 10, 2022

Sorry if I misunderstand, but if you allow the @symbol_color variable, it should do the job? Even if you are not in "categorize class"... or am I wrong?
#233

@nyalldawson
Copy link
Collaborator

"@symbol_color" would be a good approach to use here, but you'd effectively have to re-create the QGIS c++ code which is used to calculate this. The way QGIS itself handles symbol_color is deep within the vector layer rendering/labeling code, and isn't exposed for reuse in PyQGIS.

The way @ghtmtt has calculated the symbol color is not too bad -- it could be generalized using layer.renderer().originalSymbolsForFeature( feature ) so that you don't need to manually handle all the different renderer types.

Depending on how far you want to go, you could either stick with the custom expression function approach, or instead inject a symbol expression context scope into the expression context used to evaluate the values via something like:

symbols = layer.renderer().originalSymbolsForFeature( feature )
symbol_scope = QgsExpressionContextUtils.updateSymbolScope( symbols[0], QgsExpressionContextScope() )
expression_context.appendScope( symbol_scope )

@ghtmtt
Copy link
Owner Author

ghtmtt commented Jun 13, 2022

@nyalldawson great to hear that my solution is fine :)
if I use the following:

symbols = layer.renderer().originalSymbolsForFeature( feature )

I get this error:

TypeError: QgsFeatureRenderer.originalSymbolsForFeature(): not enough arguments

but if I add the missing argument QgsRenderContext as:

context = QgsRenderContext()
symbols = layer.renderer().originalSymbolsForFeature( feature, context)

the QGIS crashes. It happens both in the custom expression and also with a custom standard python script.

@nyalldawson
Copy link
Collaborator

@ghtmtt

You probably need to wrap that in startRender/stopRender calls:

context = QgsRenderContext()
renderer = layer.renderer()
renderer.startRender(context, layer.fields())
symbols = layer.renderer().originalSymbolsForFeature( feature, context)
renderer.stopRender(context)

@ghtmtt ghtmtt merged commit 8638168 into master Aug 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants