1

This question follows up on Crianopa's answer https://gis.stackexchange.com/a/107687/126329 - I am attempting to do just this, but my model fails at the Calculate value stage. Using the expression from Crianopa "%value%"[0:6] I still get error message:

File "<expression>", line 1, in <module>
ERROR 000539: Error running expression: LT05_L1GS_099072_20050106_20161127_01_T2_sr_band7 
Traceback (most recent call last):
NameError: name 'LT05_L1GS_099072_20050106_20161127_01_T2_sr_band7' is not defined

Would anyone know where I am going wrong? I'm in 10.4.1.

I'm not familiar with python so I seek a ModelBuilder solution.

  • Yes, definitely using double-quotes. I have also ensured I have no spaces, only underscore in the file path, but no luck. I tried to calculate the value of Expression: Name (rather than Expression: Value) and this work, but the composite still fails later on. – Goanna Aug 23 at 2:48
  • The 'Calculate Values' operation also fails even without the code block input, so perhaps the issue is earlier on in my model. – Goanna Aug 23 at 3:21
  • Initially I put "%value%"[0:6] in the code block in the Calculate Values tool (not knowing where else to be able to add this expression) but the operation fails both with and without. – Goanna Aug 23 at 3:27
  • The code block should be empty. "%value%"[0:6] should be in the 'Expression' field. – Son of a Beach Aug 23 at 3:28
  • Of course, that works! Thank you. – Goanna Aug 23 at 3:37
3

In this case, in the Calculate Value tool...

  • The 'Code Block' field should be empty
  • The 'Expression' field should be: "%value%"[0:6]

(The expression should include the quotation marks.)

Explanation:

The 'Expression' is a simple one-liner Python expression that returns the result.

The 'Code Block' is an optional more complex Python script that will be run before the 'Expression' is evaluated. The 'Expression' can then reference functions/variables created in the 'Code Block' if required.

In this case, the result can be evaluated in a simple one-line Python expression, and therefore no preceding code is required.

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.