Operator Overloading Problem

Hi all,

I’m trying to use SNAPPY to do some mosaicking, but I keep running into the same error that leads me to believe there is an issue in jpy when overloading the operators.

The mosaic operator expects a list of products (Product[] sourceProducts;), yet I get this error:
RuntimeError: java.lang.ClassCastException: class [Lorg.esa.snap.core.datamodel.Product; cannot be cast to class org.esa.snap.core.datamodel.Product ([Lorg.esa.snap.core.datamodel.Product; and org.esa.snap.core.datamodel.Product are in unnamed module of loader 'app')

It seems like it’s trying to convert the Product[] to a Product even though the function itself expects a Product[].

I’m using SNAP 11.0 on W11 in Python 3.10. Code snippet below.

from esa_snappy import jpy, HashMap

def Mosaic(sourceProducts):
    ProductArray = jpy.array('org.esa.snap.core.datamodel.Product', len(sourceProducts))

    for i, p in enumerate(sourceProducts):
        ProductArray[i] = p

    params = HashMap()
    product = HashMap()
    product.put("sourceProducts", ProductArray)

    return esa_snappy.GPF.createProduct("Mosaic", params, product)

You can either pass the ProductArray directly as the sourceProducts argument (see GPF.java, line 217), or use a HashMap<String, Product> as the sourceProducts argument (see GPF.java, line 284).

As shown above, passing a HashMap returns an error.

Passing ProductArray directly returns RuntimeError: org.esa.snap.core.gpf.OperatorException, showing that the signature is not recognized.

Put every product as an item of the HashMap. You can then drop the ProductArray .

productMap = HashMap()
for i, p in enumerate(sourceProducts):
    productMap.put(f"sourceProduct_{i}", p)

or use ProductMap directly

return esa_snappy.GPF.createProduct("Mosaic", params, ProductArray)

Using productMap directly also gives an OperatorException.
Passing it as a HashMap gives RuntimeError: java.lang.ClassCastException: class java.util.HashMap cannot be cast to class org.esa.snap.core.datamodel.Product (java.util.HashMap is in module java.base of loader 'bootstrap'; org.esa.snap.core.datamodel.Product is in unnamed module of loader 'app')
And here again, it’s trying to convert to a Product, not a Product[], which is the root cause of the problem.

It seems to me like the operator overloading is not properly handled by JPY.

Okay, looks like this. Someone should look at this more closely. Maybe @dolaf can do this, or @TomBlock