Tag Info

Hot answers tagged

5

A bit explanation why the OP's function is not working: f1 = a string list= a list When you are iterating over the list, for each member of list you will get either positive or negative response. So your list has nine members; you will get nine responses(+ and -). Then what the calc function will return, whole nine responses or one? Of-course python ...


4

If you plan to do this often you might be better off doing this as a function so you can reuse it. Open the Field Calculator Click on the Show Codeblock check box Enter the code block included below You can click on the Save button to save the code and load it later on with the Load button Here is a screen capture as an example. It's the same result ...


4

This should just work as a normal string in check def myCal(reportnumber, spatialjoin): if reportnumber in spatialjoin: return 1 else: return 0 or even one like: def myCal(reportnumber, spatialjoin): return 1 if reportnumber in spatialjoin else 0


4

If you're using 2.8 or later, you can use the Python function builder. This code will do the calculation. I don't think you need to use Qt objects for this. from qgis.core import * from qgis.gui import * from datetime import datetime import time @qgsfunction(args=2, group='Python') def timedifference(values,feature,parent): ''' call this function ...


4

As stated in the tool documentation for Calculate Field: Python expressions can use the geometry area and length properties with an areal or linear unit to convert the value to a different unit of measure (for example, !shape.length@kilometers!) These expressions are not usable with points or individual coordinates. Fortunately, you can use other ...


3

In this case, the same thing could be accomplished with !FieldName!.lstrip("0").


3

It's possible without using a python function, with a little bit of hacks: minute( age( todatetime('2000-01-01 10:18:00'), todatetime(2000-01-01 10:16:30') ) ) will return "1.5". To break it down, "age" returns the difference between two datetimes as an interval type. This needs to be wrapped in the "minute" function to extract the length of this ...


3

You could just check if your field value is in the remove list: def remove_zero(field): remove_list = ['01ST', '02ND', '03RD', '04TH', '05TH', '06TH', '07TH', '08TH', '09TH'] if field in remove_list: field = field.replace('0','') return field else: return field Then you'll want to do: remove_zero(!FIELDNAME!)


3

You can use function editor tab in the field calculator and make your azimuth function like this: from qgis.core import * from qgis.gui import * @qgsfunction(args="auto", group='Custom') def azimuth(x1,y1,x2,y2,feature,parent): p1 = QgsPoint(x1,y1) p2 = QgsPoint(x2,y2) a = p1.azimuth(p2) if a < 0: a += 360 return a Once ...


3

Use the Right Function Right( [textField], n ) (where n is the number of characters) eg: Right(“abcdefg”, 3) = “efg” This is pretty handy for learning how to use the field calculator http://www.esri.com/news/arcuser/0405/files/fieldcalc_1.pdf


3

so if it helps - i think it would look something like - def calc(f, fn, f1, f1n): #note colon here # everything within function is indented f = str(f) f1 = str(f1) f = f.replace(" ", "") fn = fn.replace(" ", "") f1 = f1.replace(" ", "") f1n = f1n.replace(" ", "") if f+fn == f1+f1n: # each return of if or elif ...


3

Recap: Your problem was just that you were passing the wrong field to the function. Think about it this way: Field calculator produces a new value for each row a given field (the field shown just above the expression where it says "Type =") based on the expression. In the expression, you are able to reference other fields by using !Field_name!. By using ...


3

open CalculateField tool for 'Field Name' parameter specify: DI for 'Expression' parameter specify: x(!Max_GRIDCODE!) for 'Code Block' parameter specify the code below def x(v): if v > 2: return 5 elif v > 1.5: return 4 elif v > 1: return 3 elif v > 0.5: return 2 else: return 1


2

Alright great I managed to solve it, I hope this answer helps others. while True: try: YesNo = raw_input("Do you need to change a field name (Yes/No): ") except ValueError: print("Your Input is invalid") continue if YesNo == "Yes": VB = raw_input("Enter VB Expression to change Your Field: ") ##This is the expression I use: ...


2

You could simply use the Coalesce function which returns the first non-NULL value from the given columns (fields). Insert this expression into the Field Calculator as a new field: coalesce( "Field_1", "Field_2", "Field_3" ) Below is a simple example: If a feature has columns with multiple values (eg. 1, NULL, 3), then you may need to include ...


2

QGIS has created a function called format_number to address this. You simply do format_number(12345,0) to get 12,345.


2

Try this: arcpy.CalculateField_management(var_sel, "VR_Rate", '!' + VRField_Calc + '! + 2', "PYTHON_9.3") To search for a specific field before calculating: findField = 'some_field_name' for field in VRField: if field.name == findField: arcpy.CalculateField_management(var_sel, "VR_Rate", '!' + field.name + '! + 2', "PYTHON_9.3")


2

You can't use punctuation marks in variable names in python. Your function should probably read more like this: def calc(LONG_x): if LONG_x >= -113.90550 and LONG_x <= -113.90559: return "05" else: return "0" You can then pass in !LONG_x! as an argument to the function. eg. result = calc(!LONG_x!)


1

Expression type needs to be PYTHON_9.3 This code worked for me: from arcpy import * fc = r"C:\test\test.gdb\test" fld = "testfld" shapeFldName = Describe (fc).shapeFieldName CalculateField_management (fc, fld, "!{}!.firstPoint.Z".format (shapeFldName), "PYTHON_9.3") Happy ...


1

Your code logic seems fine, you just have to change the bottom script to: Type = change(Value) You are getting a "N/A" as a value on all of your columns because you are running it on the [Type] column of data where none of the values are 1-5 as you've expected, instead of running that function on the [Value] column, where those numbers(i'm assuming) are.


1

i haven't tested it, but you may not need the wildcards at all - if you convert the value in the spatial join field to a list - something like def myCal(reportnumber,spatialjoin): #spatialjoin = spatialjoin.split(';') if reportnumber in spatialjoin: return(1) else: return(0) heck, may not even need to convert spatial join to a ...


1

Based on the screenshot you've provided, it looks like you can do this really easily by using the OID field. Those integers match the photo numbers. code block: def makeName(oid): n = 4 - len(str(oid)) return "PHOTO_{0}{1}.jpg".format(n*"0",str(oid)) expression: makeName(!OID!) Test this on a new string field to make sure it works, because it ...


1

The code below demonstrates how to get this to work using code very similar to that in the question, but I recommend also reviewing the detailed explanation that follows in order to understand why it works: import arcpy dmyString = "01.07.2015" d,m,Y = dmyString.split(".") dmyDate = datetime.datetime(int(Y),int(m),int(d)) epoch = datetime.datetime(1899, ...


1

I was experiencing a similar issue with my iterating model. I was calculating a field in a series of tables to equal the names of the tables. I was left with blank fields. Since the data type is string, the expression needed to include quotes around the substituted variable, such as '%Name%' as opposed to %Name%. With this change, the model worked as ...



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