1

Table

I am fairly new to using python in field calculator and could use some help. I want to populate SitusRdType with RD or AVE or LN or ST or DR depending on the road type. I also want to just have the street name in SitusSt. Any thoughts on how to go about this?

PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Dec 30, 2014 at 17:42
2
  • What software are you suing (QGIS, ArcGIS, other)? Commented Dec 30, 2014 at 18:00
  • I am using ArcGIS 10.2.2 Commented Dec 30, 2014 at 18:07

1 Answer 1

6

In general, you need a pre-defined list of the road types, since not all your values end in a road type. For ArcGIS, the following is a simple example of populating SitusRdType:

In the Field Calculator, check that Python is set as the parser and check Show Codeblock. In the Pre-Logic Script Code box:

def setRdType(myInput):
 lstRdTypes = [' RD', ' AVE', ' LN', ' ST', ' DR']
 for rdType in lstRdTypes:
 if rdType in myInput:
 return rdType

In the SitusRdType = box: setRdType(!SitusSt!) You may want to add 'HWY ' to the list if you want that as well. There's probably a more elegant way, but off the top of my head this should work.

To eliminate the same list from SitusSt:

def rplc(myInput):
 s = myInput
 lstRdTypes = [' RD', ' AVE', ' LN', ' ST', ' DR']
 for rdType in lstRdTypes:
 if s.endswith(rdType):
 s = s.replace(rdType, '')
 return s

In SitusSt =: rplcType(!SitusSt!)

Again, probably more efficient ways to do it, but this should work. Always try on a copy of data first.

answered Dec 30, 2014 at 18:18
5
  • The first part worked great once I changed [SitusSt] to !SitusSt! thank you! The second part just returned null values. Commented Dec 30, 2014 at 18:27
  • Sorry, forgot a return statement. Bad cold, fuzzy thinking. Edited 2nd function, should work now. Commented Dec 30, 2014 at 19:43
  • It looks like it should work but now it just returns the same values Commented Dec 30, 2014 at 19:52
  • Hmm, code looks good. Edited for a slightly different approach, try that. You could also skip the codeblock and do a replace() on each road type separately, but that's obviously a less efficient method. Commented Dec 30, 2014 at 20:04
  • Edited for slightly better method, checking endswith(). Commented Dec 30, 2014 at 20:20

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.