Using the Binary Threshold Function from ArcGIS/ArcMap in Python

1417
8
07-05-2017 09:59 AM
ThomasPiette
New Contributor

I've been trying to apply the Binary Threshold function from ArcGIS/ArcMap in Python but I can't seem to get it right. I've been trying with arcpy.EditRasterFunction_management. 
Does anyone know how to get this right?

Thanks in Advance!

0 Kudos
8 Replies
DanPatterson_Retired
MVP Esteemed Contributor

This function I assume.  Can you fill in the gaps on what you did?

0 Kudos
ThomasPiette
New Contributor

Yes, that function indeed. I need it for mapping water bodies after performing a Normalized Difference Water Index using 2 rasters (band 3 & Band 8 from sentinel 2 imagery). I found the Syntax code here: Edit Raster Function—Data Management toolbox | ArcGIS Desktop .

I filled it in like this:

Binary_Thresholding_Function = Resultaten + r"\Raster Function Temlate.rft"

arcpy.EditRasterFunction_management(NDWI1, "EDIT_MOSAIC_DATASET", "INSERT", Binary_Thresholding_Function, "Binary Thresholding function")

Where NDWI1 is the result after applying the Normalized Difference Water Index.

For the Binary_Thresholding_Function I've exported a template for the function using it manually in Arcmap itself.

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

Thomas... did you get an error message or did it not do what you want.  It is hard to see if you followed the example since you haven't specified where your environments are set and/or whether you are trying to do this from the python window or a script

0 Kudos
ThomasPiette
New Contributor

Hello Dan, when I run the script (in PyScripter) it eventually gets an error. It says:  ExecuteError: Failed to execute. Parameters are not valid. ERROR 000814: Invalid file type. Failed to execute (EditRasterFunction).

This is the script where I only try the Binary threshold function :

import arcpy


# Set workspace


dir = r"C:\Users\Thomas\Documents\School\Masterproef\Python\Deel 1"

arcpy.env.workspace = dir

arcpy.env.overwriteOutput = True

if (arcpy.CheckExtension('Spatial') == 'Available'):
arcpy.CheckOutExtension('Spatial')
from arcpy.sa import *
else:
print "Spatial analyst extension not available"

from arcpy.sa import *

# Mappen Variabelen


Banden = dir + r"\Banden"
Resultaten = dir + r"\Resultaten"

# Gebruikte bestanden variabelen

Band3 = Banden + r"\B03.jp2"
Band8 = Banden + r"\B08.jp2"
Binary_Thresholding_Function = Resultaten + r"\Raster Function Temlate.rft"

NDWI1 = dir + r"\NDWI1.tif"
arcpy.EditRasterFunction_management(NDWI1, "EDIT_MOSAIC_DATASET", "INSERT", Binary_Thresholding_Function, "Binary Thresholding function")

I hope this makes things clearer.

Thanks in advance!

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

from here.... it says that the input raster must have the function already applied.  Can't tell from your code.

The error message basically calls that function with a try except block.  It one of the input variables is wrong, then it fails with the error you got.

0 Kudos
ThomasPiette
New Contributor

I don't think it allready has a function applied. The only thing I did was using raster algebra (Minus, Plus, Divide) on two bands of sentinel 2 imagery. Wouldn't know how to apply a function to this raster by just using Python;

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

This blog post... keep it in mind for future reference

0 Kudos
XanderBakker
Esri Esteemed Contributor

Can you share the file "C:\Users\Thomas\Documents\School\Masterproef\Python\Deel 1\Resultaten\Raster Function Temlate.rft". This will allow us to explore what other functions you included in the raster function chain. Or maybe it is because you are using a TIFF file as input. I believe you will need a raster mosaic dataset to apply a raster function.

0 Kudos