# Tag Info

11

One possible expression would be: CASE WHEN RIGHT("name", 1) IN('a', 'o') THEN substr("name", 0, length("name")-1) ELSE "name" END In addition to your right("name",1), substr() part works to remove 1 letter from the original length of the "name" field.

9

We can use arrays for that: array_find( array_agg( \$id, "ReportID_2" ), \$id ) + 1 With array_agg we have an array of all id grouped by the ReportID_2 values. Then, array_find gets the position of the current id in the previous array, starting with the position 0. Finally, we add 1 to get the positions from 1.

9

Roundness is simply the ratio of the area of the circle with the same length as the polygon to the polygon area (area(Circle)/area(Polygon)) or vice versa (it depends). Open Field Calculator, select field type, run the following expression: (\$perimeter * \$perimeter / (4*pi()) ) / \$area For a circle-shaped polygon, it returns ~1; for a square, ~1,27; for a ...

8

There is not a layer_crs, but we can use the 'crs' layer property: layer_property( @layer , 'crs' ) Sample output:

8

You can use in to check for membership: if FIELD in veg: return 'Vegetable' elif FIELD in fruit: return 'Fruit' For bonus points use sets, which offer better performance for this: veg = {'Carrot', 'Celery', 'Broccoli'} fruit = {'Apple', 'Banana', 'Grape'}

8

Try something like: concat(to_string(floor(duration)),':',to_string(round((duration*60) % 60, 0)))

7

You can do this by making 2 loops: the first on the layers of your project and the second on the features of each layer. # Project layers project_layers = QgsProject.instance().mapLayers().values() # Loop on each layer for layer in project_layers : layer.startEditing() layer.dataProvider().addAttributes([QgsField("area", QVariant.Double)]) ...

7

An alternative solution to using written Python code would be to use the Graphical modeller to create a small script. Just set a Vector-Layer as input and add the field calculator as step with \$area as a formula. Something like this: You can then run the script in Batchmode and do the same operation for several layers at once.

7

Try the following expression: attribute(get_feature(@layer_id,'id',"fed by"),'C') The get_feature call finds the feature in the current layer whose id is equal to the current feature's fed by. Note double quotes on fed by since the value of this field for the current feature should be evaluated in the expression. However id has single quotes since it is a ...

7

You may use this expression in your field calculator. if( @row_number % 90=0,90,@row_number % 90)

7

You need to use a nested replace() function such as replace(replace('£10,000.00','£',''),',','') You can nest as many replace() as you want.

7

You can do both replacements using a single function call: 1) using regex_replace, where you would look for the two characters of interest (inside square brackets): regexp_replace("field_name",'[£,]','') or 2) the map variant of replace, where the map is a series of key-value, the key being the string being searched for and the value being the replacement ...

7

This expression should do what you need: array_find(array_foreach(array_distinct(array_sort(array_agg("Date"||'§'||"info"))),regexp_replace(@element,'([0-9]+)-+([0-9]+)-+([0-9]+§)','')),"info")+1 Note, that § sign is used as key-sign here to split the date from the id. How it works: similar to my previous one you have linked: ...

7

You have made a basic mistake, you have embed the variable you called field directly within a text expression, i.e. within the "", so it sees it as some text not as a variable. Do something like this: expression = "!Classific!.replace(!Classific!,'" + field + "')" shapes2=arcpy.CalculateField_management(shapes1,'Classific',...

6

You can use this script. First, select a the layer, then run the script. It adds the result as a memory layer. You should save the result as a new file manually or using a code. input_layer = iface.activeLayer() max_area_x = ["max_area_1", "max_area_2", "max_area_3"] for i in range(len(max_area_x)): # OR for i in [0, 1, 2] input_layer = processing.run("...

6

You can use the CASE WHEN statement. I will give you an example, adapt it. CASE WHEN "VALUE1" < 0.01 THEN 2 WHEN "VALUE1" >= 0.01 AND "VALUE1" < 0.1 THEN 3 ELSE 4 END I applied this formula to the VALUE2 field, here is the result:

6

I suppose your date value is in your_date column. floor(day(age("your_date", '2018-01-01')) / 15) The expression above gives 0 for the first 15 days, 1 from 16th to 30th days, ... Thee age function calculates an interval between the to dates, the day function extracts the number of days from the interval, the floor function returns the nearest smaller ...

6

You could use regex_replace function regexp_replace Returns a string with the supplied regular expression replaced. Syntax regexp_replace(input_string,regex,replacement) Arguments input_string the string to replace matches in regex The regular expression to replace. Backslash characters must be double escaped (e.g., "\s" to match a white space ...

5

You can use use the Field Calculator to create a new field using the size of the polygons to sort your objects. This is the expression that you can use: array_find(array_sort(array_agg("field_name")), "field_name")+1 In your case it will be: array_find(array_sort(array_agg("ha")), "ha")+1

5

You can use the attribute or attributes function in your expression to build the logic you want. The fragment attribute(\$currentfeature,'layer1colname') will return the value of that attribute in the current feature, if it exists, and NULL if not as opposed to an error. Ditto attributes()['layer1colname']. You could then use CASE WHEN and/or if ... is NULL ...

5

You can try the following expression: with_variable('my_array', array_agg( \$geometry, group_by:="field1", filter:="field2">0, order_by:=x(\$geometry)), CASE WHEN array_find(@my_array,\$geometry)=-1 THEN to_string("field1") ELSE to_string("field1") || to_string(array_find(@my_array,\$geometry)+1) END) a short ...

5

IF you are working on QGIS Field Calculator, try: length("ID") - length(replace("ID", '|', '')) + 1 SAGA polygon self-intersection tool will store the ids in a new field "ID" which can be used in this expression to designate the field. replace("ID", '|', '') will remove | character from the "ID"field, so ...

5

Your task can be done in the following way: If you want to access an existing attribute, that’s possible using <attribute_name>. The feature geometry can be accessed using \$geom. I recommend you to check this article https://anitagraser.com/2013/04/20/advanced-python-field-calculator/

5

Why not not just sort and select, or filter, on the values you want to update and use "update selected" or "update filtered"?

5

In your query you forgot to include the upper edge for your range of values. It is possible with using an AND-Operator. So, try: CASE WHEN "HHO_mean" > 5.7 AND "HHO_mean" <= 6.4 THEN 1 WHEN "HHO_mean" > 6.4 AND "HHO_mean" <= 7.1 THEN 2 WHEN "HHO_mean" > 7.1 AND "HHO_mean" <...

5

You can use arrays for this purpose: array_find(array_distinct(array_agg("Name")),"Name") Which returns for example this id column: So how does it work: First an array of all Name values is created, then every duplicate value gets deleted. At the end the index of the current name is searched in the array and the index used as id. PS: ...

5

You just need to double the problematic apostrophe so the expression become : 'Christy''s Yard'

5

If the layer is already georeferenced in QGIS then you can do something like this with the Field Calculator: Expression for longitude: x(transform(\$geometry, @YOUR_CRS_ID, 'EPSG:4326')) And for latitude: y(transform(\$geometry, @YOUR_CRS_ID, 'EPSG:4326')) If the layer is not georeferenced and you want the fields to be calculated based on the X and Y fields ...

4

Your print() is inside a SearchCursor. In your SearchCursor you also define the variable si for your CalculateField. So you write in CalculateField_management for all features the last si from the SearchCursor. SearchCursor can not modify rows. Try it with an arcpy.da.UpdateCursor(): # not tested fc = "streams.shp" new_field = "Sindex" arcpy....

4

If not getting very accurate area calculations does not bother you (or if you do not want to play with different options in the field calculator as mentioned by some of the answers and comments above), in the current version of QGIS (i.e., QGIS 3.12) there is an easy way to deal with this. Go to "Project >> Properties >> General" and change the units for ...

Only top voted, non community-wiki answers of a minimum length are eligible