1

I do not have a very good understanding of python and have attempted to write a code block so that my model will skip rasters that do not contain any information and continue the process with rasters that do contain information. This is pertinent part of the model: enter image description here

My current issue is that I keep getting this error:

Executing (Calculate Value): 
CalculateValue getValue(%Check Raster%) "def getValue(CheckRaster):\n import arcpy\n if "Has Values": return True\n if "Has No Values": return False" Variant 
Start Time: Fri Oct 30 10:16:54 2015 
ERROR 000539: 
SyntaxError: invalid syntax (, line 1) Failed to execute (Calculate Value).

This is what my calculate value tool currently looks like:

enter image description here

Any ideas to how I could change the code so that there are not any more syntax errors? (The complete model can be found here.)

EDIT: The below answers were helpful but they have not entirely circumvented the problem yet. The check raster tool that I am using seems to always give an output that "Has Values" even though the following error will occur during Raster To Polygon: ERROR 010151: No features found in C:\Users\boll0230\Documents\ArcGIS\Suitability_stuff\03\Scratch\polygon_03.shp. Possible empty feature class.

I believe this means their is a problem with the check raster tool I have. Perhaps this has become more complicated that it needs to be. Is there a possible code that could be written in the calculate value tool that would check Raster@Threshold for the presence of features?

8
  • 2
    There's no need to import arcpy here. It can drastically slow down your code.
    – Paul
    Oct 30, 2015 at 16:39
  • 2
    @Paul I believe if a module is already imported, any further attempts to import are ignored. Oct 30, 2015 at 17:38
  • 2
    If a module is already imported, it will not be imported again, see Stack Overflow here. Additionally, to @Paul 's point, I am fairly certain arcpy does not need to be loaded once in ArcMap.
    – GeoSharp
    Oct 30, 2015 at 17:57
  • Can you create a copy of the layer and then use a definition query on the newly copied layer to exclude any rasters that can not be turned into a polygon before the tool is run. Then run the tool on the new definition queried layer only.
    – ed.hank
    Nov 2, 2015 at 22:08
  • 1
    This looks like the same question as gis.stackexchange.com/questions/168169/… Nov 2, 2015 at 22:13

2 Answers 2

4

You want to reference the outputs of the Check Raster tool in your function definition, not the function name itself.

Also from the help:

In-line variable of type string should be enclosed within quotes ("%string variable%") in an expression. In-line variables of type numbers (double, long) do not require quotes (%double%).

Try an expression like this:

getValue ("%Has Values%", "%Has No Values%")

And your code block:

def getValue (has, doesnt):
    if has:
        return True
    if doesnt:
        return False
1
  • I have typed the code block as you have indicated but there seems to be an issue. No errors occur but the check raster tool doesn't seem to spit out any empty strings. For example: I used a raster that I know will not have any values. When it is run through the model it comes up with the error I had in my previous question where: "ERROR 010151: No features found in C:\Users\boll0230\Documents\ArcGIS\Suitability_stuff\03\Scratch\polygon_03.shp. Possible empty feature class.
    – boll0230
    Nov 2, 2015 at 19:44
2

Your code is incorrect this is what you need to do:

Set your model up as such, the Calculate Value tool is a precondition to some other tool, I've used Clip as an example. Your variable Raster_03 must have the forward slash for the code to work.

Model

In the calculate value tool it should be set up as such (note output data type is set to Boolean):

Calculate Value tool

The code is placed here for convenience:

import arcpy
def CheckRaster(r):
  result = arcpy.GetRasterProperties_management(r, "MINIMUM")
  n = int(result.getOutput(0))
  if n > 1:
    return True
  else:
    return False
1
  • This works when I start with the raster I want checked, but what if I want to have this step happen after the raster has been put through a calculator tool that has produced a green output bubble. I can not change the slashes on this output and therefore get errors/
    – boll0230
    Nov 3, 2015 at 21:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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