1

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
profile.update(
    dtype=rasterio.uint8,
    count=1,
    compress='lzw')

# 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")
print(test_ds.profile)

{'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)
print(mem_ds.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.

3
  • At least it is an impossible combination to use compress='lzw' with JPEG2000.
    – user30184
    Commented 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
    Commented 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
    Commented Oct 8, 2021 at 9:33

1 Answer 1

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()
print(mem_ds.profile)

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.