S-1 subset using geographical coordinates with snappy

Dear All,

I am trying to subset a S-1 image with Snappy . I used the code proposed in :https://github.com/ec-everest/e-learning-modules/blob/885da29f323658567614d444de9f4bf2493d2446/src/main/resources/e-learning/02%20Plot%20Sentinel-1%20subset.ipynb and it worked fine.

Now I am tryng to subset putting the lat/long coordinates of a four corners polygon but without any success.
Do you have an idea on how to solve it?

Kind regards

Fabiano

Did you look at the snappy examples in the snappy installation directory?

Thank you very much for the answer,
.Right now I am running the following code but it produce a void output file:
SAR_path=‘my_path’
image_name=“my_image_name”
SAR_image = ProductIO.readProduct(SAR_path)

wkt=“POLYGON((7.62 81.585, 7.62 81.699, 7.465 81.699, 7.465 81.585,7.62 81.585 ))”
geom = WKTReader().read(wkt)
HashMap = jpy.get_type(‘java.util.HashMap’)
GPF.getDefaultInstance().getOperatorSpiRegistry().loadOperatorSpis()
parameters = HashMap()
parameters.put(‘copyMetadata’, True)
parameters.put(‘geoRegion’, geom)
SAR_image_subset = GPF.createProduct(‘Subset’, parameters, SAR_image)
ProductIO.writeProduct(SAR_image_subset, image_name+’_subset’, ‘BEAM-DIMAP’)

Kind Regards

Fabiano

1 Like

Your code looks good.
You say you get a void output file. Does it mean that the right region is written but there is just no data?
Or is also the wrong region extracted?

Thank you very much I solved it ! I just had to restart the PC

Fabiano

Dear All,

I’m quietly newbie in snappy and I am trying to subset a S-1 image with Snappy . I have tried with different images, but i only I trained myself with error:

band.readPixels(0, 0, w, h, band_data)
RuntimeError: java.lang.RuntimeException: javax.imageio.IIOException: I/O error reading image metadata!

The source code as follows:

import sys
sys.path.append(’/home/dani-ifca/.snap/snap-python/’)
from snappy import ProductIO
from snappy import jpy
from snappy import GPF
from snappy import HashMap

import numpy as np
import matplotlib.pylab as plt

pathS1=’/home/dani-ifca/Escritorio/image sentinel/S1B_IW_GRDH_1SDV_20161111T061637_20161111T061702_002907_004EEF_B9BC.SAFE’

p = ProductIO.readProduct(pathS1)
width = p.getSceneRasterWidth()
height = p.getSceneRasterHeight()
name = p.getName()
band_names = p.getBandNames()

print(“Product name: %s. Size = %d x %d pixels” % (name, width, height))
print(“Bands: %s” % (list(band_names)))

x = 18727
y = 15438
width = 2872
height = 3459

HashMap = jpy.get_type(‘java.util.HashMap’)
GPF.getDefaultInstance().getOperatorSpiRegistry().loadOperatorSpis()
parameters = HashMap()
parameters.put(‘copyMetadata’, True)
parameters.put(‘region’, “%s,%s,%s,%s” % (x, y, width, height))
subset = GPF.createProduct(‘Subset’, parameters, p)

band = subset.getBand(‘Amplitude_VV’)

w = band.getRasterWidth()
h = band.getRasterHeight()
band_data = np.zeros(w * h, np.float32)
band.readPixels(0, 0, w, h, band_data)

Thank you in advance.

Is this thread of any help for you?

A search reveals that the error is reported several times. It seems to be a memory issue.

I am having trouble with creating the Subset Sentinel-2, but I assume the Topic is entirely the same so I would like to ask the question in here.

I have tried to run The snappy example from GitHub

But got this error:

python snappy_subset.py /workdir/S2A_MSIL2A_20200515T045701_N0214_R119_T44PLU_20200515T093525.zip "POLYGON((79.6500 12.3000,79.7500 12.3000,79.7500 12.2000,79.6500 12.2000,79.6500 12.3000))"
INFO: org.esa.snap.core.gpf.operators.tooladapter.ToolAdapterIO: Initializing external tool adapters
SEVERE: org.esa.s2tbx.dataio.gdal.activator.GDALDistributionInstaller: The environment variable LD_LIBRARY_PATH is not set. It must contain the current folder '.'.
INFO: org.esa.snap.core.util.EngineVersionCheckActivator: Please check regularly for new updates for the best SNAP experience.
Reading...
INFO: org.esa.s2tbx.dataio.s2.ortho.S2OrthoProductReaderPlugIn: Building product reader - EPSG:32644
WARNING: org.esa.s2tbx.dataio.metadata.GenericXmlMetadata: Metadata: the path to element [metadata_level] does not exist
WARNING: org.esa.s2tbx.dataio.metadata.GenericXmlMetadata: Metadata: the path to element [bandid] does not exist
WARNING: org.esa.s2tbx.dataio.metadata.GenericXmlMetadata: Metadata: the path to element [bandid] does not exist
WARNING: org.esa.s2tbx.dataio.metadata.GenericXmlMetadata: Metadata: the path to element [bandid] does not exist
WARNING: org.esa.s2tbx.dataio.metadata.GenericXmlMetadata: Metadata: the path to element [bandid] does not exist
WARNING: org.esa.s2tbx.dataio.metadata.GenericXmlMetadata: Metadata: the path to element [bandid] does not exist
INFO: org.hsqldb.persist.Logger: dataFileCache open start
WARNING: org.esa.s2tbx.dataio.s2.ortho.Sentinel2OrthoProductReader: Warning: missing file /workdir/S2A_MSIL2A_20200515T045701_N0214_R119_T44PLU_20200515T093525.zip/S2A_MSIL2A_20200515T045701_N0214_R119_T44PLU_20200515T093525.SAFE/GRANULE/L2A_T44PLU_A025570_20200515T051007/QI_DATA/L2A_T44PLU_20200515T045701_DDV_20m.jp2

WARNING: org.esa.s2tbx.dataio.s2.ortho.Sentinel2OrthoProductReader: Warning: no image files found for band quality_dense_dark_vegetation

Traceback (most recent call last):
File "snappy_subset.py", line 27, in <module>
    sub_product = op.getTargetProduct()
RuntimeError: java.lang.NullPointerException

It is possible that this error is also related to s2tbx.GDALInstaller error as I have tried to use gpt tool with same Sentinel-2 product and WKT geoRegion and got this error instead:

gpt Subset -Ssource=/workdir/S2A_MSIL2A_20200515T045701_N0214_R119_T44PLU_20200515T093525.zip -PcopyMetadata=true -PgeoRegion="POLYGON((79.6500 12.3000,79.7500 12.3000,79.7500 12.2000,79.6500 12.2000,79.6500 12.3000))"
INFO: org.esa.snap.core.gpf.operators.tooladapter.ToolAdapterIO: Initializing external tool adapters
SEVERE: org.esa.s2tbx.dataio.gdal.activator.GDALDistributionInstaller: The environment variable LD_LIBRARY_PATH is not set. It must contain the current folder '.'.
INFO: org.esa.snap.core.util.EngineVersionCheckActivator: Please check regularly for new updates for the best SNAP experience.
INFO: org.esa.s2tbx.dataio.s2.ortho.S2OrthoProductReaderPlugIn: Building product reader - EPSG:32644
WARNING: org.esa.s2tbx.dataio.metadata.GenericXmlMetadata: Metadata: the path to element [metadata_level] does not exist
WARNING: org.esa.s2tbx.dataio.metadata.GenericXmlMetadata: Metadata: the path to element [bandid] does not exist
WARNING: org.esa.s2tbx.dataio.metadata.GenericXmlMetadata: Metadata: the path to element [bandid] does not exist
WARNING: org.esa.s2tbx.dataio.metadata.GenericXmlMetadata: Metadata: the path to element [bandid] does not exist
WARNING: org.esa.s2tbx.dataio.metadata.GenericXmlMetadata: Metadata: the path to element [bandid] does not exist
WARNING: org.esa.s2tbx.dataio.metadata.GenericXmlMetadata: Metadata: the path to element [bandid] does not exist
INFO: org.hsqldb.persist.Logger: dataFileCache open start
WARNING: org.esa.s2tbx.dataio.s2.ortho.Sentinel2OrthoProductReader: Warning: missing file /workdir/S2A_MSIL2A_20200515T045701_N0214_R119_T44PLU_20200515T093525.zip/S2A_MSIL2A_20200515T045701_N0214_R119_T44PLU_20200515T093525.SAFE/GRANULE/L2A_T44PLU_A025570_20200515T051007/QI_DATA/L2A_T44PLU_20200515T045701_DDV_20m.jp2

WARNING: org.esa.s2tbx.dataio.s2.ortho.Sentinel2OrthoProductReader: Warning: no image files found for band quality_dense_dark_vegetation

java.lang.NullPointerException
        at org.esa.snap.core.gpf.common.SubsetOp.initialize(SubsetOp.java:210)
        at org.esa.snap.core.gpf.internal.OperatorContext.initializeOperator(OperatorContext.java:486)
        at org.esa.snap.core.gpf.internal.OperatorContext.getTargetProduct(OperatorContext.java:273)
        at org.esa.snap.core.gpf.Operator.getTargetProduct(Operator.java:387)
        at org.esa.snap.core.gpf.main.CommandLineTool.runOperator(CommandLineTool.java:306)
        at org.esa.snap.core.gpf.main.CommandLineTool.runGraphOrOperator(CommandLineTool.java:284)
        at org.esa.snap.core.gpf.main.CommandLineTool.run(CommandLineTool.java:188)
        at org.esa.snap.core.gpf.main.CommandLineTool.run(CommandLineTool.java:121)
        at org.esa.snap.core.gpf.main.GPT.run(GPT.java:54)
        at org.esa.snap.core.gpf.main.GPT.main(GPT.java:34)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.esa.snap.runtime.Launcher.lambda$run$0(Launcher.java:55)
        at org.esa.snap.runtime.Engine.runClientCode(Engine.java:189)
        at org.esa.snap.runtime.Launcher.run(Launcher.java:51)
        at org.esa.snap.runtime.Launcher.main(Launcher.java:31)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at com.exe4j.runtime.LauncherEngine.launch(LauncherEngine.java:65)
        at com.install4j.runtime.launcher.UnixLauncher.main(UnixLauncher.java:57)

Error: java.lang.NullPointerException

@marpet if you have a moment, would you be willing to look into it with me? I noticed that the last commit to snappy_subset.py example has failed jenkins tests.

This is where I got so far:
If I run the code line by line in Jupyter Lab, I can open the product and work with it. The code always hangs on the same line

sub_product = op.getTargetProduct()
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-19-03cefc25ad88> in <module>
----> 1 sub_product = op.getTargetProduct()

RuntimeError: java.lang.NullPointerException

Most strangely, if I resample the product first

product = ProductIO.readProduct(file)

parameters = HashMap()
parameters.put('targetResolution',10)
result = snappy.GPF.createProduct('Resample', parameters, product)

op = SubsetOp()
op.setSourceProduct(result )
op.setGeoRegion(geom)
op.setCopyMetadata(True)

sub_product = op.getTargetProduct()

Everything just works! Can anyone make sense of it? Thanks for any help.