Expressions and default values

classic Classic list List threaded Threaded
4 messages Options
Tension Tension
Reply | Threaded
Open this post in threaded view
|

Expressions and default values

Hi!

I wondering, is there a way to use some default values for expressions, if selected attribute is not found?

Let's say i have a StringExpression for TextSymbol text like [_text_], so if i apply this style to feature with attribute table without _text_ attribute, feature will render with text "_text_", but i need, for example set some default text, or even leave it empty.

Same with NumericExpressions, let's say i have some InstanceSymbol with scale property, which may depend on attribute, for example: [_height_] / 50. And i want it to be just 1 (for whole expression, or 50 for missing attribute _height_, if its possible, so whole expressions becomes 1), if there is no _height_ attribute.

Can i achieve this somehow?

Thank you.
gwaldron gwaldron
Reply | Threaded
Open this post in threaded view
|

Re: Expressions and default values

Tension, You can use scripting for that. Instead of the "[attr]" notation, you can use JavaScript to resolve the expression. The built-in global "feature" accesses the current feature. Example:
<style type="text/css">
    default {
        extrusion-height: getHeight();
    }
</style>

<script language="javascript">
  <![CDATA[
    function getHeight() {
        if ("height" in feature.properties)
            return feature.properties.height;
        else
            return 1;
    }
  ]]>
</script>
Glenn Waldron / Pelican Mapping
Tension Tension
Reply | Threaded
Open this post in threaded view
|

Re: Expressions and default values

This post was updated on .
Thanks, i'll try this.

Just to clarify: programmatically, i should set this script in StyleSheet (setScript method)?

Can it hit rendering performance? In other words, i can render a lot of feature nodes / feature model layers, can i set this script with basic js functions to every StyleSheet, even if styles within this StyleSheet don't use js functions as expression or it's better to do some preparation?
gwaldron gwaldron
Reply | Threaded
Open this post in threaded view
|

Re: Expressions and default values

Correct. The osgearth_features.cpp has an example you can reference as well.

It won't affect rendering performance - script evaluation is a load-time operation.
Glenn Waldron / Pelican Mapping