0

I am using snappy for processing SAR data, but it seems that snappy uses its own 'product' objects for storing raster data. But i only need the resulting array of pixel values after I finish working with it. How can I retrieve such array from speckle variable in the code below?

import snappy
import pathlib
from configparser import ConfigParser
import os

config = ConfigParser()
config.read('config.ini')
projLibPath = config['PATHS']['proj_lib']
gdalDataPath = config['PATHS']['gdal_data']
os.environ['PROJ_LIB'] = projLibPath
os.environ['GDAL_DATA'] = gdalDataPath

def Filter (inputRasterPath):
    print('------------Filter------------')
    print('Reading the image file..')
    read = snappy.ProductIO.readProduct(inputRasterPath)
    outputRasterPath = './Filter/Filter'+  pathlib.Path(inputRasterPath).suffix
    print(read)

    parameters = snappy.HashMap()
    parameters.put('filter', 'Lee')
    parameters.put('filterSizeX',3)
    parameters.put('filterSizeY',3)
    print('Filtering...')
    speckle = snappy.GPF.createProduct('Speckle-Filter', parameters, read)
    print('Filter successfully', speckle)

    print('Writing the filtered image...')
    snappy.ProductIO.writeProduct(speckle, outputRasterPath, 'GeoTIFF')
    print('Done')
1

1 Answer 1

0

Thanks to user2856 i resolved this problem by adding

band = speckle.getBand('band_1')
w = band.getRasterWidth()
h = band.getRasterHeight()
band_data = np.zeros(w * h, np.float32)
band.readPixels(0, 0, w, h, band_data)
speckle.dispose()
band_data.shape = h, w.  

I have a little problem with np.float32: when i change data type to, for example, np.uint16 i have an error :

    band.readPixels(0, 0, w, h, band_data)
RuntimeError: no matching Java method overloads found

So i decided to change the data type of a raster later, using band_data.astype(src.dtypes[0])

The final code looks like this:

import numpy as np
import rasterio
import snappy
import pathlib
from configparser import ConfigParser
import os



config = ConfigParser()
config.read('config.ini')
projLibPath = config['PATHS']['proj_lib']
gdalDataPath = config['PATHS']['gdal_data']
os.environ['PROJ_LIB'] = projLibPath
os.environ['GDAL_DATA'] = gdalDataPath

def Filter (inputRasterPath):
    print('------------Filter------------')
    print('Reading the image file..')
    read = snappy.ProductIO.readProduct(inputRasterPath)
    outputRasterPath = './Filter/Filter'+  pathlib.Path(inputRasterPath).suffix
    print(read)

    parameters = snappy.HashMap()
    parameters.put('filter', 'Lee')
    parameters.put('filterSizeX',3)
    parameters.put('filterSizeY',3)
    print('Filtering...')
    speckle = snappy.GPF.createProduct('Speckle-Filter', parameters, read)
    print('Filter successfully', speckle)

    band = speckle.getBand('band_1')
    w = band.getRasterWidth()
    h = band.getRasterHeight()
    band_data = np.zeros(w * h, np.uint16)
    band.readPixels(0, 0, w, h, band_data)
    speckle.dispose()
    band_data.shape = h, w

    print(band_data)
    print(band_data.shape)

    src = rasterio.open(inputRasterPath)

    profile = {
        'driver': 'GTiff', 
        'dtype': src.dtypes[0], # np.uint8, np.uint16 etc.
        'nodata': src.nodata, 
        'width': src.shape[1], 
        'height': src.shape[0],
        'count': 1, # bands count
        'crs': src.crs, 
        'transform': src.transform, 
        'tiled': False,
        'compress': None,
        }

    print('Saving the output...')
    with rasterio.open(outputRasterPath,'w',**profile) as dst: 
        dst.write(band_data.astype(src.dtypes[0]),1) # (np.array, number of bands?)
    src = None

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.