Hello step-forum community,
I want to subset a region from a Sentinel-1 product using longitude and latitude coordinates. Here’s what I have so far:
import snappy
from snappy import WKTReader
product_path = '/home/test/Documents/insar/iceland/original/'
input_S1_files = sorted(list(iglob(join(product_path, '**', '*S1*.zip'),
recursive=True)))
name, sensing_mode, product_type, polarization, height, width, band_names = (
[] for i in range(7))
for i in input_S1_files:
sensing_mode.append(i.split("_")[3])
product_type.append(i.split("_")[4])
polarization.append(i.split("_")[-6])
s1_read = snappy.ProductIO.readProduct(i)
name.append(s1_read.getName())
height.append(s1_read.getSceneRasterHeight())
width.append(s1_read.getSceneRasterWidth())
band_names.append(s1_read.getBandNames())
wkt = "POLYGON((-21.962 64.708,-21.962 64.871,-22.248 64.871,-22.248 54.708,-21.962 64.708))"
geom = WKTReader().read(wkt)
parameters = snappy.HashMap()
parameters.put('copyMetadata', True)
parameters.put('geoRegion', geom)
subset = snappy.GPF.createProduct('Subset', parameters, s1_read)
However, I get the following RuntimeError:
File " ", line , in module
subset = snappy.GPF.createProduct('Subset', parameters, s1_read)
RuntimeError: org.esa.snap.core.gpf.OperatorException: The pixel region 'java.awt.Rectangle[x=0,y=0,width=0,height=0]' is invalid.
What do you think is happening and what can I do to solve the error?
Difficult to say what the problem is.
Either the region does not intersect with one of your products, but then the error message should actually be different or the source product is not healthy and its dimensions are zero.
Does this happen with every product or just with one of them?
INFO: org.esa.s2tbx.dataio.gdal.GDALVersion: GDAL 3.0.4 found on system. JNI driver will be used.
INFO: org.esa.s2tbx.dataio.gdal.GDALVersion: Installed GDAL 3.0.4 set to be used by SNAP.
INFO: org.esa.snap.core.gpf.operators.tooladapter.ToolAdapterIO: Initializing external tool adapters
usage: <file> <geometry-wkt>
./TEST.N1 "POLYGON((15.786082 45.30223, 11.798364 46.118263, 10.878688 43.61961, 14.72272742.85818, 15.786082 45.30223))"
An exception has occurred, use %tb to see the full traceback.
SystemExit: 1
INFO: org.esa.s2tbx.dataio.gdal.GDALVersion: GDAL 3.0.4 found on system. JNI driver will be used.
INFO: org.esa.s2tbx.dataio.gdal.GDALVersion: Installed GDAL 3.0.4 set to be used by SNAP.
INFO: org.esa.snap.core.gpf.operators.tooladapter.ToolAdapterIO: Initializing external tool adapters
INFO: org.esa.snap.core.util.EngineVersionCheckActivator: Please check regularly for new updates for the best SNAP experience.
Thanks for your prompt response. You’re right, I wrote the incorrect WKT string. The number 54.708 in the WKT string should be 64.708. This solves the original error.
However, I now get a new error:
INFO: org.esa.s2tbx.dataio.gdal.GDALVersion: GDAL 3.0.4 found on system. JNI driver will be used.
INFO: org.esa.s2tbx.dataio.gdal.GDALVersion: Installed GDAL 3.0.4 set to be used by SNAP.
INFO: org.esa.snap.core.gpf.operators.tooladapter.ToolAdapterIO: Initializing external tool adapters
INFO: org.esa.snap.core.util.EngineVersionCheckActivator: Please check regularly for new updates for the best SNAP experience.
Name Sensing Mode \
0 S1A_IW_SLC__1SDV_20180623T185856_20180623T1859...
1 S1A_IW_SLC__1SDV_20180717T185857_20180717T1859...
Product Type Polarization Height Width \
0 1SDV 1SDV 13662 69691
1 1SDV 1SDV 13662 69691
Band Names
0 [i_IW1_VH, q_IW1_VH, Intensity_IW1_VH, i_IW1_V...
1 [i_IW1_VH, q_IW1_VH, Intensity_IW1_VH, i_IW1_V...
INFO: org.esa.s2tbx.dataio.gdal.GDALVersion: Installed GDAL 3.0.4 set to be used by SNAP.
Traceback (most recent call last):
File "/home/huw/Dropbox/Scripts/insar_iceland.py", line 183, in <module>
subset = snappy.GPF.createProduct('Subset', parameters, s1_read)
RuntimeError: org.esa.snap.core.gpf.OperatorException
But I’m not sure at the moment if you can really get to the message of the Java exception.
Maybe you can investigate the exception you catch, which type is it and what attributes does it provide.
It could be that exceptions need to be handled differently in SNAPPY/JPY to provide more inforamtion.
If the above does not help I can only suggest to try your configuration on the command line with gpt and see if the error also occurs.
Regarding your second suggestion: configuration on the command line with gpt. Unfortunately, that goes beyond the scope of my programming knowledge. Do you know of tutorials where I can learn to use gpt on the command line?
Instead of creating the graph manually as described in the tutorial above, you can create template graph with the Graph Builder which can be adapted.
If you like you can use it directly in the GUI with the Batch Processing tool.
It would be helpful to have some guidance on when python and SNAP snappy should be preferred over GPT. Certainly snappy is useful when you want to do something that isn’t available from GPT using numpy, pandas, etc. Python complicates error handling and memory management. When running the same GPF processing steps on a list of input files, using GPT in a shell script that loops over the list of input files is generally better than using Python.
I started using computers when programs were entered using punched cards or paper tape, so access to a command-line was a major advance. Today, many users have very little command-line experience but have learned programming with python in an IDE. It is very useful to spend some time learning to use a comand-line environment. PowerShell on Windows, POSIX shells (bash, zsh, dash) on linux and macOS (or Windows via cygwin, Msys2, or WSL) are very useful for troubleshooting and batch processing.