# Tag Info

7

The value that is written into the attribute table depends on what field calculator returns. In your case: CASE WHEN "Z" = 0 THEN "Z" = rand(1,100) END means that it compares Z and 0, and if that's true, it compares Z and a random value from 1 to 100, which has to be false because Z is 0. That's why it writes 0 (=False) into the fields which were 0. And ...

6

In the field calculator, show Codeblock. In the pre-logic script code box: import random def randnum(): return random.random() In the expression box: randnum() Result in a new float field:

3

In your if line you need to change your or to an and in the last part. if f4 == f5 and (f1 == f2 or f1 == f3) and (f6 != f7 and f6 != f8): This is because with f6 != f7 (6 != 5) it returns 1, even though f6 != f8 fails (6 = 6) in the second part. With the OR there it returns true if one of them is true. With an AND it would return true only if both are ...

3

You need to use the Field Calculator or Calculate Field tool to calculate your new field equal to your old field. Make sure that your new field is defined the same as the old field. Otherwise it cannot be considered to be a copy.

3

This python function implements the logic you described in your question: def calculate_X(A,B,C,D): if A == 0 and B == 0: X = calculation_1(A,B,C,D) elif A == 0 and B != 0 and C != 0 and D != 0: X = calculation_2(A,B,C,D) elif B == 0 and A != 0 and C != 0 and D != 0: X = calculation_3(A,B,C,D) else: X = ...

3

Try to change your code to def calc(Wert1, Wert2, Wert3): if (Wert1 == None): return Wert2/Wert3 else: return Wert1

2

Use an if to check the value of each field, and skip that value if it's one of your "no data" values. You can add other "no data" values to the no_data list to skip those as well. Expression: skip_nodata(!FOR_FISH!, !MINING!, !FARM_EMP!) Code Block: def skip_nodata(val1, val2, val3): no_data = [-98, -99] x = 0 if val1 not in no_data: ...

2

You donĀ“t need to bring your calculated field in the return part of the function: Try: if [Area_Ha] <=70 then [Area_Ha]^1.22 * 2.85 else ([Area_Ha]+9.97)*7.1 end if

2

In the FieldPyculator, you will need to: Insert your function as a Global expression and remove the AP parameter as you can choose from the GUI which field you want updated. Slightly change your Field expression by specifying fields using <Mag> instead of !Mag!. This is the global expression I used (not sure what Vector_Mag was so changed these ...

2

You can do it using the following formula in the Field Calculator: regexp_substr("Field_Name",'(\\d+|\\d+.+)') It will give you the following results: 25 2 26A 125 147A Even if you have text with a name 'Text 123456789ABCDEF', the output will be: 123456789ABCDEF

2

I would use regexp_substr instead of strpos so in your example something like: regexp_substr("FolderPath",'.*/') The first item ("FolderPath") is your column name (enclosed in "), the second is a standard python regular expression. There is a regexp howto available to help you out.

1

I think you need to add from qgis.utils import qgsfunction into your code. You can also remove the other imports as they're not necessary. So something like this should work: from qgis.utils import qgsfunction @qgsfunction(args="auto", group='Custom') def sp_split(value1, feature, parent): return value1.split(",")[2]

1

This works for me: def setindex(height): dIndexInterval = 50 dCont = height i = dCont / dIndexInterval if ( dCont / dIndexInterval) - i == 0: return 1 else: return 0 And then setindex( !ELEV! ) in the bottom window where ELEV is the name of your elevation field. Edit The code given in the link looks buggy, as far as I can tell it will always ...

1

It looks like you put the defined function into the returned field. The message says if you have multi-line you have to cut the code and activate the Show codeblock and paste the code into the "Pre-Logic" Script Code space. The last line should be used to return the defined function exist in the "Pre-Logic" Script Code. Check "Using code blocks" in the ...

1

Field calculator for Python d={} def GroupOrder(groupID): if groupID in d: d[groupID]+=1 else: d[groupID]=1 return d[groupID] --------------------------- GroupOrder( !locality! ) Change !locality! to relevant field

1

Since you are using the field calculator on that given field you can just use this with codeblock enabled. Tested with Volume field as a float and it works: if [Area_Ha]<=70 then result = 2.85 *([Area_Ha]^1.22) elseif [Area_Ha] >70 then result = 7.1* ([Area_Ha]+9.97) end if Then: Volume_De = result In python I think you're not allowed ...

1

In field calculator: right("column", strpos("column" ,' ')) Should do the job.

1

You can vary parameter in expression below, depending on what distribution is required: def aRand(): return arcgis.rand("Normal 2 2") --------------------------------------- aRand()

1

Be sure to check "Code Block": def calc_X(A,B,C,D): if A == 0 and B == 0: return calculation_1(A,B,C,D) elif A == 0 and B != 0: return calculation_2(A,B,C,D) elif B == 0 and A != 0: return calculation_3(A,B,C,D) else: return calculation_4(A,B,C,D) Then: X = calc_X(!A!, !B!, !C!, !D!)

1

Example of code in field calculator / calculate field: Field Name: X Expression: calc1(!A!,!B!,!C!,!D!) Expression type: PYTHON_9.3 Code Block(/Pre-Logic Script Code): def calc1(a,b,c,d): if a==0 and b==0: return a+b+c+d+1+2+3+4 elif a==0 and b<>0: return a-b-c-d else: return a+b-c+d

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