Tag Info

New answers tagged

2

Lets start with the field calculator. it should look like these 2 screenshots. When I calculate fields inside of an Update cursor (update_cursor) it looks more like this. Here I have a SearchCursor (row is set to searchCursor.next) and an update Cursor (row2 is set to update_cursor.next). This is actually slightly different since I am using values in a ...


4

Here's a way to replace all the nulls with zeroes: import arcpy with arcpy.da.UpdateCursor(r"...gdb\test.gdb\test",["A", "B", "C"]) as cursor: for row in cursor: a_value = row[0] if row[0] else 0 # Use 0 when "A" is falsy b_value = row[1] if row[1] else 0 # Use 0 when "B" is falsy row[2] = a_value + b_value ...


3

Disabling the "on-the-fly" projection setting should yield you the correct results when performing analytics via the Field Calculator: Project > Project properties > CRS This bug has been known in early versions of QGIS but has mostly been resolved since QGIS 2.1.


6

If all your data matches the format you posted, and all the numbers you want are in between the two dashes in the text (and those are the only dashes), you can use the split() method. Here's the code: def extractText (yourTextField): partialText = yourTextField.split("-") return partialText[1] That will split the input into 3 parts: the parts ...


0

I wanted to provide another alternative to my previous answer I am more of a fan of using the python interactive window and an UpdateCursor for most of my attribute table changes. When I wrote my above answer, I wrote it in the interactive window and translated it to the field calculator. My interactive code was: replaceList = ["DRG", "2014"] ...


1

I am assuming you mean field value that needs changed and not your layer name since you mentioned Field Calculator Same logic could be applied in a script for actual layer names too though Ok.. I understand you and have it down. (1) Make sure your parser is marked for python. (2) In the Pre-Logic Script Code: def customReplace(fieldValue): repList = ...


2

I am not completely sure to understand the aim of your script, but here are some comments def customReplace(fieldV): # you must indent after your def repList = ['-', ' ', '.'] #remove the ; rep = '_' b=fieldV for old in repList: #I guess you want to replace the values in replist b = b.replace(old, rep) #here you need return, not ...


0

I'm not sure what you're trying to do exactly. Is Layer supposed to be a feature class or shapefile, or a field? One, all the lines in the function should be indented. Two, you can't have a semicolon end a line. Three, Layer is probably a reserved word, don't use it. Four, repList isn't in Layer; that loop will either error out or give you unexpected ...


1

I can confirm some strange results. Let's take a simple area without islands and coasts, like Va1. The measurement tool says it is 111x155km large, so 17224 km² should be ok. It is calculated to 17099 km² by the identify tool and the field calculator. Va2 is rather complicated, because the hole of Iceland has lots of vertices. With laea projection, I get ...


5

I'm guessing customers and total_customers are integer fields, and the equation is using integer math, yielding the rounded-down result (0 * 100) in every case except where customers == total_customers (1 * 100). Use this equation to get decimal calculations: "float(!customers!) / float(!total_customers!) * 100"


1

If you are using ArcGIS Desktop, I think this link will help you with calculating area values when using data in a geographic coordintate system: http://www.gislounge.com/calculating-polygon-area-in-arcmap/


0

Since you're doing the calculation from within a Python script anyway (as opposed to the Field Calculator GUI or a Model Builder model), I would rewrite the code using an arcpy.da.UpdateCursor to avoid the awful code-within-a-string that's required for a CalculateField_management() codeblock: #import system modules import arcpy import math from arcpy ...


4

This function should work in field calculator (if your text is always after the numbers with a empty space): def rev(x): try: nonValue = x.split(' ')[1] value = ''.join(i for i in x if i.isdigit())[::-1] return value + ' ' + nonValue except ValueError: pass Output e.g.:


12

Something like this should work: def reverse(s): items = s.split() digs = ''.join(i for i in s if i.isdigit()) dr = digs[::-1] return ' '.join(map(None, items)).replace(digs, dr) >>> reverse('321 test') '123 test' @mnpeterson brought up a good point about assuming where the numbers are...My post above would string all digits ...


0

The vb script version is basically the same as the Python version posted by radouxju in terms of its logic. The code below also handles Null values in the Cities field. If IsNull([Cities]) Then Output = [Township] ElseIf [Cities] <= " " Then Output = [Township] Else Output = [Cities] End If Expression: Output


1

If you are using a geodatabase, you can use Attribute Assistant to do this if the sequential number is assigned regardless of the prefix letters, i.e., you do not restart at 1 for each set of prefix letters, but simply append the prefix letters to a number that is sequential and unique for each record in the entire table. Attribute Assistant is available as ...


0

You can't. A field is a defined data type - double, text, float, etc. You can't have a field that's a mixed data type You can have python data structures that consist of mixed data types, such as a list, but this isn't the same as a field in a table. If this was just for labeling, there are several ways to do this, but as far as field calculation, no. Edit: ...


4

using python field calculator, you can use str(!lat!) + !yourStringField! or you can use the format() function for more control (in this case, 2 decimal places). """lat is {:.2f}, long is {:.2f} for city {}""".format(!lat!,!long!,!citiyName!) of course the field where you do the concatenation must be a text field


1

here is an example with the Python parser code block def fillBlank(city, township): if (city == ""): return township else: return city expression (name of fields between ! !) : fillBlank(!cities!, !township!)


0

Could you do it where you first spatial join to the city polygons, then do an attribute select to find where this column is blank, and then spatial join to the township polygons? Most Arc tools respect selections and only operate on selected records.


0

This task is fairly easy if you use spatial databases. You can use Spatialite database for your needs. Use spatialite_gui to create spatialite database and import your shp-files into it (there is a 'Load shapefile' button at the toolbar). Once you have your data inside spatialite database you just need to run a query to get needed information. Assuming that ...


2

The brackets have to be inside the string, since otherwise the brackets will create a Python list. When Python passes a string that has brackets inside of it as the expression, Python does not interpret the brackets, but the Field Calculator interprets the bracketed expression within the string as a field name. Use the code that follows: for table in ...


3

You have to have a square bracket surrounding the "SUM_" thing Trying change expression = ["SUM_" + str(filename[0:-4])] to expression = "[SUM_{0}]".format(str(filename[0:-4]))


3

As you've discovered python truncates integers that are divided - this may include floating point fields with integers stored in them, sometimes 'duck typing' isn't your friend! To force the issue and produce a floating point answer from a division float your number first: float(!GRIDCODE!)/100000.0 It is best to do this always when dividing otherwise ...


3

I think this should do what you want: def concat(*args): sep = "_" nonnull_args = [str(arg).strip() for arg in args if arg] # Filter NULLs good_args = [arg for arg in nonnull_args if arg] # Filter blanks retval = sep.join(good_args) return retval The args just get passed through a couple filters and joined by sep at the ...



Top 50 recent answers are included