mdbartos / pysheds

:earth_americas: Simple and fast watershed delineation in python.
GNU General Public License v3.0
717 stars 196 forks source link

pickling a grid, lost methods? #133

Closed danchurch closed 2 years ago

danchurch commented 3 years ago

Hello! Thanks for your amazing package and all the work you do maintaining it.

I have a question about pickling grid objects. When I pickle a grid object, then read it in later, I seem to lose some methods from the grid object.

A toy example, using the public example data here:

from pysheds.grid import Grid
import pickle

pathSD = ('20190109125130_1063922483.tif')
grid = Grid.from_raster(pathSD, data_name='dem')

pickle.dump(grid, open("testPicklePyshed.p", "wb"))

grid2 = pickle.load(open('testPicklePyshed.p', 'rb'))

grid.view('dem') ## works as expected

grid2.view('dem') ## returns error

The error returned is:

Traceback (most recent call last): File "", line 1, in File "/usr/local/lib/python3.8/dist-packages/pysheds/grid.py", line 410, in view nodata = data.nodata File "/usr/local/lib/python3.8/dist-packages/pysheds/view.py", line 42, in nodata return self.viewfinder.nodata AttributeError: 'Raster' object has no attribute 'viewfinder'

All the errors I have gotten so far from this relate somehow to the absence of the 'viewfinder' attribute in the unpickled grid. I hope this is pertinent to pysheds and not just a user error on my part with pickle. Anyway, thanks in advance.

mdbartos commented 2 years ago

Greetings,

I have removed datasets as named attributes of Grid in v0.3. I'd recommend outputting Raster datasets to collections of raster images (i.e. .tiff) or ascii text files if desired.