- Serializing and deserializing rasterio object
Re: Serializing and deserializing rasterio object
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.
'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)
Join email@example.com to automatically receive all group messages.