1

How can I apply different symbologies to rasters in a python script based on the maximum raster value?

The script loads one raster, applies symbology and outputs an MXD and PDF of the map, then moves on to the next raster.

I want to add a step which gets the maximum raster value and then selects one from three symbologies to be applied based on the maximum raster value.

Here is an extract of the script:

 # Get max raster value

timeMaxResult = arcpy.GetRasterProperties_management(Raster_lyr, "MINIMUM")
timeMax = timeMaxResult.getOutput(0)

# Apply symbology
if timeMax <= "31000":
    arcpy.ApplySymbologyFromLayer_management(Raster_lyr, symbology1)
elif timeMax > "31000" and timeMax < "160000":
    arcpy.ApplySymbologyFromLayer_management(Raster_lyr, symbology2)
else:
    arcpy.ApplySymbologyFromLayer_management(Raster_lyr, symbology3)
  • what is your question exactly ? This looks OK, except that I would use numerical values instead of string to test the inequality int(timeMax). Be careful that "23"<"3" is true. – radouxju Sep 13 '18 at 14:14
  • It doesn't apply the symbology as expected! Specifically, the symbology in the elif statement is not applied to the relevant rasters.How would I apply your suggestion in the script? (I am a beginner in python) – rachah Sep 13 '18 at 14:59
0

As you mention in the comments, it seems that you are applying a symbology but that it does not correspond to your expectations. The problem is due to the fact that getOutput(0) returns a string and not a numerical value. When you compare two strings, < mean "comes before in the alphabetical order" and not "smaller than". So the comparison will be done digit by digit, starting from the left ( "23000000000" < "1" is False, but "23000000" < "25" is True). As a result, your "elif" statement is always false because you cannot start a number with a value that is both before "1" and after "3". In fact, only the string of a positive number starting with [10,11,12,13,14,15] or smaller than 1 would be < "16000000" (e.g. "0.258" < "16000000" is true, "1500000000000000" < "16000000" is true). This gives of course inconsistant results in your case. Therefore you should switch to numerical values (using float() or int() ) to make a numerical comparison.

timeMaxResult = arcpy.GetRasterProperties_management(Raster_lyr, "MINIMUM")
timeMax = int(timeMaxResult.getOutput(0)) # convert string to integer
if timeMax <= 31000: #removed the "" to have a number
    arcpy.ApplySymbologyFromLayer_management(Raster_lyr, symbology1)
elif timeMax < 160000: #note: no need to test again that it is > than 31000
    arcpy.ApplySymbologyFromLayer_management(Raster_lyr, symbology2)
else:
    arcpy.ApplySymbologyFromLayer_management(Raster_lyr, symbology3)

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.