How to add "text" in attribute table?

4119
7
Jump to solution
10-26-2018 07:59 AM
RudiniRudini
New Contributor II

Hi.. 

i want ask a question about this..

see the attachment about my shp data..

so i have the shp attribute like in the attachment..

i want to fill the "name" field using the formula like: shape_length < 50 = A, 20 < Shape_length < 500 = B and shape_length > 500 = C..

is it possible to do that?? 

can someone help me to explain it?

Thanks

0 Kudos
1 Solution

Accepted Solutions
DanPatterson_Retired
MVP Emeritus

Well you changed mine.

  • Make sure your field you are calculating into is a text field
  • You need to set it to a python parser
  • Your code has no indentation... it must be 4 spaces (not tab) exactly as I have shown
    • lines 2, 3, 5, 7 and 9 … 4 spaces indented
    • lines 4, 6 and 8 …. 8 spaces
    • change the !Shape_Length! back to 'a' (no quotes)
  • it is func(a) so it can be a generic function... 
  • So I don't have to open up Arc*… the case of the shape_length field is important... it must be exact when you pass it to func( the fieldname within ! marks)

I think I covered everything, just follow mine..

View solution in original post

7 Replies
deleted-user-qpvAI3Fo0MKR
Occasional Contributor III

To do this manually, you'll use Select by Attributes. Select all records where SHAPELENGTH <= 50, then use the Field Calculator to calculate "A" into your text field. Repeat the same workflow modifying your SHAPELENGTH query and using the Field Calculator.

0 Kudos
RudiniRudini
New Contributor II

thanks for your reply Mr Schwartz

but i want to fill automatically and i just found using VBA script in field calculator with IF statements but i have another trouble with multiple IF statments

i put this code in field calculator

Dim Name
if [SHAPE_Length] < 50 then
Name = "A"
elseif 50 < [SHAPE_Length] < 1000 then
Name = "B"
elseif [SHAPE_Length] > 1000 then
Name = "C"

end if

but after it run, the result only take Name < 50 = A, Name >50 = B..

i think i put the wrong code

0 Kudos
DanPatterson_Retired
MVP Emeritus

don't use vba

switch to python

I rearranged the logic since a Shape_Length field will never be <null> so you only have to check for < 50 ( ie 'A' ) and > 1000  ( ie 'C') then everything else has to be 'B'

your code isn't indented properly... see

 /blogs/dan_patterson/2016/08/14/script-formatting 

for formatting code blocks for geonet

DanPatterson_Retired
MVP Emeritus

shape_length < 50 = A, 20 < Shape_length < 500 = B and shape_length > 500 = C.

your ranges overlap, so I assum you meant  50 < … < 500

python parser, code block below, destination field needs to be string/text.  There will be no nulls since it is a shape_length field

expression:

func(!Shape_Length!)

def func(a):
    """ parser"""
    if a < 50:
        val = 'A'
    elif a > 500:
     val = 'C'
    else:
     val = 'B'
    return val
RudiniRudini
New Contributor II

Hi Mr Patterson,

i tried your code last night, and its still show some error.. 

i write this in Pre-Logic script code:

def func(!SHAPE_Length!):
""" parser"""
if !SHAPE_Length! < 50:
val = 'STA'
elif !SHAPE_Length! > 5000:
val = 'Center Line'
else:
val = 'Row'
return val

and in below box i write this

Name = 

func( !SHAPE_Length!) 

i keep get the error processing code..

where am i wrong??

thanks

0 Kudos
DanPatterson_Retired
MVP Emeritus

Well you changed mine.

  • Make sure your field you are calculating into is a text field
  • You need to set it to a python parser
  • Your code has no indentation... it must be 4 spaces (not tab) exactly as I have shown
    • lines 2, 3, 5, 7 and 9 … 4 spaces indented
    • lines 4, 6 and 8 …. 8 spaces
    • change the !Shape_Length! back to 'a' (no quotes)
  • it is func(a) so it can be a generic function... 
  • So I don't have to open up Arc*… the case of the shape_length field is important... it must be exact when you pass it to func( the fieldname within ! marks)

I think I covered everything, just follow mine..

RudiniRudini
New Contributor II

Thanks Mr Patterson really Helpful...

0 Kudos