1

I am doing mosaicing of 14 raster images (.img format) to completely cover a province.see original spatial location of separately opened tiles(image). Mosiac operation is completed but 4 tiles are mosiaced on wrong spatial location. whereas other 10 tiles are mosiaced accurately (see image). when i am opening separate files all files are getting opened at their origina spatial location. please help me to resolve this issue. I am using this code for mosaicing of 14 raster images.

import rasterio
from rasterio.merge import merge
from rasterio.plot import show
import glob
import os

dirpath = r"path"
outpath = r"outpath\Mosiac.tif"

search_criteria = "*.img"

q = os.path.join(dirpath, search_criteria)
print (q)
img_fps = glob.glob(q)
img_fps

src_files_to_mosiac = []

for sim in img_fps:
src = rasterio.open(sim)
src_files_to_mosiac.append(src)

src_files_to_mosiac

mos, out_trans = merge(src_files_to_mosiac)

show(mos, cmap='terrain')

with rasterio.open(outpath,"w", driver ='Gtiff',count=3,
            height= mos.shape[1],
            width= mos.shape[2],
            transform= out_trans,
            crs= src.crs,
            dtype= src.dtypes[0]) as dest:
dest.write(mos)
  • Are the far right four scenes in a different coordinate system? – Michael Stimson Feb 12 at 7:35
  • No the coordinate system is same i.e. UTM WGS 84 but the far right four scenes are in zone 51N. Rest of the scenes are in zone 50N. – Gopal Krishna Feb 12 at 7:40
  • 1
    Michael means just that, some of the images are in coordinate reference system UTM 50N and some are in 51N. It seems that your code handles them as if they were all in the same system. – user30184 Feb 12 at 7:54
  • 1
    Absolutely! UTM 50N is a different coordinate system to UTM 51N as they have a different central meridian. You will need to project the 4 right scenes into UTM 50N before attempting to mosaic. – Michael Stimson Feb 12 at 7:58
  • 1
    you could just transform them into a crs that is not bound to any zones and transform all the rasters to it. – Leo Feb 12 at 12:20
2

I got a solution. Converted all the files into lat long wgs84 CRS. By using the code given below-

import numpy as np
import rasterio
from rasterio.warp import calculate_default_transform, reproject, Resampling

dst_crs = 'EPSG:4326'

with rasterio.open('path/test.tif') as src:
transform, width, height = calculate_default_transform(
    src.crs, dst_crs, src.width, src.height, *src.bounds)
kwargs = src.meta.copy()
kwargs.update({
    'crs': dst_crs,
    'transform': transform,
    'width': width,
    'height': height
})

with rasterio.open('path/test_wgs84.tif', 'w', **kwargs) as dst:
    for i in range(1, src.count + 1):
        reproject(
            source=rasterio.band(src, i),
            destination=rasterio.band(dst, i),
            src_transform=src.transform,
            src_crs=src.crs,
            dst_transform=transform,
            dst_crs=dst_crs,
            resampling=Resampling.nearest)

This removed the barrier of different UTM zones and then using the code given in my question, mosaic image was generated. Actually this is quite obvious approach but i was trying to perform it in an automated manner. Converted above mentioned code into function, applied the function on all images through looping and got succeed.

Thanks everyone.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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