Tag Info

Hot answers tagged

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 ...


9

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!}


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

In QGIS you can use abs("fieldname") to turn them into always positive values.


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

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

Using Python would open up some more elegant solutions, but you can do this entirely in ModelBuilder with the use of a couple of temporary tables. The model would look something like this (note that you can right-click on any process step and rename it): The Add Field operation adds a new column called [Normalized_Value] to your existing polygon table. A ...


2

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


2

Using field calculator (or, arcpy.management.CalculateField() if you want): In the code block: def fix_matrix(field_value): if field_value.endswith("A") or field_value.endswith("B"): return field_value[:-1]+"AB" else: return field_value In the expression: fix_matrix(!MATRIX!) + !PLANT! + !PCT! + !SITE!


2

You could use the Python parser in the Field Calculator and construct this function. This part goes in the pre-logic. Make sure to replace the feature classes with the one from your system. The feature class table called "Temp_Table" is a temporary table and must reside in a geodatabase. It will be deleted as the script completes. def normNum(num): ...


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

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

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

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

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....


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

There might be a better way but this works: MD = '2367 2356 2352 2347 2345 2337 2331 2304' values = [int(v) for v in MD.split()] first = values[0] - 10 values = values[1:] values.insert(0, first) [abs(values[i-1]-values[i]) for i in range(1,len(values) - 1)] and if that version is a scary you can break it out so it's not as bad: newvalues = [] for i in ...


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!)


1

in the python code block: import re def strip(value): return re.sub('[.,#]','',value) and then in the field calculator: strip(!fieldname!)


1

Use python to calculate values in new field: filter(lambda x: x not in ".,#", !yourField!)


1

If it is a shapefile or any other type of file, try saving it to a new file, maybe the file itsself is somehow corrupt.



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