Topics

Serializing and deserializing rasterio object


Juliano
 

Hi all
Is there an easy way to serialize and deserialize rasterio object?
Thanks!


Sean Gillies
 

Hi,

On Mon, Sep 16, 2019 at 5:36 AM Juliano <juliano.ecc@...> wrote:
Hi all
Is there an easy way to serialize and deserialize rasterio object?
Thanks!

Do you mean a dataset object returned by rasterio.open()? We have left them not pickleable for the same reason Python file objects aren't pickleable: in the implementation there is an OS file descriptor (or several) that isn't serializable.

As an approximation, you could could save the dataset's filename or URL (its .name attribute) and the creation or opening options (in its .options attribute) and then call rasterio.open() when you want to "deserialize".
 
--
Sean Gillies


Juliano
 

Yes Sean, I mean the object returned by rasterio.open().

I'm doing a remote function that crops a small polygon from a large image, and returns only that cropped region.
The function is not able to persist the outcome on disk, so I have to return it as a JSON.

I was wondering if could save some time by not serializing and deserializing by myself.
The numpy matrix (from .read()) I can do it easily, but the metadata uses some classes in its properties (crs and transform) and json.dumps() complains about that and doesn't let me generate a dictionary.

Metadata

{'driver': 'GTiff',
 'dtype': 'uint16',
 'nodata': None,
 'width': 22,
 'height': 28,
 'count': 12,
 'crs': CRS.from_epsg(32622),
 'transform': Affine(30.0, 0.0, 606315.0,
        0.0, -30.0, -2740815.0)}


Sean Gillies
 

On Mon, Sep 16, 2019 at 11:53 AM Juliano <juliano.ecc@...> wrote:
Yes Sean, I mean the object returned by rasterio.open().

I'm doing a remote function that crops a small polygon from a large image, and returns only that cropped region.
The function is not able to persist the outcome on disk, so I have to return it as a JSON.

I was wondering if could save some time by not serializing and deserializing by myself.
The numpy matrix (from .read()) I can do it easily, but the metadata uses some classes in its properties (crs and transform) and json.dumps() complains about that and doesn't let me generate a dictionary.

Metadata
{'driver': 'GTiff',
 'dtype': 'uint16',
 'nodata': None,
 'width': 22,
 'height': 28,
 'count': 12,
 'crs': CRS.from_epsg(32622),
 'transform': Affine(30.0, 0.0, 606315.0,
        0.0, -30.0, -2740815.0)}
There's no protocol for making classes JSON-serializable (though there may be some day, see https://bugs.python.org/issue27362), so we have to coerce two of those values to built-in Python types (str and list). This is how we do it in the rio-info program:

    metadata = dict(**src.profile)
    metadata.update(crs=metadata["crs"].to_wkt(), transform=list(metadata["transform"]))

--
Sean Gillies


Juliano
 

Thank you!