spectral icon indicating copy to clipboard operation
spectral copied to clipboard

Read PDS4 images ? Use rasterio ?

Open kvngvikram opened this issue 11 months ago • 3 comments

Hi, I am trying to work with pds4 images and realized they cannot be read in a straight forward fashion.

Yeah, I can open the same image in envi and then save them in envi format, but that's just having redundant copies and steps. It would be good to have a way to directly read the original files (a .tif and .xml file each for single image). I have played around with pds4_tools and tried to define a Bipfile from metada extracted by pds4_tools. Well, it didn't seem elegant, and my wishful thinking wanted to extract projection info into some standard format and save it as metadata.

Now, rasterio has a PDS4 driver and is capable to read .tif files and extract CRS data. I am guessing its better to have rasterio read them, instead of me understanding and implementing it myself. Let rasterio deal with different image formats.

So,,.. I am just here to discuss,. Am I missing a feature in spectral that's already present? How easy it it to stich together a code that will use rasterio?

kvngvikram avatar Mar 11 '25 06:03 kvngvikram

Hello,

There is not currently support for pds4 files in spectral. Are you just trying to read pds4 image files and metadata or do you need to write those formats as well? If you just need to read them, you could just put a wrapper around spectral.open_image, which uses rasterio when the specified file is xml or calls the original function otherwise. If you come up with a working solution for reading (and optionally writing) psd4 files, I'd be happy to integrate it.

tboggs avatar Mar 16 '25 14:03 tboggs

So I have been trying out things..

I was able to load rasterio image as a numpy array and get metadata. Now I created params object with that metadata, initiated a BSQ file from

bsqfile = spectral.io.bsqfile.BsqFile(params)
spy_imagearray = spectral.image.ImageArray(numpy_image_array, bsqfile)

Now I got the image as Imagearray object, which I assume will work like a normal spyfile for further analysis. Note that here in params, I set the offset as 0 or None.

But, I see that loading as a memmap is harder, because I have no way to get the offset from rasterio. Rasterio internally takes care of where the header ends and where the image starts. And it doesn't provide that info in its metadata.

I once tried just calculating the offset with image shape/size data type and the total .tif file size, and I was able to get the offset correctly. But its only the one image I am using.

So, any guidance on calculating offsets ? Can I do the hack ?

I can try playing around more before i submit any pull_request.

kvngvikram avatar Mar 21 '25 07:03 kvngvikram

Can you save different size subregions of an image using rasterio to see if the header size is fixed by subtracting image data size from the total file size/

tboggs avatar Mar 22 '25 15:03 tboggs