I'm wondering if there is a method to perform a function similar to Arc's Con tool, using open source options in python. I have two rasters, one that shows locations of clouds and the other is just a landsat band. If a value is >x in the cloud raster, I want to take the pixel from the landsat band, and if it's
Con is a conditional raster calculation, therefore it could be done as a series of steps or directly with a raster calculator/functions that support conditionals. There are several options already covered at other questions here on SE. This may actually be a duplicate of one, but I will just link them since you are asking for any python based open source option and these are specific to QGIS, SAGA, and R:
- What is the equivalent of arcpy "Con" in QGIS and/or R raster-package?
- Replacing raster values in one raster with another raster in QGIS ... how to?
- QGIS Raster Calculater Syntax
As mentioned in some of those answers, QGIS with the RasterCalc plugin (not the default raster calculator) supports conditional statements.
#!/usr/bin/python # Import the otb applications package import otbApplication # The following line creates an instance of the BandMath application BandMath = otbApplication.Registry.CreateApplication("BandMath") # The following lines set all the application parameters: BandMath.SetParameterStringList("il", ['cloud_r.tif', 'landsat_nir.tif']) BandMath.SetParameterString("out", "apTvUtBandMathOutput.tif") BandMath.SetParameterString("exp", "(im1b1>1)?(im2b1):(0)") # The following line execute the application BandMath.ExecuteAndWriteOutput()
gdal_calc.py -A cloud.tif -B landsat_NIR.tif --outfile=out_NIR.tif --calc="B*(A>1)"
note: I assume here that your "x" is 1 , but you can change the value