What the !field1! if !field1! else '' expression does is evaluate if the corresponding value of !field1! exists or not. If there is a value, then it keeps that value and adds it to 'Comment'. However, if there is no value (it evaluates to None) it will keep and empty string ('').
Here is an illustrative example:
Suppose the value of field1 is 'ABC001' for ...
This should work:
you create a line (make_line), starting at the point with your fixed coordinates (created with the command make_point(25.906543, -80.546800)) and ending at the second point, defined with $geometry as the current point of your layer. You then measure the length with the ...
You want the length to be displayed along your line and auto-update as you draw or edit the line? In this case, simply label your line with the expression length($geometry)
For the attribute table, use Creating a Virtual Field - when you create a new field with the field-calculator, select virtual field and set the expression you like - e.g. length($...
With python parser try:
def updateValue(field1, field2):
if field1 is None:
When you call the function you must use the real fieldname enclosed in !!
All geoprocessing tools honour selections. So simply select the rows where column 1 has the NULL values then run a field calculate and select the field you want the values to come from. As you have a selection only those rows will be updated. Absolutely no code required.
I finally figured out the problem. Was my index position. So this type of code works as well.
if (val[0:2] == "07"):
return re.sub ('07','39', val)
elif (val[0:2] == "10"):
return re.sub ('10','39', val)
elif (val[0:2] == "11"):
You can use a list comprehension to index and replace the first characters for each item in a list. For example:
test = ["3901","1420","2270","3985"]
new = ["39" + i[2:] if i[:2] != "39" else i for i in test]
EDIT: or to your changed request it should be:
array_length( array_agg( "RoadMntnc", "RoadMntnc"))
You can use the following expression to count the segments of a line or polygon:
An update for QGIS 3 of the crystal clear answer by @Steven Kay
You can get around this by creating a short python script in the function editor tab of the expression editor.
Go into the function editor tab in the expression editor
Create a new function ("New file" button)
paste the following into the code window. You may get indentation errors, so ...
Field Calculator only iterates once over the table so you cannot generate a frequency list and apply it with one pass. If this is a task you need to repeat I'd recommend using a python script outside Field Calculator or creating a model in ModelBuilder.
If you don't find a solution using Field Calculator try python window with collections.Counter and two da.UpdateCursors:
from collections import Counter
fc = 'C:\data.gdb\my_riks' #Change
values = 'KKOD' #Change
dupinst = 'dupinst' #Change
totcount = 'totcount' #Change
cnt = Counter()
with arcpy.da.UpdateCursor(fc, [values, dupinst]) as ...
Instead of using regex_match you can define a new function and use that custom function to extract the unique text without using regular expression.
You can use the following function to do the job:
from qgis.core import *
from qgis.gui import *
def unique_text(text_field, feature, parent):
split_text = ...
Because you used regexp_matches() instead of regexp_match() it will return an array (notice the square brackets in Output preview at the bottom of your field calc screenshot) as opposed to a string. You need to convert the array to a string by wrapping your expression in array_to_string().
You also need to escape any backslashes so \1 needs to be \\1.
Use the normal Field Calculator and in the Formula option, select Pre-calculated Value. Then use the expression:
'(' || @a || ' + ' || @b || ') / 2'
where the parameters @a and @b are the fields you assign when running the model. This is read by the field calculator as a string version of:
(A + B) / 2
Since you are dealing with point data, the results of both expressions of $x and x($geometry) are identical. Same applied to $y and y($geometry). I tested the behavior of both expressions on my point grid data and I found that both them produced the same results as you can see below:
In the table above the coordinates in X and Y fields were calculated using ...