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

Friday, December 17, 2010

Replace((Right([ID],3)), "_", "") in ArcGIS Field Calculator

It seems like for the longest time I've used multiple temp fields for parsing values out of non-standard length text or integer unique ID values. For example, in a text field, unique (shown here in a python list format for no reason in particular other than I am learning python) ID's like;

list = [12345_N1, 12345_B25, 6789_N1, 6789_B25, 6789_B50]

Normally, I would create a temp field and say;

Right([list], 3)

create a temp2 field and select the '_N1' values and calculate that separately with;

Right([list], 2)

and then combine the two things together into one field in a haphazardly but workable way.

Granted, the more complex a statement is, the more chances for error, but once the syntax is set and ingrained in your vocabulary its amazing how much work you'll save in the future. So a more elegant solution to the problem stated above with just one field and one calculation is;

Replace((Right([ID],3)), "_", "") *

So what we are doing here is using the Replace function, which is like;
replace([FIELD_name],"",""

So instead of using [FIELD_name] I'm saying remove these stupid underscores from our selection of the 3 characters from the right side of the field and replace them with "" or nothing.

*Replace (see http://resources.arcgis.com/content/kbase?fa=articleShow&d=20119)

According to Neocartography, the Replace function "kicks ass" (see http://neocartography.wordpress.com/2009/12/22/kicking-ass-with-the-replace-function-in-arcmap-field-calculator/).

Monday, December 6, 2010

From Streetsblog: NYC Agencies Take Home EPA’s Top Honors For Smart Growth

From Streetsblog:
NYC DOT Commissioner Janette Sadik-Khan and Planning Commissioner Amanda Burden were down in D.C. yesterday to accept the Environmental Protection Agency’s annual “Overall Excellence in Smart Growth” award. The EPA highlighted four PlaNYC-related initiatives for recognition: NYC DOT’s Street Design Manual, the city’s Active Design Guidelines, City Planning’s Food Retail Expansion to Support Health (FRESH) program, and the zoning amendment that passed in 2009 requiring new apartments and offices to include bike parking.

MORE...

Wednesday, December 1, 2010

ModelBuilder-Advanced Technologies ESRI UC Video


http://video.esri.com/watch/91/modelbuilder-advanced-technologies

Here is a great video from the ESRI User Conference 2010 on Advanced Modelbuilder capabilities. It covers iterations which is awesome for me because its something I'd really like to leverage in my models.