Tag Info

Hot answers tagged

10

If you wanted to do this and not bother with the code block, just express this as a logical Python statement in the expression parameter. {!field2! if !field1! is not 0 else !field1!}


10

In the code block window, using python as parser: def my_calculate(field1_value, field2_value): if field1_value == 0: return 0 else: return field2_value In the expression window: my_calculate(!field1!, !field2!) This could also be done by using the "Select Features by Attribute" and selecting all the features with 0 in field 1, calculating ...


8

There is actually an ArcGIS tool specifically for this task: Convert Time Field. You don't even need to use the Field Calculator. Just specify the format of the source date/time field and your desired output format and the tool will create a new field and populate it with the converted values. It can be used for converting dates, times, or a combination of ...


6

The zfill string method, along with some basic string splitting/joining, will let you do this very slickly: def update_text(old_text): new_text_parts = [num.zfill(4) for num in old_text.split('_')] new_text = '_'.join(new_text_parts) return new_text If you would prefer not to bother with a function in the Field Calculator code block, you can ...


4

Could you do something like: Pre-Logic Script code: def mathFunction(y1, y2, x1, x2): return math.Atan(math.fabs(y2-y1) / math.fabs(x2-x1)) * (180/math.pi) Field = (on the bottom)...populate with your fields for y1,y2,x1,x2 mathFunction(!FIELDY1!, !FIELDY2!,!FIELDX1!,!FIELDX2!) See Python doc on the math module.


4

Am I correct in assuming you wish to carry out the operations in sequence i.e. the final value in the field comes from the !field![:9] operation? If so, i would have the code block as: def field_calc(field): str1 = field.replace("+","") str2 = "_".join([num.zfill(4) for num in str1.split('_')]) str3 = str2[:9] return str3 Then call it ...


3

Move your "is None" check up to the first evaluation. if Btype is None: return 'Olive' elif (Btype.startswith('Fig')): return 'Grape' elif(Btype.startswith('Lychee')): return 'Mango' Basically, if Btype is null, you can't do .startswith, etc. on it.


3

Using Field Calculator, you are able to create an expression for the calculation of each row in a given field. There are two languages that you can use to write the expression: VB and Python. At the top of the calculator, you'll see where you can choose which language should be used to interpret the code you have entered. If you select VB, you need to ...


3

You're using an expression type of 'PYTHON', but to support the calculation you're trying, you need to use 'PYTHON_9.3'.


3

Use the built-in str.translate(table[, deletechars]), but with None for the table argument (requires at least Python 2.6). E.g.: 'N. First St., Unit #1'.translate(None, '.,#') shows: 'N First St Unit 1' Or for ArcGIS' calculator, this can be a one-liner: !yourField!.translate(None, '.,#')


3

Your function is not returning anything. I've modified your code to return the value of aspect_m60. # Calculate Field import arcpy # Set environment settings arcpy.env.workspace = "c:/W/Sik" # Set local variables inTable = "Point" fieldName = "aspect_m60" expression = "getCalc(!aspect!)" codeblock = """def getCalc(aspect): if (aspect < 60): ...


3

It is much more intuitive, in my opinion, to work with Cursors (rather than trying to emulate the field calculator in a script) for this type of problem. This is how you would port the problem over to an Update Cursor: import arcpy # The input FC fc = "C:/W/Sik.gdb/yourFC" with arcpy.da.UpdateCursor(fc, ["aspect", "aspect_m60"]) as cursor: for row in ...


2

The data in the attribute table will not change when you reproject the geometries. After reprojection, you have to add new columns with $x and $y values. These will be in the coordiante system at the moment when you add them. You can delete the old columns with coordinates if they are not useful to you anymore.


2

Since it looks like you are running this in a separate arcpy script (as opposed to inside the Field Calculator), there may not be a need to use Code Blocks. Maybe better to use an UpdateCursor... From what I can tell from the code, you are testing to see if the field you need to fill is either Null or 0, and if it is, fill the field with the value from the ...


2

I have met this problem before. Try to use """ instead of " above the expression. Otherwise, try this one : code_block = """def output(MTH_BRNT,YR_BRNT,YSLB): month = str(month_val) month_current = int(month)/n month_fire = int(MTH_BRNT) year = str(high_year)/n year_current = int(year) year_fire = int(YR_BRNT) Mc = (12*(year_current-1)) + ...


2

Field Calculator. Right Click on Total_acci, select Field Calculator. You'll get an option to build a query. Add the other three fields together. If those are not an option, you may need to change the data type using ArcCatalog.


2

If all values are the same, just open your attribute table. choose the column you wish to modify, add the value you want and click on update. You can also look at a useful plugin called MMQGIS. Where you can export import CSV files....


2

You can use the Python setdefault() method and an UpdateCursor to do this. import arcpy fc = r'C:\temp\your_shapefile.shp' # create an empty dictionary convert = {} # Start an update cursor and add unique ID based on unique string value with arcpy.da.UpdateCursor(fc, ["species", "code"]) as cursor: for row in cursor: row[1] = ...


2

You are trying to set the field within the code block, when actually you need the code block to return the value you're looking for. If you just just add return aspect_m60 after the else block, it should work fine. Think of the code block as a place to write functions whose results can be used in your field calculator expression.


2

You can't sum columns using the Field Calculator (although it would be useful in certain situations). To be able to sum columns and obtain a value, you can use: QGIS function - Basic Statistics (Vector > Analysis Tools > Basic Statistics) QGIS Plugin - GroupStats (Plugins > Manage and Install Plugins) QGIS Plugin - Statist (Plugins > Manage and Install ...


2

I'm not really familiar with VB, but you could use this Python code: def check(value): if value == 0: results = 0.0001 else: results = value return results And for the Expression put: check(!Lung_lunc!) Make sure to change the Expression Type to Python.


2

Sorry, but the layers are not in the same CRS. The Catchments is in an Albers Equal area projection (units-meters) and the other two are in Haartebeesthoek94, with degree as units. So your area calculations are in square degrees. If you reproject the transmissivity and intersect polygons to Albers Eq Area (with the "Save As..." option in QGIS) then ...


1

Are you looking for atan2 ? It's precisely meant to deal with this signs mess. https://docs.python.org/2/library/math.html#math.atan2


1

You can use the toolbox for the spatial join (processing toolbox > search: join by location) and save the result to a temporary layer. The temporary layer is only saved in your QGIS project. Then you can join the temporary layer to the layer A. You can find the processing toolbox in view > panels > toolbox or right click over the toolbars.


1

Just try in python in filed calculator as below- "WP_" + str(!OBJECTID!) N.B. The field going to be populated also needs to be string/text


1

In your code: def EXPCODE(field_value): if field_value = "S4DNN2" return field_value "3.5" if field_value = "H2BNN2" return field_value "5.0" should probably be: def EXPCODE(field_value): if field_value == "S4DNN2": return 3.5 elif field_value == "H2BNN2": return 5.0 but if this is coming from the ...


1

This is easy to do assuming you mean that you want the Centroid coordinates of each parcel currently being calculated. Use !SHAPE.CENTROID.X! and !SHAPE.CENTROID.Y! as parameters to define the expression and then include them in the def as the east and north variables. If this is somehow trying to use two separate feature classes then you need to use a ...


1

If you are using Field Calculator, you can create a new field of text type and calculate the field to be: def updateValue(textField): newval = textField.split('_') if len(newval[0]) == 3: newval[0] = '0' + newval[0] if len(newval[1]) == 3: newval[1] = '0' + newval[1] total_val = newval[0] + '_' + newval[1] return total_val Remember to ...


1

Right click your field that you want to calculate values into, click Field Calculator. Select Python parser. FieldName = !AAAA![:3] Will bring the first 3 numbers/letters into your new field. Then dissolve on that field by the value. This should give you groups of your values. If that is what you are looking for.


1

And another way. Python Pre-Logic Script Code: def remove(text): for char in [".", ",", "#"]: if char in text: text = text.replace(char, "") return str(text) Bottom part: remove(!yourField!)



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