10

Is there in Rasterio any way to create a mosaic?

I tried to google it, but I couldn't find anything, maybe someone else already used it.

I want to use some open source possibilities, I already know of possibility to use gdalmerge, gdalwarp.

5
  • 1
    That would be merge from the rasterio.merge module, or rio merge from the command line. Commented Jul 11, 2017 at 16:50
  • Thank you. I like the vision of using CLI however I found only 'rio-merge-rgba', is there possibility that 'rio-merge' is over?
    – J.Coo
    Commented Jul 11, 2017 at 17:41
  • 1
    It's rio merge without the dash. rio --help will give you the list of all CLIs, and rio merge --help will give you more specific help for that command in particular. Commented Jul 11, 2017 at 17:49
  • Thank you very much, you are really helping me. Do you maybe know something about resampling, please? In rio-merge-rgba I read it doesn't work with bilinear and there is forced nearest neigbour.
    – J.Coo
    Commented Jul 11, 2017 at 19:04
  • 1
    If you need to resample then I suggest you use gdalwarp instead. It does mosaicking, reprojecting/resampling all at once. Commented Jul 11, 2017 at 19:45

3 Answers 3

14
import rasterio
import numpy as np
from rasterio.merge import merge
from rasterio.plot import show

src1 = rasterio.open('/path/to/your/raster1')
src2 = rasterio.open('/path/to/your/raster2')

# Taking a peek to make sure these are the rasters you want...
show(src1)
show(src2)

srcs_to_mosaic = [src1, src2]

# The merge function returns a single array and the affine transform info
arr, out_trans = merge(srcs_to_mosaic)

show(arr) # Check to make sure the merge looks good.

I was just looking for a similar technique and found this method based on the rasterio/merge.py function given here.

You can check out my example with two orthomosaic images here.

However, I'm not sure this is the best method...no doubt the dude himself @sgillies has more insight into this!

3

Rasterio's rio warp command (see https://github.com/mapbox/rasterio/blob/master/rasterio/rio/warp.py) can be used to make image mosaics. With a little work you could use that function in Python code, not just as a command line program.

1

Just a note, I was repeatedly receiving the following error:

Traceback (most recent call last):
  File "<pyshell#11>", line 1, in <module>
    mosaic, output = merge(raster_to_mosaic)
TypeError: 'module' object is not callable

I had been importing merge as:

from rasterio.merge import merge

For some reason, as soon as I changed my package import call to the following, it worked without issue:

from rasterio import merge

mosaic, ouput = merge.merge(raster_to_mosaic)

p.s I would've commented this but alas I am but a low-level contributor

I hope this saves someone the hour it took me to work-around!

Your Answer

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

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