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.

  • 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. – PolyGeo Oct 22 '14 at 10:25
  • Thx. I have edited that. – greyline Oct 22 '14 at 10:29
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

  • 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? – greyline Oct 22 '14 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. – PolyGeo Oct 22 '14 at 12:32
  • @PolyGeo I have accepted. – greyline Oct 22 '14 at 12:35
  • This may be worth looking at gis.stackexchange.com/q/61598/115 – PolyGeo Oct 22 '14 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

  • 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. – greyline Oct 22 '14 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. – greyline Oct 22 '14 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? – recurvata Oct 22 '14 at 12:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.