1

I have a workspace with NDVI rasters and another workspace with greyscale rasters. The rasters have the same name.

I want to reclassify the NDVI raster while also using the greyscale values in a function. Depending on the greyscale value, another reclass function should be used. So for every pixel, the correct function has to be found.

I need to use an if statement: if the greyscale raster cell value is high, another statement should be used but the code results in an error:

Invalid use of raster with Boolean operator or function. Check the use of parentheses where applicable.

arcpy.env.workspace = ndviws
ndvirasterlist = arcpy.ListRasters()

for rimage in ndvirasterlist:
    ndvi_raster = arcpy.sa.Raster(rimage)
    grey_raster = arcpy.sa.Raster(os.path.join(greyvalues,rimage))
    if grey_raster <= 28523:
        output_raster = Con(ndvi_raster < -0.0000006 * grey_raster +  0.3448,2,1)
    if grey_raster >28523 and grey_raster < 50000:
        output_raster = Con(ndvi_raster < -0.000009 * grey_raster + 0.437673,2,1)
    if grey_raster >=50000:
        output_raster = Con(ndvi_raster < -0.000007 * grey_raster + 0.5233,2,1)
    output_raster.save(os.path.join(outputws,rimage))

edit: **edited so that there are now 3 if statements. Is this still working with con method?

  • 1
    You have "values" misspelled in arcpy.sa.Raster(os.path.join(greyvaluews,rimage)). That could be a source of an error unless the variable name is similarly misspelled earlier in the program. – P.T. Curran Sep 9 at 11:38
  • thank you PT. that was just a typing mistake since I couldnt copy paste the code here. Would you know how I can do that with 3 IF statements (not 2)? – GIS7 Sep 9 at 12:08
  • 1
    I'm afraid I don't; my experience with rasters is a bit scattershot. I know I've encountered errors because I've misspelled variables before, so I thought that might help. – P.T. Curran Sep 10 at 13:06
1

if grey_raster <= 28523 does not make sense to arcpy. 28523 is the value you want to check against each pixel of grey_raster, which is where Con function comes handy.

In your case, you should use nested conditions like:

arcpy.env.workspace = ndviws
ndvirasterlist = arcpy.ListRasters()

for rimage in ndvirasterlist:
    ndvi_raster = arcpy.sa.Raster(rimage)
    grey_raster = arcpy.sa.Raster(os.path.join(greyvaluews,rimage))
    output_raster = Con(grey_raster <= 28523, Con(ndvi_raster < -0.0000006 * grey_raster +  0.3448,2,1), Con(ndvi_raster < -0.000009 * grey_raster + 0.437673,2,1))
    output_raster.save(os.path.join(outputws,rimage))

EDIT

If you have more than two conditions to check against, you can simply produce intermediate rasters, and work on them as you go (not tested):

arcpy.env.workspace = ndviws
ndvirasterlist = arcpy.ListRasters()

for rimage in ndvirasterlist:
    ndvi_raster = arcpy.sa.Raster(rimage)
    grey_raster = arcpy.sa.Raster(os.path.join(greyvaluews,rimage))
    # first condition (grey_raster <= 28523)
    output_raster = Con(grey_raster <= 28523, Con(ndvi_raster < -0.0000006 * grey_raster +  0.3448,2,1), 1)
    # second condition (grey_raster >28523 and grey_raster < 50000)
    output_raster = Con((grey_raster > 28523) & (grey_raster < 50000), Con(ndvi_raster < -0.000009 * grey_raster + 0.437673,2,1), output_raster)
    # third condition (grey_raster >=50000)
    output_raster = Con(grey_raster >= 50000, Con(ndvi_raster < -0.000007 * grey_raster + 0.5233,2,1), output_raster)
    output_raster.save(os.path.join(outputws,rimage))
  • thank you @umbe1987. In my real case I have 3 if statements, not just 2 (above was just an example). I guess I can't use the con method then? Which alternative would you suggest? – GIS7 Sep 9 at 9:23
  • 1
    Not sure if it will work, but I provided another possible solution. Feel free to test it and report back your results if you want. – umbe1987 Sep 9 at 14:08
  • 1
    your edit worked when i changed "output_raster>28523" to "grey_raster>28523" in the second and "output_raster >=50000" to "grey_raster >=50000" in the third condition. Also at the very end of condition 2 and 3 I had to change the value 1 to the variable "output_raster", since the values were overwritten before. Thank you very much. – GIS7 Sep 10 at 11:53
  • 1
    Glad it helped. I changed the code according to your comments so that it will be correct. – umbe1987 Sep 10 at 12:21

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.