Is it possible to subset using the center latitude and longitude coordinates and pixel size?

Dear all,

I want to subset SAR images to a size of 224x224 in order to analyze them using deep learning.
I understand that I can subset using two coordinates.
However, I have to give the coordinates either in latitude and longitude or in pixels.
I’m worried that if I specify only the latitude and longitude, the pixel size may not be 224x224.
Therefore,I want to subset by specifying the center coordinate and the size of 224x224.
Is there a way to achieve this using SNAP or snappy?

All the best,

Zuiko

image

In SNAP Desktop there is no such option.
In snappy you can use helper methods to achieve it.

GeoCoding gc = source.getSceneGeoCoding();
PixelPos pPos = gc.getPixelPos(new GeoPos(lat, lon), null);
Rectangle pixelRegion = new Rectangle(new Point((int)pPos.getX(), (int)pPos.get(Y)));
pixelRegion.grow(112, 112);
Map<String, Object> parameters = Map.of("referenceBand", referenceBand, "region", 
pixelRegion, "copyMetadata", true);
subset = GPF.createProduct("Subset", parameters, source);

I hope you can translate this into Python. I was too lazy to do it. Sorry.
I also haven’t tested the code snippet if it works. But in general, it should do.

1 Like

In any case you will have to project your SAR images into map coordinates for the coordinate-subsetting to work. The coordinate-information in unprojected SAR imagery can be vey imprecise or even wrong.

Thank you so so so much!!
With the help of Chat GPT, I created the python code.
I would be happy if I could be of help to someone.

def rectangle_subset(product):
    # define
    lat = 0.0
    lon = 0.0
    reference_band = "Amplitude_VV"

    # geo to pixel
    gc = product.getSceneGeoCoding()
    geo_pos = GeoPos(lat, lon)
    p_pos = gc.getPixelPos(geo_pos, None)

    # set rectangle
    pixel_region = Rectangle(int(p_pos.getX()), int(p_pos.getY()), 0, 0)
    pixel_region.grow(112, 112)

    # set parameters
    parameters = HashMap()
    parameters.put("referenceBand", reference_band)
    parameters.put("region", pixel_region)
    parameters.put("copyMetadata", True)

    # subset
    subset = GPF.createProduct("Subset", parameters, product)

    return subset

# read data
path_to_sentinel_data = "SAR_data_path"
product = ProductIO.readProduct(path_to_sentinel_data)

# do subset
product_subset = rectangle_subset(product)
1 Like

Thanks for the reply.
I ran the code without thinking and was happy.

I ran “Terrain-Correction” to add coordinate information.
After that, when I ran “rectangle_subset”, it was far away from the specified location.

I think the problem is the following code:
gc = product.getSceneGeoCoding()

If I pass the “product” before running “Terrain-Correction”, the result matches the specified location.
I don’t understand “getSceneGeoCoding()” correctly.
I would appreciate it if you could tell me the correct flow.

First, great that you have managed to turn the code into working Python code.
But your observation is strange.
The method getSceneGeoCoding() returns the object which encodes how the scene is geo-coded. It provides means to get the pixel position for a geo-coordinate and vice versa.

Have you checked the result of Terrain-Correction in SNAP Desktop? Can you observe something odd?

In conclusion it was a simple coding mistake on my part.
The code I shared is correct but in my code I was giving a different “product” in the function.
My apologies.

1 Like