Geographic Information Systems Asked on March 20, 2021
I’m looking to merge 3 imagery bands that are saved as separate files. The file format is NITF. The reason they are saved as NITF is because they have RPC metadata embedded into the images. I would like to combine the NITF images so the output image retains the RPC metadata. I will be doing this a lot so I would like to build a script either through Python or GDAL so that I don’t have to do this manually. Unfortunately I’m very much a novice when it comes to scripting, and after scouring the internet I have hit a road block. I was able to successfully merge the images in the desired output using GDAL, but the RPC metadata did not transfer into the output image.
Here’s the code I have so far:
gdal_merge.py -o output.ntf -of NITF -seperate red_band.ntf green_band.ntf blue_band.ntf -co PHOTOMETRIC=RGB
This should do the trick in Python. If you don't have rasterio install I suggest to get it through conda.
import numpy as np
import rasterio
data = []
input_images = [path1, path2, path3]
for img in input_images:
# Open the image file, then read the data in memory as np.ndarray
img_meta = rasterio.open(img)
img_data = img_meta.read()
data.append(img_data)
# stack along the first axis all data
new_data = np.vstack(tuple(data))
# This assumes that all of them have the same crs, transform and nodata value
newprofile = {
'count': new_data.shape[0],
'crs': img.crs,
'dtype': 'uint8',
'driver': 'GTiff', # You can pick other formats that suits you. There is read-write support for NITF if you prefer
'transform': img.transform,
'height': new_data.shape[1],
'width': new_data.shape[2],
'blockxsize': 256,
'tiled': True,
'blockysize': 256,
'nodata': img.nodata
}
# Initialise a new GeoTiff and write the data
with rasterio.open(outpath, 'w', **newprofile) as dst:
dst.write(new_data)
Regarding metadata I'm not sure how you want to combine them, but you can read them using img.tags()
. This will return a dictionary, that you can write to the new image using the dst.update_tags()
. Please note that this last function is only available if you open the image in 'w' or 'r+' mode
Answered by Alessio Arena on March 20, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP