Re: Serializing and deserializing rasterio object


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

Join main@rasterio.groups.io to automatically receive all group messages.