Date
1 - 2 of 2
Open a raster file with read and write permissions in rasterio
Sean Gillies
Hi, On Tue, Oct 2, 2018 at 8:14 AM Amine Aboufirass <amine.aboufirass@...> wrote:
Currently, the datasets in a MemoryFile can only be opened in read or write modes. Instances created with no initial bytes have their mode fixed to "w" and instances created with initial bytes have their mode fixed to "r". I could explore changing these to "w+" and "r+" instead for the next version of Rasterio. That said, few data formats can be used in this way. The ascii grid format in your example, for one, cannot be updated in place by GDAL or Rasterio. It has a "no" in the "Creation" column of the table in https://www.gdal.org/formats_list.html and like other such formats is read-only or write-on-copy only. -- Sean Gillies
|
|
Amine Aboufirass <amine.aboufirass@...>
I have already asked this question in GIS stack exchange and stack overflow but received no response:
I am trying to write a function which will help me construct a `rasterio` memory file and return the dataset reader object so I can then play around with it as needed. I am trying to avoid `with` statements because I would like to allow the user to read and write as they deem necessary. This is what I have so far:
from rasterio import mask, features, warp
from rasterio.io import MemoryFile
import rasterio
import numpy as np
def create_memory_file(data, west_bound, north_bound, cellsize, driver='AAIGrid'):
#data is a numpy array
dtype = data.dtype
shape = data.shape
transform = rasterio.transform.from_origin(west_bound, north_bound, cellsize, cellsize)
memfile = MemoryFile()
dataset = memfile.open(driver=driver, width= shape[1], height = shape[0], transform=transform, count=1, dtype = dtype)
dataset.write(data,1)
return dataset
def close_memory_file(memfile):
memfile.close()
data = np.array([[1,2,3], [4,5,6], [7,8,9]])
memfile = create_memory_file(data, 0, 2, 0.5)
memfile.read(1)
The last line throws an error:
Traceback (most recent call last):
File "D:/11202750-002_RA2CE/Basis/common.py", line 214, in <module>
memfile.read(1)
File "rasterio\_io.pyx", line 209, in rasterio._io.DatasetReaderBase.read
rasterio.errors.UnsupportedOperation: not readable
The memfile apparently only has write permissions because of the following:
>>> memfile
<open BufferedDatasetWriter name='/vsimem/3c0572dd-a295-4f2b-a65d-22a404dd5d0f.' mode='w'>
>>> memfile.mode
'w'
I have tried various combinations of adding `mode=r+`, `permissions=r+` to my `memfile.open()` call but it won't allow this.
How can I add the read/write mode at the level of the `open` statement?
[1]: https://gis.stackexchange.com/questions/297562/open-a-raster-file-with-read-and-write-permissions-in-rasterio
|
|