Last active
March 18, 2022 18:20
-
-
Save Korto19/151366c44fc52369ea95b396de062614 to your computer and use it in GitHub Desktop.
A QGIS field calculator expression to fill down a list with non null value from a column
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from qgis.core import * | |
from qgis.gui import * | |
mem = 'NULL' | |
@qgsfunction(args='auto', group='Custom', handlesnull=True) | |
def fill_down(value1, feature, parent): | |
""" | |
Restituisce un valore dal campo specificato, | |
dove il valore del campo della riga precedente | |
viene propagato ai campi con valori Null successivi | |
<p> | |
Returns a value from the specified field, | |
where the value of the field of the previous row | |
it is propagated to subsequent null fields | |
<h2>Example usage:</h2> | |
<ul> | |
<li>fill_down("Dato") -> Fill_Data</li> | |
<table> | |
<thead> | |
<tr> | |
<th>Dato</th> | |
<th>Fill_Data</th> | |
</tr> | |
</thead> | |
<tbody> | |
<tr> | |
<td style="text-align:right">a</td> | |
<td style="text-align:right">a</td> | |
</tr> | |
<tr> | |
<td> </td> | |
<td style="text-align:right">a</td> | |
</tr> | |
<tr> | |
<td> </td> | |
<td style="text-align:right">a</td> | |
</tr> | |
<tr> | |
<td style="text-align:right">b</td> | |
<td style="text-align:right">b</td> | |
</tr> | |
<tr> | |
<td></td> | |
<td style="text-align:right">b</td> | |
</tr> | |
</tbody> | |
</table> | |
</ul> | |
Opera in base all'id dei record | |
<p> | |
Operate based on the ID of the records | |
""" | |
global mem | |
res = str(value1) | |
if res in ('NULL',''): | |
res = mem | |
else: | |
mem = str(value1) | |
return res |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment