IF THEN statements in ArcGIS Field Calculator
I'm not sure if this would be best handled in vbscript or python, and unfortunately I'm less than novice at both so I'll take any help I can get from the exchange.
I'm trying to take my streets layer and remove the "type" field for a certain set of street names. Any street named "Avenue A", "Avenue B", Avenue C" and so forth would need the "TYPE" field emptied out. But all other streets and street types would remind.
A little explanation as to the why may help you understand my plight a little better: We have an emergency response software at my municipality that uses both the prefix, subname, and type to display the streets data, as well as, track calls and statistics for each street. All streets display properly except for the "Avenue A" type streets which show up "Avenue A AVE". Any other AVE type street displays properly, for example: Baltic AVE, Crawford AVE, etc. But the "Avenue (letter)" streets will not with the "TYPE" field populated. Hope this makes sense.
So what I am needing is a field calculator script that will search for any [SUBNAME] "AVENUE #" street and blank out the [TYPE] field for it. I tried a little SQL with my inadequate knowledge of VBscript to generate the below (no it didnt work):
IF [SUBNAME]="AVENUE *" THEN REPLACE([TYPE],"AVE","")
use (replace [type], "AVENUE %", "") There are many ways to use field calculator.
Trying to use the if then statement in my experience you are likely going to want to try to learn some regex. It is very powerful for finding string or numeric sequences and allowing you to make changes.
Short of needing more than what you already eluded in your question you can easily accomplish with some more basic field calculator statements.
Here are some links to old and new esri documentation for both python and vbscript that will help make you a guru.
An old vbscript help pdf but still useful.
A 9.3 doc for using a gp tool.
10.3 document with fundementals
10.3 field calculator examples
Or to not make the change in the field permanently but to change how you label.
Define separate sql statements for the types and label each differently.
Well, you could do what you want with a correctly written field calculator expression, but I would recommend instead a 2 step method to fix the problem. Make a selection of the features needing edited and then run a very simple field calculator expression to fix those selected features, this way you can manually review which features will be affected before changing them.
First, create a selection of the roads you need to edit. In ArcMap you would open the Select by Attribute and do a SQL Selection using the Like operator. For example, do something like: SUBNAME LIKE 'Avenue %' (you may have to adjust the expression slightly depending on your data source, but you should be able to use the built-in expression builder to replicate a similar expression). If using Model Builder or a python script, you may have to run the Make Feature Layer GP tool first, and then run Select by Attribute. Once the features are selected, I would recommend manually reviewing the selected and not-selected features to make sure all desired ones are included and only desired ones are included.
Then, run field calculator on the TYPE field, using Python as the field calculator expression language, and for the expression just type the word
None(first N is capitalized, that's important). That will put the field to . If you don't want Nulls or your field/data-type doesn't support Nulls (such as a shapefile), then you can instead have the expression be "".
When you run field calculator against a layer with a sub-set of features selected, the field calculator will only edit the selected features. So selected all the applicable roads, and then simply calculate them all to the new desired value. No complex calculator expressions necessary.