2

I am trying to calculate NDVI from rasters that I've downloaded with download rasters, clipping them with some features (selection_feature) that I'd split from an original archive. I am a begginer in Python scripting, and I can't understand what is going wrong with this script!

import arcpy
import os
from arcpy import env
from arcpy.sa import *

arcpy.CheckOutExtension("Spatial")

NDVI_tempo = 'Z:\\04 - Temp\\Charles\\NDVI_tempo\\NAL_PL'


arcpy.env.workspace = 'Z:\\04 - Temp\\Charles\\NDVI_tempo\\NAL\\NAL.gdb'


arcpy.SplitByAttributes_analysis("Area_produtiva", NDVI_tempo, ['ID_TALHAO'])


down = 'Z:\\04 - Temp\\Charles\\NDVI_tempo\\NAL_PL'
image_server = 'Y:\\E - IMAGEM SERVER\\IMAGEM_SERVER.gdb\\F01_PLANETLAB'

arcpy.env.workspace = r'Z:\\04 - Temp\\Charles\\NDVI_tempo\\NAL_PL'
fcs = arcpy.ListFeatureClasses()

for fc in fcs: 

    arcpy.DownloadRasters_management (image_server, down, "", fc, "CLIPPING", "CONVERT_AS_REQUIRED", "TIFF", "NONE", "", "NO_MAINTAIN_FOLDER")



rasters = arcpy.ListRasters("*", "TIF")
env.workspace = 'Z:\\04 - Temp\\Charles\\NDVI_tempo\\NAL'   
for tiff in rasters:

    arcpy.MakeRasterLayer_management(tiff, "Z:\\04 - Temp\\Charles\\NDVI_tempo\\NAL\\temp_rast1.tif", "", "", "3")
    arcpy.MakeRasterLayer_management(tiff, "Z:\\04 - Temp\\Charles\\NDVI_tempo\\NAL\\temp_rast2.tif", "", "", "4")

    arcpy.CopyRaster_management("Z:\\04 - Temp\\Charles\\NDVI_tempo\\NAL\\temp_rast1.tif", "copy_rast1", "","", "", "", "", "", "", "", "", "")
    arcpy.CopyRaster_management("Z:\\04 - Temp\\Charles\\NDVI_tempo\\NAL\\temp_rast2.tif", "copy_rast2", "","", "", "", "", "", "", "", "", "")

    Property1Result = arcpy.GetRasterProperties_management ("copy_rast1", "MINIMUM")
    Property2Result = arcpy.GetRasterProperties_management ("copy_rast2", "MINIMUM")

    Property1 = Property1Result.getOutput
    Property2 = Property2Result.getOutput


    Correcao1 = Minus ("copy_rast1", "Property1")
    Correcao2 = Minus ("copy_rast2", "Property2")

    Rastdiv1 = Minus ("Correcao2", "Correcao1")
    Rastdiv2 = Plus  ("Correcao2", "Correcao1")

    NDVI_tiff = Divide (Rastdiv1, Rastdiv2)

The script is running pretty well until make raster layer, I don't understand what is happening there, it seems like it doesn't recognize the downloaded image! The error message is that:

Traceback (most recent call last):
  File "Z:\04 - Temp\Charles\NDVI_tempo\NAL\NAL.gdb\NDVI_RF#Script_NDVIRF.py", line 38, in <module>
  File "c:\program files\arcgis\pro\Resources\arcpy\arcpy\management.py", line 6219, in MakeRasterLayer
    raise e
  File "c:\program files\arcgis\pro\Resources\arcpy\arcpy\management.py", line 6216, in MakeRasterLayer
    retval = convertArcObjectToPythonObject(gp.MakeRasterLayer_management(*gp_fixargs((in_raster, out_rasterlayer, where_clause, envelope, band_index), True)))
  File "c:\program files\arcgis\pro\Resources\arcpy\arcpy\geoprocessing\_base.py", line 506, in <lambda>
    return lambda *args: val(*gp_fixargs(args, True))
arcgisscripting.ExecuteError: Failed to execute. Parameters are not valid.
 ERROR 000865: Input raster: 20170312_124653_0e1f_381_s17.tif does not exist.
Failed to execute (MakeRasterLayer).
Failed to execute (Script).
  • In general, you'll need to provide more information to get good answers here to code related questions. Specifically: why does this not work for you? Is there an error message (what is it, what line)? Does it run, but the output is unexpected (how and what was expected)? – Evil Genius Sep 26 '17 at 11:38
  • Sorry, i forgot to write down the error message, i will edit it right now – Charlera Sep 26 '17 at 11:40
3

It looks like you are downloading the rasters to one directory ('Z:\\04 - Temp\\Charles\\NDVI_tempo\\NAL_PL') and then changing the environment to something else ('Z:\\04 - Temp\\Charles\\NDVI_tempo\\NAL') before running the tools.

That's happening in this bit of code:

rasters = arcpy.ListRasters("*", "TIF")                    # you've asked for the TIFF files in Z:\...\NDVI_tempo\NAL_PL
env.workspace = 'Z:\\04 - Temp\\Charles\\NDVI_tempo\\NAL'  # but now all of the tools will look here instead
for tiff in rasters:

The environment was set earlier, at this part:

arcpy.env.workspace = r'Z:\\04 - Temp\\Charles\\NDVI_tempo\\NAL_PL'

You could just remove the env.workspace = 'Z:\\04 - Temp\\Charles\\NDVI_tempo\\NAL' bit above, but that will place your outputs into the other directory unless you use full paths.

It also looks like you've confused actual raster paths with layer names. I can't tell what you intended to do, but for example:

arcpy.MakeRasterLayer_management(tiff, "Z:\\04 - Temp\\Charles\\NDVI_tempo\\NAL\\temp_rast1.tif", "", "", "3")

This will create a layer called "Z:\04 - Temp\Charles\NDVI_tempo\NAL\temp_rast1.tif", and not create an actual raster file.
That could be ok, as long as you reference the layer by the same name later on, but it is confusing to look at.

  • Ahh.. Got it. So, I'm making those layers just to get the bands separately, and them copying them, the objective is do some math operation to subtract the lowest pixel value, and get the NDVI file at the end... Thanks for that! I don't know if the script that i'd made is the best way to get the results that i want, do you have any ideas for that? – Charlera Sep 26 '17 at 14:34

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.