2

I'm a programmer, I have programmed in C#, Java, but not with Python. And I'm not a geographic worker. How can I write a python script into Field Calculator to be able to check the other column value. If that other column value is "a certain string", the value in this column should be 0, otherwise it should be computed with an equation. The value I want to look is a Text type, and I want to count in a Double type column.

PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Oct 22, 2014 at 10:20
2
  • It will make it much easier if you can be more specific about the field types you are dealing with, and how/whether the equation involves these or any other fields. Commented Oct 22, 2014 at 10:25
  • Thx. I have edited that. Commented Oct 22, 2014 at 10:29

2 Answers 2

4

You don't 'Dim' variables in python, you just declare and assign them. That said, your basic route is to set the parser to python and check Show Codeblock. In the Pre-Logic Script Code box, enter your function like this:

def DoThis(fld):
 val = 0
 if fld <> 'a certain string':
 val = # do your calculation here
 return val

In your 'field =' box, enter:

DoThis(!field!)

In python , # means a comment. Indentation counts. If your calculation depends on other fields, pass those in as well. That should be straightforward with your background. In calling the function, the field name is bracketed by exclaimation (!) marks.

enter image description here

GISHuman
3,7091 gold badge22 silver badges45 bronze badges
answered Oct 22, 2014 at 12:17
4
  • I have used this solution by dark fader: gis.stackexchange.com/questions/81817/… It works with unaccentuated strings, but there's a problem with accentuated strings. What to do? Commented Oct 22, 2014 at 12:23
  • @gregtom6 your question makes no mention of accentuated strings so based on what you did ask I think you should accept this answer and write a more detailed new one. Commented Oct 22, 2014 at 12:32
  • @PolyGeo I have accepted. Commented Oct 22, 2014 at 12:35
  • This may be worth looking at gis.stackexchange.com/q/61598/115 Commented Oct 22, 2014 at 12:52
-1

I'm a long time ArcGIS user but find that the QGIS field calculator is a lot more intuitive & provides options for dragging & dropping fieldnames, expressions & operators.

If you are a programmer, then you'll probably find it a lot easier and you'll find it builds your knowledge for using the expression builder in ArcGIS. QGIS Expression Builder

answered Oct 22, 2014 at 10:51
3
  • Thanks, but now it seems to be it would take hours to find out how to use that with ArcGIS, and I don't do this kind of work for months. I need only short scripts and that would be already completed if I would know the syntax of doing that. gis.stackexchange.com/questions/81817/… I read that and I think I need a double variable instead of Dim, but I'm not sure. Commented Oct 22, 2014 at 11:02
  • It seems to be it is possible with this method: gis.stackexchange.com/questions/81817/… But the problem is maybe with the accentuated letters I'm using in the string type field. Commented Oct 22, 2014 at 12:17
  • So you're saying the the value in the field has/can have an accented character, and the problem is with the comparison value ('a certain string') not being accented? Commented Oct 22, 2014 at 12:37

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.