NYGeog

Geography, GIS, Geospatial, NYC, etc.

Tuesday, January 25, 2011

Python Field Calculator: counting characters in a string using .count()

Let's say you have a field with many items in it-say you have a shapefile of states and a field with the names of all the major rivers in that state and the river names are separated by a dash. Ex. [Nile R.-Mississippi R.-Hudson R.-Snake R.-Potomac R.-Yellow R.]

If you wanted to count the number of rivers in each state in a new field you could simply count the delimiter, the dash (-) and add 1 (because each dash separates one river from another it'll be one less dash always than the number of rivers).

Go to you field calculator, select Python and use this expression;

(!riversfieldname!.count('-'))+1

Your calculating field should be short integer in this example and should return a value of 6 in this example.

Thursday, January 13, 2011

Field Calculator Python adding strings and integers with + instead the old &

Another Python tidbit from a post I made in someone else's thread: http://forums.arcgis.com/threads/18861-Field-Calc-VB-to-Python-help-%28-will-trade-work%29?highlight=python+string+integer

So I have a Unique ID field but its a text field with characters in the string (9X123456) and the create Lines from X,Y tool requires the ID field to be a number. I came up with 123456 & 01 (X is now a number) & 9 ie 9X123456 becomes 123456019. I can do this pretty easy in VB Script in the Field Calculator in a temp text field.

[GEOID] & "019"

I say this expression in Python;

This looks like it works

long(str(!GEOID!) + '019')

As Chris Snyder responded:

The Python concatenate operator is actually '+', so for example:

print "my" + " " + "dog"
'my dog'

So the expression would be:

long(str(!GEOID!) + '001' + '009')

Another simple way of "integerizing" your string unique id:

Run a frequency on your string field using the Frequency tool. Then join the frequency table to the original table. The OBJECTID field (a long integer BTW) of the frequency table gives you a 1-to-1 with the text version unique id.

You can also do something similar using cursors via Python...

Thursday, January 6, 2011

Python Equivalent to Right( ) in VB for ArcGIS Field Calculator

I'm starting to use Python in the Field Calculator and I just found this out after an hour or so of frustration using rstrip() and rsplit() realized its even easier though not as obvious.

So lets say I wanted to remove the leading character of a 8 digit string in my [UID] field

In VB I'd say:

Right([UID], 7)

In Python I'm saying

!UID![1:]

You may put numbers after the colon [1:4] as well to manipulate the return string lengths and where the string is pulling those lengths from, other than that I don't know but it may help if someone knows the name of this function - if so please comment below?

I posted this to the forum: http://forums.arcgis.com/threads/20620-Python-Field-Calculator-Equivalent-of-Right%28-%29-or-Left%28-%29-in-VB-Field-Calculator?p=66668#post66668