Is it possible to subset the image with the polygon shape (more than four edges) instead of polygon bounds using subset-operator/snappy?

I just started using snappy. I am trying to subset the image with a polygon, but the subset operator clipping with bounds of the polygon. Is it possible to subset the image with the polygon shape (more than four edges) instead of polygon bounds using subset-operator/snappy?

And where/how I can check the list of parameters in the SNAP operators?

Thank you.

Images are stored as 2-D arrays, so the actual stored data will always be a rectangle. You can crop to the polygon bounds and set pixels outside the polygon to a missing-value code.

The SNAP Help tab often provides a good description of the operators. In a terminal you can use gpt <Operator> -h.

It is usually easiest to start from an existing snappy script. The .snap/snap-python/snappy directory has example and test scripts. A Forum search can often find posts where a specific operator is discussed.

1 Like

Yes, this is possible. You can specify a polygon in WKT-format.
For example with gpt on the command line:


The ‘geoRegion’ parameter can also be used when using snappy.
But in the end always the bounding box of this region is used. Because in the end a raster needs to be created and rasters are rectangular.

There is the Land/Sea Mask operator which can mask the area outside of the polygon after the subsetting.
See:

1 Like

Thank you so much @gnwiii @marpet for your quick response

I tried clipping with the ‘Land-Sea-Mask’ operator, but I was getting the error. I attached the code and error

I transform the coordinates of the polygon to the same projection as a tile and I created wkt using geopandas.

Code:

# Clipping with Land-Sea-Mask tool
# Reading
filename = "./data/sentinel_download/S2A_MSIL1C_20220108T083331_N0301_R021_T34HBJ_20220108T103237_resampled_10m.dim"
product = ProductIO.readProduct(filename)

fields = gp.read_file('./data/boundaries/file_utm.geojson')
field = fields['geometry'].iloc[0]
field_wkt = field.to_wkt()

# field_wkt = snappy.WKTReader().read(field_wkt)

parameters = HashMap()
# parameters.put('landMask', False)
# parameters.put('useSRTM', True)
parameters.put('geometry', field_wkt)

test = GPF.createProduct('Land-Sea-Mask', parameters, product)

write_format = 'GeoTIFF-BigTIFF'
ProductIO.writeProduct(test , './data/sentinel_download/subset/subset_LSM', write_format)

print("Done!")

Error:

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-131-27ba33f30a29> in <module>
     16 parameters.put('geometry', field_wkt)
     17 
---> 18 test = GPF.createProduct('Land-Sea-Mask', parameters, product)
     19 
     20 write_format = 'GeoTIFF-BigTIFF'

RuntimeError: org.esa.snap.core.gpf.OperatorException: expression: Undefined symbol 'MULTIPOLYGON (((267619.7366652835626155 6373269.0510210823267698, ..)))'. due to Undefined symbol 'MULTIPOLYGON (((267619.7366652835626155 6373269.0510210823267698, 
..)))

When I try to use snappy WKTReader(), I am getting the following error

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-148-0bc8028329fd> in <module>
     16 parameters.put('geometry', field_wkt)
     17 
---> 18 test = GPF.createProduct('Land-Sea-Mask', parameters, product)
     19 
     20 write_format = 'GeoTIFF-BigTIFF'

RuntimeError: org.esa.snap.core.gpf.OperatorException: Operator 'CreateLandMaskOp': Value for 'Vector' must be of type 'String'.

The ‘geometry’ parameter is a bit confusing. You need to specify the name of a vector data node.
This needs to be created/imported first. To do this the Import-Vector operator needs to be used.
The format of the vector file should ESRI shapefile.

You can find example code here: