TransWikia.com

Clipping ENVI files loses information in header (.hdr) file

Geographic Information Systems Asked by Fernando Romero Galvan on July 17, 2021

I have created a command-line operated python-script to cut AVIRIS imagery with polygons. I do this by using rasterio to load the imagery and fiona to load the polygon.

The AVIRIS imagery comes in two parts, a file with no extension and a header (.hdr) file, the header file and data(?)-file share a file-name, so I assume when I open the image with rasterio it knows to look for the respective (.hdr) file and auto-fills the driver parameter.

So far, my script works as intended, I can clip the AVIRIS imagery. However, my problem is that I lose the wavelength each band belongs to in the (.hdr) file, along with other tags. I can call the full set of tags of the original image, and in fact, can print them out by calling src.tags(); held in ‘src_tags’ variable below. Where the tags are lost is on writing, meta_data on print shows full set of tags, but I can’t figure out how to get rasterio to write a (.hdr) file with all tags.

Since the (.hdr) is just a text file, I have considered writing code that appends the missing flags, but I worry if that is a bad approach since it makes my code less rigid; what if new tags are included?

Below is my code:

import rasterio as rio
import rasterio.mask import mask
import fiona

image = "filepath/to/AVIRIS/image"
poly = "filepath/to/some/geometry"
outputname = "test"

def masker(image, poly, outputname):

    image_name = ("cropped_%s" % outputname)

    with fiona.open(poly, "r") as geometries:
        geometry = [feature["geometry"] for feature in geometries]

    with rio.open(image, driver="ENVI") as src:
        out_image, out_transform = rio.mask.mask(src, geometry, crop=True)
        out_meta = src.meta
        src_tags = src.tags(ns='ENVI') 
        #print(src_tags) #<- prints out full content of (.hdr) file
        out_meta = {**out_meta, **src_tags}

    out_meta.update({
        "height" : out_image.shape[1],
        "width" : out_image.shape[2],
        "transform" : out_transform
    })

    with rio.open(image_name, 'w', **out_meta) as dest:
        dest.write(out_image)
         #dest.update_tags(**src_tags) #doesn't work

masker(image, poly, outputname)

I did notice that my full-tags get saved onto a 3rd file, a (.xml) file, but this is not useful.

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP