Skip to content

Instantly share code, notes, and snippets.

@MarcoDuiker
Created February 21, 2018 13:53
Show Gist options
  • Save MarcoDuiker/3f9895dfc8e2978fca3715852f635e34 to your computer and use it in GitHub Desktop.
Save MarcoDuiker/3f9895dfc8e2978fca3715852f635e34 to your computer and use it in GitHub Desktop.
Qgis Function grabbing a value from a list
from qgis.core import *
from qgis.gui import *
@qgsfunction(args='auto', group='Custom')
def values_from_list(string, number, feature, parent):
"""
Takes the n-th value from a list in a field where the list is
written like: (3:value1, value2,value3). Such lists are often a
result of importing gml. <br />
<h3>Syntax</h3>
values_from_list(<i>string</i>, <i>number</i>)
<h3>Arguments</h3><br/>
<i>string</i> The string containing the list in format like (3:value1, value2,value3) <br/>
<i>number</i> The index number of the value in the list to return
<h3>Examples</h3>
<pre>values_from_list("text", 1)</pre>
"""
components = string.strip('()').split(':',1)
values_list = components[1]
try:
return values_list.split(',')[number - 1]
except:
return None
@MarcoDuiker
Copy link
Author

MarcoDuiker commented Oct 29, 2018

When using a function like this for openbareruimtelabel from the BGT, then the following regex based expression grabs the last value as it should to use the right rotation for a label:

to_real(regexp_substr(  "hoek"  , '([-]?\\d+(\\.\\d+)?)[)]'))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment