I am merging datasets to a memory file using rasterio. However I can't get the memory file to take on the profile attributes passed in as dst_kwds. I get the desired result when I write the dataset to disk as below:

profile = ds.profile

# create dataset as file on disk
rasterio.merge.merge(datasets, dst_path="test.jp2", dst_kwds=profile)
#open and print profile
test_ds = rasterio.open("test.jp2")

{'driver': 'JP2OpenJPEG', 'dtype': 'uint8', 'nodata': None, 'width': 40000, 'height': 10000, 'count': 1, 'crs': CRS.from_epsg(7856), 'transform': Affine(0.15, 0.0, 294394.575, 0.0, -0.15, 6276235.425000001), 'blockxsize': 1024, 'blockysize': 1024, 'tiled': True}

But when I use a memory file it doesn't take on profile as I would expect:

# create dataset file in memory
memfile = MemoryFile()
rasterio.merge.merge(datasets, dst_path=memfile, dst_kwds=profile)
mem_ds = memfile.open(**profile)

{'driver': 'JP2OpenJPEG', 'dtype': 'uint8', 'nodata': None, 'width': 40000, 'height': 10000, 'count': 1, 'crs': None, 'transform': Affine(1.0, 0.0, 0.0, 0.0, 1.0, 0.0), 'blockxsize': 1024, 'blockysize': 1024, 'tiled': True}

I have tried many variations however I can't get the memory file to take on the profile attributes. I understand I could manually assign the profile attributes however I feel there could be a quicker way.

  • At least it is an impossible combination to use compress='lzw' with JPEG2000.
    – user30184
    Oct 6, 2021 at 14:04
  • 1
    I am not sure you can assign a memfile object to the dst_path param of the merge function.
    – diegus
    Oct 7, 2021 at 9:15
  • I tried dropping the compression and still no luck. I might be assuming too much about memoryFile thinking that it could could be used as a drop in replacement for a real file.
    – g0atm1lk
    Oct 8, 2021 at 9:33

1 Answer 1


As per docs, You need to pass str or PathLike as parameter to dst_path. In this case it would be memfile.name. Under the hood vsimem handles the file in-memory.

Also, you do not need to pass profile again when opening the in-memory raster.

memfile = MemoryFile()
rasterio.merge.merge(datasets, dst_path=memfile.name, dst_kwds=profile)
mem_ds = memfile.open()

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.