-1

I have dataset with images

Example image:

enter image description here

All images have GPS coordinates.

I need to stich this images in one TIFF using Python.

1 Answer 1

2

Use merge() from rasterio package. Use glob to list all your tiff files. Basically you want create a mosaic image.

See also: https://automating-gis-processes.github.io/CSC18/lessons/L6/raster-mosaic.html

# Imports
import rasterio
from rasterio.merge import merge
import glob
import os 

# File and folder paths
dirpath = r"location of tiffs"
out_fp = r"output"

# Make search criteria for glob
search_criteria = "*.tif"
q = os.path.join.(dirpath, search_criteria)

# List tiffs
tiff_lst = glob.glob(q)

# Initiate output mosiac
src_files_to_mosaic = []

# create list of open raster objects
    for tiff in tiff_lst:
       src = rasterio.open(tiff)
       src_files_to_mosaic.append(src)

# Merge
mosaic, out_trans = merge(src_files_to_mosaic)

# Copy the metadata
out_meta = src.meta.copy()
    
# Update the metadata
out_meta.update({"driver": "GTiff",
    "height": mosaic.shape[1],
    "width": mosaic.shape[2],
    "transform": out_trans,
    "crs": "+proj=utm +zone=35 +ellps=GRS80 +units=m +no_defs" })
    
# Write the mosaic raster to disk
with rasterio.open(out_fp, "w", **out_meta) as dest:
    dest.write(mosaic)
1
  • When merge: ValueError: negative dimensions are not allowed Oct 21, 2020 at 10:51

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