Tag Info

Hot answers tagged

14

You probably have a divide-by-zero error. Field Calculator definitely cannot divide when zero is the denominator, and sometimes will even throw errors with zero as the numerator. To get around this, click the "Select by attributes" button in the Attribute Table (top left corner, third from the left). Then type in this formula: "Asian" <> 0 AND ...


13

I have found that in 10.0 Field Calculator is quite weird. But I've managed to get it work. The main idea is to enclose field name with single quotes. Example. let suppose we have fields text1 and text2. Rather than Calculating field text2 with expression !text1!, which probably will fail, try this one: '!text1'. As you see I am using single quotes here. ...


12

You will want to concatenate the two fields together. To do this in ArcMap you can use the VB Script function "&". So using your example, the calculation would be [FROM] & "-" & [TO] You could also use Python syntax, in which case your code would be: str(!FROM!) + "-" + str(!TO!) With Python you want to be sure to enclose the fields in ...


12

You could include the Python dictionary of state abbreviation:full-name pairs here states = { 'AK': 'Alaska', 'AL': 'Alabama', 'AR': 'Arkansas', ... } as the Codeblock in Field Calculator, and then use something like the following as the actual calculation: !City! + ", " + states[!State!] I should add that you could perform this ...


12

I would use regular expressions. If you put the following in the code block section of the calculator it will return a string that has all digits stripped, no matter were they are: import re def strip_digits(s): return re.sub("\d+", "", s) This can then be called from the calculate box as: strip_digits(!column_name!) If you want to replace ...


11

Try using $perimeter The feature is there, just not exposed via the UI. Note: I'm currently working on a better UI then the current one that will expose all the possible functions. See http://hub.qgis.org/issues/3488


11

You want the in method detailed here: http://docs.python.org/reference/expressions.html#in Swap if Name=="*Emergent*": with if "Emergent" in Name:


10

Assuming you consider a space as a separator: Python: !myField!.split(" ")[0].title() # Proper Case !myField!.split(" ")[0].upper() # Upper Case VBScript: UCase(Left([myField],1)) & LCase(Mid([myField],2,InStr([myField]," ")-1)) ' Proper Case UCase(Mid([myField],1,InStr([myField]," ")-1)) ' Upper Case


10

For this you can use UpdateCursor, which opens the feature class or table and steps through each record (row) incrementally. The script below works on this test data +-----------------------+ | Time| Home_Away|Trip | +-----|----------|------+ | 1 | 0 | <nul>| | 2 | 1 | <nul>| | 4 | 1 | <nul>| | 5 | 0 | ...


9

I think Python is running into cases where dict[index] and tot are both integer values (even though they are defined as doubles in the Shapefile). Because of integer division you are ending up with 0 as the answer. For example: >>> dict = { 1:1 } >>> tot = 5 >>> dict[1]/tot 0 To fix it, simply cast the first parameter to a ...


9

You could create a Python function for the field calculator using the logic below (using the first record as an example: >>> a = "EW 140 Rd MF 71" # your primary field >>> b = str(71.6) # your secondary field >>> a.replace(a.split(" ")[-1], b) #replace the last entry in the list 'EW 140 Rd MF 71.6' Or as a def, this ...


9

If you are using version 10.1 and you are sure you want to get rid of every instance of double quotes you can use: !testing!.replace("\"","") If anyone knows why this works in 10.1 and not 10.0 I would be interested. Here are the results entry from my run.


8

At least four things needed correction: You need to use return rather than print You need to use == rather than = on your if and elif statements (thanks @DanPatterson) Take care with your indentation - your print (to become return) statements don't seem to line up return statement was written in one place as retun [sic] There are examples here.


7

As Jason Scheirer commented, you can't use !field! inside of pre-script logic. Pre Script Logic: def demo(value, arg): return value * arg avg_miles = demo(!hshld_2000!,10) Note: A "trick" you can use to simplify this mess... You can use the interactive Command window in ArcMap to create and test python code (such as defining methods). Once you're ...


7

I don't know how to do this with Field Calculator, but I do know a workaround. Run Summary Statistics in ArcToolbox with COUNT on the [name] field. This will create another table where each unique [name] entry will have next to it the number of times it appears in the original layer. Then Join this table back to your original layer, with the [name] ...


7

What you want to do in the codeblock for Python is define a function, and then call the function using your attributes as parameters as follows: def comparison(left,right): if left < right: return left else: return right output = comparison(!MIN_LEFT!,!MIN_RIGHT!) Then, all you need in the calculation is output, as you already ...


7

Here is a non script way to do it. Start edit session on you layer. Open up table and highlight field column. Select Table Options drop down arrow and select Find & Replace. Select Replace tab and enter Find what: ", select Replace all (you will have to click the Replace All button twice).


7

For the Project coordinate reference system, you have to choose a projected CRS, which uses real metres as units. Openlayers Plugin uses Google mercator, which is only corresponding to real meters at the aequator. The further north you come, the more distorted the length units are (look at Greenland in Openstreetmap, it is not so big in reality). By ...


6

if !ET_Angle! > 100 and !ET_Angle! < 200 To be more complete, put this in the codeblock: def LeftOrRight(value): if value > 100 and value < 200: return "LEFT" else: return "RIGHT" And in the calculation area, just put: LeftOrRight(!ET_Angle!)


6

I don't know what you are querying (eg shapefile, geodatabase), but have a look at http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//00s50000002t000000.htm specifically the section on "The NULL keyword" EDIT Adding a field to a shapefile, using the field calculator on this field, then using the following as the code block (Python parser) def ...


6

I never understand why people even try to mess with CalculateField_management within a Python script instead of just doing their updates within an UpdateCursor. Not only do you not have to worry about escaping characters and writing Python functions within Python strings (shudder), you can update multiple fields at once. It's very easy with the newer arcpy ...


6

If you force it you would need to convert from numeric to text. What you want to do is change the display. Open the field properties dialog by right clicking on the field name in the attribute table.. click the ellipses next to numeric... then in the number format dialog select the number of decimals you desire. then show thousands seperator check box.


6

Your question is somewhat similar to one that was asked recently. As you can see, declaring functions is a rather different affair in python, and you are not required to denote types (such as double) ahead of time. You should look into the Calculate Field examples in the resource center to give you a good idea of what your syntax looks like. Important ...


5

Your need to convert your SQL into VBA or Python for the Field Calculator in ArcMap http://webhelp.esri.com/arcgiSDEsktop/9.3/index.cfm?TopicName=Making_field_calculations ArcGIS 10 Users can also use Python scripts in ArcGIS Field Calculator This is a cheaper option than buying ArcSDE and a database that supports SQL.


5

The Python string manipulation methods should give you access to all of the features you are looking for: http://docs.python.org/library/stdtypes.html#string-methods To extract words from a string, take a look at the usage of: str.split([sep[, maxsplit]]) For capitalizing your word(s), take a look at the usage of: str.upper()


5

Take a look at the section on slicing in the Python tutorial. You can grab a range of characters from a string using slicing syntax, e.g. D = int(x[1:2]). For seconds, try S = float(x[5:]). This will grab all the characters starting at index 5 to the end of the string, in the case that you have variable length values for seconds. The !FieldName! syntax is ...


5

In your ArcToolbox tool properties I'd set the parameter to a FeatureLayer/FeatureDataset with MultiValue=Yes and use some code like the following to get the filename: for shp in featureclasses.split(';'): dsc = arcpy.Describe(shp) filename=dsc.CatalogPath #unicode is returned


5

Sounds like some simple string formatting would do the trick for you: >>> "%.2f" % 3.99999 '4.00' >>> or, with the number stored in a variable: >>> j = 3.999999 >>> "%.2f" % j '4.00' >>> This could easily be wrapped up in a Field Calculator function.


5

if !myField! == 'NULL' will not test for a null value. That will test a for a string equal to 'NULL'. Use: if !myField! is None: To accomlish your task, define a python function in the pre-logic code block and use that function in the expression window. The following code sample pulls the first token from a '-' delimited string.


5

If you're using Python in ArcGIS you may be using an = for comparison instead of an == Your code should be similar to the below, if you use Python: Pre-Logic Script Code - Check "Show Codeblock" def ifBlock(pArea,pcArea): if pArea == pcArea: return 1 else: return 0 Then in the actual code: ifBlock(!P_AreaFieldName!,!PC_AreaFieldName!)



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