Open-Source-Spatial-Clean-Cooking-Tool / OnStove

This repository contain the general code for the Open Source Spatial Clean Cooking Tool OnStove
MIT License
7 stars 8 forks source link

Invalid value encountered in cast #349

Closed camiloramirezgo closed 1 year ago

camiloramirezgo commented 1 year ago

This error is appearing when calling the raster_to_dataframe method of the model.py, when entering the interpolation method. Apparently, it is caused by the call to the fillnodata() function of the rasterio.fill module. It happens only in this two places:

Image

and Image

babakkhavari commented 1 year ago

We have nan in the mask:

image

But according to the documentation it should only be 0s and 1s? (https://rasterio.readthedocs.io/en/stable/api/rasterio.fill.html)

mask (numpy ndarray or None) – A mask band indicating which pixels to interpolate. Pixels to interpolate into are indicated by the value 0. Values > 0 indicate areas to use during interpolation. Must be same shape as image. This array always takes precedence over the image’s mask (see above). If None, the inverse of the image’s mask will be used if available.

From line 1454 in model.py (or close by depending on what each branch looks like) can we not do the following:

layer = layer.data.copy()
if fill_nodata_method:
    mask = layer.copy().astype(float)
    mask[mask == nodata] = np.nan
    if np.isnan(mask[self.rows, self.cols]).sum() > 0:
        mask[~np.isnan(mask)] = 1
        base = self.base_layer.data.copy()
        base[base == self.base_layer.meta['nodata']] = np.nan
        rows, cols = np.where(np.isnan(mask) & ~np.isnan(base))
        mask[rows, cols] = 0
        mask=np.nan_to_num(mask, 0) <--- ADDED
        if fill_nodata_method == 'interpolate':
            layer = fillnodata(layer, mask=mask,
                               max_search_distance=100)