Select to view content in your preferred language

Converting VBScript to Arcade or Python to calculate a field

786
2
10-19-2022 06:11 AM
MoniqueWolff
New Contributor

Hello,

I have the following VB Script expression to calculate a field in a shapefile.

Since ArcGIS pro doesn’t read VB Script for the field calculation, could someone help me converting it to Arcade or Python ?

I unfortunately don’t have the skills for it.

Thanks.

 

dim X

if len( [INSEE]) =4 then

X = "0" & [INSEE]

Elseif  len( [INSEE])=3 then

X = "00" & [INSEE]

Elseif  len( [INSEE])<3 then

X = "?????"

Else X = [INSEE]

end if

 

 

dim N

if len( [PREFIXE])=2 then

N = "0" & [PREFIXE]

Elseif len( [PREFIXE])=1 then

N = "00" & [PREFIXE]

Else N = [PREFIXE]

end if

 

 

dim Y

If [SECTFEUI] =" " then

Y="????"

Elseif [SECTFEUI] ="0" then

Y="????"

Elseif len( [SECTFEUI]) =3 then

Y = "0" & [SECTFEUI]

Elseif len( [SECTFEUI]) =2 then

Y = "00" & [SECTFEUI]

Elseif len( [SECTFEUI]) =1 then

Y = "000" & [SECTFEUI]

Else Y = [SECTFEUI]

end if

 

 

dim Z

if [PARCELLE] ="0" then

Z="????"

Elseif len( [PARCELLE])=3 then

Z = "0" & [PARCELLE]

Elseif len( [PARCELLE])=2 then

Z = "00" & [PARCELLE]

Elseif len( [PARCELLE]) =1 then

Z = "000" & [PARCELLE]

Else Z = [PARCELLE]

end if

 

__esri_field_calculator_splitter__

KEY=X&N&Y&Z

Tags (3)
0 Kudos
2 Replies
KenBuja
MVP Esteemed Contributor

Are each of your fields numeric? In Arcade, you can use the Text function to pad them with 0.

 

var X;
var insee = $feature.INSEE;
if (Count(Text(INSEE) < 3) {
  X = '????';
} else {
  X = Text(insee, '00000')
}

 

If the fields are strings, then you would have to use this method, using the When function and Template literals

 

var insee = #feature.INSEE;
var x = When(Count(insee) == 4, `0${insee}`,
              Count(insee) == 3, `00${insee}`,
              '?????');

 

 

 

MoniqueWolff
New Contributor

Hello,

Thank you very much for your answer.

Some of my fields are numeric and some of them are strings.

I finally managed to do it using Python 3 like this :

 

def concatspecial(INSEE,PREFIXE,SECTFEUI,PARCELLE):

if len(str(INSEE))==4:
x = "0"+INSEE
elif len(str(INSEE))==3:
x = "00"+INSEE
elif len(str(INSEE))<3:
x="?????"
else:
x=INSEE

if len(str(PREFIXE))==2:
n="0"+str(PREFIXE)
elif len(str(PREFIXE))==1:
n="00"+str(PREFIXE)
else:
n=PREFIXE

if SECTFEUI==" ":
y = "????"
elif SECTFEUI=="0":
y="????"
elif len(SECTFEUI)==3:
y="0"+SECTFEUI
elif len(SECTFEUI)==2:
y="00"+SECTFEUI
elif len(SECTFEUI)==1:
y ="000"+SECTFEUI
else:
y = SECTFEUI

if PARCELLE=="0":
z="????"
elif len(str(PARCELLE))==3:
z = "0"+str(PARCELLE)
elif len(str(PARCELLE))==2:
z = "00"+str(PARCELLE)
elif len(str(PARCELLE))==1 :
z = "000" + str(PARCELLE)
else:
z = PARCELLE
return str(x)+str(n)+str(y)+str(z)

Have a good day !

0 Kudos