I am trying to write python script for bandmaths:
import os
import sys
import glob
import gc
import snappy
from snappy import jpy
from snappy import GPF
from snappy import ProductIO
from snappy import HashMap
sys.path.append(’/opt/anaconda/bin/’)
file4 = ‘E:\proj\S1Adb.dim’
sourceProduct = snappy.ProductIO.readProduct(file4)
HashMap = jpy.get_type(‘java.util.HashMap’)
GPF.getDefaultInstance().getOperatorSpiRegistry().loadOperatorSpis()
parameters = HashMap()
parameters.put(‘targetBands’, ‘’)
parameters.put(‘name’, ‘newBand’)
parameters.put(‘type’, ‘float32’)
parameters.put(‘expression’, ‘255*(Sigma0_VH_db<-19.39)’)
parameters.put(‘expression’, ‘255*(Sigma0_VV_db<-19.39)’)
parameters.put(‘noDataValue’, ‘0.0’)
thresh = GPF.createProduct(‘BandMaths’, parameters, sourceProduct)
ProductIO.writeProduct(thresh, ‘E:\proj\S1A_thresh.dim’, ‘BEAM-DIMAP’)
I am getting an error:
Traceback (most recent call last):
File “D:/programs/self_thresh.py”, line 23, in
thresh = GPF.createProduct(‘BandMaths’, parameters, sourceProduct)
RuntimeError: java.lang.IllegalStateException: no converter defined for value ‘targetBandDescriptors’
Please help me out.
Thanks.
@ABraun
marpet
November 28, 2018, 7:39am
#2
The parameterisation of the BandMaths operator is not correct.
Please have a look here:
Hi there,
Currently trying to perform Band Maths on a image using snappy.
When I do the maths in the SNAP Gui it works fine, however when I run it in python I get:
RuntimeError: org.esa.snap.core.gpf.OperatorException: Could not parse expression: '255*(Sigma0_VV<2.22E-2)'. Numeric operands expected for binary '*' operator.
My code is as follows:
def bandMathsProduct():
global product
params = HashMap()
BandDescriptor = jpy.get_type('org.esa.snap.core.gpf.common.BandMathsOp$BandD…
Hi,
I am writing a stand alone script, using Sentinel 1 products as an input and I am applying the following steps:
Get_sigma
Create Subset
Terrain correction
Speckle filtering
PCA
My final product returns 5 band and I would like to get only the component_1 for further analysis.
In order to do this I am either using the BandMaths dividing all values of component_1 with 1:
def getBand(file):
threshold = 1
expression = "component_1 / %s" % threshold
BandDescriptor = jpy.get_type('org.esa.sn…
Thanks for your help.
The script you provided is only doing bandmaths only on one single band. I want to do bandmaths for multiple bands.
Can you please me.
marpet
November 29, 2018, 8:25am
#4
In this case you simply need to do the follwing section multiple times:
targetBand = BandDescriptor()
targetBand.name = 'Sigma0_VV'
targetBand.type = 'float32'
targetBand.expression = '255*(Sigma0_VV<2.22E-2)'
targetBands[0] = targetBand
You just need to change the index in the targetBands array and of course you need to define this array with a different size, e.g:
targetBands = jpy.array('org.esa.snap.core.gpf.common.BandMathsOp$BandDescriptor', 6)