Python Forum
Keep inner Values of 2D array - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Data Science (https://python-forum.io/forum-44.html)
+--- Thread: Keep inner Values of 2D array (/thread-34376.html)



Keep inner Values of 2D array - timste - Jul-26-2021

Hi
I have a tiff with lakes, which I converted into a 2D array. I would like to keep the outline of the lakes in a 2D array.

import rasterio
import numpy as np

with rasterio.open('myfile.tif') as dtm:
    array = dtm.read(1)

array[array>0] = 1
array = array.astype(float)
array[array==0] = np.nan
My array looks like this now, a lake can be seen in the upper right corner:


     [[      nan       nan       nan ... 2888.001  [b]2877.458  2867.5798[/b]]
     [      nan       nan       nan ... 2890.188 [b] 2879.2876 2869.0415][/b]
     [      nan       nan       nan ... 2892.2622 2880.9907 2870.4985]
     ...
     [      nan       nan       nan ...       nan       nan       nan]
     [      nan       nan       nan ...       nan       nan       nan]
     [      nan       nan       nan ...       nan       nan       nan]]
I wish to keep the outline of the lakes, thus I have to set all values to nan, which are NOT located next to a nan (marked in bold).

I have tried:

array[1:-1, 1:-1] = np.nan
However, this converts ALL inner values of the entire array to nan, not just the inner values of the lakes.

If you know of a completely different way how to keep the outline of the lakes (maybe with rasterio), I would also be thankful. I hope I made clear what I mean with inner values of the lakes. Important: A lake can appear anywhere in the array not just in a corner

Tim