Reading bands and Tie Point Grids in snappy

Hi there,

I have noted a difference in the output between raster bands and tie point grids, and was wondering how I could get around it. Using snappy, I do the following to extract a band to a raster:

# Open product
prod = ProductIO.readProduct(indir)

# Get width and height
w = prod.getSceneRasterWidth()
h = prod.getSceneRasterHeight()

     
# Initialise 
array = np.zeros((w,h),dtype=np.float32)  # Empty array

# Extract raster
currentband = prod.getBand(stringname)
bandraster = currentband.readPixels(0, 0, w, h, array)

With this method, bandraster is returned as an array. If I look at the SNAP Engine API the documentation for the Tie Point Grids says the following:

readPixels(int x, int y, int w, int h, float pixels, ProgressMonitor pm)
Retrieves an array of tie point data interpolated to the product with and height as float array.

However, using the same method as above, replacing the line:

currentband = prod.getBand(stringname)

with

currentband = prod.getTiePointGrid(stringname)

I don’t get an array anymore but instead a Java object:

In: tiepointraster
Out: [F(objectRef=0x9acec50)
In: tiepointraster.getClass()
Out: java.lang.Class(objectRef=0x9acec20)

How can I retrieve the Tie Point Grid as an array?

Cheers,

Hi Maxim,

currentTpGrid = prod.getTiePointGrid(stringname)

this return the tiepoint grid. Afterwards you can call

currentTpGrid.readPixels(0, 0, w, h, array)

You can generify this by

currentRaster = prod.getRasterDataNode(stringname)
currentRaster.readPixels(0, 0, w, h, array)

Doing it like this you can treat both bands and tie-point grids in the same way, as long you don’t need to specify methods of these classes.

1 Like

Hi Marco,

You can generify this by

currentRaster = prod.getRasterDataNode(stringname)
currentRaster.readPixels(0, 0, w, h, array)

Thanks for the tip, this method populates the array with the values from the TiePointGrid.

currentTpGrid = prod.getTiePointGrid(stringname)
this return the tiepoint grid. Afterwards you can call

currentTpGrid.readPixels(0, 0, w, h, array)

However, this method doesn’t work for me: the array remains full of zeros and the call returns a java object. See below:

IN: currentTpGrid.readPixels(0,0,w,h,array)
Out: [F(objectRef=0x9c329a8)

Hi there,

If this is of any use to other people, I found a way of reading in the TiePointGrids: