5

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)
7
  • Are the far right four scenes in a different coordinate system? Feb 12, 2019 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. Feb 12, 2019 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, 2019 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. Feb 12, 2019 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, 2019 at 12:20

1 Answer 1

4

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.

1
  • Can you share the function?
    – hhh
    Oct 29, 2020 at 7:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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