I have two files. One is PNG and the other is TIFF (or .jp2). I want to read geospatial data from the satellite image (.jp2 or .tif) and "put it in" the png file, then save it as a new tif.
I'm very new to working with raster data. I'm using rasterio and so far I've managed to get the meta of a tif file:
import rasterio as rio
tif = rio.open("TIFFile.tif")
png = rio.open("PNGFile.png")
tif_read = tif.read()
png_read = png.read()
tif_width = tif.width
tif_height = tif.height
tif_dim = (tif_width, tif_height)
png_width = png.width
png_height = png.height
png_dim = (png_width, png_height)
tif_meta = tif.meta
png_meta = png.meta
print("TIF META: ", tif_meta)
print("PNG META: ", png_meta)
Output:
TIF META: {'driver': 'GTiff', 'dtype': 'float32', 'nodata': None, 'width': 10980, 'height': 10980, 'count': 4, 'crs': CRS.from_epsg(32635), 'transform': Affine(10.0, 0.0, 699965.0,
0.0, -10.0, 4100035.0)}
PNG META: {'driver': 'GTiff', 'dtype': 'float32', 'nodata': None, 'width': 5490, 'height': 5490, 'count': 1, 'crs': None, 'transform': Affine(1.0, 0.0, 0.0,
0.0, 1.0, 0.0)}
I know I can create a new tif file using original tif's meta like this:
with rio.open('NEWTIF.tif', 'w', **tif_meta) as dst:
dst.write(tif_read.astype(rio.float32))
In light of this, basically, I'd like to know how to create a new tif file adding another tif's meta into a png file - merging png's raster data with tif's geospatial data.
I've been searching for this but couldn't understood what I've found in SO:
How to convert png files to geotiff https://gis.stackexchange.com/questions/371065/apply-same-coordinate-system-to-raster-image-and-geojson-with-rasterio https://gis.stackexchange.com/questions/379027/georeferencing-of-png-and-convert-to-tiff-using-python
How to achieve this?
First of all is important to say that from your post seems like the images do not have the same shape and for that reason it might be more challenging and require more processing.
However, i'll answer the question as you defined it on title of your post- how to read those two files and save them as tiff. In order to do this I would read both images as numpy array,stack them together and then georeference them with rasterio.
so you can try seomthin similar to this:
import rasterio
import numpy as np
tif = rio.open("TIFFile.tif")
png = rio.open("PNGFile.png")
tif_read = tif.read()
png_read = png.read()
#stack the arrays to have two bands
img=np.stack([tif_read,png_read])
#write teh results
with rasterio.open('img.tiff',
'w',
driver='GTiff',
height=img.shape[1],
width=img.shape[2],
count=2,#numebr of bands
dtype=img.dtype,
crs=tif.crs,
nodata=None, # change if data has nodata value
transform=tif.transform) as dst:
dst.write(img[0], 1)
dst.write(img[1], 2)
That should save the two images as new tiff with two bands. However for this to work you will need to have the same size for both images which seems liek you don't have now. If you provide more data regard the files you have- are they overlap? do they have different spatial resolution? why their sizes are different?what is your goal? maybe I could help more.